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

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: Change mdns.maxServiceInstancesPerEvent to mdns.MAX_SERVICE_INSTANCES_PER_EVENT, use StringPrintf 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 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
133 service_types_ = new_service_types; 137 service_types_ = new_service_types;
134 } 138 }
135 139
136 void MDnsAPI::OnDnsSdEvent(const std::string& service_type, 140 void MDnsAPI::OnDnsSdEvent(const std::string& service_type,
137 const DnsSdRegistry::DnsSdServiceList& services) { 141 const DnsSdRegistry::DnsSdServiceList& services) {
138 DCHECK(thread_checker_.CalledOnValidThread()); 142 DCHECK(thread_checker_.CalledOnValidThread());
139 143
140 std::vector<linked_ptr<mdns::MDnsService> > args; 144 std::vector<linked_ptr<mdns::MDnsService> > args;
141 for (DnsSdRegistry::DnsSdServiceList::const_iterator it = services.begin(); 145 for (DnsSdRegistry::DnsSdServiceList::const_iterator it = services.begin();
142 it != services.end(); ++it) { 146 it != services.end(); ++it) {
147 if (static_cast<long>(args.size()) ==
148 api::mdns::MAX_SERVICE_INSTANCES_PER_EVENT) {
149 std::string msg(base::StringPrintf(
150 "Truncating service instances in onServiceList event to max: %d",
151 api::mdns::MAX_SERVICE_INSTANCES_PER_EVENT));
152 WriteToConsole(service_type,
153 content::CONSOLE_MESSAGE_LEVEL_WARNING,
154 msg);
155 VLOG(logging::LOG_WARNING) << msg;
156 break;
157 }
143 linked_ptr<mdns::MDnsService> mdns_service = 158 linked_ptr<mdns::MDnsService> mdns_service =
144 make_linked_ptr(new mdns::MDnsService); 159 make_linked_ptr(new mdns::MDnsService);
145 mdns_service->service_name = (*it).service_name; 160 mdns_service->service_name = (*it).service_name;
146 mdns_service->service_host_port = (*it).service_host_port; 161 mdns_service->service_host_port = (*it).service_host_port;
147 mdns_service->ip_address = (*it).ip_address; 162 mdns_service->ip_address = (*it).ip_address;
148 mdns_service->service_data = (*it).service_data; 163 mdns_service->service_data = (*it).service_data;
149 args.push_back(mdns_service); 164 args.push_back(mdns_service);
150 } 165 }
151 166
152 scoped_ptr<base::ListValue> results = mdns::OnServiceList::Create(args); 167 scoped_ptr<base::ListValue> results = mdns::OnServiceList::Create(args);
153 scoped_ptr<Event> event( 168 scoped_ptr<Event> event(
154 new Event(mdns::OnServiceList::kEventName, results.Pass())); 169 new Event(mdns::OnServiceList::kEventName, results.Pass()));
155 event->restrict_to_browser_context = browser_context_; 170 event->restrict_to_browser_context = browser_context_;
156 event->filter_info.SetServiceType(service_type); 171 event->filter_info.SetServiceType(service_type);
157 172
158 // TODO(justinlin): To avoid having listeners without filters getting all 173 // TODO(justinlin): To avoid having listeners without filters getting all
159 // events, modify API to have this event require filters. 174 // events, modify API to have this event require filters.
160 // TODO(reddaly): If event isn't on whitelist, ensure it does not get 175 // TODO(reddaly): If event isn't on whitelist, ensure it does not get
161 // broadcast to extensions. 176 // broadcast to extensions.
162 extensions::EventRouter::Get(browser_context_)->BroadcastEvent(event.Pass()); 177 extensions::EventRouter::Get(browser_context_)->BroadcastEvent(event.Pass());
163 } 178 }
164 179
180 void MDnsAPI::WriteToConsole(const std::string& service_type,
181 content::ConsoleMessageLevel level,
182 const std::string& message) {
183 std::set<std::string> extension_ids;
184
185 // Check all listeners for service type filters.
not at google - send to devlin 2015/04/03 20:36:00 Much of this code does look very similar to what's
Red Daly 2015/04/03 21:12:22 Done.
186 for (const auto& listener :
187 extensions::EventRouter::Get(browser_context_)->listeners()
188 .GetEventListenersByName(mdns::OnServiceList::kEventName)) {
189 base::DictionaryValue* filter = (listener->filter());
190
191 std::string filter_value;
192 filter->GetStringASCII(kEventFilterServiceTypeKey, &filter_value);
193 if (filter_value.empty())
194 continue;
195
196 const Extension* extension = ExtensionRegistry::Get(browser_context_)->
197 enabled_extensions().GetByID(listener->extension_id());
198 // Don't listen for services associated only with disabled extensions.
199 if (!extension)
200 continue;
201
202 // Platform apps may query for all services; other types of extensions are
203 // restricted to a whitelist.
204 if (!extension->is_platform_app() &&
205 !IsServiceTypeWhitelisted(filter_value))
206 continue;
207
208 extension_ids.insert(listener->extension_id());
209 }
210
211 // Log to the consoles of the background pages of relevant listeners.
212 for (const std::string& extension_id : extension_ids) {
213 extensions::ExtensionHost* host =
214 extensions::ProcessManager::Get(browser_context_)
215 ->GetBackgroundHostForExtension(extension_id);
216 if (!host)
217 continue;
218 content::RenderProcessHost* rph = host->render_process_host();
219 content::RenderViewHost* rvh = host->render_view_host();
220 if (!rvh || !rph)
221 continue;
222 rph->Send(new ExtensionMsg_AddMessageToConsole(
223 rvh->GetRoutingID(),
not at google - send to devlin 2015/04/03 20:35:59 You should be able to send directly to the RenderV
Red Daly 2015/04/03 21:12:22 Done.
224 level,
225 message));
226 }
227 }
228
165 } // namespace extensions 229 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/mdns/mdns_api.h ('k') | chrome/browser/extensions/api/mdns/mdns_api_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698