OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/browser/extensions/api/terminal/terminal_private_api.h" | 5 #include "chrome/browser/extensions/api/terminal/terminal_private_api.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/command_line.h" | |
10 #include "base/json/json_writer.h" | 11 #include "base/json/json_writer.h" |
11 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
12 #include "base/sys_info.h" | 13 #include "base/sys_info.h" |
13 #include "base/values.h" | 14 #include "base/values.h" |
14 #include "chrome/browser/extensions/api/terminal/terminal_extension_helper.h" | 15 #include "chrome/browser/extensions/api/terminal/terminal_extension_helper.h" |
15 #include "chrome/browser/extensions/extension_service.h" | 16 #include "chrome/browser/extensions/extension_service.h" |
16 #include "chrome/browser/extensions/extension_tab_util.h" | 17 #include "chrome/browser/extensions/extension_tab_util.h" |
18 #include "chrome/common/chrome_switches.h" | |
17 #include "chrome/common/extensions/api/terminal_private.h" | 19 #include "chrome/common/extensions/api/terminal_private.h" |
18 #include "chromeos/process_proxy/process_proxy_registry.h" | 20 #include "chromeos/process_proxy/process_proxy_registry.h" |
19 #include "content/public/browser/browser_context.h" | 21 #include "content/public/browser/browser_context.h" |
20 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
21 #include "content/public/browser/web_contents.h" | 23 #include "content/public/browser/web_contents.h" |
22 #include "extensions/browser/app_window/app_window.h" | 24 #include "extensions/browser/app_window/app_window.h" |
23 #include "extensions/browser/app_window/app_window_registry.h" | 25 #include "extensions/browser/app_window/app_window_registry.h" |
24 #include "extensions/browser/event_router.h" | 26 #include "extensions/browser/event_router.h" |
25 | 27 |
26 namespace terminal_private = extensions::api::terminal_private; | 28 namespace terminal_private = extensions::api::terminal_private; |
27 namespace OnTerminalResize = | 29 namespace OnTerminalResize = |
28 extensions::api::terminal_private::OnTerminalResize; | 30 extensions::api::terminal_private::OnTerminalResize; |
29 namespace OpenTerminalProcess = | 31 namespace OpenTerminalProcess = |
30 extensions::api::terminal_private::OpenTerminalProcess; | 32 extensions::api::terminal_private::OpenTerminalProcess; |
31 namespace CloseTerminalProcess = | 33 namespace CloseTerminalProcess = |
32 extensions::api::terminal_private::CloseTerminalProcess; | 34 extensions::api::terminal_private::CloseTerminalProcess; |
33 namespace SendInput = extensions::api::terminal_private::SendInput; | 35 namespace SendInput = extensions::api::terminal_private::SendInput; |
34 namespace AckOutput = extensions::api::terminal_private::AckOutput; | 36 namespace AckOutput = extensions::api::terminal_private::AckOutput; |
35 | 37 |
36 namespace { | 38 namespace { |
37 | 39 |
38 const char kCroshName[] = "crosh"; | 40 const char kCroshName[] = "crosh"; |
39 const char kCroshCommand[] = "/usr/bin/crosh"; | 41 const char kCroshCommand[] = "/usr/bin/crosh"; |
40 // We make stubbed crosh just echo back input. | 42 // We make stubbed crosh just echo back input. |
41 const char kStubbedCroshCommand[] = "cat"; | 43 const char kStubbedCroshCommand[] = "cat"; |
42 | 44 |
43 const char* GetCroshPath() { | 45 std::string GetCroshPath() { |
46 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | |
47 if (command_line->HasSwitch(switches::kCroshCommand)) | |
48 return command_line->GetSwitchValueASCII(switches::kCroshCommand); | |
44 if (base::SysInfo::IsRunningOnChromeOS()) | 49 if (base::SysInfo::IsRunningOnChromeOS()) |
45 return kCroshCommand; | 50 return std::string(kCroshCommand); |
46 else | 51 else |
benwells
2017/01/10 22:25:07
Nit: While here can you remove this else (no else
reveman
2017/01/11 00:33:01
Done.
| |
47 return kStubbedCroshCommand; | 52 return std::string(kStubbedCroshCommand); |
48 } | 53 } |
49 | 54 |
50 const char* GetProcessCommandForName(const std::string& name) { | 55 std::string GetProcessCommandForName(const std::string& name) { |
51 if (name == kCroshName) | 56 if (name == kCroshName) |
52 return GetCroshPath(); | 57 return GetCroshPath(); |
53 else | 58 else |
54 return NULL; | 59 return std::string(); |
55 } | 60 } |
56 | 61 |
57 void NotifyProcessOutput(content::BrowserContext* browser_context, | 62 void NotifyProcessOutput(content::BrowserContext* browser_context, |
58 const std::string& extension_id, | 63 const std::string& extension_id, |
59 int tab_id, | 64 int tab_id, |
60 int terminal_id, | 65 int terminal_id, |
61 const std::string& output_type, | 66 const std::string& output_type, |
62 const std::string& output) { | 67 const std::string& output) { |
63 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) { | 68 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) { |
64 content::BrowserThread::PostTask( | 69 content::BrowserThread::PostTask( |
(...skipping 29 matching lines...) Expand all Loading... | |
94 extensions::AppWindowRegistry::Get(browser_context) | 99 extensions::AppWindowRegistry::Get(browser_context) |
95 ->GetAppWindowForWebContents(web_contents); | 100 ->GetAppWindowForWebContents(web_contents); |
96 return window ? window->session_id().id() : -1; | 101 return window ? window->session_id().id() : -1; |
97 } | 102 } |
98 | 103 |
99 } // namespace | 104 } // namespace |
100 | 105 |
101 namespace extensions { | 106 namespace extensions { |
102 | 107 |
103 TerminalPrivateOpenTerminalProcessFunction:: | 108 TerminalPrivateOpenTerminalProcessFunction:: |
104 TerminalPrivateOpenTerminalProcessFunction() : command_(NULL) {} | 109 TerminalPrivateOpenTerminalProcessFunction() {} |
105 | 110 |
106 TerminalPrivateOpenTerminalProcessFunction:: | 111 TerminalPrivateOpenTerminalProcessFunction:: |
107 ~TerminalPrivateOpenTerminalProcessFunction() {} | 112 ~TerminalPrivateOpenTerminalProcessFunction() {} |
108 | 113 |
109 ExtensionFunction::ResponseAction | 114 ExtensionFunction::ResponseAction |
110 TerminalPrivateOpenTerminalProcessFunction::Run() { | 115 TerminalPrivateOpenTerminalProcessFunction::Run() { |
111 std::unique_ptr<OpenTerminalProcess::Params> params( | 116 std::unique_ptr<OpenTerminalProcess::Params> params( |
112 OpenTerminalProcess::Params::Create(*args_)); | 117 OpenTerminalProcess::Params::Create(*args_)); |
113 EXTENSION_FUNCTION_VALIDATE(params.get()); | 118 EXTENSION_FUNCTION_VALIDATE(params.get()); |
114 | 119 |
115 command_ = GetProcessCommandForName(params->process_name); | 120 command_ = GetProcessCommandForName(params->process_name); |
116 if (!command_) | 121 if (command_.empty()) |
117 return RespondNow(Error("Invalid process name.")); | 122 return RespondNow(Error("Invalid process name.")); |
118 | 123 |
119 content::WebContents* caller_contents = GetSenderWebContents(); | 124 content::WebContents* caller_contents = GetSenderWebContents(); |
120 if (!caller_contents) | 125 if (!caller_contents) |
121 return RespondNow(Error("No web contents.")); | 126 return RespondNow(Error("No web contents.")); |
122 | 127 |
123 // Passed to terminalPrivate.ackOutput, which is called from the API's custom | 128 // Passed to terminalPrivate.ackOutput, which is called from the API's custom |
124 // bindings after terminalPrivate.onProcessOutput is dispatched. It is used to | 129 // bindings after terminalPrivate.onProcessOutput is dispatched. It is used to |
125 // determine whether ackOutput call should be handled or not. ackOutput will | 130 // determine whether ackOutput call should be handled or not. ackOutput will |
126 // be called from every web contents in which a onProcessOutput listener | 131 // be called from every web contents in which a onProcessOutput listener |
(...skipping 16 matching lines...) Expand all Loading... | |
143 tab_id), | 148 tab_id), |
144 base::Bind( | 149 base::Bind( |
145 &TerminalPrivateOpenTerminalProcessFunction::RespondOnUIThread, | 150 &TerminalPrivateOpenTerminalProcessFunction::RespondOnUIThread, |
146 this))); | 151 this))); |
147 return RespondLater(); | 152 return RespondLater(); |
148 } | 153 } |
149 | 154 |
150 void TerminalPrivateOpenTerminalProcessFunction::OpenOnFileThread( | 155 void TerminalPrivateOpenTerminalProcessFunction::OpenOnFileThread( |
151 const ProcessOutputCallback& output_callback, | 156 const ProcessOutputCallback& output_callback, |
152 const OpenProcessCallback& callback) { | 157 const OpenProcessCallback& callback) { |
153 DCHECK(command_); | 158 DCHECK(!command_.empty()); |
154 | 159 |
155 chromeos::ProcessProxyRegistry* registry = | 160 chromeos::ProcessProxyRegistry* registry = |
156 chromeos::ProcessProxyRegistry::Get(); | 161 chromeos::ProcessProxyRegistry::Get(); |
157 | 162 |
158 int terminal_id = registry->OpenProcess(command_, output_callback); | 163 int terminal_id = registry->OpenProcess(command_.c_str(), output_callback); |
159 | 164 |
160 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | 165 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
161 base::Bind(callback, terminal_id)); | 166 base::Bind(callback, terminal_id)); |
162 } | 167 } |
163 | 168 |
164 TerminalPrivateSendInputFunction::~TerminalPrivateSendInputFunction() {} | 169 TerminalPrivateSendInputFunction::~TerminalPrivateSendInputFunction() {} |
165 | 170 |
166 void TerminalPrivateOpenTerminalProcessFunction::RespondOnUIThread( | 171 void TerminalPrivateOpenTerminalProcessFunction::RespondOnUIThread( |
167 int terminal_id) { | 172 int terminal_id) { |
168 if (terminal_id < 0) { | 173 if (terminal_id < 0) { |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
290 params->pid)); | 295 params->pid)); |
291 | 296 |
292 return RespondNow(NoArguments()); | 297 return RespondNow(NoArguments()); |
293 } | 298 } |
294 | 299 |
295 void TerminalPrivateAckOutputFunction::AckOutputOnFileThread(int terminal_id) { | 300 void TerminalPrivateAckOutputFunction::AckOutputOnFileThread(int terminal_id) { |
296 chromeos::ProcessProxyRegistry::Get()->AckOutput(terminal_id); | 301 chromeos::ProcessProxyRegistry::Get()->AckOutput(terminal_id); |
297 } | 302 } |
298 | 303 |
299 } // namespace extensions | 304 } // namespace extensions |
OLD | NEW |