Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(10)

Side by Side Diff: components/ntp_snippets/breaking_news/subscription_json_request.h

Issue 2918513002: [NTP::Push] Add the classes for sending a breaking news subscription request (Closed)
Patch Set: Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 private:
56 std::string BuildHeaders() const;
57 std::string BuildBody() const;
58 std::unique_ptr<net::URLFetcher> BuildURLFetcher(
59 net::URLFetcherDelegate* request,
60 const std::string& headers,
61 const std::string& body) const;
62
63 // Only required, if the request needs to be sent.
64 std::string auth_header_;
65 SubscriptionRequestParams params_;
66 ParseJSONCallback parse_json_callback_;
67 GURL url_;
68 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
69
70 // Optional properties.
71 std::string obfuscated_gaia_id_;
72
73 DISALLOW_COPY_AND_ASSIGN(Builder);
74 };
75
76 SubscriptionJsonRequest(const ParseJSONCallback& callback);
Bernhard Bauer 2017/05/31 13:12:45 How is a client supposed to construct an object of
mamir 2017/05/31 14:25:03 Via the builder. Making the constructor private.
77 SubscriptionJsonRequest(SubscriptionJsonRequest&&);
78 ~SubscriptionJsonRequest() override;
79
80 void Start(CompletedCallback callback);
81
82 std::string GetResponseString() const;
83
84 private:
85 // URLFetcherDelegate implementation.
86 void OnURLFetchComplete(const net::URLFetcher* source) override;
87
88 // The fetcher for subscrbing. Only non-null if a subscription process is
Bernhard Bauer 2017/05/31 13:12:45 "subscribing"
mamir 2017/05/31 14:25:03 Done.
89 // currently ongoing.
90 std::unique_ptr<net::URLFetcher> url_fetcher_;
91
92 // This callback is called to parse a json string. It contains callbacks for
93 // error and success cases.
94 ParseJSONCallback parse_json_callback_;
95
96 // The callback to notify when URLFetcher finished and results are available.
97 CompletedCallback request_completed_callback_;
98
99 base::WeakPtrFactory<SubscriptionJsonRequest> weak_ptr_factory_;
100
101 DISALLOW_COPY_AND_ASSIGN(SubscriptionJsonRequest);
102 };
103
104 } // namespace internal
105
106 } // namespace ntp_snippets
107
108 #endif // COMPONENTS_NTP_SNIPPETS_BREAKING_NEWS_SUBSCRIPTION_JSON_REQUEST_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698