summaryrefslogtreecommitdiffstats
path: root/chrome/browser/process_singleton.h
blob: 76f5a49f6bf175baffaac4db5d3a01c100c17b03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_PROCESS_SINGLETON_H_
#define CHROME_BROWSER_PROCESS_SINGLETON_H_

#include "build/build_config.h"

#if defined(OS_WIN)
#include <windows.h>
#endif  // defined(OS_WIN)

#include <set>
#include <vector>

#include "base/basictypes.h"
#include "base/callback.h"
#include "base/command_line.h"
#include "base/file_path.h"
#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "base/process.h"
#include "base/threading/non_thread_safe.h"
#include "ui/gfx/native_widget_types.h"

#if defined(OS_POSIX)
#include "base/file_path.h"
#endif  // defined(OS_POSIX)

#if defined(OS_LINUX) || defined(OS_OPENBSD)
#include "base/scoped_temp_dir.h"
#endif  // defined(OS_LINUX) || defined(OS_OPENBSD)

class CommandLine;
class FilePath;

// ProcessSingleton ----------------------------------------------------------
//
// This class allows different browser processes to communicate with
// each other.  It is named according to the user data directory, so
// we can be sure that no more than one copy of the application can be
// running at once with a given data directory.
//
// Implementation notes:
// - the Windows implementation uses an invisible global message window;
// - the Linux implementation uses a Unix domain socket in the user data dir.

class ProcessSingleton : public base::NonThreadSafe {
 public:
  enum NotifyResult {
    PROCESS_NONE,
    PROCESS_NOTIFIED,
    PROFILE_IN_USE,
    LOCK_ERROR,
  };

  // Implement this callback to handle notifications from other processes. The
  // callback will receive the command line and directory with which the other
  // Chrome process was launched. Return true if the command line will be
  // handled within the current browser instance or false if the remote process
  // should handle it (i.e., because the current process is shutting down).
  typedef base::Callback<
      bool(const CommandLine& command_line, const FilePath& current_directory)>
      NotificationCallback;

  explicit ProcessSingleton(const FilePath& user_data_dir);
  ~ProcessSingleton();

  // Notify another process, if available.
  // Returns true if another process was found and notified, false if we
  // should continue with this process.
  // Windows code roughly based on Mozilla.
  //
  // TODO(brettw): this will not handle all cases. If two process start up too
  // close to each other, the Create() might not yet have happened for the
  // first one, so this function won't find it.
  NotifyResult NotifyOtherProcess();

  // Notify another process, if available. Otherwise sets ourselves as the
  // singleton instance and stores the provided callback for notification from
  // future processes. Returns PROCESS_NONE if we became the singleton
  // instance.
  NotifyResult NotifyOtherProcessOrCreate(
      const NotificationCallback& notification_callback);

#if defined(OS_LINUX) || defined(OS_OPENBSD)
  // Exposed for testing.  We use a timeout on Linux, and in tests we want
  // this timeout to be short.
  NotifyResult NotifyOtherProcessWithTimeout(const CommandLine& command_line,
                                             int timeout_seconds,
                                             bool kill_unresponsive);
  NotifyResult NotifyOtherProcessWithTimeoutOrCreate(
      const CommandLine& command_line,
      const NotificationCallback& notification_callback,
      int timeout_seconds);
  void OverrideCurrentPidForTesting(base::ProcessId pid);
  void OverrideKillCallbackForTesting(const base::Callback<void(int)>& callback);
  static void DisablePromptForTesting();
#endif  // defined(OS_LINUX) || defined(OS_OPENBSD)

#if defined(OS_WIN) && !defined(USE_AURA)
  // Used in specific cases to let us know that there is an existing instance
  // of Chrome running with this profile. In general, you should not use this
  // function. Instead consider using NotifyOtherProcessOrCreate().
  // For non profile-specific method, use
  // browser_util::IsBrowserAlreadyRunning().
  bool FoundOtherProcessWindow() const {
      return (NULL != remote_window_);
  }
#endif  // defined(OS_WIN)

