Skip to content

Commit

Permalink
[NTP::Push] Adding Breaking News Subscription Manager
Browse files Browse the repository at this point in the history
It wraps around the functionality in SubscriptionJsonRequest (and later UnsubscriptionJsonRequest)

BUG=728697

Review-Url: https://codereview.chromium.org/2914263002
Cr-Commit-Position: refs/heads/master@{#478006}
  • Loading branch information
mohamedamir authored and Commit Bot committed Jun 8, 2017
1 parent fcdd13c commit 1cecb82
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 0 deletions.
2 changes: 2 additions & 0 deletions chrome/browser/prefs/browser_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
#include "components/gcm_driver/gcm_channel_status_syncer.h"
#include "components/metrics/metrics_service.h"
#include "components/network_time/network_time_tracker.h"
#include "components/ntp_snippets/breaking_news/subscription_manager.h"
#include "components/ntp_snippets/content_suggestions_service.h"
#include "components/ntp_snippets/remote/remote_suggestions_provider_impl.h"
#include "components/ntp_snippets/remote/remote_suggestions_scheduler_impl.h"
Expand Down Expand Up @@ -502,6 +503,7 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
ntp_snippets::RemoteSuggestionsProviderImpl::RegisterProfilePrefs(registry);
ntp_snippets::RemoteSuggestionsSchedulerImpl::RegisterProfilePrefs(registry);
ntp_snippets::RequestThrottler::RegisterProfilePrefs(registry);
ntp_snippets::SubscriptionManager::RegisterProfilePrefs(registry);
ntp_snippets::UserClassifier::RegisterProfilePrefs(registry);
ntp_tiles::MostVisitedSites::RegisterProfilePrefs(registry);
password_bubble_experiment::RegisterPrefs(registry);
Expand Down
3 changes: 3 additions & 0 deletions components/ntp_snippets/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ static_library("ntp_snippets") {
"bookmarks/bookmark_suggestions_provider.h",
"breaking_news/subscription_json_request.cc",
"breaking_news/subscription_json_request.h",
"breaking_news/subscription_manager.cc",
"breaking_news/subscription_manager.h",
"callbacks.h",
"category.cc",
"category.h",
Expand Down Expand Up @@ -144,6 +146,7 @@ source_set("unit_tests") {
"bookmarks/bookmark_last_visit_utils_unittest.cc",
"bookmarks/bookmark_suggestions_provider_unittest.cc",
"breaking_news/subscription_json_request_unittest.cc",
"breaking_news/subscription_manager_unittest.cc",
"category_rankers/click_based_category_ranker_unittest.cc",
"category_rankers/constant_category_ranker_unittest.cc",
"category_unittest.cc",
Expand Down
65 changes: 65 additions & 0 deletions components/ntp_snippets/breaking_news/subscription_manager.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// 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,
const GURL& subscribe_url)
: url_request_context_getter_(std::move(url_request_context_getter)),
pref_service_(pref_service),
subscribe_url_(subscribe_url) {}

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.
// TODO(mamir): store region and language.
pref_service_->SetString(
ntp_snippets::prefs::kContentSuggestionsSubscriptionDataToken,
subscription_token_);
break;
default:
// TODO(mamir): handle failure.
break;
}
}

void SubscriptionManager::Unsubscribe(const std::string& token) {
// TODO(mamir): Implement.
}

void SubscriptionManager::RegisterProfilePrefs(PrefRegistrySimple* registry) {
registry->RegisterStringPref(prefs::kContentSuggestionsSubscriptionDataToken,
std::string());
}
} // namespace ntp_snippets
49 changes: 49 additions & 0 deletions components/ntp_snippets/breaking_news/subscription_manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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_MANAGER_H_
#define COMPONENTS_NTP_SNIPPETS_BREAKING_NEWS_SUBSCRIPTION_MANAGER_H_

#include "components/ntp_snippets/breaking_news/subscription_json_request.h"
#include "components/prefs/pref_registry_simple.h"
#include "net/url_request/url_request_context_getter.h"
#include "url/gurl.h"

class PrefRegistrySimple;

