[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 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 |
| 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] | 19d7e968 | 2009-02-18 22:04:28 | [diff] [blame] | 13 | #endif |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 14 | |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 15 | #include "base/basictypes.h" |
[email protected] | f7011fcb | 2009-01-28 21:54:32 | [diff] [blame] | 16 | #include "base/file_path.h" |
[email protected] | b674dc73 | 2009-05-20 20:41:00 | [diff] [blame] | 17 | #include "base/logging.h" |
| 18 | #include "base/non_thread_safe.h" |
| 19 | #include "base/ref_counted.h" |
[email protected] | 5c7293a | 2010-03-17 06:40:57 | [diff] [blame] | 20 | #include "gfx/native_widget_types.h" |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 21 | |
[email protected] | 0189bbd | 2009-10-12 22:50:39 | [diff] [blame] | 22 | class CommandLine; |
| 23 | |
[email protected] | 7c47ae3e | 2009-02-18 00:34:21 | [diff] [blame] | 24 | // ProcessSingleton ---------------------------------------------------------- |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 25 | // |
[email protected] | 7c47ae3e | 2009-02-18 00:34:21 | [diff] [blame] | 26 | // This class allows different browser processes to communicate with |
| 27 | // each other. It is named according to the user data directory, so |
| 28 | // we can be sure that no more than one copy of the application can be |
| 29 | // running at once with a given data directory. |
| 30 | // |
[email protected] | 19d7e968 | 2009-02-18 22:04:28 | [diff] [blame] | 31 | // Implementation notes: |
| 32 | // - the Windows implementation uses an invisible global message window; |
[email protected] | e134a72 | 2009-02-23 23:54:02 | [diff] [blame] | 33 | // - the Linux implementation uses a Unix domain socket in the user data dir. |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 34 | |
[email protected] | b674dc73 | 2009-05-20 20:41:00 | [diff] [blame] | 35 | class ProcessSingleton : public NonThreadSafe { |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 36 | public: |
[email protected] | 9f20a6d0 | 2009-08-21 01:18:37 | [diff] [blame] | 37 | enum NotifyResult { |
| 38 | PROCESS_NONE, |
| 39 | PROCESS_NOTIFIED, |
| 40 | PROFILE_IN_USE, |
[email protected] | 4a44bc3 | 2010-05-28 22:22:44 | [diff] [blame] | 41 | LOCK_ERROR, |
[email protected] | 9f20a6d0 | 2009-08-21 01:18:37 | [diff] [blame] | 42 | }; |
| 43 | |
[email protected] | 7c47ae3e | 2009-02-18 00:34:21 | [diff] [blame] | 44 | explicit ProcessSingleton(const FilePath& user_data_dir); |
| 45 | ~ProcessSingleton(); |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 46 | |
[email protected] | c0d29795 | 2009-09-17 21:00:18 | [diff] [blame] | 47 | // Notify another process, if available. |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 48 | // Returns true if another process was found and notified, false if we |
[email protected] | 19d7e968 | 2009-02-18 22:04:28 | [diff] [blame] | 49 | // should continue with this process. |
| 50 | // Windows code roughly based on Mozilla. |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 51 | // |
| 52 | // 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] | 53 | // close to each other, the Create() might not yet have happened for the |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 54 | // first one, so this function won't find it. |
[email protected] | 9f20a6d0 | 2009-08-21 01:18:37 | [diff] [blame] | 55 | NotifyResult NotifyOtherProcess(); |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 56 | |
[email protected] | 4a44bc3 | 2010-05-28 22:22:44 | [diff] [blame] | 57 | // Notify another process, if available. Otherwise sets ourselves as the |
| 58 | // singleton instance. Returns PROCESS_NONE if we became the singleton |
| 59 | // instance. |
| 60 | NotifyResult NotifyOtherProcessOrCreate(); |
| 61 | |
[email protected] | 753efc4 | 2010-03-09 19:52:16 | [diff] [blame] | 62 | #if defined(OS_POSIX) && !defined(OS_MACOSX) |
[email protected] | c0d29795 | 2009-09-17 21:00:18 | [diff] [blame] | 63 | // Exposed for testing. We use a timeout on Linux, and in tests we want |
| 64 | // this timeout to be short. |
[email protected] | 0189bbd | 2009-10-12 22:50:39 | [diff] [blame] | 65 | NotifyResult NotifyOtherProcessWithTimeout(const CommandLine& command_line, |
[email protected] | 4a44bc3 | 2010-05-28 22:22:44 | [diff] [blame] | 66 | int timeout_seconds, |
| 67 | bool kill_unresponsive); |
| 68 | NotifyResult NotifyOtherProcessWithTimeoutOrCreate( |
| 69 | const CommandLine& command_line, |
| 70 | int timeout_seconds); |
[email protected] | c0d29795 | 2009-09-17 21:00:18 | [diff] [blame] | 71 | #endif |
| 72 | |
[email protected] | 4dd4224 | 2010-04-07 02:21:15 | [diff] [blame] | 73 | // Sets ourself up as the singleton instance. Returns true on success. If |
| 74 | // false is returned, we are not the singleton instance and the caller must |
| 75 | // exit. |
| 76 | bool Create(); |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 77 | |
[email protected] | 9f20a6d0 | 2009-08-21 01:18:37 | [diff] [blame] | 78 | // Clear any lock state during shutdown. |
| 79 | void Cleanup(); |
| 80 | |
[email protected] | 175a7a2 | 2009-05-03 15:57:53 | [diff] [blame] | 81 | // Blocks the dispatch of CopyData messages. foreground_window refers |
| 82 | // to the window that should be set to the foreground if a CopyData message |
| 83 | // is received while the ProcessSingleton is locked. |
| 84 | void Lock(gfx::NativeWindow foreground_window) { |
[email protected] | b674dc73 | 2009-05-20 20:41:00 | [diff] [blame] | 85 | DCHECK(CalledOnValidThread()); |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 86 | locked_ = true; |
[email protected] | 175a7a2 | 2009-05-03 15:57:53 | [diff] [blame] | 87 | foreground_window_ = foreground_window; |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | // Allows the dispatch of CopyData messages. |
| 91 | void Unlock() { |
[email protected] | b674dc73 | 2009-05-20 20:41:00 | [diff] [blame] | 92 | DCHECK(CalledOnValidThread()); |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 93 | locked_ = false; |
[email protected] | 175a7a2 | 2009-05-03 15:57:53 | [diff] [blame] | 94 | foreground_window_ = NULL; |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 95 | } |
| 96 | |
[email protected] | b674dc73 | 2009-05-20 20:41:00 | [diff] [blame] | 97 | bool locked() { |
| 98 | DCHECK(CalledOnValidThread()); |
| 99 | return locked_; |
| 100 | } |
| 101 | |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 102 | private: |
[email protected] | 753efc4 | 2010-03-09 19:52:16 | [diff] [blame] | 103 | #if !defined(OS_MACOSX) |
[email protected] | 8b08cbd | 2009-08-04 05:34:19 | [diff] [blame] | 104 | // Timeout for the current browser process to respond. 20 seconds should be |
| 105 | // enough. It's only used in Windows and Linux implementations. |
| 106 | static const int kTimeoutInSeconds = 20; |
| 107 | #endif |
| 108 | |
[email protected] | 19d7e968 | 2009-02-18 22:04:28 | [diff] [blame] | 109 | bool locked_; |
[email protected] | 175a7a2 | 2009-05-03 15:57:53 | [diff] [blame] | 110 | gfx::NativeWindow foreground_window_; |
[email protected] | 19d7e968 | 2009-02-18 22:04:28 | [diff] [blame] | 111 | |
| 112 | #if defined(OS_WIN) |
| 113 | // This ugly behemoth handles startup commands sent from another process. |
| 114 | LRESULT OnCopyData(HWND hwnd, const COPYDATASTRUCT* cds); |
| 115 | |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 116 | LRESULT CALLBACK WndProc(HWND hwnd, |
| 117 | UINT message, |
| 118 | WPARAM wparam, |
| 119 | LPARAM lparam); |
| 120 | |
| 121 | static LRESULT CALLBACK WndProcStatic(HWND hwnd, |
| 122 | UINT message, |
| 123 | WPARAM wparam, |
| 124 | LPARAM lparam) { |
[email protected] | 7c47ae3e | 2009-02-18 00:34:21 | [diff] [blame] | 125 | ProcessSingleton* msg_wnd = reinterpret_cast<ProcessSingleton*>( |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 126 | GetWindowLongPtr(hwnd, GWLP_USERDATA)); |
| 127 | return msg_wnd->WndProc(hwnd, message, wparam, lparam); |
| 128 | } |
| 129 | |
| 130 | HWND remote_window_; // The HWND_MESSAGE of another browser. |
| 131 | HWND window_; // The HWND_MESSAGE window. |
[email protected] | 753efc4 | 2010-03-09 19:52:16 | [diff] [blame] | 132 | #elif !defined(OS_MACOSX) |
[email protected] | 19d7e968 | 2009-02-18 22:04:28 | [diff] [blame] | 133 | // Path in file system to the socket. |
| 134 | FilePath socket_path_; |
[email protected] | b674dc73 | 2009-05-20 20:41:00 | [diff] [blame] | 135 | |
[email protected] | 9f20a6d0 | 2009-08-21 01:18:37 | [diff] [blame] | 136 | // Path in file system to the lock. |
| 137 | FilePath lock_path_; |
| 138 | |
[email protected] | b674dc73 | 2009-05-20 20:41:00 | [diff] [blame] | 139 | // Helper class for linux specific messages. LinuxWatcher is ref counted |
| 140 | // because it posts messages between threads. |
| 141 | class LinuxWatcher; |
| 142 | scoped_refptr<LinuxWatcher> watcher_; |
[email protected] | 19d7e968 | 2009-02-18 22:04:28 | [diff] [blame] | 143 | #endif |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 144 | |
[email protected] | 7c47ae3e | 2009-02-18 00:34:21 | [diff] [blame] | 145 | DISALLOW_COPY_AND_ASSIGN(ProcessSingleton); |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 146 | }; |
| 147 | |
[email protected] | 175a7a2 | 2009-05-03 15:57:53 | [diff] [blame] | 148 | #endif // CHROME_BROWSER_PROCESS_SINGLETON_H_ |