receiver_setup_querier.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2020 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. #include "components/mirroring/service/receiver_setup_querier.h"
  5. #include <string>
  6. #include "base/bind.h"
  7. #include "base/json/json_reader.h"
  8. #include "base/strings/stringprintf.h"
  9. #include "components/mirroring/service/value_util.h"
  10. #include "components/version_info/version_info.h"
  11. #include "media/cast/cast_environment.h"
  12. #include "media/cast/logging/raw_event_subscriber_bundle.h"
  13. #include "net/traffic_annotation/network_traffic_annotation.h"
  14. #include "services/network/public/cpp/resource_request.h"
  15. #include "services/network/public/cpp/simple_url_loader.h"
  16. namespace mirroring {
  17. namespace {
  18. // The maximum number of bytes for the receiver's setup info.
  19. constexpr int kMaxSetupResponseSizeBytes = 256 << 10; // 256KB
  20. // The endpoint for the setup query request.
  21. constexpr char kSetupQueryEndpointFormat[] = "http://%s:8008/setup/eureka_info";
  22. // The method type for the setup query request.
  23. constexpr char kSetupQueryMethod[] = "GET";
  24. // Helper to parse the response for receiver setup info.
  25. bool ParseReceiverSetupInfo(const std::string& response,
  26. std::string* build_version,
  27. std::string* name) {
  28. const absl::optional<base::Value> root = base::JSONReader::Read(response);
  29. return root && root->is_dict() &&
  30. GetString(*root, "cast_build_revision", build_version) &&
  31. GetString(*root, "name", name);
  32. }
  33. net::NetworkTrafficAnnotationTag GetAnnotationTag() {
  34. // NOTE: the network annotation must be a string literal to be validated
  35. // by the network annotations checker tool.
  36. return net::DefineNetworkTrafficAnnotation("mirroring_get_setup_info", R"(
  37. semantics {
  38. sender: "Mirroring Service"
  39. description:
  40. "Mirroring Service sends a request to the receiver to obtain its "
  41. "setup info such as the build version, the model name, etc. The "
  42. "data is used to enable/disable feature sets at runtime."
  43. trigger:
  44. "A tab/desktop mirroring session starts."
  45. data: "An HTTP GET request."
  46. destination: OTHER
  47. destination_other:
  48. "A mirroring receiver, such as a ChromeCast device."
  49. }
  50. policy {
  51. cookies_allowed: NO
  52. setting: "This feature cannot be disabled in settings."
  53. chrome_policy {
  54. EnableMediaRouter {
  55. EnableMediaRouter: false
  56. }
  57. }
  58. })");
  59. }
  60. } // namespace
  61. ReceiverSetupQuerier::ReceiverSetupQuerier(
  62. const net::IPAddress& address,
  63. mojo::PendingRemote<network::mojom::URLLoaderFactory> loader_factory)
  64. : address_(address), url_loader_factory_(std::move(loader_factory)) {
  65. Query();
  66. }
  67. ReceiverSetupQuerier::~ReceiverSetupQuerier() = default;
  68. void ReceiverSetupQuerier::Query() {
  69. auto resource_request = std::make_unique<network::ResourceRequest>();
  70. resource_request->method = kSetupQueryMethod;
  71. resource_request->url = GURL(base::StringPrintf(kSetupQueryEndpointFormat,
  72. address_.ToString().c_str()));
  73. auto url_loader = network::SimpleURLLoader::Create(
  74. std::move(resource_request), GetAnnotationTag());
  75. auto* const url_loader_ptr = url_loader.get();
  76. url_loader_ptr->DownloadToString(
  77. url_loader_factory_.get(),
  78. base::BindOnce(&ReceiverSetupQuerier::ProcessResponse,
  79. weak_factory_.GetWeakPtr(), std::move(url_loader)),
  80. kMaxSetupResponseSizeBytes);
  81. }
  82. void ReceiverSetupQuerier::ProcessResponse(
  83. std::unique_ptr<network::SimpleURLLoader> url_loader,
  84. std::unique_ptr<std::string> response) {
  85. if (url_loader->NetError() != net::OK) {
  86. VLOG(2) << "Unable to fetch receiver setup info.";
  87. return;
  88. }
  89. if (!ParseReceiverSetupInfo(*response, &build_version_, &friendly_name_)) {
  90. VLOG(2) << "Unable to parse receiver setup info.";
  91. }
  92. }
  93. } // namespace mirroring