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 "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( | |
|
Matt Perry
2013/08/30 00:43:29
you can return a reference here
justinlin
2013/09/03 21:21:56
Done. Have to use a lazy instance to return if ext
| |
| 24 Profile* profile, std::string extension_id) { | |
|
Matt Perry
2013/08/30 00:43:29
pass strings by const ref
justinlin
2013/09/03 21:21:56
Done.
| |
| 25 const extensions::Extension* extension = ExtensionSystem::Get(profile)-> | |
| 26 extension_service()->GetExtensionById(extension_id, false); | |
| 27 if (!extension) { | |
| 28 NOTREACHED(); | |
|
mark a. foltz
2013/08/30 01:35:52
Include a message
justinlin
2013/09/03 21:21:56
Done.
| |
| 29 return std::vector<std::string>(); | |
| 30 } | |
| 31 const MDnsInfo& info = MDnsInfo::GetMDnsInfo(extension); | |
|
mark a. foltz
2013/08/30 01:35:52
Combine this with the return statement?
justinlin
2013/09/03 21:21:56
Done.
| |
| 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) { | |
|
mark a. foltz
2013/08/30 01:35:52
Make clear (in a comment or the .h) that the MDnsA
justinlin
2013/09/03 21:21:56
Done.
| |
| 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); | |
|
mark a. foltz
2013/08/30 01:35:52
Check return value?
I guess we don't care about du
justinlin
2013/09/03 21:21:56
Added DCHECK, we should never get duplicate insert
| |
| 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()) { | |
|
Matt Perry
2013/08/30 00:43:29
nit: size is not a boolean. use either size() == 0
justinlin
2013/09/03 21:21:56
Done.
| |
| 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); | |
|
Matt Perry
2013/08/30 00:43:29
This will invalidate your iterator. You should do
justinlin
2013/09/03 21:21:56
Done.
| |
| 92 } | |
| 93 } | |
| 94 } | |
| 95 } | |
| 96 | |
| 97 void MDnsAPI::OnMDnsEvent(const std::string& service_type, | |
|
Matt Perry
2013/08/30 00:43:29
This is where filtered events would come in really
justinlin
2013/09/03 21:21:56
Looking into using this. Does seem promising, but
Matt Perry
2013/09/03 22:24:04
Oh you're right, we don't have support for filters
| |
| 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. | |
|
mark a. foltz
2013/08/30 01:35:52
The registry should already have correctly filled
justinlin
2013/09/03 21:21:56
The TODO is to copy the information from a NsdServ
| |
| 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 | |
| OLD | NEW |