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 #include "components/ntp_snippets/breaking_news/subscription_manager.h" | |
| 6 #include "base/bind.h" | |
| 7 #include "components/ntp_snippets/breaking_news/subscription_json_request.h" | |
| 8 #include "components/ntp_snippets/pref_names.h" | |
| 9 #include "components/prefs/pref_service.h" | |
| 10 | |
| 11 namespace ntp_snippets { | |
| 12 | |
| 13 using internal::SubscriptionJsonRequest; | |
| 14 | |
| 15 SubscriptionManager::SubscriptionManager( | |
| 16 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, | |
| 17 PrefService* pref_service) | |
| 18 : url_request_context_getter_(std::move(url_request_context_getter)), | |
| 19 pref_service_(pref_service) {} | |
| 20 | |
| 21 SubscriptionManager::~SubscriptionManager() = default; | |
| 22 | |
| 23 void SubscriptionManager::Subscribe(const std::string& token) { | |
| 24 DCHECK(!subscription_request_); | |
| 25 subscription_token_ = token; | |
| 26 SubscriptionJsonRequest::Builder builder; | |
| 27 builder.SetToken(token) | |
| 28 .SetUrlRequestContextGetter(url_request_context_getter_) | |
| 29 .SetUrl(subscribe_url_); | |
| 30 | |
| 31 subscription_request_ = builder.Build(); | |
| 32 subscription_request_->Start(base::BindOnce( | |
| 33 &SubscriptionManager::DidSubscribe, base::Unretained(this))); | |
| 34 } | |
| 35 | |
| 36 void SubscriptionManager::DidSubscribe(const ntp_snippets::Status& status) { | |
| 37 subscription_request_.reset(); | |
| 38 | |
| 39 switch (status.code) { | |
| 40 case ntp_snippets::StatusCode::SUCCESS: | |
| 41 // In case of successful subscription, store the same data used for | |
| 42 // subscription in order to be able to re-subscribe in case of data | |
| 43 // change. For now, only the subscription token, but later region and | |
| 44 // 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.
| |
| 45 // TODO(mamir): store region and language. | |
| 46 pref_service_->SetString( | |
| 47 ntp_snippets::prefs::kContentSuggestionsSubscriptionToken, | |
| 48 subscription_token_); | |
| 49 break; | |
| 50 default: | |
| 51 // TODO(mamir): handle failure. | |
| 52 break; | |
| 53 } | |
| 54 } | |
| 55 | |
| 56 void SubscriptionManager::Unsubscribe(const std::string& token) { | |
| 57 // TODO(mamir): Implement. | |
| 58 } | |
| 59 } // namespace ntp_snippets | |
| OLD | NEW |