Chromium Code Reviews| 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 this | |
|
Sigurður Ásgeirsson
2016/01/08 16:55:19
each of these
chrisha
2016/01/08 22:29:00
Done.
| |
| 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. | |
| 39 base::CommandLine GenerateCommandLine(); | |
|
Sigurður Ásgeirsson
2016/01/08 16:55:19
Presumably this has to be called after all handles
chrisha
2016/01/08 22:29:00
Done.
| |
| 40 | |
| 41 // Appends all inherited handles to the provided vector. Does not clear the | |
| 42 // vector first. | |
| 43 void GetInheritedHandles(std::vector<HANDLE>* inherited_handles) const; | |
| 44 | |
| 45 protected: | |
| 46 // Exposed for unittesting. | |
| 47 | |
| 48 // Duplicate the provided |handle|, making it inheritable, and storing it in | |
| 49 // the provided |scoped_handle|. Also updated inherited_handles_. | |
|
Sigurður Ásgeirsson
2016/01/08 16:55:19
inherited_handles_ ?
chrisha
2016/01/08 22:29:00
Made the comment more verbose. You need to manuall
| |
| 50 bool SetHandle(HANDLE handle, base::win::ScopedHandle* scoped_handle); | |
| 51 | |
| 52 base::FilePath chrome_exe_; | |
| 53 | |
| 54 // Duplicated inheritable handles that are being shared from the parent to the | |
| 55 // child. | |
| 56 base::win::ScopedHandle on_initialized_event_handle_; | |
| 57 base::win::ScopedHandle parent_process_handle_; | |
| 58 | |
| 59 private: | |
| 60 DISALLOW_COPY_AND_ASSIGN(ChromeWatcherCommandLineGenerator); | |
| 61 }; | |
| 62 | |
| 63 // Class for processing a command-line used to start a Chrome Watcher process | |
| 64 // and exposing the resulting configuration and any shared handles. To be used | |
| 65 // within the watcher process. | |
| 66 class ChromeWatcherCommandLine { | |
| 67 public: | |
| 68 // Causes the process to explode if any inherited handles weren't taken from | |
| 69 // this object. | |
| 70 ~ChromeWatcherCommandLine(); | |
| 71 | |
| 72 // Parses the provided command-line used to launch a Chrome Watcher process. | |
| 73 // If this fails any successfully opened handles will be closed prior to | |
| 74 // return. Returns a ChromeWatcherCommandLine object on success, nullptr | |
| 75 // otherwise. | |
| 76 static scoped_ptr<ChromeWatcherCommandLine> InterpretCommandLine( | |
| 77 const base::CommandLine& command_line); | |
| 78 | |
| 79 // Accessors for handles. Any handles not taken from this object at the time | |
| 80 // of its destruction will cause a terminal error. | |
| 81 base::win::ScopedHandle TakeOnInitializedEventHandle(); | |
| 82 base::win::ScopedHandle TakeParentProcessHandle(); | |
| 83 | |
| 84 // Returns the ID of the main thread in the parent process. | |
| 85 DWORD main_thread_id() const { return main_thread_id_; } | |
| 86 | |
| 87 private: | |
| 88 // Private constructor for use by InterpretCommandLine. | |
| 89 ChromeWatcherCommandLine(HANDLE on_initialized_event_handle, | |
| 90 HANDLE parent_process_handle, | |
| 91 DWORD main_thread_id); | |
| 92 | |
| 93 base::win::ScopedHandle on_initialized_event_handle_; | |
| 94 base::win::ScopedHandle parent_process_handle_; | |
| 95 | |
| 96 // The ID of the main thread in the parent process. | |
| 97 DWORD main_thread_id_; | |
| 98 | |
| 99 DISALLOW_COPY_AND_ASSIGN(ChromeWatcherCommandLine); | |
| 100 }; | |
| 16 | 101 |
| 17 // Generates a CommandLine that will launch |chrome_exe| in Chrome Watcher mode | 102 // Generates a CommandLine that will launch |chrome_exe| in Chrome Watcher mode |
| 18 // to observe |parent_process|, whose main thread is identified by | 103 // to observe |parent_process|, whose main thread is identified by |
| 19 // |main_thread_id|. The watcher process will signal |on_initialized_event| when | 104 // |main_thread_id|. The watcher process will signal |on_initialized_event| when |
| 20 // its initialization is complete. | 105 // its initialization is complete. |
| 21 base::CommandLine GenerateChromeWatcherCommandLine( | 106 base::CommandLine GenerateChromeWatcherCommandLine( |
| 22 const base::FilePath& chrome_exe, | 107 const base::FilePath& chrome_exe, |
| 23 HANDLE parent_process, | 108 HANDLE parent_process, |
| 24 DWORD main_thread_id, | 109 DWORD main_thread_id, |
| 25 HANDLE on_initialized_event); | 110 HANDLE on_initialized_event); |
| 26 | 111 |
| 27 // Interprets the Command Line used to launch a Chrome Watcher process and | 112 // Interprets the Command Line used to launch a Chrome Watcher process and |
| 28 // extracts the parent process and initialization event HANDLEs and the parent | 113 // 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 | 114 // 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 | 115 // before returning them. Returns true if all parameters are successfully parsed |
| 31 // and false otherwise. In case of partial failure, any successfully parsed | 116 // and false otherwise. In case of partial failure, any successfully parsed |
| 32 // HANDLEs will be closed. | 117 // HANDLEs will be closed. |
| 33 bool InterpretChromeWatcherCommandLine( | 118 bool InterpretChromeWatcherCommandLine( |
| 34 const base::CommandLine& command_line, | 119 const base::CommandLine& command_line, |
| 35 base::win::ScopedHandle* parent_process, | 120 base::win::ScopedHandle* parent_process, |
| 36 DWORD* main_thread_id, | 121 DWORD* main_thread_id, |
| 37 base::win::ScopedHandle* on_initialized_event); | 122 base::win::ScopedHandle* on_initialized_event); |
| 38 | 123 |
| 39 #endif // CHROME_APP_CHROME_WATCHER_COMMAND_LINE_WIN_H_ | 124 #endif // CHROME_APP_CHROME_WATCHER_COMMAND_LINE_WIN_H_ |
| OLD | NEW |