Index: chrome/utility/importer/firefox_importer.cc |
diff --git a/chrome/utility/importer/firefox_importer.cc b/chrome/utility/importer/firefox_importer.cc |
index e487c92e507e10b1c7c63930d045b68a66899d29..ff6ab57fd467b555646e24228ae2d1317603a70c 100644 |
--- a/chrome/utility/importer/firefox_importer.cc |
+++ b/chrome/utility/importer/firefox_importer.cc |
@@ -23,7 +23,7 @@ |
#include "chrome/common/importer/importer_url_row.h" |
#include "chrome/grit/generated_resources.h" |
#include "chrome/utility/importer/bookmark_html_reader.h" |
-#include "chrome/utility/importer/favicon_reencode.h" |
+#include "chrome/utility/importer/firefox_places_factory.h" |
#include "chrome/utility/importer/nss_decryptor.h" |
#include "components/autofill/core/common/password_form.h" |
#include "sql/connection.h" |
@@ -32,15 +32,6 @@ |
namespace { |
-// Original definition is in http://mxr.mozilla.org/firefox/source/toolkit/ |
-// components/places/public/nsINavBookmarksService.idl |
-enum BookmarkItemType { |
- TYPE_BOOKMARK = 1, |
- TYPE_FOLDER = 2, |
- TYPE_SEPARATOR = 3, |
- TYPE_DYNAMIC_CONTAINER = 4 |
-}; |
- |
// Loads the default bookmarks in the Firefox installed at |app_path|, |
// and stores their locations in |urls|. |
void LoadDefaultBookmarks(const base::FilePath& app_path, |
@@ -81,18 +72,6 @@ bool CanImportURL(const GURL& url) { |
} // namespace |
-struct FirefoxImporter::BookmarkItem { |
- int parent; |
- int id; |
- GURL url; |
- base::string16 title; |
- BookmarkItemType type; |
- std::string keyword; |
- base::Time date_added; |
- int64_t favicon; |
- bool empty_folder; |
-}; |
- |
FirefoxImporter::FirefoxImporter() { |
} |
@@ -151,104 +130,52 @@ void FirefoxImporter::StartImport(const importer::SourceProfile& source_profile, |
} |
void FirefoxImporter::ImportHistory() { |
- base::FilePath file = source_path_.AppendASCII("places.sqlite"); |
- if (!base::PathExists(file)) |
- return; |
- |
- sql::Connection db; |
- if (!db.Open(file)) |
+ auto places = FirefoxPlacesFactory::GetForProfile(source_path_); |
+ if (!places) |
return; |
- // |visit_type| represent the transition type of URLs (typed, click, |
- // redirect, bookmark, etc.) We eliminate some URLs like sub-frames and |
- // redirects, since we don't want them to appear in history. |
- // Firefox transition types are defined in: |
- // toolkit/components/places/public/nsINavHistoryService.idl |
- const char query[] = |
- "SELECT h.url, h.title, h.visit_count, " |
- "h.hidden, h.typed, v.visit_date " |
- "FROM moz_places h JOIN moz_historyvisits v " |
- "ON h.id = v.place_id " |
- "WHERE v.visit_type <= 3"; |
- |
- sql::Statement s(db.GetUniqueStatement(query)); |
- |
std::vector<ImporterURLRow> rows; |
- while (s.Step() && !cancelled()) { |
- GURL url(s.ColumnString(0)); |
- |
- // Filter out unwanted URLs. |
- if (!CanImportURL(url)) |
- continue; |
- |
- ImporterURLRow row(url); |
- row.title = s.ColumnString16(1); |
- row.visit_count = s.ColumnInt(2); |
- row.hidden = s.ColumnInt(3) == 1; |
- row.typed_count = s.ColumnInt(4); |
- row.last_visit = base::Time::FromTimeT(s.ColumnInt64(5)/1000000); |
- |
- rows.push_back(row); |
- } |
- |
+ places->LoadHistory(&rows); |
+ rows.erase(begin(rows), |
+ std::remove_if(begin(rows), end(rows), [](const ImporterURLRow& row) { |
+ return CanImportURL(row.url); |
+ })); |
if (!rows.empty() && !cancelled()) |
bridge_->SetHistoryItems(rows, importer::VISIT_SOURCE_FIREFOX_IMPORTED); |
} |
void FirefoxImporter::ImportBookmarks() { |
- base::FilePath file = source_path_.AppendASCII("places.sqlite"); |
- if (!base::PathExists(file)) |
+ auto places = FirefoxPlacesFactory::GetForProfile(source_path_); |
+ if (!places) |
return; |
- |
- sql::Connection db; |
- if (!db.Open(file)) |
+ BookmarkList list; |
+ if (!places->LoadBookmarks(&list)) |
+ return; |
+ // TODO(jcampan): http://b/issue?id=1196285 we do not support POST based |
+ // keywords yet. We won't include them in the list. |
+ PostKeywordIds post_keyword_ids; |
+ if (!places->LoadPostKeywordIds(&post_keyword_ids)) |
return; |
- |
- // Get the bookmark folders that we are interested in. |
- int toolbar_folder_id = -1; |
- int menu_folder_id = -1; |
- int unsorted_folder_id = -1; |
- LoadRootNodeID(&db, &toolbar_folder_id, &menu_folder_id, &unsorted_folder_id); |
// Load livemark IDs. |
- std::set<int> livemark_id; |
- LoadLivemarkIDs(&db, &livemark_id); |
+ LivemarkIds livemark_ids; |
+ if (!places->LoadLivemarkIds(&livemark_ids)) |
+ return; |
// Load the default bookmarks. |
std::set<GURL> default_urls; |
LoadDefaultBookmarks(app_path_, &default_urls); |
- BookmarkList list; |
- GetTopBookmarkFolder(&db, toolbar_folder_id, &list); |
- GetTopBookmarkFolder(&db, menu_folder_id, &list); |
- GetTopBookmarkFolder(&db, unsorted_folder_id, &list); |
- size_t count = list.size(); |
- for (size_t i = 0; i < count; ++i) |
- GetWholeBookmarkFolder(&db, &list, i, NULL); |
+ auto toolbar_folder_id = places->GetFolderId(FirefoxPlaces::FolderType::TOOLBAR); |
+ auto menu_folder_id = places->GetFolderId(FirefoxPlaces::FolderType::MENU); |
+ auto unsorted_folder_id = places->GetFolderId(FirefoxPlaces::FolderType::UNSORTED); |
std::vector<ImportedBookmarkEntry> bookmarks; |
std::vector<importer::SearchEngineInfo> search_engines; |
FaviconMap favicon_map; |
- // TODO(jcampan): http://b/issue?id=1196285 we do not support POST based |
- // keywords yet. We won't include them in the list. |
- std::set<int> post_keyword_ids; |
- const char query[] = |
- "SELECT b.id FROM moz_bookmarks b " |
- "INNER JOIN moz_items_annos ia ON ia.item_id = b.id " |
- "INNER JOIN moz_anno_attributes aa ON ia.anno_attribute_id = aa.id " |
- "WHERE aa.name = 'bookmarkProperties/POSTData'"; |
- sql::Statement s(db.GetUniqueStatement(query)); |
- |
- if (!s.is_valid()) |
- return; |
- |
- while (s.Step() && !cancelled()) |
- post_keyword_ids.insert(s.ColumnInt(0)); |
- |
for (size_t i = 0; i < list.size(); ++i) { |
BookmarkItem* item = list[i]; |
- |
// Folders are added implicitly on adding children, so we only explicitly |
// add empty folders. |
if (item->type != TYPE_BOOKMARK && |
@@ -268,7 +195,7 @@ void FirefoxImporter::ImportBookmarks() { |
bool is_in_toolbar = false; |
while (child->parent >= 0) { |
BookmarkItem* parent = list[child->parent]; |
- if (livemark_id.find(parent->id) != livemark_id.end()) { |
+ if (livemark_ids.find(parent->id) != livemark_ids.end()) { |
// Don't import live bookmarks. |
break; |
} |
@@ -346,8 +273,8 @@ void FirefoxImporter::ImportBookmarks() { |
} |
if (!favicon_map.empty() && !cancelled()) { |
favicon_base::FaviconUsageDataList favicons; |
- LoadFavicons(&db, favicon_map, &favicons); |
- bridge_->SetFavicons(favicons); |
+ if (places->LoadFavicons(favicon_map, &favicons)) |
+ bridge_->SetFavicons(favicons); |
} |
} |
@@ -646,149 +573,3 @@ void FirefoxImporter::GetSearchEnginesXMLDataFromJSON( |
} |
} |
} |
- |
-void FirefoxImporter::LoadRootNodeID(sql::Connection* db, |
- int* toolbar_folder_id, |
- int* menu_folder_id, |
- int* unsorted_folder_id) { |
- static const char kToolbarFolderName[] = "toolbar"; |
- static const char kMenuFolderName[] = "menu"; |
- static const char kUnsortedFolderName[] = "unfiled"; |
- |
- const char query[] = "SELECT root_name, folder_id FROM moz_bookmarks_roots"; |
- sql::Statement s(db->GetUniqueStatement(query)); |
- |
- while (s.Step()) { |
- std::string folder = s.ColumnString(0); |
- int id = s.ColumnInt(1); |
- if (folder == kToolbarFolderName) |
- *toolbar_folder_id = id; |
- else if (folder == kMenuFolderName) |
- *menu_folder_id = id; |
- else if (folder == kUnsortedFolderName) |
- *unsorted_folder_id = id; |
- } |
-} |
- |
-void FirefoxImporter::LoadLivemarkIDs(sql::Connection* db, |
- std::set<int>* livemark) { |
- static const char kFeedAnnotation[] = "livemark/feedURI"; |
- livemark->clear(); |
- |
- const char query[] = |
- "SELECT b.item_id " |
- "FROM moz_anno_attributes a " |
- "JOIN moz_items_annos b ON a.id = b.anno_attribute_id " |
- "WHERE a.name = ? "; |
- sql::Statement s(db->GetUniqueStatement(query)); |
- s.BindString(0, kFeedAnnotation); |
- |
- while (s.Step() && !cancelled()) |
- livemark->insert(s.ColumnInt(0)); |
-} |
- |
-void FirefoxImporter::GetTopBookmarkFolder(sql::Connection* db, |
- int folder_id, |
- BookmarkList* list) { |
- const char query[] = |
- "SELECT b.title " |
- "FROM moz_bookmarks b " |
- "WHERE b.type = 2 AND b.id = ? " |
- "ORDER BY b.position"; |
- sql::Statement s(db->GetUniqueStatement(query)); |
- s.BindInt(0, folder_id); |
- |
- if (s.Step()) { |
- BookmarkItem* item = new BookmarkItem; |
- item->parent = -1; // The top level folder has no parent. |
- item->id = folder_id; |
- item->title = s.ColumnString16(0); |
- item->type = TYPE_FOLDER; |
- item->favicon = 0; |
- item->empty_folder = true; |
- list->push_back(item); |
- } |
-} |
- |
-void FirefoxImporter::GetWholeBookmarkFolder(sql::Connection* db, |
- BookmarkList* list, |
- size_t position, |
- bool* empty_folder) { |
- if (position >= list->size()) { |
- NOTREACHED(); |
- return; |
- } |
- |
- const char query[] = |
- "SELECT b.id, h.url, COALESCE(b.title, h.title), " |
- "b.type, k.keyword, b.dateAdded, h.favicon_id " |
- "FROM moz_bookmarks b " |
- "LEFT JOIN moz_places h ON b.fk = h.id " |
- "LEFT JOIN moz_keywords k ON k.id = b.keyword_id " |
- "WHERE b.type IN (1,2) AND b.parent = ? " |
- "ORDER BY b.position"; |
- sql::Statement s(db->GetUniqueStatement(query)); |
- s.BindInt(0, (*list)[position]->id); |
- |
- BookmarkList temp_list; |
- while (s.Step()) { |
- BookmarkItem* item = new BookmarkItem; |
- item->parent = static_cast<int>(position); |
- item->id = s.ColumnInt(0); |
- item->url = GURL(s.ColumnString(1)); |
- item->title = s.ColumnString16(2); |
- item->type = static_cast<BookmarkItemType>(s.ColumnInt(3)); |
- item->keyword = s.ColumnString(4); |
- item->date_added = base::Time::FromTimeT(s.ColumnInt64(5)/1000000); |
- item->favicon = s.ColumnInt64(6); |
- item->empty_folder = true; |
- |
- temp_list.push_back(item); |
- if (empty_folder != NULL) |
- *empty_folder = false; |
- } |
- |
- // Appends all items to the list. |
- for (BookmarkList::iterator i = temp_list.begin(); |
- i != temp_list.end(); ++i) { |
- list->push_back(*i); |
- // Recursive add bookmarks in sub-folders. |
- if ((*i)->type == TYPE_FOLDER) |
- GetWholeBookmarkFolder(db, list, list->size() - 1, &(*i)->empty_folder); |
- } |
-} |
- |
-void FirefoxImporter::LoadFavicons( |
- sql::Connection* db, |
- const FaviconMap& favicon_map, |
- favicon_base::FaviconUsageDataList* favicons) { |
- const char query[] = "SELECT url, data FROM moz_favicons WHERE id=?"; |
- sql::Statement s(db->GetUniqueStatement(query)); |
- |
- if (!s.is_valid()) |
- return; |
- |
- for (FaviconMap::const_iterator i = favicon_map.begin(); |
- i != favicon_map.end(); ++i) { |
- s.BindInt64(0, i->first); |
- if (s.Step()) { |
- favicon_base::FaviconUsageData usage; |
- |
- usage.favicon_url = GURL(s.ColumnString(0)); |
- if (!usage.favicon_url.is_valid()) |
- continue; // Don't bother importing favicons with invalid URLs. |
- |
- std::vector<unsigned char> data; |
- s.ColumnBlobAsVector(1, &data); |
- if (data.empty()) |
- continue; // Data definitely invalid. |
- |
- if (!importer::ReencodeFavicon(&data[0], data.size(), &usage.png_data)) |
- continue; // Unable to decode. |
- |
- usage.urls = i->second; |
- favicons->push_back(usage); |
- } |
- s.Reset(true); |
- } |
-} |