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

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

Issue 2874243003: [mDns] Make DnsSdRegistry a leaky singleton (Closed)
Patch Set: resolve code review comments from Mark Created 3 years, 7 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
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/extensions/api/mdns/mdns_api.h" 5 #include "chrome/browser/extensions/api/mdns/mdns_api.h"
6 6
7 #include <utility> 7 #include <utility>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/lazy_instance.h" 10 #include "base/lazy_instance.h"
(...skipping 20 matching lines...) Expand all
31 bool IsServiceTypeWhitelisted(const std::string& service_type) { 31 bool IsServiceTypeWhitelisted(const std::string& service_type) {
32 return service_type == kCastServiceType || 32 return service_type == kCastServiceType ||
33 service_type == kPrivetServiceType || 33 service_type == kPrivetServiceType ||
34 service_type == kTestServiceType; 34 service_type == kTestServiceType;
35 } 35 }
36 36
37 } // namespace 37 } // namespace
38 38
39 using DnsSdRegistry = media_router::DnsSdRegistry; 39 using DnsSdRegistry = media_router::DnsSdRegistry;
40 40
41 MDnsAPI::MDnsAPI(content::BrowserContext* context) : browser_context_(context) { 41 MDnsAPI::MDnsAPI(content::BrowserContext* context)
42 : browser_context_(context), dns_sd_registry_(nullptr) {
42 DCHECK(browser_context_); 43 DCHECK(browser_context_);
43 extensions::EventRouter* event_router = EventRouter::Get(context); 44 extensions::EventRouter* event_router = EventRouter::Get(context);
44 DCHECK(event_router); 45 DCHECK(event_router);
45 event_router->RegisterObserver(this, mdns::OnServiceList::kEventName); 46 event_router->RegisterObserver(this, mdns::OnServiceList::kEventName);
46 } 47 }
47 48
48 MDnsAPI::~MDnsAPI() { 49 MDnsAPI::~MDnsAPI() {
49 if (dns_sd_registry_.get()) { 50 if (dns_sd_registry_) {
50 dns_sd_registry_->RemoveObserver(this); 51 dns_sd_registry_->RemoveObserver(this);
51 } 52 }
52 } 53 }
53 54
54 // static 55 // static
55 MDnsAPI* MDnsAPI::Get(content::BrowserContext* context) { 56 MDnsAPI* MDnsAPI::Get(content::BrowserContext* context) {
56 return BrowserContextKeyedAPIFactory<MDnsAPI>::Get(context); 57 return BrowserContextKeyedAPIFactory<MDnsAPI>::Get(context);
57 } 58 }
58 59
59 static base::LazyInstance< 60 static base::LazyInstance<
60 BrowserContextKeyedAPIFactory<MDnsAPI>>::DestructorAtExit g_factory = 61 BrowserContextKeyedAPIFactory<MDnsAPI>>::DestructorAtExit g_factory =
61 LAZY_INSTANCE_INITIALIZER; 62 LAZY_INSTANCE_INITIALIZER;
62 63
63 // static 64 // static
64 BrowserContextKeyedAPIFactory<MDnsAPI>* MDnsAPI::GetFactoryInstance() { 65 BrowserContextKeyedAPIFactory<MDnsAPI>* MDnsAPI::GetFactoryInstance() {
65 return g_factory.Pointer(); 66 return g_factory.Pointer();
66 } 67 }
67 68
68 void MDnsAPI::SetDnsSdRegistryForTesting( 69 void MDnsAPI::SetDnsSdRegistryForTesting(DnsSdRegistry* dns_sd_registry) {
69 std::unique_ptr<DnsSdRegistry> dns_sd_registry) { 70 dns_sd_registry_ = dns_sd_registry;
70 dns_sd_registry_ = std::move(dns_sd_registry); 71 if (dns_sd_registry_)
71 if (dns_sd_registry_.get())
72 dns_sd_registry_->AddObserver(this); 72 dns_sd_registry_->AddObserver(this);
73 } 73 }
74 74
75 void MDnsAPI::ForceDiscovery() { 75 void MDnsAPI::ForceDiscovery() {
76 DCHECK(thread_checker_.CalledOnValidThread()); 76 DCHECK(thread_checker_.CalledOnValidThread());
77 DnsSdRegistry* registry = dns_sd_registry(); 77 DnsSdRegistry* registry = dns_sd_registry();
78 return registry->ForceDiscovery(); 78 return registry->ForceDiscovery();
79 } 79 }
80 80
81 DnsSdRegistry* MDnsAPI::dns_sd_registry() { 81 DnsSdRegistry* MDnsAPI::dns_sd_registry() {
82 DCHECK(thread_checker_.CalledOnValidThread()); 82 DCHECK(thread_checker_.CalledOnValidThread());
83 if (!dns_sd_registry_.get()) { 83 if (!dns_sd_registry_) {
84 dns_sd_registry_.reset(new media_router::DnsSdRegistry()); 84 dns_sd_registry_ = media_router::DnsSdRegistry::GetInstance();
85 dns_sd_registry_->AddObserver(this); 85 dns_sd_registry_->AddObserver(this);
86 } 86 }
87 return dns_sd_registry_.get(); 87 return dns_sd_registry_;
88 } 88 }
89 89
90 void MDnsAPI::OnListenerAdded(const EventListenerInfo& details) { 90 void MDnsAPI::OnListenerAdded(const EventListenerInfo& details) {
91 DCHECK(thread_checker_.CalledOnValidThread()); 91 DCHECK(thread_checker_.CalledOnValidThread());
92 UpdateMDnsListeners(); 92 UpdateMDnsListeners();
93 } 93 }
94 94
95 void MDnsAPI::OnListenerRemoved(const EventListenerInfo& details) { 95 void MDnsAPI::OnListenerRemoved(const EventListenerInfo& details) {
96 DCHECK(thread_checker_.CalledOnValidThread()); 96 DCHECK(thread_checker_.CalledOnValidThread());
97 UpdateMDnsListeners(); 97 UpdateMDnsListeners();
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
269 AsyncApiFunction::ResponseAction MdnsForceDiscoveryFunction::Run() { 269 AsyncApiFunction::ResponseAction MdnsForceDiscoveryFunction::Run() {
270 MDnsAPI* api = MDnsAPI::Get(browser_context()); 270 MDnsAPI* api = MDnsAPI::Get(browser_context());
271 if (!api) { 271 if (!api) {
272 return RespondNow(Error("Unknown error.")); 272 return RespondNow(Error("Unknown error."));
273 } 273 }
274 api->ForceDiscovery(); 274 api->ForceDiscovery();
275 return RespondNow(NoArguments()); 275 return RespondNow(NoArguments());
276 } 276 }
277 277
278 } // namespace extensions 278 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/mdns/mdns_api.h ('k') | chrome/browser/extensions/api/mdns/mdns_api_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698