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.
5 #include "extensions/browser/api/messaging/native_message_host.h"
10 #include "base/bind_helpers.h"
11 #include "base/command_line.h"
12 #include "base/json/json_reader.h"
13 #include "base/json/json_writer.h"
14 #include "base/location.h"
15 #include "base/macros.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/message_loop/message_loop_proxy.h"
18 #include "base/values.h"
19 #include "chrome/browser/browser_process.h"
20 #include "chrome/browser/extensions/api/messaging/native_messaging_test_util.h"
21 #include "components/policy/core/common/policy_service.h"
22 #include "extensions/common/constants.h"
23 #include "extensions/common/switches.h"
24 #include "extensions/common/url_pattern.h"
25 #include "net/url_request/url_request_context_getter.h"
27 #include "remoting/host/chromoting_host_context.h"
28 #include "remoting/host/it2me/it2me_native_messaging_host.h"
29 # endif // defined(USE_X11)
30 #include "ui/gfx/native_widget_types.h"
33 namespace extensions {
37 // A simple NativeMessageHost that mimics the implementation of
38 // chrome/test/data/native_messaging/native_hosts/echo.py. It is currently
39 // used for testing by ExtensionApiTest::NativeMessagingBasic.
41 const char* const kEchoHostOrigins[] = {
42 // ScopedTestNativeMessagingHost::kExtensionId
43 "chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/"};
45 class EchoHost : public NativeMessageHost {
47 static scoped_ptr<NativeMessageHost> Create() {
48 return scoped_ptr<NativeMessageHost>(new EchoHost());
51 EchoHost() : message_number_(0), client_(NULL) {}
53 virtual void Start(Client* client) override {
57 virtual void OnMessage(const std::string& request_string) override {
58 scoped_ptr<base::Value> request_value(
59 base::JSONReader::Read(request_string));
60 scoped_ptr<base::DictionaryValue> request(
61 static_cast<base::DictionaryValue*>(request_value.release()));
62 if (request_string.find("stopHostTest") != std::string::npos) {
63 client_->CloseChannel(kNativeHostExited);
64 } else if (request_string.find("bigMessageTest") != std::string::npos) {
65 client_->CloseChannel(kHostInputOuputError);
67 ProcessEcho(*request);
71 virtual scoped_refptr<base::SingleThreadTaskRunner> task_runner()
73 return base::MessageLoopProxy::current();
77 void ProcessEcho(const base::DictionaryValue& request) {
78 scoped_ptr<base::DictionaryValue> response(new base::DictionaryValue());
79 response->SetInteger("id", ++message_number_);
80 response->Set("echo", request.DeepCopy());
81 response->SetString("caller_url", kEchoHostOrigins[0]);
82 std::string response_string;
83 base::JSONWriter::Write(response.get(), &response_string);
84 client_->PostMessageFromNativeHost(response_string);
90 DISALLOW_COPY_AND_ASSIGN(EchoHost);
94 const char* const name;
95 const char* const* const allowed_origins;
96 int allowed_origins_count;
97 scoped_ptr<NativeMessageHost>(*create_function)();
100 // Remote assistance currently only supports X11.
101 // TODO(kelvinp): Migrate to ozone once it is ready (crbug.com/426716).
103 scoped_ptr<NativeMessageHost> CreateIt2MeHost() {
104 if (CommandLine::ForCurrentProcess()->HasSwitch(
105 switches::kEnableRemoteAssistance)) {
106 scoped_ptr<remoting::It2MeHostFactory> host_factory(
107 new remoting::It2MeHostFactory());
108 host_factory->set_policy_service(g_browser_process->policy_service());
109 scoped_ptr<remoting::ChromotingHostContext> context =
110 remoting::ChromotingHostContext::CreateForChromeOS(
111 make_scoped_refptr(g_browser_process->system_request_context()));
112 scoped_ptr<NativeMessageHost> host(new remoting::It2MeNativeMessagingHost(
113 context.Pass(), host_factory.Pass()));
119 // If you modify the list of allowed_origins, don't forget to update
120 // remoting/host/it2me/com.google.chrome.remote_assistance.json.jinja2
121 // to keep the two lists in sync.
122 // TODO(kelvinp): Load the native messaging manifest as a resource file into
123 // chrome and fetch the list of allowed_origins from the manifest (see
125 const char* const kRemotingIt2MeOrigins[] = {
126 "chrome-extension://ljacajndfccfgnfohlgkdphmbnpkjflk/",
127 "chrome-extension://gbchcmhmhahfdphkhkmpfmihenigjmpp/",
128 "chrome-extension://kgngmbheleoaphbjbaiobfdepmghbfah/",
129 "chrome-extension://odkaodonbgfohohmklejpjiejmcipmib/",
130 "chrome-extension://dokpleeekgeeiehdhmdkeimnkmoifgdd/",
131 "chrome-extension://ajoainacpilcemgiakehflpbkbfipojk/",
132 "chrome-extension://hmboipgjngjoiaeicfdifdoeacilalgc/"};
133 #endif // defined(USE_X11)
135 static const BuiltInHost kBuiltInHost[] = {
136 {"com.google.chrome.test.echo", // ScopedTestNativeMessagingHost::kHostName
138 arraysize(kEchoHostOrigins),
141 {"com.google.chrome.remote_assistance",
142 kRemotingIt2MeOrigins,
143 arraysize(kRemotingIt2MeOrigins),
145 #endif // defined(USE_X11)
148 bool MatchesSecurityOrigin(const BuiltInHost& host,
149 const std::string& extension_id) {
150 GURL origin(std::string(kExtensionScheme) + "://" + extension_id);
151 for (int i = 0; i < host.allowed_origins_count; i++) {
152 URLPattern allowed_origin(URLPattern::SCHEME_ALL, host.allowed_origins[i]);
153 if (allowed_origin.MatchesSecurityOrigin(origin)) {
162 scoped_ptr<NativeMessageHost> NativeMessageHost::Create(
163 gfx::NativeView native_view,
164 const std::string& source_extension_id,
165 const std::string& native_host_name,
166 bool allow_user_level,
167 std::string* error) {
168 for (unsigned int i = 0; i < arraysize(kBuiltInHost); i++) {
169 const BuiltInHost& host = kBuiltInHost[i];
170 std::string name(host.name);
171 if (name == native_host_name) {
172 if (MatchesSecurityOrigin(host, source_extension_id)) {
173 return (*host.create_function)();
175 *error = kForbiddenError;
179 *error = kNotFoundError;
183 } // namespace extensions