prediction_service.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. #ifndef COMPONENTS_PERMISSIONS_PREDICTION_SERVICE_PREDICTION_SERVICE_H_
  5. #define COMPONENTS_PERMISSIONS_PREDICTION_SERVICE_PREDICTION_SERVICE_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/callback.h"
  9. #include "components/keyed_service/core/keyed_service.h"
  10. #include "components/permissions/permission_request_enums.h"
  11. #include "components/permissions/prediction_service/prediction_service_base.h"
  12. #include "components/permissions/prediction_service/prediction_service_messages.pb.h"
  13. #include "services/network/public/cpp/resource_request.h"
  14. #include "services/network/public/cpp/shared_url_loader_factory.h"
  15. #include "services/network/public/cpp/simple_url_loader.h"
  16. namespace permissions {
  17. // TODO(crbug.com/1138595, andypaicu): Refactor this class and
  18. // RealTimeUrlLookupServiceBase to derive from the same base class instead of
  19. // doing a bunch of duplicate work. Design doc:
  20. // go/permissions-predictions-client-doc
  21. // Service used to makes calls to the Web Permission Suggestions Service to
  22. // obtaing recomandations regarding permission prompts.
  23. class PredictionService : public PredictionServiceBase {
  24. public:
  25. using PendingRequestsMap = std::map<std::unique_ptr<network::SimpleURLLoader>,
  26. LookupResponseCallback>;
  27. explicit PredictionService(
  28. scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory);
  29. ~PredictionService() override;
  30. void StartLookup(const PredictionRequestFeatures& entity,
  31. LookupRequestCallback request_callback,
  32. LookupResponseCallback response_callback) override;
  33. void set_prediction_service_url_for_testing(const GURL& url) {
  34. prediction_service_url_override_ = url;
  35. }
  36. const PendingRequestsMap& pending_requests_for_testing() {
  37. return pending_requests_;
  38. }
  39. void recalculate_service_url_every_time_for_testing() {
  40. recalculate_service_url_every_time = true;
  41. }
  42. private:
  43. static const GURL GetPredictionServiceUrl(bool recalculate_for_testing);
  44. std::unique_ptr<network::ResourceRequest> GetResourceRequest();
  45. void SendRequestInternal(std::unique_ptr<network::ResourceRequest> request,
  46. const std::string& request_data,
  47. const PredictionRequestFeatures& entity,
  48. LookupResponseCallback response_callback);
  49. void OnURLLoaderComplete(const PredictionRequestFeatures& entity,
  50. network::SimpleURLLoader* loader,
  51. base::TimeTicks request_start_time,
  52. std::unique_ptr<std::string> response_body);
  53. std::unique_ptr<GeneratePredictionsResponse> CreatePredictionsResponse(
  54. network::SimpleURLLoader* loader,
  55. const std::string* response_body);
  56. PendingRequestsMap pending_requests_;
  57. scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
  58. GURL prediction_service_url_override_;
  59. bool recalculate_service_url_every_time = false;
  60. base::WeakPtrFactory<PredictionService> weak_factory_{this};
  61. };
  62. } // namespace permissions
  63. #endif // COMPONENTS_PERMISSIONS_PREDICTION_SERVICE_PREDICTION_SERVICE_H_