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

Side by Side Diff: chrome/utility/importer/firefox_importer.cc

Issue 616763002: Importing certain bookmarks from firefox and HTML file as search engines. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed dependency issue. Created 5 years, 11 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
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/utility/importer/firefox_importer.h" 5 #include "chrome/utility/importer/firefox_importer.h"
6 6
7 #include <set> 7 #include <set>
8 8
9 #include "base/files/file_enumerator.h" 9 #include "base/files/file_enumerator.h"
10 #include "base/files/file_util.h" 10 #include "base/files/file_util.h"
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
43 // Loads the default bookmarks in the Firefox installed at |app_path|, 43 // Loads the default bookmarks in the Firefox installed at |app_path|,
44 // and stores their locations in |urls|. 44 // and stores their locations in |urls|.
45 void LoadDefaultBookmarks(const base::FilePath& app_path, 45 void LoadDefaultBookmarks(const base::FilePath& app_path,
46 std::set<GURL>* urls) { 46 std::set<GURL>* urls) {
47 base::FilePath file = app_path.AppendASCII("defaults") 47 base::FilePath file = app_path.AppendASCII("defaults")
48 .AppendASCII("profile") 48 .AppendASCII("profile")
49 .AppendASCII("bookmarks.html"); 49 .AppendASCII("bookmarks.html");
50 urls->clear(); 50 urls->clear();
51 51
52 std::vector<ImportedBookmarkEntry> bookmarks; 52 std::vector<ImportedBookmarkEntry> bookmarks;
53 std::vector<importer::SearchEngineInfo> search_engines;
53 bookmark_html_reader::ImportBookmarksFile(base::Callback<bool(void)>(), 54 bookmark_html_reader::ImportBookmarksFile(base::Callback<bool(void)>(),
54 base::Callback<bool(const GURL&)>(), 55 base::Callback<bool(const GURL&)>(),
55 file, 56 file,
56 &bookmarks, 57 &bookmarks,
58 &search_engines,
57 NULL); 59 NULL);
58 for (size_t i = 0; i < bookmarks.size(); ++i) 60 for (size_t i = 0; i < bookmarks.size(); ++i)
59 urls->insert(bookmarks[i].url); 61 urls->insert(bookmarks[i].url);
60 } 62 }
61 63
62 // Returns true if |url| has a valid scheme that we allow to import. We 64 // Returns true if |url| has a valid scheme that we allow to import. We
63 // filter out the URL with a unsupported scheme. 65 // filter out the URL with a unsupported scheme.
64 bool CanImportURL(const GURL& url) { 66 bool CanImportURL(const GURL& url) {
65 // The URL is not valid. 67 // The URL is not valid.
66 if (!url.is_valid()) 68 if (!url.is_valid())
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 220
219 BookmarkList list; 221 BookmarkList list;
220 GetTopBookmarkFolder(&db, toolbar_folder_id, &list); 222 GetTopBookmarkFolder(&db, toolbar_folder_id, &list);
221 GetTopBookmarkFolder(&db, menu_folder_id, &list); 223 GetTopBookmarkFolder(&db, menu_folder_id, &list);
222 GetTopBookmarkFolder(&db, unsorted_folder_id, &list); 224 GetTopBookmarkFolder(&db, unsorted_folder_id, &list);
223 size_t count = list.size(); 225 size_t count = list.size();
224 for (size_t i = 0; i < count; ++i) 226 for (size_t i = 0; i < count; ++i)
225 GetWholeBookmarkFolder(&db, &list, i, NULL); 227 GetWholeBookmarkFolder(&db, &list, i, NULL);
226 228
227 std::vector<ImportedBookmarkEntry> bookmarks; 229 std::vector<ImportedBookmarkEntry> bookmarks;
228 std::vector<importer::URLKeywordInfo> url_keywords; 230 std::vector<importer::SearchEngineInfo> search_engines;
229 FaviconMap favicon_map; 231 FaviconMap favicon_map;
230 232
231 // TODO(jcampan): http://b/issue?id=1196285 we do not support POST based 233 // TODO(jcampan): http://b/issue?id=1196285 we do not support POST based
232 // keywords yet. We won't include them in the list. 234 // keywords yet. We won't include them in the list.
233 std::set<int> post_keyword_ids; 235 std::set<int> post_keyword_ids;
234 const char query[] = 236 const char query[] =
235 "SELECT b.id FROM moz_bookmarks b " 237 "SELECT b.id FROM moz_bookmarks b "
236 "INNER JOIN moz_items_annos ia ON ia.item_id = b.id " 238 "INNER JOIN moz_items_annos ia ON ia.item_id = b.id "
237 "INNER JOIN moz_anno_attributes aa ON ia.anno_attribute_id = aa.id " 239 "INNER JOIN moz_anno_attributes aa ON ia.anno_attribute_id = aa.id "
238 "WHERE aa.name = 'bookmarkProperties/POSTData'"; 240 "WHERE aa.name = 'bookmarkProperties/POSTData'";
239 sql::Statement s(db.GetUniqueStatement(query)); 241 sql::Statement s(db.GetUniqueStatement(query));
240 242
241 if (!s.is_valid()) 243 if (!s.is_valid())
242 return; 244 return;
243 245
244 while (s.Step() && !cancelled()) 246 while (s.Step() && !cancelled())
245 post_keyword_ids.insert(s.ColumnInt(0)); 247 post_keyword_ids.insert(s.ColumnInt(0));
246 248
247 for (size_t i = 0; i < list.size(); ++i) { 249 for (size_t i = 0; i < list.size(); ++i) {
248 BookmarkItem* item = list[i]; 250 BookmarkItem* item = list[i];
249 251
250 if (item->type == TYPE_FOLDER) { 252 // Folders are added implicitly on adding children, so we only explicitly
251 // Folders are added implicitly on adding children, so we only explicitly 253 // add empty folders.
252 // add empty folders. 254 if (item->type != TYPE_BOOKMARK &&
253 if (!item->empty_folder) 255 ((item->type != TYPE_FOLDER) || !item->empty_folder))
254 continue;
255 } else if (item->type == TYPE_BOOKMARK) {
256 // Import only valid bookmarks
257 if (!CanImportURL(item->url))
258 continue;
259 } else {
260 continue;
261 }
262
263 // Skip the default bookmarks and unwanted URLs.
264 if (default_urls.find(item->url) != default_urls.end() ||
265 post_keyword_ids.find(item->id) != post_keyword_ids.end())
266 continue; 256 continue;
267 257
268 // Find the bookmark path by tracing their links to parent folders. 258 if (CanImportURL(item->url)) {
269 std::vector<base::string16> path; 259 // Skip the default bookmarks and unwanted URLs.
270 BookmarkItem* child = item; 260 if (default_urls.find(item->url) != default_urls.end() ||
271 bool found_path = false; 261 post_keyword_ids.find(item->id) != post_keyword_ids.end())
272 bool is_in_toolbar = false; 262 continue;
273 while (child->parent >= 0) { 263
274 BookmarkItem* parent = list[child->parent]; 264 // Find the bookmark path by tracing their links to parent folders.
275 if (livemark_id.find(parent->id) != livemark_id.end()) { 265 std::vector<base::string16> path;
276 // Don't import live bookmarks. 266 BookmarkItem* child = item;
277 break; 267 bool found_path = false;
268 bool is_in_toolbar = false;
269 while (child->parent >= 0) {
270 BookmarkItem* parent = list[child->parent];
271 if (livemark_id.find(parent->id) != livemark_id.end()) {
272 // Don't import live bookmarks.
273 break;
274 }
275
276 if (parent->id != menu_folder_id) {
277 // To avoid excessive nesting, omit the name for the bookmarks menu
278 // folder.
279 path.insert(path.begin(), parent->title);
280 }
281
282 if (parent->id == toolbar_folder_id)
283 is_in_toolbar = true;
284
285 if (parent->id == toolbar_folder_id ||
286 parent->id == menu_folder_id ||
287 parent->id == unsorted_folder_id) {
288 // We've reached a root node, hooray!
289 found_path = true;
290 break;
291 }
292
293 child = parent;
278 } 294 }
279 295
280 if (parent->id != menu_folder_id) { 296 if (!found_path)
281 // To avoid excessive nesting, omit the name for the bookmarks menu 297 continue;
282 // folder.
283 path.insert(path.begin(), parent->title);
284 }
285 298
286 if (parent->id == toolbar_folder_id) 299 ImportedBookmarkEntry entry;
287 is_in_toolbar = true; 300 entry.creation_time = item->date_added;
301 entry.title = item->title;
302 entry.url = item->url;
303 entry.path = path;
304 entry.in_toolbar = is_in_toolbar;
305 entry.is_folder = item->type == TYPE_FOLDER;
288 306
289 if (parent->id == toolbar_folder_id || 307 bookmarks.push_back(entry);
290 parent->id == menu_folder_id ||
291 parent->id == unsorted_folder_id) {
292 // We've reached a root node, hooray!
293 found_path = true;
294 break;
295 }
296
297 child = parent;
298 } 308 }
299 309
300 if (!found_path)
301 continue;
302
303 ImportedBookmarkEntry entry;
304 entry.creation_time = item->date_added;
305 entry.title = item->title;
306 entry.url = item->url;
307 entry.path = path;
308 entry.in_toolbar = is_in_toolbar;
309 entry.is_folder = item->type == TYPE_FOLDER;
310
311 bookmarks.push_back(entry);
312
313 if (item->type == TYPE_BOOKMARK) { 310 if (item->type == TYPE_BOOKMARK) {
314 if (item->favicon) 311 if (item->favicon)
315 favicon_map[item->favicon].insert(item->url); 312 favicon_map[item->favicon].insert(item->url);
316 313
317 // This bookmark has a keyword, we should import it. 314 // Import this bookmark as a search engine if it has a keyword and its URL
318 if (!item->keyword.empty() && item->url.is_valid()) { 315 // is usable as a search engine URL. (Even if the URL doesn't allow
319 importer::URLKeywordInfo url_keyword_info; 316 // substitution, importing as a "search engine" allows users to trigger
320 url_keyword_info.url = item->url; 317 // the bookmark by entering its keyword in the omnibox.)
321 url_keyword_info.keyword.assign(base::UTF8ToUTF16(item->keyword)); 318 if (item->keyword.empty())
322 url_keyword_info.display_name = item->title; 319 continue;
323 url_keywords.push_back(url_keyword_info); 320 importer::SearchEngineInfo search_engine_info;
324 } 321 std::string search_engine_url;
322 if (item->url.is_valid())
323 search_engine_info.url = base::UTF8ToUTF16(item->url.spec());
324 else if (bookmark_html_reader::CanImportURLAsSearchEngine(
325 item->url,
326 &search_engine_url))
327 search_engine_info.url = base::UTF8ToUTF16(search_engine_url);
328 else
329 continue;
330 search_engine_info.keyword = base::UTF8ToUTF16(item->keyword);
331 search_engine_info.display_name = item->title;
332 search_engines.push_back(search_engine_info);
325 } 333 }
326 } 334 }
327 335
328 STLDeleteElements(&list); 336 STLDeleteElements(&list);
329 337
330 // Write into profile. 338 // Write into profile.
331 if (!bookmarks.empty() && !cancelled()) { 339 if (!bookmarks.empty() && !cancelled()) {
332 const base::string16& first_folder_name = 340 const base::string16& first_folder_name =
333 bridge_->GetLocalizedString(IDS_BOOKMARK_GROUP_FROM_FIREFOX); 341 bridge_->GetLocalizedString(IDS_BOOKMARK_GROUP_FROM_FIREFOX);
334 bridge_->AddBookmarks(bookmarks, first_folder_name); 342 bridge_->AddBookmarks(bookmarks, first_folder_name);
335 } 343 }
336 if (!url_keywords.empty() && !cancelled()) { 344 if (!search_engines.empty() && !cancelled()) {
337 bridge_->SetKeywords(url_keywords, false); 345 bridge_->SetKeywords(search_engines, false);
338 } 346 }
339 if (!favicon_map.empty() && !cancelled()) { 347 if (!favicon_map.empty() && !cancelled()) {
340 std::vector<ImportedFaviconUsage> favicons; 348 std::vector<ImportedFaviconUsage> favicons;
341 LoadFavicons(&db, favicon_map, &favicons); 349 LoadFavicons(&db, favicon_map, &favicons);
342 bridge_->SetFavicons(favicons); 350 bridge_->SetFavicons(favicons);
343 } 351 }
344 } 352 }
345 353
346 void FirefoxImporter::ImportPasswords() { 354 void FirefoxImporter::ImportPasswords() {
347 // Initializes NSS3. 355 // Initializes NSS3.
(...skipping 427 matching lines...) Expand 10 before | Expand all | Expand 10 after
775 783
776 if (!importer::ReencodeFavicon(&data[0], data.size(), &usage.png_data)) 784 if (!importer::ReencodeFavicon(&data[0], data.size(), &usage.png_data))
777 continue; // Unable to decode. 785 continue; // Unable to decode.
778 786
779 usage.urls = i->second; 787 usage.urls = i->second;
780 favicons->push_back(usage); 788 favicons->push_back(usage);
781 } 789 }
782 s.Reset(true); 790 s.Reset(true);
783 } 791 }
784 } 792 }
OLDNEW
« no previous file with comments | « chrome/utility/importer/external_process_importer_bridge.cc ('k') | chrome/utility/importer/ie_importer_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698