OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/importer/profile_writer.h" | 5 #include "chrome/browser/importer/profile_writer.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 #include <set> | 10 #include <set> |
11 #include <string> | 11 #include <string> |
12 | 12 |
13 #include "base/metrics/histogram.h" | |
13 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
14 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
15 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
16 #include "base/threading/thread.h" | 17 #include "base/threading/thread.h" |
17 #include "build/build_config.h" | 18 #include "build/build_config.h" |
18 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 19 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
19 #include "chrome/browser/chrome_notification_types.h" | 20 #include "chrome/browser/chrome_notification_types.h" |
20 #include "chrome/browser/favicon/favicon_service_factory.h" | 21 #include "chrome/browser/favicon/favicon_service_factory.h" |
22 #include "chrome/browser/first_run/first_run.h" | |
21 #include "chrome/browser/history/history_service_factory.h" | 23 #include "chrome/browser/history/history_service_factory.h" |
22 #include "chrome/browser/password_manager/password_store_factory.h" | 24 #include "chrome/browser/password_manager/password_store_factory.h" |
23 #include "chrome/browser/profiles/profile.h" | 25 #include "chrome/browser/profiles/profile.h" |
24 #include "chrome/browser/search_engines/template_url_service_factory.h" | 26 #include "chrome/browser/search_engines/template_url_service_factory.h" |
25 #include "chrome/browser/web_data_service_factory.h" | 27 #include "chrome/browser/web_data_service_factory.h" |
26 #include "chrome/common/importer/imported_bookmark_entry.h" | 28 #include "chrome/common/importer/imported_bookmark_entry.h" |
27 #include "chrome/common/pref_names.h" | 29 #include "chrome/common/pref_names.h" |
28 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" | 30 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" |
29 #include "components/bookmarks/browser/bookmark_model.h" | 31 #include "components/bookmarks/browser/bookmark_model.h" |
30 #include "components/bookmarks/common/bookmark_pref_names.h" | 32 #include "components/bookmarks/common/bookmark_pref_names.h" |
31 #include "components/favicon/core/favicon_service.h" | 33 #include "components/favicon/core/favicon_service.h" |
32 #include "components/history/core/browser/history_service.h" | 34 #include "components/history/core/browser/history_service.h" |
33 #include "components/password_manager/core/browser/password_store.h" | 35 #include "components/password_manager/core/browser/password_store.h" |
34 #include "components/prefs/pref_service.h" | 36 #include "components/prefs/pref_service.h" |
35 #include "components/search_engines/template_url.h" | 37 #include "components/search_engines/template_url.h" |
36 #include "components/search_engines/template_url_service.h" | 38 #include "components/search_engines/template_url_service.h" |
37 | 39 |
38 #if defined(OS_WIN) | 40 #if defined(OS_WIN) |
39 #include "chrome/browser/web_data_service_factory.h" | |
40 #include "components/password_manager/core/browser/webdata/password_web_data_ser vice_win.h" | 41 #include "components/password_manager/core/browser/webdata/password_web_data_ser vice_win.h" |
41 #endif | 42 #endif |
42 | 43 |
43 using bookmarks::BookmarkModel; | 44 using bookmarks::BookmarkModel; |
44 using bookmarks::BookmarkNode; | 45 using bookmarks::BookmarkNode; |
45 | 46 |
46 namespace { | 47 namespace { |
47 | 48 |
48 // Generates a unique folder name. If |folder_name| is not unique, then this | 49 // Generates a unique folder name. If |folder_name| is not unique, then this |
49 // repeatedly tests for '|folder_name| + (i)' until a unique name is found. | 50 // repeatedly tests for '|folder_name| + (i)' until a unique name is found. |
50 base::string16 GenerateUniqueFolderName(BookmarkModel* model, | 51 base::string16 GenerateUniqueFolderName(BookmarkModel* model, |
51 const base::string16& folder_name) { | 52 const base::string16& folder_name) { |
52 // Build a set containing the bookmark bar folder names. | 53 // Build a set containing the bookmark bar folder names. |
53 std::set<base::string16> existing_folder_names; | 54 std::set<base::string16> existing_folder_names; |
54 const BookmarkNode* bookmark_bar = model->bookmark_bar_node(); | 55 const BookmarkNode* bookmark_bar = model->bookmark_bar_node(); |
55 for (int i = 0; i < bookmark_bar->child_count(); ++i) { | 56 for (int i = 0; i < bookmark_bar->child_count(); ++i) { |
56 const BookmarkNode* node = bookmark_bar->GetChild(i); | 57 const BookmarkNode* node = bookmark_bar->GetChild(i); |
57 if (node->is_folder()) | 58 if (node->is_folder()) |
58 existing_folder_names.insert(node->GetTitle()); | 59 existing_folder_names.insert(node->GetTitle()); |
59 } | 60 } |
60 | 61 |
61 // If the given name is unique, use it. | 62 // If the given name is unique, use it. |
62 if (existing_folder_names.find(folder_name) == existing_folder_names.end()) | 63 if (existing_folder_names.find(folder_name) == existing_folder_names.end()) |
63 return folder_name; | 64 return folder_name; |
64 | 65 |
65 // Otherwise iterate until we find a unique name. | 66 // Otherwise iterate until we find a unique name. |
66 for (size_t i = 1; i <= existing_folder_names.size(); ++i) { | 67 for (size_t i = 1; i <= existing_folder_names.size(); ++i) { |
67 base::string16 name = folder_name + base::ASCIIToUTF16(" (") + | 68 base::string16 name = folder_name + base::ASCIIToUTF16(" (") + |
68 base::SizeTToString16(i) + base::ASCIIToUTF16(")"); | 69 base::SizeTToString16(i) + base::ASCIIToUTF16(")"); |
69 if (existing_folder_names.find(name) == existing_folder_names.end()) | 70 if (existing_folder_names.find(name) == existing_folder_names.end()) |
70 return name; | 71 return name; |
71 } | 72 } |
72 | 73 |
73 NOTREACHED(); | 74 NOTREACHED(); |
74 return folder_name; | 75 return folder_name; |
75 } | 76 } |
76 | 77 |
77 // Shows the bookmarks toolbar. | 78 // Shows the bookmarks toolbar. |
78 void ShowBookmarkBar(Profile* profile) { | 79 void ShowBookmarkBar(Profile* profile) { |
79 profile->GetPrefs()->SetBoolean(bookmarks::prefs::kShowBookmarkBar, true); | 80 profile->GetPrefs()->SetBoolean(bookmarks::prefs::kShowBookmarkBar, true); |
80 } | 81 } |
81 | 82 |
82 } // namespace | 83 } // namespace |
83 | 84 |
84 ProfileWriter::ProfileWriter(Profile* profile) : profile_(profile) {} | 85 ProfileWriter::ProfileWriter(Profile* profile) : profile_(profile) {} |
85 | 86 |
86 bool ProfileWriter::BookmarkModelIsLoaded() const { | 87 bool ProfileWriter::BookmarkModelIsLoaded() const { |
87 return BookmarkModelFactory::GetForBrowserContext(profile_)->loaded(); | 88 return BookmarkModelFactory::GetForBrowserContext(profile_)->loaded(); |
88 } | 89 } |
89 | 90 |
90 bool ProfileWriter::TemplateURLServiceIsLoaded() const { | 91 bool ProfileWriter::TemplateURLServiceIsLoaded() const { |
91 return TemplateURLServiceFactory::GetForProfile(profile_)->loaded(); | 92 return TemplateURLServiceFactory::GetForProfile(profile_)->loaded(); |
92 } | 93 } |
93 | 94 |
94 void ProfileWriter::AddPasswordForm(const autofill::PasswordForm& form) { | 95 void ProfileWriter::AddPasswordForm(const autofill::PasswordForm& form) { |
95 PasswordStoreFactory::GetForProfile( | 96 PasswordStoreFactory::GetForProfile(profile_, |
manzagop (departed)
2016/08/18 15:59:56
Did git cl format do this? Typically you don't wan
gcomanici
2016/08/19 16:04:31
Done.
| |
96 profile_, ServiceAccessType::EXPLICIT_ACCESS)->AddLogin(form); | 97 ServiceAccessType::EXPLICIT_ACCESS) |
98 ->AddLogin(form); | |
97 } | 99 } |
98 | 100 |
99 #if defined(OS_WIN) | 101 #if defined(OS_WIN) |
100 void ProfileWriter::AddIE7PasswordInfo(const IE7PasswordInfo& info) { | 102 void ProfileWriter::AddIE7PasswordInfo(const IE7PasswordInfo& info) { |
101 WebDataServiceFactory::GetPasswordWebDataForProfile( | 103 WebDataServiceFactory::GetPasswordWebDataForProfile( |
102 profile_, ServiceAccessType::EXPLICIT_ACCESS)->AddIE7Login(info); | 104 profile_, ServiceAccessType::EXPLICIT_ACCESS) |
105 ->AddIE7Login(info); | |
103 } | 106 } |
104 #endif | 107 #endif |
105 | 108 |
106 void ProfileWriter::AddHistoryPage(const history::URLRows& page, | 109 void ProfileWriter::AddHistoryPage(const history::URLRows& page, |
107 history::VisitSource visit_source) { | 110 history::VisitSource visit_source) { |
108 HistoryServiceFactory::GetForProfile(profile_, | 111 HistoryServiceFactory::GetForProfile(profile_, |
109 ServiceAccessType::EXPLICIT_ACCESS) | 112 ServiceAccessType::EXPLICIT_ACCESS) |
110 ->AddPagesWithDetails(page, visit_source); | 113 ->AddPagesWithDetails(page, visit_source); |
114 // Measure the size of the history page after AutoImport on first run. | |
115 if (first_run::IsChromeFirstRun()) { | |
116 UMA_HISTOGRAM_COUNTS("Import.SizeImportedHistory.AutoImport", page.size()); | |
manzagop (departed)
2016/08/18 15:59:56
Is this called once per importer? Eg if I import b
gcomanici
2016/08/18 19:40:00
This is a very good point. I tried to see if there
manzagop (departed)
2016/08/18 20:35:29
This sounds like a discussion for the bug, where y
gcomanici
2016/08/19 16:04:31
Acknowledged.
| |
117 } | |
111 } | 118 } |
112 | 119 |
113 void ProfileWriter::AddHomepage(const GURL& home_page) { | 120 void ProfileWriter::AddHomepage(const GURL& home_page) { |
114 DCHECK(profile_); | 121 DCHECK(profile_); |
115 | 122 |
116 PrefService* prefs = profile_->GetPrefs(); | 123 PrefService* prefs = profile_->GetPrefs(); |
117 // NOTE: We set the kHomePage value, but keep the NewTab page as the homepage. | 124 // NOTE: We set the kHomePage value, but keep the NewTab page as the homepage. |
118 const PrefService::Preference* pref = prefs->FindPreference(prefs::kHomePage); | 125 const PrefService::Preference* pref = prefs->FindPreference(prefs::kHomePage); |
119 if (pref && !pref->IsManaged()) { | 126 if (pref && !pref->IsManaged()) { |
120 prefs->SetString(prefs::kHomePage, home_page.spec()); | 127 prefs->SetString(prefs::kHomePage, home_page.spec()); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
169 | 176 |
170 const BookmarkNode* parent = NULL; | 177 const BookmarkNode* parent = NULL; |
171 if (import_to_top_level && (add_all_to_top_level || bookmark->in_toolbar)) { | 178 if (import_to_top_level && (add_all_to_top_level || bookmark->in_toolbar)) { |
172 // Add directly to the bookmarks bar. | 179 // Add directly to the bookmarks bar. |
173 parent = bookmark_bar; | 180 parent = bookmark_bar; |
174 } else { | 181 } else { |
175 // Add to a folder that will contain all the imported bookmarks not added | 182 // Add to a folder that will contain all the imported bookmarks not added |
176 // to the bar. The first time we do so, create the folder. | 183 // to the bar. The first time we do so, create the folder. |
177 if (!top_level_folder) { | 184 if (!top_level_folder) { |
178 base::string16 name = | 185 base::string16 name = |
179 GenerateUniqueFolderName(model,top_level_folder_name); | 186 GenerateUniqueFolderName(model, top_level_folder_name); |
180 top_level_folder = model->AddFolder(bookmark_bar, | 187 top_level_folder = |
181 bookmark_bar->child_count(), | 188 model->AddFolder(bookmark_bar, bookmark_bar->child_count(), name); |
182 name); | |
183 } | 189 } |
184 parent = top_level_folder; | 190 parent = top_level_folder; |
185 } | 191 } |
186 | 192 |
187 // Ensure any enclosing folders are present in the model. The bookmark's | 193 // Ensure any enclosing folders are present in the model. The bookmark's |
188 // enclosing folder structure should be | 194 // enclosing folder structure should be |
189 // path[0] > path[1] > ... > path[size() - 1] | 195 // path[0] > path[1] > ... > path[size() - 1] |
190 for (std::vector<base::string16>::const_iterator folder_name = | 196 for (std::vector<base::string16>::const_iterator folder_name = |
191 bookmark->path.begin(); | 197 bookmark->path.begin(); |
192 folder_name != bookmark->path.end(); ++folder_name) { | 198 folder_name != bookmark->path.end(); ++folder_name) { |
(...skipping 14 matching lines...) Expand all Loading... | |
207 } | 213 } |
208 if (!child) | 214 if (!child) |
209 child = model->AddFolder(parent, parent->child_count(), *folder_name); | 215 child = model->AddFolder(parent, parent->child_count(), *folder_name); |
210 parent = child; | 216 parent = child; |
211 } | 217 } |
212 | 218 |
213 folders_added_to.insert(parent); | 219 folders_added_to.insert(parent); |
214 if (bookmark->is_folder) { | 220 if (bookmark->is_folder) { |
215 model->AddFolder(parent, parent->child_count(), bookmark->title); | 221 model->AddFolder(parent, parent->child_count(), bookmark->title); |
216 } else { | 222 } else { |
217 model->AddURLWithCreationTimeAndMetaInfo(parent, | 223 model->AddURLWithCreationTimeAndMetaInfo(parent, parent->child_count(), |
218 parent->child_count(), | 224 bookmark->title, bookmark->url, |
219 bookmark->title, | 225 bookmark->creation_time, NULL); |
220 bookmark->url, | |
221 bookmark->creation_time, | |
222 NULL); | |
223 } | 226 } |
224 } | 227 } |
225 | 228 |
226 // In order to keep the imported-to folders from appearing in the 'recently | 229 // In order to keep the imported-to folders from appearing in the 'recently |
227 // added to' combobox, reset their modified times. | 230 // added to' combobox, reset their modified times. |
228 for (std::set<const BookmarkNode*>::const_iterator i = | 231 for (std::set<const BookmarkNode*>::const_iterator i = |
229 folders_added_to.begin(); | 232 folders_added_to.begin(); |
230 i != folders_added_to.end(); ++i) { | 233 i != folders_added_to.end(); ++i) { |
231 model->ResetDateFolderModified(*i); | 234 model->ResetDateFolderModified(*i); |
232 } | 235 } |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
266 // matches our prepopulate data. IE's URL for Live Search is something like | 269 // matches our prepopulate data. IE's URL for Live Search is something like |
267 // 'http://...{Language}...'. As {Language} is not a valid OSDD parameter value | 270 // 'http://...{Language}...'. As {Language} is not a valid OSDD parameter value |
268 // the TemplateURL is invalid. | 271 // the TemplateURL is invalid. |
269 static std::string BuildHostPathKey(const TemplateURL* t_url, | 272 static std::string BuildHostPathKey(const TemplateURL* t_url, |
270 const SearchTermsData& search_terms_data, | 273 const SearchTermsData& search_terms_data, |
271 bool try_url_if_invalid) { | 274 bool try_url_if_invalid) { |
272 if (try_url_if_invalid && !t_url->url_ref().IsValid(search_terms_data)) | 275 if (try_url_if_invalid && !t_url->url_ref().IsValid(search_terms_data)) |
273 return HostPathKeyForURL(GURL(t_url->url())); | 276 return HostPathKeyForURL(GURL(t_url->url())); |
274 | 277 |
275 if (t_url->url_ref().SupportsReplacement(search_terms_data)) { | 278 if (t_url->url_ref().SupportsReplacement(search_terms_data)) { |
276 return HostPathKeyForURL(GURL( | 279 return HostPathKeyForURL(GURL(t_url->url_ref().ReplaceSearchTerms( |
277 t_url->url_ref().ReplaceSearchTerms( | 280 TemplateURLRef::SearchTermsArgs(base::ASCIIToUTF16("x")), |
278 TemplateURLRef::SearchTermsArgs(base::ASCIIToUTF16("x")), | 281 search_terms_data))); |
279 search_terms_data))); | |
280 } | 282 } |
281 return std::string(); | 283 return std::string(); |
282 } | 284 } |
283 | 285 |
284 // Builds a set that contains an entry of the host+path for each TemplateURL in | 286 // Builds a set that contains an entry of the host+path for each TemplateURL in |
285 // the TemplateURLService that has a valid search url. | 287 // the TemplateURLService that has a valid search url. |
286 static void BuildHostPathMap(TemplateURLService* model, | 288 static void BuildHostPathMap(TemplateURLService* model, |
287 HostPathMap* host_path_map) { | 289 HostPathMap* host_path_map) { |
288 TemplateURLService::TemplateURLVector template_urls = | 290 TemplateURLService::TemplateURLVector template_urls = |
289 model->GetTemplateURLs(); | 291 model->GetTemplateURLs(); |
290 for (size_t i = 0; i < template_urls.size(); ++i) { | 292 for (size_t i = 0; i < template_urls.size(); ++i) { |
291 const std::string host_path = BuildHostPathKey( | 293 const std::string host_path = |
292 template_urls[i], model->search_terms_data(), false); | 294 BuildHostPathKey(template_urls[i], model->search_terms_data(), false); |
293 if (!host_path.empty()) { | 295 if (!host_path.empty()) { |
294 const TemplateURL* existing_turl = (*host_path_map)[host_path]; | 296 const TemplateURL* existing_turl = (*host_path_map)[host_path]; |
295 if (!existing_turl || | 297 if (!existing_turl || (template_urls[i]->show_in_default_list() && |
296 (template_urls[i]->show_in_default_list() && | 298 !existing_turl->show_in_default_list())) { |
297 !existing_turl->show_in_default_list())) { | |
298 // If there are multiple TemplateURLs with the same host+path, favor | 299 // If there are multiple TemplateURLs with the same host+path, favor |
299 // those shown in the default list. If there are multiple potential | 300 // those shown in the default list. If there are multiple potential |
300 // defaults, favor the first one, which should be the more commonly used | 301 // defaults, favor the first one, which should be the more commonly used |
301 // one. | 302 // one. |
302 (*host_path_map)[host_path] = template_urls[i]; | 303 (*host_path_map)[host_path] = template_urls[i]; |
303 } | 304 } |
304 } // else case, TemplateURL doesn't have a search url, doesn't support | 305 } // else case, TemplateURL doesn't have a search url, doesn't support |
305 // replacement, or doesn't have valid GURL. Ignore it. | 306 // replacement, or doesn't have valid GURL. Ignore it. |
306 } | 307 } |
307 } | 308 } |
(...skipping 12 matching lines...) Expand all Loading... | |
320 // TemplateURL with this keyword, don't import it again. | 321 // TemplateURL with this keyword, don't import it again. |
321 if (model->GetTemplateURLForKeyword((*i)->keyword()) != NULL) | 322 if (model->GetTemplateURLForKeyword((*i)->keyword()) != NULL) |
322 continue; | 323 continue; |
323 | 324 |
324 // For search engines if there is already a keyword with the same | 325 // For search engines if there is already a keyword with the same |
325 // host+path, we don't import it. This is done to avoid both duplicate | 326 // host+path, we don't import it. This is done to avoid both duplicate |
326 // search providers (such as two Googles, or two Yahoos) as well as making | 327 // search providers (such as two Googles, or two Yahoos) as well as making |
327 // sure the search engines we provide aren't replaced by those from the | 328 // sure the search engines we provide aren't replaced by those from the |
328 // imported browser. | 329 // imported browser. |
329 if (unique_on_host_and_path && | 330 if (unique_on_host_and_path && |
330 (host_path_map.find(BuildHostPathKey( | 331 (host_path_map.find(BuildHostPathKey(*i, model->search_terms_data(), |
331 *i, model->search_terms_data(), true)) != host_path_map.end())) | 332 true)) != host_path_map.end())) |
332 continue; | 333 continue; |
333 | 334 |
334 // Only add valid TemplateURLs to the model. | 335 // Only add valid TemplateURLs to the model. |
335 if ((*i)->url_ref().IsValid(model->search_terms_data())) { | 336 if ((*i)->url_ref().IsValid(model->search_terms_data())) { |
336 model->Add(*i); // Takes ownership. | 337 model->Add(*i); // Takes ownership. |
337 *i = NULL; // Prevent the vector from deleting *i later. | 338 *i = NULL; // Prevent the vector from deleting *i later. |
338 } | 339 } |
339 } | 340 } |
340 } | 341 } |
341 | 342 |
342 void ProfileWriter::AddAutofillFormDataEntries( | 343 void ProfileWriter::AddAutofillFormDataEntries( |
343 const std::vector<autofill::AutofillEntry>& autofill_entries) { | 344 const std::vector<autofill::AutofillEntry>& autofill_entries) { |
344 scoped_refptr<autofill::AutofillWebDataService> web_data_service = | 345 scoped_refptr<autofill::AutofillWebDataService> web_data_service = |
345 WebDataServiceFactory::GetAutofillWebDataForProfile( | 346 WebDataServiceFactory::GetAutofillWebDataForProfile( |
346 profile_, ServiceAccessType::EXPLICIT_ACCESS); | 347 profile_, ServiceAccessType::EXPLICIT_ACCESS); |
347 if (web_data_service.get()) | 348 if (web_data_service.get()) |
348 web_data_service->UpdateAutofillEntries(autofill_entries); | 349 web_data_service->UpdateAutofillEntries(autofill_entries); |
349 } | 350 } |
350 | 351 |
351 ProfileWriter::~ProfileWriter() {} | 352 ProfileWriter::~ProfileWriter() {} |
OLD | NEW |