Commit 59c1b85b authored by Colin Blundell's avatar Colin Blundell Committed by Commit Bot

[NTP Snippets] Have SubscriptionManager use IdentityManager

This CL converts SubscriptionManager from using
//components/signin to using the Identity Service client library.
The conversion is straightforward:

- Observer IdentityManager rather than SigninManager.
- Get info of primary account via IdentityManager rather than
SigninManager.
- Create PrimaryAccountAccessTokenFetcher via IdentityManager rather
than directly.

I tested the relevant behavior manually as follows, based on
instructions from mamir@chromium.org:

1. Enabled Breaking News Push by turning on "enable-breaking-newspush
on chrome://flags:
https://cs.chromium.org/chromium/src/chrome/browser/about_flags.cc?type=cs&q=kEnableBreakingNewsPushName&sq=package:chromium&l=2497

2. Checked the about:histograms for the feature's metrics by doing
"find in page" for "subscription". Saw no histogram entries after
visiting the NTP when signed out but *did* see histogram entries after
visiting the NTP when signed in. Metrics listed here:
https://cs.chromium.org/chromium/src/components/ntp_snippets/breaking_news/breaking_news_metrics.cc?type=cs&l=17

A followup change will convert the test to use IdentityTestEnvironment.

Bug: 798413
Change-Id: I9193d25c2793cf52fd825186cd5d875b352eb8bb
Reviewed-on: https://chromium-review.googlesource.com/888520
Commit-Queue: Colin Blundell <blundell@chromium.org>
Reviewed-by: default avatarJan Krcal <jkrcal@chromium.org>
Cr-Commit-Position: refs/heads/master@{#534750}
parent 8982fe4f
...@@ -276,11 +276,8 @@ MakeBreakingNewsGCMAppHandlerIfEnabled( ...@@ -276,11 +276,8 @@ MakeBreakingNewsGCMAppHandlerIfEnabled(
gcm::GCMDriver* gcm_driver = gcm::GCMDriver* gcm_driver =
gcm::GCMProfileServiceFactory::GetForProfile(profile)->driver(); gcm::GCMProfileServiceFactory::GetForProfile(profile)->driver();
OAuth2TokenService* token_service = identity::IdentityManager* identity_manager =
ProfileOAuth2TokenServiceFactory::GetForProfile(profile); IdentityManagerFactory::GetForProfile(profile);
SigninManagerBase* signin_manager =
SigninManagerFactory::GetForProfile(profile);
scoped_refptr<net::URLRequestContextGetter> request_context = scoped_refptr<net::URLRequestContextGetter> request_context =
content::BrowserContext::GetDefaultStoragePartition(profile) content::BrowserContext::GetDefaultStoragePartition(profile)
...@@ -296,9 +293,8 @@ MakeBreakingNewsGCMAppHandlerIfEnabled( ...@@ -296,9 +293,8 @@ MakeBreakingNewsGCMAppHandlerIfEnabled(
} }
auto subscription_manager = base::MakeUnique<SubscriptionManagerImpl>( auto subscription_manager = base::MakeUnique<SubscriptionManagerImpl>(
request_context, pref_service, variations_service, signin_manager, request_context, pref_service, variations_service, identity_manager,
token_service, api_key, locale, api_key, locale, GetPushUpdatesSubscriptionEndpoint(chrome::GetChannel()),
GetPushUpdatesSubscriptionEndpoint(chrome::GetChannel()),
GetPushUpdatesUnsubscriptionEndpoint(chrome::GetChannel())); GetPushUpdatesUnsubscriptionEndpoint(chrome::GetChannel()));
instance_id::InstanceIDProfileService* instance_id_profile_service = instance_id::InstanceIDProfileService* instance_id_profile_service =
......
...@@ -14,7 +14,6 @@ ...@@ -14,7 +14,6 @@
#include "components/ntp_snippets/pref_names.h" #include "components/ntp_snippets/pref_names.h"
#include "components/prefs/pref_registry_simple.h" #include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h" #include "components/prefs/pref_service.h"
#include "components/signin/core/browser/signin_manager_base.h"
#include "components/variations/service/variations_service.h" #include "components/variations/service/variations_service.h"
#include "net/base/url_util.h" #include "net/base/url_util.h"
#include "services/identity/public/cpp/primary_account_access_token_fetcher.h" #include "services/identity/public/cpp/primary_account_access_token_fetcher.h"
...@@ -34,8 +33,7 @@ SubscriptionManagerImpl::SubscriptionManagerImpl( ...@@ -34,8 +33,7 @@ SubscriptionManagerImpl::SubscriptionManagerImpl(
scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, scoped_refptr<net::URLRequestContextGetter> url_request_context_getter,
PrefService* pref_service, PrefService* pref_service,
variations::VariationsService* variations_service, variations::VariationsService* variations_service,
SigninManagerBase* signin_manager, identity::IdentityManager* identity_manager,
OAuth2TokenService* access_token_service,
const std::string& locale, const std::string& locale,
const std::string& api_key, const std::string& api_key,
const GURL& subscribe_url, const GURL& subscribe_url,
...@@ -43,17 +41,16 @@ SubscriptionManagerImpl::SubscriptionManagerImpl( ...@@ -43,17 +41,16 @@ SubscriptionManagerImpl::SubscriptionManagerImpl(
: url_request_context_getter_(std::move(url_request_context_getter)), : url_request_context_getter_(std::move(url_request_context_getter)),
pref_service_(pref_service), pref_service_(pref_service),
variations_service_(variations_service), variations_service_(variations_service),
signin_manager_(signin_manager), identity_manager_(identity_manager),
access_token_service_(access_token_service),
locale_(locale), locale_(locale),
api_key_(api_key), api_key_(api_key),
subscribe_url_(subscribe_url), subscribe_url_(subscribe_url),
unsubscribe_url_(unsubscribe_url) { unsubscribe_url_(unsubscribe_url) {
signin_manager_->AddObserver(this); identity_manager_->AddObserver(this);
} }
SubscriptionManagerImpl::~SubscriptionManagerImpl() { SubscriptionManagerImpl::~SubscriptionManagerImpl() {
signin_manager_->RemoveObserver(this); identity_manager_->RemoveObserver(this);
} }
void SubscriptionManagerImpl::Subscribe(const std::string& subscription_token) { void SubscriptionManagerImpl::Subscribe(const std::string& subscription_token) {
...@@ -61,7 +58,7 @@ void SubscriptionManagerImpl::Subscribe(const std::string& subscription_token) { ...@@ -61,7 +58,7 @@ void SubscriptionManagerImpl::Subscribe(const std::string& subscription_token) {
if (request_) { if (request_) {
request_ = nullptr; request_ = nullptr;
} }
if (signin_manager_->IsAuthenticated()) { if (identity_manager_->HasPrimaryAccount()) {
StartAccessTokenRequest(subscription_token); StartAccessTokenRequest(subscription_token);
} else { } else {
SubscribeInternal(subscription_token, /*access_token=*/std::string()); SubscribeInternal(subscription_token, /*access_token=*/std::string());
...@@ -104,12 +101,13 @@ void SubscriptionManagerImpl::StartAccessTokenRequest( ...@@ -104,12 +101,13 @@ void SubscriptionManagerImpl::StartAccessTokenRequest(
} }
OAuth2TokenService::ScopeSet scopes = {kContentSuggestionsApiScope}; OAuth2TokenService::ScopeSet scopes = {kContentSuggestionsApiScope};
access_token_fetcher_ = std::make_unique< access_token_fetcher_ =
identity::PrimaryAccountAccessTokenFetcher>( identity_manager_->CreateAccessTokenFetcherForPrimaryAccount(
"ntp_snippets", signin_manager_, access_token_service_, scopes, "ntp_snippets", scopes,
base::BindOnce(&SubscriptionManagerImpl::AccessTokenFetchFinished, base::BindOnce(&SubscriptionManagerImpl::AccessTokenFetchFinished,
base::Unretained(this), subscription_token), base::Unretained(this), subscription_token),
identity::PrimaryAccountAccessTokenFetcher::Mode::kWaitUntilAvailable); identity::PrimaryAccountAccessTokenFetcher::Mode::
kWaitUntilAvailable);
} }
void SubscriptionManagerImpl::AccessTokenFetchFinished( void SubscriptionManagerImpl::AccessTokenFetchFinished(
...@@ -193,7 +191,7 @@ bool SubscriptionManagerImpl::NeedsToResubscribe() { ...@@ -193,7 +191,7 @@ bool SubscriptionManagerImpl::NeedsToResubscribe() {
// Check if authentication state changed after subscription. // Check if authentication state changed after subscription.
bool is_auth_subscribe = pref_service_->GetBoolean( bool is_auth_subscribe = pref_service_->GetBoolean(
prefs::kBreakingNewsSubscriptionDataIsAuthenticated); prefs::kBreakingNewsSubscriptionDataIsAuthenticated);
bool is_authenticated = signin_manager_->IsAuthenticated(); bool is_authenticated = identity_manager_->HasPrimaryAccount();
return is_auth_subscribe != is_authenticated; return is_auth_subscribe != is_authenticated;
} }
...@@ -235,14 +233,13 @@ void SubscriptionManagerImpl::DidUnsubscribe(const std::string& new_token, ...@@ -235,14 +233,13 @@ void SubscriptionManagerImpl::DidUnsubscribe(const std::string& new_token,
} }
} }
void SubscriptionManagerImpl::GoogleSigninSucceeded( void SubscriptionManagerImpl::OnPrimaryAccountSet(
const std::string& account_id, const AccountInfo& account_info) {
const std::string& username) {
SigninStatusChanged(); SigninStatusChanged();
} }
void SubscriptionManagerImpl::GoogleSignedOut(const std::string& account_id, void SubscriptionManagerImpl::OnPrimaryAccountCleared(
const std::string& username) { const AccountInfo& account_info) {
SigninStatusChanged(); SigninStatusChanged();
} }
......
...@@ -11,11 +11,10 @@ ...@@ -11,11 +11,10 @@
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "components/ntp_snippets/breaking_news/subscription_json_request.h" #include "components/ntp_snippets/breaking_news/subscription_json_request.h"
#include "components/ntp_snippets/breaking_news/subscription_manager.h" #include "components/ntp_snippets/breaking_news/subscription_manager.h"
#include "components/signin/core/browser/signin_manager_base.h"
#include "net/url_request/url_request_context_getter.h" #include "net/url_request/url_request_context_getter.h"
#include "services/identity/public/cpp/identity_manager.h"
#include "url/gurl.h" #include "url/gurl.h"
class OAuth2TokenService;
class PrefRegistrySimple; class PrefRegistrySimple;
class PrefService; class PrefService;
...@@ -35,14 +34,13 @@ namespace ntp_snippets { ...@@ -35,14 +34,13 @@ namespace ntp_snippets {
// for subscription. Bookkeeping is required to detect any change (e.g. the // for subscription. Bookkeeping is required to detect any change (e.g. the
// token render invalid), and resubscribe accordingly. // token render invalid), and resubscribe accordingly.
class SubscriptionManagerImpl : public SubscriptionManager, class SubscriptionManagerImpl : public SubscriptionManager,
public SigninManagerBase::Observer { public identity::IdentityManager::Observer {
public: public:
SubscriptionManagerImpl( SubscriptionManagerImpl(
scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, scoped_refptr<net::URLRequestContextGetter> url_request_context_getter,
PrefService* pref_service, PrefService* pref_service,
variations::VariationsService* variations_service, variations::VariationsService* variations_service,
SigninManagerBase* signin_manager, identity::IdentityManager* identity_manager,
OAuth2TokenService* access_token_service,
const std::string& locale, const std::string& locale,
const std::string& api_key, const std::string& api_key,
const GURL& subscribe_url, const GURL& subscribe_url,
...@@ -65,11 +63,9 @@ class SubscriptionManagerImpl : public SubscriptionManager, ...@@ -65,11 +63,9 @@ class SubscriptionManagerImpl : public SubscriptionManager,
static void ClearProfilePrefs(PrefService* pref_service); static void ClearProfilePrefs(PrefService* pref_service);
private: private:
// SigninManagerBase::Observer implementation. // identity:IdentityManager::Observer implementation.
void GoogleSigninSucceeded(const std::string& account_id, void OnPrimaryAccountSet(const AccountInfo& account_info) override;
const std::string& username) override; void OnPrimaryAccountCleared(const AccountInfo& account_info) override;
void GoogleSignedOut(const std::string& account_id,
const std::string& username) override;
void SigninStatusChanged(); void SigninStatusChanged();
...@@ -104,8 +100,7 @@ class SubscriptionManagerImpl : public SubscriptionManager, ...@@ -104,8 +100,7 @@ class SubscriptionManagerImpl : public SubscriptionManager,
variations::VariationsService* const variations_service_; variations::VariationsService* const variations_service_;
// Authentication for signed-in users. // Authentication for signed-in users.
SigninManagerBase* signin_manager_; identity::IdentityManager* identity_manager_;
OAuth2TokenService* access_token_service_;
const std::string locale_; const std::string locale_;
......
...@@ -18,6 +18,8 @@ ...@@ -18,6 +18,8 @@
#include "net/base/net_errors.h" #include "net/base/net_errors.h"
#include "net/url_request/test_url_fetcher_factory.h" #include "net/url_request/test_url_fetcher_factory.h"
#include "net/url_request/url_request_test_util.h" #include "net/url_request/url_request_test_util.h"
#include "services/identity/public/cpp/identity_manager.h"
#include "services/identity/public/cpp/identity_test_utils.h"
#include "testing/gmock/include/gmock/gmock.h" #include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
...@@ -25,7 +27,10 @@ using testing::ElementsAre; ...@@ -25,7 +27,10 @@ using testing::ElementsAre;
namespace ntp_snippets { namespace ntp_snippets {
#if !defined(OS_CHROMEOS)
const char kTestEmail[] = "test@email.com"; const char kTestEmail[] = "test@email.com";
#endif
const char kAPIKey[] = "fakeAPIkey"; const char kAPIKey[] = "fakeAPIkey";
const char kSubscriptionUrl[] = "http://valid-url.test/subscribe"; const char kSubscriptionUrl[] = "http://valid-url.test/subscribe";
const char kSubscriptionUrlSignedIn[] = "http://valid-url.test/subscribe"; const char kSubscriptionUrlSignedIn[] = "http://valid-url.test/subscribe";
...@@ -42,7 +47,8 @@ class SubscriptionManagerImplTest ...@@ -42,7 +47,8 @@ class SubscriptionManagerImplTest
public OAuth2TokenService::DiagnosticsObserver { public OAuth2TokenService::DiagnosticsObserver {
public: public:
SubscriptionManagerImplTest() SubscriptionManagerImplTest()
: request_context_getter_( : identity_manager_(utils_.fake_signin_manager(), utils_.token_service()),
request_context_getter_(
new net::TestURLRequestContextGetter(message_loop_.task_runner())) { new net::TestURLRequestContextGetter(message_loop_.task_runner())) {
} }
...@@ -66,13 +72,14 @@ class SubscriptionManagerImplTest ...@@ -66,13 +72,14 @@ class SubscriptionManagerImplTest
return utils_.token_service(); return utils_.token_service();
} }
identity::IdentityManager* GetIdentityManager() { return &identity_manager_; }
SigninManagerBase* GetSigninManager() { return utils_.fake_signin_manager(); } SigninManagerBase* GetSigninManager() { return utils_.fake_signin_manager(); }
std::unique_ptr<SubscriptionManagerImpl> BuildSubscriptionManager() { std::unique_ptr<SubscriptionManagerImpl> BuildSubscriptionManager() {
return std::make_unique<SubscriptionManagerImpl>( return std::make_unique<SubscriptionManagerImpl>(
GetRequestContext(), GetPrefService(), GetRequestContext(), GetPrefService(),
/*variations_service=*/nullptr, GetSigninManager(), /*variations_service=*/nullptr, GetIdentityManager(),
GetOAuth2TokenService(),
/*locale=*/"", kAPIKey, GURL(kSubscriptionUrl), /*locale=*/"", kAPIKey, GURL(kSubscriptionUrl),
GURL(kUnsubscriptionUrl)); GURL(kUnsubscriptionUrl));
} }
...@@ -130,19 +137,26 @@ class SubscriptionManagerImplTest ...@@ -130,19 +137,26 @@ class SubscriptionManagerImplTest
#if !defined(OS_CHROMEOS) #if !defined(OS_CHROMEOS)
void SignIn() { void SignIn() {
utils_.fake_signin_manager()->SignIn(kTestEmail, "user", "pass"); identity::MakePrimaryAccountAvailable(utils_.fake_signin_manager(),
GetOAuth2TokenService(),
GetIdentityManager(), kTestEmail);
} }
void SignOut() { utils_.fake_signin_manager()->ForceSignOut(); } void SignOut() {
identity::ClearPrimaryAccount(utils_.fake_signin_manager(),
GetIdentityManager());
}
#endif // !defined(OS_CHROMEOS) #endif // !defined(OS_CHROMEOS)
void IssueRefreshToken(FakeProfileOAuth2TokenService* auth_token_service) { void IssueRefreshToken(FakeProfileOAuth2TokenService* auth_token_service) {
auth_token_service->GetDelegate()->UpdateCredentials(kTestEmail, "token"); auth_token_service->GetDelegate()->UpdateCredentials(
GetIdentityManager()->GetPrimaryAccountInfo().account_id, "token");
} }
void IssueAccessToken(FakeProfileOAuth2TokenService* auth_token_service) { void IssueAccessToken(FakeProfileOAuth2TokenService* auth_token_service) {
auth_token_service->IssueAllTokensForAccount(kTestEmail, "access_token", auth_token_service->IssueAllTokensForAccount(
base::Time::Max()); GetIdentityManager()->GetPrimaryAccountInfo().account_id,
"access_token", base::Time::Max());
} }
void set_on_access_token_request_callback(base::OnceClosure callback) { void set_on_access_token_request_callback(base::OnceClosure callback) {
...@@ -178,6 +192,7 @@ class SubscriptionManagerImplTest ...@@ -178,6 +192,7 @@ class SubscriptionManagerImplTest
base::MessageLoop message_loop_; base::MessageLoop message_loop_;
test::RemoteSuggestionsTestUtils utils_; test::RemoteSuggestionsTestUtils utils_;
identity::IdentityManager identity_manager_;
scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_;
net::TestURLFetcherFactory url_fetcher_factory_; net::TestURLFetcherFactory url_fetcher_factory_;
base::OnceClosure on_access_token_request_callback_; base::OnceClosure on_access_token_request_callback_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment