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