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

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: fix chromeos compile error Created 3 years, 7 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
20 namespace net {
21 class URLRequestContextGetter;
22 }
23
24 namespace media_router {
25
26 class DeviceDescriptionFetcher;
27 class SafeDialDeviceDescriptionParser;
28
29 // This class fetches and parses device description XML for DIAL devices. Actual
30 // parsing happens in a separate utility process via SafeDeviceDescriptionParser
31 // (instead of in this class). This class lives on IO thread.
32 class DeviceDescriptionService {
33 public:
34 // Represents cached device description data parsed from device description
35 // XML.
36 struct CacheEntry {
37 CacheEntry();
38 CacheEntry(const CacheEntry& other);
39 ~CacheEntry();
40
41 // The expiration time from the cache.
42 base::Time expire_time;
43
44 // The device description version number (non-negative).
45 int32_t config_id;
46
47 // Parsed device description data from XML.
48 ParsedDialDeviceDescription description_data;
49 };
50
51 // Called if parsing device description XML in utility process succeeds, and
52 // all fields are valid.
53 // |device_data|: The device to look up.
54 // |description_data|: Device description data from device description XML.
55 using DeviceDescriptionParseSuccessCallback =
56 base::Callback<void(const DialDeviceData& device_data,
57 const ParsedDialDeviceDescription& description_data)>;
58
59 // Called if parsing device description XML in utility process fails, or some
60 // parsed fields are missing or invalid.
61 using DeviceDescriptionParseErrorCallback =
62 base::Callback<void(const DialDeviceData& device_data,
63 const std::string& error_message)>;
64
65 DeviceDescriptionService(
66 const DeviceDescriptionParseSuccessCallback& success_cb,
67 const DeviceDescriptionParseErrorCallback& error_cb);
68 virtual ~DeviceDescriptionService();
69
70 // For each device in |devices|, if there is a valid cache entry for it, call
71 // |success_cb_| with cached device description; otherwise start fetching
72 // device description XML and parsing XML in utility process. Call
73 // |success_cb_| if both fetching and parsing succeeds; otherwise call
74 // |error_cb_|.
75 // |request_context|: Used by the background URLFetchers.
76 virtual void GetDeviceDescriptions(
77 const std::vector<DialDeviceData>& devices,
78 net::URLRequestContextGetter* request_context);
79
80 private:
81 friend class DeviceDescriptionServiceTest;
82 friend class TestDeviceDescriptionService;
83 FRIEND_TEST_ALL_PREFIXES(DeviceDescriptionServiceTest,
84 TestGetDeviceDescriptionRemoveOutDatedFetchers);
85 FRIEND_TEST_ALL_PREFIXES(DeviceDescriptionServiceTest,
86 TestGetDeviceDescriptionFetchURLError);
87 FRIEND_TEST_ALL_PREFIXES(DeviceDescriptionServiceTest,
88 TestCleanUpCacheEntries);
89 FRIEND_TEST_ALL_PREFIXES(DeviceDescriptionServiceTest,
90 TestSafeParserProperlyCreated);
91
92 // Checks the cache for a valid device description. If the entry is found but
93 // is expired, it is removed from the cache. Returns cached entry of
94 // parsed device description. Returns nullptr if cache entry does not exist or
95 // is not valid.
96 // |device_data|: The device to look up.
97 const CacheEntry* CheckAndUpdateCache(const DialDeviceData& device_data);
98
99 // Issues a HTTP GET request for the device description. No-op if there is
100 // already a pending request.
101 // |device_data|: The device to look up.
102 // |request_context|: Used by the background URLFetchers.
103 void FetchDeviceDescription(const DialDeviceData& device_data,
104 net::URLRequestContextGetter* request_context);
105
106 // Invoked when HTTP GET request finishes.
107 // |device_data|: Device data initiating the HTTP request.
108 // |description_data|: Response from HTTP request.
109 void OnDeviceDescriptionFetchComplete(
110 const DialDeviceData& device_data,
111 const DialDeviceDescriptionData& description_data);
112
113 // Invoked when HTTP GET request fails.
114 // |device_data|: Device data initiating the HTTP request.
115 // |error_message|: Error message from HTTP request.
116 void OnDeviceDescriptionFetchError(const DialDeviceData& device_data,
117 const std::string& error_message);
118
119 // Invoked when SafeDialDeviceDescriptionParser finishes parsing device
120 // description XML.
121 // |device_data|: Device data initiating XML parsing in utility process.
122 // |app_url|: The app Url.
123 // |device_description_ptr|: Parsed device description from utility process,
124 // or nullptr if parsing failed.
125 void OnParsedDeviceDescription(
126 const DialDeviceData& device_data,
127 const GURL& app_url,
128 chrome::mojom::DialDeviceDescriptionPtr device_description_ptr);
129
130 // Remove expired cache entries from |description_map_|.
131 void CleanUpCacheEntries();
132
133 // Used by unit tests.
134 virtual base::Time GetNow();
135
136 // Map of current device description fetches in progress, keyed by device
137 // label.
138 std::map<std::string, std::unique_ptr<DeviceDescriptionFetcher>>
139 device_description_fetcher_map_;
140
141 // Set of device labels to be parsed in current utility process.
142 std::set<std::string> pending_device_labels_;
143
144 // Map of current cached device descriptions, keyed by device label.
145 std::map<std::string, CacheEntry> description_cache_;
146
147 // See comments for DeviceDescriptionParseSuccessCallback.
148 DeviceDescriptionParseSuccessCallback success_cb_;
149
150 // See comments for DeviceDescriptionParseErrorCallback.
151 DeviceDescriptionParseErrorCallback error_cb_;
152
153 // Timer for clean up expired cache entries.
154 std::unique_ptr<base::RepeatingTimer> clean_up_timer_;
155
156 // Safe DIAL parser associated with utility process. When this object is
157 // destroyed, associating utility process will shutdown. Keep |parser_| alive
158 // until finish parsing all device descriptions of devices passed in
159 // |GetDeviceDescriptions()|. If second |GetDeviceDescriptions()| arrives and
160 // |parser_| is still alive, reuse |parser_| instead of creating a new object.
161 std::unique_ptr<SafeDialDeviceDescriptionParser> parser_;
162
163 base::ThreadChecker thread_checker_;
164 };
165
166 } // namespace media_router
167
168 #endif // CHROME_BROWSER_MEDIA_ROUTER_DISCOVERY_DIAL_DEVICE_DESCRIPTION_SERVICE _H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698