| OLD | NEW | 
|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 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 "extensions/browser/api/messaging/native_message_host.h" | 5 #include "extensions/browser/api/messaging/native_message_host.h" | 
| 6 | 6 | 
|  | 7 #include <memory> | 
| 7 #include <string> | 8 #include <string> | 
| 8 #include <utility> | 9 #include <utility> | 
| 9 | 10 | 
| 10 #include "base/bind.h" | 11 #include "base/bind.h" | 
| 11 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" | 
| 12 #include "base/json/json_reader.h" | 13 #include "base/json/json_reader.h" | 
| 13 #include "base/json/json_writer.h" | 14 #include "base/json/json_writer.h" | 
| 14 #include "base/location.h" | 15 #include "base/location.h" | 
| 15 #include "base/macros.h" | 16 #include "base/macros.h" | 
| 16 #include "base/memory/scoped_ptr.h" |  | 
| 17 #include "base/thread_task_runner_handle.h" | 17 #include "base/thread_task_runner_handle.h" | 
| 18 #include "base/thread_task_runner_handle.h" | 18 #include "base/thread_task_runner_handle.h" | 
| 19 #include "base/values.h" | 19 #include "base/values.h" | 
| 20 #include "chrome/browser/browser_process.h" | 20 #include "chrome/browser/browser_process.h" | 
| 21 #include "chrome/browser/chromeos/arc/arc_support_host.h" | 21 #include "chrome/browser/chromeos/arc/arc_support_host.h" | 
| 22 #include "chrome/browser/extensions/api/messaging/native_messaging_test_util.h" | 22 #include "chrome/browser/extensions/api/messaging/native_messaging_test_util.h" | 
| 23 #include "components/policy/core/common/policy_service.h" | 23 #include "components/policy/core/common/policy_service.h" | 
| 24 #include "content/public/browser/browser_thread.h" | 24 #include "content/public/browser/browser_thread.h" | 
| 25 #include "extensions/common/constants.h" | 25 #include "extensions/common/constants.h" | 
| 26 #include "extensions/common/url_pattern.h" | 26 #include "extensions/common/url_pattern.h" | 
| (...skipping 10 matching lines...) Expand all  Loading... | 
| 37 // A simple NativeMessageHost that mimics the implementation of | 37 // A simple NativeMessageHost that mimics the implementation of | 
| 38 // chrome/test/data/native_messaging/native_hosts/echo.py. It is currently | 38 // chrome/test/data/native_messaging/native_hosts/echo.py. It is currently | 
| 39 // used for testing by ExtensionApiTest::NativeMessagingBasic. | 39 // used for testing by ExtensionApiTest::NativeMessagingBasic. | 
| 40 | 40 | 
| 41 const char* const kEchoHostOrigins[] = { | 41 const char* const kEchoHostOrigins[] = { | 
| 42     // ScopedTestNativeMessagingHost::kExtensionId | 42     // ScopedTestNativeMessagingHost::kExtensionId | 
| 43     "chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/"}; | 43     "chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/"}; | 
| 44 | 44 | 
| 45 class EchoHost : public NativeMessageHost { | 45 class EchoHost : public NativeMessageHost { | 
| 46  public: | 46  public: | 
| 47   static scoped_ptr<NativeMessageHost> Create() { | 47   static std::unique_ptr<NativeMessageHost> Create() { | 
| 48     return scoped_ptr<NativeMessageHost>(new EchoHost()); | 48     return std::unique_ptr<NativeMessageHost>(new EchoHost()); | 
| 49   } | 49   } | 
| 50 | 50 | 
| 51   EchoHost() : message_number_(0), client_(NULL) {} | 51   EchoHost() : message_number_(0), client_(NULL) {} | 
| 52 | 52 | 
| 53   void Start(Client* client) override { client_ = client; } | 53   void Start(Client* client) override { client_ = client; } | 
| 54 | 54 | 
| 55   void OnMessage(const std::string& request_string) override { | 55   void OnMessage(const std::string& request_string) override { | 
| 56     scoped_ptr<base::Value> request_value = | 56     std::unique_ptr<base::Value> request_value = | 
| 57         base::JSONReader::Read(request_string); | 57         base::JSONReader::Read(request_string); | 
| 58     scoped_ptr<base::DictionaryValue> request( | 58     std::unique_ptr<base::DictionaryValue> request( | 
| 59       static_cast<base::DictionaryValue*>(request_value.release())); | 59         static_cast<base::DictionaryValue*>(request_value.release())); | 
| 60     if (request_string.find("stopHostTest") != std::string::npos) { | 60     if (request_string.find("stopHostTest") != std::string::npos) { | 
| 61       client_->CloseChannel(kNativeHostExited); | 61       client_->CloseChannel(kNativeHostExited); | 
| 62     } else if (request_string.find("bigMessageTest") != std::string::npos) { | 62     } else if (request_string.find("bigMessageTest") != std::string::npos) { | 
| 63       client_->CloseChannel(kHostInputOuputError); | 63       client_->CloseChannel(kHostInputOuputError); | 
| 64     } else { | 64     } else { | 
| 65       ProcessEcho(*request); | 65       ProcessEcho(*request); | 
| 66     } | 66     } | 
| 67   }; | 67   }; | 
| 68 | 68 | 
| 69   scoped_refptr<base::SingleThreadTaskRunner> task_runner() const override { | 69   scoped_refptr<base::SingleThreadTaskRunner> task_runner() const override { | 
| (...skipping 14 matching lines...) Expand all  Loading... | 
| 84   int message_number_; | 84   int message_number_; | 
| 85   Client* client_; | 85   Client* client_; | 
| 86 | 86 | 
| 87   DISALLOW_COPY_AND_ASSIGN(EchoHost); | 87   DISALLOW_COPY_AND_ASSIGN(EchoHost); | 
| 88 }; | 88 }; | 
| 89 | 89 | 
| 90 struct BuiltInHost { | 90 struct BuiltInHost { | 
| 91   const char* const name; | 91   const char* const name; | 
| 92   const char* const* const allowed_origins; | 92   const char* const* const allowed_origins; | 
| 93   int allowed_origins_count; | 93   int allowed_origins_count; | 
| 94   scoped_ptr<NativeMessageHost>(*create_function)(); | 94   std::unique_ptr<NativeMessageHost> (*create_function)(); | 
| 95 }; | 95 }; | 
| 96 | 96 | 
| 97 scoped_ptr<NativeMessageHost> CreateIt2MeHost() { | 97 std::unique_ptr<NativeMessageHost> CreateIt2MeHost() { | 
| 98   scoped_ptr<remoting::It2MeHostFactory> host_factory( | 98   std::unique_ptr<remoting::It2MeHostFactory> host_factory( | 
| 99       new remoting::It2MeHostFactory()); | 99       new remoting::It2MeHostFactory()); | 
| 100   host_factory->set_policy_service(g_browser_process->policy_service()); | 100   host_factory->set_policy_service(g_browser_process->policy_service()); | 
| 101   scoped_ptr<remoting::ChromotingHostContext> context = | 101   std::unique_ptr<remoting::ChromotingHostContext> context = | 
| 102       remoting::ChromotingHostContext::CreateForChromeOS( | 102       remoting::ChromotingHostContext::CreateForChromeOS( | 
| 103           make_scoped_refptr(g_browser_process->system_request_context()), | 103           make_scoped_refptr(g_browser_process->system_request_context()), | 
| 104           content::BrowserThread::GetMessageLoopProxyForThread( | 104           content::BrowserThread::GetMessageLoopProxyForThread( | 
| 105               content::BrowserThread::IO), | 105               content::BrowserThread::IO), | 
| 106           content::BrowserThread::GetMessageLoopProxyForThread( | 106           content::BrowserThread::GetMessageLoopProxyForThread( | 
| 107               content::BrowserThread::UI), | 107               content::BrowserThread::UI), | 
| 108           content::BrowserThread::GetMessageLoopProxyForThread( | 108           content::BrowserThread::GetMessageLoopProxyForThread( | 
| 109               content::BrowserThread::FILE)); | 109               content::BrowserThread::FILE)); | 
| 110   scoped_ptr<NativeMessageHost> host(new remoting::It2MeNativeMessagingHost( | 110   std::unique_ptr<NativeMessageHost> host( | 
| 111       std::move(context), std::move(host_factory))); | 111       new remoting::It2MeNativeMessagingHost(std::move(context), | 
|  | 112                                              std::move(host_factory))); | 
| 112   return host; | 113   return host; | 
| 113 } | 114 } | 
| 114 | 115 | 
| 115 // If you modify the list of allowed_origins, don't forget to update | 116 // If you modify the list of allowed_origins, don't forget to update | 
| 116 // remoting/host/it2me/com.google.chrome.remote_assistance.json.jinja2 | 117 // remoting/host/it2me/com.google.chrome.remote_assistance.json.jinja2 | 
| 117 // to keep the two lists in sync. | 118 // to keep the two lists in sync. | 
| 118 // TODO(kelvinp): Load the native messaging manifest as a resource file into | 119 // TODO(kelvinp): Load the native messaging manifest as a resource file into | 
| 119 // chrome and fetch the list of allowed_origins from the manifest (see | 120 // chrome and fetch the list of allowed_origins from the manifest (see | 
| 120 // crbug/424743). | 121 // crbug/424743). | 
| 121 const char* const kRemotingIt2MeOrigins[] = { | 122 const char* const kRemotingIt2MeOrigins[] = { | 
| (...skipping 27 matching lines...) Expand all  Loading... | 
| 149     URLPattern allowed_origin(URLPattern::SCHEME_ALL, host.allowed_origins[i]); | 150     URLPattern allowed_origin(URLPattern::SCHEME_ALL, host.allowed_origins[i]); | 
| 150     if (allowed_origin.MatchesSecurityOrigin(origin)) { | 151     if (allowed_origin.MatchesSecurityOrigin(origin)) { | 
| 151       return true; | 152       return true; | 
| 152     } | 153     } | 
| 153   } | 154   } | 
| 154   return false; | 155   return false; | 
| 155 } | 156 } | 
| 156 | 157 | 
| 157 }  // namespace | 158 }  // namespace | 
| 158 | 159 | 
| 159 scoped_ptr<NativeMessageHost> NativeMessageHost::Create( | 160 std::unique_ptr<NativeMessageHost> NativeMessageHost::Create( | 
| 160     gfx::NativeView native_view, | 161     gfx::NativeView native_view, | 
| 161     const std::string& source_extension_id, | 162     const std::string& source_extension_id, | 
| 162     const std::string& native_host_name, | 163     const std::string& native_host_name, | 
| 163     bool allow_user_level, | 164     bool allow_user_level, | 
| 164     std::string* error) { | 165     std::string* error) { | 
| 165   for (unsigned int i = 0; i < arraysize(kBuiltInHost); i++) { | 166   for (unsigned int i = 0; i < arraysize(kBuiltInHost); i++) { | 
| 166     const BuiltInHost& host = kBuiltInHost[i]; | 167     const BuiltInHost& host = kBuiltInHost[i]; | 
| 167     std::string name(host.name); | 168     std::string name(host.name); | 
| 168     if (name == native_host_name) { | 169     if (name == native_host_name) { | 
| 169       if (MatchesSecurityOrigin(host, source_extension_id)) { | 170       if (MatchesSecurityOrigin(host, source_extension_id)) { | 
| 170         return (*host.create_function)(); | 171         return (*host.create_function)(); | 
| 171       } | 172       } | 
| 172       *error = kForbiddenError; | 173       *error = kForbiddenError; | 
| 173       return nullptr; | 174       return nullptr; | 
| 174     } | 175     } | 
| 175   } | 176   } | 
| 176   *error = kNotFoundError; | 177   *error = kNotFoundError; | 
| 177   return nullptr; | 178   return nullptr; | 
| 178 } | 179 } | 
| 179 | 180 | 
| 180 }  // namespace extensions | 181 }  // namespace extensions | 
| OLD | NEW | 
|---|