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

Unified Diff: chrome/browser/extensions/api/mdns/mdns_api.cc

Issue 23437015: Initial chrome.mdns API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: nit Created 7 years, 3 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 side-by-side diff with in-line comments
Download patch
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
new file mode 100644
index 0000000000000000000000000000000000000000..e6b8ac7d1779ac41af272cce07f1b67741f35e6a
--- /dev/null
+++ b/chrome/browser/extensions/api/mdns/mdns_api.cc
@@ -0,0 +1,159 @@
+// Copyright (c) 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/extensions/api/mdns/mdns_api.h"
+
+#include <vector>
+
+#include "base/lazy_instance.h"
+#include "chrome/browser/extensions/extension_service.h"
+#include "chrome/browser/extensions/extension_system.h"
+#include "chrome/common/extensions/api/mdns.h"
+
+namespace extensions {
+
+namespace mdns = api::mdns;
+
+namespace {
+
+// Whitelisted mDNS service types.
+const char kCastServiceType[] = "_googlecast._tcp.local";
+const char kTestServiceType[] = "_testing._tcp.local";
+
+bool IsServiceTypeWhitelisted(std::string service_type) {
mark a. foltz 2013/09/06 00:15:49 const std::string&
justinlin 2013/09/06 00:39:29 Done.
+ return service_type == kCastServiceType ||
+ service_type == kTestServiceType;
+}
+
+} // namespace
+
+MDnsAPI::MDnsAPI(Profile* profile) : profile_(profile) {
+ DCHECK(profile_);
+ ExtensionSystem::Get(profile)->event_router()->RegisterObserver(
+ this, mdns::OnServiceList::kEventName);
+}
+
+MDnsAPI::~MDnsAPI() {
+ if (nsd_registry_.get()) {
+ nsd_registry_->RemoveObserver(this);
+ }
+}
+
+// static
+MDnsAPI* MDnsAPI::Get(Profile* profile) {
+ return ProfileKeyedAPIFactory<MDnsAPI>::GetForProfile(profile);
+}
+
+static base::LazyInstance<ProfileKeyedAPIFactory<MDnsAPI> > g_factory =
mark a. foltz 2013/09/06 00:15:49 Is g_factory the naming convention for statics?
justinlin 2013/09/06 00:39:29 Yes, that's what used for ProfileKeyedApiFactory l
+ LAZY_INSTANCE_INITIALIZER;
+
+// static
+ProfileKeyedAPIFactory<MDnsAPI>* MDnsAPI::GetFactoryInstance() {
+ return &g_factory.Get();
+}
+
+void MDnsAPI::SetNsdRegistryForTesting(scoped_ptr<NsdRegistry> nsd_registry) {
+ nsd_registry_ = nsd_registry.Pass();
+}
+
+NsdRegistry* MDnsAPI::nsd_registry() {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ if (!nsd_registry_.get()) {
+ nsd_registry_.reset(new extensions::NsdRegistry());
+ nsd_registry_->AddObserver(this);
+ }
+ return nsd_registry_.get();
+}
+
+bool GetURLFromValue(const base::Value* in_value, std::string* out_value) {
mark a. foltz 2013/09/06 00:15:49 Put in anonymous { } namespace?
justinlin 2013/09/06 00:39:29 Oops, this was leftover, removed.
+ return in_value && out_value && in_value->GetAsString(out_value);
+}
+
+void MDnsAPI::OnListenerAdded(const EventListenerInfo& details) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ DCHECK_EQ(mdns::OnServiceList::kEventName, details.event_name);
+ UpdateMDnsListeners(details);
+}
+
+void MDnsAPI::OnListenerRemoved(const EventListenerInfo& details) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ DCHECK_EQ(mdns::OnServiceList::kEventName, details.event_name);
+ UpdateMDnsListeners(details);
+}
+
+void MDnsAPI::UpdateMDnsListeners(const EventListenerInfo& details) {
+ std::set<std::string> new_service_types;
+
+ // Check all listeners for service type filers.
+ const EventListenerMap::ListenerList& listeners =
+ extensions::ExtensionSystem::Get(profile_)->event_router()->
+ listeners().GetEventListenersByName(details.event_name);
+ for (EventListenerMap::ListenerList::const_iterator it = listeners.begin();
+ it != listeners.end(); ++it) {
+ base::DictionaryValue* filter = ((*it)->filter.get());
+ for (base::DictionaryValue::Iterator iter(*filter);
+ !iter.IsAtEnd(); iter.Advance()) {
+ }
+
+ std::string filter_value;
+ filter->GetStringASCII(kEventFilterServiceTypeKey, &filter_value);
+ if (filter_value.empty())
+ continue;
+
+ new_service_types.insert(filter_value);
+ }
+
+ // Find all the added and removed service types since last update.
+ std::set<std::string> added_service_types =
+ base::STLSetDifference<std::set<std::string> >(
+ service_types_, new_service_types);
+ std::set<std::string> removed_service_types =
+ base::STLSetDifference<std::set<std::string> >(
+ new_service_types, service_types_);
+
+ // Update the registry.
+ NsdRegistry* registry = nsd_registry();
+ for (std::set<std::string>::iterator it = added_service_types.begin();
+ it != added_service_types.end(); ++it) {
+ if (IsServiceTypeWhitelisted(*it))
+ registry->RegisterDnsSdListener(*it);
+ }
+ for (std::set<std::string>::iterator it = removed_service_types.begin();
+ it != removed_service_types.end(); ++it) {
+ if (IsServiceTypeWhitelisted(*it))
+ registry->UnregisterDnsSdListener(*it);
+ }
+
+ service_types_ = new_service_types;
+}
+
+void MDnsAPI::OnMDnsEvent(const std::string& service_type,
+ const NsdRegistry::NsdServiceList& services) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ if (services.size() == 0)
+ return;
+
+ std::vector<linked_ptr<mdns::MDnsService> > args;
+ for (NsdRegistry::NsdServiceList::const_iterator it = services.begin();
+ it != services.end(); ++it) {
+ linked_ptr<mdns::MDnsService> mdns_service =
+ make_linked_ptr(new mdns::MDnsService);
+ // TODO(justinlin, mfoltz): Copy data from service list.
+ mdns_service->service_name = service_type;
+ args.push_back(mdns_service);
+ }
+
+ scoped_ptr<base::ListValue> results = mdns::OnServiceList::Create(args);
+ scoped_ptr<Event> event(
+ new Event(mdns::OnServiceList::kEventName, results.Pass()));
+ event->restrict_to_profile = profile_;
+ event->filter_info.SetServiceType(service_type);
+
+ // TODO(justinlin): To avoid having listeners without filters getting all
+ // events, modify API to have this event require filters.
+ extensions::ExtensionSystem::Get(profile_)->event_router()->
+ BroadcastEvent(event.Pass());
+}
+
+} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698