| 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 #include "components/browser_watcher/watcher_client_win.h" | 5 #include "components/browser_watcher/watcher_client_win.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/process/launch.h" | 11 #include "base/process/launch.h" |
| 12 #include "base/win/windows_version.h" | 12 #include "base/win/windows_version.h" |
| 13 | 13 |
| 14 namespace browser_watcher { | 14 namespace browser_watcher { |
| 15 | 15 |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 base::Process OpenOwnProcessInheritable() { | 18 base::Process OpenOwnProcessInheritable() { |
| 19 return base::Process( | 19 return base::Process( |
| 20 ::OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION, | 20 ::OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION, |
| 21 TRUE, // Ineritable handle. | 21 TRUE, // Ineritable handle. |
| 22 base::GetCurrentProcId())); | 22 base::GetCurrentProcId())); |
| 23 } | 23 } |
| 24 | 24 |
| 25 } // namespace | 25 } // namespace |
| 26 | 26 |
| 27 WatcherClient::WatcherClient(const CommandLineGenerator& command_line_generator) | 27 WatcherClient::WatcherClient(const CommandLineGenerator& command_line_generator) |
| 28 : use_legacy_launch_(base::win::GetVersion() < base::win::VERSION_VISTA), | 28 : command_line_generator_(command_line_generator), |
| 29 command_line_generator_(command_line_generator), | 29 process_(base::kNullProcessHandle) {} |
| 30 process_(base::kNullProcessHandle) { | |
| 31 } | |
| 32 | 30 |
| 33 WatcherClient::~WatcherClient() { | 31 WatcherClient::~WatcherClient() { |
| 34 } | 32 } |
| 35 | 33 |
| 36 void WatcherClient::LaunchWatcher() { | 34 void WatcherClient::LaunchWatcher() { |
| 37 DCHECK(!process_.IsValid()); | 35 DCHECK(!process_.IsValid()); |
| 38 | 36 |
| 39 // Build the command line for the watcher process. | 37 // Build the command line for the watcher process. |
| 40 base::Process self = OpenOwnProcessInheritable(); | 38 base::Process self = OpenOwnProcessInheritable(); |
| 41 DCHECK(self.IsValid()); | 39 DCHECK(self.IsValid()); |
| 42 base::CommandLine cmd_line(command_line_generator_.Run(self.Handle())); | 40 base::CommandLine cmd_line(command_line_generator_.Run(self.Handle())); |
| 43 | 41 |
| 44 base::HandlesToInheritVector to_inherit; | |
| 45 base::LaunchOptions options; | 42 base::LaunchOptions options; |
| 46 options.start_hidden = true; | 43 options.start_hidden = true; |
| 47 if (use_legacy_launch_) { | 44 |
| 48 // Launch the child process inheriting all handles on XP. | 45 // Launch the child process inheriting only |self|. |
| 49 options.inherit_handles = true; | 46 options.handles_to_inherit.push_back(self.Handle()); |
| 50 } else { | 47 options.handles_to_inherit.insert(options.handles_to_inherit.end(), |
| 51 // Launch the child process inheriting only |self| on | 48 inherited_handles_.begin(), |
| 52 // Vista and better. | 49 inherited_handles_.end()); |
| 53 to_inherit.push_back(self.Handle()); | |
| 54 to_inherit.insert(to_inherit.end(), inherited_handles_.begin(), | |
| 55 inherited_handles_.end()); | |
| 56 options.handles_to_inherit = &to_inherit; | |
| 57 } | |
| 58 | 50 |
| 59 process_ = base::LaunchProcess(cmd_line, options); | 51 process_ = base::LaunchProcess(cmd_line, options); |
| 60 if (!process_.IsValid()) | 52 if (!process_.IsValid()) |
| 61 LOG(ERROR) << "Failed to launch browser watcher."; | 53 LOG(ERROR) << "Failed to launch browser watcher."; |
| 62 } | 54 } |
| 63 | 55 |
| 64 void WatcherClient::AddInheritedHandle(HANDLE handle) { | 56 void WatcherClient::AddInheritedHandle(HANDLE handle) { |
| 65 inherited_handles_.push_back(handle); | 57 inherited_handles_.push_back(handle); |
| 66 } | 58 } |
| 67 | 59 |
| 68 } // namespace browser_watcher | 60 } // namespace browser_watcher |
| OLD | NEW |