Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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_MEDIA_ROUTER_DEVICE_DESCRIPTION_SERVICE_H_ | |
| 6 #define CHROME_BROWSER_MEDIA_ROUTER_DEVICE_DESCRIPTION_SERVICE_H_ | |
| 7 | |
| 8 #include <memory> | |
| 9 #include <set> | |
| 10 #include <string> | |
| 11 #include <vector> | |
| 12 | |
| 13 #include "base/optional.h" | |
| 14 #include "base/time/time.h" | |
| 15 #include "base/timer/timer.h" | |
| 16 #include "chrome/browser/extensions/api/dial/dial_registry.h" | |
| 17 | |
| 18 namespace extensions { | |
| 19 namespace api { | |
| 20 namespace dial { | |
| 21 struct DialDeviceDescriptionData; | |
| 22 class DeviceDescriptionFetcher; | |
| 23 } | |
| 24 } | |
| 25 } | |
| 26 | |
| 27 namespace net { | |
| 28 class URLRequestContextGetter; | |
| 29 } | |
| 30 | |
| 31 namespace media_router { | |
| 32 | |
| 33 struct DialDeviceDescription { | |
| 34 DialDeviceDescription(); | |
|
mark a. foltz
2017/03/11 00:08:15
There should be a ctor that takes mandatory proper
zhaobin
2017/03/28 13:37:00
Done.
| |
| 35 ~DialDeviceDescription(); | |
| 36 DialDeviceDescription(const DialDeviceDescription& other); | |
|
mark a. foltz
2017/03/11 00:08:15
Should there be a default assignment operator assi
zhaobin
2017/03/28 13:37:00
Done.
| |
| 37 | |
| 38 std::string unique_id; | |
| 39 std::string device_label; | |
| 40 std::string friendly_name; | |
| 41 std::string ip_address; | |
|
mark a. foltz
2017/03/11 00:08:15
net::IPAddress
zhaobin
2017/03/28 13:37:00
Done.
| |
| 42 GURL app_url; | |
| 43 base::Time fetch_time_millis; | |
| 44 base::Time expire_time_millis; | |
| 45 std::string device_type; | |
| 46 std::string model_name; | |
| 47 base::Optional<int32_t> config_id; | |
| 48 GURL device_description_url; | |
| 49 }; | |
| 50 | |
| 51 class DeviceDescriptionService { | |
| 52 public: | |
| 53 using DialRegistry = extensions::api::dial::DialRegistry; | |
|
mark a. foltz
2017/03/11 00:08:16
I don't think chrome/browser/media should depend o
zhaobin
2017/03/28 13:37:00
Done.
| |
| 54 using DialDeviceDescriptionData = | |
|
mark a. foltz
2017/03/11 00:08:15
ISTM that the DialDeviceDescription struct is a su
zhaobin
2017/04/10 18:44:42
Use ParsedDialDeviceDescription struct to store da
| |
| 55 extensions::api::dial::DialDeviceDescriptionData; | |
| 56 using DialDeviceData = extensions::api::dial::DialDeviceData; | |
| 57 using CheckAccessCallback = base::Callback<void(bool)>; | |
| 58 | |
| 59 class Observer { | |
| 60 public: | |
| 61 // Methods invoked on the IO thread when device description fetch for | |
| 62 // |device_id| completed. | |
| 63 virtual void OnDeviceDescriptionAvailable( | |
| 64 const std::string& device_id, | |
|
mark a. foltz
2017/03/11 00:08:15
Is this a device_label or unique_id? Can you rena
zhaobin
2017/03/28 13:37:00
Done.
| |
| 65 const DialDeviceDescription& description) = 0; | |
| 66 virtual void OnDeviceDescriptionFetchError( | |
| 67 const std::string& device_id, | |
| 68 const std::string& error_message) = 0; | |
| 69 | |
| 70 protected: | |
| 71 virtual ~Observer() {} | |
| 72 }; | |
| 73 | |
| 74 // This class does not take ownership of |observer|. | |
| 75 explicit DeviceDescriptionService(Observer* observer); | |
| 76 virtual ~DeviceDescriptionService(); | |
| 77 | |
| 78 // Returns true and sets |out_description| to cached device description if | |
| 79 // there is a valid cache entry; Returns false and start device description | |
| 80 // fetch otherwise. | |
| 81 virtual bool GetDeviceDescription( | |
|
mark a. foltz
2017/03/11 00:08:15
Can this take a callback argument so the caller do
zhaobin
2017/04/10 18:44:42
Done.
| |
| 82 const DialDeviceData& device, | |
| 83 net::URLRequestContextGetter* request_context, | |
| 84 DialDeviceDescription* out_description); | |
| 85 | |
| 86 // Returns false if there is no pending fetcher for |device_label|. | |
| 87 virtual bool MayStopDeviceDescriptionFetching( | |
|
mark a. foltz
2017/03/11 00:08:16
It sounds like it could just be CancelFetch(device
zhaobin
2017/03/28 13:37:00
Done.
| |
| 88 const std::string& device_label); | |
| 89 | |
| 90 /** | |
| 91 * Issues a request to fetch the DIAL receiver's device description. Invoke | |
| 92 * check_access_callback with true if it was accessible, false otherwise. | |
| 93 * |device_description_url| The device description to fetch. | |
| 94 */ | |
| 95 virtual void CheckAccess(const GURL& device_description_url, | |
|
mark a. foltz
2017/03/11 00:08:15
Should this take DialDeviceData& like GetDeviceDes
zhaobin
2017/03/28 13:37:00
Code removed.
| |
| 96 net::URLRequestContextGetter* request_context, | |
| 97 const CheckAccessCallback& check_access_cb); | |
| 98 | |
| 99 private: | |
| 100 // Issues a HTTP GET request for the device description. No-op if there is | |
| 101 // already a pending request. | |
| 102 void FetchDeviceDescription(const DialDeviceData& dial_device, | |
| 103 net::URLRequestContextGetter* request_context); | |
| 104 | |
| 105 // Checks the cache for a valid device description. If the entry is found but | |
| 106 // is no longer valid, it is removed from the cache. | |
| 107 // |dial_device|: the device to look up. | |
| 108 // Returns nullptr if cache entry does not exist or is not valid. | |
| 109 bool CheckAndUpdateCache(const DialDeviceData& dial_device, | |
| 110 DialDeviceDescription* out_description); | |
| 111 | |
| 112 // Processes the result of getting device description and adds the device | |
| 113 // description to the cache. | |
| 114 // TODO(imcheng): Also cache permanent failures (b/32976125). | |
| 115 // |dial_device|: Device data from device discovery. | |
| 116 // |xml_text|: The device description. | |
| 117 // |app_url|: The application URL. | |
| 118 // Returns true and populates corresponding fields in |description| if | |
| 119 // processing succeeds; Returns false if processing fails. | |
| 120 bool ProcessDeviceDescription(const DialDeviceData& dial_device, | |
|
mark a. foltz
2017/03/11 00:08:15
ISTM that this could take just the DialDeviceDescr
zhaobin
2017/04/10 18:44:42
Done.
| |
| 121 const std::string& xmlText, | |
| 122 const GURL& app_url, | |
| 123 DialDeviceDescription* description); | |
| 124 | |
| 125 // Parses a device description document into a DialDeviceDescription object. | |
| 126 // |dial_device|: Device data from device discovery. | |
| 127 // |xml_text|: The device description. | |
| 128 // |app_url|: The application URL. | |
| 129 // Returns true and populates corresponding fields in |description| if parsing | |
| 130 // succeeds; Returns false if parsing fails. | |
| 131 bool ParseDeviceDescription(const DialDeviceData& dial_device, | |
|
mark a. foltz
2017/03/11 00:08:16
Nit: It seems like this could be written to only n
zhaobin
2017/03/28 13:37:00
Code removed.
| |
| 132 const std::string& xml_text, | |
| 133 const GURL& app_url, | |
| 134 DialDeviceDescription* description); | |
| 135 | |
| 136 // Invoked when HTTP GET request finishes. | |
| 137 // |dial_device|: Device data initiating the HTTP request. | |
| 138 // |description_data|: Response from HTTP request. | |
| 139 void OnDeviceDescriptionAvailable( | |
|
mark a. foltz
2017/03/11 00:08:16
OnDeviceDescriptionFetchComplete?
zhaobin
2017/03/28 13:37:00
Done.
| |
| 140 const DialDeviceData& dial_device, | |
| 141 const DialDeviceDescriptionData& description_data); | |
| 142 | |
| 143 // Invoked when HTTP GET request fails. | |
| 144 // |dial_device|: Device data initiating the HTTP request. | |
| 145 void OnDeviceDescriptionFetchError(const DialDeviceData& dial_device, | |
| 146 const std::string& error_message); | |
| 147 | |
| 148 // Invoked when HTTP GET request finishes. | |
|
mark a. foltz
2017/03/11 00:08:15
I'd like to think a bit about the design that requ
zhaobin
2017/03/28 13:37:00
Removed for initial impl.
| |
| 149 void OnCheckAccessSucceeded( | |
| 150 const CheckAccessCallback& check_access_cb, | |
| 151 const DialDeviceDescriptionData& description_data); | |
| 152 | |
| 153 // Invoked when HTTP GET request fails. | |
| 154 void OnCheckAccessError(const CheckAccessCallback& check_access_cb, | |
| 155 const std::string& error_message); | |
| 156 | |
| 157 // device label, DeviceDescriptionFetcher | |
| 158 std::map<std::string, | |
| 159 std::unique_ptr<extensions::api::dial::DeviceDescriptionFetcher>> | |
| 160 device_description_fetcher_map_; | |
| 161 | |
| 162 // device label, DeviceDescriptionFetcher | |
| 163 std::map<GURL, | |
|
mark a. foltz
2017/03/11 00:08:15
Is this keyed by the device description URL?
I wo
zhaobin
2017/03/28 13:37:00
Code removed.
| |
| 164 std::unique_ptr<extensions::api::dial::DeviceDescriptionFetcher>> | |
| 165 pending_access_requests_; | |
| 166 | |
| 167 // device label, DialDeviceDescription | |
| 168 std::map<std::string, DialDeviceDescription> description_map_; | |
| 169 | |
| 170 // This class does not take ownership of |observer_| | |
| 171 Observer* observer_; | |
|
mark a. foltz
2017/03/11 00:08:16
A singleton observer would usually be called a Del
zhaobin
2017/04/10 18:44:42
Done.
| |
| 172 }; | |
| 173 | |
| 174 } // namespace media_router | |
| 175 | |
| 176 #endif // CHROME_BROWSER_MEDIA_ROUTER_DEVICE_DESCRIPTION_SERVICE_H_ | |
| OLD | NEW |