This source file includes following definitions.
- GetSanitizedArg
- GetMode
- weak_factory_
- Start
- Cancel
- Retry
- RegisterProgressCallback
- ShouldEnrollSilently
- OnOwnershipStatusCheckDone
- UpdateState
#include "chrome/browser/chromeos/login/enrollment/auto_enrollment_controller.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/command_line.h"
#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chromeos/policy/device_cloud_policy_manager_chromeos.h"
#include "chromeos/chromeos_switches.h"
#include "components/policy/core/browser/browser_policy_connector.h"
#include "components/policy/core/common/cloud/device_management_service.h"
#include "net/url_request/url_request_context_getter.h"
namespace chromeos {
namespace {
int GetSanitizedArg(const std::string& switch_name) {
CommandLine* command_line = CommandLine::ForCurrentProcess();
if (!command_line->HasSwitch(switch_name))
return 0;
std::string value = command_line->GetSwitchValueASCII(switch_name);
int int_value;
if (!base::StringToInt(value, &int_value)) {
LOG(ERROR) << "Switch \"" << switch_name << "\" is not a valid int. "
<< "Defaulting to 0.";
return 0;
}
if (int_value < 0) {
LOG(ERROR) << "Switch \"" << switch_name << "\" can't be negative. "
<< "Using 0";
return 0;
}
if (int_value > policy::AutoEnrollmentClient::kMaximumPower) {
LOG(ERROR) << "Switch \"" << switch_name << "\" can't be greater than "
<< policy::AutoEnrollmentClient::kMaximumPower << ". Using "
<< policy::AutoEnrollmentClient::kMaximumPower;
return policy::AutoEnrollmentClient::kMaximumPower;
}
return int_value;
}
}
const char AutoEnrollmentController::kForcedReEnrollmentAlways[] = "always";
const char AutoEnrollmentController::kForcedReEnrollmentLegacy[] = "legacy";
const char AutoEnrollmentController::kForcedReEnrollmentNever[] = "never";
const char AutoEnrollmentController::kForcedReEnrollmentOfficialBuild[] =
"official";
AutoEnrollmentController::Mode AutoEnrollmentController::GetMode() {
CommandLine* command_line = CommandLine::ForCurrentProcess();
if (!command_line->HasSwitch(switches::kEnterpriseEnableForcedReEnrollment))
return MODE_LEGACY_AUTO_ENROLLMENT;
std::string command_line_mode = command_line->GetSwitchValueASCII(
switches::kEnterpriseEnableForcedReEnrollment);
if (command_line_mode == kForcedReEnrollmentAlways) {
return MODE_FORCED_RE_ENROLLMENT;
} else if (command_line_mode.empty() ||
command_line_mode == kForcedReEnrollmentOfficialBuild) {
#if defined(OFFICIAL_BUILD)
return MODE_FORCED_RE_ENROLLMENT;
#else
return MODE_NONE;
#endif
} else if (command_line_mode == kForcedReEnrollmentLegacy) {
return MODE_LEGACY_AUTO_ENROLLMENT;
}
return MODE_NONE;
}
AutoEnrollmentController::AutoEnrollmentController()
: state_(policy::AUTO_ENROLLMENT_STATE_IDLE),
weak_factory_(this) {}
AutoEnrollmentController::~AutoEnrollmentController() {}
void AutoEnrollmentController::Start() {
CommandLine* command_line = CommandLine::ForCurrentProcess();
if (command_line->HasSwitch(chromeos::switches::kOobeSkipPostLogin) ||
(!command_line->HasSwitch(
chromeos::switches::kEnterpriseEnrollmentInitialModulus) &&
!command_line->HasSwitch(
chromeos::switches::kEnterpriseEnrollmentModulusLimit)) ||
GetMode() == MODE_NONE) {
VLOG(1) << "Auto-enrollment disabled.";
UpdateState(policy::AUTO_ENROLLMENT_STATE_NO_ENROLLMENT);
return;
}
if (client_)
return;
UpdateState(policy::AUTO_ENROLLMENT_STATE_PENDING);
weak_factory_.InvalidateWeakPtrs();
DeviceSettingsService::Get()->GetOwnershipStatusAsync(
base::Bind(&AutoEnrollmentController::OnOwnershipStatusCheckDone,
weak_factory_.GetWeakPtr()));
}
void AutoEnrollmentController::Cancel() {
if (client_) {
client_.release()->CancelAndDeleteSoon();
}
}
void AutoEnrollmentController::Retry() {
if (client_)
client_->Retry();
}
scoped_ptr<AutoEnrollmentController::ProgressCallbackList::Subscription>
AutoEnrollmentController::RegisterProgressCallback(
const ProgressCallbackList::CallbackType& callback) {
return progress_callbacks_.Add(callback);
}
bool AutoEnrollmentController::ShouldEnrollSilently() {
return state_ == policy::AUTO_ENROLLMENT_STATE_TRIGGER_ENROLLMENT &&
GetMode() == MODE_LEGACY_AUTO_ENROLLMENT;
}
void AutoEnrollmentController::OnOwnershipStatusCheckDone(
DeviceSettingsService::OwnershipStatus status) {
if (status != DeviceSettingsService::OWNERSHIP_NONE) {
VLOG(1) << "Device already owned, skipping auto-enrollment check";
UpdateState(policy::AUTO_ENROLLMENT_STATE_NO_ENROLLMENT);
return;
}
policy::BrowserPolicyConnector* connector =
g_browser_process->browser_policy_connector();
policy::DeviceManagementService* service =
connector->device_management_service();
service->ScheduleInitialization(0);
int power_initial = GetSanitizedArg(
chromeos::switches::kEnterpriseEnrollmentInitialModulus);
int power_limit = GetSanitizedArg(
chromeos::switches::kEnterpriseEnrollmentModulusLimit);
if (power_initial > power_limit) {
LOG(ERROR) << "Initial auto-enrollment modulus is larger than the limit, "
<< "clamping to the limit.";
power_initial = power_limit;
}
bool retrieve_device_state = false;
std::string device_id;
if (GetMode() == MODE_FORCED_RE_ENROLLMENT) {
retrieve_device_state = true;
device_id =
policy::DeviceCloudPolicyManagerChromeOS::GetCurrentDeviceStateKey();
} else {
device_id = policy::DeviceCloudPolicyManagerChromeOS::GetMachineID();
}
client_.reset(new policy::AutoEnrollmentClient(
base::Bind(&AutoEnrollmentController::UpdateState,
base::Unretained(this)),
service,
g_browser_process->local_state(),
g_browser_process->system_request_context(),
device_id,
retrieve_device_state,
power_initial,
power_limit));
VLOG(1) << "Starting auto-enrollment client.";
client_->Start();
}
void AutoEnrollmentController::UpdateState(
policy::AutoEnrollmentState new_state) {
state_ = new_state;
progress_callbacks_.Notify(state_);
}
}