| 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 <map> | 7 #include <map> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
| 12 #include "base/stringprintf.h" | 12 #include "base/stringprintf.h" |
| 13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/threading/thread.h" | 14 #include "base/threading/thread.h" |
| 15 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
| 16 #include "chrome/browser/bookmarks/bookmark_model.h" | 16 #include "chrome/browser/bookmarks/bookmark_model.h" |
| 17 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 17 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
| 18 #include "chrome/browser/bookmarks/imported_bookmark_entry.h" |
| 18 #include "chrome/browser/favicon/favicon_service.h" | 19 #include "chrome/browser/favicon/favicon_service.h" |
| 19 #include "chrome/browser/favicon/favicon_service_factory.h" | 20 #include "chrome/browser/favicon/favicon_service_factory.h" |
| 21 #include "chrome/browser/favicon/imported_favicon_usage.h" |
| 20 #include "chrome/browser/history/history_service.h" | 22 #include "chrome/browser/history/history_service.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.h" | 24 #include "chrome/browser/password_manager/password_store.h" |
| 23 #include "chrome/browser/password_manager/password_store_factory.h" | 25 #include "chrome/browser/password_manager/password_store_factory.h" |
| 24 #include "chrome/browser/profiles/profile.h" | 26 #include "chrome/browser/profiles/profile.h" |
| 25 #include "chrome/browser/search_engines/template_url.h" | 27 #include "chrome/browser/search_engines/template_url.h" |
| 26 #include "chrome/browser/search_engines/template_url_service.h" | 28 #include "chrome/browser/search_engines/template_url_service.h" |
| 27 #include "chrome/browser/search_engines/template_url_service_factory.h" | 29 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 28 #include "chrome/browser/webdata/web_data_service.h" | 30 #include "chrome/browser/webdata/web_data_service.h" |
| 29 #include "chrome/common/chrome_notification_types.h" | 31 #include "chrome/common/chrome_notification_types.h" |
| (...skipping 30 matching lines...) Expand all Loading... |
| 60 return folder_name; | 62 return folder_name; |
| 61 } | 63 } |
| 62 | 64 |
| 63 // Shows the bookmarks toolbar. | 65 // Shows the bookmarks toolbar. |
| 64 void ShowBookmarkBar(Profile* profile) { | 66 void ShowBookmarkBar(Profile* profile) { |
| 65 profile->GetPrefs()->SetBoolean(prefs::kShowBookmarkBar, true); | 67 profile->GetPrefs()->SetBoolean(prefs::kShowBookmarkBar, true); |
| 66 } | 68 } |
| 67 | 69 |
| 68 } // namespace | 70 } // namespace |
| 69 | 71 |
| 70 ProfileWriter::BookmarkEntry::BookmarkEntry() | |
| 71 : in_toolbar(false), | |
| 72 is_folder(false) {} | |
| 73 | |
| 74 ProfileWriter::BookmarkEntry::~BookmarkEntry() {} | |
| 75 | |
| 76 bool ProfileWriter::BookmarkEntry::operator==( | |
| 77 const ProfileWriter::BookmarkEntry& other) const { | |
| 78 return (in_toolbar == other.in_toolbar && | |
| 79 is_folder == other.is_folder && | |
| 80 url == other.url && | |
| 81 path == other.path && | |
| 82 title == other.title && | |
| 83 creation_time == other.creation_time); | |
| 84 } | |
| 85 | |
| 86 ProfileWriter::ProfileWriter(Profile* profile) : profile_(profile) {} | 72 ProfileWriter::ProfileWriter(Profile* profile) : profile_(profile) {} |
| 87 | 73 |
| 88 bool ProfileWriter::BookmarkModelIsLoaded() const { | 74 bool ProfileWriter::BookmarkModelIsLoaded() const { |
| 89 return BookmarkModelFactory::GetForProfile(profile_)->loaded(); | 75 return BookmarkModelFactory::GetForProfile(profile_)->loaded(); |
| 90 } | 76 } |
| 91 | 77 |
| 92 bool ProfileWriter::TemplateURLServiceIsLoaded() const { | 78 bool ProfileWriter::TemplateURLServiceIsLoaded() const { |
| 93 return TemplateURLServiceFactory::GetForProfile(profile_)->loaded(); | 79 return TemplateURLServiceFactory::GetForProfile(profile_)->loaded(); |
| 94 } | 80 } |
| 95 | 81 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 114 DCHECK(profile_); | 100 DCHECK(profile_); |
| 115 | 101 |
| 116 PrefService* prefs = profile_->GetPrefs(); | 102 PrefService* prefs = profile_->GetPrefs(); |
| 117 // NOTE: We set the kHomePage value, but keep the NewTab page as the homepage. | 103 // NOTE: We set the kHomePage value, but keep the NewTab page as the homepage. |
| 118 const PrefService::Preference* pref = prefs->FindPreference(prefs::kHomePage); | 104 const PrefService::Preference* pref = prefs->FindPreference(prefs::kHomePage); |
| 119 if (pref && !pref->IsManaged()) { | 105 if (pref && !pref->IsManaged()) { |
| 120 prefs->SetString(prefs::kHomePage, home_page.spec()); | 106 prefs->SetString(prefs::kHomePage, home_page.spec()); |
| 121 } | 107 } |
| 122 } | 108 } |
| 123 | 109 |
| 124 void ProfileWriter::AddBookmarks(const std::vector<BookmarkEntry>& bookmarks, | 110 void ProfileWriter::AddBookmarks( |
| 125 const string16& top_level_folder_name) { | 111 const std::vector<ImportedBookmarkEntry>& bookmarks, |
| 112 const string16& top_level_folder_name) { |
| 126 if (bookmarks.empty()) | 113 if (bookmarks.empty()) |
| 127 return; | 114 return; |
| 128 | 115 |
| 129 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile_); | 116 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile_); |
| 130 DCHECK(model->loaded()); | 117 DCHECK(model->loaded()); |
| 131 | 118 |
| 132 // If the bookmark bar is currently empty, we should import directly to it. | 119 // If the bookmark bar is currently empty, we should import directly to it. |
| 133 // Otherwise, we should import everything to a subfolder. | 120 // Otherwise, we should import everything to a subfolder. |
| 134 const BookmarkNode* bookmark_bar = model->bookmark_bar_node(); | 121 const BookmarkNode* bookmark_bar = model->bookmark_bar_node(); |
| 135 bool import_to_top_level = bookmark_bar->empty(); | 122 bool import_to_top_level = bookmark_bar->empty(); |
| 136 | 123 |
| 137 // Reorder bookmarks so that the toolbar entries come first. | 124 // Reorder bookmarks so that the toolbar entries come first. |
| 138 std::vector<BookmarkEntry> toolbar_bookmarks; | 125 std::vector<ImportedBookmarkEntry> toolbar_bookmarks; |
| 139 std::vector<BookmarkEntry> reordered_bookmarks; | 126 std::vector<ImportedBookmarkEntry> reordered_bookmarks; |
| 140 for (std::vector<BookmarkEntry>::const_iterator it = bookmarks.begin(); | 127 for (std::vector<ImportedBookmarkEntry>::const_iterator it = |
| 128 bookmarks.begin(); |
| 141 it != bookmarks.end(); ++it) { | 129 it != bookmarks.end(); ++it) { |
| 142 if (it->in_toolbar) | 130 if (it->in_toolbar) |
| 143 toolbar_bookmarks.push_back(*it); | 131 toolbar_bookmarks.push_back(*it); |
| 144 else | 132 else |
| 145 reordered_bookmarks.push_back(*it); | 133 reordered_bookmarks.push_back(*it); |
| 146 } | 134 } |
| 147 reordered_bookmarks.insert(reordered_bookmarks.begin(), | 135 reordered_bookmarks.insert(reordered_bookmarks.begin(), |
| 148 toolbar_bookmarks.begin(), | 136 toolbar_bookmarks.begin(), |
| 149 toolbar_bookmarks.end()); | 137 toolbar_bookmarks.end()); |
| 150 | 138 |
| 151 // If the user currently has no bookmarks in the bookmark bar, make sure that | 139 // If the user currently has no bookmarks in the bookmark bar, make sure that |
| 152 // at least some of the imported bookmarks end up there. Otherwise, we'll end | 140 // at least some of the imported bookmarks end up there. Otherwise, we'll end |
| 153 // up with just a single folder containing the imported bookmarks, which makes | 141 // up with just a single folder containing the imported bookmarks, which makes |
| 154 // for unnecessary nesting. | 142 // for unnecessary nesting. |
| 155 bool add_all_to_top_level = import_to_top_level && toolbar_bookmarks.empty(); | 143 bool add_all_to_top_level = import_to_top_level && toolbar_bookmarks.empty(); |
| 156 | 144 |
| 157 model->BeginExtensiveChanges(); | 145 model->BeginExtensiveChanges(); |
| 158 | 146 |
| 159 std::set<const BookmarkNode*> folders_added_to; | 147 std::set<const BookmarkNode*> folders_added_to; |
| 160 const BookmarkNode* top_level_folder = NULL; | 148 const BookmarkNode* top_level_folder = NULL; |
| 161 for (std::vector<BookmarkEntry>::const_iterator bookmark = | 149 for (std::vector<ImportedBookmarkEntry>::const_iterator bookmark = |
| 162 reordered_bookmarks.begin(); | 150 reordered_bookmarks.begin(); |
| 163 bookmark != reordered_bookmarks.end(); ++bookmark) { | 151 bookmark != reordered_bookmarks.end(); ++bookmark) { |
| 164 // Disregard any bookmarks with invalid urls. | 152 // Disregard any bookmarks with invalid urls. |
| 165 if (!bookmark->is_folder && !bookmark->url.is_valid()) | 153 if (!bookmark->is_folder && !bookmark->url.is_valid()) |
| 166 continue; | 154 continue; |
| 167 | 155 |
| 168 const BookmarkNode* parent = NULL; | 156 const BookmarkNode* parent = NULL; |
| 169 if (import_to_top_level && (add_all_to_top_level || bookmark->in_toolbar)) { | 157 if (import_to_top_level && (add_all_to_top_level || bookmark->in_toolbar)) { |
| 170 // Add directly to the bookmarks bar. | 158 // Add directly to the bookmarks bar. |
| 171 parent = bookmark_bar; | 159 parent = bookmark_bar; |
| 172 } else { | 160 } else { |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 } | 214 } |
| 227 | 215 |
| 228 model->EndExtensiveChanges(); | 216 model->EndExtensiveChanges(); |
| 229 | 217 |
| 230 // If the user was previously using a toolbar, we should show the bar. | 218 // If the user was previously using a toolbar, we should show the bar. |
| 231 if (import_to_top_level && !add_all_to_top_level) | 219 if (import_to_top_level && !add_all_to_top_level) |
| 232 ShowBookmarkBar(profile_); | 220 ShowBookmarkBar(profile_); |
| 233 } | 221 } |
| 234 | 222 |
| 235 void ProfileWriter::AddFavicons( | 223 void ProfileWriter::AddFavicons( |
| 236 const std::vector<history::ImportedFaviconUsage>& favicons) { | 224 const std::vector<ImportedFaviconUsage>& favicons) { |
| 237 FaviconServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS)-> | 225 FaviconServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS)-> |
| 238 SetImportedFavicons(favicons); | 226 SetImportedFavicons(favicons); |
| 239 } | 227 } |
| 240 | 228 |
| 241 typedef std::map<std::string, TemplateURL*> HostPathMap; | 229 typedef std::map<std::string, TemplateURL*> HostPathMap; |
| 242 | 230 |
| 243 // Returns the key for the map built by BuildHostPathMap. If url_string is not | 231 // Returns the key for the map built by BuildHostPathMap. If url_string is not |
| 244 // a valid URL, an empty string is returned, otherwise host+path is returned. | 232 // a valid URL, an empty string is returned, otherwise host+path is returned. |
| 245 static std::string HostPathKeyForURL(const GURL& url) { | 233 static std::string HostPathKeyForURL(const GURL& url) { |
| 246 return url.is_valid() ? url.host() + url.path() : std::string(); | 234 return url.is_valid() ? url.host() + url.path() : std::string(); |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 322 | 310 |
| 323 // Only add valid TemplateURLs to the model. | 311 // Only add valid TemplateURLs to the model. |
| 324 if ((*i)->url_ref().IsValid()) { | 312 if ((*i)->url_ref().IsValid()) { |
| 325 model->AddAndSetProfile(*i, profile_); // Takes ownership. | 313 model->AddAndSetProfile(*i, profile_); // Takes ownership. |
| 326 *i = NULL; // Prevent the vector from deleting *i later. | 314 *i = NULL; // Prevent the vector from deleting *i later. |
| 327 } | 315 } |
| 328 } | 316 } |
| 329 } | 317 } |
| 330 | 318 |
| 331 ProfileWriter::~ProfileWriter() {} | 319 ProfileWriter::~ProfileWriter() {} |
| OLD | NEW |