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

Side by Side Diff: chrome/browser/process_singleton.h

Issue 9968053: Refactor ProcessSingleton so that it may be used distinctly from a full browser process. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Include cleanup. Created 8 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 #ifndef CHROME_BROWSER_PROCESS_SINGLETON_H_ 5 #ifndef CHROME_BROWSER_PROCESS_SINGLETON_H_
6 #define CHROME_BROWSER_PROCESS_SINGLETON_H_ 6 #define CHROME_BROWSER_PROCESS_SINGLETON_H_
7 #pragma once 7 #pragma once
8 8
9 #include "build/build_config.h" 9 #include "build/build_config.h"
10 10
11 #if defined(OS_WIN) 11 #if defined(OS_WIN)
12 #include <windows.h> 12 #include <windows.h>
13 #endif // defined(OS_WIN) 13 #endif // defined(OS_WIN)
14 14
15 #include <set> 15 #include <set>
16 #include <vector> 16 #include <vector>
17 17
18 #include "base/basictypes.h" 18 #include "base/basictypes.h"
19 #include "base/callback.h"
19 #include "base/command_line.h" 20 #include "base/command_line.h"
20 #include "base/file_path.h" 21 #include "base/file_path.h"
21 #include "base/logging.h" 22 #include "base/logging.h"
22 #include "base/memory/ref_counted.h" 23 #include "base/memory/ref_counted.h"
23 #include "base/threading/non_thread_safe.h" 24 #include "base/threading/non_thread_safe.h"
24 #include "ui/gfx/native_widget_types.h" 25 #include "ui/gfx/native_widget_types.h"
25 26
26 #if defined(OS_POSIX) 27 #if defined(OS_POSIX)
27 #include "base/file_path.h" 28 #include "base/file_path.h"
28 #endif // defined(OS_POSIX) 29 #endif // defined(OS_POSIX)
(...skipping 18 matching lines...) Expand all
47 48
48 class ProcessSingleton : public base::NonThreadSafe { 49 class ProcessSingleton : public base::NonThreadSafe {
49 public: 50 public:
50 enum NotifyResult { 51 enum NotifyResult {
51 PROCESS_NONE, 52 PROCESS_NONE,
52 PROCESS_NOTIFIED, 53 PROCESS_NOTIFIED,
53 PROFILE_IN_USE, 54 PROFILE_IN_USE,
54 LOCK_ERROR, 55 LOCK_ERROR,
55 }; 56 };
56 57
58 // Implement this callback to handle notifications from other processes. The
59 // callback will receive the command line and directory with which the other
60 // Chrome process was launched. Return true if the command line will be
61 // handled within the current browser instance or false if the remote process
62 // should handle it (i.e., because the current process is shutting down).
63 typedef base::Callback<
64 bool(const CommandLine& command_line, const FilePath& current_directory)>
65 NotificationCallback;
66
57 explicit ProcessSingleton(const FilePath& user_data_dir); 67 explicit ProcessSingleton(const FilePath& user_data_dir);
58 ~ProcessSingleton(); 68 ~ProcessSingleton();
59 69
60 // Notify another process, if available. 70 // Notify another process, if available.
61 // Returns true if another process was found and notified, false if we 71 // Returns true if another process was found and notified, false if we
62 // should continue with this process. 72 // should continue with this process.
63 // Windows code roughly based on Mozilla. 73 // Windows code roughly based on Mozilla.
64 // 74 //
65 // TODO(brettw): this will not handle all cases. If two process start up too 75 // TODO(brettw): this will not handle all cases. If two process start up too
66 // close to each other, the Create() might not yet have happened for the 76 // close to each other, the Create() might not yet have happened for the
67 // first one, so this function won't find it. 77 // first one, so this function won't find it.
68 NotifyResult NotifyOtherProcess(); 78 NotifyResult NotifyOtherProcess();
69 79
70 // Notify another process, if available. Otherwise sets ourselves as the 80 // Notify another process, if available. Otherwise sets ourselves as the
71 // singleton instance. Returns PROCESS_NONE if we became the singleton 81 // singleton instance and stores the provided callback for notification from
82 // future processes. Returns PROCESS_NONE if we became the singleton
72 // instance. 83 // instance.
73 NotifyResult NotifyOtherProcessOrCreate(); 84 NotifyResult NotifyOtherProcessOrCreate(
85 const NotificationCallback& notification_callback);
74 86
75 #if defined(OS_LINUX) || defined(OS_OPENBSD) 87 #if defined(OS_LINUX) || defined(OS_OPENBSD)
76 // Exposed for testing. We use a timeout on Linux, and in tests we want 88 // Exposed for testing. We use a timeout on Linux, and in tests we want
77 // this timeout to be short. 89 // this timeout to be short.
78 NotifyResult NotifyOtherProcessWithTimeout(const CommandLine& command_line, 90 NotifyResult NotifyOtherProcessWithTimeout(const CommandLine& command_line,
79 int timeout_seconds, 91 int timeout_seconds,
80 bool kill_unresponsive); 92 bool kill_unresponsive);
81 NotifyResult NotifyOtherProcessWithTimeoutOrCreate( 93 NotifyResult NotifyOtherProcessWithTimeoutOrCreate(
82 const CommandLine& command_line, 94 const CommandLine& command_line,
95 const NotificationCallback& notification_callback,
83 int timeout_seconds); 96 int timeout_seconds);
84 #endif // defined(OS_LINUX) || defined(OS_OPENBSD) 97 #endif // defined(OS_LINUX) || defined(OS_OPENBSD)
85 98
86 #if defined(OS_WIN) && !defined(USE_AURA) 99 #if defined(OS_WIN) && !defined(USE_AURA)
87 // Used in specific cases to let us know that there is an existing instance 100 // Used in specific cases to let us know that there is an existing instance
88 // of Chrome running with this profile. In general, you should not use this 101 // of Chrome running with this profile. In general, you should not use this
89 // function. Instead consider using NotifyOtherProcessOrCreate(). 102 // function. Instead consider using NotifyOtherProcessOrCreate().
90 // For non profile-specific method, use 103 // For non profile-specific method, use
91 // browser_util::IsBrowserAlreadyRunning(). 104 // browser_util::IsBrowserAlreadyRunning().
92 bool FoundOtherProcessWindow() const { 105 bool FoundOtherProcessWindow() const {
93 return (NULL != remote_window_); 106 return (NULL != remote_window_);
94 } 107 }
95 #endif // defined(OS_WIN) 108 #endif // defined(OS_WIN)
96 109
97 // Sets ourself up as the singleton instance. Returns true on success. If 110 // Sets ourself up as the singleton instance. Returns true on success. If
98 // false is returned, we are not the singleton instance and the caller must 111 // false is returned, we are not the singleton instance and the caller must
99 // exit. 112 // exit. Otherwise, stores the provided callback for notification from
100 bool Create(); 113 // future processes.
114 bool Create(
115 const NotificationCallback& notification_callback);
101 116
102 // Clear any lock state during shutdown. 117 // Clear any lock state during shutdown.
103 void Cleanup(); 118 void Cleanup();
104 119
105 // Blocks the dispatch of CopyData messages. foreground_window refers 120 // Blocks the dispatch of CopyData messages. foreground_window refers
106 // to the window that should be set to the foreground if a CopyData message 121 // to the window that should be set to the foreground if a CopyData message
107 // is received while the ProcessSingleton is locked. 122 // is received while the ProcessSingleton is locked.
108 void Lock(gfx::NativeWindow foreground_window) { 123 void Lock(gfx::NativeWindow foreground_window) {
109 DCHECK(CalledOnValidThread()); 124 DCHECK(CalledOnValidThread());
110 locked_ = true; 125 locked_ = true;
(...skipping 14 matching lines...) Expand all
125 DCHECK(CalledOnValidThread()); 140 DCHECK(CalledOnValidThread());
126 return locked_; 141 return locked_;
127 } 142 }
128 143
129 #if defined(OS_WIN) 144 #if defined(OS_WIN)
130 LRESULT WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam); 145 LRESULT WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam);
131 #endif 146 #endif
132 147
133 private: 148 private:
134 typedef std::pair<CommandLine::StringVector, FilePath> DelayedStartupMessage; 149 typedef std::pair<CommandLine::StringVector, FilePath> DelayedStartupMessage;
135 void ProcessCommandLine(const CommandLine& command_line,
136 const FilePath& current_directory);
137 150
138 #if !defined(OS_MACOSX) 151 #if !defined(OS_MACOSX)
139 // Timeout for the current browser process to respond. 20 seconds should be 152 // Timeout for the current browser process to respond. 20 seconds should be
140 // enough. It's only used in Windows and Linux implementations. 153 // enough. It's only used in Windows and Linux implementations.
141 static const int kTimeoutInSeconds = 20; 154 static const int kTimeoutInSeconds = 20;
142 #endif 155 #endif
143 156
144 bool locked_; 157 bool locked_;
145 gfx::NativeWindow foreground_window_; 158 gfx::NativeWindow foreground_window_;
159 NotificationCallback notification_callback_; // Handler for notifications
robertshield 2012/04/04 01:07:15 micro nit: '.' at the end
erikwright (departed) 2012/04/04 14:51:28 Done.
146 160
147 #if defined(OS_WIN) 161 #if defined(OS_WIN)
148 // This ugly behemoth handles startup commands sent from another process. 162 // This ugly behemoth handles startup commands sent from another process.
149 LRESULT OnCopyData(HWND hwnd, const COPYDATASTRUCT* cds); 163 LRESULT OnCopyData(HWND hwnd, const COPYDATASTRUCT* cds);
150 164
151 bool EscapeVirtualization(const FilePath& user_data_dir); 165 bool EscapeVirtualization(const FilePath& user_data_dir);
152 166
153 HWND remote_window_; // The HWND_MESSAGE of another browser. 167 HWND remote_window_; // The HWND_MESSAGE of another browser.
154 HWND window_; // The HWND_MESSAGE window. 168 HWND window_; // The HWND_MESSAGE window.
155 bool is_virtualized_; // Stuck inside Microsoft Softricity VM environment. 169 bool is_virtualized_; // Stuck inside Microsoft Softricity VM environment.
(...skipping 25 matching lines...) Expand all
181 #endif 195 #endif
182 196
183 // If messages are received in the locked state, the corresponding command 197 // If messages are received in the locked state, the corresponding command
184 // lines are saved here to be replayed later. 198 // lines are saved here to be replayed later.
185 std::vector<DelayedStartupMessage> saved_startup_messages_; 199 std::vector<DelayedStartupMessage> saved_startup_messages_;
186 200
187 DISALLOW_COPY_AND_ASSIGN(ProcessSingleton); 201 DISALLOW_COPY_AND_ASSIGN(ProcessSingleton);
188 }; 202 };
189 203
190 #endif // CHROME_BROWSER_PROCESS_SINGLETON_H_ 204 #endif // CHROME_BROWSER_PROCESS_SINGLETON_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698