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 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 void OnGetTokenFailure(const OAuth2TokenService::Request* request, | 191 void OnGetTokenFailure(const OAuth2TokenService::Request* request, |
180 const GoogleServiceAuthError& error) override; | 192 const GoogleServiceAuthError& error) override; |
181 | 193 |
182 // OAuth2TokenService::Observer overrides: | 194 // OAuth2TokenService::Observer overrides: |
183 void OnRefreshTokenAvailable(const std::string& account_id) override; | 195 void OnRefreshTokenAvailable(const std::string& account_id) override; |
184 | 196 |
185 // URLFetcherDelegate implementation. | 197 // URLFetcherDelegate implementation. |
186 void OnURLFetchComplete(const net::URLFetcher* source) override; | 198 void OnURLFetchComplete(const net::URLFetcher* source) override; |
187 | 199 |
188 bool JsonToSnippets(const base::Value& parsed, | 200 bool JsonToSnippets(const base::Value& parsed, |
189 NTPSnippet::CategoryMap* snippets); | 201 FetchedCategoriesVector* categories); |
190 void OnJsonParsed(std::unique_ptr<base::Value> parsed); | 202 void OnJsonParsed(std::unique_ptr<base::Value> parsed); |
191 void OnJsonError(const std::string& error); | 203 void OnJsonError(const std::string& error); |
192 void FetchFinished(OptionalSnippets snippets, | 204 void FetchFinished(OptionalSnippets snippets, |
193 FetchResult result, | 205 FetchResult result, |
194 const std::string& extra_message); | 206 const std::string& extra_message); |
195 | 207 |
196 // Authorization for signed-in users. | 208 // Authorization for signed-in users. |
197 SigninManagerBase* signin_manager_; | 209 SigninManagerBase* signin_manager_; |
198 OAuth2TokenService* token_service_; | 210 OAuth2TokenService* token_service_; |
199 std::unique_ptr<OAuth2TokenService::Request> oauth_request_; | 211 std::unique_ptr<OAuth2TokenService::Request> oauth_request_; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 // When a token request gets canceled, we want to retry once. | 263 // When a token request gets canceled, we want to retry once. |
252 bool oauth_token_retried_; | 264 bool oauth_token_retried_; |
253 | 265 |
254 base::WeakPtrFactory<NTPSnippetsFetcher> weak_ptr_factory_; | 266 base::WeakPtrFactory<NTPSnippetsFetcher> weak_ptr_factory_; |
255 | 267 |
256 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcher); | 268 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcher); |
257 }; | 269 }; |
258 } // namespace ntp_snippets | 270 } // namespace ntp_snippets |
259 | 271 |
260 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_FETCHER_H_ | 272 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_FETCHER_H_ |
OLD | NEW |