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.h"
19 #include "chrome/browser/favicon/favicon_service_factory.h"
20 #include "chrome/browser/history/history_service.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.h"
25 #include "chrome/browser/search_engines/template_url_service.h"
26 #include "chrome/browser/search_engines/template_url_service_factory.h"
27 #include "chrome/browser/webdata/web_data_service.h"
28 #include "chrome/common/importer/imported_bookmark_entry.h"
29 #include "chrome/common/importer/imported_favicon_usage.h"
30 #include "chrome/common/pref_names.h"
31 #include "components/bookmarks/core/browser/bookmark_model.h"
32 #include "components/password_manager/core/browser/password_store.h"
36 // Generates a unique folder name. If |folder_name| is not unique, then this
37 // repeatedly tests for '|folder_name| + (i)' until a unique name is found.
38 base::string16
GenerateUniqueFolderName(BookmarkModel
* model
,
39 const base::string16
& folder_name
) {
40 // Build a set containing the bookmark bar folder names.
41 std::set
<base::string16
> existing_folder_names
;
42 const BookmarkNode
* bookmark_bar
= model
->bookmark_bar_node();
43 for (int i
= 0; i
< bookmark_bar
->child_count(); ++i
) {
44 const BookmarkNode
* node
= bookmark_bar
->GetChild(i
);
45 if (node
->is_folder())
46 existing_folder_names
.insert(node
->GetTitle());
49 // If the given name is unique, use it.
50 if (existing_folder_names
.find(folder_name
) == existing_folder_names
.end())
53 // Otherwise iterate until we find a unique name.
54 for (size_t i
= 1; i
<= existing_folder_names
.size(); ++i
) {
55 base::string16 name
= folder_name
+ base::ASCIIToUTF16(" (") +
56 base::IntToString16(i
) + base::ASCIIToUTF16(")");
57 if (existing_folder_names
.find(name
) == existing_folder_names
.end())
65 // Shows the bookmarks toolbar.
66 void ShowBookmarkBar(Profile
* profile
) {
67 profile
->GetPrefs()->SetBoolean(prefs::kShowBookmarkBar
, true);
72 ProfileWriter::ProfileWriter(Profile
* profile
) : profile_(profile
) {}
74 bool ProfileWriter::BookmarkModelIsLoaded() const {
75 return BookmarkModelFactory::GetForProfile(profile_
)->loaded();
78 bool ProfileWriter::TemplateURLServiceIsLoaded() const {
79 return TemplateURLServiceFactory::GetForProfile(profile_
)->loaded();
82 void ProfileWriter::AddPasswordForm(const autofill::PasswordForm
& form
) {
83 PasswordStoreFactory::GetForProfile(
84 profile_
, Profile::EXPLICIT_ACCESS
)->AddLogin(form
);
88 void ProfileWriter::AddIE7PasswordInfo(const IE7PasswordInfo
& info
) {
89 WebDataService::FromBrowserContext(profile_
)->AddIE7Login(info
);
93 void ProfileWriter::AddHistoryPage(const history::URLRows
& page
,
94 history::VisitSource visit_source
) {
95 HistoryServiceFactory::GetForProfile(profile_
, Profile::EXPLICIT_ACCESS
)->
96 AddPagesWithDetails(page
, visit_source
);
99 void ProfileWriter::AddHomepage(const GURL
& home_page
) {
102 PrefService
* prefs
= profile_
->GetPrefs();
103 // NOTE: We set the kHomePage value, but keep the NewTab page as the homepage.
104 const PrefService::Preference
* pref
= prefs
->FindPreference(prefs::kHomePage
);
105 if (pref
&& !pref
->IsManaged()) {
106 prefs
->SetString(prefs::kHomePage
, home_page
.spec());
110 void ProfileWriter::AddBookmarks(
111 const std::vector
<ImportedBookmarkEntry
>& bookmarks
,
112 const base::string16
& top_level_folder_name
) {
113 if (bookmarks
.empty())
116 BookmarkModel
* model
= BookmarkModelFactory::GetForProfile(profile_
);
117 DCHECK(model
->loaded());
119 // If the bookmark bar is currently empty, we should import directly to it.
120 // Otherwise, we should import everything to a subfolder.
121 const BookmarkNode
* bookmark_bar
= model
->bookmark_bar_node();
122 bool import_to_top_level
= bookmark_bar
->empty();
124 // Reorder bookmarks so that the toolbar entries come first.
125 std::vector
<ImportedBookmarkEntry
> toolbar_bookmarks
;
126 std::vector
<ImportedBookmarkEntry
> reordered_bookmarks
;
127 for (std::vector
<ImportedBookmarkEntry
>::const_iterator it
=
129 it
!= bookmarks
.end(); ++it
) {
131 toolbar_bookmarks
.push_back(*it
);
133 reordered_bookmarks
.push_back(*it
);
135 reordered_bookmarks
.insert(reordered_bookmarks
.begin(),
136 toolbar_bookmarks
.begin(),
137 toolbar_bookmarks
.end());
139 // If the user currently has no bookmarks in the bookmark bar, make sure that
140 // at least some of the imported bookmarks end up there. Otherwise, we'll end
141 // up with just a single folder containing the imported bookmarks, which makes
142 // for unnecessary nesting.
143 bool add_all_to_top_level
= import_to_top_level
&& toolbar_bookmarks
.empty();
145 model
->BeginExtensiveChanges();
147 std::set
<const BookmarkNode
*> folders_added_to
;
148 const BookmarkNode
* top_level_folder
= NULL
;
149 for (std::vector
<ImportedBookmarkEntry
>::const_iterator bookmark
=
150 reordered_bookmarks
.begin();
151 bookmark
!= reordered_bookmarks
.end(); ++bookmark
) {
152 // Disregard any bookmarks with invalid urls.
153 if (!bookmark
->is_folder
&& !bookmark
->url
.is_valid())
156 const BookmarkNode
* parent
= NULL
;
157 if (import_to_top_level
&& (add_all_to_top_level
|| bookmark
->in_toolbar
)) {
158 // Add directly to the bookmarks bar.
159 parent
= bookmark_bar
;
161 // Add to a folder that will contain all the imported bookmarks not added
162 // to the bar. The first time we do so, create the folder.
163 if (!top_level_folder
) {
164 base::string16 name
=
165 GenerateUniqueFolderName(model
,top_level_folder_name
);
166 top_level_folder
= model
->AddFolder(bookmark_bar
,
167 bookmark_bar
->child_count(),
170 parent
= top_level_folder
;
173 // Ensure any enclosing folders are present in the model. The bookmark's
174 // enclosing folder structure should be
175 // path[0] > path[1] > ... > path[size() - 1]
176 for (std::vector
<base::string16
>::const_iterator folder_name
=
177 bookmark
->path
.begin();
178 folder_name
!= bookmark
->path
.end(); ++folder_name
) {
179 if (bookmark
->in_toolbar
&& parent
== bookmark_bar
&&
180 folder_name
== bookmark
->path
.begin()) {
181 // If we're importing directly to the bookmarks bar, skip over the
182 // folder named "Bookmarks Toolbar" (or any non-Firefox equivalent).
186 const BookmarkNode
* child
= NULL
;
187 for (int index
= 0; index
< parent
->child_count(); ++index
) {
188 const BookmarkNode
* node
= parent
->GetChild(index
);
189 if (node
->is_folder() && node
->GetTitle() == *folder_name
) {
195 child
= model
->AddFolder(parent
, parent
->child_count(), *folder_name
);
199 folders_added_to
.insert(parent
);
200 if (bookmark
->is_folder
) {
201 model
->AddFolder(parent
, parent
->child_count(), bookmark
->title
);
203 model
->AddURLWithCreationTimeAndMetaInfo(parent
,
204 parent
->child_count(),
207 bookmark
->creation_time
,
212 // In order to keep the imported-to folders from appearing in the 'recently
213 // added to' combobox, reset their modified times.
214 for (std::set
<const BookmarkNode
*>::const_iterator i
=
215 folders_added_to
.begin();
216 i
!= folders_added_to
.end(); ++i
) {
217 model
->ResetDateFolderModified(*i
);
220 model
->EndExtensiveChanges();
222 // If the user was previously using a toolbar, we should show the bar.
223 if (import_to_top_level
&& !add_all_to_top_level
)
224 ShowBookmarkBar(profile_
);
227 void ProfileWriter::AddFavicons(
228 const std::vector
<ImportedFaviconUsage
>& favicons
) {
229 FaviconServiceFactory::GetForProfile(profile_
, Profile::EXPLICIT_ACCESS
)->
230 SetImportedFavicons(favicons
);
233 typedef std::map
<std::string
, TemplateURL
*> HostPathMap
;
235 // Returns the key for the map built by BuildHostPathMap. If url_string is not
236 // a valid URL, an empty string is returned, otherwise host+path is returned.
237 static std::string
HostPathKeyForURL(const GURL
& url
) {
238 return url
.is_valid() ? url
.host() + url
.path() : std::string();
241 // Builds the key to use in HostPathMap for the specified TemplateURL. Returns
242 // an empty string if a host+path can't be generated for the TemplateURL.
243 // If an empty string is returned, the TemplateURL should not be added to
246 // If |try_url_if_invalid| is true, and |t_url| isn't valid, a string is built
247 // from the raw TemplateURL string. Use a value of true for |try_url_if_invalid|
248 // when checking imported URLs as the imported URL may not be valid yet may
249 // match the host+path of one of the default URLs. This is used to catch the
250 // case of IE using an invalid OSDD URL for Live Search, yet the host+path
251 // matches our prepopulate data. IE's URL for Live Search is something like
252 // 'http://...{Language}...'. As {Language} is not a valid OSDD parameter value
253 // the TemplateURL is invalid.
254 static std::string
BuildHostPathKey(const TemplateURL
* t_url
,
255 bool try_url_if_invalid
) {
256 if (try_url_if_invalid
&& !t_url
->url_ref().IsValid())
257 return HostPathKeyForURL(GURL(t_url
->url()));
259 if (t_url
->url_ref().SupportsReplacement()) {
260 return HostPathKeyForURL(GURL(
261 t_url
->url_ref().ReplaceSearchTerms(
262 TemplateURLRef::SearchTermsArgs(base::ASCIIToUTF16("x")))));
264 return std::string();
267 // Builds a set that contains an entry of the host+path for each TemplateURL in
268 // the TemplateURLService that has a valid search url.
269 static void BuildHostPathMap(TemplateURLService
* model
,
270 HostPathMap
* host_path_map
) {
271 TemplateURLService::TemplateURLVector template_urls
=
272 model
->GetTemplateURLs();
273 for (size_t i
= 0; i
< template_urls
.size(); ++i
) {
274 const std::string host_path
= BuildHostPathKey(template_urls
[i
], false);
275 if (!host_path
.empty()) {
276 const TemplateURL
* existing_turl
= (*host_path_map
)[host_path
];
277 if (!existing_turl
||
278 (template_urls
[i
]->show_in_default_list() &&
279 !existing_turl
->show_in_default_list())) {
280 // If there are multiple TemplateURLs with the same host+path, favor
281 // those shown in the default list. If there are multiple potential
282 // defaults, favor the first one, which should be the more commonly used
284 (*host_path_map
)[host_path
] = template_urls
[i
];
286 } // else case, TemplateURL doesn't have a search url, doesn't support
287 // replacement, or doesn't have valid GURL. Ignore it.
291 void ProfileWriter::AddKeywords(ScopedVector
<TemplateURL
> template_urls
,
292 bool unique_on_host_and_path
) {
293 TemplateURLService
* model
=
294 TemplateURLServiceFactory::GetForProfile(profile_
);
295 HostPathMap host_path_map
;
296 if (unique_on_host_and_path
)
297 BuildHostPathMap(model
, &host_path_map
);
299 for (ScopedVector
<TemplateURL
>::iterator i
= template_urls
.begin();
300 i
!= template_urls
.end(); ++i
) {
301 // TemplateURLService requires keywords to be unique. If there is already a
302 // TemplateURL with this keyword, don't import it again.
303 if (model
->GetTemplateURLForKeyword((*i
)->keyword()) != NULL
)
306 // For search engines if there is already a keyword with the same
307 // host+path, we don't import it. This is done to avoid both duplicate
308 // search providers (such as two Googles, or two Yahoos) as well as making
309 // sure the search engines we provide aren't replaced by those from the
311 if (unique_on_host_and_path
&&
312 (host_path_map
.find(BuildHostPathKey(*i
, true)) != host_path_map
.end()))
315 // Only add valid TemplateURLs to the model.
316 if ((*i
)->url_ref().IsValid()) {
317 model
->AddAndSetProfile(*i
, profile_
); // Takes ownership.
318 *i
= NULL
; // Prevent the vector from deleting *i later.
323 ProfileWriter::~ProfileWriter() {}