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

Side by Side Diff: components/ntp_snippets/breaking_news/subscription_manager.cc

Issue 2914263002: [NTP::Push] Adding Breaking News Subscription Manager (Closed)
Patch Set: Subscription endpoint as a parameter. 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 #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 const GURL& subscribe_url)
19 : url_request_context_getter_(std::move(url_request_context_getter)),
20 pref_service_(pref_service),
21 subscribe_url_(subscribe_url) {}
22
23 SubscriptionManager::~SubscriptionManager() = default;
24
25 void SubscriptionManager::Subscribe(const std::string& token) {
26 DCHECK(!subscription_request_);
27 subscription_token_ = token;
28 SubscriptionJsonRequest::Builder builder;
29 builder.SetToken(token)
30 .SetUrlRequestContextGetter(url_request_context_getter_)
31 .SetUrl(subscribe_url_);
32
33 subscription_request_ = builder.Build();
34 subscription_request_->Start(base::BindOnce(
35 &SubscriptionManager::DidSubscribe, base::Unretained(this)));
36 }
37
38 void SubscriptionManager::DidSubscribe(const ntp_snippets::Status& status) {
39 subscription_request_.reset();
40
41 switch (status.code) {
42 case ntp_snippets::StatusCode::SUCCESS:
43 // In case of successful subscription, store the same data used for
44 // subscription in order to be able to re-subscribe in case of data
45 // change.
46 // TODO(mamir): store region and language.
47 pref_service_->SetString(
48 ntp_snippets::prefs::kContentSuggestionsSubscriptionDataToken,
49 subscription_token_);
50 break;
51 default:
52 // TODO(mamir): handle failure.
53 break;
54 }
55 }
56
57 void SubscriptionManager::Unsubscribe(const std::string& token) {
58 // TODO(mamir): Implement.
59 }
60
61 void SubscriptionManager::RegisterProfilePrefs(PrefRegistrySimple* registry) {
62 registry->RegisterStringPref(prefs::kContentSuggestionsSubscriptionDataToken,
63 std::string());
64 }
65 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698