Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import cookies from Chrome #93

Merged
merged 2 commits into from
Apr 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ source_set("browser_process") {
"brave_tab_helpers.cc",
"brave_tab_helpers.h",
"brave_profile_prefs.cc",
"brave_profile_prefs.h"
"brave_profile_prefs.h",
"importer/brave_external_process_importer_client.cc",
"importer/brave_external_process_importer_client.h",
"importer/brave_external_process_importer_host.cc",
"importer/brave_external_process_importer_host.h",
"importer/brave_in_process_importer_bridge.cc",
"importer/brave_in_process_importer_bridge.h",
"importer/brave_profile_writer.cc",
"importer/brave_profile_writer.h",
]

deps = [
Expand Down
48 changes: 48 additions & 0 deletions browser/importer/brave_external_process_importer_client.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <vector>

#include "brave/browser/importer/brave_external_process_importer_client.h"

BraveExternalProcessImporterClient::BraveExternalProcessImporterClient(
base::WeakPtr<ExternalProcessImporterHost> importer_host,
const importer::SourceProfile& source_profile,
uint16_t items,
BraveInProcessImporterBridge* bridge)
: ExternalProcessImporterClient(
importer_host, source_profile, items, bridge),
total_cookies_count_(0),
bridge_(bridge),
cancelled_(false) {}

void BraveExternalProcessImporterClient::Cancel() {
if (cancelled_)
return;

cancelled_ = true;
ExternalProcessImporterClient::Cancel();
}

void BraveExternalProcessImporterClient::OnCookiesImportStart(
uint32_t total_cookies_count) {
if (cancelled_)
return;

total_cookies_count_ = total_cookies_count;
cookies_.reserve(total_cookies_count);
}

void BraveExternalProcessImporterClient::OnCookiesImportGroup(
const std::vector<net::CanonicalCookie>& cookies_group) {
if (cancelled_)
return;

cookies_.insert(cookies_.end(), cookies_group.begin(),
cookies_group.end());
if (cookies_.size() >= total_cookies_count_)
bridge_->SetCookies(cookies_);
}

BraveExternalProcessImporterClient::~BraveExternalProcessImporterClient() {}
46 changes: 46 additions & 0 deletions browser/importer/brave_external_process_importer_client.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_BROWSER_IMPORTER_BRAVE_EXTERNAL_PROCESS_IMPORTER_CLIENT_H_
#define BRAVE_BROWSER_IMPORTER_BRAVE_EXTERNAL_PROCESS_IMPORTER_CLIENT_H_

#include <vector>

#include "brave/browser/importer/brave_in_process_importer_bridge.h"
#include "chrome/browser/importer/external_process_importer_client.h"
#include "net/cookies/canonical_cookie.h"

class BraveExternalProcessImporterClient : public ExternalProcessImporterClient {
public:
BraveExternalProcessImporterClient(
base::WeakPtr<ExternalProcessImporterHost> importer_host,
const importer::SourceProfile& source_profile,
uint16_t items,
BraveInProcessImporterBridge* bridge);

// Called by the ExternalProcessImporterHost on import cancel.
void Cancel();

void OnCookiesImportStart(
uint32_t total_cookies_count) override;
void OnCookiesImportGroup(
const std::vector<net::CanonicalCookie>& cookies_group) override;

private:
~BraveExternalProcessImporterClient() override;

// Total number of cookies to import.
size_t total_cookies_count_;

scoped_refptr<BraveInProcessImporterBridge> bridge_;

std::vector<net::CanonicalCookie> cookies_;

// True if import process has been cancelled.
bool cancelled_;

DISALLOW_COPY_AND_ASSIGN(BraveExternalProcessImporterClient);
};

#endif // BRAVE_BROWSER_IMPORTER_BRAVE_EXTERNAL_PROCESS_IMPORTER_CLIENT_H_
30 changes: 30 additions & 0 deletions browser/importer/brave_external_process_importer_host.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "brave/browser/importer/brave_external_process_importer_client.h"
#include "brave/browser/importer/brave_external_process_importer_host.h"
#include "brave/browser/importer/brave_in_process_importer_bridge.h"

BraveExternalProcessImporterHost::BraveExternalProcessImporterHost()
: ExternalProcessImporterHost() {}

void BraveExternalProcessImporterHost::LaunchImportIfReady() {
if (waiting_for_bookmarkbar_model_ || template_service_subscription_.get() ||
!is_source_readable_ || cancelled_)
return;

// This is the in-process half of the bridge, which catches data from the IPC
// pipe and feeds it to the ProfileWriter. The external process half of the
// bridge lives in the external process (see ProfileImportThread).
// The ExternalProcessImporterClient created in the next line owns the bridge,
// and will delete it.
BraveInProcessImporterBridge* bridge =
new BraveInProcessImporterBridge(writer_.get(),
weak_ptr_factory_.GetWeakPtr());
client_ = new BraveExternalProcessImporterClient(
weak_ptr_factory_.GetWeakPtr(), source_profile_, items_, bridge);
client_->Start();
}

BraveExternalProcessImporterHost::~BraveExternalProcessImporterHost() {}
26 changes: 26 additions & 0 deletions browser/importer/brave_external_process_importer_host.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_BROWSER_IMPORTER_BRAVE_EXTERNAL_PROCESS_IMPORTER_HOST_H_
#define BRAVE_BROWSER_IMPORTER_BRAVE_EXTERNAL_PROCESS_IMPORTER_HOST_H_

#include "chrome/browser/importer/external_process_importer_host.h"

class BraveExternalProcessImporterHost : public ExternalProcessImporterHost {
public:
BraveExternalProcessImporterHost();

private:
~BraveExternalProcessImporterHost() override;

// Launches the utility process that starts the import task, unless bookmark
// or template model are not yet loaded. If load is not detected, this method
// will be called when the loading observer sees that model loading is
// complete.
void LaunchImportIfReady() override;

DISALLOW_COPY_AND_ASSIGN(BraveExternalProcessImporterHost);
};

#endif // BRAVE_BROWSER_IMPORTER_BRAVE_EXTERNAL_PROCESS_IMPORTER_HOST_H_
19 changes: 19 additions & 0 deletions browser/importer/brave_in_process_importer_bridge.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "brave/browser/importer/brave_in_process_importer_bridge.h"

BraveInProcessImporterBridge::BraveInProcessImporterBridge(
ProfileWriter* writer,
base::WeakPtr<ExternalProcessImporterHost> host) :
InProcessImporterBridge(writer, host),
writer_(static_cast<BraveProfileWriter*>(writer)) {
}

void BraveInProcessImporterBridge::SetCookies(
const std::vector<net::CanonicalCookie>& cookies) {
writer_->AddCookies(cookies);
}

BraveInProcessImporterBridge::~BraveInProcessImporterBridge() {}
36 changes: 36 additions & 0 deletions browser/importer/brave_in_process_importer_bridge.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_BROWSER_IMPORTER_BRAVE_IN_PROCESS_IMPORTER_BRIDGE_H_
#define BRAVE_BROWSER_IMPORTER_BRAVE_IN_PROCESS_IMPORTER_BRIDGE_H_

#include <string>
#include <vector>

#include "brave/browser/importer/brave_profile_writer.h"
#include "chrome/browser/importer/in_process_importer_bridge.h"
#include "net/cookies/canonical_cookie.h"

#include "base/compiler_specific.h"
#include "base/macros.h"
#include "build/build_config.h"

class BraveInProcessImporterBridge : public InProcessImporterBridge {
public:
BraveInProcessImporterBridge(
ProfileWriter* writer,
base::WeakPtr<ExternalProcessImporterHost> host);

void SetCookies(
const std::vector<net::CanonicalCookie>& cookies) override;

private:
~BraveInProcessImporterBridge() override;

BraveProfileWriter* const writer_; // weak

DISALLOW_COPY_AND_ASSIGN(BraveInProcessImporterBridge);
};

#endif // BRAVE_BROWSER_IMPORTER_BRAVE_IN_PROCESS_IMPORTER_BRIDGE_H_
38 changes: 38 additions & 0 deletions browser/importer/brave_profile_writer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "brave/browser/importer/brave_profile_writer.h"

#include "base/time/time.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/storage_partition.h"
#include "net/cookies/canonical_cookie.h"
#include "net/cookies/cookie_constants.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_getter.h"
#include "services/network/public/interfaces/cookie_manager.mojom.h"

BraveProfileWriter::BraveProfileWriter(Profile* profile)
: ProfileWriter(profile) {}

BraveProfileWriter::~BraveProfileWriter() {}

void BraveProfileWriter::AddCookies(
const std::vector<net::CanonicalCookie>& cookies) {
network::mojom::CookieManagerPtr cookie_manager;
content::BrowserContext::GetDefaultStoragePartition(profile_)
->GetNetworkContext()
->GetCookieManager(mojo::MakeRequest(&cookie_manager));

for (auto& cookie : cookies) {
cookie_manager->SetCanonicalCookie(
cookie,
true, // secure_source
true, // modify_http_only
// Fire and forget
network::mojom::CookieManager::SetCanonicalCookieCallback());
}
}
25 changes: 25 additions & 0 deletions browser/importer/brave_profile_writer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_BROWSER_IMPORTER_BRAVE_PROFILE_WRITER_H_
#define BRAVE_BROWSER_IMPORTER_BRAVE_PROFILE_WRITER_H_

#include <vector>

#include "base/macros.h"
#include "chrome/browser/importer/profile_writer.h"
#include "net/cookies/canonical_cookie.h"

class BraveProfileWriter : public ProfileWriter {
public:
explicit BraveProfileWriter(Profile* profile);

virtual void AddCookies(const std::vector<net::CanonicalCookie>& cookies);

protected:
friend class base::RefCountedThreadSafe<BraveProfileWriter>;
~BraveProfileWriter() override;
};

#endif // BRAVE_BROWSER_IMPORTER_BRAVE_PROFILE_WRITER_H_
9 changes: 9 additions & 0 deletions common/importer/brave_mock_importer_bridge.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "brave/common/importer/brave_mock_importer_bridge.h"

BraveMockImporterBridge::BraveMockImporterBridge() {};

BraveMockImporterBridge::~BraveMockImporterBridge() {};
25 changes: 25 additions & 0 deletions common/importer/brave_mock_importer_bridge.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_COMMON_IMPORTER_BRAVE_MOCK_IMPORTER_BRIDGE_H_
#define BRAVE_COMMON_IMPORTER_BRAVE_MOCK_IMPORTER_BRIDGE_H_

#include <vector>

#include "chrome/common/importer/mock_importer_bridge.h"
#include "net/cookies/canonical_cookie.h"
#include "testing/gmock/include/gmock/gmock.h"

class BraveMockImporterBridge : public MockImporterBridge {
public:
BraveMockImporterBridge();

MOCK_METHOD1(SetCookies,
void(const std::vector<net::CanonicalCookie>&));

private:
~BraveMockImporterBridge() override;
};

#endif // BRAVE_COMMON_IMPORTER_BRAVE_MOCK_IMPORTER_BRIDGE_H_
4 changes: 4 additions & 0 deletions common/importer/chrome_importer_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,17 @@ bool ChromeImporterCanImport(const base::FilePath& profile,
profile.Append(base::FilePath::StringType(FILE_PATH_LITERAL("History")));
base::FilePath passwords =
profile.Append(base::FilePath::StringType(FILE_PATH_LITERAL("Login Data")));
base::FilePath cookies =
profile.Append(base::FilePath::StringType(FILE_PATH_LITERAL("Cookies")));

if (base::PathExists(bookmarks))
*services_supported |= importer::FAVORITES;
if (base::PathExists(history))
*services_supported |= importer::HISTORY;
if (base::PathExists(passwords))
*services_supported |= importer::PASSWORDS;
if (base::PathExists(cookies))
*services_supported |= importer::COOKIES;

return *services_supported != importer::NONE;
}
14 changes: 14 additions & 0 deletions patches/chrome-app-settings_strings.grdp.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
diff --git a/chrome/app/settings_strings.grdp b/chrome/app/settings_strings.grdp
index 92909dc5fe5865e1a2bc278e1c7de3bfebdec144..89e30be862426b3e28e51737f25a6c6198099349 100644
--- a/chrome/app/settings_strings.grdp
+++ b/chrome/app/settings_strings.grdp
@@ -3207,6 +3207,9 @@
<message name="IDS_SETTINGS_IMPORT_AUTOFILL_FORM_DATA_CHECKBOX" desc="Checkbox for importing form data for autofill">
Autofill form data
</message>
+ <message name="IDS_SETTINGS_IMPORT_COOKIES_CHECKBOX" desc="Checkbox for importing cookies">
+ Cookies
+ </message>

<message name="IDS_SETTINGS_IMPORT_CHOOSE_FILE" desc="Text for the Choose File on dialog">
Choose File
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
diff --git a/chrome/browser/extensions/api/settings_private/prefs_util.cc b/chrome/browser/extensions/api/settings_private/prefs_util.cc
index 121888328376c5fed7056c14400a13d0d2bc28a2..3ea4ce37db03071906b106959845e6e99c0f2432 100644
--- a/chrome/browser/extensions/api/settings_private/prefs_util.cc
+++ b/chrome/browser/extensions/api/settings_private/prefs_util.cc
@@ -432,6 +432,8 @@ const PrefsUtil::TypedPrefMap& PrefsUtil::GetWhitelistedKeys() {
settings_api::PrefType::PREF_TYPE_BOOLEAN;
(*s_whitelist)[::prefs::kImportDialogSearchEngine] =
settings_api::PrefType::PREF_TYPE_BOOLEAN;
+ (*s_whitelist)[::prefs::kImportDialogCookies] =
+ settings_api::PrefType::PREF_TYPE_BOOLEAN;
#endif

// Proxy settings.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
diff --git a/chrome/browser/importer/external_process_importer_client.h b/chrome/browser/importer/external_process_importer_client.h
index 864a6951115dda5ed74963f18b35692960397d50..477c51b320a3f41b75ab36bf7e1780d1aae6e701 100644
--- a/chrome/browser/importer/external_process_importer_client.h
+++ b/chrome/browser/importer/external_process_importer_client.h
@@ -24,6 +24,7 @@
#include "components/favicon_base/favicon_usage_data.h"
#include "components/history/core/browser/history_types.h"
#include "mojo/public/cpp/bindings/binding.h"
+#include "net/cookies/canonical_cookie.h"

class ExternalProcessImporterHost;
struct ImportedBookmarkEntry;
@@ -88,6 +89,8 @@ class ExternalProcessImporterClient
void OnAutofillFormDataImportGroup(
const std::vector<ImporterAutofillFormDataEntry>&
autofill_form_data_entry_group) override;
+ void OnCookiesImportStart(uint32_t total_cookies_count) override {};
+ void OnCookiesImportGroup(const std::vector<net::CanonicalCookie>& cookies_group) override {};
void OnIE7PasswordReceived(
const importer::ImporterIE7PasswordInfo& importer_password_info) override;

Loading