-
Notifications
You must be signed in to change notification settings - Fork 877
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #101 from brave/chrome-profile-lock
Prompt user to close Chrome before importing from a Chrome profile
- Loading branch information
Showing
13 changed files
with
552 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* 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_IMPORTER_LOCK_DIALOG_H_ | ||
#define BRAVE_BROWSER_IMPORTER_BRAVE_IMPORTER_LOCK_DIALOG_H_ | ||
|
||
#include "base/callback_forward.h" | ||
#include "ui/gfx/native_widget_types.h" | ||
|
||
namespace brave { | ||
namespace importer { | ||
|
||
// This function is called by an ImporterHost, and presents the Chrome profile | ||
// warning dialog. After closing the dialog, the ImportHost receives a callback | ||
// with the message either to skip the import, or to continue the process. | ||
void ShowImportLockDialog(gfx::NativeWindow parent, | ||
const base::Callback<void(bool)>& callback); | ||
|
||
} // namespace importer | ||
} // namespace brave | ||
|
||
#endif // BRAVE_BROWSER_IMPORTER_BRAVE_IMPORTER_LOCK_DIALOG_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "brave/browser/importer/chrome_profile_lock.h" | ||
|
||
#include "base/bind.h" | ||
#include "base/bind_helpers.h" | ||
|
||
ChromeProfileLock::ChromeProfileLock( | ||
const base::FilePath& user_data_dir) | ||
: lock_acquired_(false), | ||
user_data_dir_(user_data_dir), | ||
process_singleton_(new ProcessSingleton(user_data_dir, | ||
base::Bind(&ChromeProfileLock::NotificationCallback, | ||
base::Unretained(this)))) { | ||
Lock(); | ||
} | ||
|
||
ChromeProfileLock::~ChromeProfileLock() { | ||
Unlock(); | ||
} | ||
|
||
void ChromeProfileLock::Lock() { | ||
if (HasAcquired()) | ||
return; | ||
lock_acquired_ = process_singleton_->Create(); | ||
} | ||
|
||
void ChromeProfileLock::Unlock() { | ||
if (!HasAcquired()) | ||
return; | ||
process_singleton_->Cleanup(); | ||
process_singleton_.reset(new ProcessSingleton(user_data_dir_, | ||
base::Bind(&ChromeProfileLock::NotificationCallback, | ||
base::Unretained(this)))); | ||
lock_acquired_ = false; | ||
} | ||
|
||
bool ChromeProfileLock::HasAcquired() { | ||
return lock_acquired_; | ||
} | ||
|
||
bool ChromeProfileLock::NotificationCallback( | ||
const base::CommandLine& command_line, | ||
const base::FilePath& current_directory) { | ||
return false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* 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_CHROME_PROFILE_LOCK_H__ | ||
#define BRAVE_BROWSER_IMPORTER_CHROME_PROFILE_LOCK_H__ | ||
|
||
#include "base/command_line.h" | ||
#include "base/files/file_path.h" | ||
#include "chrome/browser/process_singleton.h" | ||
|
||
class ChromeProfileLock { | ||
public: | ||
explicit ChromeProfileLock(const base::FilePath& user_data_dir); | ||
~ChromeProfileLock(); | ||
|
||
// Locks and releases the profile. | ||
void Lock(); | ||
void Unlock(); | ||
|
||
// Returns true if we lock the profile successfully. | ||
bool HasAcquired(); | ||
|
||
private: | ||
bool lock_acquired_; | ||
base::FilePath user_data_dir_; | ||
std::unique_ptr<ProcessSingleton> process_singleton_; | ||
|
||
bool NotificationCallback(const base::CommandLine& command_line, | ||
const base::FilePath& current_directory); | ||
|
||
DISALLOW_COPY_AND_ASSIGN(ChromeProfileLock); | ||
}; | ||
|
||
#endif // BRAVE_BROWSER_IMPORTER_CHROME_PROFILE_LOCK_H__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* 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/chrome_profile_lock.h" | ||
|
||
#if defined(OS_POSIX) | ||
#include <sys/types.h> | ||
#include <sys/stat.h> | ||
#include <unistd.h> | ||
#endif | ||
|
||
#include <memory> | ||
|
||
#include "base/files/file_util.h" | ||
#include "base/files/scoped_temp_dir.h" | ||
#include "base/strings/string_util.h" | ||
#include "base/test/test_simple_task_runner.h" | ||
#include "base/threading/thread_task_runner_handle.h" | ||
#include "build/build_config.h" | ||
#include "chrome/common/chrome_constants.h" | ||
#include "chrome/common/chrome_paths.h" | ||
#include "testing/gtest/include/gtest/gtest.h" | ||
|
||
#if defined(OS_WIN) | ||
const char kLockFile[] = "lockfile"; | ||
#endif | ||
|
||
class ChromeProfileLockTest : public testing::Test { | ||
public: | ||
ChromeProfileLockTest () | ||
: task_runner_(base::MakeRefCounted<base::TestSimpleTaskRunner>()), | ||
thread_task_runner_handle_override_( | ||
base::ThreadTaskRunnerHandle::OverrideForTesting(task_runner_)) {} | ||
~ChromeProfileLockTest () override { | ||
task_runner_->RunUntilIdle(); | ||
} | ||
protected: | ||
void SetUp() override { | ||
testing::Test::SetUp(); | ||
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | ||
user_data_path_ = temp_dir_.GetPath(); | ||
#if defined(OS_POSIX) | ||
lock_file_path_ = | ||
user_data_path_.Append(chrome::kSingletonLockFilename); | ||
#elif defined(OS_WIN) | ||
lock_file_path_ = user_data_path_.AppendASCII(kLockFile); | ||
#else | ||
#error unsupport platforms | ||
#endif | ||
} | ||
|
||
void LockFileExists(bool expect); | ||
|
||
base::ScopedTempDir temp_dir_; | ||
base::FilePath user_data_path_; | ||
base::FilePath lock_file_path_; | ||
scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | ||
base::ScopedClosureRunner thread_task_runner_handle_override_; | ||
}; | ||
|
||
void ChromeProfileLockTest::LockFileExists(bool expect) { | ||
#if defined(OS_POSIX) | ||
struct stat statbuf; | ||
if (expect) { | ||
ASSERT_EQ(0, lstat(lock_file_path_.value().c_str(), &statbuf)); | ||
ASSERT_TRUE(S_ISLNK(statbuf.st_mode)); | ||
char buf[PATH_MAX]; | ||
ssize_t len = readlink(lock_file_path_.value().c_str(), buf, PATH_MAX); | ||
ASSERT_GT(len, 0); | ||
} else { | ||
ASSERT_GT(0, lstat(lock_file_path_.value().c_str(), &statbuf)); | ||
} | ||
#elif defined(OS_WIN) | ||
if (expect) | ||
EXPECT_TRUE(base::PathExists(lock_file_path_)); | ||
else | ||
EXPECT_FALSE(base::PathExists(lock_file_path_)); | ||
#endif | ||
} | ||
|
||
TEST_F(ChromeProfileLockTest, LockTest) { | ||
ChromeProfileLock lock(user_data_path_); | ||
ASSERT_TRUE(lock.HasAcquired()); | ||
lock.Unlock(); | ||
ASSERT_FALSE(lock.HasAcquired()); | ||
lock.Lock(); | ||
ASSERT_TRUE(lock.HasAcquired()); | ||
} | ||
|
||
// Tests basic functionality and verifies that the lock file is deleted after | ||
// use. | ||
TEST_F(ChromeProfileLockTest, ProfileLock) { | ||
std::unique_ptr<ChromeProfileLock> lock; | ||
EXPECT_EQ(static_cast<ChromeProfileLock*>(NULL), lock.get()); | ||
LockFileExists(false); | ||
lock.reset(new ChromeProfileLock(user_data_path_)); | ||
EXPECT_TRUE(lock->HasAcquired()); | ||
LockFileExists(true); | ||
lock->Unlock(); | ||
EXPECT_FALSE(lock->HasAcquired()); | ||
|
||
lock->Lock(); | ||
EXPECT_TRUE(lock->HasAcquired()); | ||
LockFileExists(true); | ||
lock->Lock(); | ||
EXPECT_TRUE(lock->HasAcquired()); | ||
lock->Unlock(); | ||
EXPECT_FALSE(lock->HasAcquired()); | ||
} |
Oops, something went wrong.