1 // Copyright 2013 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/login_profile_policy_provider.h"
10 #include "base/callback.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/values.h"
13 #include "chrome/browser/chromeos/policy/login_screen_power_management_policy.h"
14 #include "components/policy/core/browser/policy_error_map.h"
15 #include "components/policy/core/common/external_data_fetcher.h"
16 #include "components/policy/core/common/policy_bundle.h"
17 #include "components/policy/core/common/policy_map.h"
18 #include "components/policy/core/common/policy_types.h"
19 #include "policy/policy_constants.h"
25 // Applies the value of |device_policy| in |device_policy_map| as the
26 // recommended value of |user_policy| in |user_policy_map|. If the value of
27 // |device_policy| is unset, does nothing.
28 void ApplyDevicePolicyAsRecommendedPolicy(const std::string
& device_policy
,
29 const std::string
& user_policy
,
30 const PolicyMap
& device_policy_map
,
31 PolicyMap
* user_policy_map
) {
32 const base::Value
* value
= device_policy_map
.GetValue(device_policy
);
34 user_policy_map
->Set(user_policy
,
35 POLICY_LEVEL_RECOMMENDED
,
42 // Applies |value| as the mandatory value of |user_policy| in |user_policy_map|.
43 // If |value| is NULL, does nothing.
44 void ApplyValueAsMandatoryPolicy(const base::Value
* value
,
45 const std::string
& user_policy
,
46 PolicyMap
* user_policy_map
) {
48 user_policy_map
->Set(user_policy
,
49 POLICY_LEVEL_MANDATORY
,
58 LoginProfilePolicyProvider::LoginProfilePolicyProvider(
59 PolicyService
* device_policy_service
)
60 : device_policy_service_(device_policy_service
),
61 waiting_for_device_policy_refresh_(false),
65 LoginProfilePolicyProvider::~LoginProfilePolicyProvider() {
68 void LoginProfilePolicyProvider::Init(SchemaRegistry
* registry
) {
69 ConfigurationPolicyProvider::Init(registry
);
70 device_policy_service_
->AddObserver(POLICY_DOMAIN_CHROME
, this);
71 if (device_policy_service_
->IsInitializationComplete(POLICY_DOMAIN_CHROME
))
72 UpdateFromDevicePolicy();
75 void LoginProfilePolicyProvider::Shutdown() {
76 device_policy_service_
->RemoveObserver(POLICY_DOMAIN_CHROME
, this);
77 weak_factory_
.InvalidateWeakPtrs();
78 ConfigurationPolicyProvider::Shutdown();
81 void LoginProfilePolicyProvider::RefreshPolicies() {
82 waiting_for_device_policy_refresh_
= true;
83 weak_factory_
.InvalidateWeakPtrs();
84 device_policy_service_
->RefreshPolicies(base::Bind(
85 &LoginProfilePolicyProvider::OnDevicePolicyRefreshDone
,
86 weak_factory_
.GetWeakPtr()));
89 void LoginProfilePolicyProvider::OnPolicyUpdated(const PolicyNamespace
& ns
,
90 const PolicyMap
& previous
,
91 const PolicyMap
& current
) {
92 if (ns
== PolicyNamespace(POLICY_DOMAIN_CHROME
, std::string()))
93 UpdateFromDevicePolicy();
96 void LoginProfilePolicyProvider::OnPolicyServiceInitialized(
97 PolicyDomain domain
) {
98 if (domain
== POLICY_DOMAIN_CHROME
)
99 UpdateFromDevicePolicy();
102 void LoginProfilePolicyProvider::OnDevicePolicyRefreshDone() {
103 waiting_for_device_policy_refresh_
= false;
104 UpdateFromDevicePolicy();
107 void LoginProfilePolicyProvider::UpdateFromDevicePolicy() {
108 // If a policy refresh is in progress, wait for it to finish.
109 if (waiting_for_device_policy_refresh_
)
112 const PolicyNamespace
chrome_namespaces(POLICY_DOMAIN_CHROME
, std::string());
113 const PolicyMap
& device_policy_map
=
114 device_policy_service_
->GetPolicies(chrome_namespaces
);
115 scoped_ptr
<PolicyBundle
> bundle(new PolicyBundle
);
116 PolicyMap
& user_policy_map
= bundle
->Get(chrome_namespaces
);
118 ApplyDevicePolicyAsRecommendedPolicy(
119 key::kDeviceLoginScreenDefaultLargeCursorEnabled
,
120 key::kLargeCursorEnabled
,
121 device_policy_map
, &user_policy_map
);
122 ApplyDevicePolicyAsRecommendedPolicy(
123 key::kDeviceLoginScreenDefaultSpokenFeedbackEnabled
,
124 key::kSpokenFeedbackEnabled
,
125 device_policy_map
, &user_policy_map
);
126 ApplyDevicePolicyAsRecommendedPolicy(
127 key::kDeviceLoginScreenDefaultHighContrastEnabled
,
128 key::kHighContrastEnabled
,
129 device_policy_map
, &user_policy_map
);
130 ApplyDevicePolicyAsRecommendedPolicy(
131 key::kDeviceLoginScreenDefaultScreenMagnifierType
,
132 key::kScreenMagnifierType
,
133 device_policy_map
, &user_policy_map
);
135 // TODO(bartfab): Consolidate power management user policies into a single
136 // JSON policy, allowing the value of the device policy to be simply forwarded
137 // here. http://crbug.com/258339
138 const base::Value
* value
=
139 device_policy_map
.GetValue(key::kDeviceLoginScreenPowerManagement
);
141 if (value
&& value
->GetAsString(&json
)) {
142 LoginScreenPowerManagementPolicy power_management_policy
;
143 power_management_policy
.Init(json
, NULL
);
144 ApplyValueAsMandatoryPolicy(power_management_policy
.GetScreenDimDelayAC(),
145 key::kScreenDimDelayAC
,
147 ApplyValueAsMandatoryPolicy(power_management_policy
.GetScreenOffDelayAC(),
148 key::kScreenOffDelayAC
,
150 ApplyValueAsMandatoryPolicy(power_management_policy
.GetIdleDelayAC(),
153 ApplyValueAsMandatoryPolicy(
154 power_management_policy
.GetScreenDimDelayBattery(),
155 key::kScreenDimDelayBattery
,
157 ApplyValueAsMandatoryPolicy(
158 power_management_policy
.GetScreenOffDelayBattery(),
159 key::kScreenOffDelayBattery
,
161 ApplyValueAsMandatoryPolicy(power_management_policy
.GetIdleDelayBattery(),
162 key::kIdleDelayBattery
,
164 ApplyValueAsMandatoryPolicy(power_management_policy
.GetIdleActionAC(),
167 ApplyValueAsMandatoryPolicy(power_management_policy
.GetIdleActionBattery(),
168 key::kIdleActionBattery
,
170 ApplyValueAsMandatoryPolicy(power_management_policy
.GetLidCloseAction(),
171 key::kLidCloseAction
,
173 ApplyValueAsMandatoryPolicy(
174 power_management_policy
.GetUserActivityScreenDimDelayScale(),
175 key::kUserActivityScreenDimDelayScale
,
179 UpdatePolicy(bundle
.Pass());
182 } // namespace policy