OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/extensions/api/mdns/mdns_api.h" | 5 #include "chrome/browser/extensions/api/mdns/mdns_api.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
10 #include "base/profiler/scoped_profile.h" | 10 #include "base/profiler/scoped_profile.h" |
11 #include "chrome/browser/extensions/extension_service.h" | 11 #include "chrome/browser/extensions/extension_service.h" |
12 #include "chrome/common/extensions/api/mdns.h" | 12 #include "chrome/common/extensions/api/mdns.h" |
13 #include "extensions/browser/extension_registry.h" | |
13 | 14 |
14 namespace extensions { | 15 namespace extensions { |
15 | 16 |
16 namespace mdns = api::mdns; | 17 namespace mdns = api::mdns; |
17 | 18 |
18 namespace { | 19 namespace { |
19 | 20 |
20 // Whitelisted mDNS service types. | 21 // Whitelisted mDNS service types. |
21 const char kCastServiceType[] = "_googlecast._tcp.local"; | 22 const char kCastServiceType[] = "_googlecast._tcp.local"; |
22 const char kPrivetServiceType[] = "_privet._tcp.local"; | 23 const char kPrivetServiceType[] = "_privet._tcp.local"; |
23 const char kTestServiceType[] = "_testing._tcp.local"; | 24 const char kTestServiceType[] = "_testing._tcp.local"; |
24 | 25 |
25 bool IsServiceTypeWhitelisted(const std::string& service_type) { | 26 bool IsServiceTypeWhitelisted(const std::string& service_type, |
27 bool is_extension) { | |
28 // The mdns API is available to apps without restrictions. A whitelisted set | |
29 // of extensions is allowed to use the mdns API, but only to connect to a | |
30 // whitelisted set of service types. | |
not at google - send to devlin
2014/12/10 18:23:04
Note that a more correct check here would be is_pl
Red Daly
2015/01/26 21:55:33
Done, but in UpdateMDnsListeners.
| |
31 if (!is_extension) { | |
not at google - send to devlin
2014/12/10 18:23:05
I actually think that doing all of this work insid
Red Daly
2015/01/26 21:55:33
I took the second approach (kept function static a
| |
32 return true; | |
33 } | |
26 return service_type == kCastServiceType || | 34 return service_type == kCastServiceType || |
27 service_type == kPrivetServiceType || | 35 service_type == kPrivetServiceType || |
28 service_type == kTestServiceType; | 36 service_type == kTestServiceType; |
29 } | 37 } |
30 | 38 |
31 } // namespace | 39 } // namespace |
32 | 40 |
33 MDnsAPI::MDnsAPI(content::BrowserContext* context) : browser_context_(context) { | 41 MDnsAPI::MDnsAPI(content::BrowserContext* context) : browser_context_(context) { |
34 DCHECK(browser_context_); | 42 DCHECK(browser_context_); |
35 EventRouter::Get(context) | 43 EventRouter::Get(context) |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
102 new_service_types.insert(filter_value); | 110 new_service_types.insert(filter_value); |
103 } | 111 } |
104 | 112 |
105 // Find all the added and removed service types since last update. | 113 // Find all the added and removed service types since last update. |
106 std::set<std::string> added_service_types = | 114 std::set<std::string> added_service_types = |
107 base::STLSetDifference<std::set<std::string> >( | 115 base::STLSetDifference<std::set<std::string> >( |
108 new_service_types, service_types_); | 116 new_service_types, service_types_); |
109 std::set<std::string> removed_service_types = | 117 std::set<std::string> removed_service_types = |
110 base::STLSetDifference<std::set<std::string> >( | 118 base::STLSetDifference<std::set<std::string> >( |
111 service_types_, new_service_types); | 119 service_types_, new_service_types); |
120 service_types_ = new_service_types; | |
mark a. foltz
2014/12/09 23:56:34
If we update |service_types_| here for a disabled
not at google - send to devlin
2014/12/10 18:23:05
This shouldn't happen because disabled extensions
| |
112 | 121 |
113 // Update the registry. | 122 // Update the registry. |
114 DnsSdRegistry* registry = dns_sd_registry(); | 123 DnsSdRegistry* registry = dns_sd_registry(); |
mark a. foltz
2014/12/09 23:56:34
This doesn't need to be done until after |extensio
Red Daly
2015/01/26 21:55:33
The control flow changed a little. At this point,
| |
124 | |
125 const Extension* extension = ExtensionRegistry::Get(browser_context_)-> | |
126 GetExtensionById(details.extension_id, ExtensionRegistry::ENABLED); | |
mark a. foltz
2014/12/09 23:56:34
Are only ENABLED extensions allowed to register ev
not at google - send to devlin
2014/12/10 18:23:04
Yes I think that EVERYTHING was correct, whoops, b
Red Daly
2015/01/26 21:55:33
I checked and this is called when an extension is
| |
127 if (!extension) { | |
128 return; | |
129 } | |
130 | |
mark a. foltz
2014/12/09 23:56:34
Slight preference for declaring a bool here and le
Red Daly
2015/01/26 21:55:33
Obsolete (moved IsServiceTypeWhitelisted call to l
| |
115 for (std::set<std::string>::iterator it = added_service_types.begin(); | 131 for (std::set<std::string>::iterator it = added_service_types.begin(); |
116 it != added_service_types.end(); ++it) { | 132 it != added_service_types.end(); ++it) { |
117 if (IsServiceTypeWhitelisted(*it)) | 133 if (IsServiceTypeWhitelisted(*it, extension->is_extension())) |
118 registry->RegisterDnsSdListener(*it); | 134 registry->RegisterDnsSdListener(*it); |
119 } | 135 } |
120 for (std::set<std::string>::iterator it = removed_service_types.begin(); | 136 for (std::set<std::string>::iterator it = removed_service_types.begin(); |
121 it != removed_service_types.end(); ++it) { | 137 it != removed_service_types.end(); ++it) { |
122 if (IsServiceTypeWhitelisted(*it)) | 138 if (IsServiceTypeWhitelisted(*it, extension->is_extension())) |
123 registry->UnregisterDnsSdListener(*it); | 139 registry->UnregisterDnsSdListener(*it); |
124 } | 140 } |
125 | |
126 service_types_ = new_service_types; | |
127 } | 141 } |
128 | 142 |
129 void MDnsAPI::OnDnsSdEvent(const std::string& service_type, | 143 void MDnsAPI::OnDnsSdEvent(const std::string& service_type, |
130 const DnsSdRegistry::DnsSdServiceList& services) { | 144 const DnsSdRegistry::DnsSdServiceList& services) { |
131 DCHECK(thread_checker_.CalledOnValidThread()); | 145 DCHECK(thread_checker_.CalledOnValidThread()); |
132 | 146 |
133 std::vector<linked_ptr<mdns::MDnsService> > args; | 147 std::vector<linked_ptr<mdns::MDnsService> > args; |
134 for (DnsSdRegistry::DnsSdServiceList::const_iterator it = services.begin(); | 148 for (DnsSdRegistry::DnsSdServiceList::const_iterator it = services.begin(); |
135 it != services.end(); ++it) { | 149 it != services.end(); ++it) { |
136 linked_ptr<mdns::MDnsService> mdns_service = | 150 linked_ptr<mdns::MDnsService> mdns_service = |
(...skipping 12 matching lines...) Expand all Loading... | |
149 event->filter_info.SetServiceType(service_type); | 163 event->filter_info.SetServiceType(service_type); |
150 | 164 |
151 VLOG(1) << "Broadcasting OnServiceList event: " << event.get(); | 165 VLOG(1) << "Broadcasting OnServiceList event: " << event.get(); |
152 | 166 |
153 // TODO(justinlin): To avoid having listeners without filters getting all | 167 // TODO(justinlin): To avoid having listeners without filters getting all |
154 // events, modify API to have this event require filters. | 168 // events, modify API to have this event require filters. |
155 extensions::EventRouter::Get(browser_context_)->BroadcastEvent(event.Pass()); | 169 extensions::EventRouter::Get(browser_context_)->BroadcastEvent(event.Pass()); |
156 } | 170 } |
157 | 171 |
158 } // namespace extensions | 172 } // namespace extensions |
OLD | NEW |