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_DISCOVERY_DIAL_DEVICE_DESCRIPTION_SERVICE_H_ | |
6 #define CHROME_BROWSER_MEDIA_ROUTER_DISCOVERY_DIAL_DEVICE_DESCRIPTION_SERVICE_H_ | |
7 | |
8 #include <memory> | |
9 #include <string> | |
10 | |
11 #include "base/callback.h" | |
12 #include "base/threading/thread_checker.h" | |
13 #include "base/timer/timer.h" | |
14 #include "chrome/browser/media/router/discovery/dial/dial_device_data.h" | |
15 #include "chrome/browser/media/router/discovery/dial/parsed_dial_device_descript ion.h" | |
16 #include "chrome/common/media_router/dial_device_description_parser.mojom.h" | |
17 #include "content/public/browser/browser_thread.h" | |
18 | |
19 namespace net { | |
20 class URLRequestContextGetter; | |
21 } | |
22 | |
23 namespace media_router { | |
24 | |
25 class DeviceDescriptionFetcher; | |
26 class SafeDialDeviceDescriptionParser; | |
27 | |
28 // This class fetches and parses device description xml for DIAL devices. It | |
mark a. foltz
2017/04/18 18:16:26
s/xml/XML/
zhaobin
2017/04/21 23:12:58
Done.
| |
29 // lives on IO thread. | |
mark a. foltz
2017/04/18 18:16:26
You might mention that the parsing happens in a se
zhaobin
2017/04/21 23:12:59
Done.
| |
30 class DeviceDescriptionService { | |
31 public: | |
32 // Represents cached device description data parsed from device description | |
33 // XML. | |
34 struct CacheEntry { | |
35 CacheEntry(); | |
36 CacheEntry(const CacheEntry& other); | |
37 ~CacheEntry(); | |
38 | |
39 // The expiration time from the cache. | |
40 base::Time expire_time; | |
41 | |
42 // The version number. | |
mark a. foltz
2017/04/18 18:16:27
The device description version number (non-negativ
zhaobin
2017/04/21 23:12:59
Done.
| |
43 int32_t config_id; | |
44 | |
45 // Underlying device description data from XML. | |
mark a. foltz
2017/04/18 18:16:26
s/Underlying/Parsed/
zhaobin
2017/04/21 23:12:58
Done.
| |
46 ParsedDialDeviceDescription description_data; | |
47 }; | |
48 | |
49 // Called if parsing device description XML in utility process succeeds, and | |
50 // all fields are valid. | |
51 // |device_data|: The device to look up. | |
52 // |description_data|: Device description data from device description XML. | |
53 using DeviceDescriptionParseSuccessCallback = | |
54 base::Callback<void(const DialDeviceData& device_data, | |
55 const ParsedDialDeviceDescription& description_data)>; | |
56 | |
57 // Called if parsing device description XML in utility process fails, or some | |
58 // parsed fields are missing or invalid. | |
59 using DeviceDescriptionParseErrorCallback = | |
60 base::Callback<void(const DialDeviceData& device_data, | |
61 const std::string& error_message)>; | |
62 | |
63 DeviceDescriptionService( | |
64 const DeviceDescriptionParseSuccessCallback& success_cb, | |
65 const DeviceDescriptionParseErrorCallback& error_cb); | |
66 virtual ~DeviceDescriptionService(); | |
67 | |
68 // For each device in |devices|, if there is a valid cache entry for it, call | |
69 // |success_cb_| with cached device description; otherwise start fetching | |
70 // device description XML and parsing XML in utility process. Call | |
71 // |success_cb_| if both fetching and parsing succeeds; otherwise call | |
72 // |error_cb_|. | |
73 // |request_context|: Used by the background URLFetchers. | |
74 virtual void GetDeviceDescriptions( | |
75 const std::vector<DialDeviceData>& devices, | |
76 net::URLRequestContextGetter* request_context); | |
77 | |
78 // Remove expired cache entry from |description_map_|. | |
mark a. foltz
2017/04/18 18:16:26
s/entry/entries/
zhaobin
2017/04/21 23:12:59
Done.
| |
79 void CleanUpCacheEntries(); | |
80 | |
81 protected: | |
82 virtual base::Time GetNow(); | |
83 | |
84 private: | |
85 // Checks the cache for a valid device description. If the entry is found but | |
86 // is no longer valid, it is removed from the cache. Ruturns cached entry of | |
mark a. foltz
2017/04/18 18:16:26
s/is no longer valid/is expired/
Typo in Returns
zhaobin
2017/04/21 23:12:59
Done.
| |
87 // parsed device description. Returns nullptr if cache entry does not exist or | |
88 // is not valid. | |
89 // |device_data|: The device to look up. | |
90 const CacheEntry* CheckAndUpdateCache(const DialDeviceData& device_data); | |
91 | |
92 // Issues a HTTP GET request for the device description. No-op if there is | |
93 // already a pending request. | |
94 // |device_data|: The device to look up. | |
mark a. foltz
2017/04/18 18:16:27
Nit: document |request_context|
zhaobin
2017/04/21 23:12:58
Done.
| |
95 void FetchDeviceDescription(const DialDeviceData& device_data, | |
96 net::URLRequestContextGetter* request_context); | |
97 | |
98 // Invoked when HTTP GET request finishes. | |
99 // |device_data|: Device data initiating the HTTP request. | |
100 // |description_data|: Response from HTTP request. | |
101 void OnDeviceDescriptionFetchComplete( | |
102 const DialDeviceData& device_data, | |
103 const DialDeviceDescriptionData& description_data); | |
104 | |
105 // Invoked when HTTP GET request fails. | |
106 // |device_data|: Device data initiating the HTTP request. | |
mark a. foltz
2017/04/18 18:16:26
Nit: document |error_message|
zhaobin
2017/04/21 23:12:59
Done.
| |
107 void OnDeviceDescriptionFetchError(const DialDeviceData& device_data, | |
108 const std::string& error_message); | |
109 | |
110 // Invoked when SafeDialDeviceDescriptionParser finishes parsing device | |
111 // description XML. | |
112 // |device_data|: Device data initiating XML parsing in utility process. | |
113 // |app_url|: The app Url. | |
114 // |device_description_ptr|: Parsed device description from utility process. | |
115 // Returns nullptr if parsing fails. | |
116 void OnParsedDeviceDescription( | |
117 const DialDeviceData& device_data, | |
118 const GURL& app_url, | |
119 chrome::mojom::DialDeviceDescriptionPtr device_description_ptr); | |
120 | |
121 // Map of <device label, DeviceDescriptionFetcher> | |
mark a. foltz
2017/04/18 18:16:27
Document the meaning of the entries, e.g. "Map of
zhaobin
2017/04/21 23:12:59
Done.
| |
122 std::map<std::string, std::unique_ptr<DeviceDescriptionFetcher>> | |
123 device_description_fetcher_map_; | |
124 | |
125 // Map of <device label, CacheEntry> | |
126 std::map<std::string, CacheEntry> description_map_; | |
127 | |
128 // See comments for DeviceDescriptionParseSuccessCallback. | |
129 DeviceDescriptionParseSuccessCallback success_cb_; | |
130 | |
131 // See comments for DeviceDescriptionParseErrorCallback. | |
132 DeviceDescriptionParseErrorCallback error_cb_; | |
133 | |
134 // Timer for clean up expired cache entries. | |
135 std::unique_ptr<base::RepeatingTimer, | |
136 content::BrowserThread::DeleteOnIOThread> | |
137 clean_up_timer_; | |
138 | |
139 // Safe DIAL parser associated with utility process. | |
140 scoped_refptr<SafeDialDeviceDescriptionParser> parser_; | |
141 | |
142 base::ThreadChecker thread_checker_; | |
143 }; | |
144 | |
145 } // namespace media_router | |
146 | |
147 #endif // CHROME_BROWSER_MEDIA_ROUTER_DISCOVERY_DIAL_DEVICE_DESCRIPTION_SERVICE _H_ | |
OLD | NEW |