[email protected] | e3ce40a | 2012-01-31 03:03:03 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 7c47ae3e | 2009-02-18 00:34:21 | [diff] [blame] | 5 | #ifndef CHROME_BROWSER_PROCESS_SINGLETON_H_ |
| 6 | #define CHROME_BROWSER_PROCESS_SINGLETON_H_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 8 | |
[email protected] | 19d7e968 | 2009-02-18 22:04:28 | [diff] [blame] | 9 | #include "build/build_config.h" |
| 10 | |
| 11 | #if defined(OS_WIN) |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 12 | #include <windows.h> |
[email protected] | 0ff0ff3 | 2010-12-21 19:34:42 | [diff] [blame] | 13 | #endif // defined(OS_WIN) |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 14 | |
[email protected] | edf04b51 | 2012-02-23 09:47:43 | [diff] [blame^] | 15 | #include <set> |
| 16 | #include <vector> |
| 17 | |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 18 | #include "base/basictypes.h" |
[email protected] | edf04b51 | 2012-02-23 09:47:43 | [diff] [blame^] | 19 | #include "base/command_line.h" |
| 20 | #include "base/file_path.h" |
[email protected] | b674dc73 | 2009-05-20 20:41:00 | [diff] [blame] | 21 | #include "base/logging.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 22 | #include "base/memory/ref_counted.h" |
[email protected] | c917750 | 2011-01-01 04:48:49 | [diff] [blame] | 23 | #include "base/threading/non_thread_safe.h" |
[email protected] | 08397d5 | 2011-02-05 01:53:38 | [diff] [blame] | 24 | #include "ui/gfx/native_widget_types.h" |
[email protected] | 0ff0ff3 | 2010-12-21 19:34:42 | [diff] [blame] | 25 | |
| 26 | #if defined(OS_POSIX) |
| 27 | #include "base/file_path.h" |
| 28 | #endif // defined(OS_POSIX) |
| 29 | |
[email protected] | 1d380bc0 | 2011-10-26 16:09:33 | [diff] [blame] | 30 | #if defined(OS_LINUX) || defined(OS_OPENBSD) |
[email protected] | e078590 | 2011-05-19 23:34:17 | [diff] [blame] | 31 | #include "base/scoped_temp_dir.h" |
[email protected] | 1d380bc0 | 2011-10-26 16:09:33 | [diff] [blame] | 32 | #endif // defined(OS_LINUX) || defined(OS_OPENBSD) |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 33 | |
[email protected] | 0189bbd | 2009-10-12 22:50:39 | [diff] [blame] | 34 | class CommandLine; |
[email protected] | 864b136 | 2010-08-19 03:49:38 | [diff] [blame] | 35 | class FilePath; |
[email protected] | 0189bbd | 2009-10-12 22:50:39 | [diff] [blame] | 36 | |
[email protected] | 7c47ae3e | 2009-02-18 00:34:21 | [diff] [blame] | 37 | // ProcessSingleton ---------------------------------------------------------- |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 38 | // |
[email protected] | 7c47ae3e | 2009-02-18 00:34:21 | [diff] [blame] | 39 | // This class allows different browser processes to communicate with |
| 40 | // each other. It is named according to the user data directory, so |
| 41 | // we can be sure that no more than one copy of the application can be |
| 42 | // running at once with a given data directory. |
| 43 | // |
[email protected] | 19d7e968 | 2009-02-18 22:04:28 | [diff] [blame] | 44 | // Implementation notes: |
| 45 | // - the Windows implementation uses an invisible global message window; |
[email protected] | e134a72 | 2009-02-23 23:54:02 | [diff] [blame] | 46 | // - the Linux implementation uses a Unix domain socket in the user data dir. |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 47 | |
[email protected] | c917750 | 2011-01-01 04:48:49 | [diff] [blame] | 48 | class ProcessSingleton : public base::NonThreadSafe { |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 49 | public: |
[email protected] | 9f20a6d0 | 2009-08-21 01:18:37 | [diff] [blame] | 50 | enum NotifyResult { |
| 51 | PROCESS_NONE, |
| 52 | PROCESS_NOTIFIED, |
| 53 | PROFILE_IN_USE, |
[email protected] | 4a44bc3 | 2010-05-28 22:22:44 | [diff] [blame] | 54 | LOCK_ERROR, |
[email protected] | 9f20a6d0 | 2009-08-21 01:18:37 | [diff] [blame] | 55 | }; |
| 56 | |
[email protected] | 7c47ae3e | 2009-02-18 00:34:21 | [diff] [blame] | 57 | explicit ProcessSingleton(const FilePath& user_data_dir); |
| 58 | ~ProcessSingleton(); |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 59 | |
[email protected] | c0d29795 | 2009-09-17 21:00:18 | [diff] [blame] | 60 | // Notify another process, if available. |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 61 | // Returns true if another process was found and notified, false if we |
[email protected] | 19d7e968 | 2009-02-18 22:04:28 | [diff] [blame] | 62 | // should continue with this process. |
| 63 | // Windows code roughly based on Mozilla. |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 64 | // |
| 65 | // TODO(brettw): this will not handle all cases. If two process start up too |
[email protected] | 19d7e968 | 2009-02-18 22:04:28 | [diff] [blame] | 66 | // close to each other, the Create() might not yet have happened for the |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 67 | // first one, so this function won't find it. |
[email protected] | 9f20a6d0 | 2009-08-21 01:18:37 | [diff] [blame] | 68 | NotifyResult NotifyOtherProcess(); |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 69 | |
[email protected] | 4a44bc3 | 2010-05-28 22:22:44 | [diff] [blame] | 70 | // Notify another process, if available. Otherwise sets ourselves as the |
| 71 | // singleton instance. Returns PROCESS_NONE if we became the singleton |
| 72 | // instance. |
| 73 | NotifyResult NotifyOtherProcessOrCreate(); |
| 74 | |
[email protected] | e60c023 | 2011-11-11 19:56:35 | [diff] [blame] | 75 | #if defined(OS_LINUX) || defined(OS_OPENBSD) |
[email protected] | c0d29795 | 2009-09-17 21:00:18 | [diff] [blame] | 76 | // Exposed for testing. We use a timeout on Linux, and in tests we want |
| 77 | // this timeout to be short. |
[email protected] | 0189bbd | 2009-10-12 22:50:39 | [diff] [blame] | 78 | NotifyResult NotifyOtherProcessWithTimeout(const CommandLine& command_line, |
[email protected] | 4a44bc3 | 2010-05-28 22:22:44 | [diff] [blame] | 79 | int timeout_seconds, |
| 80 | bool kill_unresponsive); |
| 81 | NotifyResult NotifyOtherProcessWithTimeoutOrCreate( |
| 82 | const CommandLine& command_line, |
| 83 | int timeout_seconds); |
[email protected] | e60c023 | 2011-11-11 19:56:35 | [diff] [blame] | 84 | #endif // defined(OS_LINUX) || defined(OS_OPENBSD) |
[email protected] | c0d29795 | 2009-09-17 21:00:18 | [diff] [blame] | 85 | |
[email protected] | e5b307f | 2011-10-06 22:55:28 | [diff] [blame] | 86 | #if defined(OS_WIN) && !defined(USE_AURA) |
[email protected] | 54430e2 | 2011-03-08 00:39:29 | [diff] [blame] | 87 | // 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 |
| 89 | // function. Instead consider using NotifyOtherProcessOrCreate(). |
[email protected] | 81585672 | 2011-04-13 17:19:19 | [diff] [blame] | 90 | // For non profile-specific method, use |
[email protected] | a3abd557 | 2011-04-15 02:09:33 | [diff] [blame] | 91 | // browser_util::IsBrowserAlreadyRunning(). |
[email protected] | 54430e2 | 2011-03-08 00:39:29 | [diff] [blame] | 92 | bool FoundOtherProcessWindow() const { |
| 93 | return (NULL != remote_window_); |
| 94 | } |
| 95 | #endif // defined(OS_WIN) |
| 96 | |
[email protected] | 4dd4224 | 2010-04-07 02:21:15 | [diff] [blame] | 97 | // 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 |
| 99 | // exit. |
| 100 | bool Create(); |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 101 | |
[email protected] | 9f20a6d0 | 2009-08-21 01:18:37 | [diff] [blame] | 102 | // Clear any lock state during shutdown. |
| 103 | void Cleanup(); |
| 104 | |
[email protected] | 175a7a2 | 2009-05-03 15:57:53 | [diff] [blame] | 105 | // Blocks the dispatch of CopyData messages. foreground_window refers |
| 106 | // to the window that should be set to the foreground if a CopyData message |
| 107 | // is received while the ProcessSingleton is locked. |
| 108 | void Lock(gfx::NativeWindow foreground_window) { |
[email protected] | b674dc73 | 2009-05-20 20:41:00 | [diff] [blame] | 109 | DCHECK(CalledOnValidThread()); |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 110 | locked_ = true; |
[email protected] | 175a7a2 | 2009-05-03 15:57:53 | [diff] [blame] | 111 | foreground_window_ = foreground_window; |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 112 | } |
| 113 | |
[email protected] | edf04b51 | 2012-02-23 09:47:43 | [diff] [blame^] | 114 | // Changes the foreground window without changing the locked state. |
| 115 | void SetForegroundWindow(gfx::NativeWindow foreground_window) { |
[email protected] | b674dc73 | 2009-05-20 20:41:00 | [diff] [blame] | 116 | DCHECK(CalledOnValidThread()); |
[email protected] | edf04b51 | 2012-02-23 09:47:43 | [diff] [blame^] | 117 | foreground_window_ = foreground_window; |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 118 | } |
| 119 | |
[email protected] | edf04b51 | 2012-02-23 09:47:43 | [diff] [blame^] | 120 | // Allows the dispatch of CopyData messages and replays the messages which |
| 121 | // were received when the ProcessSingleton was locked. |
| 122 | void Unlock(); |
| 123 | |
[email protected] | b674dc73 | 2009-05-20 20:41:00 | [diff] [blame] | 124 | bool locked() { |
| 125 | DCHECK(CalledOnValidThread()); |
| 126 | return locked_; |
| 127 | } |
| 128 | |
[email protected] | e3ce40a | 2012-01-31 03:03:03 | [diff] [blame] | 129 | #if defined(OS_WIN) |
| 130 | LRESULT WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam); |
| 131 | #endif |
| 132 | |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 133 | private: |
[email protected] | edf04b51 | 2012-02-23 09:47:43 | [diff] [blame^] | 134 | typedef std::pair<CommandLine::StringVector, FilePath> DelayedStartupMessage; |
| 135 | void ProcessCommandLine(const CommandLine& command_line, |
| 136 | const FilePath& current_directory); |
| 137 | |
[email protected] | 753efc4 | 2010-03-09 19:52:16 | [diff] [blame] | 138 | #if !defined(OS_MACOSX) |
[email protected] | 8b08cbd | 2009-08-04 05:34:19 | [diff] [blame] | 139 | // Timeout for the current browser process to respond. 20 seconds should be |
| 140 | // enough. It's only used in Windows and Linux implementations. |
| 141 | static const int kTimeoutInSeconds = 20; |
| 142 | #endif |
| 143 | |
[email protected] | 19d7e968 | 2009-02-18 22:04:28 | [diff] [blame] | 144 | bool locked_; |
[email protected] | 175a7a2 | 2009-05-03 15:57:53 | [diff] [blame] | 145 | gfx::NativeWindow foreground_window_; |
[email protected] | 19d7e968 | 2009-02-18 22:04:28 | [diff] [blame] | 146 | |
[email protected] | 95259c6 | 2011-10-25 23:23:53 | [diff] [blame] | 147 | #if defined(OS_WIN) |
[email protected] | 19d7e968 | 2009-02-18 22:04:28 | [diff] [blame] | 148 | // This ugly behemoth handles startup commands sent from another process. |
| 149 | LRESULT OnCopyData(HWND hwnd, const COPYDATASTRUCT* cds); |
| 150 | |
[email protected] | 0a19455 | 2011-09-14 17:53:35 | [diff] [blame] | 151 | bool EscapeVirtualization(const FilePath& user_data_dir); |
| 152 | |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 153 | HWND remote_window_; // The HWND_MESSAGE of another browser. |
| 154 | HWND window_; // The HWND_MESSAGE window. |
[email protected] | 0a19455 | 2011-09-14 17:53:35 | [diff] [blame] | 155 | bool is_virtualized_; // Stuck inside Microsoft Softricity VM environment. |
[email protected] | 1d380bc0 | 2011-10-26 16:09:33 | [diff] [blame] | 156 | #elif defined(OS_LINUX) || defined(OS_OPENBSD) |
[email protected] | 19d7e968 | 2009-02-18 22:04:28 | [diff] [blame] | 157 | // Path in file system to the socket. |
| 158 | FilePath socket_path_; |
[email protected] | b674dc73 | 2009-05-20 20:41:00 | [diff] [blame] | 159 | |
[email protected] | 9f20a6d0 | 2009-08-21 01:18:37 | [diff] [blame] | 160 | // Path in file system to the lock. |
| 161 | FilePath lock_path_; |
| 162 | |
[email protected] | 53f4826c | 2010-08-27 01:29:28 | [diff] [blame] | 163 | // Path in file system to the cookie file. |
| 164 | FilePath cookie_path_; |
| 165 | |
| 166 | // Temporary directory to hold the socket. |
| 167 | ScopedTempDir socket_dir_; |
| 168 | |
[email protected] | b674dc73 | 2009-05-20 20:41:00 | [diff] [blame] | 169 | // Helper class for linux specific messages. LinuxWatcher is ref counted |
| 170 | // because it posts messages between threads. |
| 171 | class LinuxWatcher; |
| 172 | scoped_refptr<LinuxWatcher> watcher_; |
[email protected] | b2841b07 | 2010-10-14 22:20:32 | [diff] [blame] | 173 | #elif defined(OS_MACOSX) |
| 174 | // Path in file system to the lock. |
| 175 | FilePath lock_path_; |
| 176 | |
| 177 | // File descriptor associated with the lockfile, valid between |
| 178 | // |Create()| and |Cleanup()|. Two instances cannot have a lock on |
| 179 | // the same file at the same time. |
| 180 | int lock_fd_; |
[email protected] | 19d7e968 | 2009-02-18 22:04:28 | [diff] [blame] | 181 | #endif |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 182 | |
[email protected] | edf04b51 | 2012-02-23 09:47:43 | [diff] [blame^] | 183 | // If messages are received in the locked state, the corresponding command |
| 184 | // lines are saved here to be replayed later. |
| 185 | std::vector<DelayedStartupMessage> saved_startup_messages_; |
| 186 | |
[email protected] | 7c47ae3e | 2009-02-18 00:34:21 | [diff] [blame] | 187 | DISALLOW_COPY_AND_ASSIGN(ProcessSingleton); |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 188 | }; |
| 189 | |
[email protected] | 175a7a2 | 2009-05-03 15:57:53 | [diff] [blame] | 190 | #endif // CHROME_BROWSER_PROCESS_SINGLETON_H_ |