Skip to content

Commit

Permalink
Merge pull request #981 from brave/issue/998
Browse files Browse the repository at this point in the history
Import open windows and tabs from legacy Brave
  • Loading branch information
garrettr authored and bsclifton committed Nov 30, 2018
1 parent 0e1c7b6 commit 705ef5d
Show file tree
Hide file tree
Showing 33 changed files with 445 additions and 28 deletions.
3 changes: 3 additions & 0 deletions app/brave_generated_resources.grd
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@
<message name="IDS_SETTINGS_IMPORT_LEDGER_CHECKBOX" desc="Checkbox for importing ledger">
Brave Payments
</message>
<message name="IDS_SETTINGS_IMPORT_WINDOWS_CHECKBOX" desc="Checkbox for importing open windows and tabs">
Open windows and tabs
</message>
<message name="IDS_WIDEVINE_NOT_INSTALLED_EXPLANATORY_TEXT" desc="Explanatory animated text that appears (and then disappears) in the address line when Widevine is blocked">
Widevine is not installed
</message>
Expand Down
1 change: 1 addition & 0 deletions browser/brave_profile_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterBooleanPref(prefs::kImportDialogCookies, true);
registry->RegisterBooleanPref(prefs::kImportDialogStats, true);
registry->RegisterBooleanPref(prefs::kImportDialogLedger, true);
registry->RegisterBooleanPref(prefs::kImportDialogWindows, true);
// Importer: ledger (used for Brave Rewards pinned => tips)
registry->RegisterIntegerPref(kBravePaymentsPinnedItemCount, 0);
}
Expand Down
2 changes: 2 additions & 0 deletions browser/extensions/api/settings_private/brave_prefs_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ const PrefsUtil::TypedPrefMap& BravePrefsUtil::GetWhitelistedKeys() {
settings_api::PrefType::PREF_TYPE_BOOLEAN;
(*s_brave_whitelist)[::prefs::kImportDialogLedger] =
settings_api::PrefType::PREF_TYPE_BOOLEAN;
(*s_brave_whitelist)[::prefs::kImportDialogWindows] =
settings_api::PrefType::PREF_TYPE_BOOLEAN;
// Default Brave shields
(*s_brave_whitelist)[kHTTPSEVerywhereControlType] =
settings_api::PrefType::PREF_TYPE_BOOLEAN;
Expand Down
8 changes: 8 additions & 0 deletions browser/importer/brave_external_process_importer_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,12 @@ void BraveExternalProcessImporterClient::OnReferralImportReady(
bridge_->UpdateReferral(referral);
}

void BraveExternalProcessImporterClient::OnWindowsImportReady(
const ImportedWindowState& windowState) {
if (cancelled_)
return;

bridge_->UpdateWindows(windowState);
}

BraveExternalProcessImporterClient::~BraveExternalProcessImporterClient() {}
3 changes: 3 additions & 0 deletions browser/importer/brave_external_process_importer_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

struct BraveStats;
struct BraveLedger;
struct ImportedWindowState;

class BraveExternalProcessImporterClient : public ExternalProcessImporterClient {
public:
Expand All @@ -35,6 +36,8 @@ class BraveExternalProcessImporterClient : public ExternalProcessImporterClient
const BraveLedger& ledger) override;
void OnReferralImportReady(
const BraveReferral& referral) override;
void OnWindowsImportReady(
const ImportedWindowState& windowState) override;

private:
~BraveExternalProcessImporterClient() override;
Expand Down
6 changes: 6 additions & 0 deletions browser/importer/brave_in_process_importer_bridge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,10 @@ void BraveInProcessImporterBridge::UpdateReferral(const BraveReferral& referral)
writer_->UpdateReferral(referral);
}

void BraveInProcessImporterBridge::UpdateWindows(
const ImportedWindowState& windowState) {
// TODO: Can we just restore windows/tabs here? Do we even need to do anything with the ProfileWriter?
writer_->UpdateWindows(windowState);
}

