OLD | NEW |
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_APP_CHROME_WATCHER_COMMAND_LINE_WIN_H_ | 5 #ifndef CHROME_APP_CHROME_WATCHER_COMMAND_LINE_WIN_H_ |
6 #define CHROME_APP_CHROME_WATCHER_COMMAND_LINE_WIN_H_ | 6 #define CHROME_APP_CHROME_WATCHER_COMMAND_LINE_WIN_H_ |
7 | 7 |
8 #include <windows.h> | 8 #include <windows.h> |
| 9 #include <vector> |
9 | 10 |
| 11 #include "base/command_line.h" |
| 12 #include "base/files/file_path.h" |
| 13 #include "base/memory/scoped_ptr.h" |
10 #include "base/win/scoped_handle.h" | 14 #include "base/win/scoped_handle.h" |
11 | 15 |
12 namespace base { | 16 // Class for configuring the Chrome watcher process via the command-line. This |
13 class CommandLine; | 17 // accepts configuration from the parent process and generates the required |
14 class FilePath; | 18 // command-line. |
15 } // namespace base | 19 // |
| 20 // It provides functionality for the common task of sharing handles from a |
| 21 // parent to a child process, automatically duplicating them with the |
| 22 // appropriate permissions. These handles are closed when the generator is |
| 23 // destroyed so the generator must outlive the process creation. |
| 24 class ChromeWatcherCommandLineGenerator { |
| 25 public: |
| 26 explicit ChromeWatcherCommandLineGenerator(const base::FilePath& chrome_exe); |
| 27 |
| 28 // Sets a handle to be shared with the child process. This will duplicate the |
| 29 // handle with the inherit flag, with this object retaining ownership of the |
| 30 // duplicated handle. As such, this object must live at least until after the |
| 31 // watcher process has been created. Returns true on success, false on |
| 32 // failure. This can fail if the call to DuplicateHandle fails. Each of these |
| 33 // may only be called once. |
| 34 bool SetOnInitializedEventHandle(HANDLE on_initialized_event_handle); |
| 35 bool SetParentProcessHandle(HANDLE parent_process_handle_); |
| 36 |
| 37 // Generates a command-line representing this configuration. Must be run from |
| 38 // the main thread of the parent process. This should only be called after the |
| 39 // generator is fully configured. |
| 40 base::CommandLine GenerateCommandLine(); |
| 41 |
| 42 // Appends all inherited handles to the provided vector. Does not clear the |
| 43 // vector first. |
| 44 void GetInheritedHandles(std::vector<HANDLE>* inherited_handles) const; |
| 45 |
| 46 protected: |
| 47 // Exposed for unittesting. |
| 48 |
| 49 // Duplicate the provided |handle|, making it inheritable, and storing it in |
| 50 // the provided |scoped_handle|. |
| 51 bool SetHandle(HANDLE handle, base::win::ScopedHandle* scoped_handle); |
| 52 |
| 53 base::FilePath chrome_exe_; |
| 54 |
| 55 // Duplicated inheritable handles that are being shared from the parent to the |
| 56 // child. |
| 57 base::win::ScopedHandle on_initialized_event_handle_; |
| 58 base::win::ScopedHandle parent_process_handle_; |
| 59 |
| 60 private: |
| 61 DISALLOW_COPY_AND_ASSIGN(ChromeWatcherCommandLineGenerator); |
| 62 }; |
| 63 |
| 64 // Class for processing a command-line used to start a Chrome Watcher process |
| 65 // and exposing the resulting configuration and any shared handles. To be used |
| 66 // within the watcher process. |
| 67 class ChromeWatcherCommandLine { |
| 68 public: |
| 69 // Causes the process to explode if any inherited handles weren't taken from |
| 70 // this object. |
| 71 ~ChromeWatcherCommandLine(); |
| 72 |
| 73 // Parses the provided command-line used to launch a Chrome Watcher process. |
| 74 // If this fails any successfully opened handles will be closed prior to |
| 75 // return. Returns a ChromeWatcherCommandLine object on success, nullptr |
| 76 // otherwise. |
| 77 static scoped_ptr<ChromeWatcherCommandLine> InterpretCommandLine( |
| 78 const base::CommandLine& command_line); |
| 79 |
| 80 // Accessors for handles. Any handles not taken from this object at the time |
| 81 // of its destruction will cause a terminal error. |
| 82 base::win::ScopedHandle TakeOnInitializedEventHandle(); |
| 83 base::win::ScopedHandle TakeParentProcessHandle(); |
| 84 |
| 85 // Returns the ID of the main thread in the parent process. |
| 86 DWORD main_thread_id() const { return main_thread_id_; } |
| 87 |
| 88 private: |
| 89 // Private constructor for use by InterpretCommandLine. |
| 90 ChromeWatcherCommandLine(HANDLE on_initialized_event_handle, |
| 91 HANDLE parent_process_handle, |
| 92 DWORD main_thread_id); |
| 93 |
| 94 base::win::ScopedHandle on_initialized_event_handle_; |
| 95 base::win::ScopedHandle parent_process_handle_; |
| 96 |
| 97 // The ID of the main thread in the parent process. |
| 98 DWORD main_thread_id_; |
| 99 |
| 100 DISALLOW_COPY_AND_ASSIGN(ChromeWatcherCommandLine); |
| 101 }; |
16 | 102 |
17 // Generates a CommandLine that will launch |chrome_exe| in Chrome Watcher mode | 103 // Generates a CommandLine that will launch |chrome_exe| in Chrome Watcher mode |
18 // to observe |parent_process|, whose main thread is identified by | 104 // to observe |parent_process|, whose main thread is identified by |
19 // |main_thread_id|. The watcher process will signal |on_initialized_event| when | 105 // |main_thread_id|. The watcher process will signal |on_initialized_event| when |
20 // its initialization is complete. | 106 // its initialization is complete. |
21 base::CommandLine GenerateChromeWatcherCommandLine( | 107 base::CommandLine GenerateChromeWatcherCommandLine( |
22 const base::FilePath& chrome_exe, | 108 const base::FilePath& chrome_exe, |
23 HANDLE parent_process, | 109 HANDLE parent_process, |
24 DWORD main_thread_id, | 110 DWORD main_thread_id, |
25 HANDLE on_initialized_event); | 111 HANDLE on_initialized_event); |
26 | 112 |
27 // Interprets the Command Line used to launch a Chrome Watcher process and | 113 // Interprets the Command Line used to launch a Chrome Watcher process and |
28 // extracts the parent process and initialization event HANDLEs and the parent | 114 // extracts the parent process and initialization event HANDLEs and the parent |
29 // process main thread ID. Verifies that the handles are usable in this process | 115 // process main thread ID. Verifies that the handles are usable in this process |
30 // before returning them. Returns true if all parameters are successfully parsed | 116 // before returning them. Returns true if all parameters are successfully parsed |
31 // and false otherwise. In case of partial failure, any successfully parsed | 117 // and false otherwise. In case of partial failure, any successfully parsed |
32 // HANDLEs will be closed. | 118 // HANDLEs will be closed. |
33 bool InterpretChromeWatcherCommandLine( | 119 bool InterpretChromeWatcherCommandLine( |
34 const base::CommandLine& command_line, | 120 const base::CommandLine& command_line, |
35 base::win::ScopedHandle* parent_process, | 121 base::win::ScopedHandle* parent_process, |
36 DWORD* main_thread_id, | 122 DWORD* main_thread_id, |
37 base::win::ScopedHandle* on_initialized_event); | 123 base::win::ScopedHandle* on_initialized_event); |
38 | 124 |
39 #endif // CHROME_APP_CHROME_WATCHER_COMMAND_LINE_WIN_H_ | 125 #endif // CHROME_APP_CHROME_WATCHER_COMMAND_LINE_WIN_H_ |
OLD | NEW |