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

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

Issue 2754703005: [Device Discovery] Make DialRegistry a Singleton (Closed)
Patch Set: resolve code review comments from Mark 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"
19 #include "base/observer_list.h" 18 #include "base/observer_list.h"
20 #include "base/time/time.h" 19 #include "base/time/time.h"
21 #include "base/timer/timer.h" 20 #include "base/timer/timer.h"
22 #include "chrome/browser/media/router/discovery/dial/dial_service.h" 21 #include "chrome/browser/media/router/discovery/dial/dial_service.h"
23 #include "chrome/browser/profiles/profile.h" 22 #include "chrome/browser/profiles/profile.h"
24 #include "net/base/network_change_notifier.h" 23 #include "net/base/network_change_notifier.h"
25 24
26 namespace media_router { 25 namespace media_router {
27 26
28 // Keeps track of devices that have responded to discovery requests and notifies 27 // Keeps track of devices that have responded to discovery requests and notifies
(...skipping 18 matching lines...) Expand all
47 public: 46 public:
48 // Methods invoked on the IO thread when a new device is discovered, an 47 // Methods invoked on the IO thread when a new device is discovered, an
49 // update is triggered by dial.discoverNow or an error occured. 48 // update is triggered by dial.discoverNow or an error occured.
50 virtual void OnDialDeviceEvent(const DeviceList& devices) = 0; 49 virtual void OnDialDeviceEvent(const DeviceList& devices) = 0;
51 virtual void OnDialError(DialErrorCode type) = 0; 50 virtual void OnDialError(DialErrorCode type) = 0;
52 51
53 protected: 52 protected:
54 virtual ~Observer() {} 53 virtual ~Observer() {}
55 }; 54 };
56 55
57 // Create the DIAL registry and pass a reference to allow it to notify on 56 static DialRegistry* GetInstance();
58 // DIAL device events.
59 DialRegistry(base::TimeDelta refresh_interval,
60 base::TimeDelta expiration,
61 const size_t max_devices);
62
63 ~DialRegistry() override;
64 57
65 // Called by the DIAL API when event listeners are added or removed. The dial 58 // 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 59 // service is started after the first listener is added and stopped after the
67 // last listener is removed. 60 // last listener is removed.
68 void OnListenerAdded(); 61 void OnListenerAdded();
69 void OnListenerRemoved(); 62 void OnListenerRemoved();
70 63
71 // This class does not take ownership of observer. 64 // This class does not take ownership of observer.
72 void RegisterObserver(Observer* observer); 65 void RegisterObserver(Observer* observer);
73 void UnregisterObserver(Observer* observer); 66 void UnregisterObserver(Observer* observer);
74 67
75 // Called by the DIAL API to try to kickoff a discovery if there is not one 68 // Called by the DIAL API to try to kickoff a discovery if there is not one
76 // already active. 69 // already active.
77 bool DiscoverNow(); 70 bool DiscoverNow();
78 71
72 // Starts and stops periodic discovery. Periodic discovery is done when there
73 // are registered event listeners.
74 void StartPeriodicDiscovery();
75 void StopPeriodicDiscovery();
76
79 // Returns the URL of the device description for the device identified by 77 // Returns the URL of the device description for the device identified by
80 // |label|, or an empty GURL if no such device exists. 78 // |label|, or an empty GURL if no such device exists.
81 GURL GetDeviceDescriptionURL(const std::string& label) const; 79 GURL GetDeviceDescriptionURL(const std::string& label) const;
82 80
83 // Adds a device directly to the registry as if it was discovered. For tests 81 // 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 82 // only. Note that if discovery is actually started, this device will be
85 // removed by PruneExpiredDevices(). 83 // removed by PruneExpiredDevices().
86 void AddDeviceForTest(const DialDeviceData& device_data); 84 void AddDeviceForTest(const DialDeviceData& device_data);
87 85
88 protected: 86 protected:
89 // Returns a new instance of the DIAL service. Overridden by tests. 87 // Returns a new instance of the DIAL service. Overridden by tests.
90 virtual std::unique_ptr<DialService> CreateDialService(); 88 virtual std::unique_ptr<DialService> CreateDialService();
91 virtual void ClearDialService(); 89 virtual void ClearDialService();
92 90
93 // Returns the current time. Overridden by tests. 91 // Returns the current time. Overridden by tests.
94 virtual base::Time Now() const; 92 virtual base::Time Now() const;
95 93
96 // The DIAL service. Periodic discovery is active when this is not NULL. 94 // The DIAL service. Periodic discovery is active when this is not NULL.
97 std::unique_ptr<DialService> dial_; 95 std::unique_ptr<DialService> dial_;
98 96
99 private: 97 private:
100 using DeviceByIdMap = 98 using DeviceByIdMap =
101 base::hash_map<std::string, std::unique_ptr<DialDeviceData>>; 99 base::hash_map<std::string, std::unique_ptr<DialDeviceData>>;
102 using DeviceByLabelMap = std::map<std::string, DialDeviceData*>; 100 using DeviceByLabelMap = std::map<std::string, DialDeviceData*>;
103 101
102 friend class MockDialRegistry;
103 friend struct base::DefaultSingletonTraits<DialRegistry>;
104
105 // Create the DIAL registry and pass a reference to allow it to notify on
imcheng 2017/03/23 23:06:00 nit: Remove this comment as it's not very useful.
zhaobin 2017/03/24 00:42:35 Done.
106 // DIAL device events.
107 DialRegistry();
108 ~DialRegistry() override;
109
104 // DialService::Observer: 110 // DialService::Observer:
105 void OnDiscoveryRequest(DialService* service) override; 111 void OnDiscoveryRequest(DialService* service) override;
106 void OnDeviceDiscovered(DialService* service, 112 void OnDeviceDiscovered(DialService* service,
107 const DialDeviceData& device) override; 113 const DialDeviceData& device) override;
108 void OnDiscoveryFinished(DialService* service) override; 114 void OnDiscoveryFinished(DialService* service) override;
109 void OnError(DialService* service, 115 void OnError(DialService* service,
110 const DialService::DialServiceErrorCode& code) override; 116 const DialService::DialServiceErrorCode& code) override;
111 117
112 // net::NetworkChangeObserver: 118 // net::NetworkChangeObserver:
113 void OnNetworkChanged( 119 void OnNetworkChanged(
114 net::NetworkChangeNotifier::ConnectionType type) override; 120 net::NetworkChangeNotifier::ConnectionType type) override;
115 121
116 // Notify all observers about DialDeviceEvent or DialError. 122 // Notify all observers about DialDeviceEvent or DialError.
117 void OnDialDeviceEvent(const DeviceList& devices); 123 void OnDialDeviceEvent(const DeviceList& devices);
118 void OnDialError(DialErrorCode type); 124 void OnDialError(DialErrorCode type);
119 125
120 // Starts and stops periodic discovery. Periodic discovery is done when there
121 // are registered event listeners.
122 void StartPeriodicDiscovery();
123 void StopPeriodicDiscovery();
124 126
125 // Check whether we are in a state ready to discover and dispatch error 127 // Check whether we are in a state ready to discover and dispatch error
126 // notifications if not. 128 // notifications if not.
127 bool ReadyToDiscover(); 129 bool ReadyToDiscover();
128 130
129 // Purge our whole registry. We may need to do this occasionally, e.g. when 131 // Purge our whole registry. We may need to do this occasionally, e.g. when
130 // the network status changes. Increments the registry generation. 132 // the network status changes. Increments the registry generation.
131 void Clear(); 133 void Clear();
132 134
133 // The repeating timer schedules discoveries with this method. 135 // The repeating timer schedules discoveries with this method.
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 const base::TimeDelta expiration_delta_; 174 const base::TimeDelta expiration_delta_;
173 const size_t max_devices_; 175 const size_t max_devices_;
174 176
175 // A map used to track known devices by their device_id. 177 // A map used to track known devices by their device_id.
176 DeviceByIdMap device_by_id_map_; 178 DeviceByIdMap device_by_id_map_;
177 179
178 // A map used to track known devices sorted by label. We iterate over this to 180 // A map used to track known devices sorted by label. We iterate over this to
179 // construct the device list sent to API clients. 181 // construct the device list sent to API clients.
180 DeviceByLabelMap device_by_label_map_; 182 DeviceByLabelMap device_by_label_map_;
181 183
182 // Timer used to manage periodic discovery requests. 184 // Timer used to manage periodic discovery requests. Timer is created and
183 base::RepeatingTimer repeating_timer_; 185 // destroyed on IO thread.
186 std::unique_ptr<base::RepeatingTimer> repeating_timer_;
184 187
185 // Interface from which the DIAL API is notified of DIAL device events. the 188 // Interface from which the DIAL API is notified of DIAL device events. the
186 // DIAL API owns this DIAL registry. 189 // DIAL API owns this DIAL registry.
187 base::ObserverList<Observer> observers_; 190 base::ObserverList<Observer> observers_;
188 191
189 FRIEND_TEST_ALL_PREFIXES(DialRegistryTest, TestAddRemoveListeners); 192 FRIEND_TEST_ALL_PREFIXES(DialRegistryTest, TestAddRemoveListeners);
190 FRIEND_TEST_ALL_PREFIXES(DialRegistryTest, TestNoDevicesDiscovered); 193 FRIEND_TEST_ALL_PREFIXES(DialRegistryTest, TestNoDevicesDiscovered);
191 FRIEND_TEST_ALL_PREFIXES(DialRegistryTest, TestDevicesDiscovered); 194 FRIEND_TEST_ALL_PREFIXES(DialRegistryTest, TestDevicesDiscovered);
192 FRIEND_TEST_ALL_PREFIXES(DialRegistryTest, 195 FRIEND_TEST_ALL_PREFIXES(DialRegistryTest,
193 TestDevicesDiscoveredWithTwoListeners); 196 TestDevicesDiscoveredWithTwoListeners);
194 FRIEND_TEST_ALL_PREFIXES(DialRegistryTest, TestDeviceExpires); 197 FRIEND_TEST_ALL_PREFIXES(DialRegistryTest, TestDeviceExpires);
195 FRIEND_TEST_ALL_PREFIXES(DialRegistryTest, TestExpiredDeviceIsRediscovered); 198 FRIEND_TEST_ALL_PREFIXES(DialRegistryTest, TestExpiredDeviceIsRediscovered);
196 FRIEND_TEST_ALL_PREFIXES(DialRegistryTest, 199 FRIEND_TEST_ALL_PREFIXES(DialRegistryTest,
197 TestRemovingListenerDoesNotClearList); 200 TestRemovingListenerDoesNotClearList);
198 FRIEND_TEST_ALL_PREFIXES(DialRegistryTest, TestNetworkEventConnectionLost); 201 FRIEND_TEST_ALL_PREFIXES(DialRegistryTest, TestNetworkEventConnectionLost);
199 FRIEND_TEST_ALL_PREFIXES(DialRegistryTest, 202 FRIEND_TEST_ALL_PREFIXES(DialRegistryTest,
200 TestNetworkEventConnectionRestored); 203 TestNetworkEventConnectionRestored);
201 DISALLOW_COPY_AND_ASSIGN(DialRegistry); 204 DISALLOW_COPY_AND_ASSIGN(DialRegistry);
202 }; 205 };
203 206
204 } // namespace media_router 207 } // namespace media_router
205 208
206 #endif // CHROME_BROWSER_MEDIA_ROUTER_DISCOVERY_DIAL_DIAL_REGISTRY_H_ 209 #endif // CHROME_BROWSER_MEDIA_ROUTER_DISCOVERY_DIAL_DIAL_REGISTRY_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698