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

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

Issue 2701633002: [Media Router] Add DialMediaSinkService and DeviceDescriptionService (Closed)
Patch Set: merge with master Created 3 years, 8 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
(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 <set>
10 #include <string>
11
12 #include "base/callback.h"
13 #include "base/gtest_prod_util.h"
14 #include "base/threading/thread_checker.h"
15 #include "base/timer/timer.h"
16 #include "chrome/browser/media/router/discovery/dial/dial_device_data.h"
17 #include "chrome/browser/media/router/discovery/dial/parsed_dial_device_descript ion.h"
18 #include "chrome/common/media_router/mojo/dial_device_description_parser.mojom.h "
19 #include "content/public/browser/browser_thread.h"
20
21 namespace net {
22 class URLRequestContextGetter;
23 }
24
25 namespace media_router {
26
27 class DeviceDescriptionFetcher;
28 class SafeDialDeviceDescriptionParser;
29
30 // This class fetches and parses device description XML for DIAL devices. Actual
31 // parsing happens in a separate utility process via SafeDeviceDescriptionParser
32 // (instead of in this class). This class lives on IO thread.
33 class DeviceDescriptionService {
34 public:
35 // Represents cached device description data parsed from device description
36 // XML.
37 struct CacheEntry {
38 CacheEntry();
39 CacheEntry(const CacheEntry& other);
40 ~CacheEntry();
41
42 // The expiration time from the cache.
43 base::Time expire_time;
44
45 // The device description version number (non-negative).
46 int32_t config_id;
47
48 // Parsed device description data from XML.
49 ParsedDialDeviceDescription description_data;
50 };
51
52 // Called if parsing device description XML in utility process succeeds, and
53 // all fields are valid.
54 // |device_data|: The device to look up.
55 // |description_data|: Device description data from device description XML.
56 using DeviceDescriptionParseSuccessCallback =
57 base::Callback<void(const DialDeviceData& device_data,
58 const ParsedDialDeviceDescription& description_data)>;
59
60 // Called if parsing device description XML in utility process fails, or some
61 // parsed fields are missing or invalid.
62 using DeviceDescriptionParseErrorCallback =
63 base::Callback<void(const DialDeviceData& device_data,
64 const std::string& error_message)>;
65
66 DeviceDescriptionService(
67 const DeviceDescriptionParseSuccessCallback& success_cb,
68 const DeviceDescriptionParseErrorCallback& error_cb);
69 virtual ~DeviceDescriptionService();
70
71 // For each device in |devices|, if there is a valid cache entry for it, call
72 // |success_cb_| with cached device description; otherwise start fetching
73 // device description XML and parsing XML in utility process. Call
74 // |success_cb_| if both fetching and parsing succeeds; otherwise call
75 // |error_cb_|.
76 // |request_context|: Used by the background URLFetchers.
77 virtual void GetDeviceDescriptions(
78 const std::vector<DialDeviceData>& devices,
79 net::URLRequestContextGetter* request_context);
80
81 // Remove expired cache entries from |description_map_|.
82 void CleanUpCacheEntries();
imcheng 2017/04/25 01:40:12 Does this need to be public?
zhaobin 2017/04/26 18:50:04 Done.
83
84 private:
85 friend class DeviceDescriptionServiceTest;
86 friend class TestDeviceDescriptionService;
87 FRIEND_TEST_ALL_PREFIXES(DeviceDescriptionServiceTest,
88 TestGetDeviceDescriptionRemoveOutDatedFetchers);
89 FRIEND_TEST_ALL_PREFIXES(DeviceDescriptionServiceTest,
90 TestGetDeviceDescriptionFetchURLError);
91 FRIEND_TEST_ALL_PREFIXES(DeviceDescriptionServiceTest,
92 TestSafeParserProperlyCreated);
93
94 // Checks the cache for a valid device description. If the entry is found but
95 // is expired, it is removed from the cache. Returns cached entry of
96 // parsed device description. Returns nullptr if cache entry does not exist or
97 // is not valid.
98 // |device_data|: The device to look up.
99 const CacheEntry* CheckAndUpdateCache(const DialDeviceData& device_data);
100
101 // Issues a HTTP GET request for the device description. No-op if there is
102 // already a pending request.
103 // |device_data|: The device to look up.
104 // |request_context|: Used by the background URLFetchers.
105 void FetchDeviceDescription(const DialDeviceData& device_data,
106 net::URLRequestContextGetter* request_context);
107
108 // Invoked when HTTP GET request finishes.
109 // |device_data|: Device data initiating the HTTP request.
110 // |description_data|: Response from HTTP request.
111 void OnDeviceDescriptionFetchComplete(
112 const DialDeviceData& device_data,
113 const DialDeviceDescriptionData& description_data);
114
115 // Invoked when HTTP GET request fails.
116 // |device_data|: Device data initiating the HTTP request.
117 // |error_message|: Error message from HTTP request.
118 void OnDeviceDescriptionFetchError(const DialDeviceData& device_data,
119 const std::string& error_message);
120
121 // Invoked when SafeDialDeviceDescriptionParser finishes parsing device
122 // description XML.
123 // |device_data|: Device data initiating XML parsing in utility process.
124 // |app_url|: The app Url.
125 // |device_description_ptr|: Parsed device description from utility process.
126 // Returns nullptr if parsing fails.
imcheng 2017/04/25 01:40:12 ... process, or nullptr if parsing failed.
zhaobin 2017/04/26 18:50:04 Done.
127 void OnParsedDeviceDescription(
128 const DialDeviceData& device_data,
129 const GURL& app_url,
130 chrome::mojom::DialDeviceDescriptionPtr device_description_ptr);
131
132 // Used by unit tests.
133 virtual base::Time GetNow();
134
135 // Map of current device description fetches in progress, keyed by device
136 // label.
137 std::map<std::string, std::unique_ptr<DeviceDescriptionFetcher>>
138 device_description_fetcher_map_;
139
140 // Set of device labels to be parsed in current utility process.
141 std::set<std::string> pending_device_labels_;
142
143 // Map of current cached device descriptions, keyed by device label.
144 std::map<std::string, CacheEntry> description_map_;
mark a. foltz 2017/04/25 21:06:25 Maybe description_cache_ ?
zhaobin 2017/04/26 18:50:04 Done.
145
146 // See comments for DeviceDescriptionParseSuccessCallback.
147 DeviceDescriptionParseSuccessCallback success_cb_;
148
149 // See comments for DeviceDescriptionParseErrorCallback.
150 DeviceDescriptionParseErrorCallback error_cb_;
151
152 // Timer for clean up expired cache entries.
153 std::unique_ptr<base::RepeatingTimer,
154 content::BrowserThread::DeleteOnIOThread>
imcheng 2017/04/25 01:40:12 Why do you need this? I thought this object lives
mark a. foltz 2017/04/25 21:06:25 BrowserThread::DeleteOnThread has a shortcut that
zhaobin 2017/04/26 18:50:04 Acknowledged.
zhaobin 2017/04/26 18:50:04 Will mark DialMediaSinkService object as DeleteOnI
155 clean_up_timer_;
156
157 // Safe DIAL parser associated with utility process.
158 std::unique_ptr<SafeDialDeviceDescriptionParser> parser_;
159
160 base::ThreadChecker thread_checker_;
161 };
162
163 } // namespace media_router
164
165 #endif // CHROME_BROWSER_MEDIA_ROUTER_DISCOVERY_DIAL_DEVICE_DESCRIPTION_SERVICE _H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698