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 "chromeos/dbus/power_policy_controller.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 const char kLidCloseAction
[] = "LidCloseAction";
26 const char kUserActivityScreenDimDelayScale
[] =
27 "UserActivityScreenDimDelayScale";
29 const char kActionSuspend
[] = "Suspend";
30 const char kActionLogout
[] = "Logout";
31 const char kActionShutdown
[] = "Shutdown";
32 const char kActionDoNothing
[] = "DoNothing";
34 scoped_ptr
<base::Value
> GetAction(const std::string
&action
) {
35 if (action
== kActionSuspend
) {
36 return scoped_ptr
<base::Value
>(new base::FundamentalValue(
37 chromeos::PowerPolicyController::ACTION_SUSPEND
));
39 if (action
== kActionLogout
) {
40 return scoped_ptr
<base::Value
>(new base::FundamentalValue(
41 chromeos::PowerPolicyController::ACTION_STOP_SESSION
));
43 if (action
== kActionShutdown
) {
44 return scoped_ptr
<base::Value
>(new base::FundamentalValue(
45 chromeos::PowerPolicyController::ACTION_SHUT_DOWN
));
47 if (action
== kActionDoNothing
) {
48 return scoped_ptr
<base::Value
>(new base::FundamentalValue(
49 chromeos::PowerPolicyController::ACTION_DO_NOTHING
));
51 return scoped_ptr
<base::Value
>();
55 // Applies the value of |device_policy| in |device_policy_map| as the
56 // recommended value of |user_policy| in |user_policy_map|. If the value of
57 // |device_policy| is unset, does nothing.
58 void ApplyDevicePolicyAsRecommendedPolicy(const std::string
& device_policy
,
59 const std::string
& user_policy
,
60 const PolicyMap
& device_policy_map
,
61 PolicyMap
* user_policy_map
) {
62 const base::Value
* value
= device_policy_map
.GetValue(device_policy
);
64 user_policy_map
->Set(user_policy
,
65 POLICY_LEVEL_RECOMMENDED
,
73 // Applies |value| as the mandatory value of |user_policy| in |user_policy_map|.
74 // If |value| is NULL, does nothing.
75 void ApplyValueAsMandatoryPolicy(const base::Value
* value
,
76 const std::string
& user_policy
,
77 PolicyMap
* user_policy_map
) {
79 user_policy_map
->Set(user_policy
,
80 POLICY_LEVEL_MANDATORY
,
90 LoginProfilePolicyProvider::LoginProfilePolicyProvider(
91 PolicyService
* device_policy_service
)
92 : device_policy_service_(device_policy_service
),
93 waiting_for_device_policy_refresh_(false),
97 LoginProfilePolicyProvider::~LoginProfilePolicyProvider() {
100 void LoginProfilePolicyProvider::Init(SchemaRegistry
* registry
) {
101 ConfigurationPolicyProvider::Init(registry
);
102 device_policy_service_
->AddObserver(POLICY_DOMAIN_CHROME
, this);
103 if (device_policy_service_
->IsInitializationComplete(POLICY_DOMAIN_CHROME
))
104 UpdateFromDevicePolicy();
107 void LoginProfilePolicyProvider::Shutdown() {
108 device_policy_service_
->RemoveObserver(POLICY_DOMAIN_CHROME
, this);
109 weak_factory_
.InvalidateWeakPtrs();
110 ConfigurationPolicyProvider::Shutdown();
113 void LoginProfilePolicyProvider::RefreshPolicies() {
114 waiting_for_device_policy_refresh_
= true;
115 weak_factory_
.InvalidateWeakPtrs();
116 device_policy_service_
->RefreshPolicies(base::Bind(
117 &LoginProfilePolicyProvider::OnDevicePolicyRefreshDone
,
118 weak_factory_
.GetWeakPtr()));
121 void LoginProfilePolicyProvider::OnPolicyUpdated(const PolicyNamespace
& ns
,
122 const PolicyMap
& previous
,
123 const PolicyMap
& current
) {
124 if (ns
== PolicyNamespace(POLICY_DOMAIN_CHROME
, std::string()))
125 UpdateFromDevicePolicy();
128 void LoginProfilePolicyProvider::OnPolicyServiceInitialized(
129 PolicyDomain domain
) {
130 if (domain
== POLICY_DOMAIN_CHROME
)
131 UpdateFromDevicePolicy();
134 void LoginProfilePolicyProvider::OnDevicePolicyRefreshDone() {
135 waiting_for_device_policy_refresh_
= false;
136 UpdateFromDevicePolicy();
139 void LoginProfilePolicyProvider::UpdateFromDevicePolicy() {
140 // If a policy refresh is in progress, wait for it to finish.
141 if (waiting_for_device_policy_refresh_
)
144 const PolicyNamespace
chrome_namespaces(POLICY_DOMAIN_CHROME
, std::string());
145 const PolicyMap
& device_policy_map
=
146 device_policy_service_
->GetPolicies(chrome_namespaces
);
147 scoped_ptr
<PolicyBundle
> bundle(new PolicyBundle
);
148 PolicyMap
& user_policy_map
= bundle
->Get(chrome_namespaces
);
150 ApplyDevicePolicyAsRecommendedPolicy(
151 key::kDeviceLoginScreenDefaultLargeCursorEnabled
,
152 key::kLargeCursorEnabled
,
153 device_policy_map
, &user_policy_map
);
154 ApplyDevicePolicyAsRecommendedPolicy(
155 key::kDeviceLoginScreenDefaultSpokenFeedbackEnabled
,
156 key::kSpokenFeedbackEnabled
,
157 device_policy_map
, &user_policy_map
);
158 ApplyDevicePolicyAsRecommendedPolicy(
159 key::kDeviceLoginScreenDefaultHighContrastEnabled
,
160 key::kHighContrastEnabled
,
161 device_policy_map
, &user_policy_map
);
162 ApplyDevicePolicyAsRecommendedPolicy(
163 key::kDeviceLoginScreenDefaultScreenMagnifierType
,
164 key::kScreenMagnifierType
,
165 device_policy_map
, &user_policy_map
);
166 ApplyDevicePolicyAsRecommendedPolicy(
167 key::kDeviceLoginScreenDefaultVirtualKeyboardEnabled
,
168 key::kVirtualKeyboardEnabled
,
169 device_policy_map
, &user_policy_map
);
171 const base::Value
* value
=
172 device_policy_map
.GetValue(key::kDeviceLoginScreenPowerManagement
);
173 const base::DictionaryValue
* dict
= NULL
;
174 if (value
&& value
->GetAsDictionary(&dict
)) {
175 scoped_ptr
<base::DictionaryValue
> policy_value(dict
->DeepCopy());
176 std::string lid_close_action
;
177 base::Value
* screen_dim_delay_scale
= NULL
;
179 if (policy_value
->GetString(kLidCloseAction
, &lid_close_action
)) {
180 scoped_ptr
<base::Value
> action
= GetAction(lid_close_action
);
182 ApplyValueAsMandatoryPolicy(
183 action
.get(), key::kLidCloseAction
, &user_policy_map
);
185 policy_value
->Remove(kLidCloseAction
, NULL
);
188 if (policy_value
->Get(kUserActivityScreenDimDelayScale
,
189 &screen_dim_delay_scale
)) {
190 ApplyValueAsMandatoryPolicy(screen_dim_delay_scale
,
191 key::kUserActivityScreenDimDelayScale
,
193 policy_value
->Remove(kUserActivityScreenDimDelayScale
, NULL
);
196 // |policy_value| is expected to be a valid value for the
197 // PowerManagementIdleSettings policy now.
198 if (!policy_value
->empty()) {
199 ApplyValueAsMandatoryPolicy(policy_value
.get(),
200 key::kPowerManagementIdleSettings
,
205 UpdatePolicy(bundle
.Pass());
208 } // namespace policy