Chromium Code Reviews| Index: chrome/browser/extensions/api/messaging/native_message_host_chromeos.cc |
| diff --git a/chrome/browser/extensions/api/messaging/native_message_host_chromeos.cc b/chrome/browser/extensions/api/messaging/native_message_host_chromeos.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9ad52a96e1d959b31689863048fa54362b80c7c7 |
| --- /dev/null |
| +++ b/chrome/browser/extensions/api/messaging/native_message_host_chromeos.cc |
| @@ -0,0 +1,114 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "extensions/browser/api/messaging/native_message_host.h" |
| + |
| +#include <string> |
| + |
| +#include "base/command_line.h" |
| +#include "base/macros.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/memory/weak_ptr.h" |
| +#include "base/message_loop/message_loop_proxy.h" |
| +#include "extensions/common/constants.h" |
| +#include "extensions/common/url_pattern.h" |
| +#include "ui/gfx/native_widget_types.h" |
| +#include "url/gurl.h" |
| + |
| +namespace extensions { |
| + |
| +namespace { |
| + |
| +// A simple NativeMesageHost that echoes the received message. It is currently |
| +// used for testing. |
| +// TODO(kelvinp): Replace this class once Remote Assistance in process host |
| +// is implemented. |
| +class EchoHost : public NativeMessageHost { |
| + public: |
| + static scoped_ptr<NativeMessageHost> CreateEchoHost() { |
| + return scoped_ptr<NativeMessageHost>(new EchoHost()); |
| + } |
| + |
| + EchoHost() {} |
|
Sergey Ulanov
2014/10/01 23:23:58
initialize client_ to NULL.
kelvinp
2014/10/02 03:12:16
Done.
|
| + |
| + virtual void set_client(Client* client) OVERRIDE { |
| + client_ = client; |
| + } |
| + |
| + virtual void OnMessage(const std::string& json) OVERRIDE { |
| + client_->PostMessageFromNativeHost(json); |
| + }; |
| + |
| + virtual scoped_refptr<base::SingleThreadTaskRunner> task_runner() |
| + const OVERRIDE { |
| + return base::MessageLoopProxy::current(); |
| + }; |
| + |
| + private: |
| + Client* client_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(EchoHost); |
| +}; |
| + |
| +struct BuiltInHost { |
| + const char* const name; |
| + const char* const* const allowed_origins; |
| + int allowed_origins_count; |
| + scoped_ptr<NativeMessageHost>(*create_function)(); |
| +}; |
| + |
| +// If you modify the list of allowed_origins, don't forget to update |
| +// remoting/host/it2me/com.google.chrome.remote_assistance.json.jinja2 |
| +// to keep the two lists in sync. |
| +// TODO(kelvinp): Load the native messaging manifest as a resource file into |
| +// chrome and fetch the list of allowed_origins from the manifest. |
| +const char* const kRemotingIt2MeOrigins[] = { |
| + "chrome-extension://ljacajndfccfgnfohlgkdphmbnpkjflk/", |
| + "chrome-extension://gbchcmhmhahfdphkhkmpfmihenigjmpp/", |
| + "chrome-extension://kgngmbheleoaphbjbaiobfdepmghbfah/", |
| + "chrome-extension://odkaodonbgfohohmklejpjiejmcipmib/", |
| + "chrome-extension://dokpleeekgeeiehdhmdkeimnkmoifgdd/", |
| + "chrome-extension://ajoainacpilcemgiakehflpbkbfipojk/", |
| + "chrome-extension://hmboipgjngjoiaeicfdifdoeacilalgc/"}; |
| + |
| +static const BuiltInHost kBuiltInHost[] = { |
| + {"com.google.chrome.remote_assistance", |
| + kRemotingIt2MeOrigins, |
| + arraysize(kRemotingIt2MeOrigins), |
| + &EchoHost::CreateEchoHost}, |
| +}; |
| + |
| +bool MatchesSecurityOrigin(const BuiltInHost& host, |
| + const std::string& extension_id) { |
| + GURL origin(std::string(kExtensionScheme) + "://" + extension_id); |
| + for (int i = 0; i < host.allowed_origins_count; i++) { |
| + URLPattern allowed_origin(URLPattern::SCHEME_ALL); |
| + DCHECK_EQ(URLPattern::PARSE_SUCCESS, |
| + allowed_origin.Parse(host.allowed_origins[i])); |
| + if (allowed_origin.MatchesSecurityOrigin(origin)) { |
| + return true; |
| + } |
| + } |
| + return false; |
| +} |
| + |
| +} // namespace |
| + |
| +scoped_ptr<NativeMessageHost> NativeMessageHost::Create( |
| + gfx::NativeView native_view, |
| + const std::string& source_extension_id, |
| + const std::string& native_host_name, |
| + bool allow_user_level) { |
| + for (unsigned int i = 0; i < arraysize(kBuiltInHost); i++) { |
| + const BuiltInHost& host = kBuiltInHost[i]; |
| + std::string name(host.name); |
| + if (name == native_host_name && |
| + MatchesSecurityOrigin(host, source_extension_id)) { |
| + return (*host.create_function)(); |
| + } |
| + } |
| + return scoped_ptr<NativeMessageHost>(); |
| +} |
| + |
| +} // namespace extensions |