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" | |
|
mark a. foltz
2017/03/11 00:08:15
I wouldn't introduce a MR -> extensions dependency
zhaobin
2017/03/28 13:36:59
Done.
| |
| 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 { | |
|
mark a. foltz
2017/03/11 00:08:15
We'll now have this struct, and DialDeviceData and
zhaobin
2017/04/10 18:44:41
Created ParsedDialDeviceDescription struct with le
| |
| 34 DialDeviceDescription(); | |
| 35 ~DialDeviceDescription(); | |
| 36 DialDeviceDescription(const DialDeviceDescription& other); | |
| 37 | |
| 38 std::string unique_id; | |
|
mark a. foltz
2017/03/11 00:08:15
Please document the meanings of these fields.
zhaobin
2017/03/28 13:36:59
Done.
| |
| 39 std::string device_label; | |
| 40 std::string friendly_name; | |
| 41 std::string ip_address; | |
| 42 GURL app_url; | |
| 43 base::Time fetch_time_millis; | |
| 44 base::Time expire_time_millis; | |
| 45 std::string device_type; | |
|
mark a. foltz
2017/03/11 00:08:15
Should this be an enum?
zhaobin
2017/03/28 13:36:59
Seems like a string according to spec...http://upn
| |
| 46 std::string model_name; | |
| 47 base::Optional<int32_t> config_id; | |
| 48 }; | |
| 49 | |
| 50 class DeviceDescriptionService { | |
| 51 public: | |
| 52 using DialRegistry = extensions::api::dial::DialRegistry; | |
| 53 using DialDeviceDescriptionData = | |
| 54 extensions::api::dial::DialDeviceDescriptionData; | |
| 55 using DialDeviceData = extensions::api::dial::DialDeviceData; | |
| 56 | |
| 57 class Observer { | |
| 58 public: | |
| 59 // Methods invoked on the IO thread when device description fetch for | |
| 60 // |device_id| completed. | |
| 61 virtual void OnDeviceDescriptionAvailable( | |
| 62 const std::string& device_id, | |
| 63 const DialDeviceDescription& description) = 0; | |
| 64 virtual void OnDeviceDescriptionFetchError( | |
| 65 const std::string& device_id, | |
| 66 const std::string& error_message) = 0; | |
| 67 | |
| 68 protected: | |
| 69 virtual ~Observer() {} | |
| 70 }; | |
| 71 | |
| 72 // This class does not take ownership of |observer|. | |
| 73 explicit DeviceDescriptionService(Observer* observer); | |
| 74 ~DeviceDescriptionService(); | |
| 75 | |
| 76 // Returns true and sets |out_description| to cached device description if | |
| 77 // there is a valid cache entry; Returns false and start device description | |
| 78 // fetch otherwise. | |
| 79 bool GetDeviceDescription(const DialDeviceData& device, | |
| 80 net::URLRequestContextGetter* request_context, | |
| 81 DialDeviceDescription* out_description); | |
| 82 | |
| 83 // Returns false if there is no pending fetcher for |device_label|. | |
| 84 bool MayStopDeviceDescriptionFetching(const std::string& device_label); | |
| 85 | |
| 86 private: | |
| 87 // Issues a HTTP GET request for the device description. No-op if there is | |
| 88 // already a pending request. | |
| 89 void FetchDeviceDescription(const DialDeviceData& dial_device, | |
| 90 net::URLRequestContextGetter* request_context); | |
| 91 | |
| 92 // Checks the cache for a valid device description. If the entry is found but | |
| 93 // is no longer valid, it is removed from the cache. | |
| 94 // |dial_device|: the device to look up. | |
| 95 // Returns nullptr if cache entry does not exist or is not valid. | |
| 96 bool CheckAndUpdateCache(const DialDeviceData& dial_device, | |
| 97 DialDeviceDescription* out_description); | |
| 98 | |
| 99 // Processes the result of getting device description and adds the device | |
| 100 // description to the cache. | |
| 101 // TODO(imcheng): Also cache permanent failures (b/32976125). | |
| 102 // |dial_device|: Device data from device discovery. | |
| 103 // |xml_text|: The device description. | |
| 104 // |app_url|: The application URL. | |
| 105 // Returns true and populates corresponding fields in |description| if | |
| 106 // processing succeeds; Returns false if processing fails. | |
| 107 bool ProcessDeviceDescription(const DialDeviceData& dial_device, | |
| 108 const std::string& xmlText, | |
| 109 const GURL& app_url, | |
| 110 DialDeviceDescription* description); | |
| 111 | |
| 112 // Parses a device description document into a DialDeviceDescription object. | |
| 113 // |dial_device|: Device data from device discovery. | |
| 114 // |xml_text|: The device description. | |
| 115 // |app_url|: The application URL. | |
| 116 // Returns true and populates corresponding fields in |description| if parsing | |
| 117 // succeeds; Returns false if parsing fails. | |
| 118 bool ParseDeviceDescription(const DialDeviceData& dial_device, | |
| 119 const std::string& xml_text, | |
| 120 const GURL& app_url, | |
| 121 DialDeviceDescription* description); | |
| 122 | |
| 123 // Invoked when HTTP GET request finishes. | |
| 124 // |dial_device|: Device data initiating the HTTP request. | |
| 125 // |description_data|: Response from HTTP request. | |
| 126 void OnDeviceDescriptionAvailable( | |
| 127 const DialDeviceData& dial_device, | |
| 128 const DialDeviceDescriptionData& description_data); | |
| 129 | |
| 130 // Invoked when HTTP GET request fails. | |
| 131 // |dial_device|: Device data initiating the HTTP request. | |
| 132 void OnDeviceDescriptionFetchError(const DialDeviceData& dial_device, | |
| 133 const std::string& error_message); | |
| 134 | |
| 135 // device label, DeviceDescriptionFetcher | |
| 136 std::map<std::string, | |
| 137 std::unique_ptr<extensions::api::dial::DeviceDescriptionFetcher>> | |
| 138 device_description_fetcher_map_; | |
| 139 | |
| 140 // device label, DialDeviceDescription | |
| 141 std::map<std::string, DialDeviceDescription> description_map_; | |
| 142 | |
| 143 // This class does not take ownership of |observer_| | |
| 144 Observer* observer_; | |
| 145 }; | |
| 146 | |
| 147 } // namespace media_router | |
| 148 | |
| 149 #endif // CHROME_BROWSER_MEDIA_ROUTER_DEVICE_DESCRIPTION_SERVICE_H_ | |
| OLD | NEW |