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 | |
23 bool IsServiceTypeWhitelisted(std::string service_type) { | |
24 return service_type == kCastServiceType; | |
25 } | |
26 | |
27 } // namespace | |
28 | |
29 MDnsAPI::MDnsAPI(Profile* profile) : profile_(profile) { | |
30 DCHECK(profile_); | |
31 ExtensionSystem::Get(profile)->event_router()->RegisterObserver( | |
32 this, mdns::OnServiceList::kEventName); | |
33 } | |
34 | |
35 MDnsAPI::~MDnsAPI() { | |
36 if (nsd_registry_.get()) { | |
37 nsd_registry_->RemoveObserver(this); | |
38 } | |
39 } | |
40 | |
41 // static | |
42 MDnsAPI* MDnsAPI::Get(Profile* profile) { | |
43 return ProfileKeyedAPIFactory<MDnsAPI>::GetForProfile(profile); | |
44 } | |
45 | |
46 static base::LazyInstance<ProfileKeyedAPIFactory<MDnsAPI> > g_factory = | |
47 LAZY_INSTANCE_INITIALIZER; | |
48 | |
49 // static | |
50 ProfileKeyedAPIFactory<MDnsAPI>* MDnsAPI::GetFactoryInstance() { | |
51 return &g_factory.Get(); | |
52 } | |
53 | |
54 void MDnsAPI::SetNsdRegistryForTesting(scoped_ptr<NsdRegistry> nsd_registry) { | |
55 nsd_registry_ = nsd_registry.Pass(); | |
56 } | |
57 | |
58 NsdRegistry* MDnsAPI::nsd_registry() { | |
59 DCHECK(thread_checker_.CalledOnValidThread()); | |
60 if (!nsd_registry_.get()) { | |
61 nsd_registry_.reset(new extensions::NsdRegistry()); | |
62 nsd_registry_->AddObserver(this); | |
63 } | |
64 return nsd_registry_.get(); | |
65 } | |
66 | |
67 bool GetURLFromValue(const base::Value* in_value, std::string* out_value) { | |
68 return in_value && out_value && in_value->GetAsString(out_value); | |
69 } | |
70 | |
71 void MDnsAPI::OnListenerAdded(const EventListenerInfo& details) { | |
72 DCHECK(thread_checker_.CalledOnValidThread()); | |
73 DCHECK_EQ(mdns::OnServiceList::kEventName, details.event_name); | |
74 UpdateMDnsListeners(details); | |
75 } | |
76 | |
77 void MDnsAPI::OnListenerRemoved(const EventListenerInfo& details) { | |
78 DCHECK(thread_checker_.CalledOnValidThread()); | |
79 DCHECK_EQ(mdns::OnServiceList::kEventName, details.event_name); | |
80 UpdateMDnsListeners(details); | |
81 } | |
82 | |
83 void MDnsAPI::UpdateMDnsListeners(const EventListenerInfo& details) { | |
84 std::set<std::string> new_service_types; | |
85 | |
86 // Check all listeners for service type filers. | |
87 const EventListenerMap::ListenerList& listeners = | |
88 extensions::ExtensionSystem::Get(profile_)->event_router()-> | |
89 listeners().GetEventListenersByName(details.event_name); | |
90 for (EventListenerMap::ListenerList::const_iterator it = listeners.begin(); | |
91 it != listeners.end(); ++it) { | |
92 base::DictionaryValue* filter = ((*it)->filter.get()); | |
93 for (base::DictionaryValue::Iterator iter(*filter); | |
94 !iter.IsAtEnd(); iter.Advance()) { | |
95 } | |
96 | |
97 std::string filter_value; | |
98 filter->GetStringASCII(kEventFilterServiceTypeKey, &filter_value); | |
99 if (filter_value.empty()) | |
100 continue; | |
101 | |
102 new_service_types.insert(filter_value); | |
103 } | |
104 | |
105 // Find all the added and removed service types since last update. | |
106 std::set<std::string> added_service_types = | |
107 base::STLSetDifference<std::set<std::string> >( | |
108 service_types_, new_service_types); | |
109 std::set<std::string> removed_service_types = | |
110 base::STLSetDifference<std::set<std::string> >( | |
111 new_service_types, service_types_); | |
112 | |
113 // Update the registry. | |
114 NsdRegistry* registry = nsd_registry(); | |
115 for (std::set<std::string>::iterator it = added_service_types.begin(); | |
116 it != added_service_types.end(); ++it) { | |
117 if (IsServiceTypeWhitelisted(*it)) | |
118 registry->RegisterMDnsListener(*it); | |
119 } | |
120 for (std::set<std::string>::iterator it = removed_service_types.begin(); | |
121 it != removed_service_types.end(); ++it) { | |
122 if (IsServiceTypeWhitelisted(*it)) | |
123 registry->UnregisterMDnsListener(*it); | |
124 } | |
125 | |
126 service_types_ = new_service_types; | |
127 } | |
128 | |
129 void MDnsAPI::OnMDnsEvent(const std::string& service_type, | |
130 const NsdRegistry::NsdServiceList& services) { | |
131 DCHECK(thread_checker_.CalledOnValidThread()); | |
132 if (services.size() == 0) | |
133 return; | |
134 | |
135 std::vector<linked_ptr<mdns::MDnsService> > args; | |
136 for (NsdRegistry::NsdServiceList::const_iterator it = services.begin(); | |
137 it != services.end(); ++it) { | |
138 linked_ptr<mdns::MDnsService> mdns_service = | |
139 make_linked_ptr(new mdns::MDnsService); | |
140 // TODO(justinlin, mfoltz): Copy data from service list. | |
141 mdns_service->service_name = service_type; | |
142 args.push_back(mdns_service); | |
143 } | |
144 | |
145 EventFilteringInfo filter_info; | |
146 filter_info.SetServiceType(service_type); | |
Matt Perry
2013/09/05 20:14:18
nit: why not set this directly on event, e.g. even
justinlin
2013/09/05 21:44:04
Done.
| |
147 | |
148 scoped_ptr<base::ListValue> results = mdns::OnServiceList::Create(args); | |
149 scoped_ptr<Event> event( | |
150 new Event(mdns::OnServiceList::kEventName, results.Pass())); | |
151 event->restrict_to_profile = profile_; | |
152 event->filter_info = filter_info; | |
153 | |
154 extensions::ExtensionSystem::Get(profile_)->event_router()-> | |
155 BroadcastEvent(event.Pass()); | |
156 } | |
157 | |
158 } // namespace extensions | |
OLD | NEW |