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/thread_task_runner_handle.h"
17 #include "base/thread_task_runner_handle.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 "content/public/browser/browser_thread.h"
23 #include "extensions/common/constants.h"
24 #include "extensions/common/url_pattern.h"
25 #include "net/url_request/url_request_context_getter.h"
26 #include "remoting/host/chromoting_host_context.h"
27 #include "remoting/host/it2me/it2me_native_messaging_host.h"
28 #include "ui/gfx/native_widget_types.h"
31 namespace extensions
{
35 // A simple NativeMessageHost that mimics the implementation of
36 // chrome/test/data/native_messaging/native_hosts/echo.py. It is currently
37 // used for testing by ExtensionApiTest::NativeMessagingBasic.
39 const char* const kEchoHostOrigins
[] = {
40 // ScopedTestNativeMessagingHost::kExtensionId
41 "chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/"};
43 class EchoHost
: public NativeMessageHost
{
45 static scoped_ptr
<NativeMessageHost
> Create() {
46 return scoped_ptr
<NativeMessageHost
>(new EchoHost());
49 EchoHost() : message_number_(0), client_(NULL
) {}
51 void Start(Client
* client
) override
{ client_
= client
; }
53 void OnMessage(const std::string
& request_string
) override
{
54 scoped_ptr
<base::Value
> request_value(
55 base::JSONReader::DeprecatedRead(request_string
));
56 scoped_ptr
<base::DictionaryValue
> request(
57 static_cast<base::DictionaryValue
*>(request_value
.release()));
58 if (request_string
.find("stopHostTest") != std::string::npos
) {
59 client_
->CloseChannel(kNativeHostExited
);
60 } else if (request_string
.find("bigMessageTest") != std::string::npos
) {
61 client_
->CloseChannel(kHostInputOuputError
);
63 ProcessEcho(*request
);
67 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner() const override
{
68 return base::ThreadTaskRunnerHandle::Get();
72 void ProcessEcho(const base::DictionaryValue
& request
) {
73 base::DictionaryValue response
;
74 response
.SetInteger("id", ++message_number_
);
75 response
.Set("echo", request
.CreateDeepCopy());
76 response
.SetString("caller_url", kEchoHostOrigins
[0]);
77 std::string response_string
;
78 base::JSONWriter::Write(response
, &response_string
);
79 client_
->PostMessageFromNativeHost(response_string
);
85 DISALLOW_COPY_AND_ASSIGN(EchoHost
);
89 const char* const name
;
90 const char* const* const allowed_origins
;
91 int allowed_origins_count
;
92 scoped_ptr
<NativeMessageHost
>(*create_function
)();
95 scoped_ptr
<NativeMessageHost
> CreateIt2MeHost() {
96 scoped_ptr
<remoting::It2MeHostFactory
> host_factory(
97 new remoting::It2MeHostFactory());
98 host_factory
->set_policy_service(g_browser_process
->policy_service());
99 scoped_ptr
<remoting::ChromotingHostContext
> context
=
100 remoting::ChromotingHostContext::CreateForChromeOS(
101 make_scoped_refptr(g_browser_process
->system_request_context()),
102 content::BrowserThread::GetMessageLoopProxyForThread(
103 content::BrowserThread::IO
),
104 content::BrowserThread::GetMessageLoopProxyForThread(
105 content::BrowserThread::UI
),
106 content::BrowserThread::GetMessageLoopProxyForThread(
107 content::BrowserThread::FILE));
108 scoped_ptr
<NativeMessageHost
> host(new remoting::It2MeNativeMessagingHost(
109 context
.Pass(), host_factory
.Pass()));
113 // If you modify the list of allowed_origins, don't forget to update
114 // remoting/host/it2me/com.google.chrome.remote_assistance.json.jinja2
115 // to keep the two lists in sync.
116 // TODO(kelvinp): Load the native messaging manifest as a resource file into
117 // chrome and fetch the list of allowed_origins from the manifest (see
119 const char* const kRemotingIt2MeOrigins
[] = {
120 "chrome-extension://ljacajndfccfgnfohlgkdphmbnpkjflk/",
121 "chrome-extension://gbchcmhmhahfdphkhkmpfmihenigjmpp/",
122 "chrome-extension://kgngmbheleoaphbjbaiobfdepmghbfah/",
123 "chrome-extension://odkaodonbgfohohmklejpjiejmcipmib/",
124 "chrome-extension://dokpleeekgeeiehdhmdkeimnkmoifgdd/",
125 "chrome-extension://ajoainacpilcemgiakehflpbkbfipojk/",
126 "chrome-extension://hmboipgjngjoiaeicfdifdoeacilalgc/"};
128 static const BuiltInHost kBuiltInHost
[] = {
129 {"com.google.chrome.test.echo", // ScopedTestNativeMessagingHost::kHostName
131 arraysize(kEchoHostOrigins
),
133 {"com.google.chrome.remote_assistance",
134 kRemotingIt2MeOrigins
,
135 arraysize(kRemotingIt2MeOrigins
),
139 bool MatchesSecurityOrigin(const BuiltInHost
& host
,
140 const std::string
& extension_id
) {
141 GURL
origin(std::string(kExtensionScheme
) + "://" + extension_id
);
142 for (int i
= 0; i
< host
.allowed_origins_count
; i
++) {
143 URLPattern
allowed_origin(URLPattern::SCHEME_ALL
, host
.allowed_origins
[i
]);
144 if (allowed_origin
.MatchesSecurityOrigin(origin
)) {
153 scoped_ptr
<NativeMessageHost
> NativeMessageHost::Create(
154 gfx::NativeView native_view
,
155 const std::string
& source_extension_id
,
156 const std::string
& native_host_name
,
157 bool allow_user_level
,
158 std::string
* error
) {
159 for (unsigned int i
= 0; i
< arraysize(kBuiltInHost
); i
++) {
160 const BuiltInHost
& host
= kBuiltInHost
[i
];
161 std::string
name(host
.name
);
162 if (name
== native_host_name
) {
163 if (MatchesSecurityOrigin(host
, source_extension_id
)) {
164 return (*host
.create_function
)();
166 *error
= kForbiddenError
;
170 *error
= kNotFoundError
;
174 } // namespace extensions