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.
5 #include "chrome/browser/importer/profile_writer.h"
11 #include "base/prefs/pref_service.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/stringprintf.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/threading/thread.h"
16 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
17 #include "chrome/browser/chrome_notification_types.h"
18 #include "chrome/browser/favicon/favicon_service_factory.h"
19 #include "chrome/browser/history/history_service_factory.h"
20 #include "chrome/browser/password_manager/password_store_factory.h"
21 #include "chrome/browser/profiles/profile.h"
22 #include "chrome/browser/search_engines/template_url_service_factory.h"
23 #include "chrome/browser/webdata/web_data_service_factory.h"
24 #include "chrome/common/importer/imported_bookmark_entry.h"
25 #include "chrome/common/pref_names.h"
26 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
27 #include "components/bookmarks/browser/bookmark_model.h"
28 #include "components/favicon/core/favicon_service.h"
29 #include "components/history/core/browser/history_service.h"
30 #include "components/password_manager/core/browser/password_store.h"
31 #include "components/search_engines/template_url.h"
32 #include "components/search_engines/template_url_service.h"
35 #include "chrome/browser/webdata/web_data_service_factory.h"
36 #include "components/password_manager/core/browser/webdata/password_web_data_service_win.h"
39 using bookmarks::BookmarkModel
;
40 using bookmarks::BookmarkNode
;
44 // Generates a unique folder name. If |folder_name| is not unique, then this
45 // repeatedly tests for '|folder_name| + (i)' until a unique name is found.
46 base::string16
GenerateUniqueFolderName(BookmarkModel
* model
,
47 const base::string16
& folder_name
) {
48 // Build a set containing the bookmark bar folder names.
49 std::set
<base::string16
> existing_folder_names
;
50 const BookmarkNode
* bookmark_bar
= model
->bookmark_bar_node();
51 for (int i
= 0; i
< bookmark_bar
->child_count(); ++i
) {
52 const BookmarkNode
* node
= bookmark_bar
->GetChild(i
);
53 if (node
->is_folder())
54 existing_folder_names
.insert(node
->GetTitle());
57 // If the given name is unique, use it.
58 if (existing_folder_names
.find(folder_name
) == existing_folder_names
.end())
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::IntToString16(i
) + base::ASCIIToUTF16(")");
65 if (existing_folder_names
.find(name
) == existing_folder_names
.end())
73 // Shows the bookmarks toolbar.
74 void ShowBookmarkBar(Profile
* profile
) {
75 profile
->GetPrefs()->SetBoolean(bookmarks::prefs::kShowBookmarkBar
, true);
80 ProfileWriter::ProfileWriter(Profile
* profile
) : profile_(profile
) {}
82 bool ProfileWriter::BookmarkModelIsLoaded() const {
83 return BookmarkModelFactory::GetForProfile(profile_
)->loaded();
86 bool ProfileWriter::TemplateURLServiceIsLoaded() const {
87 return TemplateURLServiceFactory::GetForProfile(profile_
)->loaded();
90 void ProfileWriter::AddPasswordForm(const autofill::PasswordForm
& form
) {
91 PasswordStoreFactory::GetForProfile(
92 profile_
, ServiceAccessType::EXPLICIT_ACCESS
)->AddLogin(form
);
96 void ProfileWriter::AddIE7PasswordInfo(const IE7PasswordInfo
& info
) {
97 WebDataServiceFactory::GetPasswordWebDataForProfile(
98 profile_
, ServiceAccessType::EXPLICIT_ACCESS
)->AddIE7Login(info
);
102 void ProfileWriter::AddHistoryPage(const history::URLRows
& page
,
103 history::VisitSource visit_source
) {
104 HistoryServiceFactory::GetForProfile(profile_
,
105 ServiceAccessType::EXPLICIT_ACCESS
)
106 ->AddPagesWithDetails(page
, visit_source
);
109 void ProfileWriter::AddHomepage(const GURL
& home_page
) {
112 PrefService
* prefs
= profile_
->GetPrefs();
113 // NOTE: We set the kHomePage value, but keep the NewTab page as the homepage.
114 const PrefService::Preference
* pref
= prefs
->FindPreference(prefs::kHomePage
);
115 if (pref
&& !pref
->IsManaged()) {
116 prefs
->SetString(prefs::kHomePage
, home_page
.spec());
120 void ProfileWriter::AddBookmarks(
121 const std::vector
<ImportedBookmarkEntry
>& bookmarks
,
122 const base::string16
& top_level_folder_name
) {
123 if (bookmarks
.empty())
126 BookmarkModel
* model
= BookmarkModelFactory::GetForProfile(profile_
);
127 DCHECK(model
->loaded());
129 // If the bookmark bar is currently empty, we should import directly to it.
130 // Otherwise, we should import everything to a subfolder.
131 const BookmarkNode
* bookmark_bar
= model
->bookmark_bar_node();
132 bool import_to_top_level
= bookmark_bar
->empty();
134 // Reorder bookmarks so that the toolbar entries come first.
135 std::vector
<ImportedBookmarkEntry
> toolbar_bookmarks
;
136 std::vector
<ImportedBookmarkEntry
> reordered_bookmarks
;
137 for (std::vector
<ImportedBookmarkEntry
>::const_iterator it
=
139 it
!= bookmarks
.end(); ++it
) {
141 toolbar_bookmarks
.push_back(*it
);
143 reordered_bookmarks
.push_back(*it
);
145 reordered_bookmarks
.insert(reordered_bookmarks
.begin(),
146 toolbar_bookmarks
.begin(),
147 toolbar_bookmarks
.end());
149 // If the user currently has no bookmarks in the bookmark bar, make sure that
150 // at least some of the imported bookmarks end up there. Otherwise, we'll end
151 // up with just a single folder containing the imported bookmarks, which makes
152 // for unnecessary nesting.
153 bool add_all_to_top_level
= import_to_top_level
&& toolbar_bookmarks
.empty();
155 model
->BeginExtensiveChanges();
157 std::set
<const BookmarkNode
*> folders_added_to
;
158 const BookmarkNode
* top_level_folder
= NULL
;
159 for (std::vector
<ImportedBookmarkEntry
>::const_iterator bookmark
=
160 reordered_bookmarks
.begin();
161 bookmark
!= reordered_bookmarks
.end(); ++bookmark
) {
162 // Disregard any bookmarks with invalid urls.
163 if (!bookmark
->is_folder
&& !bookmark
->url
.is_valid())
166 const BookmarkNode
* parent
= NULL
;
167 if (import_to_top_level
&& (add_all_to_top_level
|| bookmark
->in_toolbar
)) {
168 // Add directly to the bookmarks bar.
169 parent
= bookmark_bar
;
171 // Add to a folder that will contain all the imported bookmarks not added
172 // to the bar. The first time we do so, create the folder.
173 if (!top_level_folder
) {
174 base::string16 name
=
175 GenerateUniqueFolderName(model
,top_level_folder_name
);
176 top_level_folder
= model
->AddFolder(bookmark_bar
,
177 bookmark_bar
->child_count(),
180 parent
= top_level_folder
;
183 // Ensure any enclosing folders are present in the model. The bookmark's
184 // enclosing folder structure should be
185 // path[0] > path[1] > ... > path[size() - 1]
186 for (std::vector
<base::string16
>::const_iterator folder_name
=
187 bookmark
->path
.begin();
188 folder_name
!= bookmark
->path
.end(); ++folder_name
) {
189 if (bookmark
->in_toolbar
&& parent
== bookmark_bar
&&
190 folder_name
== bookmark
->path
.begin()) {
191 // If we're importing directly to the bookmarks bar, skip over the
192 // folder named "Bookmarks Toolbar" (or any non-Firefox equivalent).
196 const BookmarkNode
* child
= NULL
;
197 for (int index
= 0; index
< parent
->child_count(); ++index
) {
198 const BookmarkNode
* node
= parent
->GetChild(index
);
199 if (node
->is_folder() && node
->GetTitle() == *folder_name
) {
205 child
= model
->AddFolder(parent
, parent
->child_count(), *folder_name
);
209 folders_added_to
.insert(parent
);
210 if (bookmark
->is_folder
) {
211 model
->AddFolder(parent
, parent
->child_count(), bookmark
->title
);
213 model
->AddURLWithCreationTimeAndMetaInfo(parent
,
214 parent
->child_count(),
217 bookmark
->creation_time
,
222 // In order to keep the imported-to folders from appearing in the 'recently
223 // added to' combobox, reset their modified times.
224 for (std::set
<const BookmarkNode
*>::const_iterator i
=
225 folders_added_to
.begin();
226 i
!= folders_added_to
.end(); ++i
) {
227 model
->ResetDateFolderModified(*i
);
230 model
->EndExtensiveChanges();
232 // If the user was previously using a toolbar, we should show the bar.
233 if (import_to_top_level
&& !add_all_to_top_level
)
234 ShowBookmarkBar(profile_
);
237 void ProfileWriter::AddFavicons(
238 const favicon_base::FaviconUsageDataList
& favicons
) {
239 FaviconServiceFactory::GetForProfile(profile_
,
240 ServiceAccessType::EXPLICIT_ACCESS
)
241 ->SetImportedFavicons(favicons
);
244 typedef std::map
<std::string
, TemplateURL
*> HostPathMap
;
246 // Returns the key for the map built by BuildHostPathMap. If url_string is not
247 // a valid URL, an empty string is returned, otherwise host+path is returned.
248 static std::string
HostPathKeyForURL(const GURL
& url
) {
249 return url
.is_valid() ? url
.host() + url
.path() : std::string();
252 // Builds the key to use in HostPathMap for the specified TemplateURL. Returns
253 // an empty string if a host+path can't be generated for the TemplateURL.
254 // If an empty string is returned, the TemplateURL should not be added to
257 // If |try_url_if_invalid| is true, and |t_url| isn't valid, a string is built
258 // from the raw TemplateURL string. Use a value of true for |try_url_if_invalid|
259 // when checking imported URLs as the imported URL may not be valid yet may
260 // match the host+path of one of the default URLs. This is used to catch the
261 // case of IE using an invalid OSDD URL for Live Search, yet the host+path
262 // matches our prepopulate data. IE's URL for Live Search is something like
263 // 'http://...{Language}...'. As {Language} is not a valid OSDD parameter value
264 // the TemplateURL is invalid.
265 static std::string
BuildHostPathKey(const TemplateURL
* t_url
,
266 const SearchTermsData
& search_terms_data
,
267 bool try_url_if_invalid
) {
268 if (try_url_if_invalid
&& !t_url
->url_ref().IsValid(search_terms_data
))
269 return HostPathKeyForURL(GURL(t_url
->url()));
271 if (t_url
->url_ref().SupportsReplacement(search_terms_data
)) {
272 return HostPathKeyForURL(GURL(
273 t_url
->url_ref().ReplaceSearchTerms(
274 TemplateURLRef::SearchTermsArgs(base::ASCIIToUTF16("x")),
275 search_terms_data
)));
277 return std::string();
280 // Builds a set that contains an entry of the host+path for each TemplateURL in
281 // the TemplateURLService that has a valid search url.
282 static void BuildHostPathMap(TemplateURLService
* model
,
283 HostPathMap
* host_path_map
) {
284 TemplateURLService::TemplateURLVector template_urls
=
285 model
->GetTemplateURLs();
286 for (size_t i
= 0; i
< template_urls
.size(); ++i
) {
287 const std::string host_path
= BuildHostPathKey(
288 template_urls
[i
], model
->search_terms_data(), false);
289 if (!host_path
.empty()) {
290 const TemplateURL
* existing_turl
= (*host_path_map
)[host_path
];
291 if (!existing_turl
||
292 (template_urls
[i
]->show_in_default_list() &&
293 !existing_turl
->show_in_default_list())) {
294 // If there are multiple TemplateURLs with the same host+path, favor
295 // those shown in the default list. If there are multiple potential
296 // defaults, favor the first one, which should be the more commonly used
298 (*host_path_map
)[host_path
] = template_urls
[i
];
300 } // else case, TemplateURL doesn't have a search url, doesn't support
301 // replacement, or doesn't have valid GURL. Ignore it.
305 void ProfileWriter::AddKeywords(ScopedVector
<TemplateURL
> template_urls
,
306 bool unique_on_host_and_path
) {
307 TemplateURLService
* model
=
308 TemplateURLServiceFactory::GetForProfile(profile_
);
309 HostPathMap host_path_map
;
310 if (unique_on_host_and_path
)
311 BuildHostPathMap(model
, &host_path_map
);
313 for (ScopedVector
<TemplateURL
>::iterator i
= template_urls
.begin();
314 i
!= template_urls
.end(); ++i
) {
315 // TemplateURLService requires keywords to be unique. If there is already a
316 // TemplateURL with this keyword, don't import it again.
317 if (model
->GetTemplateURLForKeyword((*i
)->keyword()) != NULL
)
320 // For search engines if there is already a keyword with the same
321 // host+path, we don't import it. This is done to avoid both duplicate
322 // search providers (such as two Googles, or two Yahoos) as well as making
323 // sure the search engines we provide aren't replaced by those from the
325 if (unique_on_host_and_path
&&
326 (host_path_map
.find(BuildHostPathKey(
327 *i
, model
->search_terms_data(), true)) != host_path_map
.end()))
330 // Only add valid TemplateURLs to the model.
331 if ((*i
)->url_ref().IsValid(model
->search_terms_data())) {
332 model
->Add(*i
); // Takes ownership.
333 *i
= NULL
; // Prevent the vector from deleting *i later.
338 void ProfileWriter::AddAutofillFormDataEntries(
339 const std::vector
<autofill::AutofillEntry
>& autofill_entries
) {
340 scoped_refptr
<autofill::AutofillWebDataService
> web_data_service
=
341 WebDataServiceFactory::GetAutofillWebDataForProfile(
342 profile_
, ServiceAccessType::EXPLICIT_ACCESS
);
343 if (web_data_service
.get())
344 web_data_service
->UpdateAutofillEntries(autofill_entries
);
347 ProfileWriter::~ProfileWriter() {}