BraveInProcessImporterBridge::~BraveInProcessImporterBridge() {}
1 change: 1 addition & 0 deletions browser/importer/brave_in_process_importer_bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class BraveInProcessImporterBridge : public InProcessImporterBridge {
void UpdateStats(const BraveStats& stats) override;
void UpdateLedger(const BraveLedger& ledger) override;
void UpdateReferral(const BraveReferral& referral) override;
void UpdateWindows(const ImportedWindowState& windowState) override;

void FinishLedgerImport();
void Cancel();
Expand Down
93 changes: 92 additions & 1 deletion browser/importer/brave_profile_writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "brave/browser/importer/brave_profile_writer.h"
#include "brave/common/importer/brave_stats.h"
#include "brave/common/importer/brave_referral.h"
#include "brave/common/importer/imported_browser_window.h"
#include "brave/common/pref_names.h"
#include "brave/components/brave_rewards/browser/rewards_service.h"
#include "brave/components/brave_rewards/browser/rewards_service_factory.h"
Expand All @@ -19,6 +20,12 @@

#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_navigator.h"
#include "chrome/browser/ui/browser_navigator_params.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/storage_partition.h"
Expand All @@ -28,6 +35,7 @@
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_getter.h"
#include "services/network/public/mojom/cookie_manager.mojom.h"
#include "ui/base/ui_base_types.h"

#include <sstream>

Expand Down Expand Up @@ -81,7 +89,6 @@ void BraveProfileWriter::UpdateStats(const BraveStats& stats) {
https_upgrades + stats.httpsEverywhere_count);
}
}

void BraveProfileWriter::SetBridge(BraveInProcessImporterBridge* bridge) {
bridge_ptr_ = bridge;
}
Expand Down Expand Up @@ -316,3 +323,87 @@ void BraveProfileWriter::UpdateReferral(const BraveReferral& referral) {
local_state->ClearPref(kReferralTimestamp);
}
}

Browser* OpenImportedBrowserWindow(
const ImportedBrowserWindow& window,
Profile* profile) {
Browser::CreateParams params(Browser::TYPE_TABBED, profile, false);

params.initial_bounds = gfx::Rect(window.top, window.left,
window.width, window.height);

ui::WindowShowState show_state = ui::SHOW_STATE_DEFAULT;
if (window.state == "normal") {
show_state = ui::SHOW_STATE_NORMAL;
} else if (window.state == "minimized") {
show_state = ui::SHOW_STATE_MINIMIZED;
} else if (window.state == "maximized") {
show_state = ui::SHOW_STATE_MAXIMIZED;
} else if (window.state == "fullscreen") {
show_state = ui::SHOW_STATE_FULLSCREEN;
}
params.initial_show_state = show_state;

return new Browser(params);
}

void OpenImportedBrowserTabs(Browser* browser,
const std::vector<ImportedBrowserTab>& tabs,
bool pinned) {
for (const auto tab : tabs) {
NavigateParams params(browser, tab.location,
ui::PAGE_TRANSITION_AUTO_TOPLEVEL);
params.disposition = WindowOpenDisposition::NEW_BACKGROUND_TAB;
params.tabstrip_add_types = pinned ? TabStripModel::ADD_PINNED
: TabStripModel::ADD_FORCE_INDEX;
Navigate(&params);
}
}

int GetSelectedTabIndex(const ImportedBrowserWindow& window) {
// The window has an activeFrameKey, which may be equal to the key for one of
// its tabs. Find the matching tab, if one exists, and return its index in
// the tabs vector.
for (int i = 0; i < (int)window.tabs.size(); i++) {
if (window.activeFrameKey == window.tabs[i].key)
return i;
}

// If there was no matching tab, default to returning the index of the
// right-most tab.
return window.tabs.size() - 1;
}

void ShowBrowser(Browser* browser, int selected_tab_index) {
DCHECK(browser);
DCHECK(browser->tab_strip_model()->count());
browser->tab_strip_model()->ActivateTabAt(selected_tab_index, true);
browser->window()->Show();
browser->tab_strip_model()->GetActiveWebContents()->SetInitialFocus();
}

void PrependPinnedTabs(Browser* browser,
const std::vector<ImportedBrowserTab>& tabs) {
OpenImportedBrowserTabs(browser, tabs, true);
}

