Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(78)

Side by Side Diff: chrome/browser/extensions/api/mdns/mdns_api.cc

Issue 23437015: Initial chrome.mdns API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address comments Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "chrome/browser/extensions/extension_service.h"
8 #include "chrome/browser/extensions/extension_system.h"
9 #include "chrome/common/extensions/api/mdns.h"
10 #include "chrome/common/extensions/api/mdns/mdns_manifest_handler.h"
11 #include "extensions/common/manifest_constants.h"
12
13 namespace extensions {
14
15 namespace mdns = api::mdns;
16
17 namespace {
18
19 static base::LazyInstance<std::vector<std::string> > g_empty_service_types =
20 LAZY_INSTANCE_INITIALIZER;
21
22 // Retrieves the list of service types that are declared in the extension's
23 // manifest file.
24 const std::vector<std::string>& GetServiceTypesForExtension(
25 Profile* profile, const std::string& extension_id) {
26 const extensions::Extension* extension = ExtensionSystem::Get(profile)->
27 extension_service()->GetExtensionById(extension_id, false);
28 if (!extension) {
29 NOTREACHED();
30 LOG(ERROR) << "Failed to find extension " << extension_id << ".";
31 return g_empty_service_types.Get();
32 }
33 return MDnsInfo::GetMDnsInfo(extension).service_types;
34 }
35
36 } // namespace
37
38 MDnsAPI::MDnsAPI(Profile* profile) : profile_(profile) {
39 DCHECK(profile_);
40 ExtensionSystem::Get(profile)->event_router()->RegisterObserver(
41 this, mdns::OnServiceList::kEventName);
42 }
43
44 MDnsAPI::~MDnsAPI() {
45 if (nsd_registry_.get()) {
46 nsd_registry_->RemoveObserver(this);
47 }
48 }
49
50 // static
51 MDnsAPI* MDnsAPI::Get(Profile* profile) {
52 return ProfileKeyedAPIFactory<MDnsAPI>::GetForProfile(profile);
53 }
54
55 static base::LazyInstance<ProfileKeyedAPIFactory<MDnsAPI> >
56 g_factory = LAZY_INSTANCE_INITIALIZER;
57
58 // static
59 ProfileKeyedAPIFactory<MDnsAPI>* MDnsAPI::GetFactoryInstance() {
60 return &g_factory.Get();
61 }
62
63 void MDnsAPI::SetNsdRegistryForTesting(scoped_ptr<NsdRegistry> nsd_registry) {
64 nsd_registry_ = nsd_registry.Pass();
65 }
66
67 NsdRegistry* MDnsAPI::nsd_registry() {
68 DCHECK(thread_checker_.CalledOnValidThread());
69 if (!nsd_registry_.get()) {
70 nsd_registry_.reset(new extensions::NsdRegistry());
71 nsd_registry_->AddObserver(this);
72 }
73 return nsd_registry_.get();
74 }
75
76 void MDnsAPI::OnListenerAdded(const EventListenerInfo& details) {
77 DCHECK(thread_checker_.CalledOnValidThread());
78 LOG(ERROR) << "LISTENER ADDED";
Matt Perry 2013/09/03 22:24:04 fyi
justinlin 2013/09/05 05:14:46 Done...
79 const std::string& extension_id = details.extension_id;
80 const std::vector<std::string>& service_types =
81 GetServiceTypesForExtension(profile_, extension_id);
82 for (std::vector<std::string>::const_iterator it = service_types.begin();
83 it != service_types.end(); ++it) {
84 // Start watchers for all the service types this extension has declared.
85 const std::string& service_type = *it;
86 DVLOG(1) << "Registered watcher for service type \"" << service_type
87 << "\" for extension \"" << extension_id << "\"";
88 if (!service_type_to_extensions_map_[service_type].insert(
89 extension_id).second) {
90 NOTREACHED();
91 }
92 nsd_registry()->RegisterMDnsListener(service_type);
93 }
94 }
95
96 void MDnsAPI::OnListenerRemoved(const EventListenerInfo& details) {
97 DCHECK(thread_checker_.CalledOnValidThread());
98 const std::string& extension_id = details.extension_id;
99 for (ServiceTypeToExtensionsMap::iterator it =
100 service_type_to_extensions_map_.begin();
101 it != service_type_to_extensions_map_.end();) {
102 const std::string& service_type = it->first;
103 std::set<std::string>& extensions = it->second;
104 if (extensions.erase(extension_id)) {
105 if (extensions.empty()) {
106 DVLOG(1) << "Removed all watchers for service type \"" << service_type
107 << "\"";
108 nsd_registry()->UnregisterMDnsListener(service_type);
109 service_type_to_extensions_map_.erase(it++);
110 continue;
111 }
112 }
113 it++;
114 }
115 }
116
117 void MDnsAPI::OnMDnsEvent(const std::string& service_type,
118 const NsdRegistry::NsdServiceList& services) {
119 DCHECK(thread_checker_.CalledOnValidThread());
120 if (services.size() > 0 &&
121 service_type_to_extensions_map_.find(service_type) !=
122 service_type_to_extensions_map_.end()) {
123 const std::set<std::string>& extensions =
124 service_type_to_extensions_map_[service_type];
125 for (std::set<std::string>::iterator it = extensions.begin();
126 it != extensions.end(); ++it) {
127 std::vector<linked_ptr<mdns::MDnsService> > args;
128 for (NsdRegistry::NsdServiceList::const_iterator it = services.begin();
129 it != services.end(); ++it) {
130 linked_ptr<mdns::MDnsService> mdns_service =
131 make_linked_ptr(new mdns::MDnsService);
132 // TODO(justinlin, mfoltz): Correctly fill in service info.
133 mdns_service->service_name = service_type;
134 args.push_back(mdns_service);
135 }
136 scoped_ptr<base::ListValue> results = mdns::OnServiceList::Create(args);
137 scoped_ptr<Event> event(
138 new Event(mdns::OnServiceList::kEventName, results.Pass()));
139 extensions::ExtensionSystem::Get(profile_)->event_router()->
140 DispatchEventToExtension(*it, event.Pass());
141 }
142 }
143 }
144
145 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/mdns/mdns_api.h ('k') | chrome/browser/extensions/api/mdns/mdns_apitest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698