| 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 "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "chrome/browser/chromeos/process_proxy/process_proxy_registry.h" | 10 #include "chrome/browser/chromeos/process_proxy/process_proxy_registry.h" |
| 11 #include "chrome/browser/chromeos/system/runtime_environment.h" | 11 #include "chrome/browser/chromeos/system/runtime_environment.h" |
| 12 #include "chrome/browser/extensions/extension_event_names.h" | 12 #include "chrome/browser/extensions/extension_event_names.h" |
| 13 #include "chrome/browser/extensions/extension_event_router.h" | 13 #include "chrome/browser/extensions/extension_event_router.h" |
| 14 #include "chrome/browser/extensions/extension_service.h" | 14 #include "chrome/browser/extensions/extension_service.h" |
| 15 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 16 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 const char kCroshName[] = "crosh"; | 20 const char kCroshName[] = "crosh"; |
| 21 const char kCroshCommand[] = "/usr/bin/crosh"; | 21 const char kCroshCommand[] = "/usr/bin/crosh"; |
| 22 // We make stubbed crosh just echo back input. | 22 // We make stubbed crosh just echo back input. |
| 23 const char kStubbedCroshCommand[] = "cat"; | 23 const char kStubbedCroshCommand[] = "cat"; |
| 24 | 24 |
| 25 const char kPermissionError[] = | |
| 26 "Extension does not have the permission to use this API"; | |
| 27 | |
| 28 const char* kAllowedExtensionIds[] ={ | |
| 29 "okddffdblfhhnmhodogpojmfkjmhinfp", // test SSH/Crosh Client | |
| 30 "pnhechapfaindjhompbnflcldabbghjo" // HTerm App | |
| 31 }; | |
| 32 | |
| 33 // Allow component and whitelisted extensions. | |
| 34 bool AllowAccessToExtension(Profile* profile, const std::string& extension_id) { | |
| 35 ExtensionService* service = profile->GetExtensionService(); | |
| 36 const Extension* extension = service->GetExtensionById(extension_id, false); | |
| 37 | |
| 38 if (!extension) | |
| 39 return false; | |
| 40 | |
| 41 if (extension->location() == Extension::COMPONENT) | |
| 42 return true; | |
| 43 | |
| 44 for (size_t i = 0; i < arraysize(kAllowedExtensionIds); i++) { | |
| 45 if (extension->id() == kAllowedExtensionIds[i]) | |
| 46 return true; | |
| 47 } | |
| 48 return false; | |
| 49 } | |
| 50 | |
| 51 const char* GetCroshPath() { | 25 const char* GetCroshPath() { |
| 52 if (chromeos::system::runtime_environment::IsRunningOnChromeOS()) | 26 if (chromeos::system::runtime_environment::IsRunningOnChromeOS()) |
| 53 return kCroshCommand; | 27 return kCroshCommand; |
| 54 else | 28 else |
| 55 return kStubbedCroshCommand; | 29 return kStubbedCroshCommand; |
| 56 } | 30 } |
| 57 | 31 |
| 58 const char* GetProcessCommandForName(const std::string& name) { | 32 const char* GetProcessCommandForName(const std::string& name) { |
| 59 if (name == kCroshName) { | 33 if (name == kCroshName) { |
| 60 return GetCroshPath(); | 34 return GetCroshPath(); |
| 61 } else { | 35 } else { |
| 62 return NULL; | 36 return NULL; |
| 63 } | 37 } |
| 64 } | 38 } |
| 65 | 39 |
| 66 void NotifyProcessOutput(Profile* profile, | 40 void NotifyProcessOutput(Profile* profile, |
| 67 const std::string& extension_id, | 41 const std::string& extension_id, |
| 68 pid_t pid, | 42 pid_t pid, |
| 69 const std::string& output_type, | 43 const std::string& output_type, |
| 70 const std::string& output) { | 44 const std::string& output) { |
| 71 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) { | 45 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) { |
| 72 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | 46 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| 73 base::Bind(&NotifyProcessOutput, profile, extension_id, | 47 base::Bind(&NotifyProcessOutput, profile, extension_id, |
| 74 pid, output_type, output)); | 48 pid, output_type, output)); |
| 75 return; | 49 return; |
| 76 } | 50 } |
| 77 | 51 |
| 78 CHECK(AllowAccessToExtension(profile, extension_id)); | |
| 79 | |
| 80 base::ListValue args; | 52 base::ListValue args; |
| 81 args.Append(new base::FundamentalValue(pid)); | 53 args.Append(new base::FundamentalValue(pid)); |
| 82 args.Append(new base::StringValue(output_type)); | 54 args.Append(new base::StringValue(output_type)); |
| 83 args.Append(new base::StringValue(output)); | 55 args.Append(new base::StringValue(output)); |
| 84 | 56 |
| 85 std::string args_json; | 57 std::string args_json; |
| 86 base::JSONWriter::Write(&args, false /* pretty_print */, &args_json); | 58 base::JSONWriter::Write(&args, false /* pretty_print */, &args_json); |
| 87 | 59 |
| 88 if (profile && profile->GetExtensionEventRouter()) { | 60 if (profile && profile->GetExtensionEventRouter()) { |
| 89 profile->GetExtensionEventRouter()->DispatchEventToExtension( | 61 profile->GetExtensionEventRouter()->DispatchEventToExtension( |
| 90 extension_id, extension_event_names::kOnTerminalProcessOutput, | 62 extension_id, extension_event_names::kOnTerminalProcessOutput, |
| 91 args_json, NULL, GURL()); | 63 args_json, NULL, GURL()); |
| 92 } | 64 } |
| 93 } | 65 } |
| 94 | 66 |
| 95 } // namespace | 67 } // namespace |
| 96 | 68 |
| 97 TerminalPrivateFunction::TerminalPrivateFunction() { | 69 TerminalPrivateFunction::TerminalPrivateFunction() { |
| 98 } | 70 } |
| 99 | 71 |
| 100 TerminalPrivateFunction::~TerminalPrivateFunction() { | 72 TerminalPrivateFunction::~TerminalPrivateFunction() { |
| 101 } | 73 } |
| 102 | 74 |
| 103 bool TerminalPrivateFunction::RunImpl() { | 75 bool TerminalPrivateFunction::RunImpl() { |
| 104 if (!AllowAccessToExtension(profile_, extension_id())) { | |
| 105 error_ = kPermissionError; | |
| 106 return false; | |
| 107 } | |
| 108 | |
| 109 return RunTerminalFunction(); | 76 return RunTerminalFunction(); |
| 110 } | 77 } |
| 111 | 78 |
| 112 OpenTerminalProcessFunction::OpenTerminalProcessFunction() : command_(NULL) { | 79 OpenTerminalProcessFunction::OpenTerminalProcessFunction() : command_(NULL) { |
| 113 } | 80 } |
| 114 | 81 |
| 115 OpenTerminalProcessFunction::~OpenTerminalProcessFunction() { | 82 OpenTerminalProcessFunction::~OpenTerminalProcessFunction() { |
| 116 } | 83 } |
| 117 | 84 |
| 118 bool OpenTerminalProcessFunction::RunTerminalFunction() { | 85 bool OpenTerminalProcessFunction::RunTerminalFunction() { |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 | 171 |
| 205 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | 172 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| 206 base::Bind(&CloseTerminalProcessFunction::RespondOnUIThread, this, | 173 base::Bind(&CloseTerminalProcessFunction::RespondOnUIThread, this, |
| 207 success)); | 174 success)); |
| 208 } | 175 } |
| 209 | 176 |
| 210 void CloseTerminalProcessFunction::RespondOnUIThread(bool success) { | 177 void CloseTerminalProcessFunction::RespondOnUIThread(bool success) { |
| 211 result_.reset(new base::FundamentalValue(success)); | 178 result_.reset(new base::FundamentalValue(success)); |
| 212 SendResponse(true); | 179 SendResponse(true); |
| 213 } | 180 } |
| OLD | NEW |