blob: 2a7e9b04a5a1e03dd7e46d5c72b606dad3a21783 [file] [log] [blame]
[email protected]e3ce40a2012-01-31 03:03:031// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]fc14cef2009-01-27 22:17:292// 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]32b76ef2010-07-26 23:08:247#pragma once
[email protected]fc14cef2009-01-27 22:17:298
[email protected]19d7e9682009-02-18 22:04:289#include "build/build_config.h"
10
11#if defined(OS_WIN)
[email protected]fc14cef2009-01-27 22:17:2912#include <windows.h>
[email protected]0ff0ff32010-12-21 19:34:4213#endif // defined(OS_WIN)
[email protected]fc14cef2009-01-27 22:17:2914
[email protected]edf04b512012-02-23 09:47:4315#include <set>
16#include <vector>
17
[email protected]fc14cef2009-01-27 22:17:2918#include "base/basictypes.h"
[email protected]edf04b512012-02-23 09:47:4319#include "base/command_line.h"
20#include "base/file_path.h"
[email protected]b674dc732009-05-20 20:41:0021#include "base/logging.h"
[email protected]3b63f8f42011-03-28 01:54:1522#include "base/memory/ref_counted.h"
[email protected]c9177502011-01-01 04:48:4923#include "base/threading/non_thread_safe.h"
[email protected]08397d52011-02-05 01:53:3824#include "ui/gfx/native_widget_types.h"
[email protected]0ff0ff32010-12-21 19:34:4225
26#if defined(OS_POSIX)
27#include "base/file_path.h"
28#endif // defined(OS_POSIX)
29
[email protected]1d380bc02011-10-26 16:09:3330#if defined(OS_LINUX) || defined(OS_OPENBSD)
[email protected]e0785902011-05-19 23:34:1731#include "base/scoped_temp_dir.h"
[email protected]1d380bc02011-10-26 16:09:3332#endif // defined(OS_LINUX) || defined(OS_OPENBSD)
[email protected]fc14cef2009-01-27 22:17:2933
[email protected]0189bbd2009-10-12 22:50:3934class CommandLine;
[email protected]864b1362010-08-19 03:49:3835class FilePath;
[email protected]0189bbd2009-10-12 22:50:3936
[email protected]7c47ae3e2009-02-18 00:34:2137// ProcessSingleton ----------------------------------------------------------
[email protected]fc14cef2009-01-27 22:17:2938//
[email protected]7c47ae3e2009-02-18 00:34:2139// 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]19d7e9682009-02-18 22:04:2844// Implementation notes:
45// - the Windows implementation uses an invisible global message window;
[email protected]e134a722009-02-23 23:54:0246// - the Linux implementation uses a Unix domain socket in the user data dir.
[email protected]fc14cef2009-01-27 22:17:2947
[email protected]c9177502011-01-01 04:48:4948class ProcessSingleton : public base::NonThreadSafe {
[email protected]fc14cef2009-01-27 22:17:2949 public:
[email protected]9f20a6d02009-08-21 01:18:3750 enum NotifyResult {
51 PROCESS_NONE,
52 PROCESS_NOTIFIED,
53 PROFILE_IN_USE,
[email protected]4a44bc32010-05-28 22:22:4454 LOCK_ERROR,
[email protected]9f20a6d02009-08-21 01:18:3755 };
56
[email protected]7c47ae3e2009-02-18 00:34:2157 explicit ProcessSingleton(const FilePath& user_data_dir);
58 ~ProcessSingleton();
[email protected]fc14cef2009-01-27 22:17:2959
[email protected]c0d297952009-09-17 21:00:1860 // Notify another process, if available.
[email protected]fc14cef2009-01-27 22:17:2961 // Returns true if another process was found and notified, false if we
[email protected]19d7e9682009-02-18 22:04:2862 // should continue with this process.
63 // Windows code roughly based on Mozilla.
[email protected]fc14cef2009-01-27 22:17:2964 //
65 // TODO(brettw): this will not handle all cases. If two process start up too
[email protected]19d7e9682009-02-18 22:04:2866 // close to each other, the Create() might not yet have happened for the
[email protected]fc14cef2009-01-27 22:17:2967 // first one, so this function won't find it.
[email protected]9f20a6d02009-08-21 01:18:3768 NotifyResult NotifyOtherProcess();
[email protected]fc14cef2009-01-27 22:17:2969
[email protected]4a44bc32010-05-28 22:22:4470 // 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]e60c0232011-11-11 19:56:3575#if defined(OS_LINUX) || defined(OS_OPENBSD)
[email protected]c0d297952009-09-17 21:00:1876 // Exposed for testing. We use a timeout on Linux, and in tests we want
77 // this timeout to be short.
[email protected]0189bbd2009-10-12 22:50:3978 NotifyResult NotifyOtherProcessWithTimeout(const CommandLine& command_line,
[email protected]4a44bc32010-05-28 22:22:4479 int timeout_seconds,
80 bool kill_unresponsive);
81 NotifyResult NotifyOtherProcessWithTimeoutOrCreate(
82 const CommandLine& command_line,
83 int timeout_seconds);
[email protected]e60c0232011-11-11 19:56:3584#endif // defined(OS_LINUX) || defined(OS_OPENBSD)
[email protected]c0d297952009-09-17 21:00:1885
[email protected]e5b307f2011-10-06 22:55:2886#if defined(OS_WIN) && !defined(USE_AURA)
[email protected]54430e22011-03-08 00:39:2987 // 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]815856722011-04-13 17:19:1990 // For non profile-specific method, use
[email protected]a3abd5572011-04-15 02:09:3391 // browser_util::IsBrowserAlreadyRunning().
[email protected]54430e22011-03-08 00:39:2992 bool FoundOtherProcessWindow() const {
93 return (NULL != remote_window_);
94 }
95#endif // defined(OS_WIN)
96
[email protected]4dd42242010-04-07 02:21:1597 // 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]fc14cef2009-01-27 22:17:29101
[email protected]9f20a6d02009-08-21 01:18:37102 // Clear any lock state during shutdown.
103 void Cleanup();
104
[email protected]175a7a22009-05-03 15:57:53105 // 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]b674dc732009-05-20 20:41:00109 DCHECK(CalledOnValidThread());
[email protected]fc14cef2009-01-27 22:17:29110 locked_ = true;
[email protected]175a7a22009-05-03 15:57:53111 foreground_window_ = foreground_window;
[email protected]fc14cef2009-01-27 22:17:29112 }
113
[email protected]edf04b512012-02-23 09:47:43114 // Changes the foreground window without changing the locked state.
115 void SetForegroundWindow(gfx::NativeWindow foreground_window) {
[email protected]b674dc732009-05-20 20:41:00116 DCHECK(CalledOnValidThread());
[email protected]edf04b512012-02-23 09:47:43117 foreground_window_ = foreground_window;
[email protected]fc14cef2009-01-27 22:17:29118 }
119
[email protected]edf04b512012-02-23 09:47:43120 // 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]b674dc732009-05-20 20:41:00124 bool locked() {
125 DCHECK(CalledOnValidThread());
126 return locked_;
127 }
128
[email protected]e3ce40a2012-01-31 03:03:03129#if defined(OS_WIN)
130 LRESULT WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam);
131#endif
132
[email protected]fc14cef2009-01-27 22:17:29133 private:
[email protected]edf04b512012-02-23 09:47:43134 typedef std::pair<CommandLine::StringVector, FilePath> DelayedStartupMessage;
135 void ProcessCommandLine(const CommandLine& command_line,
136 const FilePath& current_directory);
137
[email protected]753efc42010-03-09 19:52:16138#if !defined(OS_MACOSX)
[email protected]8b08cbd2009-08-04 05:34:19139 // 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]19d7e9682009-02-18 22:04:28144 bool locked_;
[email protected]175a7a22009-05-03 15:57:53145 gfx::NativeWindow foreground_window_;
[email protected]19d7e9682009-02-18 22:04:28146
[email protected]95259c62011-10-25 23:23:53147#if defined(OS_WIN)
[email protected]19d7e9682009-02-18 22:04:28148 // This ugly behemoth handles startup commands sent from another process.
149 LRESULT OnCopyData(HWND hwnd, const COPYDATASTRUCT* cds);
150
[email protected]0a194552011-09-14 17:53:35151 bool EscapeVirtualization(const FilePath& user_data_dir);
152
[email protected]fc14cef2009-01-27 22:17:29153 HWND remote_window_; // The HWND_MESSAGE of another browser.
154 HWND window_; // The HWND_MESSAGE window.
[email protected]0a194552011-09-14 17:53:35155 bool is_virtualized_; // Stuck inside Microsoft Softricity VM environment.
[email protected]1d380bc02011-10-26 16:09:33156#elif defined(OS_LINUX) || defined(OS_OPENBSD)
[email protected]19d7e9682009-02-18 22:04:28157 // Path in file system to the socket.
158 FilePath socket_path_;
[email protected]b674dc732009-05-20 20:41:00159
[email protected]9f20a6d02009-08-21 01:18:37160 // Path in file system to the lock.
161 FilePath lock_path_;
162
[email protected]53f4826c2010-08-27 01:29:28163 // 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]b674dc732009-05-20 20:41:00169 // 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]b2841b072010-10-14 22:20:32173#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]19d7e9682009-02-18 22:04:28181#endif
[email protected]fc14cef2009-01-27 22:17:29182
[email protected]edf04b512012-02-23 09:47:43183 // 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]7c47ae3e2009-02-18 00:34:21187 DISALLOW_COPY_AND_ASSIGN(ProcessSingleton);
[email protected]fc14cef2009-01-27 22:17:29188};
189
[email protected]175a7a22009-05-03 15:57:53190#endif // CHROME_BROWSER_PROCESS_SINGLETON_H_