Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "extensions/browser/api/messaging/native_message_host.h" | |
| 6 | |
| 7 #include <map> | |
| 8 #include <string> | |
| 9 #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.
| |
| 10 #include "base/callback.h" | |
| 11 #include "base/memory/scoped_ptr.h" | |
| 12 #include "base/memory/weak_ptr.h" | |
| 13 #include "extensions/common/constants.h" | |
| 14 #include "extensions/common/url_pattern_set.h" | |
| 15 #include "ui/gfx/native_widget_types.h" | |
| 16 #include "url/gurl.h" | |
| 17 | |
| 18 namespace extensions { | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 typedef NativeMessageHost::Client Client; | |
| 23 typedef base::Callback<scoped_ptr<NativeMessageHost>( | |
| 24 base::WeakPtr<Client> weak_client_ui, | |
| 25 int destination_port)> NativeMessageHostFactoryMethod; | |
| 26 | |
| 27 // A simple NativeMesageHost that echoes the received message. It is currently | |
| 28 // used for testing. | |
| 29 // TODO(kelvinp): Replace this class once Remote Assistance in process host | |
| 30 // is implemented. | |
| 31 class EchoHost : public NativeMessageHost { | |
| 32 public: | |
| 33 scoped_ptr<NativeMessageHost> static CreateEchoHost( | |
| 34 base::WeakPtr<Client> weak_client_ui, | |
| 35 int destination_port) { | |
| 36 return scoped_ptr<NativeMessageHost>( | |
| 37 new EchoHost(weak_client_ui, destination_port)); | |
| 38 } | |
| 39 | |
| 40 EchoHost(base::WeakPtr<Client> client, int port) | |
| 41 : client_(client), port_(port) {} | |
| 42 | |
| 43 virtual void Send(const std::string& json) OVERRIDE { | |
| 44 client_->PostMessageFromNative(port_, json); | |
| 45 }; | |
| 46 | |
| 47 private: | |
| 48 base::WeakPtr<Client> client_; | |
| 49 int port_; | |
| 50 | |
| 51 DISALLOW_COPY_AND_ASSIGN(EchoHost); | |
| 52 }; | |
| 53 | |
| 54 // A data structure that encapsulates the factory method and the list of | |
| 55 // extensions that are allowed to connect to the NativeMessageHost. | |
| 56 class HostDescriptor { | |
| 57 public: | |
| 58 explicit HostDescriptor(NativeMessageHostFactoryMethod method); | |
| 59 void AddAllowedOrigin(const std::string& pattern); | |
| 60 | |
| 61 URLPatternSet allowed_origins; | |
| 62 NativeMessageHostFactoryMethod factory_method; | |
| 63 | |
| 64 private: | |
| 65 DISALLOW_COPY_AND_ASSIGN(HostDescriptor); | |
| 66 }; | |
| 67 | |
| 68 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.
| |
| 69 typedef std::pair<std::string, HostDescriptor*> Entry; | |
| 70 | |
| 71 class NativeMessageHostFactory { | |
| 72 public: | |
| 73 NativeMessageHostFactory(); | |
| 74 ~NativeMessageHostFactory(); | |
| 75 scoped_ptr<NativeMessageHost> Create(base::WeakPtr<Client> weak_client_ui, | |
| 76 const std::string& source_extension_id, | |
| 77 const std::string& native_host_name, | |
| 78 int destination_port); | |
| 79 | |
| 80 private: | |
| 81 HostMap host_table_; | |
| 82 | |
| 83 DISALLOW_COPY_AND_ASSIGN(NativeMessageHostFactory); | |
| 84 }; | |
| 85 | |
| 86 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.
| |
| 87 | |
| 88 } // namespace | |
| 89 | |
| 90 HostDescriptor::HostDescriptor(NativeMessageHostFactoryMethod method) | |
| 91 : factory_method(method) { | |
| 92 } | |
| 93 | |
| 94 void HostDescriptor::AddAllowedOrigin(const std::string& pattern) { | |
| 95 URLPattern allowed_origin(URLPattern::SCHEME_ALL); | |
| 96 DCHECK_EQ(URLPattern::PARSE_SUCCESS, allowed_origin.Parse(pattern)); | |
| 97 allowed_origins.AddPattern(allowed_origin); | |
| 98 } | |
| 99 | |
| 100 NativeMessageHostFactory::NativeMessageHostFactory() { | |
| 101 // Initializes the host table for remote assistance. | |
| 102 // TODO(kelvinp): Create the actual It2Me host. | |
| 103 HostDescriptor* remote_assistance = | |
| 104 new HostDescriptor(base::Bind(&EchoHost::CreateEchoHost)); | |
| 105 remote_assistance->AddAllowedOrigin( | |
| 106 "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.
| |
| 107 host_table_.insert( | |
| 108 Entry("com.google.chrome.remote_assistance", remote_assistance)); | |
| 109 } | |
| 110 | |
| 111 scoped_ptr<NativeMessageHost> NativeMessageHostFactory::Create( | |
| 112 base::WeakPtr<Client> weak_client_ui, | |
| 113 const std::string& source_extension_id, | |
| 114 const std::string& native_host_name, | |
| 115 int destination_port) { | |
| 116 // Look up the host descriptor based on the host name. | |
| 117 HostMap::iterator it = host_table_.find(native_host_name); | |
| 118 if (it == host_table_.end()) { | |
| 119 return scoped_ptr<NativeMessageHost>(); | |
| 120 } | |
| 121 | |
| 122 // Ensure only the white-listed extensions can connect to the native | |
| 123 // component. | |
| 124 HostDescriptor* descriptor = it->second; | |
| 125 GURL origin(std::string(kExtensionScheme) + "://" + source_extension_id); | |
| 126 if (!descriptor->allowed_origins.MatchesSecurityOrigin(origin)) { | |
| 127 return scoped_ptr<NativeMessageHost>(); | |
| 128 } | |
| 129 | |
| 130 // Create the Host. | |
| 131 return descriptor->factory_method.Run(weak_client_ui, destination_port); | |
| 132 } | |
| 133 | |
| 134 scoped_ptr<NativeMessageHost> NativeMessageHost::Create( | |
| 135 gfx::NativeView native_view, | |
| 136 base::WeakPtr<Client> weak_client_ui, | |
| 137 const std::string& source_extension_id, | |
| 138 const std::string& native_host_name, | |
| 139 int destination_port, | |
| 140 bool allow_user_level) { | |
| 141 if (!g_host_factory) { | |
| 142 g_host_factory = new NativeMessageHostFactory(); | |
| 143 } | |
| 144 | |
| 145 return g_host_factory->Create( | |
| 146 weak_client_ui, source_extension_id, native_host_name, destination_port); | |
| 147 } | |
| 148 | |
| 149 } // namespace extensions | |
| OLD | NEW |