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/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, | |
|
fhorschig
2017/05/31 14:51:13
s/,/.
mamir
2017/05/31 18:46:02
Done.
| |
| 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)>; | |
|
fhorschig
2017/05/31 14:51:13
How are error details used?
Could we handle errors
mamir
2017/05/31 18:46:02
Done.
| |
| 38 | |
| 39 // Builds authenticated and non-authenticated SubscriptionJsonRequests. | |
| 40 class Builder { | |
| 41 public: | |
| 42 Builder(); | |
| 43 Builder(Builder&&); | |
|
fhorschig
2017/05/31 14:51:13
Please check if the declaration of this move const
mamir
2017/05/31 18:46:02
Needed in the test IIUC.
| |
| 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); | |
|
fhorschig
2017/05/31 14:51:13
Drop if you don't need to parse JSON.
mamir
2017/05/31 18:46:02
Done.
| |
| 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(const ParseJSONCallback& callback); | |
|
fhorschig
2017/05/31 14:51:13
As discussed, I guess this constructor should be p
mamir
2017/05/31 18:46:02
Done.
| |
| 82 SubscriptionJsonRequest(SubscriptionJsonRequest&&); | |
|
fhorschig
2017/05/31 14:51:13
Do you need this? (it's =default in cpp ... please
mamir
2017/05/31 18:46:02
Removed!
| |
| 83 ~SubscriptionJsonRequest() override; | |
| 84 | |
| 85 void Start(CompletedCallback callback); | |
| 86 | |
| 87 std::string GetResponseString() const; | |
|
fhorschig
2017/05/31 14:51:13
Will you need that?
Usually all the status you nee
mamir
2017/05/31 18:46:02
Removed!
| |
| 88 | |
| 89 private: | |
| 90 // URLFetcherDelegate implementation. | |
| 91 void OnURLFetchComplete(const net::URLFetcher* source) override; | |
| 92 | |
| 93 // The fetcher for subscrbing. Only non-null if a subscription process is | |
| 94 // currently ongoing. | |
| 95 std::unique_ptr<net::URLFetcher> url_fetcher_; | |
| 96 | |
| 97 // This callback is called to parse a json string. It contains callbacks for | |
| 98 // error and success cases. | |
| 99 ParseJSONCallback parse_json_callback_; | |
|
fhorschig
2017/05/31 14:51:13
Drop if you don't need to parse JSON.
mamir
2017/05/31 18:46:03
Done.
| |
| 100 | |
| 101 // The callback to notify when URLFetcher finished and results are available. | |
| 102 CompletedCallback request_completed_callback_; | |
| 103 | |
| 104 base::WeakPtrFactory<SubscriptionJsonRequest> weak_ptr_factory_; | |
| 105 | |
| 106 DISALLOW_COPY_AND_ASSIGN(SubscriptionJsonRequest); | |
| 107 }; | |
| 108 | |
| 109 } // namespace internal | |
| 110 | |
| 111 } // namespace ntp_snippets | |
| 112 | |
| 113 #endif // COMPONENTS_NTP_SNIPPETS_BREAKING_NEWS_SUBSCRIPTION_JSON_REQUEST_H_ | |
| OLD | NEW |