| 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
|
| index 8f7dbc9a5b3f6f399f4fa12af234b4ab7530c5f3..42495e8181b082f2b88a13207ba4d710b7cf589e 100644
|
| --- a/components/ntp_snippets/breaking_news/subscription_manager.cc
|
| +++ b/components/ntp_snippets/breaking_news/subscription_manager.cc
|
| @@ -15,10 +15,12 @@ using internal::SubscriptionJsonRequest;
|
| SubscriptionManager::SubscriptionManager(
|
| scoped_refptr<net::URLRequestContextGetter> url_request_context_getter,
|
| PrefService* pref_service,
|
| - const GURL& subscribe_url)
|
| + const GURL& subscribe_url,
|
| + const GURL& unsubscribe_url)
|
| : url_request_context_getter_(std::move(url_request_context_getter)),
|
| pref_service_(pref_service),
|
| - subscribe_url_(subscribe_url) {}
|
| + subscribe_url_(subscribe_url),
|
| + unsubscribe_url_(unsubscribe_url) {}
|
|
|
| SubscriptionManager::~SubscriptionManager() = default;
|
|
|
| @@ -35,6 +37,13 @@ void SubscriptionManager::Subscribe(const std::string& token) {
|
| &SubscriptionManager::DidSubscribe, base::Unretained(this)));
|
| }
|
|
|
| +bool SubscriptionManager::CanSubscribeNow() {
|
| + if (subscription_request_) {
|
| + return false;
|
| + }
|
| + return true;
|
| +}
|
| +
|
| void SubscriptionManager::DidSubscribe(const ntp_snippets::Status& status) {
|
| subscription_request_.reset();
|
|
|
| @@ -54,8 +63,46 @@ void SubscriptionManager::DidSubscribe(const ntp_snippets::Status& status) {
|
| }
|
| }
|
|
|
| +bool SubscriptionManager::CanUnsubscribeNow() {
|
| + if (unsubscription_request_) {
|
| + return false;
|
| + }
|
| + return true;
|
| +}
|
| +
|
| void SubscriptionManager::Unsubscribe(const std::string& token) {
|
| - // TODO(mamir): Implement.
|
| + DCHECK(!unsubscription_request_);
|
| + unsubscription_token_ = token;
|
| + SubscriptionJsonRequest::Builder builder;
|
| + builder.SetToken(token)
|
| + .SetUrlRequestContextGetter(url_request_context_getter_)
|
| + .SetUrl(unsubscribe_url_);
|
| +
|
| + unsubscription_request_ = builder.Build();
|
| + unsubscription_request_->Start(base::BindOnce(
|
| + &SubscriptionManager::DidUnsubscribe, base::Unretained(this)));
|
| +}
|
| +
|
| +bool SubscriptionManager::IsSubscribed() {
|
| + std::string subscription_token_ = pref_service_->GetString(
|
| + ntp_snippets::prefs::kContentSuggestionsSubscriptionDataToken);
|
| + return !subscription_token_.empty();
|
| +}
|
| +
|
| +void SubscriptionManager::DidUnsubscribe(const ntp_snippets::Status& status) {
|
| + unsubscription_request_.reset();
|
| +
|
| + switch (status.code) {
|
| + case ntp_snippets::StatusCode::SUCCESS:
|
| + // In case of successful unsubscription, clear the previously stored data.
|
| + // TODO(mamir): clear stored region and language.
|
| + pref_service_->ClearPref(
|
| + ntp_snippets::prefs::kContentSuggestionsSubscriptionDataToken);
|
| + break;
|
| + default:
|
| + // TODO(mamir): handle failure.
|
| + break;
|
| + }
|
| }
|
|
|
| void SubscriptionManager::RegisterProfilePrefs(PrefRegistrySimple* registry) {
|
|
|