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

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: Created 7 years, 4 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..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(
Matt Perry 2013/08/30 00:43:29 you can return a reference here
justinlin 2013/09/03 21:21:56 Done. Have to use a lazy instance to return if ext
+ Profile* profile, std::string extension_id) {
Matt Perry 2013/08/30 00:43:29 pass strings by const ref
justinlin 2013/09/03 21:21:56 Done.
+ const extensions::Extension* extension = ExtensionSystem::Get(profile)->
+ extension_service()->GetExtensionById(extension_id, false);
+ if (!extension) {
+ NOTREACHED();
mark a. foltz 2013/08/30 01:35:52 Include a message
justinlin 2013/09/03 21:21:56 Done.
+ return std::vector<std::string>();
+ }
+ const MDnsInfo& info = MDnsInfo::GetMDnsInfo(extension);
mark a. foltz 2013/08/30 01:35:52 Combine this with the return statement?
justinlin 2013/09/03 21:21:56 Done.
+ 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) {
mark a. foltz 2013/08/30 01:35:52 Make clear (in a comment or the .h) that the MDnsA
justinlin 2013/09/03 21:21:56 Done.
+ 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);
mark a. foltz 2013/08/30 01:35:52 Check return value? I guess we don't care about du
justinlin 2013/09/03 21:21:56 Added DCHECK, we should never get duplicate insert
+ 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()) {
Matt Perry 2013/08/30 00:43:29 nit: size is not a boolean. use either size() == 0
justinlin 2013/09/03 21:21:56 Done.
+ DVLOG(1) << "Removed all watchers for service type \"" << service_type
+ << "\"";
+ nsd_registry()->UnregisterMDnsListener(service_type);
+ service_type_to_extensions_map_.erase(service_type);
Matt Perry 2013/08/30 00:43:29 This will invalidate your iterator. You should do
justinlin 2013/09/03 21:21:56 Done.
+ }
+ }
+ }
+}
+
+void MDnsAPI::OnMDnsEvent(const std::string& service_type,
Matt Perry 2013/08/30 00:43:29 This is where filtered events would come in really
justinlin 2013/09/03 21:21:56 Looking into using this. Does seem promising, but
Matt Perry 2013/09/03 22:24:04 Oh you're right, we don't have support for filters
+ 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.
mark a. foltz 2013/08/30 01:35:52 The registry should already have correctly filled
justinlin 2013/09/03 21:21:56 The TODO is to copy the information from a NsdServ
+ 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

Powered by Google App Engine
This is Rietveld 408576698