This source file includes following definitions.
- weak_factory_
- FetchPolicyForSignedInUser
- GoogleSignedOut
- Observe
- OnInitializationCompleted
- OnPolicyFetched
- OnRegistrationStateChanged
- OnClientError
- Shutdown
- PrepareForUserCloudPolicyManagerShutdown
- CreateClientForRegistrationOnly
- ShouldLoadPolicyForUser
- InitializeOnProfileReady
- InitializeForSignedInUser
- InitializeUserCloudPolicyManager
- ShutdownUserCloudPolicyManager
- CreateSystemRequestContext
- CreateUserRequestContext
#include "chrome/browser/policy/cloud/user_policy_signin_service_base.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "base/message_loop/message_loop.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/policy/cloud/user_cloud_policy_manager_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/signin_manager_factory.h"
#include "chrome/common/chrome_content_client.h"
#include "chrome/common/chrome_switches.h"
#include "components/policy/core/browser/browser_policy_connector.h"
#include "components/policy/core/common/cloud/device_management_service.h"
#include "components/policy/core/common/cloud/system_policy_request_context.h"
#include "components/policy/core/common/cloud/user_cloud_policy_manager.h"
#include "components/policy/core/common/cloud/user_policy_request_context.h"
#include "components/signin/core/browser/signin_manager.h"
#include "content/public/browser/notification_source.h"
#include "net/url_request/url_request_context_getter.h"
namespace policy {
UserPolicySigninServiceBase::UserPolicySigninServiceBase(
Profile* profile,
PrefService* local_state,
DeviceManagementService* device_management_service,
UserCloudPolicyManager* policy_manager,
SigninManager* signin_manager,
scoped_refptr<net::URLRequestContextGetter> system_request_context)
: policy_manager_(policy_manager),
signin_manager_(signin_manager),
local_state_(local_state),
device_management_service_(device_management_service),
system_request_context_(system_request_context),
weak_factory_(this) {
registrar_.Add(this,
chrome::NOTIFICATION_PROFILE_ADDED,
content::Source<Profile>(profile));
}
UserPolicySigninServiceBase::~UserPolicySigninServiceBase() {}
void UserPolicySigninServiceBase::FetchPolicyForSignedInUser(
const std::string& username,
const std::string& dm_token,
const std::string& client_id,
scoped_refptr<net::URLRequestContextGetter> profile_request_context,
const PolicyFetchCallback& callback) {
scoped_ptr<CloudPolicyClient> client(
UserCloudPolicyManager::CreateCloudPolicyClient(
device_management_service_,
CreateUserRequestContext(profile_request_context)).Pass());
client->SetupRegistration(dm_token, client_id);
DCHECK(client->is_registered());
UserCloudPolicyManager* manager = policy_manager();
DCHECK(manager);
DCHECK(!manager->core()->client());
InitializeUserCloudPolicyManager(username, client.Pass());
DCHECK(manager->IsClientRegistered());
manager->core()->service()->RefreshPolicy(callback);
}
void UserPolicySigninServiceBase::GoogleSignedOut(const std::string& username) {
ShutdownUserCloudPolicyManager();
}
void UserPolicySigninServiceBase::Observe(
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
switch (type) {
case chrome::NOTIFICATION_PROFILE_ADDED:
InitializeOnProfileReady(content::Source<Profile>(source).ptr());
break;
default:
NOTREACHED();
}
}
void UserPolicySigninServiceBase::OnInitializationCompleted(
CloudPolicyService* service) {
}
void UserPolicySigninServiceBase::OnPolicyFetched(CloudPolicyClient* client) {}
void UserPolicySigninServiceBase::OnRegistrationStateChanged(
CloudPolicyClient* client) {}
void UserPolicySigninServiceBase::OnClientError(CloudPolicyClient* client) {
if (client->is_registered()) {
if (client->status() == DM_STATUS_SERVICE_MANAGEMENT_NOT_SUPPORTED) {
DVLOG(1) << "DMServer returned NOT_SUPPORTED error - removing policy";
base::MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(
&UserPolicySigninServiceBase::ShutdownUserCloudPolicyManager,
weak_factory_.GetWeakPtr()));
} else {
DVLOG(1) << "Error fetching policy: " << client->status();
}
}
}
void UserPolicySigninServiceBase::Shutdown() {
if (signin_manager())
signin_manager()->RemoveObserver(this);
PrepareForUserCloudPolicyManagerShutdown();
}
void UserPolicySigninServiceBase::PrepareForUserCloudPolicyManagerShutdown() {
UserCloudPolicyManager* manager = policy_manager();
if (manager && manager->core()->client())
manager->core()->client()->RemoveObserver(this);
if (manager && manager->core()->service())
manager->core()->service()->RemoveObserver(this);
}
scoped_ptr<CloudPolicyClient>
UserPolicySigninServiceBase::CreateClientForRegistrationOnly(
const std::string& username) {
DCHECK(!username.empty());
DCHECK(!policy_manager() || !policy_manager()->core()->client());
if (!policy_manager() || !ShouldLoadPolicyForUser(username)) {
DVLOG(1) << "Signed in user is not in the whitelist";
return scoped_ptr<CloudPolicyClient>();
}
device_management_service_->ScheduleInitialization(0);
return UserCloudPolicyManager::CreateCloudPolicyClient(
device_management_service_, CreateSystemRequestContext());
}
bool UserPolicySigninServiceBase::ShouldLoadPolicyForUser(
const std::string& username) {
if (username.empty())
return false;
return !BrowserPolicyConnector::IsNonEnterpriseUser(username);
}
void UserPolicySigninServiceBase::InitializeOnProfileReady(Profile* profile) {
if (!policy_manager() || !signin_manager()) {
DVLOG(1) << "Skipping initialization for tests due to missing components.";
return;
}
signin_manager()->AddObserver(this);
std::string username = signin_manager()->GetAuthenticatedUsername();
if (username.empty())
ShutdownUserCloudPolicyManager();
else
InitializeForSignedInUser(username, profile->GetRequestContext());
}
void UserPolicySigninServiceBase::InitializeForSignedInUser(
const std::string& username,
scoped_refptr<net::URLRequestContextGetter> profile_request_context) {
DCHECK(!username.empty());
if (!ShouldLoadPolicyForUser(username)) {
DVLOG(1) << "Policy load not enabled for user: " << username;
return;
}
UserCloudPolicyManager* manager = policy_manager();
if (!manager->core()->service()) {
InitializeUserCloudPolicyManager(
username,
UserCloudPolicyManager::CreateCloudPolicyClient(
device_management_service_,
CreateUserRequestContext(profile_request_context)).Pass());
} else {
manager->SetSigninUsername(username);
}
if (manager->core()->service()->IsInitializationComplete())
OnInitializationCompleted(manager->core()->service());
}
void UserPolicySigninServiceBase::InitializeUserCloudPolicyManager(
const std::string& username,
scoped_ptr<CloudPolicyClient> client) {
DCHECK(client);
UserCloudPolicyManager* manager = policy_manager();
manager->SetSigninUsername(username);
DCHECK(!manager->core()->client());
scoped_refptr<net::URLRequestContextGetter> context =
client->GetRequestContext();
manager->Connect(local_state_, context, client.Pass());
DCHECK(manager->core()->service());
manager->core()->client()->AddObserver(this);
manager->core()->service()->AddObserver(this);
}
void UserPolicySigninServiceBase::ShutdownUserCloudPolicyManager() {
PrepareForUserCloudPolicyManagerShutdown();
UserCloudPolicyManager* manager = policy_manager();
if (manager)
manager->DisconnectAndRemovePolicy();
}
scoped_refptr<net::URLRequestContextGetter>
UserPolicySigninServiceBase::CreateSystemRequestContext() {
return new SystemPolicyRequestContext(
system_request_context(), GetUserAgent());
}
scoped_refptr<net::URLRequestContextGetter>
UserPolicySigninServiceBase::CreateUserRequestContext(
scoped_refptr<net::URLRequestContextGetter> profile_request_context) {
return new UserPolicyRequestContext(
profile_request_context, system_request_context(), GetUserAgent());
}
}