Skip to content

Commit

Permalink
Merge pull request #1105 from brave/issue/2503
Browse files Browse the repository at this point in the history
Handle stale lockfiles in Muon browser profile
  • Loading branch information
garrettr committed Dec 17, 2018
2 parents 54faec8 + 42f635f commit f5c1558
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 7 deletions.
1 change: 1 addition & 0 deletions browser/importer/brave_external_process_importer_host.cc
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ bool BraveExternalProcessImporterHost::CheckForChromeOrBraveLock() {
browser_lock_.reset(new BraveProfileLock(source_profile_.source_path));
}

browser_lock_->Lock();
if (browser_lock_->HasAcquired())
return true;

Expand Down
19 changes: 19 additions & 0 deletions browser/importer/brave_profile_lock.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,27 @@

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

#include "build/build_config.h"
#include "base/logging.h"
#include "base/threading/thread_restrictions.h"

BraveProfileLock::BraveProfileLock(
const base::FilePath& user_data_dir)
: ChromeProfileLock(user_data_dir) {}

BraveProfileLock::~BraveProfileLock() {}

void BraveProfileLock::Lock() {
#if defined(OS_WIN)
ChromeProfileLock::Lock();
#elif defined(OS_POSIX)
base::ThreadRestrictions::ScopedAllowIO allow_io;
if (HasAcquired())
return;

ProcessSingleton::NotifyResult rv =
process_singleton_->NotifyOtherProcessOrCreate();
LOG(INFO) << "BraveProfileLock::Lock: NotifyOtherProcessOrCreate rv: " << rv;
lock_acquired_ = rv == ProcessSingleton::PROCESS_NONE;
#endif
}
2 changes: 2 additions & 0 deletions browser/importer/brave_profile_lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class BraveProfileLock : public ChromeProfileLock {
public:
explicit BraveProfileLock(const base::FilePath& user_data_dir);
~BraveProfileLock() override;

void Lock() override;
};

#endif // BRAVE_BROWSER_IMPORTER_BRAVE_PROFILE_LOCK_H__
10 changes: 5 additions & 5 deletions browser/importer/chrome_profile_lock.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@

#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/logging.h"
#include "base/threading/thread_restrictions.h"
#include "chrome/browser/process_singleton.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();
}
base::Bind(&ChromeProfileLock::NotificationCallback,
base::Unretained(this)))),
user_data_dir_(user_data_dir) {}

ChromeProfileLock::~ChromeProfileLock() {
Unlock();
Expand Down
5 changes: 3 additions & 2 deletions browser/importer/chrome_profile_lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ class ChromeProfileLock : public BrowserProfileLock {
// Returns true if we lock the profile successfully.
bool HasAcquired() override;

private:
protected:
bool lock_acquired_;
base::FilePath user_data_dir_;
std::unique_ptr<ProcessSingleton> process_singleton_;

private:
base::FilePath user_data_dir_;
bool NotificationCallback(const base::CommandLine& command_line,
const base::FilePath& current_directory);

Expand Down
2 changes: 2 additions & 0 deletions browser/importer/chrome_profile_lock_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ void ChromeProfileLockTest::LockFileExists(bool expect) {

TEST_F(ChromeProfileLockTest, LockTest) {
ChromeProfileLock lock(user_data_path_);
lock.Lock();
ASSERT_TRUE(lock.HasAcquired());
lock.Unlock();
ASSERT_FALSE(lock.HasAcquired());
Expand All @@ -89,6 +90,7 @@ TEST_F(ChromeProfileLockTest, ProfileLock) {
EXPECT_EQ(static_cast<ChromeProfileLock*>(NULL), lock.get());
LockFileExists(false);
lock.reset(new ChromeProfileLock(user_data_path_));
lock->Lock();
EXPECT_TRUE(lock->HasAcquired());
LockFileExists(true);
lock->Unlock();
Expand Down

0 comments on commit f5c1558

Please sign in to comment.