This source file includes following definitions.
- oauth2_token_service_
- PrepareForUserCloudPolicyManagerShutdown
- Shutdown
- RegisterForPolicy
- CallPolicyRegistrationCallback
- OnRefreshTokenAvailable
- InitializeUserCloudPolicyManager
- ShutdownUserCloudPolicyManager
- OnInitializationCompleted
- RegisterCloudPolicyService
- OnRegistrationComplete
- ProhibitSignoutIfNeeded
#include "chrome/browser/policy/cloud/user_policy_signin_service.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/callback.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
#include "components/policy/core/common/cloud/cloud_policy_client_registration_helper.h"
#include "components/policy/core/common/cloud/user_cloud_policy_manager.h"
#include "components/signin/core/browser/profile_oauth2_token_service.h"
#include "components/signin/core/browser/signin_manager.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_source.h"
#include "google_apis/gaia/gaia_constants.h"
#include "net/url_request/url_request_context_getter.h"
namespace policy {
UserPolicySigninService::UserPolicySigninService(
Profile* profile,
PrefService* local_state,
DeviceManagementService* device_management_service,
UserCloudPolicyManager* policy_manager,
SigninManager* signin_manager,
scoped_refptr<net::URLRequestContextGetter> system_request_context,
ProfileOAuth2TokenService* token_service)
: UserPolicySigninServiceBase(profile,
local_state,
device_management_service,
policy_manager,
signin_manager,
system_request_context),
profile_(profile),
oauth2_token_service_(token_service) {
DCHECK(!oauth2_token_service_->RefreshTokenIsAvailable(
signin_manager->GetAuthenticatedAccountId()));
oauth2_token_service_->AddObserver(this);
}
UserPolicySigninService::~UserPolicySigninService() {
}
void UserPolicySigninService::PrepareForUserCloudPolicyManagerShutdown() {
registration_helper_.reset();
UserPolicySigninServiceBase::PrepareForUserCloudPolicyManagerShutdown();
}
void UserPolicySigninService::Shutdown() {
UserPolicySigninServiceBase::Shutdown();
oauth2_token_service_->RemoveObserver(this);
}
void UserPolicySigninService::RegisterForPolicy(
const std::string& username,
const std::string& oauth2_refresh_token,
const PolicyRegistrationCallback& callback) {
DCHECK(!oauth2_refresh_token.empty());
scoped_ptr<CloudPolicyClient> policy_client = CreateClientForRegistrationOnly(
username);
if (!policy_client) {
callback.Run(std::string(), std::string());
return;
}
registration_helper_.reset(new CloudPolicyClientRegistrationHelper(
policy_client.get(),
enterprise_management::DeviceRegisterRequest::BROWSER));
registration_helper_->StartRegistrationWithLoginToken(
oauth2_refresh_token,
base::Bind(&UserPolicySigninService::CallPolicyRegistrationCallback,
base::Unretained(this),
base::Passed(&policy_client),
callback));
}
void UserPolicySigninService::CallPolicyRegistrationCallback(
scoped_ptr<CloudPolicyClient> client,
PolicyRegistrationCallback callback) {
registration_helper_.reset();
callback.Run(client->dm_token(), client->client_id());
}
void UserPolicySigninService::OnRefreshTokenAvailable(
const std::string& account_id) {
if (!policy_manager()) {
DVLOG(1) << "Skipping initialization for tests due to missing components.";
return;
}
if (account_id != signin_manager()->GetAuthenticatedAccountId())
return;
InitializeForSignedInUser(signin_manager()->GetAuthenticatedUsername(),
profile_->GetRequestContext());
}
void UserPolicySigninService::InitializeUserCloudPolicyManager(
const std::string& username,
scoped_ptr<CloudPolicyClient> client) {
UserPolicySigninServiceBase::InitializeUserCloudPolicyManager(username,
client.Pass());
ProhibitSignoutIfNeeded();
}
void UserPolicySigninService::ShutdownUserCloudPolicyManager() {
UserCloudPolicyManager* manager = policy_manager();
if (manager)
signin_manager()->ProhibitSignout(false);
UserPolicySigninServiceBase::ShutdownUserCloudPolicyManager();
}
void UserPolicySigninService::OnInitializationCompleted(
CloudPolicyService* service) {
UserCloudPolicyManager* manager = policy_manager();
DCHECK_EQ(service, manager->core()->service());
DCHECK(service->IsInitializationComplete());
DVLOG_IF(1, manager->IsClientRegistered())
<< "Client already registered - not fetching DMToken";
if (!manager->IsClientRegistered()) {
if (!oauth2_token_service_->RefreshTokenIsAvailable(
signin_manager()->GetAuthenticatedAccountId())) {
DLOG(WARNING) << "No OAuth Refresh Token - delaying policy download";
return;
}
RegisterCloudPolicyService();
}
ProhibitSignoutIfNeeded();
}
void UserPolicySigninService::RegisterCloudPolicyService() {
DCHECK(!policy_manager()->IsClientRegistered());
DVLOG(1) << "Fetching new DM Token";
if (registration_helper_)
return;
registration_helper_.reset(new CloudPolicyClientRegistrationHelper(
policy_manager()->core()->client(),
enterprise_management::DeviceRegisterRequest::BROWSER));
registration_helper_->StartRegistration(
oauth2_token_service_,
signin_manager()->GetAuthenticatedAccountId(),
base::Bind(&UserPolicySigninService::OnRegistrationComplete,
base::Unretained(this)));
}
void UserPolicySigninService::OnRegistrationComplete() {
ProhibitSignoutIfNeeded();
registration_helper_.reset();
}
void UserPolicySigninService::ProhibitSignoutIfNeeded() {
if (policy_manager()->IsClientRegistered()) {
DVLOG(1) << "User is registered for policy - prohibiting signout";
signin_manager()->ProhibitSignout(true);
}
}
}