| 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 15 matching lines...) Expand all Loading... |
| 26 class PrefService; | 26 class PrefService; |
| 27 class SigninManagerBase; | 27 class SigninManagerBase; |
| 28 | 28 |
| 29 namespace base { | 29 namespace base { |
| 30 class ListValue; | 30 class ListValue; |
| 31 class Value; | 31 class Value; |
| 32 } // namespace base | 32 } // namespace base |
| 33 | 33 |
| 34 namespace ntp_snippets { | 34 namespace ntp_snippets { |
| 35 | 35 |
| 36 class UserClassifier; |
| 37 |
| 36 // Fetches snippet data for the NTP from the server. | 38 // Fetches snippet data for the NTP from the server. |
| 37 class NTPSnippetsFetcher : public OAuth2TokenService::Consumer, | 39 class NTPSnippetsFetcher : public OAuth2TokenService::Consumer, |
| 38 public OAuth2TokenService::Observer, | 40 public OAuth2TokenService::Observer, |
| 39 public net::URLFetcherDelegate { | 41 public net::URLFetcherDelegate { |
| 40 public: | 42 public: |
| 41 // Callbacks for JSON parsing, needed because the parsing is platform- | 43 // Callbacks for JSON parsing, needed because the parsing is platform- |
| 42 // dependent. | 44 // dependent. |
| 43 using SuccessCallback = base::Callback<void(std::unique_ptr<base::Value>)>; | 45 using SuccessCallback = base::Callback<void(std::unique_ptr<base::Value>)>; |
| 44 using ErrorCallback = base::Callback<void(const std::string&)>; | 46 using ErrorCallback = base::Callback<void(const std::string&)>; |
| 45 using ParseJSONCallback = base::Callback< | 47 using ParseJSONCallback = base::Callback< |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 }; | 90 }; |
| 89 | 91 |
| 90 NTPSnippetsFetcher( | 92 NTPSnippetsFetcher( |
| 91 SigninManagerBase* signin_manager, | 93 SigninManagerBase* signin_manager, |
| 92 OAuth2TokenService* token_service, | 94 OAuth2TokenService* token_service, |
| 93 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, | 95 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, |
| 94 PrefService* pref_service, | 96 PrefService* pref_service, |
| 95 CategoryFactory* category_factory, | 97 CategoryFactory* category_factory, |
| 96 translate::LanguageModel* language_model, | 98 translate::LanguageModel* language_model, |
| 97 const ParseJSONCallback& parse_json_callback, | 99 const ParseJSONCallback& parse_json_callback, |
| 98 const std::string& api_key); | 100 const std::string& api_key, |
| 101 const UserClassifier* user_classifier); |
| 99 ~NTPSnippetsFetcher() override; | 102 ~NTPSnippetsFetcher() override; |
| 100 | 103 |
| 101 // Set a callback that is called when a new set of snippets are downloaded, | 104 // Set a callback that is called when a new set of snippets are downloaded, |
| 102 // overriding any previously set callback. | 105 // overriding any previously set callback. |
| 103 void SetCallback(const SnippetsAvailableCallback& callback); | 106 void SetCallback(const SnippetsAvailableCallback& callback); |
| 104 | 107 |
| 105 // Fetches snippets from the server. |hosts| restricts the results to a set of | 108 // Fetches snippets from the server. |hosts| restricts the results to a set of |
| 106 // hosts, e.g. "www.google.com". If |hosts| is empty, no host restrictions are | 109 // hosts, e.g. "www.google.com". If |hosts| is empty, no host restrictions are |
| 107 // applied. | 110 // applied. |
| 108 // | 111 // |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 } | 147 } |
| 145 | 148 |
| 146 void SetPersonalizationForTesting(Personalization personalization) { | 149 void SetPersonalizationForTesting(Personalization personalization) { |
| 147 personalization_ = personalization; | 150 personalization_ = personalization; |
| 148 } | 151 } |
| 149 | 152 |
| 150 private: | 153 private: |
| 151 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsFetcherTest, BuildRequestAuthenticated); | 154 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsFetcherTest, BuildRequestAuthenticated); |
| 152 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsFetcherTest, BuildRequestUnauthenticated); | 155 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsFetcherTest, BuildRequestUnauthenticated); |
| 153 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsFetcherTest, BuildRequestExcludedIds); | 156 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsFetcherTest, BuildRequestExcludedIds); |
| 157 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsFetcherTest, BuildRequestNoUserClass); |
| 154 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsFetcherTest, | 158 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsFetcherTest, |
| 155 BuildRequestWithTwoLanguages); | 159 BuildRequestWithTwoLanguages); |
| 156 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsFetcherTest, | 160 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsFetcherTest, |
| 157 BuildRequestWithUILanguageOnly); | 161 BuildRequestWithUILanguageOnly); |
| 158 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsFetcherTest, | 162 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsFetcherTest, |
| 159 BuildRequestWithOtherLanguageOnly); | 163 BuildRequestWithOtherLanguageOnly); |
| 160 | 164 |
| 161 enum FetchAPI { | 165 enum FetchAPI { |
| 162 CHROME_READER_API, | 166 CHROME_READER_API, |
| 163 CHROME_CONTENT_SUGGESTIONS_API, | 167 CHROME_CONTENT_SUGGESTIONS_API, |
| 164 }; | 168 }; |
| 165 | 169 |
| 166 struct RequestParams { | 170 struct RequestParams { |
| 167 FetchAPI fetch_api; | 171 FetchAPI fetch_api; |
| 168 std::string obfuscated_gaia_id; | 172 std::string obfuscated_gaia_id; |
| 169 bool only_return_personalized_results; | 173 bool only_return_personalized_results; |
| 170 std::string user_locale; | 174 std::string user_locale; |
| 171 std::set<std::string> host_restricts; | 175 std::set<std::string> host_restricts; |
| 172 std::set<std::string> excluded_ids; | 176 std::set<std::string> excluded_ids; |
| 173 int count_to_fetch; | 177 int count_to_fetch; |
| 174 bool interactive_request; | 178 bool interactive_request; |
| 179 std::string user_class; |
| 175 translate::LanguageModel::LanguageInfo ui_language; | 180 translate::LanguageModel::LanguageInfo ui_language; |
| 176 translate::LanguageModel::LanguageInfo other_top_language; | 181 translate::LanguageModel::LanguageInfo other_top_language; |
| 177 | 182 |
| 178 RequestParams(); | 183 RequestParams(); |
| 179 ~RequestParams(); | 184 ~RequestParams(); |
| 180 | 185 |
| 181 std::string BuildRequest(); | 186 std::string BuildRequest(); |
| 182 }; | 187 }; |
| 183 | 188 |
| 184 void FetchSnippetsImpl(const GURL& url, | 189 void FetchSnippetsImpl(const GURL& url, |
| (...skipping 19 matching lines...) Expand all Loading... |
| 204 void OnURLFetchComplete(const net::URLFetcher* source) override; | 209 void OnURLFetchComplete(const net::URLFetcher* source) override; |
| 205 | 210 |
| 206 bool JsonToSnippets(const base::Value& parsed, | 211 bool JsonToSnippets(const base::Value& parsed, |
| 207 FetchedCategoriesVector* categories); | 212 FetchedCategoriesVector* categories); |
| 208 void OnJsonParsed(std::unique_ptr<base::Value> parsed); | 213 void OnJsonParsed(std::unique_ptr<base::Value> parsed); |
| 209 void OnJsonError(const std::string& error); | 214 void OnJsonError(const std::string& error); |
| 210 void FetchFinished(OptionalFetchedCategories fetched_categories, | 215 void FetchFinished(OptionalFetchedCategories fetched_categories, |
| 211 FetchResult result, | 216 FetchResult result, |
| 212 const std::string& extra_message); | 217 const std::string& extra_message); |
| 213 | 218 |
| 219 bool DemandQuotaForRequest(bool interactive_request); |
| 220 |
| 214 // Authorization for signed-in users. | 221 // Authorization for signed-in users. |
| 215 SigninManagerBase* signin_manager_; | 222 SigninManagerBase* signin_manager_; |
| 216 OAuth2TokenService* token_service_; | 223 OAuth2TokenService* token_service_; |
| 217 std::unique_ptr<OAuth2TokenService::Request> oauth_request_; | 224 std::unique_ptr<OAuth2TokenService::Request> oauth_request_; |
| 218 bool waiting_for_refresh_token_; | 225 bool waiting_for_refresh_token_; |
| 219 | 226 |
| 220 // Holds the URL request context. | 227 // Holds the URL request context. |
| 221 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; | 228 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
| 222 | 229 |
| 223 // Weak references, not owned. | 230 // Weak references, not owned. |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 257 | 264 |
| 258 // The variant of the fetching to use, loaded from variation parameters. | 265 // The variant of the fetching to use, loaded from variation parameters. |
| 259 Personalization personalization_; | 266 Personalization personalization_; |
| 260 | 267 |
| 261 // Is the request user initiated? | 268 // Is the request user initiated? |
| 262 bool interactive_request_; | 269 bool interactive_request_; |
| 263 | 270 |
| 264 // Allow for an injectable tick clock for testing. | 271 // Allow for an injectable tick clock for testing. |
| 265 std::unique_ptr<base::TickClock> tick_clock_; | 272 std::unique_ptr<base::TickClock> tick_clock_; |
| 266 | 273 |
| 267 // Request throttler for limiting requests. | 274 // Classifier that tells us how active the user is. Not owned. |
| 268 RequestThrottler request_throttler_; | 275 const UserClassifier* user_classifier_; |
| 276 |
| 277 // Request throttlers for limiting requests for different classes of users. |
| 278 RequestThrottler request_throttler_rare_ntp_user_; |
| 279 RequestThrottler request_throttler_active_ntp_user_; |
| 280 RequestThrottler request_throttler_active_suggestions_consumer_; |
| 269 | 281 |
| 270 // When a token request gets canceled, we want to retry once. | 282 // When a token request gets canceled, we want to retry once. |
| 271 bool oauth_token_retried_; | 283 bool oauth_token_retried_; |
| 272 | 284 |
| 273 base::WeakPtrFactory<NTPSnippetsFetcher> weak_ptr_factory_; | 285 base::WeakPtrFactory<NTPSnippetsFetcher> weak_ptr_factory_; |
| 274 | 286 |
| 275 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcher); | 287 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcher); |
| 276 }; | 288 }; |
| 277 } // namespace ntp_snippets | 289 } // namespace ntp_snippets |
| 278 | 290 |
| 279 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPETS_FETCHER_H_ | 291 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPETS_FETCHER_H_ |
| OLD | NEW |