Chromium Code Reviews| Index: chrome/app/chrome_watcher_command_line_win.h |
| diff --git a/chrome/app/chrome_watcher_command_line_win.h b/chrome/app/chrome_watcher_command_line_win.h |
| index d4d4a97dba0b3e3ebf80b67c0302ed6e63225504..b80b85740c2eb1daa9cc0470614b421991f8769c 100644 |
| --- a/chrome/app/chrome_watcher_command_line_win.h |
| +++ b/chrome/app/chrome_watcher_command_line_win.h |
| @@ -6,13 +6,102 @@ |
| #define CHROME_APP_CHROME_WATCHER_COMMAND_LINE_WIN_H_ |
| #include <windows.h> |
| +#include <vector> |
| +#include "base/command_line.h" |
| +#include "base/files/file_path.h" |
| +#include "base/memory/scoped_ptr.h" |
| #include "base/win/scoped_handle.h" |
| -namespace base { |
| -class CommandLine; |
| -class FilePath; |
| -} // namespace base |
| +// Class for configuring the Chrome watcher process via the command-line. This |
| +// accepts configuration from the parent process and generates the required |
| +// command-line. |
| +// |
| +// It provides functionality for the common task of sharing handles from a |
| +// parent to a child process, automatically duplicating them with the |
| +// appropriate permissions. These handles are closed when the generator is |
| +// destroyed so the generator must outlive the process creation. |
| +class ChromeWatcherCommandLineGenerator { |
| + public: |
| + explicit ChromeWatcherCommandLineGenerator(const base::FilePath& chrome_exe); |
| + |
| + // Sets a handle to be shared with the child process. This will duplicate the |
| + // handle with the inherit flag, with this object retaining ownership of the |
| + // duplicated handle. As such, this object must live at least until after the |
| + // watcher process has been created. Returns true on success, false on |
| + // failure. This can fail if the call to DuplicateHandle fails. Each of this |
| + // may only be called once. |
| + bool SetOnInitializedEventHandle(HANDLE on_initialized_event_handle); |
| + bool SetParentProcessHandle(HANDLE parent_process_handle_); |
| + |
| + // Generates a command-line representing this configuration. Must be run from |
| + // the main thread of the parent process. |
| + base::CommandLine GenerateCommandLine(); |
| + |
| + // Appends all inherited handles to the provided vector. Does not clear the |
| + // vector first. |
| + void GetInheritedHandles(std::vector<HANDLE>* inherited_handles) const; |
| + |
| + protected: |
| + // Exposed for unittesting. |
| + |
| + // Duplicate the provided |handle|, making it inheritable, and storing it in |
| + // the provided |scoped_handle|. Also updated inherited_handles_. |
| + bool SetHandle(HANDLE handle, base::win::ScopedHandle* scoped_handle); |
| + |
| + base::FilePath chrome_exe_; |
| + |
| + // Duplicated inheritable handles that are being shared from the parent to the |
| + // child. |
| + base::win::ScopedHandle on_initialized_event_handle_; |
| + base::win::ScopedHandle parent_process_handle_; |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(ChromeWatcherCommandLineGenerator); |
| +}; |
| + |
| +// Class for processing a command-line used to start a Chrome Watcher process |
| +// and exposing the resulting configuration and any shared handles. To be used |
| +// within the watcher process. |
| +class ChromeWatcherCommandLine { |
| + public: |
| + // Causes the process to explode if any inherited handles weren't taken from |
| + // this object. |
| + ~ChromeWatcherCommandLine(); |
| + |
| + // Parses the provided command-line used to launch a Chrome Watcher process. |
| + // If this fails any successfully opened handles will be closed prior to |
| + // return. Returns a ChromeWatcherCommandLine object on success, nullptr |
| + // otherwise. |
| + static scoped_ptr<ChromeWatcherCommandLine> InterpretCommandLine( |
| + const base::CommandLine& command_line); |
| + |
| + // Accessors for handles. Any handles not taken from this object at the time |
| + // of its destruction will cause a terminal error. |
| + base::win::ScopedHandle& on_initialized_event_handle() { |
|
grt (UTC plus 2)
2015/12/22 16:31:31
i like base::win::ScopedHandle TakeOnInitializedEv
chrisha
2015/12/23 14:08:23
Sounds reasonable. I'll return to that.
|
| + return on_initialized_event_handle_; |
| + } |
| + base::win::ScopedHandle& parent_process_handle() { |
| + return parent_process_handle_; |
| + } |
| + |
| + // Returns the ID of the main thread in the parent process. |
| + DWORD main_thread_id() const { return main_thread_id_; } |
| + |
| + private: |
| + // Private constructor for use by InterpretCommandLine. |
| + ChromeWatcherCommandLine(HANDLE on_initialized_event_handle, |
| + HANDLE parent_process_handle, |
| + DWORD main_thread_id); |
| + |
| + base::win::ScopedHandle on_initialized_event_handle_; |
| + base::win::ScopedHandle parent_process_handle_; |
| + |
| + // The ID of the main thread in the parent process. |
| + DWORD main_thread_id_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ChromeWatcherCommandLine); |
| +}; |
| // Generates a CommandLine that will launch |chrome_exe| in Chrome Watcher mode |
| // to observe |parent_process|, whose main thread is identified by |