• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <map>
8 #include <set>
9 #include <string>
10 
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/browser/bookmark_model.h"
32 #include "components/password_manager/core/browser/password_store.h"
33 
34 namespace {
35 
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.
GenerateUniqueFolderName(BookmarkModel * model,const base::string16 & folder_name)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());
47   }
48 
49   // If the given name is unique, use it.
50   if (existing_folder_names.find(folder_name) == existing_folder_names.end())
51     return folder_name;
52 
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())
58       return name;
59   }
60 
61   NOTREACHED();
62   return folder_name;
63 }
64 
65 // Shows the bookmarks toolbar.
ShowBookmarkBar(Profile * profile)66 void ShowBookmarkBar(Profile* profile) {
67   profile->GetPrefs()->SetBoolean(prefs::kShowBookmarkBar, true);
68 }
69 
70 }  // namespace
71 
ProfileWriter(Profile * profile)72 ProfileWriter::ProfileWriter(Profile* profile) : profile_(profile) {}
73 
BookmarkModelIsLoaded() const74 bool ProfileWriter::BookmarkModelIsLoaded() const {
75   return BookmarkModelFactory::GetForProfile(profile_)->loaded();
76 }
77 
TemplateURLServiceIsLoaded() const78 bool ProfileWriter::TemplateURLServiceIsLoaded() const {
79   return TemplateURLServiceFactory::GetForProfile(profile_)->loaded();
80 }
81 
AddPasswordForm(const autofill::PasswordForm & form)82 void ProfileWriter::AddPasswordForm(const autofill::PasswordForm& form) {
83   PasswordStoreFactory::GetForProfile(
84       profile_, Profile::EXPLICIT_ACCESS)->AddLogin(form);
85 }
86 
87 #if defined(OS_WIN)
AddIE7PasswordInfo(const IE7PasswordInfo & info)88 void ProfileWriter::AddIE7PasswordInfo(const IE7PasswordInfo& info) {
89   WebDataService::FromBrowserContext(profile_)->AddIE7Login(info);
90 }
91 #endif
92 
AddHistoryPage(const history::URLRows & page,history::VisitSource visit_source)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);
97 }
98 
AddHomepage(const GURL & home_page)99 void ProfileWriter::AddHomepage(const GURL& home_page) {
100   DCHECK(profile_);
101 
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());
107   }
108 }
109 
AddBookmarks(const std::vector<ImportedBookmarkEntry> & bookmarks,const base::string16 & top_level_folder_name)110 void ProfileWriter::AddBookmarks(
111     const std::vector<ImportedBookmarkEntry>& bookmarks,
112     const base::string16& top_level_folder_name) {
113   if (bookmarks.empty())
114     return;
115 
116   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile_);
117   DCHECK(model->loaded());
118 
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();
123 
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 =
128            bookmarks.begin();
129        it != bookmarks.end(); ++it) {
130     if (it->in_toolbar)
131       toolbar_bookmarks.push_back(*it);
132     else
133       reordered_bookmarks.push_back(*it);
134   }
135   reordered_bookmarks.insert(reordered_bookmarks.begin(),
136                              toolbar_bookmarks.begin(),
137                              toolbar_bookmarks.end());
138 
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();
144 
145   model->BeginExtensiveChanges();
146 
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())
154       continue;
155 
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;
160     } else {
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(),
168                                             name);
169       }
170       parent = top_level_folder;
171     }
172 
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).
183         continue;
184       }
185 
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) {
190           child = node;
191           break;
192         }
193       }
194       if (!child)
195         child = model->AddFolder(parent, parent->child_count(), *folder_name);
196       parent = child;
197     }
198 
199     folders_added_to.insert(parent);
200     if (bookmark->is_folder) {
201       model->AddFolder(parent, parent->child_count(), bookmark->title);
202     } else {
203       model->AddURLWithCreationTimeAndMetaInfo(parent,
204                                                parent->child_count(),
205                                                bookmark->title,
206                                                bookmark->url,
207                                                bookmark->creation_time,
208                                                NULL);
209     }
210   }
211 
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);
218   }
219 
220   model->EndExtensiveChanges();
221 
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_);
225 }
226 
AddFavicons(const std::vector<ImportedFaviconUsage> & favicons)227 void ProfileWriter::AddFavicons(
228     const std::vector<ImportedFaviconUsage>& favicons) {
229   FaviconServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS)->
230       SetImportedFavicons(favicons);
231 }
232 
233 typedef std::map<std::string, TemplateURL*> HostPathMap;
234 
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.
HostPathKeyForURL(const GURL & url)237 static std::string HostPathKeyForURL(const GURL& url) {
238   return url.is_valid() ? url.host() + url.path() : std::string();
239 }
240 
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
244 // HostPathMap.
245 //
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.
BuildHostPathKey(const TemplateURL * t_url,const SearchTermsData & search_terms_data,bool try_url_if_invalid)254 static std::string BuildHostPathKey(const TemplateURL* t_url,
255                                     const SearchTermsData& search_terms_data,
256                                     bool try_url_if_invalid) {
257   if (try_url_if_invalid && !t_url->url_ref().IsValid(search_terms_data))
258     return HostPathKeyForURL(GURL(t_url->url()));
259 
260   if (t_url->url_ref().SupportsReplacement(search_terms_data)) {
261     return HostPathKeyForURL(GURL(
262         t_url->url_ref().ReplaceSearchTerms(
263             TemplateURLRef::SearchTermsArgs(base::ASCIIToUTF16("x")),
264             search_terms_data)));
265   }
266   return std::string();
267 }
268 
269 // Builds a set that contains an entry of the host+path for each TemplateURL in
270 // the TemplateURLService that has a valid search url.
BuildHostPathMap(TemplateURLService * model,HostPathMap * host_path_map)271 static void BuildHostPathMap(TemplateURLService* model,
272                              HostPathMap* host_path_map) {
273   TemplateURLService::TemplateURLVector template_urls =
274       model->GetTemplateURLs();
275   for (size_t i = 0; i < template_urls.size(); ++i) {
276     const std::string host_path = BuildHostPathKey(
277         template_urls[i], model->search_terms_data(), false);
278     if (!host_path.empty()) {
279       const TemplateURL* existing_turl = (*host_path_map)[host_path];
280       if (!existing_turl ||
281           (template_urls[i]->show_in_default_list() &&
282            !existing_turl->show_in_default_list())) {
283         // If there are multiple TemplateURLs with the same host+path, favor
284         // those shown in the default list.  If there are multiple potential
285         // defaults, favor the first one, which should be the more commonly used
286         // one.
287         (*host_path_map)[host_path] = template_urls[i];
288       }
289     }  // else case, TemplateURL doesn't have a search url, doesn't support
290        // replacement, or doesn't have valid GURL. Ignore it.
291   }
292 }
293 
AddKeywords(ScopedVector<TemplateURL> template_urls,bool unique_on_host_and_path)294 void ProfileWriter::AddKeywords(ScopedVector<TemplateURL> template_urls,
295                                 bool unique_on_host_and_path) {
296   TemplateURLService* model =
297       TemplateURLServiceFactory::GetForProfile(profile_);
298   HostPathMap host_path_map;
299   if (unique_on_host_and_path)
300     BuildHostPathMap(model, &host_path_map);
301 
302   for (ScopedVector<TemplateURL>::iterator i = template_urls.begin();
303        i != template_urls.end(); ++i) {
304     // TemplateURLService requires keywords to be unique. If there is already a
305     // TemplateURL with this keyword, don't import it again.
306     if (model->GetTemplateURLForKeyword((*i)->keyword()) != NULL)
307       continue;
308 
309     // For search engines if there is already a keyword with the same
310     // host+path, we don't import it. This is done to avoid both duplicate
311     // search providers (such as two Googles, or two Yahoos) as well as making
312     // sure the search engines we provide aren't replaced by those from the
313     // imported browser.
314     if (unique_on_host_and_path &&
315         (host_path_map.find(BuildHostPathKey(
316             *i, model->search_terms_data(), true)) != host_path_map.end()))
317       continue;
318 
319     // Only add valid TemplateURLs to the model.
320     if ((*i)->url_ref().IsValid(model->search_terms_data())) {
321       model->Add(*i);  // Takes ownership.
322       *i = NULL;  // Prevent the vector from deleting *i later.
323     }
324   }
325 }
326 
~ProfileWriter()327 ProfileWriter::~ProfileWriter() {}
328