| 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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 ~FetchedCategory(); // = default, in .cc | 58 ~FetchedCategory(); // = default, in .cc |
| 59 FetchedCategory& operator=(FetchedCategory&&); // = default, in .cc | 59 FetchedCategory& operator=(FetchedCategory&&); // = default, in .cc |
| 60 }; | 60 }; |
| 61 using FetchedCategoriesVector = std::vector<FetchedCategory>; | 61 using FetchedCategoriesVector = std::vector<FetchedCategory>; |
| 62 using OptionalFetchedCategories = base::Optional<FetchedCategoriesVector>; | 62 using OptionalFetchedCategories = base::Optional<FetchedCategoriesVector>; |
| 63 | 63 |
| 64 // |snippets| contains parsed snippets if a fetch succeeded. If problems | 64 // |snippets| contains parsed snippets if a fetch succeeded. If problems |
| 65 // occur, |snippets| contains no value (no actual vector in base::Optional). | 65 // occur, |snippets| contains no value (no actual vector in base::Optional). |
| 66 // Error details can be retrieved using last_status(). | 66 // Error details can be retrieved using last_status(). |
| 67 using SnippetsAvailableCallback = | 67 using SnippetsAvailableCallback = |
| 68 base::Callback<void(OptionalFetchedCategories fetched_categories)>; | 68 base::OnceCallback<void(OptionalFetchedCategories fetched_categories)>; |
| 69 | 69 |
| 70 // Enumeration listing all possible outcomes for fetch attempts. Used for UMA | 70 // Enumeration listing all possible outcomes for fetch attempts. Used for UMA |
| 71 // histograms, so do not change existing values. Insert new values at the end, | 71 // histograms, so do not change existing values. Insert new values at the end, |
| 72 // and update the histogram definition. | 72 // and update the histogram definition. |
| 73 enum class FetchResult { | 73 enum class FetchResult { |
| 74 SUCCESS, | 74 SUCCESS, |
| 75 DEPRECATED_EMPTY_HOSTS, | 75 DEPRECATED_EMPTY_HOSTS, |
| 76 URL_REQUEST_STATUS_ERROR, | 76 URL_REQUEST_STATUS_ERROR, |
| 77 HTTP_ERROR, | 77 HTTP_ERROR, |
| 78 JSON_PARSE_ERROR, | 78 JSON_PARSE_ERROR, |
| (...skipping 26 matching lines...) Expand all Loading... |
| 105 // A set of suggestion IDs that should not be returned again. | 105 // A set of suggestion IDs that should not be returned again. |
| 106 std::set<std::string> excluded_ids; | 106 std::set<std::string> excluded_ids; |
| 107 | 107 |
| 108 // Maximum number of snippets to fetch. | 108 // Maximum number of snippets to fetch. |
| 109 int count_to_fetch = 0; | 109 int count_to_fetch = 0; |
| 110 | 110 |
| 111 // Whether this is an interactive request, i.e. triggered by an explicit | 111 // Whether this is an interactive request, i.e. triggered by an explicit |
| 112 // user action. Typically, non-interactive requests are subject to a daily | 112 // user action. Typically, non-interactive requests are subject to a daily |
| 113 // quota. | 113 // quota. |
| 114 bool interactive_request = false; | 114 bool interactive_request = false; |
| 115 |
| 116 // If set, only return results for this category. |
| 117 base::Optional<Category> exclusive_category; |
| 115 }; | 118 }; |
| 116 | 119 |
| 117 NTPSnippetsFetcher( | 120 NTPSnippetsFetcher( |
| 118 SigninManagerBase* signin_manager, | 121 SigninManagerBase* signin_manager, |
| 119 OAuth2TokenService* token_service, | 122 OAuth2TokenService* token_service, |
| 120 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, | 123 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, |
| 121 PrefService* pref_service, | 124 PrefService* pref_service, |
| 122 CategoryFactory* category_factory, | 125 CategoryFactory* category_factory, |
| 123 translate::LanguageModel* language_model, | 126 translate::LanguageModel* language_model, |
| 124 const ParseJSONCallback& parse_json_callback, | 127 const ParseJSONCallback& parse_json_callback, |
| 125 const std::string& api_key, | 128 const std::string& api_key, |
| 126 const UserClassifier* user_classifier); | 129 const UserClassifier* user_classifier); |
| 127 ~NTPSnippetsFetcher() override; | 130 ~NTPSnippetsFetcher() override; |
| 128 | 131 |
| 129 // Set a callback that is called when a new set of snippets are downloaded, | |
| 130 // overriding any previously set callback. | |
| 131 void SetCallback(const SnippetsAvailableCallback& callback); | |
| 132 | |
| 133 // Initiates a fetch from the server. When done (successfully or not), calls | 132 // Initiates a fetch from the server. When done (successfully or not), calls |
| 134 // the subscriber of SetCallback(). | 133 // the subscriber of SetCallback(). |
| 135 // | 134 // |
| 136 // If an ongoing fetch exists, it will be silently abandoned and a new one | 135 // If an ongoing fetch exists, it will be silently abandoned and a new one |
| 137 // started, without triggering an additional callback (i.e. the callback will | 136 // started, without triggering an additional callback (i.e. the callback will |
| 138 // only be called once). | 137 // only be called once). |
| 139 void FetchSnippets(const Params& params); | 138 void FetchSnippets(const Params& params, SnippetsAvailableCallback callback); |
| 140 | 139 |
| 141 // Debug string representing the status/result of the last fetch attempt. | 140 // Debug string representing the status/result of the last fetch attempt. |
| 142 const std::string& last_status() const { return last_status_; } | 141 const std::string& last_status() const { return last_status_; } |
| 143 | 142 |
| 144 // Returns the last JSON fetched from the server. | 143 // Returns the last JSON fetched from the server. |
| 145 const std::string& last_json() const { | 144 const std::string& last_json() const { |
| 146 return last_fetch_json_; | 145 return last_fetch_json_; |
| 147 } | 146 } |
| 148 | 147 |
| 149 // Returns the personalization setting of the fetcher. | 148 // Returns the personalization setting of the fetcher. |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 // URLFetcherDelegate implementation. | 219 // URLFetcherDelegate implementation. |
| 221 void OnURLFetchComplete(const net::URLFetcher* source) override; | 220 void OnURLFetchComplete(const net::URLFetcher* source) override; |
| 222 | 221 |
| 223 bool JsonToSnippets(const base::Value& parsed, | 222 bool JsonToSnippets(const base::Value& parsed, |
| 224 FetchedCategoriesVector* categories); | 223 FetchedCategoriesVector* categories); |
| 225 void OnJsonParsed(std::unique_ptr<base::Value> parsed); | 224 void OnJsonParsed(std::unique_ptr<base::Value> parsed); |
| 226 void OnJsonError(const std::string& error); | 225 void OnJsonError(const std::string& error); |
| 227 void FetchFinished(OptionalFetchedCategories fetched_categories, | 226 void FetchFinished(OptionalFetchedCategories fetched_categories, |
| 228 FetchResult result, | 227 FetchResult result, |
| 229 const std::string& extra_message); | 228 const std::string& extra_message); |
| 229 void FilterCategories(FetchedCategoriesVector* categories); |
| 230 | 230 |
| 231 bool DemandQuotaForRequest(bool interactive_request); | 231 bool DemandQuotaForRequest(bool interactive_request); |
| 232 | 232 |
| 233 // Authentication for signed-in users. | 233 // Authentication for signed-in users. |
| 234 SigninManagerBase* signin_manager_; | 234 SigninManagerBase* signin_manager_; |
| 235 OAuth2TokenService* token_service_; | 235 OAuth2TokenService* token_service_; |
| 236 std::unique_ptr<OAuth2TokenService::Request> oauth_request_; | 236 std::unique_ptr<OAuth2TokenService::Request> oauth_request_; |
| 237 bool waiting_for_refresh_token_ = false; | 237 bool waiting_for_refresh_token_ = false; |
| 238 | 238 |
| 239 // When a token request gets canceled, we want to retry once. | 239 // When a token request gets canceled, we want to retry once. |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 287 std::string last_status_; | 287 std::string last_status_; |
| 288 std::string last_fetch_json_; | 288 std::string last_fetch_json_; |
| 289 | 289 |
| 290 base::WeakPtrFactory<NTPSnippetsFetcher> weak_ptr_factory_; | 290 base::WeakPtrFactory<NTPSnippetsFetcher> weak_ptr_factory_; |
| 291 | 291 |
| 292 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcher); | 292 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcher); |
| 293 }; | 293 }; |
| 294 } // namespace ntp_snippets | 294 } // namespace ntp_snippets |
| 295 | 295 |
| 296 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPETS_FETCHER_H_ | 296 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPETS_FETCHER_H_ |
| OLD | NEW |