void BraveProfileWriter::UpdateWindows(
const ImportedWindowState& windowState) {
Browser* active = chrome::FindBrowserWithActiveWindow();
Browser* first = nullptr;

for (const auto window : windowState.windows) {
Browser* browser = OpenImportedBrowserWindow(window, profile_);
OpenImportedBrowserTabs(browser, window.tabs, false);
ShowBrowser(browser, GetSelectedTabIndex(window));

if (!first)
first = browser;
}

PrependPinnedTabs(first, windowState.pinnedTabs);

// Re-focus the window that was originally focused before import.
if (active)
active->window()->Show();
}
2 changes: 2 additions & 0 deletions browser/importer/brave_profile_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
struct BraveStats;
struct BraveReferral;
class BraveInProcessImporterBridge;
struct ImportedWindowState;

class BraveProfileWriter : public ProfileWriter,
public brave_rewards::RewardsServiceObserver {
Expand All @@ -26,6 +27,7 @@ class BraveProfileWriter : public ProfileWriter,
virtual void UpdateStats(const BraveStats& stats);
virtual void UpdateLedger(const BraveLedger& ledger);
virtual void UpdateReferral(const BraveReferral& referral);
virtual void UpdateWindows(const ImportedWindowState& windowState);

void SetBridge(BraveInProcessImporterBridge* bridge);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@

struct BraveLedger;
struct BraveStats;
struct ImportedWindowState;

#include "../../../../../chrome/browser/importer/external_process_importer_client.h"
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ void BraveAddImportDataStrings(content::WebUIDataSource* html_source) {
{"importCookies", IDS_SETTINGS_IMPORT_COOKIES_CHECKBOX},
{"importStats", IDS_SETTINGS_IMPORT_STATS_CHECKBOX},
{"importLedger", IDS_SETTINGS_IMPORT_LEDGER_CHECKBOX},
{"importWindows", IDS_SETTINGS_IMPORT_WINDOWS_CHECKBOX},
};
AddLocalizedStringsBulk(html_source, localized_strings,
arraysize(localized_strings));
Expand Down
1 change: 1 addition & 0 deletions chromium_src/chrome/common/importer/importer_bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
struct BraveLedger;
struct BraveStats;
struct BraveReferral;
struct ImportedWindowState;

#include "../../../../../chrome/common/importer/importer_bridge.h"
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "brave/common/importer/brave_ledger.h"
#include "brave/common/importer/brave_referral.h"
#include "brave/common/importer/brave_stats.h"
#include "brave/common/importer/imported_browser_window.h"

IPC_STRUCT_TRAITS_BEGIN(BraveStats)
IPC_STRUCT_TRAITS_MEMBER(adblock_count)
Expand Down Expand Up @@ -45,3 +46,24 @@ IPC_STRUCT_TRAITS_BEGIN(BraveReferral)
IPC_STRUCT_TRAITS_MEMBER(finalize_timestamp)
IPC_STRUCT_TRAITS_MEMBER(week_of_installation)
IPC_STRUCT_TRAITS_END()

IPC_STRUCT_TRAITS_BEGIN(ImportedBrowserTab)
IPC_STRUCT_TRAITS_MEMBER(key)
IPC_STRUCT_TRAITS_MEMBER(location)
IPC_STRUCT_TRAITS_END()

IPC_STRUCT_TRAITS_BEGIN(ImportedBrowserWindow)
IPC_STRUCT_TRAITS_MEMBER(top)
IPC_STRUCT_TRAITS_MEMBER(left)
IPC_STRUCT_TRAITS_MEMBER(width)
IPC_STRUCT_TRAITS_MEMBER(height)
IPC_STRUCT_TRAITS_MEMBER(focused)
IPC_STRUCT_TRAITS_MEMBER(state)
IPC_STRUCT_TRAITS_MEMBER(activeFrameKey)
IPC_STRUCT_TRAITS_MEMBER(tabs)
IPC_STRUCT_TRAITS_END()

IPC_STRUCT_TRAITS_BEGIN(ImportedWindowState)
IPC_STRUCT_TRAITS_MEMBER(windows)
IPC_STRUCT_TRAITS_MEMBER(pinnedTabs)
IPC_STRUCT_TRAITS_END()
3 changes: 2 additions & 1 deletion chromium_src/chrome/common/pref_names.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ namespace prefs {
const char kImportDialogCookies[] = "import_dialog_cookies";
const char kImportDialogStats[] = "import_dialog_stats";
const char kImportDialogLedger[] = "import_dialog_ledger";
const char kImportDialogWindows[] = "import_dialog_windows";

} // namespace prefs
} // namespace prefs
3 changes: 2 additions & 1 deletion chromium_src/chrome/common/pref_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ namespace prefs {
extern const char kImportDialogCookies[];
extern const char kImportDialogStats[];
extern const char kImportDialogLedger[];
extern const char kImportDialogWindows[];

} // namespace prefs
} // namespace prefs
2 changes: 2 additions & 0 deletions common/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ source_set("common") {
"brave_switches.h",
"importer/brave_referral.cc",
"importer/brave_referral.h",
"importer/imported_browser_window.cc",
"importer/imported_browser_window.h",
"pref_names.cc",
"pref_names.h",
"webui_url_constants.cc",
Expand Down
3 changes: 2 additions & 1 deletion common/importer/brave_importer_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ bool BraveImporterCanImport(const base::FilePath& profile,
profile.Append(base::FilePath::StringType(FILE_PATH_LITERAL("ledger-state.json")));

if (base::PathExists(session_store))
*services_supported |= importer::HISTORY | importer::FAVORITES | importer::STATS;
*services_supported |= importer::HISTORY | importer::FAVORITES |
importer::STATS | importer::WINDOWS;
if (base::PathExists(passwords))
*services_supported |= importer::PASSWORDS;
if (base::PathExists(cookies))
Expand Down
3 changes: 3 additions & 0 deletions common/importer/brave_mock_importer_bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "brave/common/importer/brave_ledger.h"
#include "brave/common/importer/brave_stats.h"
#include "brave/common/importer/imported_browser_window.h"
#include "chrome/common/importer/mock_importer_bridge.h"
#include "net/cookies/canonical_cookie.h"
#include "testing/gmock/include/gmock/gmock.h"
Expand All @@ -26,6 +27,8 @@ class BraveMockImporterBridge : public MockImporterBridge {
void(const BraveStats&));
MOCK_METHOD1(UpdateLedger,
void(const BraveLedger&));
MOCK_METHOD1(UpdateWindows,
void(const ImportedWindowState&));

private:
~BraveMockImporterBridge() override;
Expand Down
17 changes: 17 additions & 0 deletions common/importer/imported_browser_window.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* 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/imported_browser_window.h"

ImportedBrowserTab::ImportedBrowserTab() {}
ImportedBrowserTab::ImportedBrowserTab(const ImportedBrowserTab& other) = default;
ImportedBrowserTab::~ImportedBrowserTab() {}

ImportedBrowserWindow::ImportedBrowserWindow() {}
ImportedBrowserWindow::ImportedBrowserWindow(const ImportedBrowserWindow& other) = default;
ImportedBrowserWindow::~ImportedBrowserWindow() {}

ImportedWindowState::ImportedWindowState() {}
ImportedWindowState::ImportedWindowState(const ImportedWindowState& other) = default;
ImportedWindowState::~ImportedWindowState() {}
54 changes: 54 additions & 0 deletions common/importer/imported_browser_window.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* 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_IMPORTED_BROWSER_WINDOW_H_
#define BRAVE_COMMON_IMPORTER_IMPORTED_BROWSER_WINDOW_H_

#include <string>
#include <vector>
#include "url/gurl.h"

struct ImportedBrowserTab {
ImportedBrowserTab();
ImportedBrowserTab(const ImportedBrowserTab& other);
~ImportedBrowserTab();

int key;
GURL location;
};

struct ImportedBrowserWindow {
ImportedBrowserWindow();
ImportedBrowserWindow(const ImportedBrowserWindow& other);
~ImportedBrowserWindow();

int top;
int left;
int width;
int height;

bool focused;

// "normal", "minimized", "maximized", or "fullscreen"
std::string state;

// Warning: activeFrameKey may not reference an existing key in the frames
// Array. For example, if a private or private w/ Tor tab was focused when the
// browser closed, it will be the activeFrameKey, but it will not be
// persisted.
int activeFrameKey;

std::vector<ImportedBrowserTab> tabs;
};

struct ImportedWindowState {
ImportedWindowState();
ImportedWindowState(const ImportedWindowState& other);
~ImportedWindowState();

std::vector<ImportedBrowserWindow> windows;
std::vector<ImportedBrowserTab> pinnedTabs;
};

#endif // BRAVE_COMMON_IMPORTER_IMPORTED_BROWSER_WINDOW_H_
Loading

0 comments on commit 705ef5d

Please sign in to comment.