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