OLD | NEW |
(Empty) | |
| 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 <string> |
| 8 |
| 9 #include "base/command_line.h" |
| 10 #include "base/macros.h" |
| 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/memory/weak_ptr.h" |
| 13 #include "base/message_loop/message_loop_proxy.h" |
| 14 #include "extensions/common/constants.h" |
| 15 #include "extensions/common/url_pattern.h" |
| 16 #include "ui/gfx/native_widget_types.h" |
| 17 #include "url/gurl.h" |
| 18 |
| 19 namespace extensions { |
| 20 |
| 21 namespace { |
| 22 |
| 23 // A simple NativeMesageHost that echoes the received message. It is currently |
| 24 // used for testing. |
| 25 // TODO(kelvinp): Replace this class once Remote Assistance in process host |
| 26 // is implemented. |
| 27 class EchoHost : public NativeMessageHost { |
| 28 public: |
| 29 static scoped_ptr<NativeMessageHost> CreateEchoHost() { |
| 30 return scoped_ptr<NativeMessageHost>(new EchoHost()); |
| 31 } |
| 32 |
| 33 EchoHost() {} |
| 34 |
| 35 virtual void set_client(base::WeakPtr<Client> client) OVERRIDE { |
| 36 client_ = client; |
| 37 } |
| 38 |
| 39 virtual void OnMessage(const std::string& json) OVERRIDE { |
| 40 client_->PostMessageFromNativeHost(json); |
| 41 }; |
| 42 |
| 43 virtual scoped_refptr<base::SingleThreadTaskRunner> task_runner() |
| 44 const OVERRIDE { |
| 45 return base::MessageLoopProxy::current(); |
| 46 }; |
| 47 |
| 48 private: |
| 49 base::WeakPtr<Client> client_; |
| 50 |
| 51 DISALLOW_COPY_AND_ASSIGN(EchoHost); |
| 52 }; |
| 53 |
| 54 struct BuiltInHost { |
| 55 const char* const name; |
| 56 const char* const* const allowed_origins; |
| 57 int allowed_origins_count; |
| 58 scoped_ptr<NativeMessageHost>(*create_function)(); |
| 59 }; |
| 60 |
| 61 // If you modify the list of allowed_origins, don't forget to update |
| 62 // remoting/host/it2me/com.google.chrome.remote_assistance.json.jinja2 |
| 63 // to keep the two lists in sync. |
| 64 // TODO(kelvinp): Load the native messaging manifest as a resource file into |
| 65 // chrome and fetch the list of allowed_origins from the manifest. |
| 66 const char* const kRemotingIt2MeOrigins[] = { |
| 67 "chrome-extension://ljacajndfccfgnfohlgkdphmbnpkjflk/", |
| 68 "chrome-extension://gbchcmhmhahfdphkhkmpfmihenigjmpp/", |
| 69 "chrome-extension://kgngmbheleoaphbjbaiobfdepmghbfah/", |
| 70 "chrome-extension://odkaodonbgfohohmklejpjiejmcipmib/", |
| 71 "chrome-extension://dokpleeekgeeiehdhmdkeimnkmoifgdd/", |
| 72 "chrome-extension://ajoainacpilcemgiakehflpbkbfipojk/", |
| 73 "chrome-extension://hmboipgjngjoiaeicfdifdoeacilalgc/"}; |
| 74 |
| 75 static const BuiltInHost kBuiltInHost[] = { |
| 76 {"com.google.chrome.remote_assistance", |
| 77 kRemotingIt2MeOrigins, |
| 78 arraysize(kRemotingIt2MeOrigins), |
| 79 &EchoHost::CreateEchoHost}, |
| 80 }; |
| 81 |
| 82 bool MatchesSecurityOrigin(const BuiltInHost& host, |
| 83 const std::string& extension_id) { |
| 84 GURL origin(std::string(kExtensionScheme) + "://" + extension_id); |
| 85 for (int i = 0; i < host.allowed_origins_count; i++) { |
| 86 URLPattern allowed_origin(URLPattern::SCHEME_ALL); |
| 87 DCHECK_EQ(URLPattern::PARSE_SUCCESS, |
| 88 allowed_origin.Parse(host.allowed_origins[i])); |
| 89 if (allowed_origin.MatchesSecurityOrigin(origin)) { |
| 90 return true; |
| 91 } |
| 92 } |
| 93 return false; |
| 94 } |
| 95 |
| 96 } // namespace |
| 97 |
| 98 scoped_ptr<NativeMessageHost> NativeMessageHost::Create( |
| 99 gfx::NativeView native_view, |
| 100 const std::string& source_extension_id, |
| 101 const std::string& native_host_name, |
| 102 bool allow_user_level) { |
| 103 for (unsigned int i = 0; i < arraysize(kBuiltInHost); i++) { |
| 104 const BuiltInHost& host = kBuiltInHost[i]; |
| 105 std::string name(host.name); |
| 106 if (name == native_host_name && |
| 107 MatchesSecurityOrigin(host, source_extension_id)) { |
| 108 return (*host.create_function)(); |
| 109 } |
| 110 } |
| 111 return scoped_ptr<NativeMessageHost>(); |
| 112 } |
| 113 |
| 114 } // namespace extensions |
OLD | NEW |