| 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..6b170a253d2b0d4560066584ef27de010772c9d1
|
| --- /dev/null
|
| +++ b/chrome/browser/extensions/api/mdns/mdns_api.cc
|
| @@ -0,0 +1,125 @@
|
| +// 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/api/mdns/mdns_api_factory.h"
|
| +#include "chrome/browser/extensions/extension_service.h"
|
| +#include "chrome/browser/extensions/extension_system.h"
|
| +#include "chrome/browser/profiles/profile.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 {
|
| +
|
| +// Retrieves the list of service types that are declared in the extension's
|
| +// manifest file.
|
| +const std::vector<std::string> GetServiceTypesForExtension(
|
| + Profile* profile, std::string extension_id) {
|
| + const extensions::Extension* extension = ExtensionSystem::Get(profile)->
|
| + extension_service()->GetExtensionById(extension_id, false);
|
| + if (!extension) {
|
| + NOTREACHED();
|
| + return std::vector<std::string>();
|
| + }
|
| + const MDnsInfo& info = MDnsInfo::GetMDnsInfo(extension);
|
| + return info.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);
|
| + }
|
| +}
|
| +
|
| +void MDnsAPI::SetNsdRegistryForTesting(NsdRegistry* nsd_registry) {
|
| + nsd_registry_.reset(nsd_registry);
|
| +}
|
| +
|
| +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());
|
| + 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 << "\"";
|
| + service_type_to_extensions_map_[service_type].insert(extension_id);
|
| + 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(); it++) {
|
| + const std::string& service_type = it->first;
|
| + std::set<std::string>& extensions = it->second;
|
| + if (extensions.erase(extension_id)) {
|
| + if (!extensions.size()) {
|
| + DVLOG(1) << "Removed all watchers for service type \"" << service_type
|
| + << "\"";
|
| + nsd_registry()->UnregisterMDnsListener(service_type);
|
| + service_type_to_extensions_map_.erase(service_type);
|
| + }
|
| + }
|
| + }
|
| +}
|
| +
|
| +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
|
|
|