| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/base_paths.h" | 7 #include "base/base_paths.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
| 10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 *result = IsWindowVisible(window) != 0; | 35 *result = IsWindowVisible(window) != 0; |
| 36 // Stops enumeration if a visible window has been found. | 36 // Stops enumeration if a visible window has been found. |
| 37 return !*result; | 37 return !*result; |
| 38 } | 38 } |
| 39 | 39 |
| 40 } // namespace | 40 } // namespace |
| 41 | 41 |
| 42 // Look for a Chrome instance that uses the same profile directory. | 42 // Look for a Chrome instance that uses the same profile directory. |
| 43 ProcessSingleton::ProcessSingleton(const FilePath& user_data_dir) | 43 ProcessSingleton::ProcessSingleton(const FilePath& user_data_dir) |
| 44 : window_(NULL), locked_(false), foreground_window_(NULL) { | 44 : window_(NULL), locked_(false), foreground_window_(NULL) { |
| 45 std::wstring user_data_dir_str(user_data_dir.ToWStringHack()); | |
| 46 remote_window_ = FindWindowEx(HWND_MESSAGE, NULL, | 45 remote_window_ = FindWindowEx(HWND_MESSAGE, NULL, |
| 47 chrome::kMessageWindowClass, | 46 chrome::kMessageWindowClass, |
| 48 user_data_dir_str.c_str()); | 47 user_data_dir.value().c_str()); |
| 49 if (!remote_window_) { | 48 if (!remote_window_) { |
| 50 // Make sure we will be the one and only process creating the window. | 49 // Make sure we will be the one and only process creating the window. |
| 51 // We use a named Mutex since we are protecting against multi-process | 50 // We use a named Mutex since we are protecting against multi-process |
| 52 // access. As documented, it's clearer to NOT request ownership on creation | 51 // access. As documented, it's clearer to NOT request ownership on creation |
| 53 // since it isn't guaranteed we will get it. It is better to create it | 52 // since it isn't guaranteed we will get it. It is better to create it |
| 54 // without ownership and explicitly get the ownership afterward. | 53 // without ownership and explicitly get the ownership afterward. |
| 55 std::wstring mutex_name(L"Local\\ChromeProcessSingletonStartup!"); | 54 std::wstring mutex_name(L"Local\\ChromeProcessSingletonStartup!"); |
| 56 base::win::ScopedHandle only_me( | 55 base::win::ScopedHandle only_me( |
| 57 CreateMutex(NULL, FALSE, mutex_name.c_str())); | 56 CreateMutex(NULL, FALSE, mutex_name.c_str())); |
| 58 DCHECK(only_me.Get() != NULL) << "GetLastError = " << GetLastError(); | 57 DCHECK(only_me.Get() != NULL) << "GetLastError = " << GetLastError(); |
| 59 | 58 |
| 60 // This is how we acquire the mutex (as opposed to the initial ownership). | 59 // This is how we acquire the mutex (as opposed to the initial ownership). |
| 61 DWORD result = WaitForSingleObject(only_me, INFINITE); | 60 DWORD result = WaitForSingleObject(only_me, INFINITE); |
| 62 DCHECK(result == WAIT_OBJECT_0) << "Result = " << result << | 61 DCHECK(result == WAIT_OBJECT_0) << "Result = " << result << |
| 63 "GetLastError = " << GetLastError(); | 62 "GetLastError = " << GetLastError(); |
| 64 | 63 |
| 65 // We now own the mutex so we are the only process that can create the | 64 // We now own the mutex so we are the only process that can create the |
| 66 // window at this time, but we must still check if someone created it | 65 // window at this time, but we must still check if someone created it |
| 67 // between the time where we looked for it above and the time the mutex | 66 // between the time where we looked for it above and the time the mutex |
| 68 // was given to us. | 67 // was given to us. |
| 69 remote_window_ = FindWindowEx(HWND_MESSAGE, NULL, | 68 remote_window_ = FindWindowEx(HWND_MESSAGE, NULL, |
| 70 chrome::kMessageWindowClass, | 69 chrome::kMessageWindowClass, |
| 71 user_data_dir_str.c_str()); | 70 user_data_dir.value().c_str()); |
| 72 if (!remote_window_) | 71 if (!remote_window_) |
| 73 Create(); | 72 Create(); |
| 74 BOOL success = ReleaseMutex(only_me); | 73 BOOL success = ReleaseMutex(only_me); |
| 75 DCHECK(success) << "GetLastError = " << GetLastError(); | 74 DCHECK(success) << "GetLastError = " << GetLastError(); |
| 76 } | 75 } |
| 77 } | 76 } |
| 78 | 77 |
| 79 ProcessSingleton::~ProcessSingleton() { | 78 ProcessSingleton::~ProcessSingleton() { |
| 80 if (window_) { | 79 if (window_) { |
| 81 DestroyWindow(window_); | 80 DestroyWindow(window_); |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 305 switch (message) { | 304 switch (message) { |
| 306 case WM_COPYDATA: | 305 case WM_COPYDATA: |
| 307 return OnCopyData(reinterpret_cast<HWND>(wparam), | 306 return OnCopyData(reinterpret_cast<HWND>(wparam), |
| 308 reinterpret_cast<COPYDATASTRUCT*>(lparam)); | 307 reinterpret_cast<COPYDATASTRUCT*>(lparam)); |
| 309 default: | 308 default: |
| 310 break; | 309 break; |
| 311 } | 310 } |
| 312 | 311 |
| 313 return ::DefWindowProc(hwnd, message, wparam, lparam); | 312 return ::DefWindowProc(hwnd, message, wparam, lparam); |
| 314 } | 313 } |
| OLD | NEW |