1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/importer/profile_writer.h"
6
7 #include <stddef.h>
8
9 #include <map>
10 #include <set>
11 #include <string>
12
13 #include "base/metrics/histogram_macros.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "base/threading/thread.h"
17 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
18 #include "chrome/browser/chrome_notification_types.h"
19 #include "chrome/browser/favicon/favicon_service_factory.h"
20 #include "chrome/browser/first_run/first_run.h"
21 #include "chrome/browser/history/history_service_factory.h"
22 #include "chrome/browser/password_manager/password_store_factory.h"
23 #include "chrome/browser/profiles/profile.h"
24 #include "chrome/browser/search_engines/template_url_service_factory.h"
25 #include "chrome/browser/web_data_service_factory.h"
26 #include "chrome/common/importer/imported_bookmark_entry.h"
27 #include "chrome/common/pref_names.h"
28 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
29 #include "components/bookmarks/browser/bookmark_model.h"
30 #include "components/bookmarks/common/bookmark_pref_names.h"
31 #include "components/favicon/core/favicon_service.h"
32 #include "components/history/core/browser/history_service.h"
33 #include "components/password_manager/core/browser/password_form.h"
34 #include "components/password_manager/core/browser/password_store.h"
35 #include "components/password_manager/core/common/password_manager_pref_names.h"
36 #include "components/prefs/pref_service.h"
37 #include "components/search_engines/template_url.h"
38 #include "components/search_engines/template_url_service.h"
39
40 using bookmarks::BookmarkModel;
41 using bookmarks::BookmarkNode;
42
43 namespace {
44
45 // Generates a unique folder name. If |folder_name| is not unique, then this
46 // repeatedly tests for '|folder_name| + (i)' until a unique name is found.
GenerateUniqueFolderName(BookmarkModel * model,const base::string16 & folder_name)47 base::string16 GenerateUniqueFolderName(BookmarkModel* model,
48 const base::string16& folder_name) {
49 // Build a set containing the bookmark bar folder names.
50 std::set<base::string16> existing_folder_names;
51 const BookmarkNode* bookmark_bar = model->bookmark_bar_node();
52 for (const auto& node : bookmark_bar->children()) {
53 if (node->is_folder())
54 existing_folder_names.insert(node->GetTitle());
55 }
56
57 // If the given name is unique, use it.
58 if (existing_folder_names.find(folder_name) == existing_folder_names.end())
59 return folder_name;
60
61 // Otherwise iterate until we find a unique name.
62 for (size_t i = 1; i <= existing_folder_names.size(); ++i) {
63 base::string16 name = folder_name + base::ASCIIToUTF16(" (") +
64 base::NumberToString16(i) + base::ASCIIToUTF16(")");
65 if (existing_folder_names.find(name) == existing_folder_names.end())
66 return name;
67 }
68
69 NOTREACHED();
70 return folder_name;
71 }
72
73 // Shows the bookmarks toolbar.
ShowBookmarkBar(Profile * profile)74 void ShowBookmarkBar(Profile* profile) {
75 profile->GetPrefs()->SetBoolean(bookmarks::prefs::kShowBookmarkBar, true);
76 }
77
78 } // namespace
79
ProfileWriter(Profile * profile)80 ProfileWriter::ProfileWriter(Profile* profile) : profile_(profile) {}
81
BookmarkModelIsLoaded() const82 bool ProfileWriter::BookmarkModelIsLoaded() const {
83 return BookmarkModelFactory::GetForBrowserContext(profile_)->loaded();
84 }
85
TemplateURLServiceIsLoaded() const86 bool ProfileWriter::TemplateURLServiceIsLoaded() const {
87 return TemplateURLServiceFactory::GetForProfile(profile_)->loaded();
88 }
89
AddPasswordForm(const password_manager::PasswordForm & form)90 void ProfileWriter::AddPasswordForm(
91 const password_manager::PasswordForm& form) {
92 DCHECK(profile_);
93
94 if (profile_->GetPrefs()->GetBoolean(
95 password_manager::prefs::kCredentialsEnableService)) {
96 PasswordStoreFactory::GetForProfile(profile_,
97 ServiceAccessType::EXPLICIT_ACCESS)
98 ->AddLogin(form);
99 }
100 }
101
AddHistoryPage(const history::URLRows & page,history::VisitSource visit_source)102 void ProfileWriter::AddHistoryPage(const history::URLRows& page,
103 history::VisitSource visit_source) {
104 if (!page.empty())
105 HistoryServiceFactory::GetForProfile(profile_,
106 ServiceAccessType::EXPLICIT_ACCESS)
107 ->AddPagesWithDetails(page, visit_source);
108 // Measure the size of the history page after Auto Import on first run.
109 if (first_run::IsChromeFirstRun() &&
110 visit_source == history::SOURCE_IE_IMPORTED) {
111 UMA_HISTOGRAM_COUNTS_1M("Import.ImportedHistorySize.AutoImportFromIE",
112 page.size());
113 }
114 }
115
AddHomepage(const GURL & home_page)116 void ProfileWriter::AddHomepage(const GURL& home_page) {
117 DCHECK(profile_);
118
119 PrefService* prefs = profile_->GetPrefs();
120 // NOTE: We set the kHomePage value, but keep the NewTab page as the homepage.
121 const PrefService::Preference* pref = prefs->FindPreference(prefs::kHomePage);
122 if (pref && !pref->IsManaged()) {
123 prefs->SetString(prefs::kHomePage, home_page.spec());
124 }
125 }
126
AddBookmarks(const std::vector<ImportedBookmarkEntry> & bookmarks,const base::string16 & top_level_folder_name)127 void ProfileWriter::AddBookmarks(
128 const std::vector<ImportedBookmarkEntry>& bookmarks,
129 const base::string16& top_level_folder_name) {
130 if (bookmarks.empty())
131 return;
132
133 BookmarkModel* model = BookmarkModelFactory::GetForBrowserContext(profile_);
134 DCHECK(model->loaded());
135
136 // If the bookmark bar is currently empty, we should import directly to it.
137 // Otherwise, we should import everything to a subfolder.
138 const BookmarkNode* bookmark_bar = model->bookmark_bar_node();
139 bool import_to_top_level = bookmark_bar->children().empty();
140
141 // Reorder bookmarks so that the toolbar entries come first.
142 std::vector<ImportedBookmarkEntry> toolbar_bookmarks;
143 std::vector<ImportedBookmarkEntry> reordered_bookmarks;
144 for (auto it = bookmarks.begin(); it != bookmarks.end(); ++it) {
145 if (it->in_toolbar)
146 toolbar_bookmarks.push_back(*it);
147 else
148 reordered_bookmarks.push_back(*it);
149 }
150 reordered_bookmarks.insert(reordered_bookmarks.begin(),
151 toolbar_bookmarks.begin(),
152 toolbar_bookmarks.end());
153
154 // If the user currently has no bookmarks in the bookmark bar, make sure that
155 // at least some of the imported bookmarks end up there. Otherwise, we'll end
156 // up with just a single folder containing the imported bookmarks, which makes
157 // for unnecessary nesting.
158 bool add_all_to_top_level = import_to_top_level && toolbar_bookmarks.empty();
159
160 model->BeginExtensiveChanges();
161
162 std::set<const BookmarkNode*> folders_added_to;
163 const BookmarkNode* top_level_folder = NULL;
164 for (std::vector<ImportedBookmarkEntry>::const_iterator bookmark =
165 reordered_bookmarks.begin();
166 bookmark != reordered_bookmarks.end(); ++bookmark) {
167 // Disregard any bookmarks with invalid urls.
168 if (!bookmark->is_folder && !bookmark->url.is_valid())
169 continue;
170
171 const BookmarkNode* parent = NULL;
172 if (import_to_top_level && (add_all_to_top_level || bookmark->in_toolbar)) {
173 // Add directly to the bookmarks bar.
174 parent = bookmark_bar;
175 } else {
176 // Add to a folder that will contain all the imported bookmarks not added
177 // to the bar. The first time we do so, create the folder.
178 if (!top_level_folder) {
179 base::string16 name =
180 GenerateUniqueFolderName(model,top_level_folder_name);
181 top_level_folder = model->AddFolder(
182 bookmark_bar, bookmark_bar->children().size(), name);
183 }
184 parent = top_level_folder;
185 }
186
187 // Ensure any enclosing folders are present in the model. The bookmark's
188 // enclosing folder structure should be
189 // path[0] > path[1] > ... > path[size() - 1]
190 for (auto folder_name = bookmark->path.begin();
191 folder_name != bookmark->path.end(); ++folder_name) {
192 if (bookmark->in_toolbar && parent == bookmark_bar &&
193 folder_name == bookmark->path.begin()) {
194 // If we're importing directly to the bookmarks bar, skip over the
195 // folder named "Bookmarks Toolbar" (or any non-Firefox equivalent).
196 continue;
197 }
198
199 const auto it = std::find_if(
200 parent->children().cbegin(), parent->children().cend(),
201 [folder_name](const auto& node) {
202 return node->is_folder() && node->GetTitle() == *folder_name;
203 });
204 parent = (it == parent->children().cend())
205 ? model->AddFolder(parent, parent->children().size(),
206 *folder_name)
207 : it->get();
208 }
209
210 folders_added_to.insert(parent);
211 if (bookmark->is_folder) {
212 model->AddFolder(parent, parent->children().size(), bookmark->title);
213 } else {
214 model->AddURL(parent, parent->children().size(), bookmark->title,
215 bookmark->url, nullptr, bookmark->creation_time);
216 }
217 }
218
219 // In order to keep the imported-to folders from appearing in the 'recently
220 // added to' combobox, reset their modified times.
221 for (auto i = folders_added_to.begin(); i != folders_added_to.end(); ++i) {
222 model->ResetDateFolderModified(*i);
223 }
224
225 model->EndExtensiveChanges();
226
227 // If the user was previously using a toolbar, we should show the bar.
228 if (import_to_top_level && !add_all_to_top_level)
229 ShowBookmarkBar(profile_);
230 }
231
AddFavicons(const favicon_base::FaviconUsageDataList & favicons)232 void ProfileWriter::AddFavicons(
233 const favicon_base::FaviconUsageDataList& favicons) {
234 FaviconServiceFactory::GetForProfile(profile_,
235 ServiceAccessType::EXPLICIT_ACCESS)
236 ->SetImportedFavicons(favicons);
237 }
238
239 typedef std::map<std::string, TemplateURL*> HostPathMap;
240
241 // Returns the key for the map built by BuildHostPathMap. If url_string is not
242 // a valid URL, an empty string is returned, otherwise host+path is returned.
HostPathKeyForURL(const GURL & url)243 static std::string HostPathKeyForURL(const GURL& url) {
244 return url.is_valid() ? url.host() + url.path() : std::string();
245 }
246
247 // Builds the key to use in HostPathMap for the specified TemplateURL. Returns
248 // an empty string if a host+path can't be generated for the TemplateURL.
249 // If an empty string is returned, the TemplateURL should not be added to
250 // HostPathMap.
251 //
252 // If |try_url_if_invalid| is true, and |t_url| isn't valid, a string is built
253 // from the raw TemplateURL string. Use a value of true for |try_url_if_invalid|
254 // when checking imported URLs as the imported URL may not be valid yet may
255 // match the host+path of one of the default URLs. This is used to catch the
256 // case of IE using an invalid OSDD URL for Live Search, yet the host+path
257 // matches our prepopulate data. IE's URL for Live Search is something like
258 // 'http://...{Language}...'. As {Language} is not a valid OSDD parameter value
259 // the TemplateURL is invalid.
BuildHostPathKey(const TemplateURL * t_url,const SearchTermsData & search_terms_data,bool try_url_if_invalid)260 static std::string BuildHostPathKey(const TemplateURL* t_url,
261 const SearchTermsData& search_terms_data,
262 bool try_url_if_invalid) {
263 if (try_url_if_invalid && !t_url->url_ref().IsValid(search_terms_data))
264 return HostPathKeyForURL(GURL(t_url->url()));
265
266 if (t_url->url_ref().SupportsReplacement(search_terms_data)) {
267 return HostPathKeyForURL(GURL(
268 t_url->url_ref().ReplaceSearchTerms(
269 TemplateURLRef::SearchTermsArgs(base::ASCIIToUTF16("x")),
270 search_terms_data)));
271 }
272 return std::string();
273 }
274
275 // Builds a set that contains an entry of the host+path for each TemplateURL in
276 // the TemplateURLService that has a valid search url.
BuildHostPathMap(TemplateURLService * model,HostPathMap * host_path_map)277 static void BuildHostPathMap(TemplateURLService* model,
278 HostPathMap* host_path_map) {
279 TemplateURLService::TemplateURLVector template_urls =
280 model->GetTemplateURLs();
281 for (size_t i = 0; i < template_urls.size(); ++i) {
282 const std::string host_path = BuildHostPathKey(
283 template_urls[i], model->search_terms_data(), false);
284 if (!host_path.empty()) {
285 const TemplateURL* existing_turl = (*host_path_map)[host_path];
286 TemplateURL* t_url = template_urls[i];
287 if (!existing_turl || (model->ShowInDefaultList(t_url) &&
288 !model->ShowInDefaultList(existing_turl))) {
289 // If there are multiple TemplateURLs with the same host+path, favor
290 // those shown in the default list. If there are multiple potential
291 // defaults, favor the first one, which should be the more commonly used
292 // one.
293 (*host_path_map)[host_path] = t_url;
294 }
295 } // else case, TemplateURL doesn't have a search url, doesn't support
296 // replacement, or doesn't have valid GURL. Ignore it.
297 }
298 }
299
AddKeywords(TemplateURLService::OwnedTemplateURLVector template_urls,bool unique_on_host_and_path)300 void ProfileWriter::AddKeywords(
301 TemplateURLService::OwnedTemplateURLVector template_urls,
302 bool unique_on_host_and_path) {
303 TemplateURLService* model =
304 TemplateURLServiceFactory::GetForProfile(profile_);
305 HostPathMap host_path_map;
306 if (unique_on_host_and_path)
307 BuildHostPathMap(model, &host_path_map);
308
309 for (auto& turl : template_urls) {
310 // TemplateURLService requires keywords to be unique. If there is already a
311 // TemplateURL with this keyword, don't import it again.
312 if (model->GetTemplateURLForKeyword(turl->keyword()) != nullptr)
313 continue;
314
315 // The omnibox doesn't properly handle search keywords with whitespace,
316 // so skip importing them.
317 if (turl->keyword().find_first_of(base::kWhitespaceUTF16) !=
318 base::string16::npos)
319 continue;
320
321 // For search engines if there is already a keyword with the same
322 // host+path, we don't import it. This is done to avoid both duplicate
323 // search providers (such as two Googles, or two Yahoos) as well as making
324 // sure the search engines we provide aren't replaced by those from the
325 // imported browser.
326 if (unique_on_host_and_path && (host_path_map.find(BuildHostPathKey(
327 turl.get(), model->search_terms_data(),
328 true)) != host_path_map.end()))
329 continue;
330
331 // Only add valid TemplateURLs to the model.
332 if (turl->url_ref().IsValid(model->search_terms_data()))
333 model->Add(std::move(turl));
334 }
335 }
336
AddAutofillFormDataEntries(const std::vector<autofill::AutofillEntry> & autofill_entries)337 void ProfileWriter::AddAutofillFormDataEntries(
338 const std::vector<autofill::AutofillEntry>& autofill_entries) {
339 scoped_refptr<autofill::AutofillWebDataService> web_data_service =
340 WebDataServiceFactory::GetAutofillWebDataForProfile(
341 profile_, ServiceAccessType::EXPLICIT_ACCESS);
342 if (web_data_service.get())
343 web_data_service->UpdateAutofillEntries(autofill_entries);
344 }
345
~ProfileWriter()346 ProfileWriter::~ProfileWriter() {}
347