Skip to content
This repository has been archived by the owner on Jan 4, 2019. It is now read-only.

Commit

Permalink
Add favicon for chrome bookmarks import
Browse files Browse the repository at this point in the history
  • Loading branch information
darkdh committed Oct 19, 2016
1 parent 073f337 commit 5ed1ed2
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
74 changes: 74 additions & 0 deletions atom/utility/importer/chrome_importer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "chrome/common/importer/imported_bookmark_entry.h"
#include "chrome/common/importer/importer_bridge.h"
#include "chrome/common/importer/importer_url_row.h"
#include "chrome/utility/importer/favicon_reencode.h"
#include "sql/connection.h"
#include "sql/statement.h"
#include "url/gurl.h"
Expand Down Expand Up @@ -132,6 +133,79 @@ void ChromeImporter::ImportBookmarks() {
base::UTF8ToUTF16("Imported from Chrome");
bridge_->AddBookmarks(bookmarks, first_folder_name);
}

// Import favicons.
base::FilePath favicons_path =
source_path_.Append(
base::FilePath::StringType(FILE_PATH_LITERAL("Favicons")));
if (!base::PathExists(favicons_path))
return;

sql::Connection db;
if (!db.Open(favicons_path))
return;

FaviconMap favicon_map;
ImportFaviconURLs(&db, &favicon_map);
// Write favicons into profile.
if (!favicon_map.empty() && !cancelled()) {
favicon_base::FaviconUsageDataList favicons;
LoadFaviconData(&db, favicon_map, &favicons);
bridge_->SetFavicons(favicons);
}
}

void ChromeImporter::ImportFaviconURLs(
sql::Connection* db,
FaviconMap* favicon_map) {
const char query[] = "SELECT icon_id, page_url FROM icon_mapping;";
sql::Statement s(db->GetUniqueStatement(query));

while (s.Step() && !cancelled()) {
int64_t icon_id = s.ColumnInt64(0);
GURL url = GURL(s.ColumnString(1));
(*favicon_map)[icon_id].insert(url);
}
}

void ChromeImporter::LoadFaviconData(
sql::Connection* db,
const FaviconMap& favicon_map,
favicon_base::FaviconUsageDataList* favicons) {
const char query[] = "SELECT url "
"FROM favicons "
"WHERE id = ?;";
sql::Statement s(db->GetUniqueStatement(query));

for (FaviconMap::const_iterator i = favicon_map.begin();
i != favicon_map.end(); ++i) {
s.Reset(true);
s.BindInt64(0, i->first);
if (s.Step()) {
favicon_base::FaviconUsageData usage;

GURL url = GURL(s.ColumnString(0));
if (url.is_valid()) {
if (url.SchemeIs(url::kDataScheme)) {
std::vector<unsigned char> data;
s.ColumnBlobAsVector(0, &data);
if (data.empty()) {
continue; // Data definitely invalid.
}
if (!importer::ReencodeFavicon(&data[0], data.size(),
&usage.png_data))
continue; // Unable to decode.
} else {
usage.favicon_url = url;
}
} else {
continue; // Don't bother importing favicons with invalid URLs.
}

usage.urls = i->second;
favicons->push_back(usage);
}
}
}

void ChromeImporter::ImportCookies() {
Expand Down
22 changes: 22 additions & 0 deletions atom/utility/importer/chrome_importer.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,27 @@

#include <stdint.h>

#include <map>
#include <set>
#include <vector>

#include "base/compiler_specific.h"
#include "base/files/file_path.h"
#include "base/macros.h"
#include "build/build_config.h"
#include "chrome/utility/importer/importer.h"
#include "components/favicon_base/favicon_usage_data.h"

struct ImportedBookmarkEntry;

namespace base {
class DictionaryValue;
}

namespace sql {
class Connection;
}

class ChromeImporter : public Importer {
public:
ChromeImporter();
Expand All @@ -37,6 +44,21 @@ class ChromeImporter : public Importer {
void ImportHistory();
void ImportCookies();

// Multiple URLs can share the same favicon; this is a map
// of URLs -> IconIDs that we load as a temporary step before
// actually loading the icons.
typedef std::map<int64_t, std::set<GURL>> FaviconMap;

// Loads the urls associated with the favicons into favicon_map;
void ImportFaviconURLs(
sql::Connection* db,
FaviconMap* favicon_map);

// Loads and reencodes the individual favicons.
void LoadFaviconData(sql::Connection* db,
const FaviconMap& favicon_map,
favicon_base::FaviconUsageDataList* favicons);

void RecursiveReadBookmarksFolder(
const base::DictionaryValue* folder,
const std::vector<base::string16>& parent_path,
Expand Down

0 comments on commit 5ed1ed2

Please sign in to comment.