Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(275)

Side by Side Diff: chrome/browser/process_singleton_win.cc

Issue 57082: Greatly reduce the race window in ProcessSingleton... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 11 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "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/process_util.h" 9 #include "base/process_util.h"
10 #include "base/win_util.h" 10 #include "base/win_util.h"
11 #include "chrome/browser/browser_init.h" 11 #include "chrome/browser/browser_init.h"
12 #include "chrome/browser/browser_process.h" 12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/profile.h" 13 #include "chrome/browser/profile.h"
14 #include "chrome/browser/profile_manager.h" 14 #include "chrome/browser/profile_manager.h"
15 #include "chrome/common/chrome_constants.h" 15 #include "chrome/common/chrome_constants.h"
16 #include "chrome/common/chrome_paths.h" 16 #include "chrome/common/chrome_paths.h"
17 #include "chrome/common/l10n_util.h" 17 #include "chrome/common/l10n_util.h"
18 #include "chrome/common/result_codes.h" 18 #include "chrome/common/result_codes.h"
19 #include "chrome/common/win_util.h" 19 #include "chrome/common/win_util.h"
20 #include "grit/chromium_strings.h" 20 #include "grit/chromium_strings.h"
21 #include "grit/generated_resources.h" 21 #include "grit/generated_resources.h"
22 22
23 namespace { 23 namespace {
24 24
25 // Checks the visiblilty of the enumerated window and signals once a visible 25 // Checks the visibility of the enumerated window and signals once a visible
26 // window has been found. 26 // window has been found.
27 BOOL CALLBACK BrowserWindowEnumeration(HWND window, LPARAM param) { 27 BOOL CALLBACK BrowserWindowEnumeration(HWND window, LPARAM param) {
28 bool* result = reinterpret_cast<bool*>(param); 28 bool* result = reinterpret_cast<bool*>(param);
29 *result = IsWindowVisible(window) != 0; 29 *result = IsWindowVisible(window) != 0;
30 // Stops enumeration if a visible window has been found. 30 // Stops enumeration if a visible window has been found.
31 return !*result; 31 return !*result;
32 } 32 }
33 33
34 } // namespace 34 } // namespace
35 35
36 // Look for a Chrome instance that uses the same profile directory.
36 ProcessSingleton::ProcessSingleton(const FilePath& user_data_dir) 37 ProcessSingleton::ProcessSingleton(const FilePath& user_data_dir)
37 : window_(NULL), 38 : window_(NULL), locked_(false) {
38 locked_(false) { 39 // FindWindoEx and Create() should be one atomic operation in order to not
39 // Look for a Chrome instance that uses the same profile directory: 40 // have a race condition.
40 remote_window_ = FindWindowEx(HWND_MESSAGE, 41 remote_window_ = FindWindowEx(HWND_MESSAGE, NULL, chrome::kMessageWindowClass,
41 NULL,
42 chrome::kMessageWindowClass,
43 user_data_dir.ToWStringHack().c_str()); 42 user_data_dir.ToWStringHack().c_str());
43 if (!remote_window_)
44 Create();
44 } 45 }
45 46
46 ProcessSingleton::~ProcessSingleton() { 47 ProcessSingleton::~ProcessSingleton() {
47 if (window_) 48 if (window_)
48 DestroyWindow(window_); 49 DestroyWindow(window_);
49 } 50 }
50 51
51 bool ProcessSingleton::NotifyOtherProcess() { 52 bool ProcessSingleton::NotifyOtherProcess() {
52 if (!remote_window_) 53 if (!remote_window_)
53 return false; 54 return false;
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 return true; 120 return true;
120 } 121 }
121 } 122 }
122 123
123 // Time to take action. Kill the browser process. 124 // Time to take action. Kill the browser process.
124 base::KillProcessById(process_id, ResultCodes::HUNG, true); 125 base::KillProcessById(process_id, ResultCodes::HUNG, true);
125 remote_window_ = NULL; 126 remote_window_ = NULL;
126 return false; 127 return false;
127 } 128 }
128 129
130 // For windows, there is no need to call Create() since the call is made in
131 // the constructor but to avoid having more platform specific code in
132 // browser_main.cc we tolerate a second call which will do nothing.
129 void ProcessSingleton::Create() { 133 void ProcessSingleton::Create() {
130 DCHECK(!window_);
131 DCHECK(!remote_window_); 134 DCHECK(!remote_window_);
135 if (window_)
136 return;
137
132 HINSTANCE hinst = GetModuleHandle(NULL); 138 HINSTANCE hinst = GetModuleHandle(NULL);
133 139
134 WNDCLASSEX wc = {0}; 140 WNDCLASSEX wc = {0};
135 wc.cbSize = sizeof(wc); 141 wc.cbSize = sizeof(wc);
136 wc.lpfnWndProc = ProcessSingleton::WndProcStatic; 142 wc.lpfnWndProc = ProcessSingleton::WndProcStatic;
137 wc.hInstance = hinst; 143 wc.hInstance = hinst;
138 wc.lpszClassName = chrome::kMessageWindowClass; 144 wc.lpszClassName = chrome::kMessageWindowClass;
139 RegisterClassEx(&wc); 145 RegisterClassEx(&wc);
140 146
141 std::wstring user_data_dir; 147 std::wstring user_data_dir;
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 switch (message) { 247 switch (message) {
242 case WM_COPYDATA: 248 case WM_COPYDATA:
243 return OnCopyData(reinterpret_cast<HWND>(wparam), 249 return OnCopyData(reinterpret_cast<HWND>(wparam),
244 reinterpret_cast<COPYDATASTRUCT*>(lparam)); 250 reinterpret_cast<COPYDATASTRUCT*>(lparam));
245 default: 251 default:
246 break; 252 break;
247 } 253 }
248 254
249 return ::DefWindowProc(hwnd, message, wparam, lparam); 255 return ::DefWindowProc(hwnd, message, wparam, lparam);
250 } 256 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698