Chromium Code Reviews| 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 "extensions/common/constants.h" | |
| 14 #include "extensions/common/switches.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 typedef NativeMessageHost::Client Client; | |
| 24 | |
| 25 // A simple NativeMesageHost that echoes the received message. It is currently | |
| 26 // used for testing. | |
| 27 // TODO(kelvinp): Replace this class once Remote Assistance in process host | |
| 28 // is implemented. | |
| 29 class EchoHost : public NativeMessageHost { | |
| 30 public: | |
| 31 scoped_ptr<NativeMessageHost> static CreateEchoHost( | |
| 32 base::WeakPtr<Client> weak_client, | |
| 33 int destination_port) { | |
| 34 return scoped_ptr<NativeMessageHost>( | |
| 35 new EchoHost(weak_client, destination_port)); | |
| 36 } | |
| 37 | |
| 38 EchoHost(base::WeakPtr<Client> client, int port) | |
| 39 : client_(client), port_(port) {} | |
| 40 | |
| 41 virtual void Send(const std::string& json) OVERRIDE { | |
| 42 client_->PostMessageFromNativeHost(port_, json); | |
| 43 }; | |
| 44 | |
| 45 private: | |
| 46 base::WeakPtr<Client> client_; | |
| 47 int port_; | |
| 48 | |
| 49 DISALLOW_COPY_AND_ASSIGN(EchoHost); | |
| 50 }; | |
| 51 | |
| 52 struct BuiltInHost { | |
| 53 const char* const name; | |
| 54 const char* const* const allowed_origins; | |
| 55 int allowed_origins_count; | |
| 56 scoped_ptr<NativeMessageHost>(*create_function)(base::WeakPtr<Client>, | |
| 57 int port); | |
| 58 }; | |
| 59 | |
| 60 // If you modify the list of allowed_origins, don't forget to update | |
| 61 // remoting/host/it2me/com.google.chrome.remote_assistance.json.jinja2 | |
| 62 // to keep the two lists in sync. | |
| 63 // TODO(kelvinp): Load the native messaging manifest as a resource file into | |
| 64 // chrome and fetch the list of allowed_origins from the manifest. | |
| 65 const char* const kRemotingIt2MeOrigins[] = { | |
| 66 "chrome-extension://ljacajndfccfgnfohlgkdphmbnpkjflk/", | |
| 67 "chrome-extension://gbchcmhmhahfdphkhkmpfmihenigjmpp/", | |
| 68 "chrome-extension://kgngmbheleoaphbjbaiobfdepmghbfah/", | |
| 69 "chrome-extension://odkaodonbgfohohmklejpjiejmcipmib/", | |
| 70 "chrome-extension://ojoimpklfciegopdfgeenehpalipignm/", | |
| 71 "chrome-extension://dokpleeekgeeiehdhmdkeimnkmoifgdd/", | |
| 72 "chrome-extension://ajoainacpilcemgiakehflpbkbfipojk/", | |
| 73 "chrome-extension://hmboipgjngjoiaeicfdifdoeacilalgc/", | |
| 74 "chrome-extension://dhdboenlgbbeohfjeboeooomjmgbejoh/"}; | |
|
Sergey Ulanov
2014/09/25 01:45:22
This list is different what we have in com.google.
kelvinp
2014/09/26 18:12:27
Done.
| |
| 75 | |
| 76 static const BuiltInHost kBuiltInHost[] = { | |
| 77 {"com.google.chrome.remote_assistance", | |
| 78 kRemotingIt2MeOrigins, | |
| 79 arraysize(kRemotingIt2MeOrigins), | |
| 80 &EchoHost::CreateEchoHost}}; | |
|
Sergey Ulanov
2014/09/25 01:45:22
Given that this CL contains changes for the It2Me
kelvinp
2014/09/26 18:12:27
It2Me host would crash on launching now. We still
| |
| 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 base::WeakPtr<Client> weak_client, | |
| 101 const std::string& source_extension_id, | |
| 102 const std::string& native_host_name, | |
| 103 int destination_port, | |
| 104 bool allow_user_level) { | |
| 105 DCHECK(weak_client); | |
| 106 scoped_ptr<NativeMessageHost> not_found; | |
| 107 | |
| 108 if (!CommandLine::ForCurrentProcess() | |
| 109 ->HasSwitch(switches::kEnableRemoteAssistance)) { | |
| 110 return not_found.Pass(); | |
| 111 } | |
| 112 | |
| 113 for (unsigned int i = 0; i < arraysize(kBuiltInHost); i++) { | |
| 114 const BuiltInHost& host = kBuiltInHost[i]; | |
| 115 std::string name(host.name); | |
| 116 if (name == native_host_name && | |
| 117 MatchesSecurityOrigin(host, source_extension_id)) { | |
| 118 return (*host.create_function)(weak_client, destination_port); | |
| 119 } | |
| 120 } | |
| 121 | |
| 122 return not_found.Pass(); | |
| 123 } | |
| 124 | |
| 125 } // namespace extensions | |
| OLD | NEW |