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 #include "chrome/app/chrome_watcher_command_line_win.h" | 5 #include "chrome/app/chrome_watcher_command_line_win.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 14 #include "base/win/win_util.h" | 14 #include "base/win/win_util.h" |
| 15 #include "chrome/common/chrome_switches.h" | 15 #include "chrome/common/chrome_switches.h" |
| 16 #include "content/public/common/content_switches.h" | 16 #include "content/public/common/content_switches.h" |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 const char kMainThreadIdSwitch[] = "main-thread-id"; | |
| 20 const char kOnIninitializedEventHandleSwitch[] = "on-initialized-event-handle"; | 21 const char kOnIninitializedEventHandleSwitch[] = "on-initialized-event-handle"; |
| 21 const char kParentHandleSwitch[] = "parent-handle"; | 22 const char kParentHandleSwitch[] = "parent-handle"; |
| 22 const char kMainThreadIdSwitch[] = "main-thread-id"; | |
| 23 | 23 |
| 24 void AppendHandleSwitch(const std::string& switch_name, | 24 void AppendHandleSwitch(const std::string& switch_name, |
| 25 HANDLE handle, | 25 HANDLE handle, |
| 26 base::CommandLine* command_line) { | 26 base::CommandLine* command_line) { |
| 27 command_line->AppendSwitchASCII( | 27 command_line->AppendSwitchASCII( |
| 28 switch_name, base::UintToString(base::win::HandleToUint32(handle))); | 28 switch_name, base::UintToString(base::win::HandleToUint32(handle))); |
| 29 } | 29 } |
| 30 | 30 |
| 31 uint32_t ReadUintSwitch(const base::CommandLine& command_line, | 31 uint32_t ReadUintSwitch(const base::CommandLine& command_line, |
| 32 const std::string& switch_name) { | 32 const std::string& switch_name) { |
| 33 std::string switch_string = command_line.GetSwitchValueASCII(switch_name); | 33 std::string switch_string = command_line.GetSwitchValueASCII(switch_name); |
| 34 unsigned int switch_uint = 0; | 34 unsigned int switch_uint = 0; |
| 35 if (switch_string.empty() || | 35 if (switch_string.empty() || |
| 36 !base::StringToUint(switch_string, &switch_uint)) { | 36 !base::StringToUint(switch_string, &switch_uint)) { |
| 37 DLOG(ERROR) << "Missing or invalid " << switch_name << " argument."; | 37 DLOG(ERROR) << "Missing or invalid " << switch_name << " argument."; |
| 38 return 0; | 38 return 0; |
| 39 } | 39 } |
| 40 return switch_uint; | 40 return switch_uint; |
| 41 } | 41 } |
| 42 | 42 |
| 43 HANDLE ReadHandleFromSwitch(const base::CommandLine& command_line, | 43 HANDLE ReadHandleFromSwitch(const base::CommandLine& command_line, |
| 44 const std::string& switch_name) { | 44 const std::string& switch_name) { |
| 45 return reinterpret_cast<HANDLE>(ReadUintSwitch(command_line, switch_name)); | 45 return reinterpret_cast<HANDLE>(ReadUintSwitch(command_line, switch_name)); |
| 46 } | 46 } |
| 47 | 47 |
| 48 } // namespace | 48 } // namespace |
| 49 | 49 |
| 50 ChromeWatcherCommandLineGenerator::ChromeWatcherCommandLineGenerator( | |
| 51 const base::FilePath& chrome_exe) : chrome_exe_(chrome_exe) { | |
| 52 } | |
| 53 | |
| 54 bool ChromeWatcherCommandLineGenerator::SetOnInitializedEventHandle( | |
| 55 HANDLE on_initialized_event_handle) { | |
| 56 return SetHandle(on_initialized_event_handle, &on_initialized_event_handle_); | |
| 57 } | |
| 58 | |
| 59 bool ChromeWatcherCommandLineGenerator::SetParentProcessHandle( | |
| 60 HANDLE parent_process_handle) { | |
| 61 return SetHandle(parent_process_handle, &parent_process_handle_); | |
| 62 } | |
| 63 | |
| 64 // Generates a command-line representing this configuration. | |
| 65 base::CommandLine ChromeWatcherCommandLineGenerator::GenerateCommandLine() { | |
| 66 // TODO(chrisha): Get rid of the following function and move the | |
| 67 // implementation here. | |
| 68 return GenerateChromeWatcherCommandLine( | |
| 69 chrome_exe_, parent_process_handle_.Get(), ::GetCurrentThreadId(), | |
| 70 on_initialized_event_handle_.Get()); | |
| 71 } | |
| 72 | |
| 73 void ChromeWatcherCommandLineGenerator::GetInheritedHandles( | |
| 74 std::vector<HANDLE>* inherited_handles) const { | |
| 75 if (on_initialized_event_handle_.IsValid()) | |
| 76 inherited_handles->push_back(on_initialized_event_handle_.Get()); | |
| 77 if (parent_process_handle_.IsValid()) | |
| 78 inherited_handles->push_back(parent_process_handle_.Get()); | |
| 79 } | |
| 80 | |
| 81 bool ChromeWatcherCommandLineGenerator::SetHandle( | |
| 82 HANDLE handle, base::win::ScopedHandle* scoped_handle) { | |
| 83 // Create a duplicate handle that is inheritable. | |
| 84 HANDLE proc = ::GetCurrentProcess(); | |
| 85 HANDLE new_handle = 0; | |
| 86 if (!::DuplicateHandle(proc, handle, proc, &new_handle, 0, TRUE, | |
| 87 DUPLICATE_SAME_ACCESS)) { | |
| 88 return false; | |
| 89 } | |
| 90 | |
| 91 scoped_handle->Set(new_handle); | |
| 92 return true; | |
| 93 } | |
| 94 | |
| 95 ChromeWatcherCommandLine::ChromeWatcherCommandLine( | |
| 96 HANDLE on_initialized_event_handle, | |
| 97 HANDLE parent_process_handle, | |
| 98 DWORD main_thread_id) | |
| 99 : on_initialized_event_handle_(on_initialized_event_handle), | |
| 100 parent_process_handle_(parent_process_handle), | |
| 101 main_thread_id_(main_thread_id) { | |
| 102 } | |
| 103 | |
| 104 ChromeWatcherCommandLine::~ChromeWatcherCommandLine() { | |
| 105 // If any handles were not taken then die violently. | |
| 106 if (on_initialized_event_handle_.IsValid() || | |
|
grt (UTC plus 2)
2015/12/22 16:31:31
is this the same?
CHECK(!on_initialized_event_ha
chrisha
2015/12/23 14:08:23
Indeed... I wanted to have an explicit log stateme
| |
| 107 parent_process_handle_.IsValid()) | |
| 108 LOG(FATAL) << "Handles left untaken."; | |
| 109 } | |
| 110 | |
| 111 scoped_ptr<ChromeWatcherCommandLine> | |
| 112 ChromeWatcherCommandLine::InterpretCommandLine( | |
| 113 const base::CommandLine& command_line) { | |
| 114 base::win::ScopedHandle on_initialized_event_handle; | |
| 115 base::win::ScopedHandle parent_process_handle; | |
| 116 DWORD main_thread_id = 0; | |
| 117 | |
| 118 // TODO(chrisha): Get rid of the following function and move the | |
| 119 // implementation here. | |
| 120 if (!InterpretChromeWatcherCommandLine( | |
| 121 command_line, &parent_process_handle, &main_thread_id, | |
| 122 &on_initialized_event_handle)) | |
| 123 return scoped_ptr<ChromeWatcherCommandLine>(); | |
| 124 | |
| 125 return scoped_ptr<ChromeWatcherCommandLine>(new ChromeWatcherCommandLine( | |
| 126 on_initialized_event_handle.Take(), parent_process_handle.Take(), | |
| 127 main_thread_id)); | |
| 128 } | |
| 129 | |
| 50 base::CommandLine GenerateChromeWatcherCommandLine( | 130 base::CommandLine GenerateChromeWatcherCommandLine( |
| 51 const base::FilePath& chrome_exe, | 131 const base::FilePath& chrome_exe, |
| 52 HANDLE parent_process, | 132 HANDLE parent_process, |
| 53 DWORD main_thread_id, | 133 DWORD main_thread_id, |
| 54 HANDLE on_initialized_event) { | 134 HANDLE on_initialized_event) { |
| 55 base::CommandLine command_line(chrome_exe); | 135 base::CommandLine command_line(chrome_exe); |
| 56 command_line.AppendSwitchASCII(switches::kProcessType, "watcher"); | 136 command_line.AppendSwitchASCII(switches::kProcessType, "watcher"); |
| 57 command_line.AppendSwitchASCII(kMainThreadIdSwitch, | 137 command_line.AppendSwitchASCII(kMainThreadIdSwitch, |
| 58 base::UintToString(main_thread_id)); | 138 base::UintToString(main_thread_id)); |
| 59 AppendHandleSwitch(kOnIninitializedEventHandleSwitch, on_initialized_event, | 139 AppendHandleSwitch(kOnIninitializedEventHandleSwitch, on_initialized_event, |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 110 !parent_process->IsValid()) { | 190 !parent_process->IsValid()) { |
| 111 // If one was valid and not the other, free the valid one. | 191 // If one was valid and not the other, free the valid one. |
| 112 on_initialized_event->Close(); | 192 on_initialized_event->Close(); |
| 113 parent_process->Close(); | 193 parent_process->Close(); |
| 114 *main_thread_id = 0; | 194 *main_thread_id = 0; |
| 115 return false; | 195 return false; |
| 116 } | 196 } |
| 117 | 197 |
| 118 return true; | 198 return true; |
| 119 } | 199 } |
| OLD | NEW |