| OLD | NEW |
| 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 | 7 |
| 8 #include "build/build_config.h" | 8 #include "build/build_config.h" |
| 9 | 9 |
| 10 #if defined(OS_WIN) | 10 #if defined(OS_WIN) |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 // Chrome process was launched. Return true if the command line will be | 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 | 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). | 62 // should handle it (i.e., because the current process is shutting down). |
| 63 typedef base::Callback< | 63 typedef base::Callback< |
| 64 bool(const CommandLine& command_line, const FilePath& current_directory)> | 64 bool(const CommandLine& command_line, const FilePath& current_directory)> |
| 65 NotificationCallback; | 65 NotificationCallback; |
| 66 | 66 |
| 67 explicit ProcessSingleton(const FilePath& user_data_dir); | 67 explicit ProcessSingleton(const FilePath& user_data_dir); |
| 68 ~ProcessSingleton(); | 68 ~ProcessSingleton(); |
| 69 | 69 |
| 70 // Notify another process, if available. | |
| 71 // Returns true if another process was found and notified, false if we | |
| 72 // should continue with this process. | |
| 73 // Windows code roughly based on Mozilla. | |
| 74 // | |
| 75 // TODO(brettw): this will not handle all cases. If two process start up too | |
| 76 // close to each other, the Create() might not yet have happened for the | |
| 77 // first one, so this function won't find it. | |
| 78 NotifyResult NotifyOtherProcess(); | |
| 79 | |
| 80 // Notify another process, if available. Otherwise sets ourselves as the | 70 // Notify another process, if available. Otherwise sets ourselves as the |
| 81 // singleton instance and stores the provided callback for notification from | 71 // singleton instance and stores the provided callback for notification from |
| 82 // future processes. Returns PROCESS_NONE if we became the singleton | 72 // future processes. Returns PROCESS_NONE if we became the singleton |
| 83 // instance. | 73 // instance. |
| 84 NotifyResult NotifyOtherProcessOrCreate( | 74 NotifyResult NotifyOtherProcessOrCreate( |
| 85 const NotificationCallback& notification_callback); | 75 const NotificationCallback& notification_callback); |
| 86 | 76 |
| 87 #if defined(OS_LINUX) || defined(OS_OPENBSD) | 77 #if defined(OS_WIN) |
| 88 // Exposed for testing. We use a timeout on Linux, and in tests we want | 78 // Used in specific cases to let us know that there is an existing instance |
| 89 // this timeout to be short. | 79 // of Chrome running with this profile. In general, you should not use this |
| 90 NotifyResult NotifyOtherProcessWithTimeout(const CommandLine& command_line, | 80 // function. Instead consider using NotifyOtherProcessOrCreate(). |
| 91 int timeout_seconds, | 81 // For non profile-specific method, use |
| 92 bool kill_unresponsive); | 82 // browser_util::IsBrowserAlreadyRunning(). |
| 93 NotifyResult NotifyOtherProcessWithTimeoutOrCreate( | 83 bool FoundOtherProcessWindow() const { |
| 94 const CommandLine& command_line, | 84 return (NULL != remote_window_); |
| 95 const NotificationCallback& notification_callback, | 85 } |
| 96 int timeout_seconds); | 86 #endif // defined(OS_WIN) |
| 97 void OverrideCurrentPidForTesting(base::ProcessId pid); | |
| 98 void OverrideKillCallbackForTesting(const base::Callback<void(int)>& callback)
; | |
| 99 static void DisablePromptForTesting(); | |
| 100 #endif // defined(OS_LINUX) || defined(OS_OPENBSD) | |
| 101 | |
| 102 // Sets ourself up as the singleton instance. Returns true on success. If | |
| 103 // false is returned, we are not the singleton instance and the caller must | |
| 104 // exit. Otherwise, stores the provided callback for notification from | |
| 105 // future processes. | |
| 106 bool Create( | |
| 107 const NotificationCallback& notification_callback); | |
| 108 | 87 |
| 109 // Clear any lock state during shutdown. | 88 // Clear any lock state during shutdown. |
| 110 void Cleanup(); | 89 void Cleanup(); |
| 111 | 90 |
| 112 // Blocks the dispatch of CopyData messages. foreground_window refers | 91 // Blocks the dispatch of CopyData messages. foreground_window refers |
| 113 // to the window that should be set to the foreground if a CopyData message | 92 // to the window that should be set to the foreground if a CopyData message |
| 114 // is received while the ProcessSingleton is locked. | 93 // is received while the ProcessSingleton is locked. |
| 115 void Lock(gfx::NativeWindow foreground_window) { | 94 void Lock(gfx::NativeWindow foreground_window) { |
| 116 DCHECK(CalledOnValidThread()); | 95 DCHECK(CalledOnValidThread()); |
| 117 locked_ = true; | 96 locked_ = true; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 130 | 109 |
| 131 bool locked() { | 110 bool locked() { |
| 132 DCHECK(CalledOnValidThread()); | 111 DCHECK(CalledOnValidThread()); |
| 133 return locked_; | 112 return locked_; |
| 134 } | 113 } |
| 135 | 114 |
| 136 #if defined(OS_WIN) | 115 #if defined(OS_WIN) |
| 137 LRESULT WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam); | 116 LRESULT WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam); |
| 138 #endif | 117 #endif |
| 139 | 118 |
| 119 #if defined(OS_LINUX) || defined(OS_OPENBSD) |
| 120 static void DisablePromptForTesting(); |
| 121 #endif // defined(OS_LINUX) || defined(OS_OPENBSD) |
| 122 |
| 123 protected: |
| 124 // Notify another process, if available. |
| 125 // Returns true if another process was found and notified, false if we |
| 126 // should continue with this process. |
| 127 // Windows code roughly based on Mozilla. |
| 128 // |
| 129 // TODO(brettw): this will not handle all cases. If two processes start up too |
| 130 // close to each other, the Create() might not yet have happened for the |
| 131 // first one, so this function won't find it. |
| 132 virtual NotifyResult NotifyOtherProcess(); |
| 133 |
| 134 // Sets ourself up as the singleton instance. Returns true on success. If |
| 135 // false is returned, we are not the singleton instance and the caller must |
| 136 // exit. Otherwise, stores the provided callback for notification from |
| 137 // future processes. |
| 138 virtual bool Create( |
| 139 const NotificationCallback& notification_callback); |
| 140 |
| 141 #if defined(OS_LINUX) || defined(OS_OPENBSD) |
| 142 // Exposed for testing. We use a timeout on Linux, and in tests we want |
| 143 // this timeout to be short. |
| 144 virtual NotifyResult NotifyOtherProcessWithTimeout( |
| 145 const CommandLine& command_line, |
| 146 int timeout_seconds, |
| 147 bool kill_unresponsive); |
| 148 virtual NotifyResult NotifyOtherProcessWithTimeoutOrCreate( |
| 149 const CommandLine& command_line, |
| 150 const NotificationCallback& notification_callback, |
| 151 int timeout_seconds); |
| 152 virtual void OverrideCurrentPidForTesting(base::ProcessId pid); |
| 153 virtual void OverrideKillCallbackForTesting( |
| 154 const base::Callback<void(int)>& callback); |
| 155 #endif // defined(OS_LINUX) || defined(OS_OPENBSD) |
| 156 |
| 140 private: | 157 private: |
| 141 typedef std::pair<CommandLine::StringVector, FilePath> DelayedStartupMessage; | 158 typedef std::pair<CommandLine::StringVector, FilePath> DelayedStartupMessage; |
| 142 | 159 |
| 143 #if !defined(OS_MACOSX) | 160 #if !defined(OS_MACOSX) |
| 144 // Timeout for the current browser process to respond. 20 seconds should be | 161 // Timeout for the current browser process to respond. 20 seconds should be |
| 145 // enough. It's only used in Windows and Linux implementations. | 162 // enough. It's only used in Windows and Linux implementations. |
| 146 static const int kTimeoutInSeconds = 20; | 163 static const int kTimeoutInSeconds = 20; |
| 147 #endif | 164 #endif |
| 148 | 165 |
| 149 bool locked_; | 166 bool locked_; |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 #endif | 227 #endif |
| 211 | 228 |
| 212 // If messages are received in the locked state, the corresponding command | 229 // If messages are received in the locked state, the corresponding command |
| 213 // lines are saved here to be replayed later. | 230 // lines are saved here to be replayed later. |
| 214 std::vector<DelayedStartupMessage> saved_startup_messages_; | 231 std::vector<DelayedStartupMessage> saved_startup_messages_; |
| 215 | 232 |
| 216 DISALLOW_COPY_AND_ASSIGN(ProcessSingleton); | 233 DISALLOW_COPY_AND_ASSIGN(ProcessSingleton); |
| 217 }; | 234 }; |
| 218 | 235 |
| 219 #endif // CHROME_BROWSER_PROCESS_SINGLETON_H_ | 236 #endif // CHROME_BROWSER_PROCESS_SINGLETON_H_ |
| OLD | NEW |