blob: 4ae0b1085007001d62cb1961bcc82e4a00c2cf70 [file] [log] [blame]
[email protected]fc14cef2009-01-27 22:17:291// 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]7c47ae3e2009-02-18 00:34:215#ifndef CHROME_BROWSER_PROCESS_SINGLETON_H_
6#define CHROME_BROWSER_PROCESS_SINGLETON_H_
[email protected]fc14cef2009-01-27 22:17:297
[email protected]19d7e9682009-02-18 22:04:288#include "build/build_config.h"
9
10#if defined(OS_WIN)
[email protected]fc14cef2009-01-27 22:17:2911#include <windows.h>
[email protected]19d7e9682009-02-18 22:04:2812#endif
[email protected]fc14cef2009-01-27 22:17:2913
[email protected]fc14cef2009-01-27 22:17:2914#include "base/basictypes.h"
[email protected]f7011fcb2009-01-28 21:54:3215#include "base/file_path.h"
[email protected]b674dc732009-05-20 20:41:0016#include "base/logging.h"
17#include "base/non_thread_safe.h"
18#include "base/ref_counted.h"
[email protected]5c7293a2010-03-17 06:40:5719#include "gfx/native_widget_types.h"
[email protected]fc14cef2009-01-27 22:17:2920
[email protected]0189bbd2009-10-12 22:50:3921class CommandLine;
22
[email protected]7c47ae3e2009-02-18 00:34:2123// ProcessSingleton ----------------------------------------------------------
[email protected]fc14cef2009-01-27 22:17:2924//
[email protected]7c47ae3e2009-02-18 00:34:2125// This class allows different browser processes to communicate with
26// each other. It is named according to the user data directory, so
27// we can be sure that no more than one copy of the application can be
28// running at once with a given data directory.
29//
[email protected]19d7e9682009-02-18 22:04:2830// Implementation notes:
31// - the Windows implementation uses an invisible global message window;
[email protected]e134a722009-02-23 23:54:0232// - the Linux implementation uses a Unix domain socket in the user data dir.
[email protected]fc14cef2009-01-27 22:17:2933
[email protected]b674dc732009-05-20 20:41:0034class ProcessSingleton : public NonThreadSafe {
[email protected]fc14cef2009-01-27 22:17:2935 public:
[email protected]9f20a6d02009-08-21 01:18:3736 enum NotifyResult {
37 PROCESS_NONE,
38 PROCESS_NOTIFIED,
39 PROFILE_IN_USE,
40 };
41
[email protected]7c47ae3e2009-02-18 00:34:2142 explicit ProcessSingleton(const FilePath& user_data_dir);
43 ~ProcessSingleton();
[email protected]fc14cef2009-01-27 22:17:2944
[email protected]c0d297952009-09-17 21:00:1845 // Notify another process, if available.
[email protected]fc14cef2009-01-27 22:17:2946 // Returns true if another process was found and notified, false if we
[email protected]19d7e9682009-02-18 22:04:2847 // should continue with this process.
48 // Windows code roughly based on Mozilla.
[email protected]fc14cef2009-01-27 22:17:2949 //
50 // TODO(brettw): this will not handle all cases. If two process start up too
[email protected]19d7e9682009-02-18 22:04:2851 // close to each other, the Create() might not yet have happened for the
[email protected]fc14cef2009-01-27 22:17:2952 // first one, so this function won't find it.
[email protected]9f20a6d02009-08-21 01:18:3753 NotifyResult NotifyOtherProcess();
[email protected]fc14cef2009-01-27 22:17:2954
[email protected]753efc42010-03-09 19:52:1655#if defined(OS_POSIX) && !defined(OS_MACOSX)
[email protected]c0d297952009-09-17 21:00:1856 // Exposed for testing. We use a timeout on Linux, and in tests we want
57 // this timeout to be short.
[email protected]0189bbd2009-10-12 22:50:3958 NotifyResult NotifyOtherProcessWithTimeout(const CommandLine& command_line,
59 int timeout_seconds);
[email protected]c0d297952009-09-17 21:00:1860#endif
61
[email protected]4dd42242010-04-07 02:21:1562 // Sets ourself up as the singleton instance. Returns true on success. If
63 // false is returned, we are not the singleton instance and the caller must
64 // exit.
65 bool Create();
[email protected]fc14cef2009-01-27 22:17:2966
[email protected]9f20a6d02009-08-21 01:18:3767 // Clear any lock state during shutdown.
68 void Cleanup();
69
[email protected]175a7a22009-05-03 15:57:5370 // Blocks the dispatch of CopyData messages. foreground_window refers
71 // to the window that should be set to the foreground if a CopyData message
72 // is received while the ProcessSingleton is locked.
73 void Lock(gfx::NativeWindow foreground_window) {
[email protected]b674dc732009-05-20 20:41:0074 DCHECK(CalledOnValidThread());
[email protected]fc14cef2009-01-27 22:17:2975 locked_ = true;
[email protected]175a7a22009-05-03 15:57:5376 foreground_window_ = foreground_window;
[email protected]fc14cef2009-01-27 22:17:2977 }
78
79 // Allows the dispatch of CopyData messages.
80 void Unlock() {
[email protected]b674dc732009-05-20 20:41:0081 DCHECK(CalledOnValidThread());
[email protected]fc14cef2009-01-27 22:17:2982 locked_ = false;
[email protected]175a7a22009-05-03 15:57:5383 foreground_window_ = NULL;
[email protected]fc14cef2009-01-27 22:17:2984 }
85
[email protected]b674dc732009-05-20 20:41:0086 bool locked() {
87 DCHECK(CalledOnValidThread());
88 return locked_;
89 }
90
[email protected]fc14cef2009-01-27 22:17:2991 private:
[email protected]753efc42010-03-09 19:52:1692#if !defined(OS_MACOSX)
[email protected]8b08cbd2009-08-04 05:34:1993 // Timeout for the current browser process to respond. 20 seconds should be
94 // enough. It's only used in Windows and Linux implementations.
95 static const int kTimeoutInSeconds = 20;
96#endif
97
[email protected]19d7e9682009-02-18 22:04:2898 bool locked_;
[email protected]175a7a22009-05-03 15:57:5399 gfx::NativeWindow foreground_window_;
[email protected]19d7e9682009-02-18 22:04:28100
101#if defined(OS_WIN)
102 // This ugly behemoth handles startup commands sent from another process.
103 LRESULT OnCopyData(HWND hwnd, const COPYDATASTRUCT* cds);
104
[email protected]fc14cef2009-01-27 22:17:29105 LRESULT CALLBACK WndProc(HWND hwnd,
106 UINT message,
107 WPARAM wparam,
108 LPARAM lparam);
109
110 static LRESULT CALLBACK WndProcStatic(HWND hwnd,
111 UINT message,
112 WPARAM wparam,
113 LPARAM lparam) {
[email protected]7c47ae3e2009-02-18 00:34:21114 ProcessSingleton* msg_wnd = reinterpret_cast<ProcessSingleton*>(
[email protected]fc14cef2009-01-27 22:17:29115 GetWindowLongPtr(hwnd, GWLP_USERDATA));
116 return msg_wnd->WndProc(hwnd, message, wparam, lparam);
117 }
118
119 HWND remote_window_; // The HWND_MESSAGE of another browser.
120 HWND window_; // The HWND_MESSAGE window.
[email protected]753efc42010-03-09 19:52:16121#elif !defined(OS_MACOSX)
[email protected]19d7e9682009-02-18 22:04:28122 // Path in file system to the socket.
123 FilePath socket_path_;
[email protected]b674dc732009-05-20 20:41:00124
[email protected]9f20a6d02009-08-21 01:18:37125 // Path in file system to the lock.
126 FilePath lock_path_;
127
[email protected]b674dc732009-05-20 20:41:00128 // Helper class for linux specific messages. LinuxWatcher is ref counted
129 // because it posts messages between threads.
130 class LinuxWatcher;
131 scoped_refptr<LinuxWatcher> watcher_;
[email protected]19d7e9682009-02-18 22:04:28132#endif
[email protected]fc14cef2009-01-27 22:17:29133
[email protected]7c47ae3e2009-02-18 00:34:21134 DISALLOW_COPY_AND_ASSIGN(ProcessSingleton);
[email protected]fc14cef2009-01-27 22:17:29135};
136
[email protected]175a7a22009-05-03 15:57:53137#endif // CHROME_BROWSER_PROCESS_SINGLETON_H_