Skip to content

Commit 7673133

Browse files
authored
Merge pull request #245 from brave/brave-profile-lock
BraveProfileLock
2 parents 926df02 + 1dac838 commit 7673133

14 files changed

+142
-46
lines changed

app/brave_generated_resources.grd

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@
8080
<message name="IDS_CHROME_IMPORTER_LOCK_TEXT" desc="The message to be displayed in the Chrome importer lock dialog">
8181
To finish importing, close all Chrome windows.
8282
</message>
83+
<message name="IDS_BRAVE_IMPORTER_LOCK_TITLE" desc="Dialog title for Brave importer lock dialog">
84+
Close Brave
85+
</message>
86+
<message name="IDS_BRAVE_IMPORTER_LOCK_TEXT" desc="The message to be displayed in the Brave importer lock dialog">
87+
To finish importing, close all Brave windows.
88+
</message>
8389
<message name="IDS_SETTINGS_IMPORT_COOKIES_CHECKBOX" desc="Checkbox for importing cookies">
8490
Cookies
8591
</message>

browser/BUILD.gn

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ source_set("browser_process") {
3535
"importer/brave_in_process_importer_bridge.h",
3636
"importer/brave_profile_writer.cc",
3737
"importer/brave_profile_writer.h",
38+
"importer/brave_profile_lock.cc",
39+
"importer/brave_profile_lock.h",
40+
"importer/browser_profile_lock.h",
3841
"importer/chrome_profile_lock.cc",
3942
"importer/chrome_profile_lock.h",
4043
"sparkle_glue_mac.mm",

browser/importer/brave_external_process_importer_host.cc

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include "brave/browser/importer/brave_external_process_importer_client.h"
66
#include "brave/browser/importer/brave_external_process_importer_host.h"
7+
#include "brave/browser/importer/brave_profile_lock.h"
8+
#include "brave/browser/importer/chrome_profile_lock.h"
79
#include "brave/browser/importer/brave_in_process_importer_bridge.h"
810

911
#include "brave/browser/importer/brave_importer_lock_dialog.h"
@@ -33,7 +35,7 @@ void BraveExternalProcessImporterHost::StartImportSettings(
3335
return;
3436
}
3537

36-
if (!CheckForChromeLock(source_profile)) {
38+
if (!CheckForChromeOrBraveLock()) {
3739
Cancel();
3840
return;
3941
}
@@ -65,17 +67,18 @@ void BraveExternalProcessImporterHost::ShowWarningDialog() {
6567
DCHECK(!headless_);
6668
brave::importer::ShowImportLockDialog(
6769
parent_window_,
70+
source_profile_,
6871
base::Bind(&BraveExternalProcessImporterHost::OnImportLockDialogEnd,
6972
weak_ptr_factory_.GetWeakPtr()));
7073
}
7174

7275
void BraveExternalProcessImporterHost::OnImportLockDialogEnd(bool is_continue) {
7376
if (is_continue) {
74-
// User chose to continue, then we check the lock again to make
75-
// sure that Chrome has been closed. Try to import the settings
76-
// if successful. Otherwise, show a warning dialog.
77-
chrome_lock_->Lock();
78-
if (chrome_lock_->HasAcquired()) {
77+
// User chose to continue, then we check the lock again to make sure that
78+
// the other browser has been closed. Try to import the settings if
79+
// successful. Otherwise, show a warning dialog.
80+
browser_lock_->Lock();
81+
if (browser_lock_->HasAcquired()) {
7982
is_source_readable_ = true;
8083
LaunchImportIfReady();
8184
} else {
@@ -86,19 +89,24 @@ void BraveExternalProcessImporterHost::OnImportLockDialogEnd(bool is_continue) {
8689
}
8790
}
8891

89-
bool BraveExternalProcessImporterHost::CheckForChromeLock(
90-
const importer::SourceProfile& source_profile) {
91-
if (source_profile.importer_type != importer::TYPE_CHROME)
92+
bool BraveExternalProcessImporterHost::CheckForChromeOrBraveLock() {
93+
if (!(source_profile_.importer_type == importer::TYPE_CHROME ||
94+
source_profile_.importer_type == importer::TYPE_BRAVE))
9295
return true;
9396

94-
// Extract the user data directory from the path of the profile to be
95-
// imported, because we can only lock/unlock the entire user directory with
96-
// ProcessSingleton.
97-
base::FilePath user_data_dir = source_profile.source_path.DirName();
97+
DCHECK(!browser_lock_.get());
9898

99-
DCHECK(!chrome_lock_.get());
100-
chrome_lock_.reset(new ChromeProfileLock(user_data_dir));
101-
if (chrome_lock_->HasAcquired())
99+
if (source_profile_.importer_type == importer::TYPE_CHROME) {
100+
// Extract the user data directory from the path of the profile to be
101+
// imported, because we can only lock/unlock the entire user directory with
102+
// ProcessSingleton.
103+
base::FilePath user_data_dir = source_profile_.source_path.DirName();
104+
browser_lock_.reset(new ChromeProfileLock(user_data_dir));
105+
} else { // source_profile_.importer_type == importer::TYPE_BRAVE
106+
browser_lock_.reset(new BraveProfileLock(source_profile_.source_path));
107+
}
108+
109+
if (browser_lock_->HasAcquired())
102110
return true;
103111

104112
// If fail to acquire the lock, we set the source unreadable and

browser/importer/brave_external_process_importer_host.h

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#define BRAVE_BROWSER_IMPORTER_BRAVE_EXTERNAL_PROCESS_IMPORTER_HOST_H_
77

88
#include "base/memory/weak_ptr.h"
9-
#include "brave/browser/importer/chrome_profile_lock.h"
9+
#include "brave/browser/importer/browser_profile_lock.h"
1010
#include "chrome/browser/importer/external_process_importer_host.h"
1111

1212
class BraveExternalProcessImporterHost : public ExternalProcessImporterHost {
@@ -28,12 +28,11 @@ class BraveExternalProcessImporterHost : public ExternalProcessImporterHost {
2828
// complete.
2929
void LaunchImportIfReady() override;
3030

31-
// Make sure that Chrome isn't running, if import browser is Chrome. Show
32-
// to the user a dialog that notifies that is necessary to close Chrome
33-
// prior to continuing the import.
34-
// |source_profile| - importer profile to import.
35-
// Returns false iff import should be aborted.
36-
bool CheckForChromeLock(const importer::SourceProfile& source_profile);
31+
// Make sure that Chrome or Brave isn't running, if import browser is Chrome
32+
// or Brave. Show to the user a dialog that notifies that is necessary to
33+
// close Chrome or Brave prior to continuing the import. Returns false iff
34+
// import should be aborted.
35+
bool CheckForChromeOrBraveLock();
3736

3837
// ShowWarningDialog() asks user to close the application that is owning the
3938
// lock. They can retry or skip the importing process.
@@ -45,8 +44,8 @@ class BraveExternalProcessImporterHost : public ExternalProcessImporterHost {
4544
// the "Continue" button.
4645
void OnImportLockDialogEnd(bool is_continue);
4746

48-
// Chrome profile lock.
49-
std::unique_ptr<ChromeProfileLock> chrome_lock_;
47+
// Chrome or Brave profile lock.
48+
std::unique_ptr<BrowserProfileLock> browser_lock_;
5049

5150
// Vends weak pointers for the importer to call us back.
5251
base::WeakPtrFactory<BraveExternalProcessImporterHost> weak_ptr_factory_;

browser/importer/brave_importer_lock_dialog.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define BRAVE_BROWSER_IMPORTER_BRAVE_IMPORTER_LOCK_DIALOG_H_
77

88
#include "base/callback_forward.h"
9+
#include "chrome/common/importer/importer_data_types.h"
910
#include "ui/gfx/native_widget_types.h"
1011

1112
namespace brave {
@@ -15,6 +16,7 @@ namespace importer {
1516
// warning dialog. After closing the dialog, the ImportHost receives a callback
1617
// with the message either to skip the import, or to continue the process.
1718
void ShowImportLockDialog(gfx::NativeWindow parent,
19+
::importer::SourceProfile source_profile,
1820
const base::Callback<void(bool)>& callback);
1921

2022
} // namespace importer
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
3+
* You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
#include "brave/browser/importer/brave_profile_lock.h"
6+
7+
BraveProfileLock::BraveProfileLock(
8+
const base::FilePath& user_data_dir)
9+
: ChromeProfileLock(user_data_dir) {}
10+
11+
BraveProfileLock::~BraveProfileLock() {}

browser/importer/brave_profile_lock.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
3+
* You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
#ifndef BRAVE_BROWSER_IMPORTER_BRAVE_PROFILE_LOCK_H__
6+
#define BRAVE_BROWSER_IMPORTER_BRAVE_PROFILE_LOCK_H__
7+
8+
#include "base/files/file_path.h"
9+
#include "brave/browser/importer/chrome_profile_lock.h"
10+
11+
class BraveProfileLock : public ChromeProfileLock {
12+
public:
13+
explicit BraveProfileLock(const base::FilePath& user_data_dir);
14+
~BraveProfileLock() override;
15+
};
16+
17+
#endif // BRAVE_BROWSER_IMPORTER_BRAVE_PROFILE_LOCK_H__
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
3+
* You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
#ifndef BRAVE_BROWSER_IMPORTER_BROWSER_PROFILE_LOCK_H__
6+
#define BRAVE_BROWSER_IMPORTER_BROWSER_PROFILE_LOCK_H__
7+
8+
class BrowserProfileLock {
9+
public:
10+
virtual ~BrowserProfileLock() {};
11+
12+
// Locks and releases the profile.
13+
virtual void Lock() = 0;
14+
virtual void Unlock() = 0;
15+
16+
// Returns true if we lock the profile successfully.
17+
virtual bool HasAcquired() = 0;
18+
};
19+
20+
#endif // BRAVE_BROWSER_IMPORTER_BROWSER_PROFILE_LOCK_H__

browser/importer/chrome_profile_lock.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,28 @@
77

88
#include "base/command_line.h"
99
#include "base/files/file_path.h"
10+
#include "brave/browser/importer/browser_profile_lock.h"
1011
#include "chrome/browser/process_singleton.h"
1112

12-
class ChromeProfileLock {
13+
class ChromeProfileLock : public BrowserProfileLock {
1314
public:
1415
explicit ChromeProfileLock(const base::FilePath& user_data_dir);
15-
~ChromeProfileLock();
16+
~ChromeProfileLock() override;
1617

1718
// Locks and releases the profile.
18-
void Lock();
19-
void Unlock();
19+
void Lock() override;
20+
void Unlock() override;
2021

2122
// Returns true if we lock the profile successfully.
22-
bool HasAcquired();
23+
bool HasAcquired() override;
2324

2425
private:
2526
bool lock_acquired_;
2627
base::FilePath user_data_dir_;
2728
std::unique_ptr<ProcessSingleton> process_singleton_;
2829

2930
bool NotificationCallback(const base::CommandLine& command_line,
30-
const base::FilePath& current_directory);
31+
const base::FilePath& current_directory);
3132

3233
DISALLOW_COPY_AND_ASSIGN(ChromeProfileLock);
3334
};

browser/ui/cocoa/importer/brave_import_lock_dialog_cocoa.mm

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
#include "base/threading/thread_task_runner_handle.h"
1212
#include "brave/browser/importer/brave_importer_lock_dialog.h"
1313
#include "brave/browser/ui/views/importer/brave_import_lock_dialog_view.h"
14-
#include "brave/grit/brave_generated_resources.h"
1514
#include "chrome/browser/ui/cocoa/browser_dialogs_views_mac.h"
15+
#include "chrome/common/importer/importer_data_types.h"
1616
#include "chrome/grit/chromium_strings.h"
1717
#include "chrome/grit/generated_resources.h"
1818
#include "components/strings/grit/components_strings.h"
@@ -24,18 +24,27 @@
2424
namespace importer {
2525

2626
void ShowImportLockDialog(gfx::NativeWindow parent,
27+
importer::SourceProfile source_profile,
2728
const base::Callback<void(bool)>& callback) {
2829
if (chrome::ShowAllDialogsWithViewsToolkit())
29-
return ImportLockDialogView::Show(parent, callback);
30+
return ImportLockDialogView::Show(parent, source_profile, callback);
3031

3132
base::scoped_nsobject<NSAlert> lock_alert([[NSAlert alloc] init]);
3233
[lock_alert addButtonWithTitle:l10n_util::GetNSStringWithFixup(
3334
IDS_IMPORTER_LOCK_OK)];
3435
[lock_alert addButtonWithTitle:l10n_util::GetNSStringWithFixup(IDS_CANCEL)];
35-
[lock_alert setInformativeText:l10n_util::GetNSStringWithFixup(
36-
IDS_CHROME_IMPORTER_LOCK_TEXT)];
37-
[lock_alert setMessageText:l10n_util::GetNSStringWithFixup(
38-
IDS_CHROME_IMPORTER_LOCK_TITLE)];
36+
37+
if (source_profile.importer_type == importer::TYPE_CHROME) {
38+
[lock_alert setInformativeText:l10n_util::GetNSStringWithFixup(
39+
IDS_CHROME_IMPORTER_LOCK_TEXT)];
40+
[lock_alert setMessageText:l10n_util::GetNSStringWithFixup(
41+
IDS_CHROME_IMPORTER_LOCK_TITLE)];
42+
} else if (source_profile.importer_type == importer::TYPE_BRAVE) {
43+
[lock_alert setInformativeText:l10n_util::GetNSStringWithFixup(
44+
IDS_BRAVE_IMPORTER_LOCK_TEXT)];
45+
[lock_alert setMessageText:l10n_util::GetNSStringWithFixup(
46+
IDS_BRAVE_IMPORTER_LOCK_TITLE)];
47+
}
3948

4049
base::ThreadTaskRunnerHandle::Get()->PostTask(
4150
FROM_HERE,

browser/ui/views/importer/brave_import_lock_dialog_view.cc

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,38 @@ namespace importer {
3232

3333
#if !defined(OS_MACOSX) || BUILDFLAG(MAC_VIEWS_BROWSER)
3434
void ShowImportLockDialog(gfx::NativeWindow parent,
35+
::importer::SourceProfile source_profile,
3536
const base::Callback<void(bool)>& callback) {
36-
ImportLockDialogView::Show(parent, callback);
37+
ImportLockDialogView::Show(parent, source_profile, callback);
3738
}
3839
#endif // !OS_MACOSX || MAC_VIEWS_BROWSER
3940

4041
} // namespace importer
4142

4243
// static
4344
void ImportLockDialogView::Show(gfx::NativeWindow parent,
45+
::importer::SourceProfile source_profile,
4446
const base::Callback<void(bool)>& callback) {
4547
views::DialogDelegate::CreateDialogWidget(
46-
new ImportLockDialogView(callback), NULL, NULL)->Show();
48+
new ImportLockDialogView(source_profile, callback), NULL, NULL)->Show();
4749
base::RecordAction(UserMetricsAction("ImportLockDialogView_Shown"));
4850
}
4951

5052
ImportLockDialogView::ImportLockDialogView(
53+
::importer::SourceProfile source_profile,
5154
const base::Callback<void(bool)>& callback)
52-
: callback_(callback) {
55+
: source_profile_(source_profile),
56+
callback_(callback) {
5357
SetLayoutManager(std::make_unique<views::FillLayout>());
54-
views::Label* description_label =
58+
59+
views::Label* description_label;
60+
if (source_profile_.importer_type == ::importer::TYPE_CHROME) {
61+
description_label =
5562
new views::Label(l10n_util::GetStringUTF16(IDS_CHROME_IMPORTER_LOCK_TEXT));
63+
} else { // if (source_profile_.importer_type == ::importer::TYPE_BRAVE)
64+
description_label =
65+
new views::Label(l10n_util::GetStringUTF16(IDS_BRAVE_IMPORTER_LOCK_TEXT));
66+
}
5667
description_label->SetBorder(views::CreateEmptyBorder(
5768
ChromeLayoutProvider::Get()->GetDialogInsetsForContentType(views::TEXT,
5869
views::TEXT)));
@@ -79,7 +90,11 @@ base::string16 ImportLockDialogView::GetDialogButtonLabel(
7990
}
8091

8192
base::string16 ImportLockDialogView::GetWindowTitle() const {
82-
return l10n_util::GetStringUTF16(IDS_CHROME_IMPORTER_LOCK_TITLE);
93+
if (source_profile_.importer_type == ::importer::TYPE_CHROME) {
94+
return l10n_util::GetStringUTF16(IDS_CHROME_IMPORTER_LOCK_TITLE);
95+
} else { // if (source_profile_.importer_type == ::importer::TYPE_BRAVE) {
96+
return l10n_util::GetStringUTF16(IDS_BRAVE_IMPORTER_LOCK_TITLE);
97+
}
8398
}
8499

85100
bool ImportLockDialogView::Accept() {

browser/ui/views/importer/brave_import_lock_dialog_view.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "base/callback.h"
99
#include "base/compiler_specific.h"
1010
#include "base/macros.h"
11+
#include "chrome/common/importer/importer_data_types.h"
1112
#include "ui/views/window/dialog_delegate.h"
1213

1314
namespace brave {
@@ -17,10 +18,12 @@ namespace brave {
1718
class ImportLockDialogView : public views::DialogDelegateView {
1819
public:
1920
static void Show(gfx::NativeWindow parent,
21+
::importer::SourceProfile source_profile,
2022
const base::Callback<void(bool)>& callback);
2123

2224
private:
23-
explicit ImportLockDialogView(const base::Callback<void(bool)>& callback);
25+
explicit ImportLockDialogView(::importer::SourceProfile source_profile,
26+
const base::Callback<void(bool)>& callback);
2427
~ImportLockDialogView() override;
2528

2629
// views::View:
@@ -36,6 +39,8 @@ class ImportLockDialogView : public views::DialogDelegateView {
3639
bool ShouldShowCloseButton() const override;
3740

3841
private:
42+
::importer::SourceProfile source_profile_;
43+
3944
// Called with the result of the dialog.
4045
base::Callback<void(bool)> callback_;
4146

utility/importer/chrome_importer.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ double ChromeImporter::chromeTimeToDouble(int64_t time) {
302302
return ((time * 10 - 0x19DB1DED53E8000) / 10000) / 1000;
303303
}
304304

305-
void ChromeImporter::ImportPasswords(const base::FilePath& prefs) {
305+
void ChromeImporter::ImportPasswords(const base::FilePath& prefs_filename) {
306306
#if !defined(USE_X11)
307307
base::FilePath passwords_path =
308308
source_path_.Append(
@@ -329,7 +329,7 @@ void ChromeImporter::ImportPasswords(const base::FilePath& prefs) {
329329
}
330330
}
331331
#else
332-
base::FilePath prefs_path = source_path_.Append(prefs);
332+
base::FilePath prefs_path = source_path_.Append(prefs_filename);
333333
const base::Value *value;
334334
scoped_refptr<JsonPrefStore> prefs = new JsonPrefStore(prefs_path);
335335
int local_profile_id;

utility/importer/chrome_importer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class ChromeImporter : public Importer {
4545

4646
virtual void ImportBookmarks();
4747
virtual void ImportHistory();
48-
virtual void ImportPasswords(const base::FilePath& prefs);
48+
virtual void ImportPasswords(const base::FilePath& prefs_filename);
4949
virtual void ImportCookies();
5050

5151
double chromeTimeToDouble(int64_t time);

0 commit comments

Comments
 (0)