| 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); |
| 49 |
| 44 if (base::SysInfo::IsRunningOnChromeOS()) | 50 if (base::SysInfo::IsRunningOnChromeOS()) |
| 45 return kCroshCommand; | 51 return std::string(kCroshCommand); |
| 46 else | 52 |
| 47 return kStubbedCroshCommand; | 53 return std::string(kStubbedCroshCommand); |
| 48 } | 54 } |
| 49 | 55 |
| 50 const char* GetProcessCommandForName(const std::string& name) { | 56 std::string GetProcessCommandForName(const std::string& name) { |
| 51 if (name == kCroshName) | 57 if (name == kCroshName) |
| 52 return GetCroshPath(); | 58 return GetCroshPath(); |
| 53 else | 59 else |
| 54 return NULL; | 60 return std::string(); |
| 55 } | 61 } |
| 56 | 62 |
| 57 void NotifyProcessOutput(content::BrowserContext* browser_context, | 63 void NotifyProcessOutput(content::BrowserContext* browser_context, |
| 58 const std::string& extension_id, | 64 const std::string& extension_id, |
| 59 int tab_id, | 65 int tab_id, |
| 60 int terminal_id, | 66 int terminal_id, |
| 61 const std::string& output_type, | 67 const std::string& output_type, |
| 62 const std::string& output) { | 68 const std::string& output) { |
| 63 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) { | 69 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) { |
| 64 content::BrowserThread::PostTask( | 70 content::BrowserThread::PostTask( |
| (...skipping 29 matching lines...) Expand all Loading... |
| 94 extensions::AppWindowRegistry::Get(browser_context) | 100 extensions::AppWindowRegistry::Get(browser_context) |
| 95 ->GetAppWindowForWebContents(web_contents); | 101 ->GetAppWindowForWebContents(web_contents); |
| 96 return window ? window->session_id().id() : -1; | 102 return window ? window->session_id().id() : -1; |
| 97 } | 103 } |
| 98 | 104 |
| 99 } // namespace | 105 } // namespace |
| 100 | 106 |
| 101 namespace extensions { | 107 namespace extensions { |
| 102 | 108 |
| 103 TerminalPrivateOpenTerminalProcessFunction:: | 109 TerminalPrivateOpenTerminalProcessFunction:: |
| 104 TerminalPrivateOpenTerminalProcessFunction() : command_(NULL) {} | 110 TerminalPrivateOpenTerminalProcessFunction() {} |
| 105 | 111 |
| 106 TerminalPrivateOpenTerminalProcessFunction:: | 112 TerminalPrivateOpenTerminalProcessFunction:: |
| 107 ~TerminalPrivateOpenTerminalProcessFunction() {} | 113 ~TerminalPrivateOpenTerminalProcessFunction() {} |
| 108 | 114 |
| 109 ExtensionFunction::ResponseAction | 115 ExtensionFunction::ResponseAction |
| 110 TerminalPrivateOpenTerminalProcessFunction::Run() { | 116 TerminalPrivateOpenTerminalProcessFunction::Run() { |
| 111 std::unique_ptr<OpenTerminalProcess::Params> params( | 117 std::unique_ptr<OpenTerminalProcess::Params> params( |
| 112 OpenTerminalProcess::Params::Create(*args_)); | 118 OpenTerminalProcess::Params::Create(*args_)); |
| 113 EXTENSION_FUNCTION_VALIDATE(params.get()); | 119 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 114 | 120 |
| 115 command_ = GetProcessCommandForName(params->process_name); | 121 command_ = GetProcessCommandForName(params->process_name); |
| 116 if (!command_) | 122 if (command_.empty()) |
| 117 return RespondNow(Error("Invalid process name.")); | 123 return RespondNow(Error("Invalid process name.")); |
| 118 | 124 |
| 119 content::WebContents* caller_contents = GetSenderWebContents(); | 125 content::WebContents* caller_contents = GetSenderWebContents(); |
| 120 if (!caller_contents) | 126 if (!caller_contents) |
| 121 return RespondNow(Error("No web contents.")); | 127 return RespondNow(Error("No web contents.")); |
| 122 | 128 |
| 123 // Passed to terminalPrivate.ackOutput, which is called from the API's custom | 129 // Passed to terminalPrivate.ackOutput, which is called from the API's custom |
| 124 // bindings after terminalPrivate.onProcessOutput is dispatched. It is used to | 130 // bindings after terminalPrivate.onProcessOutput is dispatched. It is used to |
| 125 // determine whether ackOutput call should be handled or not. ackOutput will | 131 // determine whether ackOutput call should be handled or not. ackOutput will |
| 126 // be called from every web contents in which a onProcessOutput listener | 132 // be called from every web contents in which a onProcessOutput listener |
| (...skipping 16 matching lines...) Expand all Loading... |
| 143 tab_id), | 149 tab_id), |
| 144 base::Bind( | 150 base::Bind( |
| 145 &TerminalPrivateOpenTerminalProcessFunction::RespondOnUIThread, | 151 &TerminalPrivateOpenTerminalProcessFunction::RespondOnUIThread, |
| 146 this))); | 152 this))); |
| 147 return RespondLater(); | 153 return RespondLater(); |
| 148 } | 154 } |
| 149 | 155 |
| 150 void TerminalPrivateOpenTerminalProcessFunction::OpenOnFileThread( | 156 void TerminalPrivateOpenTerminalProcessFunction::OpenOnFileThread( |
| 151 const ProcessOutputCallback& output_callback, | 157 const ProcessOutputCallback& output_callback, |
| 152 const OpenProcessCallback& callback) { | 158 const OpenProcessCallback& callback) { |
| 153 DCHECK(command_); | 159 DCHECK(!command_.empty()); |
| 154 | 160 |
| 155 chromeos::ProcessProxyRegistry* registry = | 161 chromeos::ProcessProxyRegistry* registry = |
| 156 chromeos::ProcessProxyRegistry::Get(); | 162 chromeos::ProcessProxyRegistry::Get(); |
| 157 | 163 |
| 158 int terminal_id = registry->OpenProcess(command_, output_callback); | 164 int terminal_id = registry->OpenProcess(command_.c_str(), output_callback); |
| 159 | 165 |
| 160 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | 166 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| 161 base::Bind(callback, terminal_id)); | 167 base::Bind(callback, terminal_id)); |
| 162 } | 168 } |
| 163 | 169 |
| 164 TerminalPrivateSendInputFunction::~TerminalPrivateSendInputFunction() {} | 170 TerminalPrivateSendInputFunction::~TerminalPrivateSendInputFunction() {} |
| 165 | 171 |
| 166 void TerminalPrivateOpenTerminalProcessFunction::RespondOnUIThread( | 172 void TerminalPrivateOpenTerminalProcessFunction::RespondOnUIThread( |
| 167 int terminal_id) { | 173 int terminal_id) { |
| 168 if (terminal_id < 0) { | 174 if (terminal_id < 0) { |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 params->pid)); | 296 params->pid)); |
| 291 | 297 |
| 292 return RespondNow(NoArguments()); | 298 return RespondNow(NoArguments()); |
| 293 } | 299 } |
| 294 | 300 |
| 295 void TerminalPrivateAckOutputFunction::AckOutputOnFileThread(int terminal_id) { | 301 void TerminalPrivateAckOutputFunction::AckOutputOnFileThread(int terminal_id) { |
| 296 chromeos::ProcessProxyRegistry::Get()->AckOutput(terminal_id); | 302 chromeos::ProcessProxyRegistry::Get()->AckOutput(terminal_id); |
| 297 } | 303 } |
| 298 | 304 |
| 299 } // namespace extensions | 305 } // namespace extensions |
| OLD | NEW |