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

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

Issue 2754703005: [Device Discovery] Make DialRegistry a Singleton (Closed)
Patch Set: fix mac and windows compile errors 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/singleton.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 "net/base/network_change_notifier.h" 24 #include "net/base/network_change_notifier.h"
25 25
26 namespace media_router { 26 namespace media_router {
27 27
28 // Keeps track of devices that have responded to discovery requests and notifies 28 // Keeps track of devices that have responded to discovery requests and notifies
(...skipping 18 matching lines...) Expand all
47 public: 47 public:
48 // Methods invoked on the IO thread when a new device is discovered, an 48 // Methods invoked on the IO thread when a new device is discovered, an
49 // update is triggered by dial.discoverNow or an error occured. 49 // update is triggered by dial.discoverNow or an error occured.
50 virtual void OnDialDeviceEvent(const DeviceList& devices) = 0; 50 virtual void OnDialDeviceEvent(const DeviceList& devices) = 0;
51 virtual void OnDialError(DialErrorCode type) = 0; 51 virtual void OnDialError(DialErrorCode type) = 0;
52 52
53 protected: 53 protected:
54 virtual ~Observer() {} 54 virtual ~Observer() {}
55 }; 55 };
56 56
57 // Create the DIAL registry and pass a reference to allow it to notify on 57 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 58
65 // Called by the DIAL API when event listeners are added or removed. The dial 59 // 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 60 // service is started after the first listener is added and stopped after the
67 // last listener is removed. 61 // last listener is removed.
68 void OnListenerAdded(); 62 void OnListenerAdded();
69 void OnListenerRemoved(); 63 void OnListenerRemoved();
70 64
71 // This class does not take ownership of observer. 65 // pass a reference of |observer| to allow it to notify on DIAL device events.
66 // This class does not take ownership of |observer|.
72 void RegisterObserver(Observer* observer); 67 void RegisterObserver(Observer* observer);
73 void UnregisterObserver(Observer* observer); 68 void UnregisterObserver(Observer* observer);
74 69
75 // Called by the DIAL API to try to kickoff a discovery if there is not one 70 // Called by the DIAL API to try to kickoff a discovery if there is not one
76 // already active. 71 // already active.
77 bool DiscoverNow(); 72 bool DiscoverNow();
78 73
74 // Starts and stops periodic discovery. Periodic discovery is done when there
75 // are registered event listeners.
76 void StartPeriodicDiscovery();
77 void StopPeriodicDiscovery();
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 // Returns a new instance of the DIAL service. Overridden by tests. 89 // Returns a new instance of the DIAL service. Overridden by tests.
90 virtual std::unique_ptr<DialService> CreateDialService(); 90 virtual std::unique_ptr<DialService> CreateDialService();
91 virtual void ClearDialService(); 91 virtual void ClearDialService();
92 92
93 // Returns the current time. Overridden by tests. 93 // Returns the current time. Overridden by tests.
94 virtual base::Time Now() const; 94 virtual base::Time Now() const;
95 95
96 // The DIAL service. Periodic discovery is active when this is not NULL. 96 // The DIAL service. Periodic discovery is active when this is not NULL.
97 std::unique_ptr<DialService> dial_; 97 std::unique_ptr<DialService> dial_;
98 98
99 private: 99 private:
100 using DeviceByIdMap = 100 using DeviceByIdMap =
101 base::hash_map<std::string, std::unique_ptr<DialDeviceData>>; 101 base::hash_map<std::string, std::unique_ptr<DialDeviceData>>;
102 using DeviceByLabelMap = std::map<std::string, DialDeviceData*>; 102 using DeviceByLabelMap = std::map<std::string, DialDeviceData*>;
103 103
104 friend class MockDialRegistry;
105 friend struct base::DefaultSingletonTraits<DialRegistry>;
106
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
« no previous file with comments | « chrome/browser/extensions/api/dial/dial_api.cc ('k') | chrome/browser/media/router/discovery/dial/dial_registry.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698