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_NTP_SNIPPETS_FETCHER_H_ | 5 #ifndef COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_FETCHER_H_ |
6 #define COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_FETCHER_H_ | 6 #define COMPONENTS_NTP_SNIPPETS_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 29 matching lines...) Expand all Loading... |
40 public OAuth2TokenService::Observer, | 40 public OAuth2TokenService::Observer, |
41 public net::URLFetcherDelegate { | 41 public net::URLFetcherDelegate { |
42 public: | 42 public: |
43 // Callbacks for JSON parsing, needed because the parsing is platform- | 43 // Callbacks for JSON parsing, needed because the parsing is platform- |
44 // dependent. | 44 // dependent. |
45 using SuccessCallback = base::Callback<void(std::unique_ptr<base::Value>)>; | 45 using SuccessCallback = base::Callback<void(std::unique_ptr<base::Value>)>; |
46 using ErrorCallback = base::Callback<void(const std::string&)>; | 46 using ErrorCallback = base::Callback<void(const std::string&)>; |
47 using ParseJSONCallback = base::Callback< | 47 using ParseJSONCallback = base::Callback< |
48 void(const std::string&, const SuccessCallback&, const ErrorCallback&)>; | 48 void(const std::string&, const SuccessCallback&, const ErrorCallback&)>; |
49 | 49 |
50 using OptionalSnippets = base::Optional<NTPSnippet::CategoryMap>; | 50 struct FetchedCategory { |
| 51 Category category; |
| 52 base::string16 localized_title; // Ignored for non-server categories. |
| 53 NTPSnippet::PtrVector snippets; |
| 54 |
| 55 FetchedCategory(Category c); |
| 56 FetchedCategory(FetchedCategory&&); // = default, in .cc |
| 57 ~FetchedCategory(); // = default, in .cc |
| 58 FetchedCategory& operator=(FetchedCategory&&); // = default, in .cc |
| 59 }; |
| 60 using FetchedCategoriesVector = std::vector<FetchedCategory>; |
| 61 using OptionalSnippets = base::Optional<FetchedCategoriesVector>; |
| 62 |
51 // |snippets| contains parsed snippets if a fetch succeeded. If problems | 63 // |snippets| contains parsed snippets if a fetch succeeded. If problems |
52 // occur, |snippets| contains no value (no actual vector in base::Optional). | 64 // occur, |snippets| contains no value (no actual vector in base::Optional). |
53 // Error details can be retrieved using last_status(). | 65 // Error details can be retrieved using last_status(). |
54 using SnippetsAvailableCallback = | 66 using SnippetsAvailableCallback = |
55 base::Callback<void(OptionalSnippets snippets)>; | 67 base::Callback<void(OptionalSnippets snippets)>; |
56 | 68 |
57 // Enumeration listing all possible outcomes for fetch attempts. Used for UMA | 69 // Enumeration listing all possible outcomes for fetch attempts. Used for UMA |
58 // histograms, so do not change existing values. Insert new values at the end, | 70 // histograms, so do not change existing values. Insert new values at the end, |
59 // and update the histogram definition. | 71 // and update the histogram definition. |
60 enum class FetchResult { | 72 enum class FetchResult { |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 void OnGetTokenFailure(const OAuth2TokenService::Request* request, | 188 void OnGetTokenFailure(const OAuth2TokenService::Request* request, |
177 const GoogleServiceAuthError& error) override; | 189 const GoogleServiceAuthError& error) override; |
178 | 190 |
179 // OAuth2TokenService::Observer overrides: | 191 // OAuth2TokenService::Observer overrides: |
180 void OnRefreshTokenAvailable(const std::string& account_id) override; | 192 void OnRefreshTokenAvailable(const std::string& account_id) override; |
181 | 193 |
182 // URLFetcherDelegate implementation. | 194 // URLFetcherDelegate implementation. |
183 void OnURLFetchComplete(const net::URLFetcher* source) override; | 195 void OnURLFetchComplete(const net::URLFetcher* source) override; |
184 | 196 |
185 bool JsonToSnippets(const base::Value& parsed, | 197 bool JsonToSnippets(const base::Value& parsed, |
186 NTPSnippet::CategoryMap* snippets); | 198 FetchedCategoriesVector* categories); |
187 void OnJsonParsed(std::unique_ptr<base::Value> parsed); | 199 void OnJsonParsed(std::unique_ptr<base::Value> parsed); |
188 void OnJsonError(const std::string& error); | 200 void OnJsonError(const std::string& error); |
189 void FetchFinished(OptionalSnippets snippets, | 201 void FetchFinished(OptionalSnippets snippets, |
190 FetchResult result, | 202 FetchResult result, |
191 const std::string& extra_message); | 203 const std::string& extra_message); |
192 | 204 |
193 // Authorization for signed-in users. | 205 // Authorization for signed-in users. |
194 SigninManagerBase* signin_manager_; | 206 SigninManagerBase* signin_manager_; |
195 OAuth2TokenService* token_service_; | 207 OAuth2TokenService* token_service_; |
196 std::unique_ptr<OAuth2TokenService::Request> oauth_request_; | 208 std::unique_ptr<OAuth2TokenService::Request> oauth_request_; |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 // When a token request gets canceled, we want to retry once. | 257 // When a token request gets canceled, we want to retry once. |
246 bool oauth_token_retried_; | 258 bool oauth_token_retried_; |
247 | 259 |
248 base::WeakPtrFactory<NTPSnippetsFetcher> weak_ptr_factory_; | 260 base::WeakPtrFactory<NTPSnippetsFetcher> weak_ptr_factory_; |
249 | 261 |
250 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcher); | 262 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcher); |
251 }; | 263 }; |
252 } // namespace ntp_snippets | 264 } // namespace ntp_snippets |
253 | 265 |
254 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_FETCHER_H_ | 266 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_FETCHER_H_ |
OLD | NEW |