Chromium Code Reviews| 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"; |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 79 } | 80 } |
| 80 | 81 |
| 81 void MDnsAPI::OnListenerRemoved(const EventListenerInfo& details) { | 82 void MDnsAPI::OnListenerRemoved(const EventListenerInfo& details) { |
| 82 DCHECK(thread_checker_.CalledOnValidThread()); | 83 DCHECK(thread_checker_.CalledOnValidThread()); |
| 83 UpdateMDnsListeners(details); | 84 UpdateMDnsListeners(details); |
| 84 } | 85 } |
| 85 | 86 |
| 86 void MDnsAPI::UpdateMDnsListeners(const EventListenerInfo& details) { | 87 void MDnsAPI::UpdateMDnsListeners(const EventListenerInfo& details) { |
| 87 std::set<std::string> new_service_types; | 88 std::set<std::string> new_service_types; |
| 88 | 89 |
| 89 // Check all listeners for service type filers. | 90 // Check all listeners for service type filters. |
| 90 const EventListenerMap::ListenerList& listeners = | 91 const EventListenerMap::ListenerList& listeners = |
| 91 extensions::EventRouter::Get(browser_context_) | 92 extensions::EventRouter::Get(browser_context_) |
| 92 ->listeners() | 93 ->listeners() |
| 93 .GetEventListenersByName(details.event_name); | 94 .GetEventListenersByName(details.event_name); |
| 94 for (EventListenerMap::ListenerList::const_iterator it = listeners.begin(); | 95 for (EventListenerMap::ListenerList::const_iterator it = listeners.begin(); |
| 95 it != listeners.end(); ++it) { | 96 it != listeners.end(); ++it) { |
| 96 base::DictionaryValue* filter = ((*it)->filter()); | 97 base::DictionaryValue* filter = ((*it)->filter()); |
| 97 | 98 |
| 98 std::string filter_value; | 99 std::string filter_value; |
| 99 filter->GetStringASCII(kEventFilterServiceTypeKey, &filter_value); | 100 filter->GetStringASCII(kEventFilterServiceTypeKey, &filter_value); |
| 100 if (filter_value.empty()) | 101 if (filter_value.empty()) |
| 101 continue; | 102 continue; |
| 103 | |
| 104 const Extension* extension = ExtensionRegistry::Get(browser_context_)-> | |
| 105 GetExtensionById((*it)->extension_id(), ExtensionRegistry::ENABLED); | |
|
not at google - send to devlin
2015/01/26 22:13:35
Just do:
... = ExtensionRegistry::Get(...)->enabl
Red Daly
2015/01/26 23:46:23
Done.
| |
| 106 // Don't listen for services associated only with disabled extensions. | |
| 107 if (!extension) | |
| 108 continue; | |
| 109 | |
| 110 // Platform apps may query for all services; other types of extensions are | |
| 111 // restricted to a whitelist. | |
| 112 if (!extension->is_platform_app() && | |
| 113 !IsServiceTypeWhitelisted(filter_value)) | |
| 114 continue; | |
| 115 | |
| 102 new_service_types.insert(filter_value); | 116 new_service_types.insert(filter_value); |
| 103 } | 117 } |
| 104 | 118 |
| 105 // Find all the added and removed service types since last update. | 119 // Find all the added and removed service types since last update. |
| 106 std::set<std::string> added_service_types = | 120 std::set<std::string> added_service_types = |
| 107 base::STLSetDifference<std::set<std::string> >( | 121 base::STLSetDifference<std::set<std::string> >( |
| 108 new_service_types, service_types_); | 122 new_service_types, service_types_); |
| 109 std::set<std::string> removed_service_types = | 123 std::set<std::string> removed_service_types = |
| 110 base::STLSetDifference<std::set<std::string> >( | 124 base::STLSetDifference<std::set<std::string> >( |
| 111 service_types_, new_service_types); | 125 service_types_, new_service_types); |
| 112 | 126 |
| 113 // Update the registry. | 127 // Update the registry. |
| 114 DnsSdRegistry* registry = dns_sd_registry(); | 128 DnsSdRegistry* registry = dns_sd_registry(); |
| 129 | |
| 115 for (std::set<std::string>::iterator it = added_service_types.begin(); | 130 for (std::set<std::string>::iterator it = added_service_types.begin(); |
| 116 it != added_service_types.end(); ++it) { | 131 it != added_service_types.end(); ++it) { |
| 117 if (IsServiceTypeWhitelisted(*it)) | 132 registry->RegisterDnsSdListener(*it); |
| 118 registry->RegisterDnsSdListener(*it); | 133 |
|
not at google - send to devlin
2015/01/26 22:13:35
Nits: delete this empty line, don't add an extra o
Red Daly
2015/01/26 23:46:23
Done.
| |
| 119 } | 134 } |
| 120 for (std::set<std::string>::iterator it = removed_service_types.begin(); | 135 for (std::set<std::string>::iterator it = removed_service_types.begin(); |
| 121 it != removed_service_types.end(); ++it) { | 136 it != removed_service_types.end(); ++it) { |
| 122 if (IsServiceTypeWhitelisted(*it)) | 137 registry->UnregisterDnsSdListener(*it); |
| 123 registry->UnregisterDnsSdListener(*it); | |
| 124 } | 138 } |
| 125 | |
| 126 service_types_ = new_service_types; | 139 service_types_ = new_service_types; |
| 127 } | 140 } |
| 128 | 141 |
| 129 void MDnsAPI::OnDnsSdEvent(const std::string& service_type, | 142 void MDnsAPI::OnDnsSdEvent(const std::string& service_type, |
| 130 const DnsSdRegistry::DnsSdServiceList& services) { | 143 const DnsSdRegistry::DnsSdServiceList& services) { |
| 131 DCHECK(thread_checker_.CalledOnValidThread()); | 144 DCHECK(thread_checker_.CalledOnValidThread()); |
| 132 | 145 |
| 133 std::vector<linked_ptr<mdns::MDnsService> > args; | 146 std::vector<linked_ptr<mdns::MDnsService> > args; |
| 134 for (DnsSdRegistry::DnsSdServiceList::const_iterator it = services.begin(); | 147 for (DnsSdRegistry::DnsSdServiceList::const_iterator it = services.begin(); |
| 135 it != services.end(); ++it) { | 148 it != services.end(); ++it) { |
| 136 linked_ptr<mdns::MDnsService> mdns_service = | 149 linked_ptr<mdns::MDnsService> mdns_service = |
| 137 make_linked_ptr(new mdns::MDnsService); | 150 make_linked_ptr(new mdns::MDnsService); |
| 138 mdns_service->service_name = (*it).service_name; | 151 mdns_service->service_name = (*it).service_name; |
| 139 mdns_service->service_host_port = (*it).service_host_port; | 152 mdns_service->service_host_port = (*it).service_host_port; |
| 140 mdns_service->ip_address = (*it).ip_address; | 153 mdns_service->ip_address = (*it).ip_address; |
| 141 mdns_service->service_data = (*it).service_data; | 154 mdns_service->service_data = (*it).service_data; |
| 142 args.push_back(mdns_service); | 155 args.push_back(mdns_service); |
| 143 } | 156 } |
| 144 | 157 |
| 145 scoped_ptr<base::ListValue> results = mdns::OnServiceList::Create(args); | 158 scoped_ptr<base::ListValue> results = mdns::OnServiceList::Create(args); |
| 146 scoped_ptr<Event> event( | 159 scoped_ptr<Event> event( |
| 147 new Event(mdns::OnServiceList::kEventName, results.Pass())); | 160 new Event(mdns::OnServiceList::kEventName, results.Pass())); |
| 148 event->restrict_to_browser_context = browser_context_; | 161 event->restrict_to_browser_context = browser_context_; |
| 149 event->filter_info.SetServiceType(service_type); | 162 event->filter_info.SetServiceType(service_type); |
| 150 | 163 |
| 151 VLOG(1) << "Broadcasting OnServiceList event: " << event.get(); | |
| 152 | |
| 153 // TODO(justinlin): To avoid having listeners without filters getting all | 164 // TODO(justinlin): To avoid having listeners without filters getting all |
| 154 // events, modify API to have this event require filters. | 165 // events, modify API to have this event require filters. |
| 155 extensions::EventRouter::Get(browser_context_)->BroadcastEvent(event.Pass()); | 166 extensions::EventRouter::Get(browser_context_)->BroadcastEvent(event.Pass()); |
| 156 } | 167 } |
| 157 | 168 |
| 158 } // namespace extensions | 169 } // namespace extensions |
| OLD | NEW |