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 <memory>
8 #include <string>
9 #include <utility>
10 
11 #include "base/bind.h"
12 #include "base/callback_helpers.h"
13 #include "base/json/json_reader.h"
14 #include "base/json/json_writer.h"
15 #include "base/location.h"
16 #include "base/single_thread_task_runner.h"
17 #include "base/stl_util.h"
18 #include "base/threading/thread_task_runner_handle.h"
19 #include "base/values.h"
20 #include "chrome/browser/browser_process.h"
21 #include "chrome/browser/chromeos/arc/extensions/arc_support_message_host.h"
22 #include "chrome/browser/chromeos/drive/drivefs_native_message_host.h"
23 #include "chrome/browser/chromeos/wilco_dtc_supportd/wilco_dtc_supportd_messaging.h"
24 #include "content/public/browser/browser_task_traits.h"
25 #include "content/public/browser/browser_thread.h"
26 #include "extensions/common/constants.h"
27 #include "extensions/common/url_pattern.h"
28 #include "remoting/host/it2me/it2me_native_messaging_host_chromeos.h"
29 #include "ui/gfx/native_widget_types.h"
30 #include "url/gurl.h"
31 
32 namespace extensions {
33 
34 namespace {
35 
36 // A simple NativeMessageHost that mimics the implementation of
37 // chrome/test/data/native_messaging/native_hosts/echo.py. It is currently
38 // used for testing by ExtensionApiTest::NativeMessagingBasic.
39 
40 const char* const kEchoHostOrigins[] = {
41     // ScopedTestNativeMessagingHost::kExtensionId
42     "chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/"};
43 
44 class EchoHost : public NativeMessageHost {
45  public:
Create(content::BrowserContext * browser_context)46   static std::unique_ptr<NativeMessageHost> Create(
47       content::BrowserContext* browser_context) {
48     return std::make_unique<EchoHost>();
49   }
50 
51   EchoHost() = default;
52 
Start(Client * client)53   void Start(Client* client) override { client_ = client; }
54 
OnMessage(const std::string & request_string)55   void OnMessage(const std::string& request_string) override {
56     std::unique_ptr<base::Value> request_value =
57         base::JSONReader::ReadDeprecated(request_string);
58     std::unique_ptr<base::DictionaryValue> request(
59         static_cast<base::DictionaryValue*>(request_value.release()));
60     if (request_string.find("stopHostTest") != std::string::npos) {
61       client_->CloseChannel(kNativeHostExited);
62     } else if (request_string.find("bigMessageTest") != std::string::npos) {
63       client_->CloseChannel(kHostInputOutputError);
64     } else {
65       ProcessEcho(*request);
66     }
67   }
68 
task_runner() const69   scoped_refptr<base::SingleThreadTaskRunner> task_runner() const override {
70     return base::ThreadTaskRunnerHandle::Get();
71   }
72 
73  private:
ProcessEcho(const base::DictionaryValue & request)74   void ProcessEcho(const base::DictionaryValue& request) {
75     base::DictionaryValue response;
76     response.SetInteger("id", ++message_number_);
77     response.Set("echo", request.CreateDeepCopy());
78     response.SetString("caller_url", kEchoHostOrigins[0]);
79     std::string response_string;
80     base::JSONWriter::Write(response, &response_string);
81     client_->PostMessageFromNativeHost(response_string);
82   }
83 
84   int message_number_ = 0;
85   Client* client_ = nullptr;
86 
87   DISALLOW_COPY_AND_ASSIGN(EchoHost);
88 };
89 
90 struct BuiltInHost {
91   const char* const name;
92   const char* const* const allowed_origins;
93   int allowed_origins_count;
94   std::unique_ptr<NativeMessageHost> (*create_function)(
95       content::BrowserContext*);
96 };
97 
CreateIt2MeHost(content::BrowserContext * browser_context)98 std::unique_ptr<NativeMessageHost> CreateIt2MeHost(
99     content::BrowserContext* browser_context) {
100   return remoting::CreateIt2MeNativeMessagingHostForChromeOS(
101       content::GetIOThreadTaskRunner({}), content::GetUIThreadTaskRunner({}),
102       g_browser_process->policy_service());
103 }
104 
105 // If you modify the list of allowed_origins, don't forget to update
106 // remoting/host/it2me/com.google.chrome.remote_assistance.json.jinja2
107 // to keep the two lists in sync.
108 // TODO(kelvinp): Load the native messaging manifest as a resource file into
109 // chrome and fetch the list of allowed_origins from the manifest (see
110 // crbug/424743).
111 const char* const kRemotingIt2MeOrigins[] = {
112     "chrome-extension://inomeogfingihgjfjlpeplalcfajhgai/",
113     "chrome-extension://hpodccmdligbeohchckkeajbfohibipg/"};
114 
MatchesSecurityOrigin(const BuiltInHost & host,const std::string & extension_id)115 bool MatchesSecurityOrigin(const BuiltInHost& host,
116                            const std::string& extension_id) {
117   GURL origin(std::string(kExtensionScheme) + "://" + extension_id);
118   for (int i = 0; i < host.allowed_origins_count; i++) {
119     URLPattern allowed_origin(URLPattern::SCHEME_ALL, host.allowed_origins[i]);
120     if (allowed_origin.MatchesSecurityOrigin(origin)) {
121       return true;
122     }
123   }
124   return false;
125 }
126 
127 }  // namespace
128 
Create(content::BrowserContext * browser_context,gfx::NativeView native_view,const std::string & source_extension_id,const std::string & native_host_name,bool allow_user_level,std::string * error)129 std::unique_ptr<NativeMessageHost> NativeMessageHost::Create(
130     content::BrowserContext* browser_context,
131     gfx::NativeView native_view,
132     const std::string& source_extension_id,
133     const std::string& native_host_name,
134     bool allow_user_level,
135     std::string* error) {
136   static const BuiltInHost kBuiltInHosts[] = {
137       // ScopedTestNativeMessagingHost::kHostName
138       {"com.google.chrome.test.echo", kEchoHostOrigins,
139        base::size(kEchoHostOrigins), &EchoHost::Create},
140       {"com.google.chrome.remote_assistance", kRemotingIt2MeOrigins,
141        base::size(kRemotingIt2MeOrigins), &CreateIt2MeHost},
142       {arc::ArcSupportMessageHost::kHostName,
143        arc::ArcSupportMessageHost::kHostOrigin, 1,
144        &arc::ArcSupportMessageHost::Create},
145       {chromeos::kWilcoDtcSupportdUiMessageHost,
146        chromeos::kWilcoDtcSupportdHostOrigins,
147        chromeos::kWilcoDtcSupportdHostOriginsSize,
148        &chromeos::CreateExtensionOwnedWilcoDtcSupportdMessageHost},
149       {drive::kDriveFsNativeMessageHostName,
150        drive::kDriveFsNativeMessageHostOrigins,
151        drive::kDriveFsNativeMessageHostOriginsSize,
152        &drive::CreateDriveFsNativeMessageHost},
153   };
154 
155   for (const BuiltInHost& host : kBuiltInHosts) {
156     if (host.name == native_host_name) {
157       if (MatchesSecurityOrigin(host, source_extension_id)) {
158         return (*host.create_function)(browser_context);
159       }
160       *error = kForbiddenError;
161       return nullptr;
162     }
163   }
164   *error = kNotFoundError;
165   return nullptr;
166 }
167 
168 }  // namespace extensions
169