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

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

Issue 22870011: chrome.mdns API (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: add tests 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 | Annotate | Revision Log
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/api/mdns/mdns_api_factory.h"
8 #include "chrome/browser/extensions/extension_service.h"
9 #include "chrome/browser/extensions/extension_system.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/common/extensions/api/mdns.h"
12 #include "chrome/common/extensions/api/mdns/mdns_manifest_handler.h"
13 #include "extensions/common/manifest_constants.h"
14
15 namespace extensions {
16
17 namespace mdns = api::mdns;
18
19 namespace {
20
21 // Retrieves the list of service types that are declared in the extension's
22 // manifest file.
23 const std::vector<std::string> GetServiceTypesForExtension(
24 Profile* profile, std::string extension_id) {
25 const extensions::Extension* extension = ExtensionSystem::Get(profile)->
26 extension_service()->GetExtensionById(extension_id, false);
27 if (!extension) {
28 NOTREACHED();
29 return std::vector<std::string>();
30 }
31 const MDnsInfo& info = MDnsInfo::GetMDnsInfo(extension);
32 return info.service_types;
33 }
34
35 } // namespace
36
37 MDnsAPI::MDnsAPI(Profile* profile) : profile_(profile) {
38 DCHECK(profile_);
39 ExtensionSystem::Get(profile)->event_router()->RegisterObserver(
40 this, mdns::OnServiceList::kEventName);
41 }
42
43 MDnsAPI::~MDnsAPI() {
44 if (nsd_registry_.get()) {
45 nsd_registry_->RemoveObserver(this);
46 }
47 }
48
49 void MDnsAPI::SetNsdRegistryForTesting(NsdRegistry* nsd_registry) {
50 nsd_registry_.reset(nsd_registry);
51 }
52
53 NsdRegistry* MDnsAPI::nsd_registry() {
54 DCHECK(thread_checker_.CalledOnValidThread());
55 if (!nsd_registry_.get()) {
56 nsd_registry_.reset(new extensions::NsdRegistry());
57 nsd_registry_->AddObserver(this);
58 }
59 return nsd_registry_.get();
60 }
61
62 void MDnsAPI::OnListenerAdded(const EventListenerInfo& details) {
63 DCHECK(thread_checker_.CalledOnValidThread());
64 const std::string& extension_id = details.extension_id;
65 const std::vector<std::string>& service_types =
66 GetServiceTypesForExtension(profile_, extension_id);
67 for (std::vector<std::string>::const_iterator it = service_types.begin();
68 it != service_types.end(); ++it) {
69 // Start watchers for all the service types this extension has declared.
70 const std::string& service_type = *it;
71 DVLOG(1) << "Registered watcher for service type \"" << service_type
72 << "\" for extension \"" << extension_id << "\"";
73 service_type_to_extensions_map_[service_type].insert(extension_id);
74 nsd_registry()->RegisterMDnsListener(service_type);
75 }
76 }
77
78 void MDnsAPI::OnListenerRemoved(const EventListenerInfo& details) {
79 DCHECK(thread_checker_.CalledOnValidThread());
80 const std::string& extension_id = details.extension_id;
81 for (ServiceTypeToExtensionsMap::iterator it =
82 service_type_to_extensions_map_.begin();
83 it != service_type_to_extensions_map_.end(); it++) {
84 const std::string& service_type = it->first;
85 std::set<std::string>& extensions = it->second;
86 if (extensions.erase(extension_id)) {
87 if (!extensions.size()) {
88 DVLOG(1) << "Removed all watchers for service type \"" << service_type
89 << "\"";
90 nsd_registry()->UnregisterMDnsListener(service_type);
91 service_type_to_extensions_map_.erase(service_type);
92 }
93 }
94 }
95 }
96
97 void MDnsAPI::OnMDnsEvent(const std::string& service_type,
98 const NsdRegistry::NsdServiceList& services) {
99 DCHECK(thread_checker_.CalledOnValidThread());
100 if (services.size() > 0 &&
101 service_type_to_extensions_map_.find(service_type) !=
102 service_type_to_extensions_map_.end()) {
103 const std::set<std::string>& extensions =
104 service_type_to_extensions_map_[service_type];
105 for (std::set<std::string>::iterator it = extensions.begin();
106 it != extensions.end(); ++it) {
107 std::vector<linked_ptr<mdns::MDnsService> > args;
108 for (NsdRegistry::NsdServiceList::const_iterator it = services.begin();
109 it != services.end(); ++it) {
110 linked_ptr<mdns::MDnsService> mdns_service =
111 make_linked_ptr(new mdns::MDnsService);
112 // TODO(justinlin, mfoltz): Correctly fill in service info.
113 mdns_service->service_name = service_type;
114 args.push_back(mdns_service);
115 }
116 scoped_ptr<base::ListValue> results = mdns::OnServiceList::Create(args);
117 scoped_ptr<Event> event(
118 new Event(mdns::OnServiceList::kEventName, results.Pass()));
119 extensions::ExtensionSystem::Get(profile_)->event_router()->
120 DispatchEventToExtension(*it, event.Pass());
121 }
122 }
123 }
124
125 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698