| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef COMPONENTS_NTP_SNIPPETS_BREAKING_NEWS_SUBSCRIPTION_JSON_REQUEST_H_ |
| 6 #define COMPONENTS_NTP_SNIPPETS_BREAKING_NEWS_SUBSCRIPTION_JSON_REQUEST_H_ |
| 7 |
| 8 #include <memory> |
| 9 #include <string> |
| 10 #include <utility> |
| 11 |
| 12 #include "base/callback.h" |
| 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/optional.h" |
| 15 #include "base/time/time.h" |
| 16 #include "components/ntp_snippets/breaking_news/subscription_request_params.h" |
| 17 #include "components/ntp_snippets/status.h" |
| 18 #include "components/translate/core/browser/language_model.h" |
| 19 #include "google_apis/gaia/oauth2_token_service.h" |
| 20 #include "net/http/http_request_headers.h" |
| 21 |
| 22 namespace base { |
| 23 class Value; |
| 24 } // namespace base |
| 25 |
| 26 namespace ntp_snippets { |
| 27 |
| 28 namespace internal { |
| 29 |
| 30 // A single request to subscribe for breaking news via GCM, |
| 31 class SubscriptionJsonRequest : public net::URLFetcherDelegate { |
| 32 public: |
| 33 // A client can expect error_details only, if there was any error during the |
| 34 // subscription or parsing. In successful cases, it will be an empty string. |
| 35 using CompletedCallback = |
| 36 base::OnceCallback<void(std::unique_ptr<base::Value> result, |
| 37 const std::string& error_details)>; |
| 38 |
| 39 // Builds authenticated and non-authenticated SubscriptionJsonRequests. |
| 40 class Builder { |
| 41 public: |
| 42 Builder(); |
| 43 Builder(Builder&&); |
| 44 ~Builder(); |
| 45 |
| 46 // Builds a Request object that contains all data to fetch new snippets. |
| 47 std::unique_ptr<SubscriptionJsonRequest> Build() const; |
| 48 |
| 49 Builder& SetParams(const SubscriptionRequestParams& params); |
| 50 Builder& SetUrl(const GURL& url); |
| 51 Builder& SetUrlRequestContextGetter( |
| 52 const scoped_refptr<net::URLRequestContextGetter>& context_getter); |
| 53 Builder& SetParseJsonCallback(ParseJSONCallback callback); |
| 54 |
| 55 // These preview methods allow to inspect the Request without exposing it |
| 56 // publicly. |
| 57 std::string PreviewRequestBodyForTesting() { return BuildBody(); } |
| 58 std::string PreviewRequestHeadersForTesting() { return BuildHeaders(); } |
| 59 |
| 60 private: |
| 61 std::string BuildHeaders() const; |
| 62 std::string BuildBody() const; |
| 63 std::unique_ptr<net::URLFetcher> BuildURLFetcher( |
| 64 net::URLFetcherDelegate* request, |
| 65 const std::string& headers, |
| 66 const std::string& body) const; |
| 67 |
| 68 // Only required, if the request needs to be sent. |
| 69 std::string auth_header_; |
| 70 SubscriptionRequestParams params_; |
| 71 ParseJSONCallback parse_json_callback_; |
| 72 GURL url_; |
| 73 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
| 74 |
| 75 // Optional properties. |
| 76 std::string obfuscated_gaia_id_; |
| 77 |
| 78 DISALLOW_COPY_AND_ASSIGN(Builder); |
| 79 }; |
| 80 |
| 81 SubscriptionJsonRequest(SubscriptionJsonRequest&&); |
| 82 ~SubscriptionJsonRequest() override; |
| 83 |
| 84 void Start(CompletedCallback callback); |
| 85 |
| 86 std::string GetResponseString() const; |
| 87 |
| 88 private: |
| 89 friend class Builder; |
| 90 SubscriptionJsonRequest(const ParseJSONCallback& callback); |
| 91 // URLFetcherDelegate implementation. |
| 92 void OnURLFetchComplete(const net::URLFetcher* source) override; |
| 93 |
| 94 // The fetcher for subscribing. Only non-null if a subscription process is |
| 95 // currently ongoing. |
| 96 std::unique_ptr<net::URLFetcher> url_fetcher_; |
| 97 |
| 98 // This callback is called to parse a json string. It contains callbacks for |
| 99 // error and success cases. |
| 100 ParseJSONCallback parse_json_callback_; |
| 101 |
| 102 // The callback to notify when URLFetcher finished and results are available. |
| 103 CompletedCallback request_completed_callback_; |
| 104 |
| 105 base::WeakPtrFactory<SubscriptionJsonRequest> weak_ptr_factory_; |
| 106 |
| 107 DISALLOW_COPY_AND_ASSIGN(SubscriptionJsonRequest); |
| 108 }; |
| 109 |
| 110 } // namespace internal |
| 111 |
| 112 } // namespace ntp_snippets |
| 113 |
| 114 #endif // COMPONENTS_NTP_SNIPPETS_BREAKING_NEWS_SUBSCRIPTION_JSON_REQUEST_H_ |
| OLD | NEW |