| 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/status.h" |
| 17 #include "components/translate/core/browser/language_model.h" |
| 18 #include "google_apis/gaia/oauth2_token_service.h" |
| 19 #include "net/http/http_request_headers.h" |
| 20 |
| 21 namespace ntp_snippets { |
| 22 |
| 23 namespace internal { |
| 24 |
| 25 // A single request to subscribe for breaking news via GCM. The Request has to |
| 26 // stay alive in order to be successfully completed. |
| 27 class SubscriptionJsonRequest : public net::URLFetcherDelegate { |
| 28 public: |
| 29 // A client can expect a message in the status only, if there was any error |
| 30 // during the subscription. In successful cases, it will be an empty string. |
| 31 using CompletedCallback = |
| 32 base::OnceCallback<void(const ntp_snippets::Status& status)>; |
| 33 |
| 34 // Builds non-authenticated SubscriptionJsonRequests. |
| 35 class Builder { |
| 36 public: |
| 37 Builder(); |
| 38 Builder(Builder&&); |
| 39 ~Builder(); |
| 40 |
| 41 // Builds a Request object that contains all data to fetch new snippets. |
| 42 std::unique_ptr<SubscriptionJsonRequest> Build() const; |
| 43 |
| 44 Builder& SetToken(const std::string& token); |
| 45 Builder& SetUrl(const GURL& url); |
| 46 Builder& SetUrlRequestContextGetter( |
| 47 const scoped_refptr<net::URLRequestContextGetter>& context_getter); |
| 48 |
| 49 private: |
| 50 std::string BuildHeaders() const; |
| 51 std::string BuildBody() const; |
| 52 std::unique_ptr<net::URLFetcher> BuildURLFetcher( |
| 53 net::URLFetcherDelegate* request, |
| 54 const std::string& headers, |
| 55 const std::string& body) const; |
| 56 |
| 57 // GCM subscribtion token obtain from GCM driver (instanceID::getToken()) |
| 58 std::string token_; |
| 59 // TODO(mamir): Additional fields to be added: country, language |
| 60 |
| 61 GURL url_; |
| 62 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
| 63 }; |
| 64 |
| 65 ~SubscriptionJsonRequest() override; |
| 66 |
| 67 // Starts an async request. The callback is invoked when the request succeeds, |
| 68 // fails or gets destroyed. |
| 69 void Start(CompletedCallback callback); |
| 70 |
| 71 private: |
| 72 friend class Builder; |
| 73 SubscriptionJsonRequest(); |
| 74 // URLFetcherDelegate implementation. |
| 75 void OnURLFetchComplete(const net::URLFetcher* source) override; |
| 76 |
| 77 // The fetcher for subscribing. |
| 78 std::unique_ptr<net::URLFetcher> url_fetcher_; |
| 79 |
| 80 // The callback to notify when URLFetcher finished and results are available. |
| 81 // When the request is finished/aborted/destroyed, it's called in the dtor! |
| 82 CompletedCallback request_completed_callback_; |
| 83 |
| 84 DISALLOW_COPY_AND_ASSIGN(SubscriptionJsonRequest); |
| 85 }; |
| 86 |
| 87 } // namespace internal |
| 88 |
| 89 } // namespace ntp_snippets |
| 90 |
| 91 #endif // COMPONENTS_NTP_SNIPPETS_BREAKING_NEWS_SUBSCRIPTION_JSON_REQUEST_H_ |
| OLD | NEW |