| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_DIAL_DIAL_REGISTRY_H_ | |
| 6 #define CHROME_BROWSER_EXTENSIONS_API_DIAL_DIAL_REGISTRY_H_ | |
| 7 | |
| 8 #include <stddef.h> | |
| 9 | |
| 10 #include <map> | |
| 11 #include <memory> | |
| 12 #include <string> | |
| 13 #include <vector> | |
| 14 | |
| 15 #include "base/containers/hash_tables.h" | |
| 16 #include "base/gtest_prod_util.h" | |
| 17 #include "base/macros.h" | |
| 18 #include "base/memory/ref_counted.h" | |
| 19 #include "base/observer_list.h" | |
| 20 #include "base/time/time.h" | |
| 21 #include "base/timer/timer.h" | |
| 22 #include "chrome/browser/extensions/api/dial/dial_service.h" | |
| 23 #include "chrome/browser/profiles/profile.h" | |
| 24 #include "net/base/network_change_notifier.h" | |
| 25 | |
| 26 namespace extensions { | |
| 27 namespace api { | |
| 28 namespace dial { | |
| 29 | |
| 30 // Keeps track of devices that have responded to discovery requests and notifies | |
| 31 // the observer with an updated, complete set of active devices. The registry's | |
| 32 // observer (i.e., the Dial API) owns the registry instance. | |
| 33 // DialRegistry lives on the IO thread. | |
| 34 class DialRegistry : public DialService::Observer, | |
| 35 public net::NetworkChangeNotifier::NetworkChangeObserver { | |
| 36 public: | |
| 37 using DeviceList = std::vector<DialDeviceData>; | |
| 38 | |
| 39 enum DialErrorCode { | |
| 40 DIAL_NO_LISTENERS = 0, | |
| 41 DIAL_NO_INTERFACES, | |
| 42 DIAL_NETWORK_DISCONNECTED, | |
| 43 DIAL_CELLULAR_NETWORK, | |
| 44 DIAL_SOCKET_ERROR, | |
| 45 DIAL_UNKNOWN | |
| 46 }; | |
| 47 | |
| 48 class Observer { | |
| 49 public: | |
| 50 // Methods invoked on the IO thread when a new device is discovered, an | |
| 51 // update is triggered by dial.discoverNow or an error occured. | |
| 52 virtual void OnDialDeviceEvent(const DeviceList& devices) = 0; | |
| 53 virtual void OnDialError(DialErrorCode type) = 0; | |
| 54 | |
| 55 protected: | |
| 56 virtual ~Observer() {} | |
| 57 }; | |
| 58 | |
| 59 // Create the DIAL registry and pass a reference to allow it to notify on | |
| 60 // DIAL device events. | |
| 61 DialRegistry(base::TimeDelta refresh_interval, | |
| 62 base::TimeDelta expiration, | |
| 63 const size_t max_devices); | |
| 64 | |
| 65 ~DialRegistry() override; | |
| 66 | |
| 67 // Called by the DIAL API when event listeners are added or removed. The dial | |
| 68 // service is started after the first listener is added and stopped after the | |
| 69 // last listener is removed. | |
| 70 void OnListenerAdded(); | |
| 71 void OnListenerRemoved(); | |
| 72 | |
| 73 // This class does not take ownership of observer. | |
| 74 void RegisterObserver(Observer* observer); | |
| 75 void UnregisterObserver(Observer* observer); | |
| 76 | |
| 77 // Called by the DIAL API to try to kickoff a discovery if there is not one | |
| 78 // already active. | |
| 79 bool DiscoverNow(); | |
| 80 | |
| 81 // Returns the URL of the device description for the device identified by | |
| 82 // |label|, or an empty GURL if no such device exists. | |
| 83 GURL GetDeviceDescriptionURL(const std::string& label) const; | |
| 84 | |
| 85 // Adds a device directly to the registry as if it was discovered. For tests | |
| 86 // only. Note that if discovery is actually started, this device will be | |
| 87 // removed by PruneExpiredDevices(). | |
| 88 void AddDeviceForTest(const DialDeviceData& device_data); | |
| 89 | |
| 90 protected: | |
| 91 // Returns a new instance of the DIAL service. Overridden by tests. | |
| 92 virtual std::unique_ptr<DialService> CreateDialService(); | |
| 93 virtual void ClearDialService(); | |
| 94 | |
| 95 // Returns the current time. Overridden by tests. | |
| 96 virtual base::Time Now() const; | |
| 97 | |
| 98 // The DIAL service. Periodic discovery is active when this is not NULL. | |
| 99 std::unique_ptr<DialService> dial_; | |
| 100 | |
| 101 private: | |
| 102 using DeviceByIdMap = | |
| 103 base::hash_map<std::string, std::unique_ptr<DialDeviceData>>; | |
| 104 using DeviceByLabelMap = std::map<std::string, DialDeviceData*>; | |
| 105 | |
| 106 // DialService::Observer: | |
| 107 void OnDiscoveryRequest(DialService* service) override; | |
| 108 void OnDeviceDiscovered(DialService* service, | |
| 109 const DialDeviceData& device) override; | |
| 110 void OnDiscoveryFinished(DialService* service) override; | |
| 111 void OnError(DialService* service, | |
| 112 const DialService::DialServiceErrorCode& code) override; | |
| 113 | |
| 114 // net::NetworkChangeObserver: | |
| 115 void OnNetworkChanged( | |
| 116 net::NetworkChangeNotifier::ConnectionType type) override; | |
| 117 | |
| 118 // Notify all observers about DialDeviceEvent or DialError. | |
| 119 void OnDialDeviceEvent(const DeviceList& devices); | |
| 120 void OnDialError(DialErrorCode type); | |
| 121 | |
| 122 // Starts and stops periodic discovery. Periodic discovery is done when there | |
| 123 // are registered event listeners. | |
| 124 void StartPeriodicDiscovery(); | |
| 125 void StopPeriodicDiscovery(); | |
| 126 | |
| 127 // Check whether we are in a state ready to discover and dispatch error | |
| 128 // notifications if not. | |
| 129 bool ReadyToDiscover(); | |
| 130 | |
| 131 // Purge our whole registry. We may need to do this occasionally, e.g. when | |
| 132 // the network status changes. Increments the registry generation. | |
| 133 void Clear(); | |
| 134 | |
| 135 // The repeating timer schedules discoveries with this method. | |
| 136 void DoDiscovery(); | |
| 137 | |
| 138 // Attempts to add a newly discovered device to the registry. Returns true if | |
| 139 // successful. | |
| 140 bool MaybeAddDevice(std::unique_ptr<DialDeviceData> device_data); | |
| 141 | |
| 142 // Remove devices from the registry that have expired, i.e. not responded | |
| 143 // after some time. Returns true if the registry was modified. | |
| 144 bool PruneExpiredDevices(); | |
| 145 | |
| 146 // Returns true if the device has expired and should be removed from the | |
| 147 // active set. | |
| 148 bool IsDeviceExpired(const DialDeviceData& device) const; | |
| 149 | |
| 150 // Notify listeners with the current device list if the list has changed. | |
| 151 void MaybeSendEvent(); | |
| 152 | |
| 153 // Notify listeners with the current device list. | |
| 154 void SendEvent(); | |
| 155 | |
| 156 // Returns the next label to use for a newly-seen device. | |
| 157 std::string NextLabel(); | |
| 158 | |
| 159 // The current number of event listeners attached to this registry. | |
| 160 int num_listeners_; | |
| 161 | |
| 162 // Incremented each time we modify the registry of active devices. | |
| 163 int registry_generation_; | |
| 164 | |
| 165 // The registry generation associated with the last time we sent an event. | |
| 166 // Used to suppress events with duplicate device lists. | |
| 167 int last_event_registry_generation_; | |
| 168 | |
| 169 // Counter to generate device labels. | |
| 170 int label_count_; | |
| 171 | |
| 172 // Registry parameters | |
| 173 const base::TimeDelta refresh_interval_delta_; | |
| 174 const base::TimeDelta expiration_delta_; | |
| 175 const size_t max_devices_; | |
| 176 | |
| 177 // A map used to track known devices by their device_id. | |
| 178 DeviceByIdMap device_by_id_map_; | |
| 179 | |
| 180 // A map used to track known devices sorted by label. We iterate over this to | |
| 181 // construct the device list sent to API clients. | |
| 182 DeviceByLabelMap device_by_label_map_; | |
| 183 | |
| 184 // Timer used to manage periodic discovery requests. | |
| 185 base::RepeatingTimer repeating_timer_; | |
| 186 | |
| 187 // Interface from which the DIAL API is notified of DIAL device events. the | |
| 188 // DIAL API owns this DIAL registry. | |
| 189 base::ObserverList<Observer> observers_; | |
| 190 | |
| 191 FRIEND_TEST_ALL_PREFIXES(DialRegistryTest, TestAddRemoveListeners); | |
| 192 FRIEND_TEST_ALL_PREFIXES(DialRegistryTest, TestNoDevicesDiscovered); | |
| 193 FRIEND_TEST_ALL_PREFIXES(DialRegistryTest, TestDevicesDiscovered); | |
| 194 FRIEND_TEST_ALL_PREFIXES(DialRegistryTest, | |
| 195 TestDevicesDiscoveredWithTwoListeners); | |
| 196 FRIEND_TEST_ALL_PREFIXES(DialRegistryTest, TestDeviceExpires); | |
| 197 FRIEND_TEST_ALL_PREFIXES(DialRegistryTest, TestExpiredDeviceIsRediscovered); | |
| 198 FRIEND_TEST_ALL_PREFIXES(DialRegistryTest, | |
| 199 TestRemovingListenerDoesNotClearList); | |
| 200 FRIEND_TEST_ALL_PREFIXES(DialRegistryTest, TestNetworkEventConnectionLost); | |
| 201 FRIEND_TEST_ALL_PREFIXES(DialRegistryTest, | |
| 202 TestNetworkEventConnectionRestored); | |
| 203 DISALLOW_COPY_AND_ASSIGN(DialRegistry); | |
| 204 }; | |
| 205 | |
| 206 } // namespace dial | |
| 207 } // namespace api | |
| 208 } // namespace extensions | |
| 209 | |
| 210 #endif // CHROME_BROWSER_EXTENSIONS_API_DIAL_DIAL_REGISTRY_H_ | |
| OLD | NEW |