Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/extensions/api/mdns/mdns_api.h" | |
| 6 | |
| 7 #include <vector> | |
| 8 | |
| 9 #include "base/time/time.h" | |
| 10 #include "chrome/browser/extensions/api/mdns/mdns_api_factory.h" | |
| 11 #include "chrome/browser/extensions/event_router.h" | |
| 12 #include "chrome/browser/extensions/extension_service.h" | |
| 13 #include "chrome/browser/extensions/extension_system.h" | |
| 14 #include "chrome/browser/profiles/profile.h" | |
| 15 #include "chrome/common/extensions/api/mdns.h" | |
| 16 #include "chrome/common/extensions/api/mdns/mdns_manifest_handler.h" | |
| 17 #include "content/public/browser/browser_thread.h" | |
| 18 #include "extensions/common/manifest_constants.h" | |
| 19 | |
| 20 using base::TimeDelta; | |
| 21 using content::BrowserThread; | |
| 22 | |
| 23 namespace extensions { | |
| 24 | |
| 25 namespace mdns = api::mdns; | |
| 26 | |
| 27 MDnsAPI::MDnsAPI(Profile* profile) : profile_(profile) { | |
| 28 DCHECK(profile_); | |
| 29 ExtensionSystem::Get(profile)->event_router()->RegisterObserver( | |
| 30 this, mdns::OnServiceList::kEventName); | |
| 31 } | |
| 32 | |
| 33 MDnsAPI::~MDnsAPI() {} | |
| 34 | |
| 35 NsdRegistry* MDnsAPI::nsd_registry() { | |
| 36 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 37 if (!nsd_registry_.get()) { | |
| 38 nsd_registry_.reset(new extensions::NsdRegistry(this)); | |
| 39 } | |
| 40 return nsd_registry_.get(); | |
| 41 } | |
| 42 | |
| 43 void MDnsAPI::OnListenerAdded(const EventListenerInfo& details) { | |
| 44 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 45 const std::string& extension_id = details.extension_id; | |
| 46 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.
| |
| 47 extension_service()->GetExtensionById(extension_id, false); | |
| 48 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.
| |
| 49 extension->GetManifestData(manifest_keys::kMDnsServiceTypes)); | |
| 50 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.
| |
| 51 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.
| |
| 52 for (std::vector<std::string>::iterator it = info->service_types.begin(); | |
| 53 it != info->service_types.end(); ++it) { | |
| 54 const std::string& service_type = *it; | |
| 55 DVLOG(1) << "Registered listener for service type " << service_type | |
| 56 << " for extension " << extension_id; | |
| 57 service_type_to_extension_map_[service_type].insert(extension_id); | |
| 58 nsd_registry->RegisterMDnsListener(service_type); | |
| 59 } | |
| 60 } | |
| 61 | |
| 62 void MDnsAPI::OnListenerRemoved(const EventListenerInfo& details) { | |
| 63 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 64 | |
| 65 // TODO(justinlin): Check if unloading extension removes all listeners. | |
| 66 const std::string& extension_id = details.extension_id; | |
| 67 for (ServiceTypeToExtensionsMap::iterator it = | |
| 68 service_type_to_extension_map_.begin(); | |
| 69 it != service_type_to_extension_map_.end(); it++) { | |
| 70 const std::string& service_type = it->first; | |
| 71 std::set<std::string>& extensions = it->second; | |
| 72 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
| |
| 73 MDnsAPIFactory::GetForProfile(profile_)->nsd_registry(); | |
| 74 if (extensions.erase(extension_id)) { | |
| 75 if (!extensions.size()) { | |
| 76 nsd_registry->UnregisterMDnsListener(service_type); | |
| 77 service_type_to_extension_map_.erase(service_type); | |
| 78 } | |
| 79 } | |
| 80 } | |
| 81 } | |
| 82 | |
| 83 void MDnsAPI::OnMDnsEvent(const std::string& service_type, | |
| 84 const NsdRegistry::NsdDeviceList& devices) { | |
| 85 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 86 | |
| 87 if (service_type_to_extension_map_.find(service_type) != | |
| 88 service_type_to_extension_map_.end()) { | |
| 89 const std::set<std::string>& extensions = | |
| 90 service_type_to_extension_map_[service_type]; | |
| 91 for (std::set<std::string>::iterator it = extensions.begin(); | |
| 92 it != extensions.end(); ++it) { | |
| 93 std::vector<linked_ptr<api::mdns::MDnsService> > args; | |
| 94 // TODO(justinlin,mfoltz): Fill in service object. | |
| 95 scoped_ptr<base::ListValue> results = | |
| 96 api::mdns::OnServiceList::Create(args); | |
| 97 scoped_ptr<Event> event( | |
| 98 new Event(mdns::OnServiceList::kEventName, results.Pass())); | |
| 99 extensions::ExtensionSystem::Get(profile_)->event_router()-> | |
| 100 BroadcastEvent(event.Pass()); | |
| 101 } | |
| 102 } | |
| 103 } | |
| 104 | |
| 105 } // namespace extensions | |
| OLD | NEW |