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

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 new mdns.getMaxServiceInstancesPerEvent() function and update unit tests 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 "extensions/browser/extension_registry.h" 12 #include "extensions/browser/extension_registry.h"
13 13
14 namespace extensions { 14 namespace extensions {
15 15
16 namespace mdns = api::mdns; 16 namespace mdns = api::mdns;
17 17
18 namespace { 18 namespace {
19 19
20 // Whitelisted mDNS service types. 20 // Whitelisted mDNS service types.
21 const char kCastServiceType[] = "_googlecast._tcp.local"; 21 const char kCastServiceType[] = "_googlecast._tcp.local";
22 const char kPrivetServiceType[] = "_privet._tcp.local"; 22 const char kPrivetServiceType[] = "_privet._tcp.local";
23 const char kTestServiceType[] = "_testing._tcp.local"; 23 const char kTestServiceType[] = "_testing._tcp.local";
24 24
25 // Maximum number of services that may be included in an onServiceList event
26 // as arguments.
27 const uint8 kMaxServicesPerOnServiceListEvent = 64;
28
25 bool IsServiceTypeWhitelisted(const std::string& service_type) { 29 bool IsServiceTypeWhitelisted(const std::string& service_type) {
26 return service_type == kCastServiceType || 30 return service_type == kCastServiceType ||
27 service_type == kPrivetServiceType || 31 service_type == kPrivetServiceType ||
28 service_type == kTestServiceType; 32 service_type == kTestServiceType;
29 } 33 }
30 34
31 } // namespace 35 } // namespace
32 36
33 MDnsAPI::MDnsAPI(content::BrowserContext* context) : browser_context_(context) { 37 MDnsAPI::MDnsAPI(content::BrowserContext* context) : browser_context_(context) {
34 DCHECK(browser_context_); 38 DCHECK(browser_context_);
(...skipping 98 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 (args.size() == kMaxServicesPerOnServiceListEvent) {
148 VLOG(logging::LOG_WARNING)
149 << "Truncating service instances reported in onServiceList event to "
150 << "max: "
151 << kMaxServicesPerOnServiceListEvent;
152 break;
153 }
143 linked_ptr<mdns::MDnsService> mdns_service = 154 linked_ptr<mdns::MDnsService> mdns_service =
144 make_linked_ptr(new mdns::MDnsService); 155 make_linked_ptr(new mdns::MDnsService);
145 mdns_service->service_name = (*it).service_name; 156 mdns_service->service_name = (*it).service_name;
146 mdns_service->service_host_port = (*it).service_host_port; 157 mdns_service->service_host_port = (*it).service_host_port;
147 mdns_service->ip_address = (*it).ip_address; 158 mdns_service->ip_address = (*it).ip_address;
148 mdns_service->service_data = (*it).service_data; 159 mdns_service->service_data = (*it).service_data;
149 args.push_back(mdns_service); 160 args.push_back(mdns_service);
150 } 161 }
151 162
152 scoped_ptr<base::ListValue> results = mdns::OnServiceList::Create(args); 163 scoped_ptr<base::ListValue> results = mdns::OnServiceList::Create(args);
153 scoped_ptr<Event> event( 164 scoped_ptr<Event> event(
154 new Event(mdns::OnServiceList::kEventName, results.Pass())); 165 new Event(mdns::OnServiceList::kEventName, results.Pass()));
155 event->restrict_to_browser_context = browser_context_; 166 event->restrict_to_browser_context = browser_context_;
156 event->filter_info.SetServiceType(service_type); 167 event->filter_info.SetServiceType(service_type);
157 168
158 // TODO(justinlin): To avoid having listeners without filters getting all 169 // TODO(justinlin): To avoid having listeners without filters getting all
159 // events, modify API to have this event require filters. 170 // events, modify API to have this event require filters.
160 // TODO(reddaly): If event isn't on whitelist, ensure it does not get 171 // TODO(reddaly): If event isn't on whitelist, ensure it does not get
161 // broadcast to extensions. 172 // broadcast to extensions.
162 extensions::EventRouter::Get(browser_context_)->BroadcastEvent(event.Pass()); 173 extensions::EventRouter::Get(browser_context_)->BroadcastEvent(event.Pass());
163 } 174 }
164 175
176 ExtensionFunction::ResponseAction
177 MdnsGetMaxServiceInstancesPerEventFunction::Run() {
178 return RespondNow(OneArgument(new base::FundamentalValue(
179 kMaxServicesPerOnServiceListEvent)));
180 }
181
165 } // namespace extensions 182 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698