namespace ntp_snippets {

class SubscriptionManager {
public:
SubscriptionManager(
scoped_refptr<net::URLRequestContextGetter> url_request_context_getter,
PrefService* pref_service,
const GURL& subscribe_url);

~SubscriptionManager();

void Subscribe(const std::string& token);
void Unsubscribe(const std::string& token);

static void RegisterProfilePrefs(PrefRegistrySimple* registry);

private:
std::string subscription_token_;

// Holds the URL request context.
scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;

std::unique_ptr<internal::SubscriptionJsonRequest> subscription_request_;

PrefService* pref_service_;

// API endpoint for subscribing.
const GURL subscribe_url_;

void DidSubscribe(const ntp_snippets::Status& status);

DISALLOW_COPY_AND_ASSIGN(SubscriptionManager);
};
}
#endif // COMPONENTS_NTP_SNIPPETS_BREAKING_NEWS_SUBSCRIPTION_MANAGER_H_
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// 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/message_loop/message_loop.h"
#include "components/ntp_snippets/pref_names.h"
#include "components/prefs/testing_pref_service.h"
#include "net/url_request/test_url_fetcher_factory.h"
#include "net/url_request/url_request_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace ntp_snippets {

class SubscriptionManagerTest : public testing::Test {
public:
SubscriptionManagerTest()
: request_context_getter_(
new net::TestURLRequestContextGetter(message_loop_.task_runner())),
pref_service_(base::MakeUnique<TestingPrefServiceSimple>()) {}

void SetUp() override {
SubscriptionManager::RegisterProfilePrefs(pref_service_->registry());
}

scoped_refptr<net::URLRequestContextGetter> GetRequestContext() {
return request_context_getter_.get();
}

PrefService* GetPrefService() { return pref_service_.get(); }

net::TestURLFetcher* GetRunningFetcher() {
// All created TestURLFetchers have ID 0 by default.
net::TestURLFetcher* url_fetcher = url_fetcher_factory_.GetFetcherByID(0);
DCHECK(url_fetcher);
return url_fetcher;
}

void RespondWithData(const std::string& data) {
net::TestURLFetcher* url_fetcher = GetRunningFetcher();
url_fetcher->set_status(net::URLRequestStatus());
url_fetcher->set_response_code(net::HTTP_OK);
url_fetcher->SetResponseString(data);
// Call the URLFetcher delegate to continue the test.
url_fetcher->delegate()->OnURLFetchComplete(url_fetcher);
}

void RespondWithError(int error_code) {
net::TestURLFetcher* url_fetcher = GetRunningFetcher();
url_fetcher->set_status(net::URLRequestStatus::FromError(error_code));
url_fetcher->SetResponseString(std::string());
// Call the URLFetcher delegate to continue the test.
url_fetcher->delegate()->OnURLFetchComplete(url_fetcher);
}

private:
base::MessageLoop message_loop_;
scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_;
net::TestURLFetcherFactory url_fetcher_factory_;
std::unique_ptr<TestingPrefServiceSimple> pref_service_;
};

TEST_F(SubscriptionManagerTest, SubscribeSuccessfully) {
std::string token = "1234567890";
SubscriptionManager manager(GetRequestContext(), GetPrefService(),
GURL("http://valid-url.test"));
manager.Subscribe(token);
RespondWithData("");
EXPECT_EQ(GetPrefService()->GetString(
ntp_snippets::prefs::kContentSuggestionsSubscriptionDataToken),
token);
}

TEST_F(SubscriptionManagerTest, SubscribeWithErrors) {
std::string token = "1234567890";
SubscriptionManager manager(GetRequestContext(), GetPrefService(),
GURL("http://valid-url.test"));
manager.Subscribe(token);
RespondWithError(net::ERR_TIMED_OUT);
EXPECT_FALSE(GetPrefService()->HasPrefPath(
ntp_snippets::prefs::kContentSuggestionsSubscriptionDataToken));
}

} // namespace ntp_snippets
3 changes: 3 additions & 0 deletions components/ntp_snippets/pref_names.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,8 @@ const char kClickBasedCategoryRankerOrderWithClicks[] =
const char kClickBasedCategoryRankerLastDecayTime[] =
"ntp_suggestions.click_based_category_ranker.last_decay_time";

const char kContentSuggestionsSubscriptionDataToken[] =
"ntp_suggestions.content_suggestions_subscription_data.token";

} // namespace prefs
} // namespace ntp_snippets
4 changes: 4 additions & 0 deletions components/ntp_snippets/pref_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ extern const char kClickBasedCategoryRankerOrderWithClicks[];
// The pref name for the time when last click decay has happened.
extern const char kClickBasedCategoryRankerLastDecayTime[];

// The pref name for the subscription token used when subscription for breaking
// news push updates,
extern const char kContentSuggestionsSubscriptionDataToken[];

} // namespace prefs
} // namespace ntp_snippets

Expand Down

0 comments on commit 1cecb82

Please sign in to comment.