Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Side by Side Diff: components/ntp_snippets/remote/remote_suggestions_fetcher.h

Issue 2771713003: RemoteSuggestionsFetcher: Use common AccessTokenFetcher helper class (Closed)
Patch Set: comment Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | components/ntp_snippets/remote/remote_suggestions_fetcher.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_FETCHER_H_ 5 #ifndef COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_FETCHER_H_
6 #define COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_FETCHER_H_ 6 #define COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_FETCHER_H_
7 7
8 #include <memory> 8 #include <memory>
9 #include <queue> 9 #include <queue>
10 #include <string> 10 #include <string>
11 #include <utility> 11 #include <utility>
12 #include <vector> 12 #include <vector>
13 13
14 #include "base/callback.h" 14 #include "base/callback.h"
15 #include "base/optional.h" 15 #include "base/optional.h"
16 #include "base/time/clock.h" 16 #include "base/time/clock.h"
17 #include "base/time/tick_clock.h" 17 #include "base/time/tick_clock.h"
18 #include "components/ntp_snippets/category.h" 18 #include "components/ntp_snippets/category.h"
19 #include "components/ntp_snippets/category_info.h" 19 #include "components/ntp_snippets/category_info.h"
20 #include "components/ntp_snippets/remote/json_request.h" 20 #include "components/ntp_snippets/remote/json_request.h"
21 #include "components/ntp_snippets/remote/remote_suggestion.h" 21 #include "components/ntp_snippets/remote/remote_suggestion.h"
22 #include "components/ntp_snippets/remote/request_params.h" 22 #include "components/ntp_snippets/remote/request_params.h"
23 #include "components/ntp_snippets/status.h" 23 #include "components/ntp_snippets/status.h"
24 #include "components/translate/core/browser/language_model.h" 24 #include "components/translate/core/browser/language_model.h"
25 #include "components/version_info/version_info.h" 25 #include "components/version_info/version_info.h"
26 #include "net/url_request/url_request_context_getter.h" 26 #include "net/url_request/url_request_context_getter.h"
27 27
28 class AccessTokenFetcher;
29 class OAuth2TokenService;
28 class PrefService; 30 class PrefService;
29 class SigninManagerBase; 31 class SigninManagerBase;
30 32
31 namespace base { 33 namespace base {
32 class Value; 34 class Value;
33 } // namespace base 35 } // namespace base
34 36
35 namespace ntp_snippets { 37 namespace ntp_snippets {
36 38
37 class UserClassifier; 39 class UserClassifier;
(...skipping 15 matching lines...) Expand all
53 const base::Optional<base::string16>& title); 55 const base::Optional<base::string16>& title);
54 56
55 // Provides the CategoryInfo data for other remote suggestions. 57 // Provides the CategoryInfo data for other remote suggestions.
56 CategoryInfo BuildRemoteCategoryInfo(const base::string16& title, 58 CategoryInfo BuildRemoteCategoryInfo(const base::string16& title,
57 bool allow_fetching_more_results); 59 bool allow_fetching_more_results);
58 60
59 // Fetches suggestion data for the NTP from the server. 61 // Fetches suggestion data for the NTP from the server.
60 // TODO(fhorschig): Untangle cyclic dependencies by introducing a 62 // TODO(fhorschig): Untangle cyclic dependencies by introducing a
61 // RemoteSuggestionsFetcherInterface. (Would be good for mock implementations, 63 // RemoteSuggestionsFetcherInterface. (Would be good for mock implementations,
62 // too!) 64 // too!)
63 class RemoteSuggestionsFetcher : public OAuth2TokenService::Consumer, 65 class RemoteSuggestionsFetcher {
64 public OAuth2TokenService::Observer {
65 public: 66 public:
66 struct FetchedCategory { 67 struct FetchedCategory {
67 Category category; 68 Category category;
68 CategoryInfo info; 69 CategoryInfo info;
69 RemoteSuggestion::PtrVector suggestions; 70 RemoteSuggestion::PtrVector suggestions;
70 71
71 FetchedCategory(Category c, CategoryInfo&& info); 72 FetchedCategory(Category c, CategoryInfo&& info);
72 FetchedCategory(FetchedCategory&&); // = default, in .cc 73 FetchedCategory(FetchedCategory&&); // = default, in .cc
73 ~FetchedCategory(); // = default, in .cc 74 ~FetchedCategory(); // = default, in .cc
74 FetchedCategory& operator=(FetchedCategory&&); // = default, in .cc 75 FetchedCategory& operator=(FetchedCategory&&); // = default, in .cc
75 }; 76 };
76 using FetchedCategoriesVector = std::vector<FetchedCategory>; 77 using FetchedCategoriesVector = std::vector<FetchedCategory>;
77 using OptionalFetchedCategories = base::Optional<FetchedCategoriesVector>; 78 using OptionalFetchedCategories = base::Optional<FetchedCategoriesVector>;
78 79
79 using SnippetsAvailableCallback = 80 using SnippetsAvailableCallback =
80 base::OnceCallback<void(Status status, 81 base::OnceCallback<void(Status status,
81 OptionalFetchedCategories fetched_categories)>; 82 OptionalFetchedCategories fetched_categories)>;
82 83
83 RemoteSuggestionsFetcher( 84 RemoteSuggestionsFetcher(
84 SigninManagerBase* signin_manager, 85 SigninManagerBase* signin_manager,
85 OAuth2TokenService* token_service, 86 OAuth2TokenService* token_service,
86 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, 87 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter,
87 PrefService* pref_service, 88 PrefService* pref_service,
88 translate::LanguageModel* language_model, 89 translate::LanguageModel* language_model,
89 const ParseJSONCallback& parse_json_callback, 90 const ParseJSONCallback& parse_json_callback,
90 const GURL& api_endpoint, 91 const GURL& api_endpoint,
91 const std::string& api_key, 92 const std::string& api_key,
92 const UserClassifier* user_classifier); 93 const UserClassifier* user_classifier);
93 ~RemoteSuggestionsFetcher() override; 94 ~RemoteSuggestionsFetcher();
94 95
95 // Initiates a fetch from the server. When done (successfully or not), calls 96 // Initiates a fetch from the server. When done (successfully or not), calls
96 // the callback. 97 // the callback.
97 // 98 //
98 // If an ongoing fetch exists, both fetches won't influence each other (i.e. 99 // If an ongoing fetch exists, both fetches won't influence each other (i.e.
99 // every callback will be called exactly once). 100 // every callback will be called exactly once).
100 void FetchSnippets(const RequestParams& params, 101 void FetchSnippets(const RequestParams& params,
101 SnippetsAvailableCallback callback); 102 SnippetsAvailableCallback callback);
102 103
103 // Debug string representing the status/result of the last fetch attempt. 104 // Debug string representing the status/result of the last fetch attempt.
(...skipping 24 matching lines...) Expand all
128 FRIEND_TEST_ALL_PREFIXES(ChromeReaderSnippetsFetcherTest, 129 FRIEND_TEST_ALL_PREFIXES(ChromeReaderSnippetsFetcherTest,
129 BuildRequestWithUILanguageOnly); 130 BuildRequestWithUILanguageOnly);
130 FRIEND_TEST_ALL_PREFIXES(ChromeReaderSnippetsFetcherTest, 131 FRIEND_TEST_ALL_PREFIXES(ChromeReaderSnippetsFetcherTest,
131 BuildRequestWithOtherLanguageOnly); 132 BuildRequestWithOtherLanguageOnly);
132 friend class ChromeReaderSnippetsFetcherTest; 133 friend class ChromeReaderSnippetsFetcherTest;
133 134
134 void FetchSnippetsNonAuthenticated(internal::JsonRequest::Builder builder, 135 void FetchSnippetsNonAuthenticated(internal::JsonRequest::Builder builder,
135 SnippetsAvailableCallback callback); 136 SnippetsAvailableCallback callback);
136 void FetchSnippetsAuthenticated(internal::JsonRequest::Builder builder, 137 void FetchSnippetsAuthenticated(internal::JsonRequest::Builder builder,
137 SnippetsAvailableCallback callback, 138 SnippetsAvailableCallback callback,
138 const std::string& account_id,
139 const std::string& oauth_access_token); 139 const std::string& oauth_access_token);
140 void StartRequest(internal::JsonRequest::Builder builder, 140 void StartRequest(internal::JsonRequest::Builder builder,
141 SnippetsAvailableCallback callback); 141 SnippetsAvailableCallback callback);
142 142
143 void StartTokenRequest(); 143 void StartTokenRequest();
144 144
145 // OAuth2TokenService::Consumer overrides: 145 void AccessTokenFetchFinished(const GoogleServiceAuthError& error,
146 void OnGetTokenSuccess(const OAuth2TokenService::Request* request, 146 const std::string& access_token);
147 const std::string& access_token, 147 void AccessTokenError(const GoogleServiceAuthError& error);
148 const base::Time& expiration_time) override;
149 void OnGetTokenFailure(const OAuth2TokenService::Request* request,
150 const GoogleServiceAuthError& error) override;
151
152 // OAuth2TokenService::Observer overrides:
153 void OnRefreshTokenAvailable(const std::string& account_id) override;
154 148
155 void JsonRequestDone(std::unique_ptr<internal::JsonRequest> request, 149 void JsonRequestDone(std::unique_ptr<internal::JsonRequest> request,
156 SnippetsAvailableCallback callback, 150 SnippetsAvailableCallback callback,
157 std::unique_ptr<base::Value> result, 151 std::unique_ptr<base::Value> result,
158 internal::FetchResult status_code, 152 internal::FetchResult status_code,
159 const std::string& error_details); 153 const std::string& error_details);
160 void FetchFinished(OptionalFetchedCategories categories, 154 void FetchFinished(OptionalFetchedCategories categories,
161 SnippetsAvailableCallback callback, 155 SnippetsAvailableCallback callback,
162 internal::FetchResult status_code, 156 internal::FetchResult status_code,
163 const std::string& error_details); 157 const std::string& error_details);
164 158
165 bool JsonToSnippets(const base::Value& parsed, 159 bool JsonToSnippets(const base::Value& parsed,
166 FetchedCategoriesVector* categories, 160 FetchedCategoriesVector* categories,
167 const base::Time& fetch_time); 161 const base::Time& fetch_time);
168 162
169 // Authentication for signed-in users. 163 // Authentication for signed-in users.
170 SigninManagerBase* signin_manager_; 164 SigninManagerBase* signin_manager_;
171 OAuth2TokenService* token_service_; 165 OAuth2TokenService* token_service_;
172 std::unique_ptr<OAuth2TokenService::Request> oauth_request_;
173 bool waiting_for_refresh_token_ = false;
174 166
175 // When a token request gets canceled, we want to retry once. 167 std::unique_ptr<AccessTokenFetcher> token_fetcher_;
176 bool oauth_token_retried_ = false;
177 168
178 // Holds the URL request context. 169 // Holds the URL request context.
179 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; 170 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
180 171
181 // Stores requests that wait for an access token. 172 // Stores requests that wait for an access token.
182 std::queue< 173 std::queue<
183 std::pair<internal::JsonRequest::Builder, SnippetsAvailableCallback>> 174 std::pair<internal::JsonRequest::Builder, SnippetsAvailableCallback>>
184 pending_requests_; 175 pending_requests_;
185 176
186 // Weak reference, not owned. 177 // Weak reference, not owned.
(...skipping 16 matching lines...) Expand all
203 // Info on the last finished fetch. 194 // Info on the last finished fetch.
204 std::string last_status_; 195 std::string last_status_;
205 std::string last_fetch_json_; 196 std::string last_fetch_json_;
206 197
207 DISALLOW_COPY_AND_ASSIGN(RemoteSuggestionsFetcher); 198 DISALLOW_COPY_AND_ASSIGN(RemoteSuggestionsFetcher);
208 }; 199 };
209 200
210 } // namespace ntp_snippets 201 } // namespace ntp_snippets
211 202
212 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_FETCHER_H_ 203 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_FETCHER_H_
OLDNEW
« no previous file with comments | « no previous file | components/ntp_snippets/remote/remote_suggestions_fetcher.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698