Chromium Code Reviews| 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(std::string service_type) { | |
|
mark a. foltz
2013/09/06 00:15:49
const std::string&
justinlin
2013/09/06 00:39:29
Done.
| |
| 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 (nsd_registry_.get()) { | |
| 39 nsd_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 = | |
|
mark a. foltz
2013/09/06 00:15:49
Is g_factory the naming convention for statics?
justinlin
2013/09/06 00:39:29
Yes, that's what used for ProfileKeyedApiFactory l
| |
| 49 LAZY_INSTANCE_INITIALIZER; | |
| 50 | |
| 51 // static | |
| 52 ProfileKeyedAPIFactory<MDnsAPI>* MDnsAPI::GetFactoryInstance() { | |
| 53 return &g_factory.Get(); | |
| 54 } | |
| 55 | |
| 56 void MDnsAPI::SetNsdRegistryForTesting(scoped_ptr<NsdRegistry> nsd_registry) { | |
| 57 nsd_registry_ = nsd_registry.Pass(); | |
| 58 } | |
| 59 | |
| 60 NsdRegistry* MDnsAPI::nsd_registry() { | |
| 61 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 62 if (!nsd_registry_.get()) { | |
| 63 nsd_registry_.reset(new extensions::NsdRegistry()); | |
| 64 nsd_registry_->AddObserver(this); | |
| 65 } | |
| 66 return nsd_registry_.get(); | |
| 67 } | |
| 68 | |
| 69 bool GetURLFromValue(const base::Value* in_value, std::string* out_value) { | |
|
mark a. foltz
2013/09/06 00:15:49
Put in anonymous { } namespace?
justinlin
2013/09/06 00:39:29
Oops, this was leftover, removed.
| |
| 70 return in_value && out_value && in_value->GetAsString(out_value); | |
| 71 } | |
| 72 | |
| 73 void MDnsAPI::OnListenerAdded(const EventListenerInfo& details) { | |
| 74 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 75 DCHECK_EQ(mdns::OnServiceList::kEventName, details.event_name); | |
| 76 UpdateMDnsListeners(details); | |
| 77 } | |
| 78 | |
| 79 void MDnsAPI::OnListenerRemoved(const EventListenerInfo& details) { | |
| 80 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 81 DCHECK_EQ(mdns::OnServiceList::kEventName, details.event_name); | |
| 82 UpdateMDnsListeners(details); | |
| 83 } | |
| 84 | |
| 85 void MDnsAPI::UpdateMDnsListeners(const EventListenerInfo& details) { | |
| 86 std::set<std::string> new_service_types; | |
| 87 | |
| 88 // Check all listeners for service type filers. | |
| 89 const EventListenerMap::ListenerList& listeners = | |
| 90 extensions::ExtensionSystem::Get(profile_)->event_router()-> | |
| 91 listeners().GetEventListenersByName(details.event_name); | |
| 92 for (EventListenerMap::ListenerList::const_iterator it = listeners.begin(); | |
| 93 it != listeners.end(); ++it) { | |
| 94 base::DictionaryValue* filter = ((*it)->filter.get()); | |
| 95 for (base::DictionaryValue::Iterator iter(*filter); | |
| 96 !iter.IsAtEnd(); iter.Advance()) { | |
| 97 } | |
| 98 | |
| 99 std::string filter_value; | |
| 100 filter->GetStringASCII(kEventFilterServiceTypeKey, &filter_value); | |
| 101 if (filter_value.empty()) | |
| 102 continue; | |
| 103 | |
| 104 new_service_types.insert(filter_value); | |
| 105 } | |
| 106 | |
| 107 // Find all the added and removed service types since last update. | |
| 108 std::set<std::string> added_service_types = | |
| 109 base::STLSetDifference<std::set<std::string> >( | |
| 110 service_types_, new_service_types); | |
| 111 std::set<std::string> removed_service_types = | |
| 112 base::STLSetDifference<std::set<std::string> >( | |
| 113 new_service_types, service_types_); | |
| 114 | |
| 115 // Update the registry. | |
| 116 NsdRegistry* registry = nsd_registry(); | |
| 117 for (std::set<std::string>::iterator it = added_service_types.begin(); | |
| 118 it != added_service_types.end(); ++it) { | |
| 119 if (IsServiceTypeWhitelisted(*it)) | |
| 120 registry->RegisterDnsSdListener(*it); | |
| 121 } | |
| 122 for (std::set<std::string>::iterator it = removed_service_types.begin(); | |
| 123 it != removed_service_types.end(); ++it) { | |
| 124 if (IsServiceTypeWhitelisted(*it)) | |
| 125 registry->UnregisterDnsSdListener(*it); | |
| 126 } | |
| 127 | |
| 128 service_types_ = new_service_types; | |
| 129 } | |
| 130 | |
| 131 void MDnsAPI::OnMDnsEvent(const std::string& service_type, | |
| 132 const NsdRegistry::NsdServiceList& services) { | |
| 133 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 134 if (services.size() == 0) | |
| 135 return; | |
| 136 | |
| 137 std::vector<linked_ptr<mdns::MDnsService> > args; | |
| 138 for (NsdRegistry::NsdServiceList::const_iterator it = services.begin(); | |
| 139 it != services.end(); ++it) { | |
| 140 linked_ptr<mdns::MDnsService> mdns_service = | |
| 141 make_linked_ptr(new mdns::MDnsService); | |
| 142 // TODO(justinlin, mfoltz): Copy data from service list. | |
| 143 mdns_service->service_name = service_type; | |
| 144 args.push_back(mdns_service); | |
| 145 } | |
| 146 | |
| 147 scoped_ptr<base::ListValue> results = mdns::OnServiceList::Create(args); | |
| 148 scoped_ptr<Event> event( | |
| 149 new Event(mdns::OnServiceList::kEventName, results.Pass())); | |
| 150 event->restrict_to_profile = profile_; | |
| 151 event->filter_info.SetServiceType(service_type); | |
| 152 | |
| 153 // TODO(justinlin): To avoid having listeners without filters getting all | |
| 154 // events, modify API to have this event require filters. | |
| 155 extensions::ExtensionSystem::Get(profile_)->event_router()-> | |
| 156 BroadcastEvent(event.Pass()); | |
| 157 } | |
| 158 | |
| 159 } // namespace extensions | |
| OLD | NEW |