Index: chrome/browser/extensions/api/mdns/mdns_api.cc |
diff --git a/chrome/browser/extensions/api/mdns/mdns_api.cc b/chrome/browser/extensions/api/mdns/mdns_api.cc |
index 2a7c41d210931d1540e61b8cb89fe402a203e1e8..f84b9a7034b746924f2754239823211b9c2f1c9f 100644 |
--- a/chrome/browser/extensions/api/mdns/mdns_api.cc |
+++ b/chrome/browser/extensions/api/mdns/mdns_api.cc |
@@ -9,7 +9,11 @@ |
#include "base/lazy_instance.h" |
#include "chrome/browser/extensions/extension_service.h" |
#include "chrome/common/extensions/api/mdns.h" |
+#include "content/public/browser/render_process_host.h" |
+#include "content/public/browser/render_view_host.h" |
+#include "extensions/browser/extension_host.h" |
#include "extensions/browser/extension_registry.h" |
+#include "extensions/common/extension_messages.h" |
namespace extensions { |
@@ -84,34 +88,11 @@ void MDnsAPI::OnListenerRemoved(const EventListenerInfo& details) { |
void MDnsAPI::UpdateMDnsListeners(const EventListenerInfo& details) { |
std::set<std::string> new_service_types; |
- |
- // Check all listeners for service type filters. |
- const EventListenerMap::ListenerList& listeners = |
- extensions::EventRouter::Get(browser_context_) |
- ->listeners() |
- .GetEventListenersByName(details.event_name); |
- for (EventListenerMap::ListenerList::const_iterator it = listeners.begin(); |
- it != listeners.end(); ++it) { |
- base::DictionaryValue* filter = ((*it)->filter()); |
- |
- std::string filter_value; |
- filter->GetStringASCII(kEventFilterServiceTypeKey, &filter_value); |
- if (filter_value.empty()) |
- continue; |
- |
- const Extension* extension = ExtensionRegistry::Get(browser_context_)-> |
- enabled_extensions().GetByID((*it)->extension_id()); |
- // Don't listen for services associated only with disabled extensions. |
- if (!extension) |
- continue; |
- |
- // Platform apps may query for all services; other types of extensions are |
- // restricted to a whitelist. |
- if (!extension->is_platform_app() && |
- !IsServiceTypeWhitelisted(filter_value)) |
- continue; |
- |
- new_service_types.insert(filter_value); |
+ for (const EventListener* listener : GetValidOnServiceListListeners()) { |
+ std::string service_type; |
+ listener->filter()->GetStringASCII(kEventFilterServiceTypeKey, |
+ &service_type); |
+ new_service_types.insert(service_type); |
} |
// Find all the added and removed service types since last update. |
@@ -140,6 +121,22 @@ void MDnsAPI::OnDnsSdEvent(const std::string& service_type, |
std::vector<linked_ptr<mdns::MDnsService> > args; |
for (DnsSdRegistry::DnsSdServiceList::const_iterator it = services.begin(); |
it != services.end(); ++it) { |
+ if (static_cast<long>(args.size()) == |
+ api::mdns::MAX_SERVICE_INSTANCES_PER_EVENT) { |
+ // TODO(reddaly): This is not the most meaningful way of notifying the |
+ // application that something bad happened. It will go to the user's |
+ // console (which most users don't look at)and the developer will be none |
+ // the wiser. Instead, changing the event to pass the number of |
+ // discovered instances would allow the caller to know when the list is |
+ // truncated and tell the user something meaningful in the extension/app. |
+ WriteToConsole(service_type, |
+ content::CONSOLE_MESSAGE_LEVEL_WARNING, |
+ base::StringPrintf( |
+ "Truncating number of service instances in " |
+ "onServiceList to maximum allowed: %d", |
+ api::mdns::MAX_SERVICE_INSTANCES_PER_EVENT)); |
+ break; |
+ } |
linked_ptr<mdns::MDnsService> mdns_service = |
make_linked_ptr(new mdns::MDnsService); |
mdns_service->service_name = (*it).service_name; |
@@ -162,4 +159,62 @@ void MDnsAPI::OnDnsSdEvent(const std::string& service_type, |
extensions::EventRouter::Get(browser_context_)->BroadcastEvent(event.Pass()); |
} |
+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
|
+ std::set<const EventListener*> listeners; |
+ for (const auto& listener : |
+ extensions::EventRouter::Get(browser_context_)->listeners() |
+ .GetEventListenersByName(mdns::OnServiceList::kEventName)) { |
+ base::DictionaryValue* filter = (listener->filter()); |
+ |
+ std::string service_type; |
+ filter->GetStringASCII(kEventFilterServiceTypeKey, &service_type); |
+ if (service_type.empty()) |
+ continue; |
+ |
+ const Extension* extension = ExtensionRegistry::Get(browser_context_)-> |
+ enabled_extensions().GetByID(listener->extension_id()); |
+ // Don't listen for services associated only with disabled extensions. |
+ if (!extension) |
+ continue; |
+ |
+ // Platform apps may query for all services; other types of extensions are |
+ // restricted to a whitelist. |
+ if (!extension->is_platform_app() && |
+ !IsServiceTypeWhitelisted(service_type)) |
+ continue; |
+ |
+ listeners.insert(listener.get()); |
+ } |
+ return listeners; |
+} |
+ |
+void MDnsAPI::WriteToConsole(const std::string& service_type, |
+ content::ConsoleMessageLevel level, |
+ const std::string& message) { |
+ // Get all the extensions with an onServiceList listener for a particular |
+ // service type. |
+ std::set<std::string> extension_ids; |
+ 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.
|
+ extension_ids.insert(listener->extension_id()); |
+ } |
+ |
+ std::string logged_message(std::string("[chrome.mdns] ") + message); |
+ |
+ // Log to the consoles of the background pages for those extensions. |
+ for (const std::string& extension_id : extension_ids) { |
+ extensions::ExtensionHost* host = |
+ extensions::ProcessManager::Get(browser_context_) |
+ ->GetBackgroundHostForExtension(extension_id); |
+ if (!host) |
+ continue; |
+ content::RenderViewHost* rvh = host->render_view_host(); |
+ if (!rvh) |
+ continue; |
+ rvh->Send(new ExtensionMsg_AddMessageToConsole( |
+ rvh->GetRoutingID(), |
+ level, |
+ logged_message)); |
+ } |
+} |
+ |
} // namespace extensions |