Add ICU message format support
[chromium-blink-merge.git] / chrome / browser / process_singleton.h
blob3260c4ef240cc0c07574840777051a3c5ce44ef0
1 // Copyright (c) 2012 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_
8 #include "build/build_config.h"
10 #if defined(OS_WIN)
11 #include <windows.h>
12 #endif // defined(OS_WIN)
14 #include <set>
15 #include <vector>
17 #include "base/basictypes.h"
18 #include "base/callback.h"
19 #include "base/command_line.h"
20 #include "base/files/file_path.h"
21 #include "base/logging.h"
22 #include "base/memory/ref_counted.h"
23 #include "base/process/process.h"
24 #include "base/threading/non_thread_safe.h"
25 #include "ui/gfx/native_widget_types.h"
27 #if defined(OS_POSIX) && !defined(OS_ANDROID)
28 #include "base/files/scoped_temp_dir.h"
29 #endif
31 #if defined(OS_WIN)
32 #include "base/win/message_window.h"
33 #endif // defined(OS_WIN)
35 namespace base {
36 class CommandLine;
39 // ProcessSingleton ----------------------------------------------------------
41 // This class allows different browser processes to communicate with
42 // each other. It is named according to the user data directory, so
43 // we can be sure that no more than one copy of the application can be
44 // running at once with a given data directory.
46 // Implementation notes:
47 // - the Windows implementation uses an invisible global message window;
48 // - the Linux implementation uses a Unix domain socket in the user data dir.
50 class ProcessSingleton : public base::NonThreadSafe {
51 public:
52 enum NotifyResult {
53 PROCESS_NONE,
54 PROCESS_NOTIFIED,
55 PROFILE_IN_USE,
56 LOCK_ERROR,
59 // Implement this callback to handle notifications from other processes. The
60 // callback will receive the command line and directory with which the other
61 // Chrome process was launched. Return true if the command line will be
62 // handled within the current browser instance or false if the remote process
63 // should handle it (i.e., because the current process is shutting down).
64 using NotificationCallback =
65 base::Callback<bool(const base::CommandLine& command_line,
66 const base::FilePath& current_directory)>;
68 ProcessSingleton(const base::FilePath& user_data_dir,
69 const NotificationCallback& notification_callback);
70 ~ProcessSingleton();
72 // Notify another process, if available. Otherwise sets ourselves as the
73 // singleton instance. Returns PROCESS_NONE if we became the singleton
74 // instance. Callers are guaranteed to either have notified an existing
75 // process or have grabbed the singleton (unless the profile is locked by an
76 // unreachable process).
77 // TODO(brettw): Make the implementation of this method non-platform-specific
78 // by making Linux re-use the Windows implementation.
79 NotifyResult NotifyOtherProcessOrCreate();
81 // Sets ourself up as the singleton instance. Returns true on success. If
82 // false is returned, we are not the singleton instance and the caller must
83 // exit.
84 // NOTE: Most callers should generally prefer NotifyOtherProcessOrCreate() to
85 // this method, only callers for whom failure is preferred to notifying
86 // another process should call this directly.
87 bool Create();
89 // Clear any lock state during shutdown.
90 void Cleanup();
92 #if defined(OS_POSIX) && !defined(OS_ANDROID)
93 static void DisablePromptForTesting();
94 #endif
95 #if defined(OS_WIN)
96 // Called to query whether to kill a hung browser process that has visible
97 // windows. Return true to allow killing the hung process.
98 using ShouldKillRemoteProcessCallback = base::Callback<bool()>;
99 void OverrideShouldKillRemoteProcessCallbackForTesting(
100 const ShouldKillRemoteProcessCallback& display_dialog_callback);
101 #endif
103 protected:
104 // Notify another process, if available.
105 // Returns true if another process was found and notified, false if we should
106 // continue with the current process.
107 // On Windows, Create() has to be called before this.
108 NotifyResult NotifyOtherProcess();
110 #if defined(OS_POSIX) && !defined(OS_ANDROID)
111 // Exposed for testing. We use a timeout on Linux, and in tests we want
112 // this timeout to be short.
113 NotifyResult NotifyOtherProcessWithTimeout(
114 const base::CommandLine& command_line,
115 int retry_attempts,
116 const base::TimeDelta& timeout,
117 bool kill_unresponsive);
118 NotifyResult NotifyOtherProcessWithTimeoutOrCreate(
119 const base::CommandLine& command_line,
120 int retry_attempts,
121 const base::TimeDelta& timeout);
122 void OverrideCurrentPidForTesting(base::ProcessId pid);
123 void OverrideKillCallbackForTesting(
124 const base::Callback<void(int)>& callback);
125 #endif
127 private:
128 NotificationCallback notification_callback_; // Handler for notifications.
130 #if defined(OS_WIN)
131 bool EscapeVirtualization(const base::FilePath& user_data_dir);
133 HWND remote_window_; // The HWND_MESSAGE of another browser.
134 base::win::MessageWindow window_; // The message-only window.
135 bool is_virtualized_; // Stuck inside Microsoft Softricity VM environment.
136 HANDLE lock_file_;
137 base::FilePath user_data_dir_;
138 ShouldKillRemoteProcessCallback should_kill_remote_process_callback_;
139 #elif defined(OS_POSIX) && !defined(OS_ANDROID)
140 // Return true if the given pid is one of our child processes.
141 // Assumes that the current pid is the root of all pids of the current
142 // instance.
143 bool IsSameChromeInstance(pid_t pid);
145 // Extract the process's pid from a symbol link path and if it is on
146 // the same host, kill the process, unlink the lock file and return true.
147 // If the process is part of the same chrome instance, unlink the lock file
148 // and return true without killing it.
149 // If the process is on a different host, return false.
150 bool KillProcessByLockPath();
152 // Default function to kill a process, overridable by tests.
153 void KillProcess(int pid);
155 // Allow overriding for tests.
156 base::ProcessId current_pid_;
158 // Function to call when the other process is hung and needs to be killed.
159 // Allows overriding for tests.
160 base::Callback<void(int)> kill_callback_;
162 // Path in file system to the socket.
163 base::FilePath socket_path_;
165 // Path in file system to the lock.
166 base::FilePath lock_path_;
168 // Path in file system to the cookie file.
169 base::FilePath cookie_path_;
171 // Temporary directory to hold the socket.
172 base::ScopedTempDir socket_dir_;
174 // Helper class for linux specific messages. LinuxWatcher is ref counted
175 // because it posts messages between threads.
176 class LinuxWatcher;
177 scoped_refptr<LinuxWatcher> watcher_;
178 #endif
180 DISALLOW_COPY_AND_ASSIGN(ProcessSingleton);
183 #endif // CHROME_BROWSER_PROCESS_SINGLETON_H_