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 "chrome/browser/extensions/extension_service.h" | 10 #include "chrome/browser/extensions/extension_service.h" |
| 11 #include "chrome/common/extensions/api/mdns.h" | 11 #include "chrome/common/extensions/api/mdns.h" |
| 12 #include "content/public/browser/render_process_host.h" | |
| 13 #include "content/public/browser/render_view_host.h" | |
| 14 #include "extensions/browser/extension_host.h" | |
| 12 #include "extensions/browser/extension_registry.h" | 15 #include "extensions/browser/extension_registry.h" |
| 16 #include "extensions/common/extension_messages.h" | |
| 13 | 17 |
| 14 namespace extensions { | 18 namespace extensions { |
| 15 | 19 |
| 16 namespace mdns = api::mdns; | 20 namespace mdns = api::mdns; |
| 17 | 21 |
| 18 namespace { | 22 namespace { |
| 19 | 23 |
| 20 // Whitelisted mDNS service types. | 24 // Whitelisted mDNS service types. |
| 21 const char kCastServiceType[] = "_googlecast._tcp.local"; | 25 const char kCastServiceType[] = "_googlecast._tcp.local"; |
| 22 const char kPrivetServiceType[] = "_privet._tcp.local"; | 26 const char kPrivetServiceType[] = "_privet._tcp.local"; |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 77 UpdateMDnsListeners(details); | 81 UpdateMDnsListeners(details); |
| 78 } | 82 } |
| 79 | 83 |
| 80 void MDnsAPI::OnListenerRemoved(const EventListenerInfo& details) { | 84 void MDnsAPI::OnListenerRemoved(const EventListenerInfo& details) { |
| 81 DCHECK(thread_checker_.CalledOnValidThread()); | 85 DCHECK(thread_checker_.CalledOnValidThread()); |
| 82 UpdateMDnsListeners(details); | 86 UpdateMDnsListeners(details); |
| 83 } | 87 } |
| 84 | 88 |
| 85 void MDnsAPI::UpdateMDnsListeners(const EventListenerInfo& details) { | 89 void MDnsAPI::UpdateMDnsListeners(const EventListenerInfo& details) { |
| 86 std::set<std::string> new_service_types; | 90 std::set<std::string> new_service_types; |
| 87 | 91 for (const EventListener* listener : GetValidOnServiceListListeners()) { |
| 88 // Check all listeners for service type filters. | 92 std::string service_type; |
| 89 const EventListenerMap::ListenerList& listeners = | 93 listener->filter()->GetStringASCII(kEventFilterServiceTypeKey, |
| 90 extensions::EventRouter::Get(browser_context_) | 94 &service_type); |
| 91 ->listeners() | 95 new_service_types.insert(service_type); |
| 92 .GetEventListenersByName(details.event_name); | |
| 93 for (EventListenerMap::ListenerList::const_iterator it = listeners.begin(); | |
| 94 it != listeners.end(); ++it) { | |
| 95 base::DictionaryValue* filter = ((*it)->filter()); | |
| 96 | |
| 97 std::string filter_value; | |
| 98 filter->GetStringASCII(kEventFilterServiceTypeKey, &filter_value); | |
| 99 if (filter_value.empty()) | |
| 100 continue; | |
| 101 | |
| 102 const Extension* extension = ExtensionRegistry::Get(browser_context_)-> | |
| 103 enabled_extensions().GetByID((*it)->extension_id()); | |
| 104 // Don't listen for services associated only with disabled extensions. | |
| 105 if (!extension) | |
| 106 continue; | |
| 107 | |
| 108 // Platform apps may query for all services; other types of extensions are | |
| 109 // restricted to a whitelist. | |
| 110 if (!extension->is_platform_app() && | |
| 111 !IsServiceTypeWhitelisted(filter_value)) | |
| 112 continue; | |
| 113 | |
| 114 new_service_types.insert(filter_value); | |
| 115 } | 96 } |
| 116 | 97 |
| 117 // Find all the added and removed service types since last update. | 98 // Find all the added and removed service types since last update. |
| 118 std::set<std::string> added_service_types = | 99 std::set<std::string> added_service_types = |
| 119 base::STLSetDifference<std::set<std::string> >( | 100 base::STLSetDifference<std::set<std::string> >( |
| 120 new_service_types, service_types_); | 101 new_service_types, service_types_); |
| 121 std::set<std::string> removed_service_types = | 102 std::set<std::string> removed_service_types = |
| 122 base::STLSetDifference<std::set<std::string> >( | 103 base::STLSetDifference<std::set<std::string> >( |
| 123 service_types_, new_service_types); | 104 service_types_, new_service_types); |
| 124 | 105 |
| 125 // Update the registry. | 106 // Update the registry. |
| 126 DnsSdRegistry* registry = dns_sd_registry(); | 107 DnsSdRegistry* registry = dns_sd_registry(); |
| 127 for (const auto& srv : added_service_types) { | 108 for (const auto& srv : added_service_types) { |
| 128 registry->RegisterDnsSdListener(srv); | 109 registry->RegisterDnsSdListener(srv); |
| 129 } | 110 } |
| 130 for (const auto& srv : removed_service_types) { | 111 for (const auto& srv : removed_service_types) { |
| 131 registry->UnregisterDnsSdListener(srv); | 112 registry->UnregisterDnsSdListener(srv); |
| 132 } | 113 } |
| 133 service_types_ = new_service_types; | 114 service_types_ = new_service_types; |
| 134 } | 115 } |
| 135 | 116 |
| 136 void MDnsAPI::OnDnsSdEvent(const std::string& service_type, | 117 void MDnsAPI::OnDnsSdEvent(const std::string& service_type, |
| 137 const DnsSdRegistry::DnsSdServiceList& services) { | 118 const DnsSdRegistry::DnsSdServiceList& services) { |
| 138 DCHECK(thread_checker_.CalledOnValidThread()); | 119 DCHECK(thread_checker_.CalledOnValidThread()); |
| 139 | 120 |
| 140 std::vector<linked_ptr<mdns::MDnsService> > args; | 121 std::vector<linked_ptr<mdns::MDnsService> > args; |
| 141 for (DnsSdRegistry::DnsSdServiceList::const_iterator it = services.begin(); | 122 for (DnsSdRegistry::DnsSdServiceList::const_iterator it = services.begin(); |
| 142 it != services.end(); ++it) { | 123 it != services.end(); ++it) { |
| 124 if (static_cast<long>(args.size()) == | |
| 125 api::mdns::MAX_SERVICE_INSTANCES_PER_EVENT) { | |
| 126 // TODO(reddaly): This is not the most meaningful way of notifying the | |
| 127 // application that something bad happened. It will go to the user's | |
| 128 // console (which most users don't look at)and the developer will be none | |
| 129 // the wiser. Instead, changing the event to pass the number of | |
| 130 // discovered instances would allow the caller to know when the list is | |
| 131 // truncated and tell the user something meaningful in the extension/app. | |
| 132 WriteToConsole(service_type, | |
| 133 content::CONSOLE_MESSAGE_LEVEL_WARNING, | |
| 134 base::StringPrintf( | |
| 135 "Truncating number of service instances in " | |
| 136 "onServiceList to maximum allowed: %d", | |
| 137 api::mdns::MAX_SERVICE_INSTANCES_PER_EVENT)); | |
| 138 break; | |
| 139 } | |
| 143 linked_ptr<mdns::MDnsService> mdns_service = | 140 linked_ptr<mdns::MDnsService> mdns_service = |
| 144 make_linked_ptr(new mdns::MDnsService); | 141 make_linked_ptr(new mdns::MDnsService); |
| 145 mdns_service->service_name = (*it).service_name; | 142 mdns_service->service_name = (*it).service_name; |
| 146 mdns_service->service_host_port = (*it).service_host_port; | 143 mdns_service->service_host_port = (*it).service_host_port; |
| 147 mdns_service->ip_address = (*it).ip_address; | 144 mdns_service->ip_address = (*it).ip_address; |
| 148 mdns_service->service_data = (*it).service_data; | 145 mdns_service->service_data = (*it).service_data; |
| 149 args.push_back(mdns_service); | 146 args.push_back(mdns_service); |
| 150 } | 147 } |
| 151 | 148 |
| 152 scoped_ptr<base::ListValue> results = mdns::OnServiceList::Create(args); | 149 scoped_ptr<base::ListValue> results = mdns::OnServiceList::Create(args); |
| 153 scoped_ptr<Event> event( | 150 scoped_ptr<Event> event( |
| 154 new Event(mdns::OnServiceList::kEventName, results.Pass())); | 151 new Event(mdns::OnServiceList::kEventName, results.Pass())); |
| 155 event->restrict_to_browser_context = browser_context_; | 152 event->restrict_to_browser_context = browser_context_; |
| 156 event->filter_info.SetServiceType(service_type); | 153 event->filter_info.SetServiceType(service_type); |
| 157 | 154 |
| 158 // TODO(justinlin): To avoid having listeners without filters getting all | 155 // TODO(justinlin): To avoid having listeners without filters getting all |
| 159 // events, modify API to have this event require filters. | 156 // events, modify API to have this event require filters. |
| 160 // TODO(reddaly): If event isn't on whitelist, ensure it does not get | 157 // TODO(reddaly): If event isn't on whitelist, ensure it does not get |
| 161 // broadcast to extensions. | 158 // broadcast to extensions. |
| 162 extensions::EventRouter::Get(browser_context_)->BroadcastEvent(event.Pass()); | 159 extensions::EventRouter::Get(browser_context_)->BroadcastEvent(event.Pass()); |
| 163 } | 160 } |
| 164 | 161 |
| 162 std::set<const EventListener*> MDnsAPI::GetValidOnServiceListListeners() { | |
|
mark a. foltz
2015/04/06 23:01:43
So there are two places iterating over this, and b
Red Daly
2015/04/07 18:08:14
I like the idea - I had something similar in patch
| |
| 163 std::set<const EventListener*> listeners; | |
| 164 for (const auto& listener : | |
| 165 extensions::EventRouter::Get(browser_context_)->listeners() | |
| 166 .GetEventListenersByName(mdns::OnServiceList::kEventName)) { | |
| 167 base::DictionaryValue* filter = (listener->filter()); | |
| 168 | |
| 169 std::string service_type; | |
| 170 filter->GetStringASCII(kEventFilterServiceTypeKey, &service_type); | |
| 171 if (service_type.empty()) | |
| 172 continue; | |
| 173 | |
| 174 const Extension* extension = ExtensionRegistry::Get(browser_context_)-> | |
| 175 enabled_extensions().GetByID(listener->extension_id()); | |
| 176 // Don't listen for services associated only with disabled extensions. | |
| 177 if (!extension) | |
| 178 continue; | |
| 179 | |
| 180 // Platform apps may query for all services; other types of extensions are | |
| 181 // restricted to a whitelist. | |
| 182 if (!extension->is_platform_app() && | |
| 183 !IsServiceTypeWhitelisted(service_type)) | |
| 184 continue; | |
| 185 | |
| 186 listeners.insert(listener.get()); | |
| 187 } | |
| 188 return listeners; | |
| 189 } | |
| 190 | |
| 191 void MDnsAPI::WriteToConsole(const std::string& service_type, | |
| 192 content::ConsoleMessageLevel level, | |
| 193 const std::string& message) { | |
| 194 // Get all the extensions with an onServiceList listener for a particular | |
| 195 // service type. | |
| 196 std::set<std::string> extension_ids; | |
| 197 for (const EventListener* listener : GetValidOnServiceListListeners()) { | |
|
mark a. foltz
2015/04/06 23:01:43
How does this filter for |service_type|?
Red Daly
2015/04/07 18:08:14
Fixed. Thanks for catching that.
| |
| 198 extension_ids.insert(listener->extension_id()); | |
| 199 } | |
| 200 | |
| 201 std::string logged_message(std::string("[chrome.mdns] ") + message); | |
| 202 | |
| 203 // Log to the consoles of the background pages for those extensions. | |
| 204 for (const std::string& extension_id : extension_ids) { | |
| 205 extensions::ExtensionHost* host = | |
| 206 extensions::ProcessManager::Get(browser_context_) | |
| 207 ->GetBackgroundHostForExtension(extension_id); | |
| 208 if (!host) | |
| 209 continue; | |
| 210 content::RenderViewHost* rvh = host->render_view_host(); | |
| 211 if (!rvh) | |
| 212 continue; | |
| 213 rvh->Send(new ExtensionMsg_AddMessageToConsole( | |
| 214 rvh->GetRoutingID(), | |
| 215 level, | |
| 216 logged_message)); | |
| 217 } | |
| 218 } | |
| 219 | |
| 165 } // namespace extensions | 220 } // namespace extensions |
| OLD | NEW |