Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1169)

Side by Side Diff: chrome/browser/extensions/api/mdns/mdns_api.cc

Issue 1040773002: Limit number of service instances passed to onServiceList listeners. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add missing semicolon Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 GetValidOnServiceListListeners(nullptr, &new_service_types);
mark a. foltz 2015/04/03 23:26:40 What is this nullptr argument? Optional arguments
Red Daly 2015/04/04 00:56:15 Done. I did it the other way to reduce code dupli
88 // Check all listeners for service type filters.
89 const EventListenerMap::ListenerList& listeners =
90 extensions::EventRouter::Get(browser_context_)
91 ->listeners()
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 }
116 92
117 // Find all the added and removed service types since last update. 93 // Find all the added and removed service types since last update.
118 std::set<std::string> added_service_types = 94 std::set<std::string> added_service_types =
119 base::STLSetDifference<std::set<std::string> >( 95 base::STLSetDifference<std::set<std::string> >(
120 new_service_types, service_types_); 96 new_service_types, service_types_);
121 std::set<std::string> removed_service_types = 97 std::set<std::string> removed_service_types =
122 base::STLSetDifference<std::set<std::string> >( 98 base::STLSetDifference<std::set<std::string> >(
123 service_types_, new_service_types); 99 service_types_, new_service_types);
124 100
125 // Update the registry. 101 // Update the registry.
126 DnsSdRegistry* registry = dns_sd_registry(); 102 DnsSdRegistry* registry = dns_sd_registry();
127 for (const auto& srv : added_service_types) { 103 for (const auto& srv : added_service_types) {
128 registry->RegisterDnsSdListener(srv); 104 registry->RegisterDnsSdListener(srv);
129 } 105 }
130 for (const auto& srv : removed_service_types) { 106 for (const auto& srv : removed_service_types) {
131 registry->UnregisterDnsSdListener(srv); 107 registry->UnregisterDnsSdListener(srv);
132 } 108 }
133 service_types_ = new_service_types; 109 service_types_ = new_service_types;
134 } 110 }
135 111
136 void MDnsAPI::OnDnsSdEvent(const std::string& service_type, 112 void MDnsAPI::OnDnsSdEvent(const std::string& service_type,
137 const DnsSdRegistry::DnsSdServiceList& services) { 113 const DnsSdRegistry::DnsSdServiceList& services) {
138 DCHECK(thread_checker_.CalledOnValidThread()); 114 DCHECK(thread_checker_.CalledOnValidThread());
139 115
140 std::vector<linked_ptr<mdns::MDnsService> > args; 116 std::vector<linked_ptr<mdns::MDnsService> > args;
141 for (DnsSdRegistry::DnsSdServiceList::const_iterator it = services.begin(); 117 for (DnsSdRegistry::DnsSdServiceList::const_iterator it = services.begin();
142 it != services.end(); ++it) { 118 it != services.end(); ++it) {
119 if (static_cast<long>(args.size()) ==
120 api::mdns::MAX_SERVICE_INSTANCES_PER_EVENT) {
mark a. foltz 2015/04/03 23:26:40 Can this constant be declared as size_t?
Red Daly 2015/04/04 00:56:15 I don't think there's currently support for that i
121 WriteToConsole(service_type,
122 content::CONSOLE_MESSAGE_LEVEL_WARNING,
123 base::StringPrintf(
124 "Truncating number of service instances in "
mark a. foltz 2015/04/03 23:26:40 Is there a prefix logged for this? If not, prepen
Red Daly 2015/04/04 00:56:15 Done.
125 "onServiceList to maximum allowed: %d",
126 api::mdns::MAX_SERVICE_INSTANCES_PER_EVENT));
mark a. foltz 2015/04/03 23:26:39 This is not the most meaningful way of notifying t
Red Daly 2015/04/04 00:56:15 I agree, added a TODO for the future enhancement.
127 break;
128 }
143 linked_ptr<mdns::MDnsService> mdns_service = 129 linked_ptr<mdns::MDnsService> mdns_service =
144 make_linked_ptr(new mdns::MDnsService); 130 make_linked_ptr(new mdns::MDnsService);
145 mdns_service->service_name = (*it).service_name; 131 mdns_service->service_name = (*it).service_name;
146 mdns_service->service_host_port = (*it).service_host_port; 132 mdns_service->service_host_port = (*it).service_host_port;
147 mdns_service->ip_address = (*it).ip_address; 133 mdns_service->ip_address = (*it).ip_address;
148 mdns_service->service_data = (*it).service_data; 134 mdns_service->service_data = (*it).service_data;
149 args.push_back(mdns_service); 135 args.push_back(mdns_service);
150 } 136 }
151 137
152 scoped_ptr<base::ListValue> results = mdns::OnServiceList::Create(args); 138 scoped_ptr<base::ListValue> results = mdns::OnServiceList::Create(args);
153 scoped_ptr<Event> event( 139 scoped_ptr<Event> event(
154 new Event(mdns::OnServiceList::kEventName, results.Pass())); 140 new Event(mdns::OnServiceList::kEventName, results.Pass()));
155 event->restrict_to_browser_context = browser_context_; 141 event->restrict_to_browser_context = browser_context_;
156 event->filter_info.SetServiceType(service_type); 142 event->filter_info.SetServiceType(service_type);
157 143
158 // TODO(justinlin): To avoid having listeners without filters getting all 144 // TODO(justinlin): To avoid having listeners without filters getting all
159 // events, modify API to have this event require filters. 145 // events, modify API to have this event require filters.
160 // TODO(reddaly): If event isn't on whitelist, ensure it does not get 146 // TODO(reddaly): If event isn't on whitelist, ensure it does not get
161 // broadcast to extensions. 147 // broadcast to extensions.
162 extensions::EventRouter::Get(browser_context_)->BroadcastEvent(event.Pass()); 148 extensions::EventRouter::Get(browser_context_)->BroadcastEvent(event.Pass());
163 } 149 }
164 150
151 void MDnsAPI::GetValidOnServiceListListeners(
152 std::set<const EventListener*>* listeners,
153 std::set<std::string>* service_types) {
154 for (const auto& listener :
155 extensions::EventRouter::Get(browser_context_)->listeners()
156 .GetEventListenersByName(mdns::OnServiceList::kEventName)) {
157 base::DictionaryValue* filter = (listener->filter());
158
159 std::string service_type;
160 filter->GetStringASCII(kEventFilterServiceTypeKey, &service_type);
161 if (service_type.empty())
162 continue;
163
164 const Extension* extension = ExtensionRegistry::Get(browser_context_)->
165 enabled_extensions().GetByID(listener->extension_id());
166 // Don't listen for services associated only with disabled extensions.
167 if (!extension)
168 continue;
169
170 // Platform apps may query for all services; other types of extensions are
171 // restricted to a whitelist.
172 if (!extension->is_platform_app() &&
173 !IsServiceTypeWhitelisted(service_type))
174 continue;
175
176 if (listeners)
177 listeners->insert(listener.get());
178 if (service_types)
179 service_types->insert(service_type);
180 }
181 }
182
183 void MDnsAPI::WriteToConsole(const std::string& service_type,
184 content::ConsoleMessageLevel level,
185 const std::string& message) {
186 // Get all the extensions with an onServiceList listener for a particular
187 // service type.
188 std::set<const EventListener*> listeners;
189 GetValidOnServiceListListeners(&listeners, nullptr);
190 std::set<std::string> extension_ids;
191 for (const EventListener* listener : listeners) {
192 extension_ids.insert(listener->extension_id());
193 }
194
195 // Log to the consoles of the background pages for those extensions.
196 for (const std::string& extension_id : extension_ids) {
197 extensions::ExtensionHost* host =
198 extensions::ProcessManager::Get(browser_context_)
199 ->GetBackgroundHostForExtension(extension_id);
200 if (!host)
201 continue;
202 content::RenderViewHost* rvh = host->render_view_host();
203 if (!rvh)
204 continue;
205 rvh->Send(new ExtensionMsg_AddMessageToConsole(
206 rvh->GetRoutingID(),
207 level,
208 message));
209 }
210 }
211
165 } // namespace extensions 212 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698