remote_suggestions_fetcher_impl.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright 2017 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_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_FETCHER_IMPL_H_
  5. #define COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_FETCHER_IMPL_H_
  6. #include <memory>
  7. #include <string>
  8. #include <utility>
  9. #include "base/callback.h"
  10. #include "base/containers/queue.h"
  11. #include "base/memory/raw_ptr.h"
  12. #include "base/time/clock.h"
  13. #include "components/ntp_snippets/remote/json_request.h"
  14. #include "components/ntp_snippets/remote/json_to_categories.h"
  15. #include "components/ntp_snippets/remote/remote_suggestions_fetcher.h"
  16. #include "components/ntp_snippets/remote/request_params.h"
  17. #include "components/signin/public/identity_manager/access_token_info.h"
  18. #include "google_apis/gaia/google_service_auth_error.h"
  19. class PrefService;
  20. namespace base {
  21. class Value;
  22. } // namespace base
  23. namespace signin {
  24. class IdentityManager;
  25. class PrimaryAccountAccessTokenFetcher;
  26. } // namespace signin
  27. namespace language {
  28. class UrlLanguageHistogram;
  29. } // namespace language
  30. namespace network {
  31. class SharedURLLoaderFactory;
  32. } // namespace network
  33. namespace ntp_snippets {
  34. class UserClassifier;
  35. class RemoteSuggestionsFetcherImpl : public RemoteSuggestionsFetcher {
  36. public:
  37. RemoteSuggestionsFetcherImpl(
  38. signin::IdentityManager* identity_manager,
  39. scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
  40. PrefService* pref_service,
  41. language::UrlLanguageHistogram* language_histogram,
  42. const ParseJSONCallback& parse_json_callback,
  43. const GURL& api_endpoint,
  44. const std::string& api_key,
  45. const UserClassifier* user_classifier);
  46. RemoteSuggestionsFetcherImpl(const RemoteSuggestionsFetcherImpl&) = delete;
  47. RemoteSuggestionsFetcherImpl& operator=(const RemoteSuggestionsFetcherImpl&) =
  48. delete;
  49. ~RemoteSuggestionsFetcherImpl() override;
  50. void FetchSnippets(const RequestParams& params,
  51. SnippetsAvailableCallback callback) override;
  52. const std::string& GetLastStatusForDebugging() const override;
  53. const std::string& GetLastJsonForDebugging() const override;
  54. bool WasLastFetchAuthenticatedForDebugging() const override;
  55. const GURL& GetFetchUrlForDebugging() const override;
  56. // Overrides internal clock for testing purposes.
  57. void SetClockForTesting(const base::Clock* clock) { clock_ = clock; }
  58. static void set_skip_api_key_check_for_testing();
  59. private:
  60. void FetchSnippetsNonAuthenticated(internal::JsonRequest::Builder builder,
  61. SnippetsAvailableCallback callback);
  62. void FetchSnippetsAuthenticated(internal::JsonRequest::Builder builder,
  63. SnippetsAvailableCallback callback,
  64. const std::string& oauth_access_token);
  65. void StartRequest(internal::JsonRequest::Builder builder,
  66. SnippetsAvailableCallback callback,
  67. bool is_authenticated,
  68. std::string access_token);
  69. void StartTokenRequest();
  70. void AccessTokenFetchFinished(base::Time token_start_time,
  71. GoogleServiceAuthError error,
  72. signin::AccessTokenInfo access_token_info);
  73. void AccessTokenError(const GoogleServiceAuthError& error);
  74. void JsonRequestDone(std::unique_ptr<internal::JsonRequest> request,
  75. SnippetsAvailableCallback callback,
  76. bool is_authenticated,
  77. std::string access_token,
  78. base::Value result,
  79. internal::FetchResult status_code,
  80. const std::string& error_details);
  81. void FetchFinished(OptionalFetchedCategories categories,
  82. SnippetsAvailableCallback callback,
  83. internal::FetchResult status_code,
  84. const std::string& error_details,
  85. bool is_authenticated,
  86. std::string access_token);
  87. void EmitDurationAndInvokeCallback(
  88. base::Time start_time,
  89. SnippetsAvailableCallback callback,
  90. Status status,
  91. OptionalFetchedCategories fetched_categories);
  92. // Authentication for signed-in users.
  93. raw_ptr<signin::IdentityManager> identity_manager_;
  94. std::unique_ptr<signin::PrimaryAccountAccessTokenFetcher> token_fetcher_;
  95. // Holds the URL loader factory
  96. scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
  97. // Stores requests that wait for an access token.
  98. base::queue<
  99. std::pair<internal::JsonRequest::Builder, SnippetsAvailableCallback>>
  100. pending_requests_;
  101. // Weak reference, not owned.
  102. const raw_ptr<language::UrlLanguageHistogram> language_histogram_;
  103. const ParseJSONCallback parse_json_callback_;
  104. // API endpoint for fetching suggestions.
  105. const GURL fetch_url_;
  106. // API key to use for non-authenticated requests.
  107. const std::string api_key_;
  108. // Allow for an injectable clock for testing.
  109. raw_ptr<const base::Clock> clock_;
  110. // Classifier that tells us how active the user is. Not owned.
  111. raw_ptr<const UserClassifier> user_classifier_;
  112. // Info on the last finished fetch.
  113. std::string last_status_;
  114. std::string last_fetch_json_;
  115. bool last_fetch_authenticated_;
  116. static bool skip_api_key_check_for_testing_;
  117. };
  118. } // namespace ntp_snippets
  119. #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_FETCHER_IMPL_H_