1 // Copyright (c) 2011 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.
5 #ifndef CHROME_BROWSER_PROCESS_SINGLETON_H_
6 #define CHROME_BROWSER_PROCESS_SINGLETON_H_
9 #include "build/build_config.h"
13 #endif // defined(OS_WIN)
15 #include "base/basictypes.h"
16 #include "base/logging.h"
17 #include "base/memory/ref_counted.h"
18 #include "base/threading/non_thread_safe.h"
19 #include "ui/gfx/native_widget_types.h"
22 #include "base/file_path.h"
23 #endif // defined(OS_POSIX)
26 #include "base/scoped_temp_dir.h"
27 #endif // defined(USE_X11)
32 // ProcessSingleton ----------------------------------------------------------
34 // This class allows different browser processes to communicate with
35 // each other. It is named according to the user data directory, so
36 // we can be sure that no more than one copy of the application can be
37 // running at once with a given data directory.
39 // Implementation notes:
40 // - the Windows implementation uses an invisible global message window;
41 // - the Linux implementation uses a Unix domain socket in the user data dir.
43 class ProcessSingleton
: public base::NonThreadSafe
{
52 explicit ProcessSingleton(const FilePath
& user_data_dir
);
55 // Notify another process, if available.
56 // Returns true if another process was found and notified, false if we
57 // should continue with this process.
58 // Windows code roughly based on Mozilla.
60 // TODO(brettw): this will not handle all cases. If two process start up too
61 // close to each other, the Create() might not yet have happened for the
62 // first one, so this function won't find it.
63 NotifyResult
NotifyOtherProcess();
65 // Notify another process, if available. Otherwise sets ourselves as the
66 // singleton instance. Returns PROCESS_NONE if we became the singleton
68 NotifyResult
NotifyOtherProcessOrCreate();
71 // Exposed for testing. We use a timeout on Linux, and in tests we want
72 // this timeout to be short.
73 NotifyResult
NotifyOtherProcessWithTimeout(const CommandLine
& command_line
,
75 bool kill_unresponsive
);
76 NotifyResult
NotifyOtherProcessWithTimeoutOrCreate(
77 const CommandLine
& command_line
,
79 #endif // defined(OS_LINUX)
82 // Used in specific cases to let us know that there is an existing instance
83 // of Chrome running with this profile. In general, you should not use this
84 // function. Instead consider using NotifyOtherProcessOrCreate().
85 // For non profile-specific method, use
86 // browser_util::IsBrowserAlreadyRunning().
87 bool FoundOtherProcessWindow() const {
88 return (NULL
!= remote_window_
);
90 #endif // defined(OS_WIN)
92 // Sets ourself up as the singleton instance. Returns true on success. If
93 // false is returned, we are not the singleton instance and the caller must
97 // Clear any lock state during shutdown.
100 // Blocks the dispatch of CopyData messages. foreground_window refers
101 // to the window that should be set to the foreground if a CopyData message
102 // is received while the ProcessSingleton is locked.
103 void Lock(gfx::NativeWindow foreground_window
) {
104 DCHECK(CalledOnValidThread());
106 foreground_window_
= foreground_window
;
109 // Allows the dispatch of CopyData messages.
111 DCHECK(CalledOnValidThread());
113 foreground_window_
= NULL
;
117 DCHECK(CalledOnValidThread());
122 #if !defined(OS_MACOSX)
123 // Timeout for the current browser process to respond. 20 seconds should be
124 // enough. It's only used in Windows and Linux implementations.
125 static const int kTimeoutInSeconds
= 20;
129 gfx::NativeWindow foreground_window_
;
132 // This ugly behemoth handles startup commands sent from another process.
133 LRESULT
OnCopyData(HWND hwnd
, const COPYDATASTRUCT
* cds
);
135 LRESULT CALLBACK
WndProc(HWND hwnd
,
140 static LRESULT CALLBACK
WndProcStatic(HWND hwnd
,
144 ProcessSingleton
* msg_wnd
= reinterpret_cast<ProcessSingleton
*>(
145 GetWindowLongPtr(hwnd
, GWLP_USERDATA
));
146 return msg_wnd
->WndProc(hwnd
, message
, wparam
, lparam
);
149 HWND remote_window_
; // The HWND_MESSAGE of another browser.
150 HWND window_
; // The HWND_MESSAGE window.
151 #elif defined(USE_X11)
152 // Path in file system to the socket.
153 FilePath socket_path_
;
155 // Path in file system to the lock.
158 // Path in file system to the cookie file.
159 FilePath cookie_path_
;
161 // Temporary directory to hold the socket.
162 ScopedTempDir socket_dir_
;
164 // Helper class for linux specific messages. LinuxWatcher is ref counted
165 // because it posts messages between threads.
167 scoped_refptr
<LinuxWatcher
> watcher_
;
168 #elif defined(OS_MACOSX)
169 // Path in file system to the lock.
172 // File descriptor associated with the lockfile, valid between
173 // |Create()| and |Cleanup()|. Two instances cannot have a lock on
174 // the same file at the same time.
178 DISALLOW_COPY_AND_ASSIGN(ProcessSingleton
);
181 #endif // CHROME_BROWSER_PROCESS_SINGLETON_H_