Chromium Code Reviews| Index: chrome/browser/media/router/discovery/dial/device_description_service.h |
| diff --git a/chrome/browser/media/router/discovery/dial/device_description_service.h b/chrome/browser/media/router/discovery/dial/device_description_service.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..8d54eb804af4bd77787b37db9a46900df37c7e76 |
| --- /dev/null |
| +++ b/chrome/browser/media/router/discovery/dial/device_description_service.h |
| @@ -0,0 +1,147 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef CHROME_BROWSER_MEDIA_ROUTER_DISCOVERY_DIAL_DEVICE_DESCRIPTION_SERVICE_H_ |
| +#define CHROME_BROWSER_MEDIA_ROUTER_DISCOVERY_DIAL_DEVICE_DESCRIPTION_SERVICE_H_ |
| + |
| +#include <memory> |
| +#include <string> |
| + |
| +#include "base/callback.h" |
| +#include "base/threading/thread_checker.h" |
| +#include "base/timer/timer.h" |
| +#include "chrome/browser/media/router/discovery/dial/dial_device_data.h" |
| +#include "chrome/browser/media/router/discovery/dial/parsed_dial_device_description.h" |
| +#include "chrome/common/media_router/dial_device_description_parser.mojom.h" |
| +#include "content/public/browser/browser_thread.h" |
| + |
| +namespace net { |
| +class URLRequestContextGetter; |
| +} |
| + |
| +namespace media_router { |
| + |
| +class DeviceDescriptionFetcher; |
| +class SafeDialDeviceDescriptionParser; |
| + |
| +// 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.
|
| +// 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.
|
| +class DeviceDescriptionService { |
| + public: |
| + // Represents cached device description data parsed from device description |
| + // XML. |
| + struct CacheEntry { |
| + CacheEntry(); |
| + CacheEntry(const CacheEntry& other); |
| + ~CacheEntry(); |
| + |
| + // The expiration time from the cache. |
| + base::Time expire_time; |
| + |
| + // 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.
|
| + int32_t config_id; |
| + |
| + // 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.
|
| + ParsedDialDeviceDescription description_data; |
| + }; |
| + |
| + // Called if parsing device description XML in utility process succeeds, and |
| + // all fields are valid. |
| + // |device_data|: The device to look up. |
| + // |description_data|: Device description data from device description XML. |
| + using DeviceDescriptionParseSuccessCallback = |
| + base::Callback<void(const DialDeviceData& device_data, |
| + const ParsedDialDeviceDescription& description_data)>; |
| + |
| + // Called if parsing device description XML in utility process fails, or some |
| + // parsed fields are missing or invalid. |
| + using DeviceDescriptionParseErrorCallback = |
| + base::Callback<void(const DialDeviceData& device_data, |
| + const std::string& error_message)>; |
| + |
| + DeviceDescriptionService( |
| + const DeviceDescriptionParseSuccessCallback& success_cb, |
| + const DeviceDescriptionParseErrorCallback& error_cb); |
| + virtual ~DeviceDescriptionService(); |
| + |
| + // For each device in |devices|, if there is a valid cache entry for it, call |
| + // |success_cb_| with cached device description; otherwise start fetching |
| + // device description XML and parsing XML in utility process. Call |
| + // |success_cb_| if both fetching and parsing succeeds; otherwise call |
| + // |error_cb_|. |
| + // |request_context|: Used by the background URLFetchers. |
| + virtual void GetDeviceDescriptions( |
| + const std::vector<DialDeviceData>& devices, |
| + net::URLRequestContextGetter* request_context); |
| + |
| + // 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.
|
| + void CleanUpCacheEntries(); |
| + |
| + protected: |
| + virtual base::Time GetNow(); |
| + |
| + private: |
| + // Checks the cache for a valid device description. If the entry is found but |
| + // 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.
|
| + // parsed device description. Returns nullptr if cache entry does not exist or |
| + // is not valid. |
| + // |device_data|: The device to look up. |
| + const CacheEntry* CheckAndUpdateCache(const DialDeviceData& device_data); |
| + |
| + // Issues a HTTP GET request for the device description. No-op if there is |
| + // already a pending request. |
| + // |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.
|
| + void FetchDeviceDescription(const DialDeviceData& device_data, |
| + net::URLRequestContextGetter* request_context); |
| + |
| + // Invoked when HTTP GET request finishes. |
| + // |device_data|: Device data initiating the HTTP request. |
| + // |description_data|: Response from HTTP request. |
| + void OnDeviceDescriptionFetchComplete( |
| + const DialDeviceData& device_data, |
| + const DialDeviceDescriptionData& description_data); |
| + |
| + // Invoked when HTTP GET request fails. |
| + // |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.
|
| + void OnDeviceDescriptionFetchError(const DialDeviceData& device_data, |
| + const std::string& error_message); |
| + |
| + // Invoked when SafeDialDeviceDescriptionParser finishes parsing device |
| + // description XML. |
| + // |device_data|: Device data initiating XML parsing in utility process. |
| + // |app_url|: The app Url. |
| + // |device_description_ptr|: Parsed device description from utility process. |
| + // Returns nullptr if parsing fails. |
| + void OnParsedDeviceDescription( |
| + const DialDeviceData& device_data, |
| + const GURL& app_url, |
| + chrome::mojom::DialDeviceDescriptionPtr device_description_ptr); |
| + |
| + // 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.
|
| + std::map<std::string, std::unique_ptr<DeviceDescriptionFetcher>> |
| + device_description_fetcher_map_; |
| + |
| + // Map of <device label, CacheEntry> |
| + std::map<std::string, CacheEntry> description_map_; |
| + |
| + // See comments for DeviceDescriptionParseSuccessCallback. |
| + DeviceDescriptionParseSuccessCallback success_cb_; |
| + |
| + // See comments for DeviceDescriptionParseErrorCallback. |
| + DeviceDescriptionParseErrorCallback error_cb_; |
| + |
| + // Timer for clean up expired cache entries. |
| + std::unique_ptr<base::RepeatingTimer, |
| + content::BrowserThread::DeleteOnIOThread> |
| + clean_up_timer_; |
| + |
| + // Safe DIAL parser associated with utility process. |
| + scoped_refptr<SafeDialDeviceDescriptionParser> parser_; |
| + |
| + base::ThreadChecker thread_checker_; |
| +}; |
| + |
| +} // namespace media_router |
| + |
| +#endif // CHROME_BROWSER_MEDIA_ROUTER_DISCOVERY_DIAL_DEVICE_DESCRIPTION_SERVICE_H_ |