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..076173451d5bbbf0d5a3eda4c65c7055e5524e28 |
| --- /dev/null |
| +++ b/chrome/browser/extensions/api/messaging/native_message_host_chromeos.cc |
| @@ -0,0 +1,119 @@ |
| +// 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 "extensions/common/constants.h" |
| +#include "extensions/common/switches.h" |
| +#include "extensions/common/url_pattern.h" |
| +#include "ui/gfx/native_widget_types.h" |
| +#include "url/gurl.h" |
| + |
| +namespace extensions { |
| + |
| +namespace { |
| + |
| +typedef NativeMessageHost::Client Client; |
|
Sergey Ulanov
2014/09/27 00:24:10
Don't need this typedef. EchoHost inherits NativeM
kelvinp
2014/09/29 22:59:40
Done.
|
| + |
| +// 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() {} |
| + |
| + virtual void set_client(base::WeakPtr<Client> client) OVERRIDE { |
| + client_ = client; |
| + } |
| + |
| + virtual void Send(const std::string& json) OVERRIDE { |
| + client_->PostMessageFromNativeHost(json); |
| + }; |
| + |
| + private: |
| + base::WeakPtr<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) { |
| + scoped_ptr<NativeMessageHost> not_found; |
| + |
| + if (!CommandLine::ForCurrentProcess() |
| + ->HasSwitch(switches::kEnableRemoteAssistance)) { |
|
Sergey Ulanov
2014/09/27 00:24:10
I think it makes more sense to put this in the fun
kelvinp
2014/09/29 22:59:40
Done.
|
| + return not_found.Pass(); |
| + } |
| + |
| + 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 not_found.Pass(); |
| +} |
| + |
| +} // namespace extensions |