Add ENABLE_MEDIA_ROUTER define to builds other than Android and iOS.
[chromium-blink-merge.git] / chrome / browser / extensions / api / messaging / native_message_host_chromeos.cc
blob416db2980d44e11546a874a9d2e40d65126aab92
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"
7 #include <string>
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/json/json_reader.h"
12 #include "base/json/json_writer.h"
13 #include "base/location.h"
14 #include "base/macros.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/message_loop/message_loop_proxy.h"
17 #include "base/values.h"
18 #include "chrome/browser/browser_process.h"
19 #include "chrome/browser/extensions/api/messaging/native_messaging_test_util.h"
20 #include "components/policy/core/common/policy_service.h"
21 #include "content/public/browser/browser_thread.h"
22 #include "extensions/common/constants.h"
23 #include "extensions/common/url_pattern.h"
24 #include "net/url_request/url_request_context_getter.h"
25 #include "remoting/host/chromoting_host_context.h"
26 #include "remoting/host/it2me/it2me_native_messaging_host.h"
27 #include "ui/gfx/native_widget_types.h"
28 #include "url/gurl.h"
30 namespace extensions {
32 namespace {
34 // A simple NativeMessageHost that mimics the implementation of
35 // chrome/test/data/native_messaging/native_hosts/echo.py. It is currently
36 // used for testing by ExtensionApiTest::NativeMessagingBasic.
38 const char* const kEchoHostOrigins[] = {
39 // ScopedTestNativeMessagingHost::kExtensionId
40 "chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/"};
42 class EchoHost : public NativeMessageHost {
43 public:
44 static scoped_ptr<NativeMessageHost> Create() {
45 return scoped_ptr<NativeMessageHost>(new EchoHost());
48 EchoHost() : message_number_(0), client_(NULL) {}
50 void Start(Client* client) override { client_ = client; }
52 void OnMessage(const std::string& request_string) override {
53 scoped_ptr<base::Value> request_value(
54 base::JSONReader::Read(request_string));
55 scoped_ptr<base::DictionaryValue> request(
56 static_cast<base::DictionaryValue*>(request_value.release()));
57 if (request_string.find("stopHostTest") != std::string::npos) {
58 client_->CloseChannel(kNativeHostExited);
59 } else if (request_string.find("bigMessageTest") != std::string::npos) {
60 client_->CloseChannel(kHostInputOuputError);
61 } else {
62 ProcessEcho(*request);
66 scoped_refptr<base::SingleThreadTaskRunner> task_runner() const override {
67 return base::MessageLoopProxy::current();
70 private:
71 void ProcessEcho(const base::DictionaryValue& request) {
72 scoped_ptr<base::DictionaryValue> response(new base::DictionaryValue());
73 response->SetInteger("id", ++message_number_);
74 response->Set("echo", request.DeepCopy());
75 response->SetString("caller_url", kEchoHostOrigins[0]);
76 std::string response_string;
77 base::JSONWriter::Write(response.get(), &response_string);
78 client_->PostMessageFromNativeHost(response_string);
81 int message_number_;
82 Client* client_;
84 DISALLOW_COPY_AND_ASSIGN(EchoHost);
87 struct BuiltInHost {
88 const char* const name;
89 const char* const* const allowed_origins;
90 int allowed_origins_count;
91 scoped_ptr<NativeMessageHost>(*create_function)();
94 scoped_ptr<NativeMessageHost> CreateIt2MeHost() {
95 scoped_ptr<remoting::It2MeHostFactory> host_factory(
96 new remoting::It2MeHostFactory());
97 host_factory->set_policy_service(g_browser_process->policy_service());
98 scoped_ptr<remoting::ChromotingHostContext> context =
99 remoting::ChromotingHostContext::CreateForChromeOS(
100 make_scoped_refptr(g_browser_process->system_request_context()),
101 content::BrowserThread::GetMessageLoopProxyForThread(
102 content::BrowserThread::IO),
103 content::BrowserThread::GetMessageLoopProxyForThread(
104 content::BrowserThread::UI),
105 content::BrowserThread::GetMessageLoopProxyForThread(
106 content::BrowserThread::FILE));
107 scoped_ptr<NativeMessageHost> host(new remoting::It2MeNativeMessagingHost(
108 context.Pass(), host_factory.Pass()));
109 return host.Pass();
112 // If you modify the list of allowed_origins, don't forget to update
113 // remoting/host/it2me/com.google.chrome.remote_assistance.json.jinja2
114 // to keep the two lists in sync.
115 // TODO(kelvinp): Load the native messaging manifest as a resource file into
116 // chrome and fetch the list of allowed_origins from the manifest (see
117 // crbug/424743).
118 const char* const kRemotingIt2MeOrigins[] = {
119 "chrome-extension://ljacajndfccfgnfohlgkdphmbnpkjflk/",
120 "chrome-extension://gbchcmhmhahfdphkhkmpfmihenigjmpp/",
121 "chrome-extension://kgngmbheleoaphbjbaiobfdepmghbfah/",
122 "chrome-extension://odkaodonbgfohohmklejpjiejmcipmib/",
123 "chrome-extension://dokpleeekgeeiehdhmdkeimnkmoifgdd/",
124 "chrome-extension://ajoainacpilcemgiakehflpbkbfipojk/",
125 "chrome-extension://hmboipgjngjoiaeicfdifdoeacilalgc/"};
127 static const BuiltInHost kBuiltInHost[] = {
128 {"com.google.chrome.test.echo", // ScopedTestNativeMessagingHost::kHostName
129 kEchoHostOrigins,
130 arraysize(kEchoHostOrigins),
131 &EchoHost::Create},
132 {"com.google.chrome.remote_assistance",
133 kRemotingIt2MeOrigins,
134 arraysize(kRemotingIt2MeOrigins),
135 &CreateIt2MeHost},
138 bool MatchesSecurityOrigin(const BuiltInHost& host,
139 const std::string& extension_id) {
140 GURL origin(std::string(kExtensionScheme) + "://" + extension_id);
141 for (int i = 0; i < host.allowed_origins_count; i++) {
142 URLPattern allowed_origin(URLPattern::SCHEME_ALL, host.allowed_origins[i]);
143 if (allowed_origin.MatchesSecurityOrigin(origin)) {
144 return true;
147 return false;
150 } // namespace
152 scoped_ptr<NativeMessageHost> NativeMessageHost::Create(
153 gfx::NativeView native_view,
154 const std::string& source_extension_id,
155 const std::string& native_host_name,
156 bool allow_user_level,
157 std::string* error) {
158 for (unsigned int i = 0; i < arraysize(kBuiltInHost); i++) {
159 const BuiltInHost& host = kBuiltInHost[i];
160 std::string name(host.name);
161 if (name == native_host_name) {
162 if (MatchesSecurityOrigin(host, source_extension_id)) {
163 return (*host.create_function)();
165 *error = kForbiddenError;
166 return nullptr;
169 *error = kNotFoundError;
170 return nullptr;
173 } // namespace extensions