request_sender.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright (c) 2012 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 GOOGLE_APIS_COMMON_REQUEST_SENDER_H_
  5. #define GOOGLE_APIS_COMMON_REQUEST_SENDER_H_
  6. #include <memory>
  7. #include <set>
  8. #include <string>
  9. #include "base/callback_forward.h"
  10. #include "base/containers/unique_ptr_adapters.h"
  11. #include "base/memory/ref_counted.h"
  12. #include "base/memory/weak_ptr.h"
  13. #include "base/threading/thread_checker.h"
  14. #include "google_apis/common/api_error_codes.h"
  15. #include "services/network/public/cpp/shared_url_loader_factory.h"
  16. namespace base {
  17. class SequencedTaskRunner;
  18. }
  19. namespace google_apis {
  20. class AuthenticatedRequestInterface;
  21. class AuthServiceInterface;
  22. // Helper class that sends requests implementing
  23. // AuthenticatedRequestInterface and handles retries and authentication.
  24. class RequestSender {
  25. public:
  26. // |auth_service| is used for fetching OAuth tokens.
  27. //
  28. // |url_loader_factory| is the factory used to load resources requested by
  29. // this RequestSender.
  30. //
  31. // |blocking_task_runner| is used for running blocking operation, e.g.,
  32. // parsing JSON response from the server.
  33. //
  34. // |custom_user_agent| will be used for the User-Agent header in HTTP
  35. // requests issued through the request sender if the value is not empty.
  36. RequestSender(
  37. std::unique_ptr<AuthServiceInterface> auth_service,
  38. scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
  39. const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner,
  40. const std::string& custom_user_agent,
  41. const net::NetworkTrafficAnnotationTag& traffic_annotation);
  42. RequestSender(const RequestSender&) = delete;
  43. RequestSender& operator=(const RequestSender&) = delete;
  44. ~RequestSender();
  45. AuthServiceInterface* auth_service() { return auth_service_.get(); }
  46. network::SharedURLLoaderFactory* url_loader_factory() const {
  47. return url_loader_factory_.get();
  48. }
  49. base::SequencedTaskRunner* blocking_task_runner() const {
  50. return blocking_task_runner_.get();
  51. }
  52. // Starts a request implementing the AuthenticatedRequestInterface
  53. // interface, and makes the request retry upon authentication failures by
  54. // calling back to RetryRequest.
  55. //
  56. // Returns a closure to cancel the request. The closure cancels the request
  57. // if it is in-flight, and does nothing if it is already terminated.
  58. base::RepeatingClosure StartRequestWithAuthRetry(
  59. std::unique_ptr<AuthenticatedRequestInterface> request);
  60. // Notifies to this RequestSender that |request| has finished.
  61. // TODO(kinaba): refactor the life time management and make this at private.
  62. void RequestFinished(AuthenticatedRequestInterface* request);
  63. // Returns traffic annotation tag asssigned to this object.
  64. const net::NetworkTrafficAnnotationTag& get_traffic_annotation_tag() const {
  65. return traffic_annotation_;
  66. }
  67. private:
  68. base::RepeatingClosure StartRequestWithAuthRetryInternal(
  69. AuthenticatedRequestInterface* request);
  70. // Called when the access token is fetched.
  71. void OnAccessTokenFetched(
  72. const base::WeakPtr<AuthenticatedRequestInterface>& request,
  73. ApiErrorCode error,
  74. const std::string& access_token);
  75. // Clears any authentication token and retries the request, which forces
  76. // an authentication token refresh.
  77. void RetryRequest(AuthenticatedRequestInterface* request);
  78. // Cancels the request. Used for implementing the returned closure of
  79. // StartRequestWithAuthRetry.
  80. void CancelRequest(
  81. const base::WeakPtr<AuthenticatedRequestInterface>& request);
  82. std::unique_ptr<AuthServiceInterface> auth_service_;
  83. scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
  84. scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
  85. std::set<std::unique_ptr<AuthenticatedRequestInterface>,
  86. base::UniquePtrComparator>
  87. in_flight_requests_;
  88. const std::string custom_user_agent_;
  89. base::ThreadChecker thread_checker_;
  90. const net::NetworkTrafficAnnotationTag traffic_annotation_;
  91. // Note: This should remain the last member so it'll be destroyed and
  92. // invalidate its weak pointers before any other members are destroyed.
  93. base::WeakPtrFactory<RequestSender> weak_ptr_factory_{this};
  94. };
  95. } // namespace google_apis
  96. #endif // GOOGLE_APIS_COMMON_REQUEST_SENDER_H_