prediction_model_fetcher_impl.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2019 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. #ifndef COMPONENTS_OPTIMIZATION_GUIDE_CORE_PREDICTION_MODEL_FETCHER_IMPL_H_
  5. #define COMPONENTS_OPTIMIZATION_GUIDE_CORE_PREDICTION_MODEL_FETCHER_IMPL_H_
  6. #include <memory>
  7. #include <string>
  8. #include <vector>
  9. #include "base/callback.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/memory/scoped_refptr.h"
  12. #include "base/sequence_checker.h"
  13. #include "components/optimization_guide/core/prediction_model_fetcher.h"
  14. #include "components/optimization_guide/proto/models.pb.h"
  15. #include "third_party/abseil-cpp/absl/types/optional.h"
  16. #include "url/gurl.h"
  17. namespace network {
  18. class SharedURLLoaderFactory;
  19. class SimpleURLLoader;
  20. } // namespace network
  21. namespace optimization_guide {
  22. // A class to handle requests for prediction models (and prediction data) from
  23. // a remote Optimization Guide Service.
  24. //
  25. // This class fetches new models from the remote Optimization Guide Service.
  26. class PredictionModelFetcherImpl : public PredictionModelFetcher {
  27. public:
  28. PredictionModelFetcherImpl(
  29. scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
  30. const GURL& optimization_guide_service_get_models_url);
  31. PredictionModelFetcherImpl(const PredictionModelFetcherImpl&) = delete;
  32. PredictionModelFetcherImpl& operator=(const PredictionModelFetcherImpl&) =
  33. delete;
  34. ~PredictionModelFetcherImpl() override;
  35. // PredictionModelFetcher implementation
  36. bool FetchOptimizationGuideServiceModels(
  37. const std::vector<proto::ModelInfo>& models_request_info,
  38. proto::RequestContext request_context,
  39. const std::string& locale,
  40. ModelsFetchedCallback models_fetched_callback) override;
  41. private:
  42. // URL loader completion callback.
  43. void OnURLLoadComplete(std::unique_ptr<std::string> response_body);
  44. // Handles the response from the remote Optimization Guide Service.
  45. // |response| is the response body, |status| is the
  46. // |net::Error| of the response, and response_code is the HTTP
  47. // response code (if available).
  48. void HandleResponse(const std::string& response,
  49. int status,
  50. int response_code);
  51. // Used to hold the callback while the SimpleURLLoader performs the request
  52. // asynchronously.
  53. ModelsFetchedCallback models_fetched_callback_;
  54. // The URL for the remote Optimization Guide Service that serves models and
  55. // host features.
  56. const GURL optimization_guide_service_get_models_url_;
  57. // Used to hold the GetModelsRequest being constructed and sent as a remote
  58. // request.
  59. std::unique_ptr<optimization_guide::proto::GetModelsRequest>
  60. pending_models_request_;
  61. // Holds the URLLoader for an active hints request.
  62. std::unique_ptr<network::SimpleURLLoader> url_loader_;
  63. // Used for creating a |url_loader_| when needed for request hints.
  64. scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
  65. SEQUENCE_CHECKER(sequence_checker_);
  66. };
  67. } // namespace optimization_guide
  68. #endif // COMPONENTS_OPTIMIZATION_GUIDE_CORE_PREDICTION_MODEL_FETCHER_IMPL_H_