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 | 13 |
14 namespace extensions { | 14 namespace extensions { |
15 | 15 |
16 namespace mdns = api::mdns; | 16 namespace mdns = api::mdns; |
17 | 17 |
18 namespace { | |
19 | |
20 // Whitelisted mDNS service types. | |
21 const char kCastServiceType[] = "_googlecast._tcp.local"; | |
22 const char kPrivetServiceType[] = "_privet._tcp.local"; | |
23 const char kTestServiceType[] = "_testing._tcp.local"; | |
24 | |
25 bool IsServiceTypeWhitelisted(const std::string& service_type) { | |
26 return service_type == kCastServiceType || | |
27 service_type == kPrivetServiceType || | |
28 service_type == kTestServiceType; | |
29 } | |
30 | |
31 } // namespace | |
32 | |
33 MDnsAPI::MDnsAPI(content::BrowserContext* context) : browser_context_(context) { | 18 MDnsAPI::MDnsAPI(content::BrowserContext* context) : browser_context_(context) { |
34 DCHECK(browser_context_); | 19 DCHECK(browser_context_); |
35 EventRouter::Get(context) | 20 EventRouter::Get(context) |
36 ->RegisterObserver(this, mdns::OnServiceList::kEventName); | 21 ->RegisterObserver(this, mdns::OnServiceList::kEventName); |
37 } | 22 } |
38 | 23 |
39 MDnsAPI::~MDnsAPI() { | 24 MDnsAPI::~MDnsAPI() { |
40 if (dns_sd_registry_.get()) { | 25 if (dns_sd_registry_.get()) { |
41 dns_sd_registry_->RemoveObserver(this); | 26 dns_sd_registry_->RemoveObserver(this); |
42 } | 27 } |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
107 base::STLSetDifference<std::set<std::string> >( | 92 base::STLSetDifference<std::set<std::string> >( |
108 new_service_types, service_types_); | 93 new_service_types, service_types_); |
109 std::set<std::string> removed_service_types = | 94 std::set<std::string> removed_service_types = |
110 base::STLSetDifference<std::set<std::string> >( | 95 base::STLSetDifference<std::set<std::string> >( |
111 service_types_, new_service_types); | 96 service_types_, new_service_types); |
112 | 97 |
113 // Update the registry. | 98 // Update the registry. |
114 DnsSdRegistry* registry = dns_sd_registry(); | 99 DnsSdRegistry* registry = dns_sd_registry(); |
115 for (std::set<std::string>::iterator it = added_service_types.begin(); | 100 for (std::set<std::string>::iterator it = added_service_types.begin(); |
116 it != added_service_types.end(); ++it) { | 101 it != added_service_types.end(); ++it) { |
117 if (IsServiceTypeWhitelisted(*it)) | 102 registry->RegisterDnsSdListener(*it); |
mark a. foltz
2014/10/31 06:29:15
To preserve existing behavior, I think extensions
Red Daly
2014/11/17 22:50:01
Done.
| |
118 registry->RegisterDnsSdListener(*it); | |
119 } | 103 } |
120 for (std::set<std::string>::iterator it = removed_service_types.begin(); | 104 for (std::set<std::string>::iterator it = removed_service_types.begin(); |
121 it != removed_service_types.end(); ++it) { | 105 it != removed_service_types.end(); ++it) { |
122 if (IsServiceTypeWhitelisted(*it)) | 106 registry->UnregisterDnsSdListener(*it); |
123 registry->UnregisterDnsSdListener(*it); | |
124 } | 107 } |
125 | 108 |
126 service_types_ = new_service_types; | 109 service_types_ = new_service_types; |
127 } | 110 } |
128 | 111 |
129 void MDnsAPI::OnDnsSdEvent(const std::string& service_type, | 112 void MDnsAPI::OnDnsSdEvent(const std::string& service_type, |
130 const DnsSdRegistry::DnsSdServiceList& services) { | 113 const DnsSdRegistry::DnsSdServiceList& services) { |
131 DCHECK(thread_checker_.CalledOnValidThread()); | 114 DCHECK(thread_checker_.CalledOnValidThread()); |
132 | 115 |
133 std::vector<linked_ptr<mdns::MDnsService> > args; | 116 std::vector<linked_ptr<mdns::MDnsService> > args; |
(...skipping 15 matching lines...) Expand all Loading... | |
149 event->filter_info.SetServiceType(service_type); | 132 event->filter_info.SetServiceType(service_type); |
150 | 133 |
151 VLOG(1) << "Broadcasting OnServiceList event: " << event.get(); | 134 VLOG(1) << "Broadcasting OnServiceList event: " << event.get(); |
152 | 135 |
153 // TODO(justinlin): To avoid having listeners without filters getting all | 136 // TODO(justinlin): To avoid having listeners without filters getting all |
154 // events, modify API to have this event require filters. | 137 // events, modify API to have this event require filters. |
155 extensions::EventRouter::Get(browser_context_)->BroadcastEvent(event.Pass()); | 138 extensions::EventRouter::Get(browser_context_)->BroadcastEvent(event.Pass()); |
156 } | 139 } |
157 | 140 |
158 } // namespace extensions | 141 } // namespace extensions |
OLD | NEW |