Chromium Code Reviews| Index: components/ntp_snippets/breaking_news/subscription_manager.cc |
| diff --git a/components/ntp_snippets/breaking_news/subscription_manager.cc b/components/ntp_snippets/breaking_news/subscription_manager.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..583a19154e1193e48c3c417c9c6db124bab28d69 |
| --- /dev/null |
| +++ b/components/ntp_snippets/breaking_news/subscription_manager.cc |
| @@ -0,0 +1,59 @@ |
| +// 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. |
| + |
| +#include "components/ntp_snippets/breaking_news/subscription_manager.h" |
| +#include "base/bind.h" |
| +#include "components/ntp_snippets/breaking_news/subscription_json_request.h" |
| +#include "components/ntp_snippets/pref_names.h" |
| +#include "components/prefs/pref_service.h" |
| + |
| +namespace ntp_snippets { |
| + |
| +using internal::SubscriptionJsonRequest; |
| + |
| +SubscriptionManager::SubscriptionManager( |
| + scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, |
| + PrefService* pref_service) |
| + : url_request_context_getter_(std::move(url_request_context_getter)), |
| + pref_service_(pref_service) {} |
| + |
| +SubscriptionManager::~SubscriptionManager() = default; |
| + |
| +void SubscriptionManager::Subscribe(const std::string& token) { |
| + DCHECK(!subscription_request_); |
| + subscription_token_ = token; |
| + SubscriptionJsonRequest::Builder builder; |
| + builder.SetToken(token) |
| + .SetUrlRequestContextGetter(url_request_context_getter_) |
| + .SetUrl(subscribe_url_); |
| + |
| + subscription_request_ = builder.Build(); |
| + subscription_request_->Start(base::BindOnce( |
| + &SubscriptionManager::DidSubscribe, base::Unretained(this))); |
| +} |
| + |
| +void SubscriptionManager::DidSubscribe(const ntp_snippets::Status& status) { |
| + subscription_request_.reset(); |
| + |
| + switch (status.code) { |
| + case ntp_snippets::StatusCode::SUCCESS: |
| + // In case of successful subscription, store the same data used for |
| + // subscription in order to be able to re-subscribe in case of data |
| + // change. For now, only the subscription token, but later region and |
| + // language will be stored as well. |
|
fhorschig
2017/06/06 09:00:26
Wouldn't "Store the data for re-subscriptions." su
mamir
2017/06/06 14:05:23
Done.
|
| + // TODO(mamir): store region and language. |
| + pref_service_->SetString( |
| + ntp_snippets::prefs::kContentSuggestionsSubscriptionToken, |
| + subscription_token_); |
| + break; |
| + default: |
| + // TODO(mamir): handle failure. |
| + break; |
| + } |
| +} |
| + |
| +void SubscriptionManager::Unsubscribe(const std::string& token) { |
| + // TODO(mamir): Implement. |
| +} |
| +} // namespace ntp_snippets |