Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / chromeos / policy / consumer_unenrollment_handler.cc
blobbd7dfaae0e8374c8a730b578b32b4ff03d7ca177
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/policy/consumer_unenrollment_handler.h"
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "chrome/browser/chromeos/ownership/owner_settings_service_chromeos.h"
10 #include "chrome/browser/chromeos/policy/consumer_management_service.h"
11 #include "chrome/browser/chromeos/policy/consumer_management_stage.h"
12 #include "chrome/browser/chromeos/policy/device_cloud_policy_manager_chromeos.h"
13 #include "chrome/browser/chromeos/settings/device_settings_service.h"
14 #include "components/policy/core/common/cloud/cloud_policy_constants.h"
15 #include "policy/proto/device_management_backend.pb.h"
17 namespace em = enterprise_management;
19 namespace policy {
21 ConsumerUnenrollmentHandler::ConsumerUnenrollmentHandler(
22 chromeos::DeviceSettingsService* device_settings_service,
23 ConsumerManagementService* consumer_management_service,
24 DeviceCloudPolicyManagerChromeOS* device_cloud_policy_manager,
25 chromeos::OwnerSettingsServiceChromeOS* owner_settings_service)
26 : device_settings_service_(device_settings_service),
27 consumer_management_service_(consumer_management_service),
28 device_cloud_policy_manager_(device_cloud_policy_manager),
29 owner_settings_service_(owner_settings_service),
30 weak_factory_(this) {
33 ConsumerUnenrollmentHandler::~ConsumerUnenrollmentHandler() {
36 void ConsumerUnenrollmentHandler::Start() {
37 if (consumer_management_service_->GetStatus() !=
38 ConsumerManagementService::STATUS_ENROLLED) {
39 return;
42 device_cloud_policy_manager_->Unregister(
43 base::Bind(&ConsumerUnenrollmentHandler::OnUnregistered,
44 weak_factory_.GetWeakPtr()));
47 void ConsumerUnenrollmentHandler::OnUnregistered(bool success) {
48 if (!success) {
49 consumer_management_service_->SetStage(
50 ConsumerManagementStage::UnenrollmentDMServerFailed());
51 LOG(ERROR) << "Failed to unregister and disconnect device cloud policy "
52 << "manager.";
53 return;
57 chromeos::OwnerSettingsServiceChromeOS::ManagementSettings settings;
58 settings.management_mode = MANAGEMENT_MODE_LOCAL_OWNER;
59 owner_settings_service_->SetManagementSettings(
60 settings,
61 base::Bind(&ConsumerUnenrollmentHandler::OnManagementSettingsSet,
62 weak_factory_.GetWeakPtr()));
65 void ConsumerUnenrollmentHandler::OnManagementSettingsSet(bool success) {
66 if (!success) {
67 consumer_management_service_->SetStage(
68 ConsumerManagementStage::UnenrollmentUpdateDeviceSettingsFailed());
69 LOG(ERROR) << "Failed to unset request token and device ID.";
70 return;
73 consumer_management_service_->SetStage(
74 ConsumerManagementStage::UnenrollmentSuccess());
76 // Disconnecting the device cloud policy manager will restart the device
77 // cloud policy initializer. So this must be done after the management
78 // settings are updated, so that the initializer won't reconnect the manager.
79 device_cloud_policy_manager_->Disconnect();
82 } // namespace policy