Chromium Code Reviews| 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..b7e96855090b9add8e0ef6249bbd13c2b8b9c2af |
| --- /dev/null |
| +++ b/chrome/browser/extensions/api/mdns/mdns_api.cc |
| @@ -0,0 +1,158 @@ |
| +// 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"; |
| + |
| +bool IsServiceTypeWhitelisted(std::string service_type) { |
| + return service_type == kCastServiceType; |
| +} |
| + |
| +} // 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 = |
| + 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) { |
| + 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->RegisterMDnsListener(*it); |
| + } |
| + for (std::set<std::string>::iterator it = removed_service_types.begin(); |
| + it != removed_service_types.end(); ++it) { |
| + if (IsServiceTypeWhitelisted(*it)) |
| + registry->UnregisterMDnsListener(*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); |
| + } |
| + |
| + EventFilteringInfo filter_info; |
| + filter_info.SetServiceType(service_type); |
|
Matt Perry
2013/09/05 20:14:18
nit: why not set this directly on event, e.g. even
justinlin
2013/09/05 21:44:04
Done.
|
| + |
| + 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 = filter_info; |
| + |
| + extensions::ExtensionSystem::Get(profile_)->event_router()-> |
| + BroadcastEvent(event.Pass()); |
| +} |
| + |
| +} // namespace extensions |