  // Sets ourself up as the singleton instance.  Returns true on success.  If
  // false is returned, we are not the singleton instance and the caller must
  // exit. Otherwise, stores the provided callback for notification from
  // future processes.
  bool Create(
      const NotificationCallback& notification_callback);

  // Clear any lock state during shutdown.
  void Cleanup();

  // Blocks the dispatch of CopyData messages. foreground_window refers
  // to the window that should be set to the foreground if a CopyData message
  // is received while the ProcessSingleton is locked.
  void Lock(gfx::NativeWindow foreground_window) {
    DCHECK(CalledOnValidThread());
    locked_ = true;
    foreground_window_ = foreground_window;
  }

  // Changes the foreground window without changing the locked state.
  void SetForegroundWindow(gfx::NativeWindow foreground_window) {
    DCHECK(CalledOnValidThread());
    foreground_window_ = foreground_window;
  }

  // Allows the dispatch of CopyData messages and replays the messages which
  // were received when the ProcessSingleton was locked.
  void Unlock();

  bool locked() {
    DCHECK(CalledOnValidThread());
    return locked_;
  }

#if defined(OS_WIN)
  LRESULT WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam);
#endif

 private:
  typedef std::pair<CommandLine::StringVector, FilePath> DelayedStartupMessage;

#if !defined(OS_MACOSX)
  // Timeout for the current browser process to respond. 20 seconds should be
  // enough. It's only used in Windows and Linux implementations.
  static const int kTimeoutInSeconds = 20;
#endif

  bool locked_;
  gfx::NativeWindow foreground_window_;
  NotificationCallback notification_callback_;  // Handler for notifications.

#if defined(OS_WIN)
  // This ugly behemoth handles startup commands sent from another process.
  LRESULT OnCopyData(HWND hwnd, const COPYDATASTRUCT* cds);

  bool EscapeVirtualization(const FilePath& user_data_dir);

  HWND remote_window_;  // The HWND_MESSAGE of another browser.
  HWND window_;  // The HWND_MESSAGE window.
  bool is_virtualized_;  // Stuck inside Microsoft Softricity VM environment.
  HANDLE lock_file_;
#elif defined(OS_LINUX) || defined(OS_OPENBSD)
  // Return true if the given pid is one of our child processes.
  // Assumes that the current pid is the root of all pids of the current
  // instance.
  bool IsSameChromeInstance(pid_t pid);

  // Extract the process's pid from a symbol link path and if it is on
  // the same host, kill the process, unlink the lock file and return true.
  // If the process is part of the same chrome instance, unlink the lock file
  // and return true without killing it.
  // If the process is on a different host, return false.
  bool KillProcessByLockPath();

  // Default function to kill a process, overridable by tests.
  void KillProcess(int pid);

  // Allow overriding for tests.
  base::ProcessId current_pid_;

  // Function to call when the other process is hung and needs to be killed.
  // Allows overriding for tests.
  base::Callback<void(int)> kill_callback_;

  // Path in file system to the socket.
  FilePath socket_path_;

  // Path in file system to the lock.
  FilePath lock_path_;

  // Path in file system to the cookie file.
  FilePath cookie_path_;

  // Temporary directory to hold the socket.
  ScopedTempDir socket_dir_;

  // Helper class for linux specific messages.  LinuxWatcher is ref counted
  // because it posts messages between threads.
  class LinuxWatcher;
  scoped_refptr<LinuxWatcher> watcher_;
#elif defined(OS_MACOSX)
  // Path in file system to the lock.
  FilePath lock_path_;

  // File descriptor associated with the lockfile, valid between
  // |Create()| and |Cleanup()|.  Two instances cannot have a lock on
  // the same file at the same time.
  int lock_fd_;
#endif

  // If messages are received in the locked state, the corresponding command
  // lines are saved here to be replayed later.
  std::vector<DelayedStartupMessage> saved_startup_messages_;

  DISALLOW_COPY_AND_ASSIGN(ProcessSingleton);
};

#endif  // CHROME_BROWSER_PROCESS_SINGLETON_H_