Chromium Code Reviews| Index: chrome/browser/process_singleton_win.cc |
| diff --git a/chrome/browser/process_singleton_win.cc b/chrome/browser/process_singleton_win.cc |
| index 783c9da1a6260270fecd633ad675d9e16a3125e4..787deb11ba4f1891d9b909c50f097e1eb47aeb09 100644 |
| --- a/chrome/browser/process_singleton_win.cc |
| +++ b/chrome/browser/process_singleton_win.cc |
| @@ -23,6 +23,8 @@ |
| namespace { |
| +const char kLockfile[] = "lockfile"; |
| + |
| // Checks the visibility of the enumerated window and signals once a visible |
| // window has been found. |
| BOOL CALLBACK BrowserWindowEnumeration(HWND window, LPARAM param) { |
| @@ -148,7 +150,7 @@ bool ProcessSingleton::EscapeVirtualization(const FilePath& user_data_dir) { |
| // the profile directory path. |
| ProcessSingleton::ProcessSingleton(const FilePath& user_data_dir) |
| : window_(NULL), locked_(false), foreground_window_(NULL), |
| - is_virtualized_(false) { |
| + is_virtualized_(false), lock_file_(INVALID_HANDLE_VALUE) { |
| remote_window_ = FindWindowEx(HWND_MESSAGE, NULL, |
| chrome::kMessageWindowClass, |
| user_data_dir.value().c_str()); |
| @@ -175,7 +177,24 @@ ProcessSingleton::ProcessSingleton(const FilePath& user_data_dir) |
| remote_window_ = FindWindowEx(HWND_MESSAGE, NULL, |
| chrome::kMessageWindowClass, |
| user_data_dir.value().c_str()); |
| - if (!remote_window_) { |
| + |
| + // We have to make sure there is no Chrome instance running on another |
| + // machine that uses the same profile. |
| + FilePath lock_file_path = user_data_dir.AppendASCII(kLockfile); |
| + lock_file_ = CreateFile(lock_file_path.value().c_str(), |
| + GENERIC_WRITE, |
| + FILE_SHARE_READ, |
| + NULL, |
| + CREATE_ALWAYS, |
| + FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE, |
| + NULL); |
| + DWORD error = GetLastError(); |
|
cpu_(ooo_6.6-7.5)
2012/07/16 20:36:31
this version I like better. Now for two local chro
pastarmovj
2012/07/16 21:17:53
For two local instances the first one will acquire
|
| + LOG_IF(WARNING, lock_file_ != INVALID_HANDLE_VALUE && |
| + error == ERROR_ALREADY_EXISTS) << "Lock file exists but is writable."; |
| + LOG_IF(ERROR, lock_file_ == INVALID_HANDLE_VALUE) |
| + << "Lock file can not be created! Error code: " << error; |
| + |
| + if (!remote_window_ && lock_file_ != INVALID_HANDLE_VALUE) { |
| HINSTANCE hinst = base::GetModuleFromAddress(&ThunkWndProc); |
| WNDCLASSEX wc = {0}; |
| @@ -207,11 +226,15 @@ ProcessSingleton::~ProcessSingleton() { |
| ::UnregisterClass(chrome::kMessageWindowClass, |
| base::GetModuleFromAddress(&ThunkWndProc)); |
| } |
| + if (lock_file_ != INVALID_HANDLE_VALUE) |
| + CloseHandle(lock_file_); |
| } |
| ProcessSingleton::NotifyResult ProcessSingleton::NotifyOtherProcess() { |
| if (is_virtualized_) |
| return PROCESS_NOTIFIED; // We already spawned the process in this case. |
| + if (lock_file_ == INVALID_HANDLE_VALUE && !remote_window_) |
| + return LOCK_ERROR; |
| else if (!remote_window_) |
| return PROCESS_NONE; |