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

Unified Diff: chrome/browser/media/router/device_description_service.h

Issue 2701633002: [Media Router] Add DialMediaSinkService and DeviceDescriptionService (Closed)
Patch Set: Created 3 years, 10 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/media/router/device_description_service.h
diff --git a/chrome/browser/media/router/device_description_service.h b/chrome/browser/media/router/device_description_service.h
new file mode 100644
index 0000000000000000000000000000000000000000..d7c235a3deab5406e6dec617a8226d0a6b7bf06c
--- /dev/null
+++ b/chrome/browser/media/router/device_description_service.h
@@ -0,0 +1,149 @@
+// 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_DEVICE_DESCRIPTION_SERVICE_H_
+#define CHROME_BROWSER_MEDIA_ROUTER_DEVICE_DESCRIPTION_SERVICE_H_
+
+#include <memory>
+#include <set>
+#include <string>
+#include <vector>
+
+#include "base/optional.h"
+#include "base/time/time.h"
+#include "base/timer/timer.h"
+#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.
+
+namespace extensions {
+namespace api {
+namespace dial {
+struct DialDeviceDescriptionData;
+class DeviceDescriptionFetcher;
+}
+}
+}
+
+namespace net {
+class URLRequestContextGetter;
+}
+
+namespace media_router {
+
+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
+ DialDeviceDescription();
+ ~DialDeviceDescription();
+ DialDeviceDescription(const DialDeviceDescription& other);
+
+ 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.
+ std::string device_label;
+ std::string friendly_name;
+ std::string ip_address;
+ GURL app_url;
+ base::Time fetch_time_millis;
+ base::Time expire_time_millis;
+ 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
+ std::string model_name;
+ base::Optional<int32_t> config_id;
+};
+
+class DeviceDescriptionService {
+ public:
+ using DialRegistry = extensions::api::dial::DialRegistry;
+ using DialDeviceDescriptionData =
+ extensions::api::dial::DialDeviceDescriptionData;
+ using DialDeviceData = extensions::api::dial::DialDeviceData;
+
+ class Observer {
+ public:
+ // Methods invoked on the IO thread when device description fetch for
+ // |device_id| completed.
+ virtual void OnDeviceDescriptionAvailable(
+ const std::string& device_id,
+ const DialDeviceDescription& description) = 0;
+ virtual void OnDeviceDescriptionFetchError(
+ const std::string& device_id,
+ const std::string& error_message) = 0;
+
+ protected:
+ virtual ~Observer() {}
+ };
+
+ // This class does not take ownership of |observer|.
+ explicit DeviceDescriptionService(Observer* observer);
+ ~DeviceDescriptionService();
+
+ // Returns true and sets |out_description| to cached device description if
+ // there is a valid cache entry; Returns false and start device description
+ // fetch otherwise.
+ bool GetDeviceDescription(const DialDeviceData& device,
+ net::URLRequestContextGetter* request_context,
+ DialDeviceDescription* out_description);
+
+ // Returns false if there is no pending fetcher for |device_label|.
+ bool MayStopDeviceDescriptionFetching(const std::string& device_label);
+
+ private:
+ // Issues a HTTP GET request for the device description. No-op if there is
+ // already a pending request.
+ void FetchDeviceDescription(const DialDeviceData& dial_device,
+ net::URLRequestContextGetter* request_context);
+
+ // Checks the cache for a valid device description. If the entry is found but
+ // is no longer valid, it is removed from the cache.
+ // |dial_device|: the device to look up.
+ // Returns nullptr if cache entry does not exist or is not valid.
+ bool CheckAndUpdateCache(const DialDeviceData& dial_device,
+ DialDeviceDescription* out_description);
+
+ // Processes the result of getting device description and adds the device
+ // description to the cache.
+ // TODO(imcheng): Also cache permanent failures (b/32976125).
+ // |dial_device|: Device data from device discovery.
+ // |xml_text|: The device description.
+ // |app_url|: The application URL.
+ // Returns true and populates corresponding fields in |description| if
+ // processing succeeds; Returns false if processing fails.
+ bool ProcessDeviceDescription(const DialDeviceData& dial_device,
+ const std::string& xmlText,
+ const GURL& app_url,
+ DialDeviceDescription* description);
+
+ // Parses a device description document into a DialDeviceDescription object.
+ // |dial_device|: Device data from device discovery.
+ // |xml_text|: The device description.
+ // |app_url|: The application URL.
+ // Returns true and populates corresponding fields in |description| if parsing
+ // succeeds; Returns false if parsing fails.
+ bool ParseDeviceDescription(const DialDeviceData& dial_device,
+ const std::string& xml_text,
+ const GURL& app_url,
+ DialDeviceDescription* description);
+
+ // Invoked when HTTP GET request finishes.
+ // |dial_device|: Device data initiating the HTTP request.
+ // |description_data|: Response from HTTP request.
+ void OnDeviceDescriptionAvailable(
+ const DialDeviceData& dial_device,
+ const DialDeviceDescriptionData& description_data);
+
+ // Invoked when HTTP GET request fails.
+ // |dial_device|: Device data initiating the HTTP request.
+ void OnDeviceDescriptionFetchError(const DialDeviceData& dial_device,
+ const std::string& error_message);
+
+ // device label, DeviceDescriptionFetcher
+ std::map<std::string,
+ std::unique_ptr<extensions::api::dial::DeviceDescriptionFetcher>>
+ device_description_fetcher_map_;
+
+ // device label, DialDeviceDescription
+ std::map<std::string, DialDeviceDescription> description_map_;
+
+ // This class does not take ownership of |observer_|
+ Observer* observer_;
+};
+
+} // namespace media_router
+
+#endif // CHROME_BROWSER_MEDIA_ROUTER_DEVICE_DESCRIPTION_SERVICE_H_

Powered by Google App Engine
This is Rietveld 408576698