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; | |
Sergey Ulanov
2014/09/27 00:24:10
Don't need this typedef. EchoHost inherits NativeM
kelvinp
2014/09/29 22:59:40
Done.
| |
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 static scoped_ptr<NativeMessageHost> CreateEchoHost() { | |
32 return scoped_ptr<NativeMessageHost>(new EchoHost()); | |
33 } | |
34 | |
35 EchoHost() {} | |
36 | |
37 virtual void set_client(base::WeakPtr<Client> client) OVERRIDE { | |
38 client_ = client; | |
39 } | |
40 | |
41 virtual void Send(const std::string& json) OVERRIDE { | |
42 client_->PostMessageFromNativeHost(json); | |
43 }; | |
44 | |
45 private: | |
46 base::WeakPtr<Client> client_; | |
47 | |
48 DISALLOW_COPY_AND_ASSIGN(EchoHost); | |
49 }; | |
50 | |
51 struct BuiltInHost { | |
52 const char* const name; | |
53 const char* const* const allowed_origins; | |
54 int allowed_origins_count; | |
55 scoped_ptr<NativeMessageHost>(*create_function)(); | |
56 }; | |
57 | |
58 // If you modify the list of allowed_origins, don't forget to update | |
59 // remoting/host/it2me/com.google.chrome.remote_assistance.json.jinja2 | |
60 // to keep the two lists in sync. | |
61 // TODO(kelvinp): Load the native messaging manifest as a resource file into | |
62 // chrome and fetch the list of allowed_origins from the manifest. | |
63 const char* const kRemotingIt2MeOrigins[] = { | |
64 "chrome-extension://ljacajndfccfgnfohlgkdphmbnpkjflk/", | |
65 "chrome-extension://gbchcmhmhahfdphkhkmpfmihenigjmpp/", | |
66 "chrome-extension://kgngmbheleoaphbjbaiobfdepmghbfah/", | |
67 "chrome-extension://odkaodonbgfohohmklejpjiejmcipmib/", | |
68 "chrome-extension://dokpleeekgeeiehdhmdkeimnkmoifgdd/", | |
69 "chrome-extension://ajoainacpilcemgiakehflpbkbfipojk/", | |
70 "chrome-extension://hmboipgjngjoiaeicfdifdoeacilalgc/"}; | |
71 | |
72 static const BuiltInHost kBuiltInHost[] = { | |
73 {"com.google.chrome.remote_assistance", | |
74 kRemotingIt2MeOrigins, | |
75 arraysize(kRemotingIt2MeOrigins), | |
76 &EchoHost::CreateEchoHost}, | |
77 }; | |
78 | |
79 bool MatchesSecurityOrigin(const BuiltInHost& host, | |
80 const std::string& extension_id) { | |
81 GURL origin(std::string(kExtensionScheme) + "://" + extension_id); | |
82 for (int i = 0; i < host.allowed_origins_count; i++) { | |
83 URLPattern allowed_origin(URLPattern::SCHEME_ALL); | |
84 DCHECK_EQ(URLPattern::PARSE_SUCCESS, | |
85 allowed_origin.Parse(host.allowed_origins[i])); | |
86 if (allowed_origin.MatchesSecurityOrigin(origin)) { | |
87 return true; | |
88 } | |
89 } | |
90 return false; | |
91 } | |
92 | |
93 } // namespace | |
94 | |
95 scoped_ptr<NativeMessageHost> NativeMessageHost::Create( | |
96 gfx::NativeView native_view, | |
97 const std::string& source_extension_id, | |
98 const std::string& native_host_name, | |
99 bool allow_user_level) { | |
100 scoped_ptr<NativeMessageHost> not_found; | |
101 | |
102 if (!CommandLine::ForCurrentProcess() | |
103 ->HasSwitch(switches::kEnableRemoteAssistance)) { | |
Sergey Ulanov
2014/09/27 00:24:10
I think it makes more sense to put this in the fun
kelvinp
2014/09/29 22:59:40
Done.
| |
104 return not_found.Pass(); | |
105 } | |
106 | |
107 for (unsigned int i = 0; i < arraysize(kBuiltInHost); i++) { | |
108 const BuiltInHost& host = kBuiltInHost[i]; | |
109 std::string name(host.name); | |
110 if (name == native_host_name && | |
111 MatchesSecurityOrigin(host, source_extension_id)) { | |
112 return (*host.create_function)(); | |
113 } | |
114 } | |
115 | |
116 return not_found.Pass(); | |
117 } | |
118 | |
119 } // namespace extensions | |
OLD | NEW |