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

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

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
« no previous file with comments | « no previous file | chrome/browser/extensions/api/mdns/mdns_api.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #ifndef CHROME_BROWSER_EXTENSIONS_API_MDNS_MDNS_API_H_ 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_MDNS_MDNS_API_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_MDNS_MDNS_API_H_ 6 #define CHROME_BROWSER_EXTENSIONS_API_MDNS_MDNS_API_H_
7 7
8 #include <map> 8 #include <map>
9 #include <memory> 9 #include <memory>
10 #include <set> 10 #include <set>
11 #include <string> 11 #include <string>
12 12
13 #include "base/gtest_prod_util.h" 13 #include "base/gtest_prod_util.h"
14 #include "base/macros.h" 14 #include "base/macros.h"
15 #include "base/threading/thread_checker.h" 15 #include "base/threading/thread_checker.h"
16 #include "chrome/browser/media/router/discovery/mdns/dns_sd_registry.h" 16 #include "chrome/browser/media/router/discovery/mdns/dns_sd_registry.h"
17 #include "chrome/common/extensions/api/mdns.h" 17 #include "chrome/common/extensions/api/mdns.h"
18 #include "extensions/browser/api/async_api_function.h" 18 #include "extensions/browser/api/async_api_function.h"
19 #include "extensions/browser/browser_context_keyed_api_factory.h" 19 #include "extensions/browser/browser_context_keyed_api_factory.h"
20 #include "extensions/browser/event_router.h" 20 #include "extensions/browser/event_router.h"
21 #include "extensions/browser/extension_function.h" 21 #include "extensions/browser/extension_function.h"
22 22
23 namespace content { 23 namespace content {
24 class BrowserContext; 24 class BrowserContext;
25 } 25 }
26 26
27 namespace media_router {
28 class DnsSdRegistry;
29 }
30
31 namespace extensions { 27 namespace extensions {
32 // MDnsAPI is instantiated with the profile and will listen for extensions that 28 // MDnsAPI is instantiated with the profile and will listen for extensions that
33 // register listeners for the chrome.mdns extension API. It will use a registry 29 // register listeners for the chrome.mdns extension API. It will use a registry
34 // class to start the mDNS listener process (if necessary) and observe new 30 // class to start the mDNS listener process (if necessary) and observe new
35 // service events to dispatch them to registered extensions. 31 // service events to dispatch them to registered extensions.
36 class MDnsAPI : public BrowserContextKeyedAPI, 32 class MDnsAPI : public BrowserContextKeyedAPI,
37 public EventRouter::Observer, 33 public EventRouter::Observer,
38 public media_router::DnsSdRegistry::DnsSdObserver { 34 public media_router::DnsSdRegistry::DnsSdObserver {
39 public: 35 public:
40 explicit MDnsAPI(content::BrowserContext* context); 36 explicit MDnsAPI(content::BrowserContext* context);
41 ~MDnsAPI() override; 37 ~MDnsAPI() override;
42 38
43 static MDnsAPI* Get(content::BrowserContext* context); 39 static MDnsAPI* Get(content::BrowserContext* context);
44 40
45 // BrowserContextKeyedAPI implementation. 41 // BrowserContextKeyedAPI implementation.
46 static BrowserContextKeyedAPIFactory<MDnsAPI>* GetFactoryInstance(); 42 static BrowserContextKeyedAPIFactory<MDnsAPI>* GetFactoryInstance();
47 43
48 // Used to mock out the DnsSdRegistry for testing. 44 // Used to mock out the DnsSdRegistry for testing. Does not take ownership of
49 void SetDnsSdRegistryForTesting( 45 // |registry|.
50 std::unique_ptr<media_router::DnsSdRegistry> registry); 46 void SetDnsSdRegistryForTesting(media_router::DnsSdRegistry* registry);
51 47
52 // Immediately issues a multicast DNS query for all service types. 48 // Immediately issues a multicast DNS query for all service types.
53 // NOTE: Discovery queries are sent to all event handlers associated with 49 // NOTE: Discovery queries are sent to all event handlers associated with
54 // |this| service's BrowserContext. 50 // |this| service's BrowserContext.
55 void ForceDiscovery(); 51 void ForceDiscovery();
56 52
57 protected: 53 protected:
58 // Retrieve an instance of the registry. Lazily created when needed. 54 // Retrieve an instance of the registry. Lazily created when needed.
59 virtual media_router::DnsSdRegistry* dns_sd_registry(); 55 virtual media_router::DnsSdRegistry* dns_sd_registry();
60 56
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 // if non-null. 101 // if non-null.
106 // The counts for each service type are output to |service_type_counts|, if 102 // The counts for each service type are output to |service_type_counts|, if
107 // non-null. 103 // non-null.
108 void GetValidOnServiceListListeners(const std::string& service_type_filter, 104 void GetValidOnServiceListListeners(const std::string& service_type_filter,
109 std::set<std::string>* extension_ids, 105 std::set<std::string>* extension_ids,
110 ServiceTypeCounts* service_type_counts); 106 ServiceTypeCounts* service_type_counts);
111 107
112 // Ensure methods are only called on UI thread. 108 // Ensure methods are only called on UI thread.
113 base::ThreadChecker thread_checker_; 109 base::ThreadChecker thread_checker_;
114 content::BrowserContext* const browser_context_; 110 content::BrowserContext* const browser_context_;
115 // Lazily created on first access and destroyed with this API class. 111 // Raw pointer to a leaky singleton. Lazily created on first access. Must
116 std::unique_ptr<media_router::DnsSdRegistry> dns_sd_registry_; 112 // outlive this object.
113 media_router::DnsSdRegistry* dns_sd_registry_;
117 // Count of active listeners per service type, saved from the previous 114 // Count of active listeners per service type, saved from the previous
118 // invocation of UpdateMDnsListeners(). 115 // invocation of UpdateMDnsListeners().
119 ServiceTypeCounts prev_service_counts_; 116 ServiceTypeCounts prev_service_counts_;
120 117
121 DISALLOW_COPY_AND_ASSIGN(MDnsAPI); 118 DISALLOW_COPY_AND_ASSIGN(MDnsAPI);
122 }; 119 };
123 120
124 class MdnsForceDiscoveryFunction : public UIThreadExtensionFunction { 121 class MdnsForceDiscoveryFunction : public UIThreadExtensionFunction {
125 public: 122 public:
126 MdnsForceDiscoveryFunction(); 123 MdnsForceDiscoveryFunction();
127 124
128 protected: 125 protected:
129 ~MdnsForceDiscoveryFunction() override; 126 ~MdnsForceDiscoveryFunction() override;
130 127
131 private: 128 private:
132 // UIThreadExtensionFunction override. 129 // UIThreadExtensionFunction override.
133 ResponseAction Run() override; 130 ResponseAction Run() override;
134 131
135 DECLARE_EXTENSION_FUNCTION("mdns.forceDiscovery", MDNS_FORCEDISCOVERY); 132 DECLARE_EXTENSION_FUNCTION("mdns.forceDiscovery", MDNS_FORCEDISCOVERY);
136 DISALLOW_COPY_AND_ASSIGN(MdnsForceDiscoveryFunction); 133 DISALLOW_COPY_AND_ASSIGN(MdnsForceDiscoveryFunction);
137 }; 134 };
138 135
139 } // namespace extensions 136 } // namespace extensions
140 137
141 #endif // CHROME_BROWSER_EXTENSIONS_API_MDNS_MDNS_API_H_ 138 #endif // CHROME_BROWSER_EXTENSIONS_API_MDNS_MDNS_API_H_
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/extensions/api/mdns/mdns_api.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698