Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(30)

Side by Side Diff: chrome/browser/extensions/api/messaging/native_message_host_chromeos.cc

Issue 591463003: Remote Assistance on Chrome OS Part III - NativeMessageHost (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@native_messaging
Patch Set: Rebase on ToT Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/message_loop/message_loop_proxy.h"
13 #include "extensions/common/constants.h"
14 #include "extensions/common/url_pattern.h"
15 #include "ui/gfx/native_widget_types.h"
16 #include "url/gurl.h"
17
18 namespace extensions {
19
20 namespace {
21
22 // A simple NativeMesageHost that echoes the received message. It is currently
23 // used for testing.
24 // TODO(kelvinp): Replace this class once Remote Assistance in process host
25 // is implemented.
26 class EchoHost : public NativeMessageHost {
27 public:
28 static scoped_ptr<NativeMessageHost> CreateEchoHost() {
29 return scoped_ptr<NativeMessageHost>(new EchoHost());
30 }
31
32 EchoHost() : client_(NULL) {}
33
34 virtual void Start(Client* client) OVERRIDE {
35 client_ = client;
36 }
37
38 virtual void OnMessage(const std::string& json) OVERRIDE {
39 client_->PostMessageFromNativeHost(json);
40 };
41
42 virtual scoped_refptr<base::SingleThreadTaskRunner> task_runner()
43 const OVERRIDE {
44 return base::MessageLoopProxy::current();
45 };
46
47 private:
48 Client* client_;
49
50 DISALLOW_COPY_AND_ASSIGN(EchoHost);
51 };
52
53 struct BuiltInHost {
54 const char* const name;
55 const char* const* const allowed_origins;
56 int allowed_origins_count;
57 scoped_ptr<NativeMessageHost>(*create_function)();
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://dokpleeekgeeiehdhmdkeimnkmoifgdd/",
71 "chrome-extension://ajoainacpilcemgiakehflpbkbfipojk/",
72 "chrome-extension://hmboipgjngjoiaeicfdifdoeacilalgc/"};
73
74 static const BuiltInHost kBuiltInHost[] = {
75 {"com.google.chrome.remote_assistance",
76 kRemotingIt2MeOrigins,
77 arraysize(kRemotingIt2MeOrigins),
78 &EchoHost::CreateEchoHost},
79 };
80
81 bool MatchesSecurityOrigin(const BuiltInHost& host,
82 const std::string& extension_id) {
83 GURL origin(std::string(kExtensionScheme) + "://" + extension_id);
84 for (int i = 0; i < host.allowed_origins_count; i++) {
85 URLPattern allowed_origin(URLPattern::SCHEME_ALL);
86 DCHECK_EQ(URLPattern::PARSE_SUCCESS,
87 allowed_origin.Parse(host.allowed_origins[i]));
88 if (allowed_origin.MatchesSecurityOrigin(origin)) {
89 return true;
90 }
91 }
92 return false;
93 }
94
95 } // namespace
96
97 scoped_ptr<NativeMessageHost> NativeMessageHost::Create(
98 gfx::NativeView native_view,
99 const std::string& source_extension_id,
100 const std::string& native_host_name,
101 bool allow_user_level) {
102 for (unsigned int i = 0; i < arraysize(kBuiltInHost); i++) {
103 const BuiltInHost& host = kBuiltInHost[i];
104 std::string name(host.name);
105 if (name == native_host_name &&
106 MatchesSecurityOrigin(host, source_extension_id)) {
107 return (*host.create_function)();
108 }
109 }
110 return scoped_ptr<NativeMessageHost>();
111 }
112
113 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698