Index: chrome/browser/extensions/api/messaging/native_message_host_chromeos.cc |
diff --git a/chrome/browser/extensions/api/messaging/native_message_host_chromeos.cc b/chrome/browser/extensions/api/messaging/native_message_host_chromeos.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..61fc49ee8799a546445e745a511b01efa77770da |
--- /dev/null |
+++ b/chrome/browser/extensions/api/messaging/native_message_host_chromeos.cc |
@@ -0,0 +1,149 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "extensions/browser/api/messaging/native_message_host.h" |
+ |
+#include <map> |
+#include <string> |
+#include "base/bind.h" |
Sergey Ulanov
2014/09/22 23:42:58
empty line above this one
kelvinp
2014/09/23 20:16:41
Done.
|
+#include "base/callback.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "base/memory/weak_ptr.h" |
+#include "extensions/common/constants.h" |
+#include "extensions/common/url_pattern_set.h" |
+#include "ui/gfx/native_widget_types.h" |
+#include "url/gurl.h" |
+ |
+namespace extensions { |
+ |
+namespace { |
+ |
+typedef NativeMessageHost::Client Client; |
+typedef base::Callback<scoped_ptr<NativeMessageHost>( |
+ base::WeakPtr<Client> weak_client_ui, |
+ int destination_port)> NativeMessageHostFactoryMethod; |
+ |
+// A simple NativeMesageHost that echoes the received message. It is currently |
+// used for testing. |
+// TODO(kelvinp): Replace this class once Remote Assistance in process host |
+// is implemented. |
+class EchoHost : public NativeMessageHost { |
+ public: |
+ scoped_ptr<NativeMessageHost> static CreateEchoHost( |
+ base::WeakPtr<Client> weak_client_ui, |
+ int destination_port) { |
+ return scoped_ptr<NativeMessageHost>( |
+ new EchoHost(weak_client_ui, destination_port)); |
+ } |
+ |
+ EchoHost(base::WeakPtr<Client> client, int port) |
+ : client_(client), port_(port) {} |
+ |
+ virtual void Send(const std::string& json) OVERRIDE { |
+ client_->PostMessageFromNative(port_, json); |
+ }; |
+ |
+ private: |
+ base::WeakPtr<Client> client_; |
+ int port_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(EchoHost); |
+}; |
+ |
+// A data structure that encapsulates the factory method and the list of |
+// extensions that are allowed to connect to the NativeMessageHost. |
+class HostDescriptor { |
+ public: |
+ explicit HostDescriptor(NativeMessageHostFactoryMethod method); |
+ void AddAllowedOrigin(const std::string& pattern); |
+ |
+ URLPatternSet allowed_origins; |
+ NativeMessageHostFactoryMethod factory_method; |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(HostDescriptor); |
+}; |
+ |
+typedef std::map<std::string, HostDescriptor*> HostMap; |
Sergey Ulanov
2014/09/22 23:42:58
This all seems to be more complicated than it need
kelvinp
2014/09/23 20:16:41
Done.
|
+typedef std::pair<std::string, HostDescriptor*> Entry; |
+ |
+class NativeMessageHostFactory { |
+ public: |
+ NativeMessageHostFactory(); |
+ ~NativeMessageHostFactory(); |
+ scoped_ptr<NativeMessageHost> Create(base::WeakPtr<Client> weak_client_ui, |
+ const std::string& source_extension_id, |
+ const std::string& native_host_name, |
+ int destination_port); |
+ |
+ private: |
+ HostMap host_table_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(NativeMessageHostFactory); |
+}; |
+ |
+NativeMessageHostFactory* g_host_factory; |
Sergey Ulanov
2014/09/22 23:42:58
As mentioned above I don't think you need this. Bu
kelvinp
2014/09/23 20:16:41
Done.
|
+ |
+} // namespace |
+ |
+HostDescriptor::HostDescriptor(NativeMessageHostFactoryMethod method) |
+ : factory_method(method) { |
+} |
+ |
+void HostDescriptor::AddAllowedOrigin(const std::string& pattern) { |
+ URLPattern allowed_origin(URLPattern::SCHEME_ALL); |
+ DCHECK_EQ(URLPattern::PARSE_SUCCESS, allowed_origin.Parse(pattern)); |
+ allowed_origins.AddPattern(allowed_origin); |
+} |
+ |
+NativeMessageHostFactory::NativeMessageHostFactory() { |
+ // Initializes the host table for remote assistance. |
+ // TODO(kelvinp): Create the actual It2Me host. |
+ HostDescriptor* remote_assistance = |
+ new HostDescriptor(base::Bind(&EchoHost::CreateEchoHost)); |
+ remote_assistance->AddAllowedOrigin( |
+ "chrome-extension://ljacajndfccfgnfohlgkdphmbnpkjflk/"); |
Sergey Ulanov
2014/09/22 23:42:58
You need to whitelist all chromoting IDs. See remo
kelvinp
2014/09/23 20:16:41
Done.
|
+ host_table_.insert( |
+ Entry("com.google.chrome.remote_assistance", remote_assistance)); |
+} |
+ |
+scoped_ptr<NativeMessageHost> NativeMessageHostFactory::Create( |
+ base::WeakPtr<Client> weak_client_ui, |
+ const std::string& source_extension_id, |
+ const std::string& native_host_name, |
+ int destination_port) { |
+ // Look up the host descriptor based on the host name. |
+ HostMap::iterator it = host_table_.find(native_host_name); |
+ if (it == host_table_.end()) { |
+ return scoped_ptr<NativeMessageHost>(); |
+ } |
+ |
+ // Ensure only the white-listed extensions can connect to the native |
+ // component. |
+ HostDescriptor* descriptor = it->second; |
+ GURL origin(std::string(kExtensionScheme) + "://" + source_extension_id); |
+ if (!descriptor->allowed_origins.MatchesSecurityOrigin(origin)) { |
+ return scoped_ptr<NativeMessageHost>(); |
+ } |
+ |
+ // Create the Host. |
+ return descriptor->factory_method.Run(weak_client_ui, destination_port); |
+} |
+ |
+scoped_ptr<NativeMessageHost> NativeMessageHost::Create( |
+ gfx::NativeView native_view, |
+ base::WeakPtr<Client> weak_client_ui, |
+ const std::string& source_extension_id, |
+ const std::string& native_host_name, |
+ int destination_port, |
+ bool allow_user_level) { |
+ if (!g_host_factory) { |
+ g_host_factory = new NativeMessageHostFactory(); |
+ } |
+ |
+ return g_host_factory->Create( |
+ weak_client_ui, source_extension_id, native_host_name, destination_port); |
+} |
+ |
+} // namespace extensions |