[M120][Tizen][Onscreen] Fix build errors for TV profile
[platform/framework/web/chromium-efl.git] / chrome / browser / process_singleton.h
1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef CHROME_BROWSER_PROCESS_SINGLETON_H_
6 #define CHROME_BROWSER_PROCESS_SINGLETON_H_
7
8 #include "base/sequence_checker.h"
9 #include "build/build_config.h"
10
11 #if BUILDFLAG(IS_WIN)
12 #include "base/win/windows_types.h"
13 #endif  // BUILDFLAG(IS_WIN)
14
15 #include "base/check.h"
16 #include "base/command_line.h"
17 #include "base/files/file_path.h"
18 #include "base/functional/callback.h"
19 #include "base/memory/ref_counted.h"
20 #include "base/process/process.h"
21 #include "ui/gfx/native_widget_types.h"
22
23 #if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_ANDROID)
24 #include "base/files/scoped_temp_dir.h"
25 #endif
26
27 #if BUILDFLAG(IS_WIN)
28 #include "base/win/message_window.h"
29 #endif  // BUILDFLAG(IS_WIN)
30
31 namespace base {
32 class CommandLine;
33 }
34
35 // ProcessSingleton ----------------------------------------------------------
36 //
37 // This class allows different browser processes to communicate with
38 // each other.  It is named according to the user data directory, so
39 // we can be sure that no more than one copy of the application can be
40 // running at once with a given data directory.
41 //
42 // Implementation notes:
43 // - the Windows implementation uses an invisible global message window;
44 // - the Linux implementation uses a Unix domain socket in the user data dir.
45
46 class ProcessSingleton {
47  public:
48   // Used to send the reason of remote hang process termination as histogram.
49   enum RemoteHungProcessTerminateReason {
50 #if BUILDFLAG(IS_WIN)
51     USER_ACCEPTED_TERMINATION = 1,
52     NO_VISIBLE_WINDOW_FOUND = 2,
53 #elif BUILDFLAG(IS_POSIX)
54     NOTIFY_ATTEMPTS_EXCEEDED = 3,
55     SOCKET_WRITE_FAILED = 4,
56     SOCKET_READ_FAILED = 5,
57 #endif
58     REMOTE_HUNG_PROCESS_TERMINATE_REASON_COUNT
59   };
60
61   // Used to send the result of interaction with remote process as histograms in
62   // case when remote process influences on start.
63   enum RemoteProcessInteractionResult {
64     TERMINATE_SUCCEEDED = 0,
65     TERMINATE_FAILED = 1,
66     REMOTE_PROCESS_NOT_FOUND = 2,
67 #if BUILDFLAG(IS_WIN)
68     TERMINATE_WAIT_TIMEOUT = 3,
69     RUNNING_PROCESS_NOTIFY_ERROR = 4,
70 #elif BUILDFLAG(IS_POSIX)
71     TERMINATE_NOT_ENOUGH_PERMISSIONS = 5,
72     REMOTE_PROCESS_SHUTTING_DOWN = 6,
73     PROFILE_UNLOCKED = 7,
74     PROFILE_UNLOCKED_BEFORE_KILL = 8,
75     SAME_BROWSER_INSTANCE = 9,
76     SAME_BROWSER_INSTANCE_BEFORE_KILL = 10,
77     FAILED_TO_EXTRACT_PID = 11,
78     INVALID_LOCK_FILE = 12,
79     ORPHANED_LOCK_FILE = 13,
80 #endif
81     USER_REFUSED_TERMINATION = 14,
82     REMOTE_PROCESS_INTERACTION_RESULT_COUNT
83   };
84
85   // Logged as histograms, do not modify these values.
86   enum NotifyResult {
87     PROCESS_NONE = 0,
88     PROCESS_NOTIFIED = 1,
89     PROFILE_IN_USE = 2,
90     LOCK_ERROR = 3,
91     LAST_VALUE = LOCK_ERROR
92   };
93
94   static constexpr int kNumNotifyResults = LAST_VALUE + 1;
95
96   // Implement this callback to handle notifications from other processes. The
97   // callback will receive the command line and directory with which the other
98   // Chrome process was launched. Return true if the command line will be
99   // handled within the current browser instance or false if the remote process
100   // should handle it (i.e., because the current process is shutting down).
101   using NotificationCallback =
102       base::RepeatingCallback<bool(const base::CommandLine& command_line,
103                                    const base::FilePath& current_directory)>;
104
105   ProcessSingleton(const base::FilePath& user_data_dir,
106                    const NotificationCallback& notification_callback);
107
108   ProcessSingleton(const ProcessSingleton&) = delete;
109   ProcessSingleton& operator=(const ProcessSingleton&) = delete;
110
111   ~ProcessSingleton();
112
113   // Notify another process, if available. Otherwise sets ourselves as the
114   // singleton instance. Returns PROCESS_NONE if we became the singleton
115   // instance. Callers are guaranteed to either have notified an existing
116   // process or have grabbed the singleton (unless the profile is locked by an
117   // unreachable process).
118   // TODO(brettw): Make the implementation of this method non-platform-specific
119   // by making Linux re-use the Windows implementation.
120   NotifyResult NotifyOtherProcessOrCreate();
121
122   // Sets ourself up as the singleton instance.  Returns true on success.  If
123   // false is returned, we are not the singleton instance and the caller must
124   // exit.
125   // NOTE: Most callers should generally prefer NotifyOtherProcessOrCreate() to
126   // this method, only callers for whom failure is preferred to notifying
127   // another process should call this directly.
128   bool Create();
129
130   // Start watching for notifications from other processes.
131   void StartWatching();
132
133   // Clear any lock state during shutdown.
134   void Cleanup();
135
136 #if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_ANDROID)
137   static void DisablePromptForTesting();
138   static void SkipIsChromeProcessCheckForTesting(bool skip);
139   static void SetUserOptedUnlockInUseProfileForTesting(bool set_unlock);
140 #endif
141 #if BUILDFLAG(IS_WIN)
142   // Called to query whether to kill a hung browser process that has visible
143   // windows. Return true to allow killing the hung process.
144   using ShouldKillRemoteProcessCallback = base::RepeatingCallback<bool()>;
145   void OverrideShouldKillRemoteProcessCallbackForTesting(
146       const ShouldKillRemoteProcessCallback& display_dialog_callback);
147 #endif
148
149  protected:
150   // Notify another process, if available.
151   // Returns true if another process was found and notified, false if we should
152   // continue with the current process.
153   // On Windows, Create() has to be called before this.
154   NotifyResult NotifyOtherProcess();
155
156 #if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_ANDROID)
157   // Exposed for testing.  We use a timeout on Linux, and in tests we want
158   // this timeout to be short.
159   NotifyResult NotifyOtherProcessWithTimeout(
160       const base::CommandLine& command_line,
161       int retry_attempts,
162       const base::TimeDelta& timeout,
163       bool kill_unresponsive);
164   NotifyResult NotifyOtherProcessWithTimeoutOrCreate(
165       const base::CommandLine& command_line,
166       int retry_attempts,
167       const base::TimeDelta& timeout);
168   void OverrideCurrentPidForTesting(base::ProcessId pid);
169   void OverrideKillCallbackForTesting(
170       const base::RepeatingCallback<void(int)>& callback);
171 #endif
172
173  private:
174   NotificationCallback notification_callback_;  // Handler for notifications.
175
176 #if BUILDFLAG(IS_WIN)
177   bool EscapeVirtualization(const base::FilePath& user_data_dir);
178
179   HWND remote_window_;  // The HWND_MESSAGE of another browser.
180   base::win::MessageWindow window_;  // The message-only window.
181   bool is_virtualized_;  // Stuck inside Microsoft Softricity VM environment.
182   HANDLE lock_file_;
183   base::FilePath user_data_dir_;
184   ShouldKillRemoteProcessCallback should_kill_remote_process_callback_;
185 #elif BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_ANDROID)
186   // Return true if the given pid is one of our child processes.
187   // Assumes that the current pid is the root of all pids of the current
188   // instance.
189   bool IsSameChromeInstance(pid_t pid);
190
191   // Extract the process's pid from a symbol link path and if it is on
192   // the same host or is_connected_to_socket is true, kill the process, unlink
193   // the lock file and return true.
194   // If the process is part of the same chrome instance, unlink the lock file
195   // and return true without killing it.
196   // If the process is on a different host and is_connected_to_socket is false,
197   // display profile in use error dialog (on Linux). If user opted to unlock
198   // profile (on Mac OS X by default), unlink the lock file and return true.
199   // Otherwise return false.
200   bool KillProcessByLockPath(bool is_connected_to_socket);
201
202   // Default function to kill a process, overridable by tests.
203   void KillProcess(int pid);
204
205   // Allow overriding for tests.
206   base::ProcessId current_pid_;
207
208   // Function to call when the other process is hung and needs to be killed.
209   // Allows overriding for tests.
210   base::RepeatingCallback<void(int)> kill_callback_;
211
212   // Path in file system to the socket.
213   base::FilePath socket_path_;
214
215   // Path in file system to the lock.
216   base::FilePath lock_path_;
217
218   // Path in file system to the cookie file.
219   base::FilePath cookie_path_;
220
221   // Temporary directory to hold the socket.
222   base::ScopedTempDir socket_dir_;
223   int sock_ = -1;
224
225   // Helper class for linux specific messages.  LinuxWatcher is ref counted
226   // because it posts messages between threads.
227   class LinuxWatcher;
228   scoped_refptr<LinuxWatcher> watcher_;
229 #endif
230
231 #if BUILDFLAG(IS_MAC)
232   // macOS 10.13 tries to open a new Chrome instance if a user tries to
233   // open an external link after Chrome has updated, but not relaunched.
234   // This method extracts any waiting "open URL" AppleEvent and forwards
235   // it to the running process. Returns true if an event was found and
236   // forwarded.
237   // crbug.com/777863
238   bool WaitForAndForwardOpenURLEvent(pid_t event_destination_pid);
239 #endif
240
241   SEQUENCE_CHECKER(sequence_checker_);
242 };
243
244 #endif  // CHROME_BROWSER_PROCESS_SINGLETON_H_