Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/process_singleton.h" | 5 #include "chrome/browser/process_singleton.h" |
| 6 | 6 |
| 7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
| 8 #include "app/win_util.h" | 8 #include "app/win_util.h" |
| 9 #include "base/base_paths.h" | 9 #include "base/base_paths.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/path_service.h" | 11 #include "base/path_service.h" |
| 12 #include "base/process_util.h" | 12 #include "base/process_util.h" |
| 13 #include "base/scoped_handle.h" | |
| 13 #include "base/win_util.h" | 14 #include "base/win_util.h" |
| 14 #include "chrome/browser/browser_init.h" | 15 #include "chrome/browser/browser_init.h" |
| 15 #include "chrome/browser/browser_process.h" | 16 #include "chrome/browser/browser_process.h" |
| 16 #include "chrome/browser/profile.h" | 17 #include "chrome/browser/profile.h" |
| 17 #include "chrome/browser/profile_manager.h" | 18 #include "chrome/browser/profile_manager.h" |
| 18 #include "chrome/common/chrome_constants.h" | 19 #include "chrome/common/chrome_constants.h" |
| 19 #include "chrome/common/chrome_paths.h" | 20 #include "chrome/common/chrome_paths.h" |
| 20 #include "chrome/common/result_codes.h" | 21 #include "chrome/common/result_codes.h" |
| 22 #include "chrome/installer/util/browser_distribution.h" | |
| 21 #include "grit/chromium_strings.h" | 23 #include "grit/chromium_strings.h" |
| 22 #include "grit/generated_resources.h" | 24 #include "grit/generated_resources.h" |
| 23 | 25 |
| 24 namespace { | 26 namespace { |
| 25 | 27 |
| 26 // Checks the visibility of the enumerated window and signals once a visible | 28 // Checks the visibility of the enumerated window and signals once a visible |
| 27 // window has been found. | 29 // window has been found. |
| 28 BOOL CALLBACK BrowserWindowEnumeration(HWND window, LPARAM param) { | 30 BOOL CALLBACK BrowserWindowEnumeration(HWND window, LPARAM param) { |
| 29 bool* result = reinterpret_cast<bool*>(param); | 31 bool* result = reinterpret_cast<bool*>(param); |
| 30 *result = IsWindowVisible(window) != 0; | 32 *result = IsWindowVisible(window) != 0; |
| 31 // Stops enumeration if a visible window has been found. | 33 // Stops enumeration if a visible window has been found. |
| 32 return !*result; | 34 return !*result; |
| 33 } | 35 } |
| 34 | 36 |
| 35 } // namespace | 37 } // namespace |
| 36 | 38 |
| 37 // Look for a Chrome instance that uses the same profile directory. | 39 // Look for a Chrome instance that uses the same profile directory. |
| 38 ProcessSingleton::ProcessSingleton(const FilePath& user_data_dir) | 40 ProcessSingleton::ProcessSingleton(const FilePath& user_data_dir) |
| 39 : window_(NULL), locked_(false), foreground_window_(NULL) { | 41 : window_(NULL), locked_(false), foreground_window_(NULL) { |
| 40 // FindWindoEx and Create() should be one atomic operation in order to not | 42 std::wstring user_data_dir_str(user_data_dir.ToWStringHack()); |
| 41 // have a race condition. | 43 remote_window_ = FindWindowEx(HWND_MESSAGE, NULL, |
| 42 remote_window_ = FindWindowEx(HWND_MESSAGE, NULL, chrome::kMessageWindowClass, | 44 chrome::kMessageWindowClass, |
| 43 user_data_dir.ToWStringHack().c_str()); | 45 user_data_dir_str.c_str()); |
| 44 if (!remote_window_) | 46 if (!remote_window_) { |
| 45 Create(); | 47 // Make sure we will be the one and only process creating the window. |
| 48 // We use a named Mutex since we are protecting against multi-process | |
| 49 // access. As documented, it's clearer to NOT request ownership on creation | |
| 50 // since it isn't guaranteed we will get it. It is better to create it | |
| 51 // without ownership and explicitly get the ownership afterward. | |
| 52 std::wstring mutex_name(L"Local\\ProcessSingletonStartup!"); | |
| 53 mutex_name += BrowserDistribution::GetDistribution()->GetAppGuid(); | |
| 54 ScopedHandle only_me(CreateMutex(NULL, FALSE, mutex_name.c_str())); | |
| 55 DCHECK(only_me.Get() != NULL) << "GetLastError = " << GetLastError(); | |
| 56 | |
| 57 // This is how we acquire the mutex (as opposed to the initial ownership). | |
| 58 DWORD result = WaitForSingleObject(only_me, INFINITE); | |
|
Sigurður Ásgeirsson
2012/04/11 12:17:40
WFSO can also return WAIT_ABANDONED for success. T
| |
| 59 DCHECK(result == WAIT_OBJECT_0) << "Result = " << result << | |
| 60 "GetLastError = " << GetLastError(); | |
| 61 | |
| 62 // We now own the mutex so we are the only process that can create the | |
| 63 // window at this time, but we must still check if someone created it | |
| 64 // between the time where we looked for it above and the time the mutex | |
| 65 // was given to us. | |
| 66 remote_window_ = FindWindowEx(HWND_MESSAGE, NULL, | |
| 67 chrome::kMessageWindowClass, | |
| 68 user_data_dir_str.c_str()); | |
| 69 if (!remote_window_) | |
| 70 Create(); | |
| 71 BOOL success = ReleaseMutex(only_me); | |
| 72 DCHECK(success) << "GetLastError = " << GetLastError(); | |
| 73 } | |
| 46 } | 74 } |
| 47 | 75 |
| 48 ProcessSingleton::~ProcessSingleton() { | 76 ProcessSingleton::~ProcessSingleton() { |
| 49 if (window_) { | 77 if (window_) { |
| 50 DestroyWindow(window_); | 78 DestroyWindow(window_); |
| 51 UnregisterClass(chrome::kMessageWindowClass, GetModuleHandle(NULL)); | 79 UnregisterClass(chrome::kMessageWindowClass, GetModuleHandle(NULL)); |
| 52 } | 80 } |
| 53 } | 81 } |
| 54 | 82 |
| 55 ProcessSingleton::NotifyResult ProcessSingleton::NotifyOtherProcess() { | 83 ProcessSingleton::NotifyResult ProcessSingleton::NotifyOtherProcess() { |
| (...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 261 switch (message) { | 289 switch (message) { |
| 262 case WM_COPYDATA: | 290 case WM_COPYDATA: |
| 263 return OnCopyData(reinterpret_cast<HWND>(wparam), | 291 return OnCopyData(reinterpret_cast<HWND>(wparam), |
| 264 reinterpret_cast<COPYDATASTRUCT*>(lparam)); | 292 reinterpret_cast<COPYDATASTRUCT*>(lparam)); |
| 265 default: | 293 default: |
| 266 break; | 294 break; |
| 267 } | 295 } |
| 268 | 296 |
| 269 return ::DefWindowProc(hwnd, message, wparam, lparam); | 297 return ::DefWindowProc(hwnd, message, wparam, lparam); |
| 270 } | 298 } |
| OLD | NEW |