Skip to content

Commit

Permalink
Subclass ChromeImporter to reuse code where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Garrett Robinson committed Jul 5, 2018
1 parent 994da20 commit 6cd1669
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 88 deletions.
67 changes: 0 additions & 67 deletions utility/importer/brave_importer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@
#include "chrome/browser/password_manager/native_backend_kwallet_x.h"
#include "chrome/browser/password_manager/password_store_x.h"
#include "components/os_crypt/key_storage_util_linux.h"

base::nix::DesktopEnvironment BraveImporter::GetDesktopEnvironment() {
std::unique_ptr<base::Environment> env(base::Environment::Create());
return base::nix::GetDesktopEnvironment(env.get());
}
#endif

using base::Time;
Expand Down Expand Up @@ -287,10 +282,6 @@ void BraveImporter::ImportBookmarks() {
}
}

double BraveImporter::chromeTimeToDouble(int64_t time) {
return ((time * 10 - 0x19DB1DED53E8000) / 10000) / 1000;
}

void BraveImporter::ImportPasswords() {
#if !defined(USE_X11)
base::FilePath passwords_path =
Expand Down Expand Up @@ -380,61 +371,3 @@ void BraveImporter::ImportPasswords() {
}
#endif
}

void BraveImporter::ImportCookies() {
base::FilePath cookies_path =
source_path_.Append(
base::FilePath::StringType(FILE_PATH_LITERAL("Cookies")));
if (!base::PathExists(cookies_path))
return;

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

const char query[] =
"SELECT creation_utc, host_key, name, value, encrypted_value, path, "
"expires_utc, is_secure, is_httponly, firstpartyonly, last_access_utc, "
"has_expires, is_persistent, priority FROM cookies";

sql::Statement s(db.GetUniqueStatement(query));

net::CookieCryptoDelegate* delegate =
cookie_config::GetCookieCryptoDelegate();
#if defined(OS_LINUX)
OSCrypt::SetConfig(std::make_unique<os_crypt::Config>());
#endif

std::vector<net::CanonicalCookie> cookies;
while (s.Step() && !cancelled()) {
std::string encrypted_value = s.ColumnString(4);
std::string value;
if (!encrypted_value.empty() && delegate) {
if (!delegate->DecryptString(encrypted_value, &value)) {
continue;
}
} else {
value = s.ColumnString(3);
}

auto cookie = net::CanonicalCookie(
s.ColumnString(2), // name
value, // value
s.ColumnString(1), // domain
s.ColumnString(5), // path
Time::FromInternalValue(s.ColumnInt64(0)), // creation_utc
Time::FromInternalValue(s.ColumnInt64(6)), // expires_utc
Time::FromInternalValue(s.ColumnInt64(10)), // last_access_utc
s.ColumnBool(7), // secure
s.ColumnBool(8), // http_only
static_cast<net::CookieSameSite>(s.ColumnInt(9)), // samesite
static_cast<net::CookiePriority>(s.ColumnInt(13))); // priority
if (cookie.IsCanonical()) {
cookies.push_back(cookie);
}
}

if (!cookies.empty() && !cancelled()) {
bridge_->SetCookies(cookies);
}
}
17 changes: 5 additions & 12 deletions utility/importer/brave_importer.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
#include "base/macros.h"
#include "base/nix/xdg_util.h"
#include "base/values.h"
#include "brave/utility/importer/chrome_importer.h"
#include "build/build_config.h"
#include "chrome/common/importer/imported_bookmark_entry.h"
#include "chrome/utility/importer/importer.h"

class BraveImporter : public Importer {
class BraveImporter : public ChromeImporter {
public:
BraveImporter();

Expand All @@ -30,12 +30,9 @@ class BraveImporter : public Importer {
private:
~BraveImporter() override;

static base::nix::DesktopEnvironment GetDesktopEnvironment();

void ImportHistory();
void ImportBookmarks();
void ImportPasswords();
void ImportCookies();
void ImportBookmarks() override;
void ImportHistory() override;
void ImportPasswords() override;

void ParseBookmarks(std::vector<ImportedBookmarkEntry>* bookmarks);
void RecursiveReadBookmarksFolder(
Expand All @@ -48,10 +45,6 @@ class BraveImporter : public Importer {
base::Value* bookmark_order_dict,
std::vector<ImportedBookmarkEntry>* bookmarks);

double chromeTimeToDouble(int64_t time);

base::FilePath source_path_;

DISALLOW_COPY_AND_ASSIGN(BraveImporter);
};

Expand Down
19 changes: 10 additions & 9 deletions utility/importer/chrome_importer.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,21 @@ class ChromeImporter : public Importer {
uint16_t items,
ImporterBridge* bridge) override;

private:
protected:
~ChromeImporter() override;

static base::nix::DesktopEnvironment GetDesktopEnvironment();

void ImportBookmarks();
void ImportHistory();
void ImportPasswords();
void ImportCookies();
virtual void ImportBookmarks();
virtual void ImportHistory();
virtual void ImportPasswords();
virtual void ImportCookies();

double chromeTimeToDouble(int64_t time);

base::FilePath source_path_;

private:
// 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.
Expand All @@ -69,10 +74,6 @@ class ChromeImporter : public Importer {
bool is_in_toolbar,
std::vector<ImportedBookmarkEntry>* bookmarks);

double chromeTimeToDouble(int64_t time);

base::FilePath source_path_;

DISALLOW_COPY_AND_ASSIGN(ChromeImporter);
};

Expand Down

0 comments on commit 6cd1669

Please sign in to comment.