Commit 4f96a312 authored by Jochen Eisinger's avatar Jochen Eisinger Committed by Commit Bot

Move ContextualSuggestionService to IdentityManager

BUG=849652
R=blundell@chromium.org

Change-Id: I145400236cf07490b24bab1519dc924c3402bc60
Reviewed-on: https://chromium-review.googlesource.com/1095295Reviewed-by: default avatarColin Blundell <blundell@chromium.org>
Commit-Queue: Jochen Eisinger <jochen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#566012}
parent 457bd390
......@@ -6,12 +6,9 @@
#include "base/memory/singleton.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
#include "chrome/browser/signin/signin_manager_factory.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/omnibox/browser/contextual_suggestions_service.h"
#include "components/signin/core/browser/profile_oauth2_token_service.h"
#include "components/signin/core/browser/signin_manager.h"
// static
ContextualSuggestionsService*
......@@ -30,11 +27,9 @@ ContextualSuggestionsServiceFactory::GetInstance() {
KeyedService* ContextualSuggestionsServiceFactory::BuildServiceInstanceFor(
content::BrowserContext* context) const {
Profile* profile = Profile::FromBrowserContext(context);
SigninManagerBase* signin_manager =
SigninManagerFactory::GetForProfile(profile);
ProfileOAuth2TokenService* token_service =
ProfileOAuth2TokenServiceFactory::GetForProfile(profile);
return new ContextualSuggestionsService(signin_manager, token_service,
identity::IdentityManager* identity_manager =
IdentityManagerFactory::GetForProfile(profile);
return new ContextualSuggestionsService(identity_manager,
profile->GetRequestContext());
}
......@@ -42,8 +37,7 @@ ContextualSuggestionsServiceFactory::ContextualSuggestionsServiceFactory()
: BrowserContextKeyedServiceFactory(
"ContextualSuggestionsService",
BrowserContextDependencyManager::GetInstance()) {
DependsOn(SigninManagerFactory::GetInstance());
DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance());
DependsOn(IdentityManagerFactory::GetInstance());
}
ContextualSuggestionsServiceFactory::~ContextualSuggestionsServiceFactory() {}
......@@ -23,6 +23,8 @@
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "net/url_request/url_fetcher.h"
#include "net/url_request/url_request_context_getter.h"
#include "services/identity/public/cpp/identity_manager.h"
#include "services/identity/public/cpp/primary_account_access_token_fetcher.h"
namespace {
......@@ -91,12 +93,10 @@ std::string FormatRequestBodyExperimentalService(const std::string& current_url,
} // namespace
ContextualSuggestionsService::ContextualSuggestionsService(
SigninManagerBase* signin_manager,
OAuth2TokenService* token_service,
identity::IdentityManager* identity_manager,
net::URLRequestContextGetter* request_context)
: request_context_(request_context),
signin_manager_(signin_manager),
token_service_(token_service),
identity_manager_(identity_manager),
token_fetcher_(nullptr) {}
ContextualSuggestionsService::~ContextualSuggestionsService() {}
......@@ -294,12 +294,10 @@ void ContextualSuggestionsService::CreateExperimentalRequest(
fetcher->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
net::LOAD_DO_NOT_SAVE_COOKIES);
const bool should_fetch_access_token =
(signin_manager_ != nullptr) && (token_service_ != nullptr);
// If authentication services are unavailable or if this request is still
// waiting for an oauth2 token, run the contextual service without access
// tokens.
if (!should_fetch_access_token || (token_fetcher_ != nullptr)) {
if ((identity_manager_ == nullptr) || (token_fetcher_ != nullptr)) {
std::move(callback).Run(std::move(fetcher));
return;
}
......@@ -307,8 +305,8 @@ void ContextualSuggestionsService::CreateExperimentalRequest(
// Create the oauth2 token fetcher.
const OAuth2TokenService::ScopeSet scopes{
"https://www.googleapis.com/auth/cusco-chrome-extension"};
token_fetcher_ = std::make_unique<identity::PrimaryAccountAccessTokenFetcher>(
"contextual_suggestions_service", signin_manager_, token_service_, scopes,
token_fetcher_ = identity_manager_->CreateAccessTokenFetcherForPrimaryAccount(
"contextual_suggestions_service", scopes,
base::BindOnce(&ContextualSuggestionsService::AccessTokenAvailable,
base::Unretained(this), std::move(fetcher),
std::move(callback)),
......
......@@ -10,21 +10,28 @@
#include "components/keyed_service/core/keyed_service.h"
#include "net/url_request/url_fetcher_delegate.h"
#include "services/identity/public/cpp/primary_account_access_token_fetcher.h"
#include "url/gurl.h"
class OAuth2TokenService;
class SigninManagerBase;
namespace base {
class Time;
}
namespace identity {
class IdentityManager;
class PrimaryAccountAccessTokenFetcher;
} // namespace identity
namespace net {
class URLRequestContextGetter;
}
class GoogleServiceAuthError;
class TemplateURLService;
// A service to fetch suggestions from a remote endpoint given a URL.
class ContextualSuggestionsService : public KeyedService {
public:
// |signin_manager| and |token_service| may be null but only unauthenticated
// requests will issued.
// |identity_manager| may be null but only unauthenticated requests will
// issued.
// |request_context| may be null, but some services may be disabled.
ContextualSuggestionsService(SigninManagerBase* signin_manager,
OAuth2TokenService* token_service,
ContextualSuggestionsService(identity::IdentityManager* identity_manager,
net::URLRequestContextGetter* request_context);
~ContextualSuggestionsService() override;
......@@ -128,8 +135,7 @@ class ContextualSuggestionsService : public KeyedService {
const std::string& access_token);
net::URLRequestContextGetter* request_context_;
SigninManagerBase* signin_manager_;
OAuth2TokenService* token_service_;
identity::IdentityManager* identity_manager_;
// Helper for fetching OAuth2 access tokens. This is non-null when an access
// token request is currently in progress.
......
......@@ -9,8 +9,7 @@
MockAutocompleteProviderClient::MockAutocompleteProviderClient() {
contextual_suggestions_service_ =
std::make_unique<ContextualSuggestionsService>(
/*signin_manager=*/nullptr, /*token_service=*/nullptr,
GetRequestContext());
/*identity_manager=*/nullptr, GetRequestContext());
}
MockAutocompleteProviderClient::~MockAutocompleteProviderClient() {
......
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