| OLD | NEW |
| 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_NTP_SNIPPETS_FETCHER_H_ | 5 #ifndef COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPETS_FETCHER_H_ |
| 6 #define COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPETS_FETCHER_H_ | 6 #define COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPETS_FETCHER_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 | 24 |
| 25 class PrefService; | 25 class PrefService; |
| 26 class SigninManagerBase; | 26 class SigninManagerBase; |
| 27 | 27 |
| 28 namespace base { | 28 namespace base { |
| 29 class Value; | 29 class Value; |
| 30 } // namespace base | 30 } // namespace base |
| 31 | 31 |
| 32 namespace ntp_snippets { | 32 namespace ntp_snippets { |
| 33 | 33 |
| 34 class UserClassifier; |
| 35 |
| 34 // Fetches snippet data for the NTP from the server. | 36 // Fetches snippet data for the NTP from the server. |
| 35 class NTPSnippetsFetcher : public OAuth2TokenService::Consumer, | 37 class NTPSnippetsFetcher : public OAuth2TokenService::Consumer, |
| 36 public OAuth2TokenService::Observer, | 38 public OAuth2TokenService::Observer, |
| 37 public net::URLFetcherDelegate { | 39 public net::URLFetcherDelegate { |
| 38 public: | 40 public: |
| 39 // Callbacks for JSON parsing, needed because the parsing is platform- | 41 // Callbacks for JSON parsing, needed because the parsing is platform- |
| 40 // dependent. | 42 // dependent. |
| 41 using SuccessCallback = base::Callback<void(std::unique_ptr<base::Value>)>; | 43 using SuccessCallback = base::Callback<void(std::unique_ptr<base::Value>)>; |
| 42 using ErrorCallback = base::Callback<void(const std::string&)>; | 44 using ErrorCallback = base::Callback<void(const std::string&)>; |
| 43 using ParseJSONCallback = base::Callback< | 45 using ParseJSONCallback = base::Callback< |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 kBoth | 87 kBoth |
| 86 }; | 88 }; |
| 87 | 89 |
| 88 NTPSnippetsFetcher( | 90 NTPSnippetsFetcher( |
| 89 SigninManagerBase* signin_manager, | 91 SigninManagerBase* signin_manager, |
| 90 OAuth2TokenService* token_service, | 92 OAuth2TokenService* token_service, |
| 91 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, | 93 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, |
| 92 PrefService* pref_service, | 94 PrefService* pref_service, |
| 93 CategoryFactory* category_factory, | 95 CategoryFactory* category_factory, |
| 94 const ParseJSONCallback& parse_json_callback, | 96 const ParseJSONCallback& parse_json_callback, |
| 95 const std::string& api_key); | 97 const std::string& api_key, |
| 98 const UserClassifier* user_classifier); |
| 96 ~NTPSnippetsFetcher() override; | 99 ~NTPSnippetsFetcher() override; |
| 97 | 100 |
| 98 // Set a callback that is called when a new set of snippets are downloaded, | 101 // Set a callback that is called when a new set of snippets are downloaded, |
| 99 // overriding any previously set callback. | 102 // overriding any previously set callback. |
| 100 void SetCallback(const SnippetsAvailableCallback& callback); | 103 void SetCallback(const SnippetsAvailableCallback& callback); |
| 101 | 104 |
| 102 // Fetches snippets from the server. |hosts| restricts the results to a set of | 105 // Fetches snippets from the server. |hosts| restricts the results to a set of |
| 103 // hosts, e.g. "www.google.com". If |hosts| is empty, no host restrictions are | 106 // hosts, e.g. "www.google.com". If |hosts| is empty, no host restrictions are |
| 104 // applied. | 107 // applied. |
| 105 // | 108 // |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 | 159 |
| 157 struct RequestParams { | 160 struct RequestParams { |
| 158 FetchAPI fetch_api; | 161 FetchAPI fetch_api; |
| 159 std::string obfuscated_gaia_id; | 162 std::string obfuscated_gaia_id; |
| 160 bool only_return_personalized_results; | 163 bool only_return_personalized_results; |
| 161 std::string user_locale; | 164 std::string user_locale; |
| 162 std::set<std::string> host_restricts; | 165 std::set<std::string> host_restricts; |
| 163 std::set<std::string> excluded_ids; | 166 std::set<std::string> excluded_ids; |
| 164 int count_to_fetch; | 167 int count_to_fetch; |
| 165 bool interactive_request; | 168 bool interactive_request; |
| 169 std::string user_class; |
| 166 | 170 |
| 167 RequestParams(); | 171 RequestParams(); |
| 168 ~RequestParams(); | 172 ~RequestParams(); |
| 169 | 173 |
| 170 std::string BuildRequest(); | 174 std::string BuildRequest(); |
| 171 }; | 175 }; |
| 172 | 176 |
| 173 void FetchSnippetsImpl(const GURL& url, | 177 void FetchSnippetsImpl(const GURL& url, |
| 174 const std::string& auth_header, | 178 const std::string& auth_header, |
| 175 const std::string& request); | 179 const std::string& request); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 192 void OnURLFetchComplete(const net::URLFetcher* source) override; | 196 void OnURLFetchComplete(const net::URLFetcher* source) override; |
| 193 | 197 |
| 194 bool JsonToSnippets(const base::Value& parsed, | 198 bool JsonToSnippets(const base::Value& parsed, |
| 195 FetchedCategoriesVector* categories); | 199 FetchedCategoriesVector* categories); |
| 196 void OnJsonParsed(std::unique_ptr<base::Value> parsed); | 200 void OnJsonParsed(std::unique_ptr<base::Value> parsed); |
| 197 void OnJsonError(const std::string& error); | 201 void OnJsonError(const std::string& error); |
| 198 void FetchFinished(OptionalFetchedCategories fetched_categories, | 202 void FetchFinished(OptionalFetchedCategories fetched_categories, |
| 199 FetchResult result, | 203 FetchResult result, |
| 200 const std::string& extra_message); | 204 const std::string& extra_message); |
| 201 | 205 |
| 206 bool DemandQuotaForRequest(bool interactive_request); |
| 207 |
| 202 // Authorization for signed-in users. | 208 // Authorization for signed-in users. |
| 203 SigninManagerBase* signin_manager_; | 209 SigninManagerBase* signin_manager_; |
| 204 OAuth2TokenService* token_service_; | 210 OAuth2TokenService* token_service_; |
| 205 std::unique_ptr<OAuth2TokenService::Request> oauth_request_; | 211 std::unique_ptr<OAuth2TokenService::Request> oauth_request_; |
| 206 bool waiting_for_refresh_token_; | 212 bool waiting_for_refresh_token_; |
| 207 | 213 |
| 208 // Holds the URL request context. | 214 // Holds the URL request context. |
| 209 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; | 215 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
| 210 | 216 |
| 211 CategoryFactory* const category_factory_; | 217 CategoryFactory* const category_factory_; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 242 | 248 |
| 243 // The variant of the fetching to use, loaded from variation parameters. | 249 // The variant of the fetching to use, loaded from variation parameters. |
| 244 Personalization personalization_; | 250 Personalization personalization_; |
| 245 | 251 |
| 246 // Is the request user initiated? | 252 // Is the request user initiated? |
| 247 bool interactive_request_; | 253 bool interactive_request_; |
| 248 | 254 |
| 249 // Allow for an injectable tick clock for testing. | 255 // Allow for an injectable tick clock for testing. |
| 250 std::unique_ptr<base::TickClock> tick_clock_; | 256 std::unique_ptr<base::TickClock> tick_clock_; |
| 251 | 257 |
| 252 // Request throttler for limiting requests. | 258 // Classifier that tells us how active the user is. Not owned. |
| 253 RequestThrottler request_throttler_; | 259 const UserClassifier* user_classifier_; |
| 260 |
| 261 // Request throttlers for limiting requests for different classes of users. |
| 262 RequestThrottler request_throttler_rare_ntp_user_; |
| 263 RequestThrottler request_throttler_active_ntp_user_; |
| 264 RequestThrottler request_throttler_active_suggestions_consumer_; |
| 254 | 265 |
| 255 // When a token request gets canceled, we want to retry once. | 266 // When a token request gets canceled, we want to retry once. |
| 256 bool oauth_token_retried_; | 267 bool oauth_token_retried_; |
| 257 | 268 |
| 258 base::WeakPtrFactory<NTPSnippetsFetcher> weak_ptr_factory_; | 269 base::WeakPtrFactory<NTPSnippetsFetcher> weak_ptr_factory_; |
| 259 | 270 |
| 260 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcher); | 271 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcher); |
| 261 }; | 272 }; |
| 262 } // namespace ntp_snippets | 273 } // namespace ntp_snippets |
| 263 | 274 |
| 264 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPETS_FETCHER_H_ | 275 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPETS_FETCHER_H_ |
| OLD | NEW |