Chromium Code Reviews| 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> |
| 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/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 16 #include "base/optional.h" | 16 #include "base/optional.h" |
| 17 #include "base/time/tick_clock.h" | 17 #include "base/time/tick_clock.h" |
| 18 #include "base/time/time.h" | 18 #include "base/time/time.h" |
| 19 #include "components/ntp_snippets/remote/ntp_snippet.h" | 19 #include "components/ntp_snippets/remote/ntp_snippet.h" |
| 20 #include "components/ntp_snippets/remote/request_throttler.h" | 20 #include "components/ntp_snippets/remote/request_throttler.h" |
| 21 #include "components/ntp_snippets/user_classifier.h" | |
|
Marc Treib
2016/10/06 13:41:55
Not needed I think, the forward decl below should
jkrcal
2016/10/06 14:45:58
Done.
| |
| 21 #include "google_apis/gaia/oauth2_token_service.h" | 22 #include "google_apis/gaia/oauth2_token_service.h" |
| 22 #include "net/url_request/url_fetcher_delegate.h" | 23 #include "net/url_request/url_fetcher_delegate.h" |
| 23 #include "net/url_request/url_request_context_getter.h" | 24 #include "net/url_request/url_request_context_getter.h" |
| 24 | 25 |
| 25 class PrefService; | 26 class PrefService; |
| 26 class SigninManagerBase; | 27 class SigninManagerBase; |
| 27 | 28 |
| 28 namespace base { | 29 namespace base { |
| 29 class Value; | 30 class Value; |
| 30 } // namespace base | 31 } // namespace base |
| 31 | 32 |
| 32 namespace ntp_snippets { | 33 namespace ntp_snippets { |
| 33 | 34 |
| 35 class UserClassifier; | |
| 36 | |
| 34 // Fetches snippet data for the NTP from the server. | 37 // Fetches snippet data for the NTP from the server. |
| 35 class NTPSnippetsFetcher : public OAuth2TokenService::Consumer, | 38 class NTPSnippetsFetcher : public OAuth2TokenService::Consumer, |
| 36 public OAuth2TokenService::Observer, | 39 public OAuth2TokenService::Observer, |
| 37 public net::URLFetcherDelegate { | 40 public net::URLFetcherDelegate { |
| 38 public: | 41 public: |
| 39 // Callbacks for JSON parsing, needed because the parsing is platform- | 42 // Callbacks for JSON parsing, needed because the parsing is platform- |
| 40 // dependent. | 43 // dependent. |
| 41 using SuccessCallback = base::Callback<void(std::unique_ptr<base::Value>)>; | 44 using SuccessCallback = base::Callback<void(std::unique_ptr<base::Value>)>; |
| 42 using ErrorCallback = base::Callback<void(const std::string&)>; | 45 using ErrorCallback = base::Callback<void(const std::string&)>; |
| 43 using ParseJSONCallback = base::Callback< | 46 using ParseJSONCallback = base::Callback< |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 85 kBoth | 88 kBoth |
| 86 }; | 89 }; |
| 87 | 90 |
| 88 NTPSnippetsFetcher( | 91 NTPSnippetsFetcher( |
| 89 SigninManagerBase* signin_manager, | 92 SigninManagerBase* signin_manager, |
| 90 OAuth2TokenService* token_service, | 93 OAuth2TokenService* token_service, |
| 91 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, | 94 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, |
| 92 PrefService* pref_service, | 95 PrefService* pref_service, |
| 93 CategoryFactory* category_factory, | 96 CategoryFactory* category_factory, |
| 94 const ParseJSONCallback& parse_json_callback, | 97 const ParseJSONCallback& parse_json_callback, |
| 95 const std::string& api_key); | 98 const std::string& api_key, |
| 99 const UserClassifier* user_classifier); | |
| 96 ~NTPSnippetsFetcher() override; | 100 ~NTPSnippetsFetcher() override; |
| 97 | 101 |
| 98 // Set a callback that is called when a new set of snippets are downloaded, | 102 // Set a callback that is called when a new set of snippets are downloaded, |
| 99 // overriding any previously set callback. | 103 // overriding any previously set callback. |
| 100 void SetCallback(const SnippetsAvailableCallback& callback); | 104 void SetCallback(const SnippetsAvailableCallback& callback); |
| 101 | 105 |
| 102 // Fetches snippets from the server. |hosts| restricts the results to a set of | 106 // Fetches snippets from the server. |hosts| restricts the results to a set of |
| 103 // hosts, e.g. "www.google.com". If host restrictions are enabled, an empty | 107 // hosts, e.g. "www.google.com". If host restrictions are enabled, an empty |
| 104 // host set produces an error without issuing a fetch. | 108 // host set produces an error without issuing a fetch. |
| 105 // | 109 // |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 158 | 162 |
| 159 struct RequestParams { | 163 struct RequestParams { |
| 160 FetchAPI fetch_api; | 164 FetchAPI fetch_api; |
| 161 std::string obfuscated_gaia_id; | 165 std::string obfuscated_gaia_id; |
| 162 bool only_return_personalized_results; | 166 bool only_return_personalized_results; |
| 163 std::string user_locale; | 167 std::string user_locale; |
| 164 std::set<std::string> host_restricts; | 168 std::set<std::string> host_restricts; |
| 165 std::set<std::string> excluded_ids; | 169 std::set<std::string> excluded_ids; |
| 166 int count_to_fetch; | 170 int count_to_fetch; |
| 167 bool interactive_request; | 171 bool interactive_request; |
| 172 std::string user_class; | |
| 168 | 173 |
| 169 RequestParams(); | 174 RequestParams(); |
| 170 ~RequestParams(); | 175 ~RequestParams(); |
| 171 | 176 |
| 172 std::string BuildRequest(); | 177 std::string BuildRequest(); |
| 173 }; | 178 }; |
| 174 | 179 |
| 175 void FetchSnippetsImpl(const GURL& url, | 180 void FetchSnippetsImpl(const GURL& url, |
| 176 const std::string& auth_header, | 181 const std::string& auth_header, |
| 177 const std::string& request); | 182 const std::string& request); |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 246 Personalization personalization_; | 251 Personalization personalization_; |
| 247 // Should we apply host restriction? It is loaded from variation parameters. | 252 // Should we apply host restriction? It is loaded from variation parameters. |
| 248 bool use_host_restriction_; | 253 bool use_host_restriction_; |
| 249 | 254 |
| 250 // Is the request user initiated? | 255 // Is the request user initiated? |
| 251 bool interactive_request_; | 256 bool interactive_request_; |
| 252 | 257 |
| 253 // Allow for an injectable tick clock for testing. | 258 // Allow for an injectable tick clock for testing. |
| 254 std::unique_ptr<base::TickClock> tick_clock_; | 259 std::unique_ptr<base::TickClock> tick_clock_; |
| 255 | 260 |
| 261 // Classifier that tells us how active the user is. Not owned. | |
| 262 const UserClassifier* user_classifier_; | |
| 263 | |
| 256 // Request throttler for limiting requests. | 264 // Request throttler for limiting requests. |
| 257 RequestThrottler request_throttler_; | 265 RequestThrottler request_throttler_; |
| 258 | 266 |
| 259 // When a token request gets canceled, we want to retry once. | 267 // When a token request gets canceled, we want to retry once. |
| 260 bool oauth_token_retried_; | 268 bool oauth_token_retried_; |
| 261 | 269 |
| 262 base::WeakPtrFactory<NTPSnippetsFetcher> weak_ptr_factory_; | 270 base::WeakPtrFactory<NTPSnippetsFetcher> weak_ptr_factory_; |
| 263 | 271 |
| 264 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcher); | 272 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcher); |
| 265 }; | 273 }; |
| 266 } // namespace ntp_snippets | 274 } // namespace ntp_snippets |
| 267 | 275 |
| 268 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPETS_FETCHER_H_ | 276 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPETS_FETCHER_H_ |
| OLD | NEW |