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..7b0363007869a95982503e93356901f0823f5f7b |
| --- /dev/null |
| +++ b/chrome/browser/extensions/api/mdns/mdns_api.cc |
| @@ -0,0 +1,145 @@ |
| +// 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 "chrome/browser/extensions/extension_service.h" |
| +#include "chrome/browser/extensions/extension_system.h" |
| +#include "chrome/common/extensions/api/mdns.h" |
| +#include "chrome/common/extensions/api/mdns/mdns_manifest_handler.h" |
| +#include "extensions/common/manifest_constants.h" |
| + |
| +namespace extensions { |
| + |
| +namespace mdns = api::mdns; |
| + |
| +namespace { |
| + |
| +static base::LazyInstance<std::vector<std::string> > g_empty_service_types = |
| + LAZY_INSTANCE_INITIALIZER; |
| + |
| +// Retrieves the list of service types that are declared in the extension's |
| +// manifest file. |
| +const std::vector<std::string>& GetServiceTypesForExtension( |
| + Profile* profile, const std::string& extension_id) { |
| + const extensions::Extension* extension = ExtensionSystem::Get(profile)-> |
| + extension_service()->GetExtensionById(extension_id, false); |
| + if (!extension) { |
| + NOTREACHED(); |
| + LOG(ERROR) << "Failed to find extension " << extension_id << "."; |
| + return g_empty_service_types.Get(); |
| + } |
| + return MDnsInfo::GetMDnsInfo(extension).service_types; |
| +} |
| + |
| +} // 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(); |
| +} |
| + |
| +void MDnsAPI::OnListenerAdded(const EventListenerInfo& details) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + LOG(ERROR) << "LISTENER ADDED"; |
|
Matt Perry
2013/09/03 22:24:04
fyi
justinlin
2013/09/05 05:14:46
Done...
|
| + const std::string& extension_id = details.extension_id; |
| + const std::vector<std::string>& service_types = |
| + GetServiceTypesForExtension(profile_, extension_id); |
| + for (std::vector<std::string>::const_iterator it = service_types.begin(); |
| + it != service_types.end(); ++it) { |
| + // Start watchers for all the service types this extension has declared. |
| + const std::string& service_type = *it; |
| + DVLOG(1) << "Registered watcher for service type \"" << service_type |
| + << "\" for extension \"" << extension_id << "\""; |
| + if (!service_type_to_extensions_map_[service_type].insert( |
| + extension_id).second) { |
| + NOTREACHED(); |
| + } |
| + nsd_registry()->RegisterMDnsListener(service_type); |
| + } |
| +} |
| + |
| +void MDnsAPI::OnListenerRemoved(const EventListenerInfo& details) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + const std::string& extension_id = details.extension_id; |
| + for (ServiceTypeToExtensionsMap::iterator it = |
| + service_type_to_extensions_map_.begin(); |
| + it != service_type_to_extensions_map_.end();) { |
| + const std::string& service_type = it->first; |
| + std::set<std::string>& extensions = it->second; |
| + if (extensions.erase(extension_id)) { |
| + if (extensions.empty()) { |
| + DVLOG(1) << "Removed all watchers for service type \"" << service_type |
| + << "\""; |
| + nsd_registry()->UnregisterMDnsListener(service_type); |
| + service_type_to_extensions_map_.erase(it++); |
| + continue; |
| + } |
| + } |
| + it++; |
| + } |
| +} |
| + |
| +void MDnsAPI::OnMDnsEvent(const std::string& service_type, |
| + const NsdRegistry::NsdServiceList& services) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + if (services.size() > 0 && |
| + service_type_to_extensions_map_.find(service_type) != |
| + service_type_to_extensions_map_.end()) { |
| + const std::set<std::string>& extensions = |
| + service_type_to_extensions_map_[service_type]; |
| + for (std::set<std::string>::iterator it = extensions.begin(); |
| + it != extensions.end(); ++it) { |
| + 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): Correctly fill in service info. |
| + 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())); |
| + extensions::ExtensionSystem::Get(profile_)->event_router()-> |
| + DispatchEventToExtension(*it, event.Pass()); |
| + } |
| + } |
| +} |
| + |
| +} // namespace extensions |