Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / importer / profile_writer.cc
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.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.h"
20 #include "chrome/browser/favicon/favicon_service_factory.h"
21 #include "chrome/browser/history/history_service.h"
22 #include "chrome/browser/history/history_service_factory.h"
23 #include "chrome/browser/password_manager/password_store_factory.h"
24 #include "chrome/browser/profiles/profile.h"
25 #include "chrome/browser/search_engines/template_url.h"
26 #include "chrome/browser/search_engines/template_url_service.h"
27 #include "chrome/browser/search_engines/template_url_service_factory.h"
28 #include "chrome/browser/webdata/web_data_service.h"
29 #include "chrome/common/importer/imported_bookmark_entry.h"
30 #include "chrome/common/importer/imported_favicon_usage.h"
31 #include "chrome/common/pref_names.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.
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.
66 void ShowBookmarkBar(Profile* profile) {
67   profile->GetPrefs()->SetBoolean(prefs::kShowBookmarkBar, true);
68 }
69
70 }  // namespace
71
72 ProfileWriter::ProfileWriter(Profile* profile) : profile_(profile) {}
73
74 bool ProfileWriter::BookmarkModelIsLoaded() const {
75   return BookmarkModelFactory::GetForProfile(profile_)->loaded();
76 }
77
78 bool ProfileWriter::TemplateURLServiceIsLoaded() const {
79   return TemplateURLServiceFactory::GetForProfile(profile_)->loaded();
80 }
81
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)
88 void ProfileWriter::AddIE7PasswordInfo(const IE7PasswordInfo& info) {
89   WebDataService::FromBrowserContext(profile_)->AddIE7Login(info);
90 }
91 #endif
92
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
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
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->AddURLWithCreationTime(parent, parent->child_count(),
204                                     bookmark->title, bookmark->url,
205                                     bookmark->creation_time);
206     }
207   }
208
209   // In order to keep the imported-to folders from appearing in the 'recently
210   // added to' combobox, reset their modified times.
211   for (std::set<const BookmarkNode*>::const_iterator i =
212            folders_added_to.begin();
213        i != folders_added_to.end(); ++i) {
214     model->ResetDateFolderModified(*i);
215   }
216
217   model->EndExtensiveChanges();
218
219   // If the user was previously using a toolbar, we should show the bar.
220   if (import_to_top_level && !add_all_to_top_level)
221     ShowBookmarkBar(profile_);
222 }
223
224 void ProfileWriter::AddFavicons(
225     const std::vector<ImportedFaviconUsage>& favicons) {
226   FaviconServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS)->
227       SetImportedFavicons(favicons);
228 }
229
230 typedef std::map<std::string, TemplateURL*> HostPathMap;
231
232 // Returns the key for the map built by BuildHostPathMap. If url_string is not
233 // a valid URL, an empty string is returned, otherwise host+path is returned.
234 static std::string HostPathKeyForURL(const GURL& url) {
235   return url.is_valid() ? url.host() + url.path() : std::string();
236 }
237
238 // Builds the key to use in HostPathMap for the specified TemplateURL. Returns
239 // an empty string if a host+path can't be generated for the TemplateURL.
240 // If an empty string is returned, the TemplateURL should not be added to
241 // HostPathMap.
242 //
243 // If |try_url_if_invalid| is true, and |t_url| isn't valid, a string is built
244 // from the raw TemplateURL string. Use a value of true for |try_url_if_invalid|
245 // when checking imported URLs as the imported URL may not be valid yet may
246 // match the host+path of one of the default URLs. This is used to catch the
247 // case of IE using an invalid OSDD URL for Live Search, yet the host+path
248 // matches our prepopulate data. IE's URL for Live Search is something like
249 // 'http://...{Language}...'. As {Language} is not a valid OSDD parameter value
250 // the TemplateURL is invalid.
251 static std::string BuildHostPathKey(const TemplateURL* t_url,
252                                     bool try_url_if_invalid) {
253   if (try_url_if_invalid && !t_url->url_ref().IsValid())
254     return HostPathKeyForURL(GURL(t_url->url()));
255
256   if (t_url->url_ref().SupportsReplacement()) {
257     return HostPathKeyForURL(GURL(
258         t_url->url_ref().ReplaceSearchTerms(
259             TemplateURLRef::SearchTermsArgs(base::ASCIIToUTF16("x")))));
260   }
261   return std::string();
262 }
263
264 // Builds a set that contains an entry of the host+path for each TemplateURL in
265 // the TemplateURLService that has a valid search url.
266 static void BuildHostPathMap(TemplateURLService* model,
267                              HostPathMap* host_path_map) {
268   TemplateURLService::TemplateURLVector template_urls =
269       model->GetTemplateURLs();
270   for (size_t i = 0; i < template_urls.size(); ++i) {
271     const std::string host_path = BuildHostPathKey(template_urls[i], false);
272     if (!host_path.empty()) {
273       const TemplateURL* existing_turl = (*host_path_map)[host_path];
274       if (!existing_turl ||
275           (template_urls[i]->show_in_default_list() &&
276            !existing_turl->show_in_default_list())) {
277         // If there are multiple TemplateURLs with the same host+path, favor
278         // those shown in the default list.  If there are multiple potential
279         // defaults, favor the first one, which should be the more commonly used
280         // one.
281         (*host_path_map)[host_path] = template_urls[i];
282       }
283     }  // else case, TemplateURL doesn't have a search url, doesn't support
284        // replacement, or doesn't have valid GURL. Ignore it.
285   }
286 }
287
288 void ProfileWriter::AddKeywords(ScopedVector<TemplateURL> template_urls,
289                                 bool unique_on_host_and_path) {
290   TemplateURLService* model =
291       TemplateURLServiceFactory::GetForProfile(profile_);
292   HostPathMap host_path_map;
293   if (unique_on_host_and_path)
294     BuildHostPathMap(model, &host_path_map);
295
296   for (ScopedVector<TemplateURL>::iterator i = template_urls.begin();
297        i != template_urls.end(); ++i) {
298     // TemplateURLService requires keywords to be unique. If there is already a
299     // TemplateURL with this keyword, don't import it again.
300     if (model->GetTemplateURLForKeyword((*i)->keyword()) != NULL)
301       continue;
302
303     // For search engines if there is already a keyword with the same
304     // host+path, we don't import it. This is done to avoid both duplicate
305     // search providers (such as two Googles, or two Yahoos) as well as making
306     // sure the search engines we provide aren't replaced by those from the
307     // imported browser.
308     if (unique_on_host_and_path &&
309         (host_path_map.find(BuildHostPathKey(*i, true)) != host_path_map.end()))
310       continue;
311
312     // Only add valid TemplateURLs to the model.
313     if ((*i)->url_ref().IsValid()) {
314       model->AddAndSetProfile(*i, profile_);  // Takes ownership.
315       *i = NULL;  // Prevent the vector from deleting *i later.
316     }
317   }
318 }
319
320 ProfileWriter::~ProfileWriter() {}