Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(277)

Side by Side Diff: chrome/browser/importer/profile_writer.cc

Issue 14575004: Extract BookmarksFileImporter from Firefox2Importer. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: win fix again Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
20 #include "chrome/browser/history/history_service.h" 21 #include "chrome/browser/history/history_service.h"
21 #include "chrome/browser/history/history_service_factory.h" 22 #include "chrome/browser/history/history_service_factory.h"
22 #include "chrome/browser/password_manager/password_store.h" 23 #include "chrome/browser/password_manager/password_store.h"
23 #include "chrome/browser/password_manager/password_store_factory.h" 24 #include "chrome/browser/password_manager/password_store_factory.h"
24 #include "chrome/browser/profiles/profile.h" 25 #include "chrome/browser/profiles/profile.h"
25 #include "chrome/browser/search_engines/template_url.h" 26 #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.h"
27 #include "chrome/browser/search_engines/template_url_service_factory.h" 28 #include "chrome/browser/search_engines/template_url_service_factory.h"
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
60 return folder_name; 61 return folder_name;
61 } 62 }
62 63
63 // Shows the bookmarks toolbar. 64 // Shows the bookmarks toolbar.
64 void ShowBookmarkBar(Profile* profile) { 65 void ShowBookmarkBar(Profile* profile) {
65 profile->GetPrefs()->SetBoolean(prefs::kShowBookmarkBar, true); 66 profile->GetPrefs()->SetBoolean(prefs::kShowBookmarkBar, true);
66 } 67 }
67 68
68 } // namespace 69 } // namespace
69 70
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) {} 71 ProfileWriter::ProfileWriter(Profile* profile) : profile_(profile) {}
87 72
88 bool ProfileWriter::BookmarkModelIsLoaded() const { 73 bool ProfileWriter::BookmarkModelIsLoaded() const {
89 return BookmarkModelFactory::GetForProfile(profile_)->loaded(); 74 return BookmarkModelFactory::GetForProfile(profile_)->loaded();
90 } 75 }
91 76
92 bool ProfileWriter::TemplateURLServiceIsLoaded() const { 77 bool ProfileWriter::TemplateURLServiceIsLoaded() const {
93 return TemplateURLServiceFactory::GetForProfile(profile_)->loaded(); 78 return TemplateURLServiceFactory::GetForProfile(profile_)->loaded();
94 } 79 }
95 80
(...skipping 18 matching lines...) Expand all
114 DCHECK(profile_); 99 DCHECK(profile_);
115 100
116 PrefService* prefs = profile_->GetPrefs(); 101 PrefService* prefs = profile_->GetPrefs();
117 // NOTE: We set the kHomePage value, but keep the NewTab page as the homepage. 102 // NOTE: We set the kHomePage value, but keep the NewTab page as the homepage.
118 const PrefService::Preference* pref = prefs->FindPreference(prefs::kHomePage); 103 const PrefService::Preference* pref = prefs->FindPreference(prefs::kHomePage);
119 if (pref && !pref->IsManaged()) { 104 if (pref && !pref->IsManaged()) {
120 prefs->SetString(prefs::kHomePage, home_page.spec()); 105 prefs->SetString(prefs::kHomePage, home_page.spec());
121 } 106 }
122 } 107 }
123 108
124 void ProfileWriter::AddBookmarks(const std::vector<BookmarkEntry>& bookmarks, 109 void ProfileWriter::AddBookmarks(
125 const string16& top_level_folder_name) { 110 const std::vector<ImportedBookmarkEntry>& bookmarks,
111 const string16& top_level_folder_name) {
126 if (bookmarks.empty()) 112 if (bookmarks.empty())
127 return; 113 return;
128 114
129 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile_); 115 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile_);
130 DCHECK(model->loaded()); 116 DCHECK(model->loaded());
131 117
132 // If the bookmark bar is currently empty, we should import directly to it. 118 // If the bookmark bar is currently empty, we should import directly to it.
133 // Otherwise, we should import everything to a subfolder. 119 // Otherwise, we should import everything to a subfolder.
134 const BookmarkNode* bookmark_bar = model->bookmark_bar_node(); 120 const BookmarkNode* bookmark_bar = model->bookmark_bar_node();
135 bool import_to_top_level = bookmark_bar->empty(); 121 bool import_to_top_level = bookmark_bar->empty();
136 122
137 // Reorder bookmarks so that the toolbar entries come first. 123 // Reorder bookmarks so that the toolbar entries come first.
138 std::vector<BookmarkEntry> toolbar_bookmarks; 124 std::vector<ImportedBookmarkEntry> toolbar_bookmarks;
139 std::vector<BookmarkEntry> reordered_bookmarks; 125 std::vector<ImportedBookmarkEntry> reordered_bookmarks;
140 for (std::vector<BookmarkEntry>::const_iterator it = bookmarks.begin(); 126 for (std::vector<ImportedBookmarkEntry>::const_iterator it =
127 bookmarks.begin();
141 it != bookmarks.end(); ++it) { 128 it != bookmarks.end(); ++it) {
142 if (it->in_toolbar) 129 if (it->in_toolbar)
143 toolbar_bookmarks.push_back(*it); 130 toolbar_bookmarks.push_back(*it);
144 else 131 else
145 reordered_bookmarks.push_back(*it); 132 reordered_bookmarks.push_back(*it);
146 } 133 }
147 reordered_bookmarks.insert(reordered_bookmarks.begin(), 134 reordered_bookmarks.insert(reordered_bookmarks.begin(),
148 toolbar_bookmarks.begin(), 135 toolbar_bookmarks.begin(),
149 toolbar_bookmarks.end()); 136 toolbar_bookmarks.end());
150 137
151 // If the user currently has no bookmarks in the bookmark bar, make sure that 138 // 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 139 // 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 140 // up with just a single folder containing the imported bookmarks, which makes
154 // for unnecessary nesting. 141 // for unnecessary nesting.
155 bool add_all_to_top_level = import_to_top_level && toolbar_bookmarks.empty(); 142 bool add_all_to_top_level = import_to_top_level && toolbar_bookmarks.empty();
156 143
157 model->BeginExtensiveChanges(); 144 model->BeginExtensiveChanges();
158 145
159 std::set<const BookmarkNode*> folders_added_to; 146 std::set<const BookmarkNode*> folders_added_to;
160 const BookmarkNode* top_level_folder = NULL; 147 const BookmarkNode* top_level_folder = NULL;
161 for (std::vector<BookmarkEntry>::const_iterator bookmark = 148 for (std::vector<ImportedBookmarkEntry>::const_iterator bookmark =
162 reordered_bookmarks.begin(); 149 reordered_bookmarks.begin();
163 bookmark != reordered_bookmarks.end(); ++bookmark) { 150 bookmark != reordered_bookmarks.end(); ++bookmark) {
164 // Disregard any bookmarks with invalid urls. 151 // Disregard any bookmarks with invalid urls.
165 if (!bookmark->is_folder && !bookmark->url.is_valid()) 152 if (!bookmark->is_folder && !bookmark->url.is_valid())
166 continue; 153 continue;
167 154
168 const BookmarkNode* parent = NULL; 155 const BookmarkNode* parent = NULL;
169 if (import_to_top_level && (add_all_to_top_level || bookmark->in_toolbar)) { 156 if (import_to_top_level && (add_all_to_top_level || bookmark->in_toolbar)) {
170 // Add directly to the bookmarks bar. 157 // Add directly to the bookmarks bar.
171 parent = bookmark_bar; 158 parent = bookmark_bar;
172 } else { 159 } else {
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
322 309
323 // Only add valid TemplateURLs to the model. 310 // Only add valid TemplateURLs to the model.
324 if ((*i)->url_ref().IsValid()) { 311 if ((*i)->url_ref().IsValid()) {
325 model->AddAndSetProfile(*i, profile_); // Takes ownership. 312 model->AddAndSetProfile(*i, profile_); // Takes ownership.
326 *i = NULL; // Prevent the vector from deleting *i later. 313 *i = NULL; // Prevent the vector from deleting *i later.
327 } 314 }
328 } 315 }
329 } 316 }
330 317
331 ProfileWriter::~ProfileWriter() {} 318 ProfileWriter::~ProfileWriter() {}
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698