Chromium Code Reviews| Index: components/ntp_snippets/breaking_news/subscription_json_request.h |
| diff --git a/components/ntp_snippets/breaking_news/subscription_json_request.h b/components/ntp_snippets/breaking_news/subscription_json_request.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..358a9a4b7be6372f8d4d80f5026b9811aa63b69f |
| --- /dev/null |
| +++ b/components/ntp_snippets/breaking_news/subscription_json_request.h |
| @@ -0,0 +1,92 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef COMPONENTS_NTP_SNIPPETS_BREAKING_NEWS_SUBSCRIPTION_JSON_REQUEST_H_ |
| +#define COMPONENTS_NTP_SNIPPETS_BREAKING_NEWS_SUBSCRIPTION_JSON_REQUEST_H_ |
| + |
| +#include <memory> |
| +#include <string> |
| +#include <utility> |
| + |
| +#include "base/callback.h" |
| +#include "base/memory/weak_ptr.h" |
| +#include "base/optional.h" |
| +#include "base/time/time.h" |
| +#include "components/ntp_snippets/status.h" |
| +#include "components/translate/core/browser/language_model.h" |
| +#include "google_apis/gaia/oauth2_token_service.h" |
| +#include "net/http/http_request_headers.h" |
| + |
| +namespace ntp_snippets { |
| + |
| +namespace internal { |
| + |
| +// A single request to subscribe for breaking news via GCM. |
|
fhorschig
2017/06/01 16:25:21
nit: Please mention that the Request has to stay a
mamir
2017/06/01 17:13:00
Done.
|
| +class SubscriptionJsonRequest : public net::URLFetcherDelegate { |
| + public: |
| + // A client can expect a message in the status only, if there was any error |
| + // during the subscription. In successful cases, it will be an empty string. |
| + using CompletedCallback = |
| + base::OnceCallback<void(const ntp_snippets::Status& status)>; |
| + |
| + // Builds non-authenticated SubscriptionJsonRequests. |
| + class Builder { |
| + public: |
| + Builder(); |
| + Builder(Builder&&); |
| + ~Builder(); |
| + |
| + // Builds a Request object that contains all data to fetch new snippets. |
| + std::unique_ptr<SubscriptionJsonRequest> Build() const; |
| + |
| + Builder& SetToken(const std::string& token); |
| + Builder& SetUrl(const GURL& url); |
| + Builder& SetUrlRequestContextGetter( |
| + const scoped_refptr<net::URLRequestContextGetter>& context_getter); |
| + |
| + private: |
| + std::string BuildHeaders() const; |
| + std::string BuildBody() const; |
| + std::unique_ptr<net::URLFetcher> BuildURLFetcher( |
| + net::URLFetcherDelegate* request, |
| + const std::string& headers, |
| + const std::string& body) const; |
| + |
| + // GCM subscribtion token obtain from GCM driver (instanceID::getToken()) |
| + std::string token_; |
| + // TODO(mamir): Additional fields to be added: country, language |
| + |
| + GURL url_; |
| + scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
| + |
| + // Optional properties. |
| + std::string obfuscated_gaia_id_; |
|
fhorschig
2017/06/01 16:25:21
This property is very optional as it's never used.
mamir
2017/06/01 17:13:00
Done.
|
| + }; |
| + |
| + ~SubscriptionJsonRequest() override; |
| + |
| + void Start(CompletedCallback callback); |
|
fhorschig
2017/06/01 16:25:21
Could you comment this please?
(Something like:
S
mamir
2017/06/01 17:13:00
Done.
|
| + |
| + private: |
| + friend class Builder; |
| + SubscriptionJsonRequest(); |
| + // URLFetcherDelegate implementation. |
| + void OnURLFetchComplete(const net::URLFetcher* source) override; |
| + |
| + // The fetcher for subscribing. |
| + std::unique_ptr<net::URLFetcher> url_fetcher_; |
| + |
| + // The callback to notify when URLFetcher finished and results are available. |
|
fhorschig
2017/06/01 16:25:21
Nit: When the request is finished/aborted/destroye
mamir
2017/06/01 17:13:00
Done.
|
| + CompletedCallback request_completed_callback_; |
| + |
| + base::WeakPtrFactory<SubscriptionJsonRequest> weak_ptr_factory_; |
|
fhorschig
2017/06/01 16:25:21
unused
mamir
2017/06/01 17:13:00
Done.
|
| + |
| + DISALLOW_COPY_AND_ASSIGN(SubscriptionJsonRequest); |
| +}; |
| + |
| +} // namespace internal |
| + |
| +} // namespace ntp_snippets |
| + |
| +#endif // COMPONENTS_NTP_SNIPPETS_BREAKING_NEWS_SUBSCRIPTION_JSON_REQUEST_H_ |