Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "extensions/browser/api/messaging/native_message_host.h" | 5 #include "extensions/browser/api/messaging/native_message_host.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/json/json_reader.h" | 12 #include "base/json/json_reader.h" |
| 13 #include "base/json/json_writer.h" | 13 #include "base/json/json_writer.h" |
| 14 #include "base/location.h" | 14 #include "base/location.h" |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
| 17 #include "base/message_loop/message_loop_proxy.h" | 17 #include "base/message_loop/message_loop_proxy.h" |
| 18 #include "base/values.h" | 18 #include "base/values.h" |
| 19 #include "chrome/browser/browser_process.h" | |
| 19 #include "chrome/browser/extensions/api/messaging/native_messaging_test_util.h" | 20 #include "chrome/browser/extensions/api/messaging/native_messaging_test_util.h" |
| 21 #include "components/policy/core/common/policy_service.h" | |
| 20 #include "extensions/common/constants.h" | 22 #include "extensions/common/constants.h" |
| 23 #include "extensions/common/switches.h" | |
| 21 #include "extensions/common/url_pattern.h" | 24 #include "extensions/common/url_pattern.h" |
| 25 #include "net/url_request/url_request_context_getter.h" | |
| 26 #include "remoting/host/chromoting_host_context.h" | |
| 27 #include "remoting/host/it2me/it2me_native_messaging_host.h" | |
| 22 #include "ui/gfx/native_widget_types.h" | 28 #include "ui/gfx/native_widget_types.h" |
| 23 #include "url/gurl.h" | 29 #include "url/gurl.h" |
| 24 | 30 |
| 25 namespace extensions { | 31 namespace extensions { |
| 26 | 32 |
| 27 namespace { | 33 namespace { |
| 28 | 34 |
| 29 // A simple NativeMesageHost that echoes the received message. It is currently | 35 // A simple NativeMessageHost that mimics the implementation of |
| 30 // used for testing. | 36 // chrome/test/data/native_messaging/native_hosts/echo.py. It is currently |
| 31 // TODO(kelvinp): Replace this class once Remote Assistance in process host | 37 // used for testing by ExtensionApiTest::NativeMessagingBasic. |
| 32 // is implemented. | |
| 33 | 38 |
| 34 const char* const kEchoHostOrigins[] = { | 39 const char* const kEchoHostOrigins[] = { |
| 35 // ScopedTestNativeMessagingHost::kExtensionId | 40 // ScopedTestNativeMessagingHost::kExtensionId |
| 36 "chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/"}; | 41 "chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/"}; |
| 37 | 42 |
| 38 class EchoHost : public NativeMessageHost { | 43 class EchoHost : public NativeMessageHost { |
| 39 public: | 44 public: |
| 40 static scoped_ptr<NativeMessageHost> Create() { | 45 static scoped_ptr<NativeMessageHost> Create() { |
| 41 return scoped_ptr<NativeMessageHost>(new EchoHost()); | 46 return scoped_ptr<NativeMessageHost>(new EchoHost()); |
| 42 } | 47 } |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 83 DISALLOW_COPY_AND_ASSIGN(EchoHost); | 88 DISALLOW_COPY_AND_ASSIGN(EchoHost); |
| 84 }; | 89 }; |
| 85 | 90 |
| 86 struct BuiltInHost { | 91 struct BuiltInHost { |
| 87 const char* const name; | 92 const char* const name; |
| 88 const char* const* const allowed_origins; | 93 const char* const* const allowed_origins; |
| 89 int allowed_origins_count; | 94 int allowed_origins_count; |
| 90 scoped_ptr<NativeMessageHost>(*create_function)(); | 95 scoped_ptr<NativeMessageHost>(*create_function)(); |
| 91 }; | 96 }; |
| 92 | 97 |
| 98 scoped_ptr<NativeMessageHost> CreateIt2MeHost() { | |
| 99 if (CommandLine::ForCurrentProcess()->HasSwitch( | |
| 100 switches::kEnableRemoteAssistance)) { | |
| 101 scoped_ptr<remoting::It2MeHostFactory> host_factory( | |
| 102 new remoting::It2MeHostFactory(g_browser_process->policy_service())); | |
| 103 scoped_ptr<remoting::ChromotingHostContext> context = | |
| 104 remoting::ChromotingHostContext::CreateForChromeOS( | |
| 105 make_scoped_refptr(g_browser_process->system_request_context())); | |
| 106 // This is a hack. AutoThreadTaskRunner is a TaskRunner with the special | |
| 107 // property that it will continue to process tasks until no references | |
| 108 // remain, at least. The QuitClosure we usually pass does the simple thing | |
| 109 // of stopping the underlying TaskRunner. Since we are re-using the | |
| 110 // ui_task_runner of the browser thread, we cannot stop it explicitly. | |
| 111 // Therefore, base::DoNothing is passed in as the quit closure. | |
|
Wez
2014/10/24 00:28:47
This comment is out-of-date and doesn't belong her
kelvinp
2014/10/24 21:39:41
Done.
| |
| 112 scoped_ptr<NativeMessageHost> host(new remoting::It2MeNativeMessagingHost( | |
| 113 context.Pass(), host_factory.Pass())); | |
| 114 return host.Pass(); | |
| 115 } | |
| 116 return nullptr; | |
| 117 } | |
| 118 | |
| 93 // If you modify the list of allowed_origins, don't forget to update | 119 // If you modify the list of allowed_origins, don't forget to update |
| 94 // remoting/host/it2me/com.google.chrome.remote_assistance.json.jinja2 | 120 // remoting/host/it2me/com.google.chrome.remote_assistance.json.jinja2 |
| 95 // to keep the two lists in sync. | 121 // to keep the two lists in sync. |
| 96 // TODO(kelvinp): Load the native messaging manifest as a resource file into | 122 // TODO(kelvinp): Load the native messaging manifest as a resource file into |
| 97 // chrome and fetch the list of allowed_origins from the manifest. | 123 // chrome and fetch the list of allowed_origins from the manifest (see |
| 98 /*const char* const kRemotingIt2MeOrigins[] = { | 124 // crbug/424743). |
| 125 const char* const kRemotingIt2MeOrigins[] = { | |
| 99 "chrome-extension://ljacajndfccfgnfohlgkdphmbnpkjflk/", | 126 "chrome-extension://ljacajndfccfgnfohlgkdphmbnpkjflk/", |
| 100 "chrome-extension://gbchcmhmhahfdphkhkmpfmihenigjmpp/", | 127 "chrome-extension://gbchcmhmhahfdphkhkmpfmihenigjmpp/", |
| 101 "chrome-extension://kgngmbheleoaphbjbaiobfdepmghbfah/", | 128 "chrome-extension://kgngmbheleoaphbjbaiobfdepmghbfah/", |
| 102 "chrome-extension://odkaodonbgfohohmklejpjiejmcipmib/", | 129 "chrome-extension://odkaodonbgfohohmklejpjiejmcipmib/", |
| 103 "chrome-extension://dokpleeekgeeiehdhmdkeimnkmoifgdd/", | 130 "chrome-extension://dokpleeekgeeiehdhmdkeimnkmoifgdd/", |
| 104 "chrome-extension://ajoainacpilcemgiakehflpbkbfipojk/", | 131 "chrome-extension://ajoainacpilcemgiakehflpbkbfipojk/", |
| 105 "chrome-extension://hmboipgjngjoiaeicfdifdoeacilalgc/"};*/ | 132 "chrome-extension://hmboipgjngjoiaeicfdifdoeacilalgc/"}; |
| 106 | 133 |
| 107 static const BuiltInHost kBuiltInHost[] = { | 134 static const BuiltInHost kBuiltInHost[] = { |
| 108 {"com.google.chrome.test.echo", // ScopedTestNativeMessagingHost::kHostName | 135 {"com.google.chrome.test.echo", // ScopedTestNativeMessagingHost::kHostName |
| 109 kEchoHostOrigins, | 136 kEchoHostOrigins, |
| 110 arraysize(kEchoHostOrigins), | 137 arraysize(kEchoHostOrigins), |
| 111 &EchoHost::Create}, | 138 &EchoHost::Create}, |
| 139 {"com.google.chrome.remote_assistance", | |
| 140 kRemotingIt2MeOrigins, | |
| 141 arraysize(kRemotingIt2MeOrigins), | |
| 142 &CreateIt2MeHost}, | |
| 112 }; | 143 }; |
| 113 | 144 |
| 114 bool MatchesSecurityOrigin(const BuiltInHost& host, | 145 bool MatchesSecurityOrigin(const BuiltInHost& host, |
| 115 const std::string& extension_id) { | 146 const std::string& extension_id) { |
| 116 GURL origin(std::string(kExtensionScheme) + "://" + extension_id); | 147 GURL origin(std::string(kExtensionScheme) + "://" + extension_id); |
| 117 for (int i = 0; i < host.allowed_origins_count; i++) { | 148 for (int i = 0; i < host.allowed_origins_count; i++) { |
| 118 URLPattern allowed_origin(URLPattern::SCHEME_ALL, host.allowed_origins[i]); | 149 URLPattern allowed_origin(URLPattern::SCHEME_ALL, host.allowed_origins[i]); |
| 119 if (allowed_origin.MatchesSecurityOrigin(origin)) { | 150 if (allowed_origin.MatchesSecurityOrigin(origin)) { |
| 120 return true; | 151 return true; |
| 121 } | 152 } |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 140 } | 171 } |
| 141 *error = kForbiddenError; | 172 *error = kForbiddenError; |
| 142 return nullptr; | 173 return nullptr; |
| 143 } | 174 } |
| 144 } | 175 } |
| 145 *error = kNotFoundError; | 176 *error = kNotFoundError; |
| 146 return nullptr; | 177 return nullptr; |
| 147 } | 178 } |
| 148 | 179 |
| 149 } // namespace extensions | 180 } // namespace extensions |
| OLD | NEW |