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

Side by Side Diff: chrome/browser/media/router/discovery/dial/dial_registry.h

Issue 2754703005: [Device Discovery] Make DialRegistry a Singleton (Closed)
Patch Set: Created 3 years, 9 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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_MEDIA_ROUTER_DISCOVERY_DIAL_DIAL_REGISTRY_H_ 5 #ifndef CHROME_BROWSER_MEDIA_ROUTER_DISCOVERY_DIAL_DIAL_REGISTRY_H_
6 #define CHROME_BROWSER_MEDIA_ROUTER_DISCOVERY_DIAL_DIAL_REGISTRY_H_ 6 #define CHROME_BROWSER_MEDIA_ROUTER_DISCOVERY_DIAL_DIAL_REGISTRY_H_
7 7
8 #include <stddef.h> 8 #include <stddef.h>
9 9
10 #include <map> 10 #include <map>
11 #include <memory> 11 #include <memory>
12 #include <string> 12 #include <string>
13 #include <vector> 13 #include <vector>
14 14
15 #include "base/containers/hash_tables.h" 15 #include "base/containers/hash_tables.h"
16 #include "base/gtest_prod_util.h" 16 #include "base/gtest_prod_util.h"
17 #include "base/macros.h" 17 #include "base/macros.h"
18 #include "base/memory/ref_counted.h" 18 #include "base/memory/ref_counted.h"
19 #include "base/observer_list.h" 19 #include "base/observer_list.h"
20 #include "base/time/time.h" 20 #include "base/time/time.h"
21 #include "base/timer/timer.h" 21 #include "base/timer/timer.h"
22 #include "chrome/browser/media/router/discovery/dial/dial_service.h" 22 #include "chrome/browser/media/router/discovery/dial/dial_service.h"
23 #include "chrome/browser/profiles/profile.h" 23 #include "chrome/browser/profiles/profile.h"
24 #include "components/keyed_service/core/refcounted_keyed_service.h"
24 #include "net/base/network_change_notifier.h" 25 #include "net/base/network_change_notifier.h"
25 26
26 namespace media_router { 27 namespace media_router {
27 28
28 // Keeps track of devices that have responded to discovery requests and notifies 29 // Keeps track of devices that have responded to discovery requests and notifies
29 // the observer with an updated, complete set of active devices. The registry's 30 // the observer with an updated, complete set of active devices. The registry's
30 // observer (i.e., the Dial API) owns the registry instance. 31 // observer (i.e., the Dial API) owns the registry instance.
31 // DialRegistry lives on the IO thread. 32 // DialRegistry lives on the IO thread.
32 class DialRegistry : public DialService::Observer, 33 class DialRegistry : public RefcountedKeyedService,
mark a. foltz 2017/03/18 18:04:58 I don't think the DialRegistry needs to be a keyed
zhaobin 2017/03/22 02:48:45 Code removed.
34 public DialService::Observer,
33 public net::NetworkChangeNotifier::NetworkChangeObserver { 35 public net::NetworkChangeNotifier::NetworkChangeObserver {
34 public: 36 public:
35 using DeviceList = std::vector<DialDeviceData>; 37 using DeviceList = std::vector<DialDeviceData>;
36 38
37 enum DialErrorCode { 39 enum DialErrorCode {
38 DIAL_NO_LISTENERS = 0, 40 DIAL_NO_LISTENERS = 0,
39 DIAL_NO_INTERFACES, 41 DIAL_NO_INTERFACES,
40 DIAL_NETWORK_DISCONNECTED, 42 DIAL_NETWORK_DISCONNECTED,
41 DIAL_CELLULAR_NETWORK, 43 DIAL_CELLULAR_NETWORK,
42 DIAL_SOCKET_ERROR, 44 DIAL_SOCKET_ERROR,
(...skipping 10 matching lines...) Expand all
53 protected: 55 protected:
54 virtual ~Observer() {} 56 virtual ~Observer() {}
55 }; 57 };
56 58
57 // Create the DIAL registry and pass a reference to allow it to notify on 59 // Create the DIAL registry and pass a reference to allow it to notify on
58 // DIAL device events. 60 // DIAL device events.
59 DialRegistry(base::TimeDelta refresh_interval, 61 DialRegistry(base::TimeDelta refresh_interval,
60 base::TimeDelta expiration, 62 base::TimeDelta expiration,
61 const size_t max_devices); 63 const size_t max_devices);
62 64
63 ~DialRegistry() override;
64
65 // Called by the DIAL API when event listeners are added or removed. The dial 65 // Called by the DIAL API when event listeners are added or removed. The dial
66 // service is started after the first listener is added and stopped after the 66 // service is started after the first listener is added and stopped after the
67 // last listener is removed. 67 // last listener is removed.
68 void OnListenerAdded(); 68 void OnListenerAdded();
69 void OnListenerRemoved(); 69 void OnListenerRemoved();
70 70
71 // This class does not take ownership of observer. 71 // This class does not take ownership of observer.
72 void RegisterObserver(Observer* observer); 72 void RegisterObserver(Observer* observer);
73 void UnregisterObserver(Observer* observer); 73 void UnregisterObserver(Observer* observer);
74 74
75 // Called by the DIAL API to try to kickoff a discovery if there is not one 75 // Called by the DIAL API to try to kickoff a discovery if there is not one
76 // already active. 76 // already active.
77 bool DiscoverNow(); 77 bool DiscoverNow();
78 78
79 // Returns the URL of the device description for the device identified by 79 // Returns the URL of the device description for the device identified by
80 // |label|, or an empty GURL if no such device exists. 80 // |label|, or an empty GURL if no such device exists.
81 GURL GetDeviceDescriptionURL(const std::string& label) const; 81 GURL GetDeviceDescriptionURL(const std::string& label) const;
82 82
83 // Adds a device directly to the registry as if it was discovered. For tests 83 // Adds a device directly to the registry as if it was discovered. For tests
84 // only. Note that if discovery is actually started, this device will be 84 // only. Note that if discovery is actually started, this device will be
85 // removed by PruneExpiredDevices(). 85 // removed by PruneExpiredDevices().
86 void AddDeviceForTest(const DialDeviceData& device_data); 86 void AddDeviceForTest(const DialDeviceData& device_data);
87 87
88 protected: 88 protected:
89 ~DialRegistry() override;
90
89 // Returns a new instance of the DIAL service. Overridden by tests. 91 // Returns a new instance of the DIAL service. Overridden by tests.
90 virtual std::unique_ptr<DialService> CreateDialService(); 92 virtual std::unique_ptr<DialService> CreateDialService();
91 virtual void ClearDialService(); 93 virtual void ClearDialService();
92 94
93 // Returns the current time. Overridden by tests. 95 // Returns the current time. Overridden by tests.
94 virtual base::Time Now() const; 96 virtual base::Time Now() const;
95 97
96 // The DIAL service. Periodic discovery is active when this is not NULL. 98 // The DIAL service. Periodic discovery is active when this is not NULL.
97 std::unique_ptr<DialService> dial_; 99 std::unique_ptr<DialService> dial_;
98 100
99 private: 101 private:
100 using DeviceByIdMap = 102 using DeviceByIdMap =
101 base::hash_map<std::string, std::unique_ptr<DialDeviceData>>; 103 base::hash_map<std::string, std::unique_ptr<DialDeviceData>>;
102 using DeviceByLabelMap = std::map<std::string, DialDeviceData*>; 104 using DeviceByLabelMap = std::map<std::string, DialDeviceData*>;
103 105
106 // RefcountedKeyedService:
107 void ShutdownOnUIThread() override;
108
104 // DialService::Observer: 109 // DialService::Observer:
105 void OnDiscoveryRequest(DialService* service) override; 110 void OnDiscoveryRequest(DialService* service) override;
106 void OnDeviceDiscovered(DialService* service, 111 void OnDeviceDiscovered(DialService* service,
107 const DialDeviceData& device) override; 112 const DialDeviceData& device) override;
108 void OnDiscoveryFinished(DialService* service) override; 113 void OnDiscoveryFinished(DialService* service) override;
109 void OnError(DialService* service, 114 void OnError(DialService* service,
110 const DialService::DialServiceErrorCode& code) override; 115 const DialService::DialServiceErrorCode& code) override;
111 116
112 // net::NetworkChangeObserver: 117 // net::NetworkChangeObserver:
113 void OnNetworkChanged( 118 void OnNetworkChanged(
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
197 TestRemovingListenerDoesNotClearList); 202 TestRemovingListenerDoesNotClearList);
198 FRIEND_TEST_ALL_PREFIXES(DialRegistryTest, TestNetworkEventConnectionLost); 203 FRIEND_TEST_ALL_PREFIXES(DialRegistryTest, TestNetworkEventConnectionLost);
199 FRIEND_TEST_ALL_PREFIXES(DialRegistryTest, 204 FRIEND_TEST_ALL_PREFIXES(DialRegistryTest,
200 TestNetworkEventConnectionRestored); 205 TestNetworkEventConnectionRestored);
201 DISALLOW_COPY_AND_ASSIGN(DialRegistry); 206 DISALLOW_COPY_AND_ASSIGN(DialRegistry);
202 }; 207 };
203 208
204 } // namespace media_router 209 } // namespace media_router
205 210
206 #endif // CHROME_BROWSER_MEDIA_ROUTER_DISCOVERY_DIAL_DIAL_REGISTRY_H_ 211 #endif // CHROME_BROWSER_MEDIA_ROUTER_DISCOVERY_DIAL_DIAL_REGISTRY_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698