OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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/lazy_instance.h" |
| 10 #include "chrome/browser/extensions/extension_service.h" |
| 11 #include "chrome/browser/extensions/extension_system.h" |
| 12 #include "chrome/common/extensions/api/mdns.h" |
| 13 |
| 14 namespace extensions { |
| 15 |
| 16 namespace mdns = api::mdns; |
| 17 |
| 18 namespace { |
| 19 |
| 20 // Whitelisted mDNS service types. |
| 21 const char kCastServiceType[] = "_googlecast._tcp.local"; |
| 22 const char kTestServiceType[] = "_testing._tcp.local"; |
| 23 |
| 24 bool IsServiceTypeWhitelisted(const std::string& service_type) { |
| 25 return service_type == kCastServiceType || |
| 26 service_type == kTestServiceType; |
| 27 } |
| 28 |
| 29 } // namespace |
| 30 |
| 31 MDnsAPI::MDnsAPI(Profile* profile) : profile_(profile) { |
| 32 DCHECK(profile_); |
| 33 ExtensionSystem::Get(profile)->event_router()->RegisterObserver( |
| 34 this, mdns::OnServiceList::kEventName); |
| 35 } |
| 36 |
| 37 MDnsAPI::~MDnsAPI() { |
| 38 if (dns_sd_registry_.get()) { |
| 39 dns_sd_registry_->RemoveObserver(this); |
| 40 } |
| 41 } |
| 42 |
| 43 // static |
| 44 MDnsAPI* MDnsAPI::Get(Profile* profile) { |
| 45 return ProfileKeyedAPIFactory<MDnsAPI>::GetForProfile(profile); |
| 46 } |
| 47 |
| 48 static base::LazyInstance<ProfileKeyedAPIFactory<MDnsAPI> > g_factory = |
| 49 LAZY_INSTANCE_INITIALIZER; |
| 50 |
| 51 // static |
| 52 ProfileKeyedAPIFactory<MDnsAPI>* MDnsAPI::GetFactoryInstance() { |
| 53 return &g_factory.Get(); |
| 54 } |
| 55 |
| 56 void MDnsAPI::SetDnsSdRegistryForTesting( |
| 57 scoped_ptr<DnsSdRegistry> dns_sd_registry) { |
| 58 dns_sd_registry_ = dns_sd_registry.Pass(); |
| 59 } |
| 60 |
| 61 DnsSdRegistry* MDnsAPI::dns_sd_registry() { |
| 62 DCHECK(thread_checker_.CalledOnValidThread()); |
| 63 if (!dns_sd_registry_.get()) { |
| 64 dns_sd_registry_.reset(new extensions::DnsSdRegistry()); |
| 65 dns_sd_registry_->AddObserver(this); |
| 66 } |
| 67 return dns_sd_registry_.get(); |
| 68 } |
| 69 |
| 70 void MDnsAPI::OnListenerAdded(const EventListenerInfo& details) { |
| 71 DCHECK(thread_checker_.CalledOnValidThread()); |
| 72 UpdateMDnsListeners(details); |
| 73 } |
| 74 |
| 75 void MDnsAPI::OnListenerRemoved(const EventListenerInfo& details) { |
| 76 DCHECK(thread_checker_.CalledOnValidThread()); |
| 77 UpdateMDnsListeners(details); |
| 78 } |
| 79 |
| 80 void MDnsAPI::UpdateMDnsListeners(const EventListenerInfo& details) { |
| 81 std::set<std::string> new_service_types; |
| 82 |
| 83 // Check all listeners for service type filers. |
| 84 const EventListenerMap::ListenerList& listeners = |
| 85 extensions::ExtensionSystem::Get(profile_)->event_router()-> |
| 86 listeners().GetEventListenersByName(details.event_name); |
| 87 for (EventListenerMap::ListenerList::const_iterator it = listeners.begin(); |
| 88 it != listeners.end(); ++it) { |
| 89 base::DictionaryValue* filter = ((*it)->filter.get()); |
| 90 for (base::DictionaryValue::Iterator iter(*filter); |
| 91 !iter.IsAtEnd(); iter.Advance()) { |
| 92 } |
| 93 |
| 94 std::string filter_value; |
| 95 filter->GetStringASCII(kEventFilterServiceTypeKey, &filter_value); |
| 96 if (filter_value.empty()) |
| 97 continue; |
| 98 |
| 99 new_service_types.insert(filter_value); |
| 100 } |
| 101 |
| 102 // Find all the added and removed service types since last update. |
| 103 std::set<std::string> added_service_types = |
| 104 base::STLSetDifference<std::set<std::string> >( |
| 105 service_types_, new_service_types); |
| 106 std::set<std::string> removed_service_types = |
| 107 base::STLSetDifference<std::set<std::string> >( |
| 108 new_service_types, service_types_); |
| 109 |
| 110 // Update the registry. |
| 111 DnsSdRegistry* registry = dns_sd_registry(); |
| 112 for (std::set<std::string>::iterator it = added_service_types.begin(); |
| 113 it != added_service_types.end(); ++it) { |
| 114 if (IsServiceTypeWhitelisted(*it)) |
| 115 registry->RegisterDnsSdListener(*it); |
| 116 } |
| 117 for (std::set<std::string>::iterator it = removed_service_types.begin(); |
| 118 it != removed_service_types.end(); ++it) { |
| 119 if (IsServiceTypeWhitelisted(*it)) |
| 120 registry->UnregisterDnsSdListener(*it); |
| 121 } |
| 122 |
| 123 service_types_ = new_service_types; |
| 124 } |
| 125 |
| 126 void MDnsAPI::OnDnsSdEvent(const std::string& service_type, |
| 127 const DnsSdRegistry::DnsSdServiceList& services) { |
| 128 DCHECK(thread_checker_.CalledOnValidThread()); |
| 129 if (services.size() == 0) |
| 130 return; |
| 131 |
| 132 std::vector<linked_ptr<mdns::MDnsService> > args; |
| 133 for (DnsSdRegistry::DnsSdServiceList::const_iterator it = services.begin(); |
| 134 it != services.end(); ++it) { |
| 135 linked_ptr<mdns::MDnsService> mdns_service = |
| 136 make_linked_ptr(new mdns::MDnsService); |
| 137 // TODO(justinlin, mfoltz): Copy data from service list. |
| 138 mdns_service->service_name = service_type; |
| 139 args.push_back(mdns_service); |
| 140 } |
| 141 |
| 142 scoped_ptr<base::ListValue> results = mdns::OnServiceList::Create(args); |
| 143 scoped_ptr<Event> event( |
| 144 new Event(mdns::OnServiceList::kEventName, results.Pass())); |
| 145 event->restrict_to_profile = profile_; |
| 146 event->filter_info.SetServiceType(service_type); |
| 147 |
| 148 // TODO(justinlin): To avoid having listeners without filters getting all |
| 149 // events, modify API to have this event require filters. |
| 150 extensions::ExtensionSystem::Get(profile_)->event_router()-> |
| 151 BroadcastEvent(event.Pass()); |
| 152 } |
| 153 |
| 154 } // namespace extensions |
OLD | NEW |