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 "google_apis/gaia/oauth2_token_service.h" | 21 #include "google_apis/gaia/oauth2_token_service.h" |
| 22 #include "net/url_request/url_fetcher_delegate.h" | 22 #include "net/url_request/url_fetcher_delegate.h" |
| 23 #include "net/url_request/url_request_context_getter.h" | 23 #include "net/url_request/url_request_context_getter.h" |
| 24 | 24 |
| 25 class PrefService; | 25 class PrefService; |
| 26 class SigninManagerBase; | 26 class SigninManagerBase; |
| 27 | 27 |
| 28 namespace base { | 28 namespace base { |
| 29 class ListValue; | |
| 29 class Value; | 30 class Value; |
| 30 } // namespace base | 31 } // namespace base |
| 31 | 32 |
| 33 namespace translate { | |
| 34 class LanguageModel; | |
| 35 } // namespace translate | |
| 36 | |
| 32 namespace ntp_snippets { | 37 namespace ntp_snippets { |
| 33 | 38 |
| 34 // Fetches snippet data for the NTP from the server. | 39 // Fetches snippet data for the NTP from the server. |
| 35 class NTPSnippetsFetcher : public OAuth2TokenService::Consumer, | 40 class NTPSnippetsFetcher : public OAuth2TokenService::Consumer, |
| 36 public OAuth2TokenService::Observer, | 41 public OAuth2TokenService::Observer, |
| 37 public net::URLFetcherDelegate { | 42 public net::URLFetcherDelegate { |
| 38 public: | 43 public: |
| 39 // Callbacks for JSON parsing, needed because the parsing is platform- | 44 // Callbacks for JSON parsing, needed because the parsing is platform- |
| 40 // dependent. | 45 // dependent. |
| 41 using SuccessCallback = base::Callback<void(std::unique_ptr<base::Value>)>; | 46 using SuccessCallback = base::Callback<void(std::unique_ptr<base::Value>)>; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 84 kNonPersonal, | 89 kNonPersonal, |
| 85 kBoth | 90 kBoth |
| 86 }; | 91 }; |
| 87 | 92 |
| 88 NTPSnippetsFetcher( | 93 NTPSnippetsFetcher( |
| 89 SigninManagerBase* signin_manager, | 94 SigninManagerBase* signin_manager, |
| 90 OAuth2TokenService* token_service, | 95 OAuth2TokenService* token_service, |
| 91 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, | 96 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, |
| 92 PrefService* pref_service, | 97 PrefService* pref_service, |
| 93 CategoryFactory* category_factory, | 98 CategoryFactory* category_factory, |
| 99 translate::LanguageModel* language_model, | |
| 94 const ParseJSONCallback& parse_json_callback, | 100 const ParseJSONCallback& parse_json_callback, |
| 95 const std::string& api_key); | 101 const std::string& api_key); |
| 96 ~NTPSnippetsFetcher() override; | 102 ~NTPSnippetsFetcher() override; |
| 97 | 103 |
| 98 // 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, |
| 99 // overriding any previously set callback. | 105 // overriding any previously set callback. |
| 100 void SetCallback(const SnippetsAvailableCallback& callback); | 106 void SetCallback(const SnippetsAvailableCallback& callback); |
| 101 | 107 |
| 102 // 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 |
| 103 // 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 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 141 } | 147 } |
| 142 | 148 |
| 143 void SetPersonalizationForTesting(Personalization personalization) { | 149 void SetPersonalizationForTesting(Personalization personalization) { |
| 144 personalization_ = personalization; | 150 personalization_ = personalization; |
| 145 } | 151 } |
| 146 | 152 |
| 147 private: | 153 private: |
| 148 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsFetcherTest, BuildRequestAuthenticated); | 154 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsFetcherTest, BuildRequestAuthenticated); |
| 149 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsFetcherTest, BuildRequestUnauthenticated); | 155 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsFetcherTest, BuildRequestUnauthenticated); |
| 150 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsFetcherTest, BuildRequestExcludedIds); | 156 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsFetcherTest, BuildRequestExcludedIds); |
| 157 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsFetcherTest, | |
| 158 BuildRequestWithTwoLanguages); | |
| 159 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsFetcherTest, | |
| 160 BuildRequestWithUILanguageOnly); | |
| 161 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsFetcherTest, | |
| 162 BuildRequestWithOtherLanguageOnly); | |
| 151 | 163 |
| 152 enum FetchAPI { | 164 enum FetchAPI { |
| 153 CHROME_READER_API, | 165 CHROME_READER_API, |
| 154 CHROME_CONTENT_SUGGESTIONS_API, | 166 CHROME_CONTENT_SUGGESTIONS_API, |
| 155 }; | 167 }; |
| 156 | 168 |
| 169 struct ContentLanguageInfo { | |
|
Bernhard Bauer
2016/10/07 12:26:13
Why don't we just use LanguageModel::LanguageInfo
jkrcal
2016/10/07 12:58:43
Good point. Done.
| |
| 170 // ISO 639 code. | |
| 171 std::string language_code; | |
| 172 | |
| 173 // The frequency of the given language | |
| 174 float frequency; | |
| 175 | |
| 176 ContentLanguageInfo(); | |
| 177 ~ContentLanguageInfo() = default; | |
| 178 | |
| 179 void AppendToList(base::ListValue* list); | |
| 180 }; | |
| 181 | |
| 157 struct RequestParams { | 182 struct RequestParams { |
| 158 FetchAPI fetch_api; | 183 FetchAPI fetch_api; |
| 159 std::string obfuscated_gaia_id; | 184 std::string obfuscated_gaia_id; |
| 160 bool only_return_personalized_results; | 185 bool only_return_personalized_results; |
| 161 std::string user_locale; | 186 std::string user_locale; |
| 162 std::set<std::string> host_restricts; | 187 std::set<std::string> host_restricts; |
| 163 std::set<std::string> excluded_ids; | 188 std::set<std::string> excluded_ids; |
| 164 int count_to_fetch; | 189 int count_to_fetch; |
| 165 bool interactive_request; | 190 bool interactive_request; |
| 191 ContentLanguageInfo ui_language; | |
| 192 ContentLanguageInfo other_top_language; | |
| 166 | 193 |
| 167 RequestParams(); | 194 RequestParams(); |
| 168 ~RequestParams(); | 195 ~RequestParams(); |
| 169 | 196 |
| 170 std::string BuildRequest(); | 197 std::string BuildRequest(); |
| 171 }; | 198 }; |
| 172 | 199 |
| 173 void FetchSnippetsImpl(const GURL& url, | 200 void FetchSnippetsImpl(const GURL& url, |
| 174 const std::string& auth_header, | 201 const std::string& auth_header, |
| 175 const std::string& request); | 202 const std::string& request); |
| 203 void SetUpCommonFetchingParameters(RequestParams* params) const; | |
| 176 void FetchSnippetsNonAuthenticated(); | 204 void FetchSnippetsNonAuthenticated(); |
| 177 void FetchSnippetsAuthenticated(const std::string& account_id, | 205 void FetchSnippetsAuthenticated(const std::string& account_id, |
| 178 const std::string& oauth_access_token); | 206 const std::string& oauth_access_token); |
| 179 void StartTokenRequest(); | 207 void StartTokenRequest(); |
| 180 | 208 |
| 181 // OAuth2TokenService::Consumer overrides: | 209 // OAuth2TokenService::Consumer overrides: |
| 182 void OnGetTokenSuccess(const OAuth2TokenService::Request* request, | 210 void OnGetTokenSuccess(const OAuth2TokenService::Request* request, |
| 183 const std::string& access_token, | 211 const std::string& access_token, |
| 184 const base::Time& expiration_time) override; | 212 const base::Time& expiration_time) override; |
| 185 void OnGetTokenFailure(const OAuth2TokenService::Request* request, | 213 void OnGetTokenFailure(const OAuth2TokenService::Request* request, |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 201 | 229 |
| 202 // Authorization for signed-in users. | 230 // Authorization for signed-in users. |
| 203 SigninManagerBase* signin_manager_; | 231 SigninManagerBase* signin_manager_; |
| 204 OAuth2TokenService* token_service_; | 232 OAuth2TokenService* token_service_; |
| 205 std::unique_ptr<OAuth2TokenService::Request> oauth_request_; | 233 std::unique_ptr<OAuth2TokenService::Request> oauth_request_; |
| 206 bool waiting_for_refresh_token_; | 234 bool waiting_for_refresh_token_; |
| 207 | 235 |
| 208 // Holds the URL request context. | 236 // Holds the URL request context. |
| 209 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; | 237 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
| 210 | 238 |
| 239 // Weak references, not owned. | |
| 211 CategoryFactory* const category_factory_; | 240 CategoryFactory* const category_factory_; |
| 241 translate::LanguageModel* const language_model_; | |
| 242 | |
| 212 const ParseJSONCallback parse_json_callback_; | 243 const ParseJSONCallback parse_json_callback_; |
| 213 base::TimeTicks fetch_start_time_; | 244 base::TimeTicks fetch_start_time_; |
| 214 std::string last_status_; | 245 std::string last_status_; |
| 215 std::string last_fetch_json_; | 246 std::string last_fetch_json_; |
| 216 | 247 |
| 217 // Hosts to restrict the snippets to. | 248 // Hosts to restrict the snippets to. |
| 218 std::set<std::string> hosts_; | 249 std::set<std::string> hosts_; |
| 219 | 250 |
| 220 // Snippets to exclude from the results. | 251 // Snippets to exclude from the results. |
| 221 std::set<std::string> excluded_ids_; | 252 std::set<std::string> excluded_ids_; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 255 // When a token request gets canceled, we want to retry once. | 286 // When a token request gets canceled, we want to retry once. |
| 256 bool oauth_token_retried_; | 287 bool oauth_token_retried_; |
| 257 | 288 |
| 258 base::WeakPtrFactory<NTPSnippetsFetcher> weak_ptr_factory_; | 289 base::WeakPtrFactory<NTPSnippetsFetcher> weak_ptr_factory_; |
| 259 | 290 |
| 260 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcher); | 291 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcher); |
| 261 }; | 292 }; |
| 262 } // namespace ntp_snippets | 293 } // namespace ntp_snippets |
| 263 | 294 |
| 264 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPETS_FETCHER_H_ | 295 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPETS_FETCHER_H_ |
| OLD | NEW |