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/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"
30 namespace extensions
{
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
{
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
);
62 ProcessEcho(*request
);
66 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner() const override
{
67 return base::MessageLoopProxy::current();
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
);
84 DISALLOW_COPY_AND_ASSIGN(EchoHost
);
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()));
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
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
130 arraysize(kEchoHostOrigins
),
132 {"com.google.chrome.remote_assistance",
133 kRemotingIt2MeOrigins
,
134 arraysize(kRemotingIt2MeOrigins
),
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
)) {
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
;
169 *error
= kNotFoundError
;
173 } // namespace extensions