Chromium Code Reviews| 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. | |
| 26 class SubscriptionJsonRequest : public net::URLFetcherDelegate { | |
| 27 public: | |
| 28 // A client can expect error_details only, if there was any error during the | |
|
fhorschig
2017/06/01 09:23:56
please update the comment
mamir
2017/06/01 14:52:32
Done.
| |
| 29 // subscription or parsing. In successful cases, it will be an empty string. | |
| 30 using CompletedCallback = | |
| 31 base::OnceCallback<void(const ntp_snippets::Status& status)>; | |
| 32 | |
| 33 // Builds authenticated and non-authenticated SubscriptionJsonRequests. | |
|
fhorschig
2017/06/01 09:23:56
outdated comment?
mamir
2017/06/01 14:52:31
Acknowledged.
| |
| 34 class Builder { | |
| 35 public: | |
| 36 Builder(); | |
| 37 Builder(Builder&&); | |
| 38 ~Builder(); | |
| 39 | |
| 40 // Builds a Request object that contains all data to fetch new snippets. | |
| 41 std::unique_ptr<SubscriptionJsonRequest> Build() const; | |
| 42 | |
| 43 Builder& SetToken(const std::string& token); | |
| 44 Builder& SetUrl(const GURL& url); | |
| 45 Builder& SetUrlRequestContextGetter( | |
| 46 const scoped_refptr<net::URLRequestContextGetter>& context_getter); | |
| 47 | |
| 48 // These preview methods allow to inspect the Request without exposing it | |
| 49 // publicly. | |
| 50 std::string PreviewRequestBodyForTesting() { return BuildBody(); } | |
| 51 std::string PreviewRequestHeadersForTesting() { return BuildHeaders(); } | |
| 52 | |
| 53 private: | |
| 54 std::string BuildHeaders() const; | |
| 55 std::string BuildBody() const; | |
| 56 std::unique_ptr<net::URLFetcher> BuildURLFetcher( | |
| 57 net::URLFetcherDelegate* request, | |
| 58 const std::string& headers, | |
| 59 const std::string& body) const; | |
| 60 | |
| 61 // GCM subscribtion token obtain from GCM driver (instanceID::getToken()) | |
| 62 std::string token_; | |
| 63 // TODO(mamir): Additional fields to be added: country, language | |
| 64 | |
| 65 GURL url_; | |
| 66 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; | |
| 67 | |
| 68 // Optional properties. | |
| 69 std::string obfuscated_gaia_id_; | |
| 70 }; | |
| 71 | |
| 72 ~SubscriptionJsonRequest() override; | |
| 73 | |
| 74 void Start(CompletedCallback callback); | |
| 75 | |
| 76 private: | |
| 77 friend class Builder; | |
| 78 SubscriptionJsonRequest(); | |
| 79 // URLFetcherDelegate implementation. | |
| 80 void OnURLFetchComplete(const net::URLFetcher* source) override; | |
| 81 | |
| 82 // The fetcher for subscribing. Only non-null if a subscription process is | |
| 83 // currently ongoing. | |
|
fhorschig
2017/06/01 09:23:56
That comment is a lie :D
Your builder always adds
mamir
2017/06/01 14:52:32
Acknowledged.
| |
| 84 std::unique_ptr<net::URLFetcher> url_fetcher_; | |
| 85 | |
| 86 // The callback to notify when URLFetcher finished and results are available. | |
| 87 CompletedCallback request_completed_callback_; | |
| 88 | |
| 89 base::WeakPtrFactory<SubscriptionJsonRequest> weak_ptr_factory_; | |
| 90 | |
| 91 DISALLOW_COPY_AND_ASSIGN(SubscriptionJsonRequest); | |
| 92 }; | |
| 93 | |
| 94 } // namespace internal | |
| 95 | |
| 96 } // namespace ntp_snippets | |
| 97 | |
| 98 #endif // COMPONENTS_NTP_SNIPPETS_BREAKING_NEWS_SUBSCRIPTION_JSON_REQUEST_H_ | |
| OLD | NEW |