| 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/bind.h" | |
| 10 #include "base/bind_helpers.h" | |
| 11 #include "base/command_line.h" | |
| 12 #include "base/json/json_reader.h" | |
| 13 #include "base/json/json_writer.h" | |
| 14 #include "base/location.h" | |
| 15 #include "base/macros.h" | |
| 16 #include "base/memory/scoped_ptr.h" | |
| 17 #include "base/message_loop/message_loop_proxy.h" | |
| 18 #include "base/values.h" | |
| 19 #include "chrome/browser/extensions/api/messaging/native_messaging_test_util.h" | |
| 20 #include "extensions/common/constants.h" | |
| 21 #include "extensions/common/url_pattern.h" | |
| 22 #include "ui/gfx/native_widget_types.h" | |
| 23 #include "url/gurl.h" | |
| 24 | |
| 25 namespace extensions { | |
| 26 | |
| 27 namespace { | |
| 28 | |
| 29 // A simple NativeMesageHost that echoes the received message. It is currently | |
| 30 // used for testing. | |
| 31 // TODO(kelvinp): Replace this class once Remote Assistance in process host | |
| 32 // is implemented. | |
| 33 | |
| 34 const char* const kEchoHostOrigins[] = { | |
| 35 // ScopedTestNativeMessagingHost::kExtensionId | |
| 36 "chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/"}; | |
| 37 | |
| 38 class EchoHost : public NativeMessageHost { | |
| 39 public: | |
| 40 static scoped_ptr<NativeMessageHost> Create() { | |
| 41 return scoped_ptr<NativeMessageHost>(new EchoHost()); | |
| 42 } | |
| 43 | |
| 44 EchoHost() : message_number_(0), client_(NULL) {} | |
| 45 | |
| 46 virtual void Start(Client* client) OVERRIDE { | |
| 47 client_ = client; | |
| 48 } | |
| 49 | |
| 50 virtual void OnMessage(const std::string& request_string) OVERRIDE { | |
| 51 scoped_ptr<base::Value> request_value( | |
| 52 base::JSONReader::Read(request_string)); | |
| 53 scoped_ptr<base::DictionaryValue> request( | |
| 54 static_cast<base::DictionaryValue*>(request_value.release())); | |
| 55 if (request_string.find("stopHostTest") != std::string::npos) { | |
| 56 client_->CloseChannel(kNativeHostExited); | |
| 57 } else if (request_string.find("bigMessageTest") != std::string::npos) { | |
| 58 client_->CloseChannel(kHostInputOuputError); | |
| 59 } else { | |
| 60 ProcessEcho(*request); | |
| 61 } | |
| 62 }; | |
| 63 | |
| 64 virtual scoped_refptr<base::SingleThreadTaskRunner> task_runner() | |
| 65 const OVERRIDE { | |
| 66 return base::MessageLoopProxy::current(); | |
| 67 }; | |
| 68 | |
| 69 private: | |
| 70 void ProcessEcho(const base::DictionaryValue& request) { | |
| 71 scoped_ptr<base::DictionaryValue> response(new base::DictionaryValue()); | |
| 72 response->SetInteger("id", ++message_number_); | |
| 73 response->Set("echo", request.DeepCopy()); | |
| 74 response->SetString("caller_url", kEchoHostOrigins[0]); | |
| 75 std::string response_string; | |
| 76 base::JSONWriter::Write(response.get(), &response_string); | |
| 77 client_->PostMessageFromNativeHost(response_string); | |
| 78 } | |
| 79 | |
| 80 int message_number_; | |
| 81 Client* client_; | |
| 82 | |
| 83 DISALLOW_COPY_AND_ASSIGN(EchoHost); | |
| 84 }; | |
| 85 | |
| 86 struct BuiltInHost { | |
| 87 const char* const name; | |
| 88 const char* const* const allowed_origins; | |
| 89 int allowed_origins_count; | |
| 90 scoped_ptr<NativeMessageHost>(*create_function)(); | |
| 91 }; | |
| 92 | |
| 93 // If you modify the list of allowed_origins, don't forget to update | |
| 94 // remoting/host/it2me/com.google.chrome.remote_assistance.json.jinja2 | |
| 95 // to keep the two lists in sync. | |
| 96 // TODO(kelvinp): Load the native messaging manifest as a resource file into | |
| 97 // chrome and fetch the list of allowed_origins from the manifest. | |
| 98 /*const char* const kRemotingIt2MeOrigins[] = { | |
| 99 "chrome-extension://ljacajndfccfgnfohlgkdphmbnpkjflk/", | |
| 100 "chrome-extension://gbchcmhmhahfdphkhkmpfmihenigjmpp/", | |
| 101 "chrome-extension://kgngmbheleoaphbjbaiobfdepmghbfah/", | |
| 102 "chrome-extension://odkaodonbgfohohmklejpjiejmcipmib/", | |
| 103 "chrome-extension://dokpleeekgeeiehdhmdkeimnkmoifgdd/", | |
| 104 "chrome-extension://ajoainacpilcemgiakehflpbkbfipojk/", | |
| 105 "chrome-extension://hmboipgjngjoiaeicfdifdoeacilalgc/"};*/ | |
| 106 | |
| 107 static const BuiltInHost kBuiltInHost[] = { | |
| 108 {"com.google.chrome.test.echo", // ScopedTestNativeMessagingHost::kHostName | |
| 109 kEchoHostOrigins, | |
| 110 arraysize(kEchoHostOrigins), | |
| 111 &EchoHost::Create}, | |
| 112 }; | |
| 113 | |
| 114 bool MatchesSecurityOrigin(const BuiltInHost& host, | |
| 115 const std::string& extension_id) { | |
| 116 GURL origin(std::string(kExtensionScheme) + "://" + extension_id); | |
| 117 for (int i = 0; i < host.allowed_origins_count; i++) { | |
| 118 URLPattern allowed_origin(URLPattern::SCHEME_ALL); | |
| 119 DCHECK_EQ(URLPattern::PARSE_SUCCESS, | |
| 120 allowed_origin.Parse(host.allowed_origins[i])); | |
| 121 if (allowed_origin.MatchesSecurityOrigin(origin)) { | |
| 122 return true; | |
| 123 } | |
| 124 } | |
| 125 return false; | |
| 126 } | |
| 127 | |
| 128 } // namespace | |
| 129 | |
| 130 scoped_ptr<NativeMessageHost> NativeMessageHost::Create( | |
| 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 scoped_ptr<NativeMessageHost> not_found; | |
| 137 for (unsigned int i = 0; i < arraysize(kBuiltInHost); i++) { | |
| 138 const BuiltInHost& host = kBuiltInHost[i]; | |
| 139 std::string name(host.name); | |
| 140 if (name == native_host_name) { | |
| 141 if (MatchesSecurityOrigin(host, source_extension_id)) { | |
| 142 return (*host.create_function)(); | |
| 143 } | |
| 144 *error = kForbiddenError; | |
| 145 return not_found; | |
| 146 } | |
| 147 } | |
| 148 *error = kNotFoundError; | |
| 149 return not_found; | |
| 150 } | |
| 151 | |
| 152 } // namespace extensions | |
| OLD | NEW |