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..3b43527ee3b6f596f2a94d727000c7e19b72bd1e |
--- /dev/null |
+++ b/chrome/browser/extensions/api/mdns/mdns_api.cc |
@@ -0,0 +1,105 @@ |
+// Copyright (c) 2012 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/time/time.h" |
+#include "chrome/browser/extensions/api/mdns/mdns_api_factory.h" |
+#include "chrome/browser/extensions/event_router.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 "content/public/browser/browser_thread.h" |
+#include "extensions/common/manifest_constants.h" |
+ |
+using base::TimeDelta; |
+using content::BrowserThread; |
+ |
+namespace extensions { |
+ |
+namespace mdns = api::mdns; |
+ |
+MDnsAPI::MDnsAPI(Profile* profile) : profile_(profile) { |
+ DCHECK(profile_); |
+ ExtensionSystem::Get(profile)->event_router()->RegisterObserver( |
+ this, mdns::OnServiceList::kEventName); |
+} |
+ |
+MDnsAPI::~MDnsAPI() {} |
+ |
+NsdRegistry* MDnsAPI::nsd_registry() { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ if (!nsd_registry_.get()) { |
+ nsd_registry_.reset(new extensions::NsdRegistry(this)); |
+ } |
+ return nsd_registry_.get(); |
+} |
+ |
+void MDnsAPI::OnListenerAdded(const EventListenerInfo& details) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ const std::string& extension_id = details.extension_id; |
+ const extensions::Extension* extension = ExtensionSystem::Get(profile_)-> |
mark a. foltz
2013/08/27 00:35:42
extension guaranteed to be non-NULL?
justinlin
2013/08/27 01:50:20
Done. Should be.. but added check.
|
+ extension_service()->GetExtensionById(extension_id, false); |
+ MDnsInfo* info = static_cast<MDnsInfo*>( |
mark a. foltz
2013/08/27 00:35:42
Guaranteed to be non-NULL?
justinlin
2013/08/27 01:50:20
Done. Now it is.
|
+ extension->GetManifestData(manifest_keys::kMDnsServiceTypes)); |
+ NsdRegistry* nsd_registry = |
mark a. foltz
2013/08/27 00:35:42
Why not use nsd_registry()?
justinlin
2013/08/27 01:50:20
Done.
|
+ MDnsAPIFactory::GetForProfile(profile_)->nsd_registry(); |
mark a. foltz
2013/08/27 00:35:42
Maybe a method to return the service_types vector
justinlin
2013/08/27 01:50:20
Done.
|
+ for (std::vector<std::string>::iterator it = info->service_types.begin(); |
+ it != info->service_types.end(); ++it) { |
+ const std::string& service_type = *it; |
+ DVLOG(1) << "Registered listener for service type " << service_type |
+ << " for extension " << extension_id; |
+ service_type_to_extension_map_[service_type].insert(extension_id); |
+ nsd_registry->RegisterMDnsListener(service_type); |
+ } |
+} |
+ |
+void MDnsAPI::OnListenerRemoved(const EventListenerInfo& details) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ |
+ // TODO(justinlin): Check if unloading extension removes all listeners. |
+ const std::string& extension_id = details.extension_id; |
+ for (ServiceTypeToExtensionsMap::iterator it = |
+ service_type_to_extension_map_.begin(); |
+ it != service_type_to_extension_map_.end(); it++) { |
+ const std::string& service_type = it->first; |
+ std::set<std::string>& extensions = it->second; |
+ NsdRegistry* nsd_registry = |
mark a. foltz
2013/08/27 00:35:42
nsd_registry()?
justinlin
2013/08/27 01:50:20
Done. Made it private too. Oh right, we only neede
|
+ MDnsAPIFactory::GetForProfile(profile_)->nsd_registry(); |
+ if (extensions.erase(extension_id)) { |
+ if (!extensions.size()) { |
+ nsd_registry->UnregisterMDnsListener(service_type); |
+ service_type_to_extension_map_.erase(service_type); |
+ } |
+ } |
+ } |
+} |
+ |
+void MDnsAPI::OnMDnsEvent(const std::string& service_type, |
+ const NsdRegistry::NsdDeviceList& devices) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ |
+ if (service_type_to_extension_map_.find(service_type) != |
+ service_type_to_extension_map_.end()) { |
+ const std::set<std::string>& extensions = |
+ service_type_to_extension_map_[service_type]; |
+ for (std::set<std::string>::iterator it = extensions.begin(); |
+ it != extensions.end(); ++it) { |
+ std::vector<linked_ptr<api::mdns::MDnsService> > args; |
+ // TODO(justinlin,mfoltz): Fill in service object. |
+ scoped_ptr<base::ListValue> results = |
+ api::mdns::OnServiceList::Create(args); |
+ scoped_ptr<Event> event( |
+ new Event(mdns::OnServiceList::kEventName, results.Pass())); |
+ extensions::ExtensionSystem::Get(profile_)->event_router()-> |
+ BroadcastEvent(event.Pass()); |
+ } |
+ } |
+} |
+ |
+} // namespace extensions |