OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/webui/policy_ui.h" | 5 #include "chrome/browser/ui/webui/policy_ui.h" |
6 | 6 |
7 #include <vector> | |
8 | |
9 #include "base/bind.h" | 7 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
11 #include "base/callback.h" | 9 #include "base/callback.h" |
12 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
13 #include "base/hash_tables.h" | 11 #include "base/logging.h" |
14 #include "base/stl_util.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/string16.h" |
15 #include "base/time.h" | 15 #include "base/time.h" |
16 #include "base/utf_string_conversions.h" | 16 #include "base/values.h" |
17 #include "chrome/browser/browser_process.h" | 17 #include "chrome/browser/browser_process.h" |
18 #include "chrome/browser/policy/browser_policy_connector.h" | 18 #include "chrome/browser/policy/browser_policy_connector.h" |
19 #include "chrome/browser/policy/cloud_policy_client.h" | 19 #include "chrome/browser/policy/cloud_policy_client.h" |
| 20 #include "chrome/browser/policy/cloud_policy_constants.h" |
20 #include "chrome/browser/policy/cloud_policy_core.h" | 21 #include "chrome/browser/policy/cloud_policy_core.h" |
21 #include "chrome/browser/policy/cloud_policy_refresh_scheduler.h" | 22 #include "chrome/browser/policy/cloud_policy_refresh_scheduler.h" |
22 #include "chrome/browser/policy/cloud_policy_store.h" | 23 #include "chrome/browser/policy/cloud_policy_store.h" |
23 #include "chrome/browser/policy/cloud_policy_validator.h" | 24 #include "chrome/browser/policy/cloud_policy_validator.h" |
| 25 #include "chrome/browser/policy/configuration_policy_handler_list.h" |
| 26 #include "chrome/browser/policy/device_local_account_policy_service.h" |
24 #include "chrome/browser/policy/message_util.h" | 27 #include "chrome/browser/policy/message_util.h" |
25 #include "chrome/browser/policy/policy_error_map.h" | 28 #include "chrome/browser/policy/policy_error_map.h" |
| 29 #include "chrome/browser/policy/policy_map.h" |
26 #include "chrome/browser/policy/policy_service.h" | 30 #include "chrome/browser/policy/policy_service.h" |
| 31 #include "chrome/browser/policy/policy_types.h" |
27 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | 32 #include "chrome/browser/policy/proto/device_management_backend.pb.h" |
28 #include "chrome/browser/prefs/pref_service.h" | |
29 #include "chrome/browser/profiles/profile.h" | 33 #include "chrome/browser/profiles/profile.h" |
30 #include "chrome/common/pref_names.h" | |
31 #include "chrome/common/time_format.h" | 34 #include "chrome/common/time_format.h" |
32 #include "chrome/common/url_constants.h" | 35 #include "chrome/common/url_constants.h" |
33 #include "content/public/browser/web_contents.h" | |
34 #include "content/public/browser/web_ui.h" | 36 #include "content/public/browser/web_ui.h" |
35 #include "content/public/browser/web_ui_data_source.h" | 37 #include "content/public/browser/web_ui_data_source.h" |
| 38 #include "content/public/browser/web_ui_message_handler.h" |
| 39 #include "google_apis/gaia/gaia_auth_util.h" |
36 #include "grit/browser_resources.h" | 40 #include "grit/browser_resources.h" |
37 #include "grit/generated_resources.h" | 41 #include "grit/generated_resources.h" |
| 42 #include "policy/policy_constants.h" |
38 #include "ui/base/l10n/l10n_util.h" | 43 #include "ui/base/l10n/l10n_util.h" |
39 | 44 |
40 #if defined(OS_CHROMEOS) | 45 #if defined(OS_CHROMEOS) |
41 #include "chrome/browser/chromeos/login/user_manager.h" | 46 #include "chrome/browser/chromeos/login/user_manager.h" |
42 #include "chrome/browser/policy/device_cloud_policy_manager_chromeos.h" | 47 #include "chrome/browser/policy/device_cloud_policy_manager_chromeos.h" |
43 #include "chrome/browser/policy/device_local_account_policy_service.h" | 48 #include "chrome/browser/policy/device_local_account_policy_service.h" |
44 #include "chrome/browser/policy/user_cloud_policy_manager_chromeos.h" | 49 #include "chrome/browser/policy/user_cloud_policy_manager_chromeos.h" |
45 #else | 50 #else |
46 #include "chrome/browser/policy/user_cloud_policy_manager.h" | 51 #include "chrome/browser/policy/user_cloud_policy_manager.h" |
47 #include "chrome/browser/policy/user_cloud_policy_manager_factory.h" | 52 #include "chrome/browser/policy/user_cloud_policy_manager_factory.h" |
48 #endif | 53 #endif |
49 | 54 |
50 namespace em = enterprise_management; | 55 namespace em = enterprise_management; |
51 | 56 |
52 const char PolicyUIHandler::kLevel[] = "level"; | |
53 const char PolicyUIHandler::kName[] = "name"; | |
54 const char PolicyUIHandler::kScope[] = "scope"; | |
55 const char PolicyUIHandler::kSet[] = "set"; | |
56 const char PolicyUIHandler::kStatus[] = "status"; | |
57 const char PolicyUIHandler::kValue[] = "value"; | |
58 | |
59 namespace { | 57 namespace { |
60 | 58 |
61 content::WebUIDataSource* CreatePolicyUIHTMLSource() { | 59 content::WebUIDataSource* CreatePolicyUIHTMLSource() { |
62 content::WebUIDataSource* source = | 60 content::WebUIDataSource* source = |
63 content::WebUIDataSource::Create(chrome::kChromeUIPolicyHost); | 61 content::WebUIDataSource::Create(chrome::kChromeUIPolicyHost); |
64 | 62 |
65 // Localized strings. | 63 // Localized strings. |
| 64 source->AddLocalizedString("title", IDS_POLICY_TITLE); |
| 65 source->AddLocalizedString("filterPlaceholder", |
| 66 IDS_POLICY_FILTER_PLACEHOLDER); |
| 67 source->AddLocalizedString("reloadPolicies", IDS_POLICY_RELOAD_POLICIES); |
| 68 source->AddLocalizedString("status", IDS_POLICY_STATUS); |
| 69 source->AddLocalizedString("statusDevice", IDS_POLICY_STATUS_DEVICE); |
| 70 source->AddLocalizedString("statusUser", IDS_POLICY_STATUS_USER); |
| 71 source->AddLocalizedString("labelDomain", IDS_POLICY_LABEL_DOMAIN); |
| 72 source->AddLocalizedString("labelUsername", IDS_POLICY_LABEL_USERNAME); |
| 73 source->AddLocalizedString("labelClientId", IDS_POLICY_LABEL_CLIENT_ID); |
| 74 source->AddLocalizedString("labelTimeSinceLastRefresh", |
| 75 IDS_POLICY_LABEL_TIME_SINCE_LAST_REFRESH); |
| 76 source->AddLocalizedString("labelRefreshInterval", |
| 77 IDS_POLICY_LABEL_REFRESH_INTERVAL); |
| 78 source->AddLocalizedString("labelStatus", IDS_POLICY_LABEL_STATUS); |
| 79 source->AddLocalizedString("showUnset", IDS_POLICY_SHOW_UNSET); |
| 80 source->AddLocalizedString("noPoliciesSet", IDS_POLICY_NO_POLICIES_SET); |
| 81 source->AddLocalizedString("headerScope", IDS_POLICY_HEADER_SCOPE); |
| 82 source->AddLocalizedString("headerLevel", IDS_POLICY_HEADER_LEVEL); |
| 83 source->AddLocalizedString("headerName", IDS_POLICY_HEADER_NAME); |
| 84 source->AddLocalizedString("headerValue", IDS_POLICY_HEADER_VALUE); |
| 85 source->AddLocalizedString("headerStatus", IDS_POLICY_HEADER_STATUS); |
| 86 source->AddLocalizedString("showExpandedValue", |
| 87 IDS_POLICY_SHOW_EXPANDED_VALUE); |
| 88 source->AddLocalizedString("hideExpandedValue", |
| 89 IDS_POLICY_HIDE_EXPANDED_VALUE); |
| 90 source->AddLocalizedString("scopeUser", IDS_POLICY_SCOPE_USER); |
| 91 source->AddLocalizedString("scopeDevice", IDS_POLICY_SCOPE_DEVICE); |
| 92 source->AddLocalizedString("levelRecommended", IDS_POLICY_LEVEL_RECOMMENDED); |
| 93 source->AddLocalizedString("levelMandatory", IDS_POLICY_LEVEL_MANDATORY); |
| 94 source->AddLocalizedString("ok", IDS_POLICY_OK); |
| 95 source->AddLocalizedString("unset", IDS_POLICY_UNSET); |
| 96 source->AddLocalizedString("unknown", IDS_POLICY_UNKNOWN); |
| 97 |
66 source->SetUseJsonJSFormatV2(); | 98 source->SetUseJsonJSFormatV2(); |
67 source->AddLocalizedString("policyTitle", IDS_POLICY_TITLE); | |
68 source->AddLocalizedString("statusPaneTitle", IDS_POLICY_STATUS_TITLE); | |
69 source->AddLocalizedString("fetchPoliciesText", IDS_POLICY_FETCH); | |
70 source->AddLocalizedString("devicePoliciesBoxTitle", | |
71 IDS_POLICY_DEVICE_POLICIES); | |
72 source->AddLocalizedString("userPoliciesBoxTitle", IDS_POLICY_USER_POLICIES); | |
73 source->AddLocalizedString("enrollmentDomainText", | |
74 IDS_POLICY_ENROLLMENT_DOMAIN); | |
75 source->AddLocalizedString("clientIdText", IDS_POLICY_CLIENT_ID); | |
76 source->AddLocalizedString("usernameText", IDS_POLICY_USERNAME); | |
77 source->AddLocalizedString("timeSinceLastFetchText", IDS_POLICY_LAST_FETCHED); | |
78 source->AddLocalizedString("fetchIntervalText", IDS_POLICY_FETCH_INTERVAL); | |
79 source->AddLocalizedString("serverStatusText", IDS_POLICY_SERVER_STATUS); | |
80 source->AddLocalizedString("showUnsentPoliciesText", IDS_POLICY_SHOW_UNSENT); | |
81 source->AddLocalizedString("filterPoliciesText", IDS_POLICY_FILTER); | |
82 source->AddLocalizedString("noPoliciesSet",IDS_POLICY_NO_POLICIES_SET); | |
83 source->AddLocalizedString("appliesToTableHeader", IDS_POLICY_APPLIES_TO); | |
84 source->AddLocalizedString("policyLevelTableHeader", IDS_POLICY_LEVEL); | |
85 source->AddLocalizedString("policyNameTableHeader", IDS_POLICY_ENTRY_NAME); | |
86 source->AddLocalizedString("policyValueTableHeader", IDS_POLICY_ENTRY_VALUE); | |
87 source->AddLocalizedString("policyStatusTableHeader", | |
88 IDS_POLICY_ENTRY_STATUS); | |
89 source->AddLocalizedString("showMoreText", IDS_POLICY_SHOW_MORE); | |
90 source->AddLocalizedString("hideText", IDS_POLICY_HIDE); | |
91 source->SetJsonPath("strings.js"); | 99 source->SetJsonPath("strings.js"); |
92 | 100 |
93 // Add required resources. | 101 // Add required resources. |
94 source->AddResourcePath("policy.css", IDR_POLICY_CSS); | 102 source->AddResourcePath("policy.css", IDR_POLICY_CSS); |
95 source->AddResourcePath("policy.js", IDR_POLICY_JS); | 103 source->AddResourcePath("policy.js", IDR_POLICY_JS); |
| 104 source->AddResourcePath("uber_utils.js", IDR_UBER_UTILS_JS); |
96 source->SetDefaultResource(IDR_POLICY_HTML); | 105 source->SetDefaultResource(IDR_POLICY_HTML); |
97 | 106 |
98 return source; | 107 return source; |
99 } | 108 } |
100 | 109 |
101 string16 GetPolicyScopeString(policy::PolicyScope scope) { | 110 void GetStatusFromCore(const policy::CloudPolicyCore* core, |
102 switch (scope) { | 111 base::DictionaryValue* dict) { |
103 case policy::POLICY_SCOPE_USER: | 112 const policy::CloudPolicyStore* store = core->store(); |
104 return l10n_util::GetStringUTF16(IDS_POLICY_SCOPE_USER); | 113 const policy::CloudPolicyClient* client = core->client(); |
105 case policy::POLICY_SCOPE_MACHINE: | 114 const policy::CloudPolicyRefreshScheduler* refresh_scheduler = |
106 return l10n_util::GetStringUTF16(IDS_POLICY_SCOPE_MACHINE); | 115 core->refresh_scheduler(); |
107 } | 116 |
108 NOTREACHED(); | 117 bool no_error = store->status() == policy::CloudPolicyStore::STATUS_OK && |
109 return string16(); | 118 client && client->status() == policy::DM_STATUS_SUCCESS; |
| 119 string16 status = store->status() == policy::CloudPolicyStore::STATUS_OK && |
| 120 client && client->status() != policy::DM_STATUS_SUCCESS ? |
| 121 policy::FormatDeviceManagementStatus(client->status()) : |
| 122 policy::FormatStoreStatus(store->status(), |
| 123 store->validation_status()); |
| 124 const em::PolicyData* policy = store->policy(); |
| 125 std::string client_id = policy ? policy->device_id() : std::string(); |
| 126 std::string username = policy ? policy->username() : std::string(); |
| 127 base::TimeDelta refresh_interval = |
| 128 base::TimeDelta::FromMilliseconds(refresh_scheduler ? |
| 129 refresh_scheduler->refresh_delay() : |
| 130 policy::CloudPolicyRefreshScheduler::kDefaultRefreshDelayMs); |
| 131 base::Time last_refresh_time = refresh_scheduler ? |
| 132 refresh_scheduler->last_refresh() : base::Time(); |
| 133 |
| 134 dict->SetBoolean("error", !no_error); |
| 135 dict->SetString("status", status); |
| 136 dict->SetString("clientId", client_id); |
| 137 dict->SetString("username", username); |
| 138 dict->SetString("refreshInterval", |
| 139 TimeFormat::TimeRemainingShort(refresh_interval)); |
| 140 dict->SetString("timeSinceLastRefresh", last_refresh_time.is_null() ? |
| 141 l10n_util::GetStringUTF16(IDS_POLICY_NEVER_FETCHED) : |
| 142 TimeFormat::TimeElapsed(base::Time::NowFromSystemTime() - |
| 143 last_refresh_time)); |
110 } | 144 } |
111 | 145 |
112 string16 GetPolicyLevelString(policy::PolicyLevel level) { | 146 void ExtractDomainFromUsername(base::DictionaryValue* dict) { |
113 switch (level) { | 147 std::string username; |
114 case policy::POLICY_LEVEL_RECOMMENDED: | 148 dict->GetString("username", &username); |
115 return l10n_util::GetStringUTF16(IDS_POLICY_LEVEL_RECOMMENDED); | 149 if (!username.empty()) |
116 case policy::POLICY_LEVEL_MANDATORY: | 150 dict->SetString("domain", gaia::ExtractDomainName(username)); |
117 return l10n_util::GetStringUTF16(IDS_POLICY_LEVEL_MANDATORY); | |
118 } | |
119 NOTREACHED(); | |
120 return string16(); | |
121 } | |
122 | |
123 base::DictionaryValue* GetPolicyDetails( | |
124 const policy::PolicyDefinitionList::Entry* policy_definition, | |
125 const policy::PolicyMap::Entry* policy_value, | |
126 const string16& error_message) { | |
127 base::DictionaryValue* details = new base::DictionaryValue(); | |
128 details->SetString(PolicyUIHandler::kName, | |
129 ASCIIToUTF16(policy_definition->name)); | |
130 details->SetBoolean(PolicyUIHandler::kSet, true); | |
131 details->SetString(PolicyUIHandler::kLevel, | |
132 GetPolicyLevelString(policy_value->level)); | |
133 details->SetString(PolicyUIHandler::kScope, | |
134 GetPolicyScopeString(policy_value->scope)); | |
135 details->Set(PolicyUIHandler::kValue, policy_value->value->DeepCopy()); | |
136 if (error_message.empty()) { | |
137 details->SetString(PolicyUIHandler::kStatus, | |
138 l10n_util::GetStringUTF16(IDS_OK)); | |
139 } else { | |
140 details->SetString(PolicyUIHandler::kStatus, error_message); | |
141 } | |
142 return details; | |
143 } | |
144 | |
145 base::DictionaryValue* GetPolicyErrorDetails(const std::string& policy_name, | |
146 bool is_set) { | |
147 base::DictionaryValue* details = new base::DictionaryValue(); | |
148 details->SetString(PolicyUIHandler::kName, ASCIIToUTF16(policy_name)); | |
149 details->SetBoolean(PolicyUIHandler::kSet, is_set); | |
150 details->SetString(PolicyUIHandler::kLevel, ""); | |
151 details->SetString(PolicyUIHandler::kScope, ""); | |
152 details->SetString(PolicyUIHandler::kValue, ""); | |
153 if (is_set) | |
154 details->SetString(PolicyUIHandler::kStatus, | |
155 l10n_util::GetStringUTF16(IDS_POLICY_UNKNOWN)); | |
156 else | |
157 details->SetString(PolicyUIHandler::kStatus, | |
158 l10n_util::GetStringUTF16(IDS_POLICY_NOT_SET)); | |
159 return details; | |
160 } | 151 } |
161 | 152 |
162 } // namespace | 153 } // namespace |
163 | 154 |
164 // An interface for querying status data. | 155 // An interface for querying the status of cloud policy. |
165 class CloudPolicyStatusProvider { | 156 class CloudPolicyStatusProvider { |
166 public: | 157 public: |
167 CloudPolicyStatusProvider() {} | 158 CloudPolicyStatusProvider(); |
168 virtual ~CloudPolicyStatusProvider() {} | 159 virtual ~CloudPolicyStatusProvider(); |
169 | 160 |
170 // Sets a callback to invoke upon status changes. | 161 // Sets a callback to invoke upon status changes. |
171 void SetStatusChangeCallback(const base::Closure& callback) { | 162 void SetStatusChangeCallback(const base::Closure& callback); |
172 callback_ = callback; | |
173 } | |
174 | 163 |
175 // Fills |status_dict| and returns true if the status is relevant. | 164 virtual void GetStatus(base::DictionaryValue* dict); |
176 virtual bool GetStatus(base::DictionaryValue* status_dict) { | |
177 return false; | |
178 } | |
179 | 165 |
180 protected: | 166 protected: |
181 void NotifyStatusChange() { | 167 void NotifyStatusChange(); |
182 if (!callback_.is_null()) | |
183 callback_.Run(); | |
184 } | |
185 | 168 |
186 private: | 169 private: |
187 base::Closure callback_; | 170 base::Closure callback_; |
188 | 171 |
189 DISALLOW_COPY_AND_ASSIGN(CloudPolicyStatusProvider); | 172 DISALLOW_COPY_AND_ASSIGN(CloudPolicyStatusProvider); |
190 }; | 173 }; |
191 | 174 |
192 // Status provider implementation that pulls cloud policy status from a | 175 // Status provider implementation that pulls cloud policy status from a |
193 // CloudPolicyCore instance provided at construction time. Also listens for | 176 // CloudPolicyCore instance provided at construction time. Also listens for |
194 // changes on that CloudPolicyCore and reports them through the status change | 177 // changes on that CloudPolicyCore and reports them through the status change |
195 // callback. | 178 // callback. |
196 class CloudPolicyCoreStatusProvider | 179 class CloudPolicyCoreStatusProvider |
197 : public CloudPolicyStatusProvider, | 180 : public CloudPolicyStatusProvider, |
198 public policy::CloudPolicyStore::Observer { | 181 public policy::CloudPolicyStore::Observer { |
199 public: | 182 public: |
200 explicit CloudPolicyCoreStatusProvider(policy::CloudPolicyCore* core) | 183 explicit CloudPolicyCoreStatusProvider(policy::CloudPolicyCore* core); |
201 : core_(core) { | 184 virtual ~CloudPolicyCoreStatusProvider(); |
202 core_->store()->AddObserver(this); | |
203 } | |
204 virtual ~CloudPolicyCoreStatusProvider() { | |
205 core_->store()->RemoveObserver(this); | |
206 } | |
207 | 185 |
208 // CloudPolicyStatusProvider: | 186 // policy::CloudPolicyStore::Observer implementation. |
209 virtual bool GetStatus(base::DictionaryValue* status_dict) OVERRIDE { | 187 virtual void OnStoreLoaded(policy::CloudPolicyStore* store) OVERRIDE; |
210 return GetStatusFromCore(core_, status_dict); | 188 virtual void OnStoreError(policy::CloudPolicyStore* store) OVERRIDE; |
211 } | |
212 | 189 |
213 // policy::CloudPolicyStore::Observer: | 190 protected: |
214 virtual void OnStoreLoaded(policy::CloudPolicyStore* store) OVERRIDE { | |
215 NotifyStatusChange(); | |
216 } | |
217 virtual void OnStoreError(policy::CloudPolicyStore* store) OVERRIDE { | |
218 NotifyStatusChange(); | |
219 } | |
220 | |
221 // Extracts status information from |core| and writes it to |status_dict|. | |
222 static bool GetStatusFromCore(policy::CloudPolicyCore* core, | |
223 base::DictionaryValue* status_dict); | |
224 | |
225 private: | |
226 // Policy status is read from the CloudPolicyClient, CloudPolicyStore and | 191 // Policy status is read from the CloudPolicyClient, CloudPolicyStore and |
227 // CloudPolicyRefreshScheduler hosted by this |core_|. | 192 // CloudPolicyRefreshScheduler hosted by this |core_|. |
228 policy::CloudPolicyCore* core_; | 193 policy::CloudPolicyCore* core_; |
229 | 194 |
230 DISALLOW_COPY_AND_ASSIGN(CloudPolicyCoreStatusProvider); | 195 DISALLOW_COPY_AND_ASSIGN(CloudPolicyCoreStatusProvider); |
231 }; | 196 }; |
232 | 197 |
233 // static | 198 // A cloud policy status provider for user policy. |
234 bool CloudPolicyCoreStatusProvider::GetStatusFromCore( | 199 class UserPolicyStatusProvider : public CloudPolicyCoreStatusProvider { |
235 policy::CloudPolicyCore* core, | 200 public: |
236 base::DictionaryValue* status_dict) { | 201 explicit UserPolicyStatusProvider(policy::CloudPolicyCore* core); |
237 policy::CloudPolicyStore* store = core->store(); | 202 virtual ~UserPolicyStatusProvider(); |
238 policy::CloudPolicyClient* client = core->client(); | |
239 policy::CloudPolicyRefreshScheduler* refresh_scheduler = | |
240 core->refresh_scheduler(); | |
241 | 203 |
242 string16 status; | 204 // CloudPolicyCoreStatusProvider implementation. |
243 if (store->status() == policy::CloudPolicyStore::STATUS_OK && | 205 virtual void GetStatus(base::DictionaryValue* dict) OVERRIDE; |
244 client && client->status() != policy::DM_STATUS_SUCCESS) { | |
245 status = policy::FormatDeviceManagementStatus(client->status()); | |
246 } else { | |
247 status = policy::FormatStoreStatus(store->status(), | |
248 store->validation_status()); | |
249 } | |
250 status_dict->SetString("statusMessage", status); | |
251 | 206 |
252 base::Time last_refresh_time; | 207 private: |
253 if (refresh_scheduler) | 208 DISALLOW_COPY_AND_ASSIGN(UserPolicyStatusProvider); |
254 last_refresh_time = refresh_scheduler->last_refresh(); | 209 }; |
255 status_dict->SetString( | |
256 "timeSinceLastFetch", | |
257 last_refresh_time.is_null() ? | |
258 l10n_util::GetStringUTF16(IDS_POLICY_NEVER_FETCHED) : | |
259 TimeFormat::TimeElapsed(base::Time::NowFromSystemTime() - | |
260 last_refresh_time)); | |
261 | |
262 const em::PolicyData* policy = store->policy(); | |
263 status_dict->SetString( | |
264 "clientId", policy ? ASCIIToUTF16(policy->device_id()) : string16()); | |
265 status_dict->SetString( | |
266 "user", policy ? UTF8ToUTF16(policy->username()) : string16()); | |
267 | |
268 int64 interval = policy::CloudPolicyRefreshScheduler::kDefaultRefreshDelayMs; | |
269 if (refresh_scheduler) | |
270 interval = refresh_scheduler->refresh_delay(); | |
271 status_dict->SetString( | |
272 "fetchInterval", | |
273 TimeFormat::TimeRemainingShort( | |
274 base::TimeDelta::FromMilliseconds(interval))); | |
275 | |
276 return store->is_managed(); | |
277 } | |
278 | 210 |
279 #if defined(OS_CHROMEOS) | 211 #if defined(OS_CHROMEOS) |
| 212 // A cloud policy status provider for device policy. |
| 213 class DevicePolicyStatusProvider : public CloudPolicyCoreStatusProvider { |
| 214 public: |
| 215 explicit DevicePolicyStatusProvider( |
| 216 policy::BrowserPolicyConnector* connector); |
| 217 virtual ~DevicePolicyStatusProvider(); |
| 218 |
| 219 // CloudPolicyCoreStatusProvider implementation. |
| 220 virtual void GetStatus(base::DictionaryValue* dict) OVERRIDE; |
| 221 |
| 222 private: |
| 223 std::string domain_; |
| 224 |
| 225 DISALLOW_COPY_AND_ASSIGN(DevicePolicyStatusProvider); |
| 226 }; |
| 227 |
280 // A cloud policy status provider that reads policy status from the policy core | 228 // A cloud policy status provider that reads policy status from the policy core |
281 // associated with the device-local account specified by |account_id| at | 229 // associated with the device-local account specified by |account_id| at |
282 // construction time. The indirection via account ID and | 230 // construction time. The indirection via account ID and |
283 // DeviceLocalAccountPolicyService is necessary because the device-local account | 231 // DeviceLocalAccountPolicyService is necessary because the device-local account |
284 // may go away any time behind the scenes, at which point the status message | 232 // may go away any time behind the scenes, at which point the status message |
285 // text will indicate CloudPolicyStore::STATUS_BAD_STATE. | 233 // text will indicate CloudPolicyStore::STATUS_BAD_STATE. |
286 class DeviceLocalAccountPolicyStatusProvider | 234 class DeviceLocalAccountPolicyStatusProvider |
287 : public CloudPolicyStatusProvider, | 235 : public CloudPolicyStatusProvider, |
288 public policy::DeviceLocalAccountPolicyService::Observer { | 236 public policy::DeviceLocalAccountPolicyService::Observer { |
289 public: | 237 public: |
290 DeviceLocalAccountPolicyStatusProvider( | 238 DeviceLocalAccountPolicyStatusProvider( |
291 const std::string& account_id, | 239 const std::string& account_id, |
292 policy::DeviceLocalAccountPolicyService* service) | 240 policy::DeviceLocalAccountPolicyService* service); |
293 : account_id_(account_id), | 241 virtual ~DeviceLocalAccountPolicyStatusProvider(); |
294 service_(service) { | |
295 service_->AddObserver(this); | |
296 } | |
297 virtual ~DeviceLocalAccountPolicyStatusProvider() { | |
298 service_->RemoveObserver(this); | |
299 } | |
300 | 242 |
301 // CloudPolicyStatusProvider: | 243 // CloudPolicyStatusProvider implementation. |
302 virtual bool GetStatus(base::DictionaryValue* status_dict) OVERRIDE { | 244 virtual void GetStatus(base::DictionaryValue* dict) OVERRIDE; |
303 policy::DeviceLocalAccountPolicyBroker* broker = | |
304 service_->GetBrokerForAccount(account_id_); | |
305 if (broker) { | |
306 return CloudPolicyCoreStatusProvider::GetStatusFromCore(broker->core(), | |
307 status_dict); | |
308 } | |
309 | 245 |
310 status_dict->SetString("user", account_id_); | 246 // policy::DeviceLocalAccountPolicyService::Observer implementation. |
311 status_dict->SetString( | 247 virtual void OnPolicyUpdated(const std::string& account_id) OVERRIDE; |
312 "statusMessage", | 248 virtual void OnDeviceLocalAccountsChanged() OVERRIDE; |
313 policy::FormatStoreStatus( | |
314 policy::CloudPolicyStore::STATUS_BAD_STATE, | |
315 policy::CloudPolicyValidatorBase::VALIDATION_OK)); | |
316 return true; | |
317 } | |
318 | |
319 // policy::DeviceLocalAccountPolicyService::Observer: | |
320 virtual void OnPolicyUpdated(const std::string& account_id) OVERRIDE { | |
321 if (account_id == account_id_) | |
322 NotifyStatusChange(); | |
323 } | |
324 virtual void OnDeviceLocalAccountsChanged() OVERRIDE { | |
325 NotifyStatusChange(); | |
326 } | |
327 | 249 |
328 private: | 250 private: |
329 const std::string account_id_; | 251 const std::string account_id_; |
330 policy::DeviceLocalAccountPolicyService* service_; | 252 policy::DeviceLocalAccountPolicyService* service_; |
331 | 253 |
332 DISALLOW_COPY_AND_ASSIGN(DeviceLocalAccountPolicyStatusProvider); | 254 DISALLOW_COPY_AND_ASSIGN(DeviceLocalAccountPolicyStatusProvider); |
333 }; | 255 }; |
334 #endif | 256 #endif |
335 | 257 |
336 //////////////////////////////////////////////////////////////////////////////// | 258 // The JavaScript message handler for the chrome://policy page. |
337 // | 259 class PolicyUIHandler : public content::WebUIMessageHandler, |
338 // PolicyUIHandler | 260 public policy::PolicyService::Observer { |
339 // | 261 public: |
340 //////////////////////////////////////////////////////////////////////////////// | 262 PolicyUIHandler(); |
| 263 virtual ~PolicyUIHandler(); |
| 264 |
| 265 // content::WebUIMessageHandler implementation. |
| 266 virtual void RegisterMessages() OVERRIDE; |
| 267 |
| 268 // policy::PolicyService::Observer implementation. |
| 269 virtual void OnPolicyUpdated(const policy::PolicyNamespace& ns, |
| 270 const policy::PolicyMap& previous, |
| 271 const policy::PolicyMap& current) OVERRIDE; |
| 272 |
| 273 private: |
| 274 // Send a dictionary containing the names of all known policies to the UI. |
| 275 void SendPolicyNames() const; |
| 276 |
| 277 // Send information about the current policy values to the UI. For each policy |
| 278 // whose value has been set, a dictionary containing the value and additional |
| 279 // metadata is sent. |
| 280 void SendPolicyValues() const; |
| 281 |
| 282 // Send the status of cloud policy to the UI. For each scope that has cloud |
| 283 // policy enabled (device and/or user), a dictionary containing status |
| 284 // information is sent. |
| 285 void SendStatus() const; |
| 286 |
| 287 void HandleInitialized(const base::ListValue* args); |
| 288 void HandleReloadPolicies(const base::ListValue* args); |
| 289 |
| 290 void OnRefreshPoliciesDone() const; |
| 291 |
| 292 policy::PolicyService* GetPolicyService() const; |
| 293 |
| 294 bool initialized_; |
| 295 std::string device_domain_; |
| 296 base::WeakPtrFactory<PolicyUIHandler> weak_factory_; |
| 297 |
| 298 // Providers that supply status dictionaries for user and device policy, |
| 299 // respectively. These are created on initialization time as appropriate for |
| 300 // the platform (Chrome OS / desktop) and type of policy that is in effect. |
| 301 scoped_ptr<CloudPolicyStatusProvider> user_status_provider_; |
| 302 scoped_ptr<CloudPolicyStatusProvider> device_status_provider_; |
| 303 |
| 304 DISALLOW_COPY_AND_ASSIGN(PolicyUIHandler); |
| 305 }; |
| 306 |
| 307 CloudPolicyStatusProvider::CloudPolicyStatusProvider() { |
| 308 } |
| 309 |
| 310 CloudPolicyStatusProvider::~CloudPolicyStatusProvider() { |
| 311 } |
| 312 |
| 313 void CloudPolicyStatusProvider::SetStatusChangeCallback( |
| 314 const base::Closure& callback) { |
| 315 callback_ = callback; |
| 316 } |
| 317 |
| 318 void CloudPolicyStatusProvider::GetStatus(base::DictionaryValue* dict) { |
| 319 } |
| 320 |
| 321 void CloudPolicyStatusProvider::NotifyStatusChange() { |
| 322 if (!callback_.is_null()) |
| 323 callback_.Run(); |
| 324 } |
| 325 |
| 326 CloudPolicyCoreStatusProvider::CloudPolicyCoreStatusProvider( |
| 327 policy::CloudPolicyCore* core) : core_(core) { |
| 328 core_->store()->AddObserver(this); |
| 329 // TODO(bartfab): Add an observer that watches for client errors. Observing |
| 330 // core_->client() directly is not safe as the client may be destroyed and |
| 331 // (re-)created anytime if the user signs in or out on desktop platforms. |
| 332 } |
| 333 |
| 334 CloudPolicyCoreStatusProvider::~CloudPolicyCoreStatusProvider() { |
| 335 core_->store()->RemoveObserver(this); |
| 336 } |
| 337 |
| 338 void CloudPolicyCoreStatusProvider::OnStoreLoaded( |
| 339 policy::CloudPolicyStore* store) { |
| 340 NotifyStatusChange(); |
| 341 } |
| 342 |
| 343 void CloudPolicyCoreStatusProvider::OnStoreError( |
| 344 policy::CloudPolicyStore* store) { |
| 345 NotifyStatusChange(); |
| 346 } |
| 347 |
| 348 UserPolicyStatusProvider::UserPolicyStatusProvider( |
| 349 policy::CloudPolicyCore* core) : CloudPolicyCoreStatusProvider(core) { |
| 350 } |
| 351 |
| 352 UserPolicyStatusProvider::~UserPolicyStatusProvider() { |
| 353 } |
| 354 |
| 355 void UserPolicyStatusProvider::GetStatus(base::DictionaryValue* dict) { |
| 356 if (!core_->store()->is_managed()) |
| 357 return; |
| 358 GetStatusFromCore(core_, dict); |
| 359 ExtractDomainFromUsername(dict); |
| 360 } |
| 361 |
| 362 #if defined(OS_CHROMEOS) |
| 363 DevicePolicyStatusProvider::DevicePolicyStatusProvider( |
| 364 policy::BrowserPolicyConnector* connector) |
| 365 : CloudPolicyCoreStatusProvider( |
| 366 connector->GetDeviceCloudPolicyManager()->core()) { |
| 367 domain_ = connector->GetEnterpriseDomain(); |
| 368 } |
| 369 |
| 370 DevicePolicyStatusProvider::~DevicePolicyStatusProvider() { |
| 371 } |
| 372 |
| 373 void DevicePolicyStatusProvider::GetStatus(base::DictionaryValue* dict) { |
| 374 GetStatusFromCore(core_, dict); |
| 375 dict->SetString("domain", domain_); |
| 376 } |
| 377 |
| 378 DeviceLocalAccountPolicyStatusProvider::DeviceLocalAccountPolicyStatusProvider( |
| 379 const std::string& account_id, |
| 380 policy::DeviceLocalAccountPolicyService* service) |
| 381 : account_id_(account_id), |
| 382 service_(service) { |
| 383 service_->AddObserver(this); |
| 384 } |
| 385 |
| 386 DeviceLocalAccountPolicyStatusProvider:: |
| 387 ~DeviceLocalAccountPolicyStatusProvider() { |
| 388 service_->RemoveObserver(this); |
| 389 } |
| 390 |
| 391 void DeviceLocalAccountPolicyStatusProvider::GetStatus( |
| 392 base::DictionaryValue* dict) { |
| 393 const policy::DeviceLocalAccountPolicyBroker* broker = |
| 394 service_->GetBrokerForAccount(account_id_); |
| 395 if (broker) { |
| 396 GetStatusFromCore(broker->core(), dict); |
| 397 } else { |
| 398 dict->SetBoolean("error", true); |
| 399 dict->SetString("status", |
| 400 policy::FormatStoreStatus( |
| 401 policy::CloudPolicyStore::STATUS_BAD_STATE, |
| 402 policy::CloudPolicyValidatorBase::VALIDATION_OK)); |
| 403 dict->SetString("username", account_id_); |
| 404 } |
| 405 ExtractDomainFromUsername(dict); |
| 406 dict->SetBoolean("publicAccount", true); |
| 407 } |
| 408 |
| 409 void DeviceLocalAccountPolicyStatusProvider::OnPolicyUpdated( |
| 410 const std::string& account_id) { |
| 411 if (account_id == account_id_) |
| 412 NotifyStatusChange(); |
| 413 } |
| 414 |
| 415 void DeviceLocalAccountPolicyStatusProvider::OnDeviceLocalAccountsChanged() { |
| 416 NotifyStatusChange(); |
| 417 } |
| 418 #endif |
341 | 419 |
342 PolicyUIHandler::PolicyUIHandler() | 420 PolicyUIHandler::PolicyUIHandler() |
343 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {} | 421 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
| 422 } |
344 | 423 |
345 PolicyUIHandler::~PolicyUIHandler() { | 424 PolicyUIHandler::~PolicyUIHandler() { |
346 GetPolicyService()->RemoveObserver(policy::POLICY_DOMAIN_CHROME, this); | 425 GetPolicyService()->RemoveObserver(policy::POLICY_DOMAIN_CHROME, this); |
347 } | 426 } |
348 | 427 |
349 void PolicyUIHandler::RegisterMessages() { | 428 void PolicyUIHandler::RegisterMessages() { |
350 #if defined(OS_CHROMEOS) | 429 #if defined(OS_CHROMEOS) |
351 policy::BrowserPolicyConnector* connector = | 430 policy::BrowserPolicyConnector* connector = |
352 g_browser_process->browser_policy_connector(); | 431 g_browser_process->browser_policy_connector(); |
353 if (connector->IsEnterpriseManaged()) { | 432 if (connector->IsEnterpriseManaged()) |
354 enterprise_domain_ = UTF8ToUTF16(connector->GetEnterpriseDomain()); | 433 device_status_provider_.reset(new DevicePolicyStatusProvider(connector)); |
355 device_status_provider_.reset( | |
356 new CloudPolicyCoreStatusProvider( | |
357 connector->GetDeviceCloudPolicyManager()->core())); | |
358 } | |
359 | 434 |
360 chromeos::UserManager* user_manager = chromeos::UserManager::Get(); | 435 const chromeos::UserManager* user_manager = chromeos::UserManager::Get(); |
361 if (user_manager->IsLoggedInAsPublicAccount()) { | 436 if (user_manager->IsLoggedInAsPublicAccount()) { |
362 policy::DeviceLocalAccountPolicyService* local_account_service = | 437 policy::DeviceLocalAccountPolicyService* local_account_service = |
363 connector->GetDeviceLocalAccountPolicyService(); | 438 connector->GetDeviceLocalAccountPolicyService(); |
364 if (local_account_service) { | 439 if (local_account_service) { |
365 user_status_provider_.reset( | 440 user_status_provider_.reset( |
366 new DeviceLocalAccountPolicyStatusProvider( | 441 new DeviceLocalAccountPolicyStatusProvider( |
367 user_manager->GetLoggedInUser()->email(), local_account_service)); | 442 user_manager->GetLoggedInUser()->email(), local_account_service)); |
368 } | 443 } |
369 } else { | 444 } else { |
370 policy::CloudPolicyManager* user_cloud_policy_manager = | 445 policy::CloudPolicyManager* user_cloud_policy_manager = |
371 connector->GetUserCloudPolicyManager(); | 446 connector->GetUserCloudPolicyManager(); |
372 if (user_cloud_policy_manager) { | 447 if (user_cloud_policy_manager) { |
373 user_status_provider_.reset( | 448 user_status_provider_.reset( |
374 new CloudPolicyCoreStatusProvider(user_cloud_policy_manager->core())); | 449 new UserPolicyStatusProvider(user_cloud_policy_manager->core())); |
375 } | 450 } |
376 } | 451 } |
377 #else | 452 #else |
378 policy::CloudPolicyManager* user_cloud_policy_manager = | 453 policy::CloudPolicyManager* user_cloud_policy_manager = |
379 policy::UserCloudPolicyManagerFactory::GetForProfile( | 454 policy::UserCloudPolicyManagerFactory::GetForProfile( |
380 Profile::FromWebUI(web_ui())); | 455 Profile::FromWebUI(web_ui())); |
381 if (user_cloud_policy_manager) { | 456 if (user_cloud_policy_manager) { |
382 user_status_provider_.reset( | 457 user_status_provider_.reset( |
383 new CloudPolicyCoreStatusProvider(user_cloud_policy_manager->core())); | 458 new UserPolicyStatusProvider(user_cloud_policy_manager->core())); |
384 } | 459 } |
385 #endif | 460 #endif |
386 | 461 |
387 if (!user_status_provider_.get()) | 462 if (!user_status_provider_.get()) |
388 user_status_provider_.reset(new CloudPolicyStatusProvider()); | 463 user_status_provider_.reset(new CloudPolicyStatusProvider()); |
389 if (!device_status_provider_.get()) | 464 if (!device_status_provider_.get()) |
390 device_status_provider_.reset(new CloudPolicyStatusProvider()); | 465 device_status_provider_.reset(new CloudPolicyStatusProvider()); |
391 | 466 |
392 base::Closure update_callback(base::Bind(&PolicyUIHandler::SendDataToUI, | 467 base::Closure update_callback(base::Bind(&PolicyUIHandler::SendStatus, |
393 base::Unretained(this))); | 468 base::Unretained(this))); |
394 user_status_provider_->SetStatusChangeCallback(update_callback); | 469 user_status_provider_->SetStatusChangeCallback(update_callback); |
395 device_status_provider_->SetStatusChangeCallback(update_callback); | 470 device_status_provider_->SetStatusChangeCallback(update_callback); |
396 GetPolicyService()->AddObserver(policy::POLICY_DOMAIN_CHROME, this); | 471 GetPolicyService()->AddObserver(policy::POLICY_DOMAIN_CHROME, this); |
397 | 472 |
398 web_ui()->RegisterMessageCallback( | 473 web_ui()->RegisterMessageCallback( |
399 "requestData", | 474 "initialized", |
400 base::Bind(&PolicyUIHandler::HandleRequestData, | 475 base::Bind(&PolicyUIHandler::HandleInitialized, base::Unretained(this))); |
401 base::Unretained(this))); | |
402 web_ui()->RegisterMessageCallback( | 476 web_ui()->RegisterMessageCallback( |
403 "fetchPolicy", | 477 "reloadPolicies", |
404 base::Bind(&PolicyUIHandler::HandleFetchPolicy, | 478 base::Bind(&PolicyUIHandler::HandleReloadPolicies, |
405 base::Unretained(this))); | 479 base::Unretained(this))); |
406 } | 480 } |
407 | 481 |
408 void PolicyUIHandler::OnPolicyUpdated(const policy::PolicyNamespace& ns, | 482 void PolicyUIHandler::OnPolicyUpdated(const policy::PolicyNamespace& ns, |
409 const policy::PolicyMap& previous, | 483 const policy::PolicyMap& previous, |
410 const policy::PolicyMap& current) { | 484 const policy::PolicyMap& current) { |
411 DCHECK_EQ(policy::POLICY_DOMAIN_CHROME, ns.domain); | 485 DCHECK_EQ(policy::POLICY_DOMAIN_CHROME, ns.domain); |
412 DCHECK(ns.component_id.empty()); | 486 DCHECK(ns.component_id.empty()); |
413 SendDataToUI(); | 487 SendPolicyValues(); |
414 } | 488 } |
415 | 489 |
416 // static | 490 void PolicyUIHandler::SendPolicyNames() const { |
417 scoped_ptr<base::ListValue> PolicyUIHandler::GetPolicyStatusList( | 491 base::DictionaryValue names; |
418 const policy::PolicyMap& policies, | 492 const policy::PolicyDefinitionList* list = |
419 bool* any_policies_set) { | 493 policy::GetChromePolicyDefinitionList(); |
420 scoped_ptr<base::ListValue> result(new base::ListValue()); | 494 for (const policy::PolicyDefinitionList::Entry* entry = list->begin; |
| 495 entry != list->end; ++entry) { |
| 496 names.SetBoolean(entry->name, true); |
| 497 } |
| 498 web_ui()->CallJavascriptFunction("policy.Page.setPolicyNames", names); |
| 499 } |
| 500 |
| 501 void PolicyUIHandler::SendPolicyValues() const { |
421 // Make a copy that can be modified, since some policy values are modified | 502 // Make a copy that can be modified, since some policy values are modified |
422 // before being displayed. | 503 // before being displayed. |
423 policy::PolicyMap policy_map; | 504 policy::PolicyMap map; |
424 policy_map.CopyFrom(policies); | 505 map.CopyFrom(GetPolicyService()->GetPolicies(policy::PolicyNamespace( |
| 506 policy::POLICY_DOMAIN_CHROME, std::string()))); |
425 | 507 |
426 // Get a list of all the errors in the policy values. | 508 // Get a list of all the errors in the policy values. |
427 const policy::ConfigurationPolicyHandlerList* handler_list = | 509 const policy::ConfigurationPolicyHandlerList* handler_list = |
428 g_browser_process->browser_policy_connector()->GetHandlerList(); | 510 g_browser_process->browser_policy_connector()->GetHandlerList(); |
429 policy::PolicyErrorMap errors; | 511 policy::PolicyErrorMap errors; |
430 handler_list->ApplyPolicySettings(policy_map, NULL, &errors); | 512 handler_list->ApplyPolicySettings(map, NULL, &errors); |
431 handler_list->PrepareForDisplaying(&policy_map); | |
432 | 513 |
433 // Append each known policy and its status. | 514 // Convert dictionary values to strings for display. |
434 *any_policies_set = false; | 515 handler_list->PrepareForDisplaying(&map); |
435 std::vector<base::DictionaryValue*> unset_policies; | 516 |
436 base::hash_set<std::string> known_names; | 517 base::DictionaryValue values; |
437 const policy::PolicyDefinitionList* policy_list = | 518 for (policy::PolicyMap::const_iterator entry = map.begin(); |
438 policy::GetChromePolicyDefinitionList(); | 519 entry != map.end(); ++entry) { |
439 for (const policy::PolicyDefinitionList::Entry* policy = policy_list->begin; | 520 base::DictionaryValue* value = new base::DictionaryValue; |
440 policy != policy_list->end; ++policy) { | 521 value->Set("value", entry->second.value->DeepCopy()); |
441 known_names.insert(policy->name); | 522 if (entry->second.scope == policy::POLICY_SCOPE_USER) |
442 const policy::PolicyMap::Entry* entry = policy_map.Get(policy->name); | 523 value->SetString("scope", "user"); |
443 string16 error_message = errors.GetErrors(policy->name); | 524 else |
444 if (entry) { | 525 value->SetString("scope", "machine"); |
445 *any_policies_set = true; | 526 if (entry->second.level == policy::POLICY_LEVEL_RECOMMENDED) |
446 result->Append(GetPolicyDetails(policy, entry, error_message)); | 527 value->SetString("level", "recommended"); |
447 } else { | 528 else |
448 unset_policies.push_back(GetPolicyErrorDetails(policy->name, false)); | 529 value->SetString("level", "mandatory"); |
449 } | 530 string16 error = errors.GetErrors(entry->first); |
| 531 if (!error.empty()) |
| 532 value->SetString("error", error); |
| 533 values.Set(entry->first, value); |
450 } | 534 } |
451 | 535 |
452 // Append unrecognized policy names. | 536 web_ui()->CallJavascriptFunction("policy.Page.setPolicyValues", values); |
453 for (policy::PolicyMap::const_iterator it = policy_map.begin(); | |
454 it != policy_map.end(); ++it) { | |
455 if (!ContainsKey(known_names, it->first)) | |
456 result->Append(GetPolicyErrorDetails(it->first, true)); | |
457 } | |
458 | |
459 // Append policies that aren't currently configured to the end. | |
460 for (std::vector<base::DictionaryValue*>::const_iterator it = | |
461 unset_policies.begin(); | |
462 it != unset_policies.end(); ++it) { | |
463 result->Append(*it); | |
464 } | |
465 | |
466 return result.Pass(); | |
467 } | 537 } |
468 | 538 |
469 void PolicyUIHandler::HandleRequestData(const base::ListValue* args) { | 539 void PolicyUIHandler::SendStatus() const { |
470 SendDataToUI(); | 540 scoped_ptr<base::DictionaryValue> device_status(new base::DictionaryValue); |
| 541 device_status_provider_->GetStatus(device_status.get()); |
| 542 if (!device_domain_.empty()) |
| 543 device_status->SetString("domain", device_domain_); |
| 544 scoped_ptr<base::DictionaryValue> user_status(new base::DictionaryValue); |
| 545 user_status_provider_->GetStatus(user_status.get()); |
| 546 std::string username; |
| 547 user_status->GetString("username", &username); |
| 548 if (!username.empty()) |
| 549 user_status->SetString("domain", gaia::ExtractDomainName(username)); |
| 550 |
| 551 base::DictionaryValue status; |
| 552 if (!device_status->empty()) |
| 553 status.Set("device", device_status.release()); |
| 554 if (!user_status->empty()) |
| 555 status.Set("user", user_status.release()); |
| 556 |
| 557 web_ui()->CallJavascriptFunction("policy.Page.setStatus", status); |
471 } | 558 } |
472 | 559 |
473 void PolicyUIHandler::HandleFetchPolicy(const base::ListValue* args) { | 560 void PolicyUIHandler::HandleInitialized(const base::ListValue* args) { |
474 // Fetching policy can potentially take a while due to cloud policy fetches. | 561 SendPolicyNames(); |
475 // Use a WeakPtr to make sure the callback is invalidated if the tab is closed | 562 SendPolicyValues(); |
476 // before the fetching completes. | 563 SendStatus(); |
| 564 } |
| 565 |
| 566 void PolicyUIHandler::HandleReloadPolicies(const base::ListValue* args) { |
477 GetPolicyService()->RefreshPolicies( | 567 GetPolicyService()->RefreshPolicies( |
478 base::Bind(&PolicyUIHandler::OnRefreshDone, | 568 base::Bind(&PolicyUIHandler::OnRefreshPoliciesDone, |
479 weak_factory_.GetWeakPtr())); | 569 weak_factory_.GetWeakPtr())); |
480 } | 570 } |
481 | 571 |
482 void PolicyUIHandler::OnRefreshDone() { | 572 void PolicyUIHandler::OnRefreshPoliciesDone() const { |
483 web_ui()->CallJavascriptFunction("Policy.refreshDone"); | 573 web_ui()->CallJavascriptFunction("policy.Page.reloadPoliciesDone"); |
484 } | 574 } |
485 | 575 |
486 void PolicyUIHandler::SendDataToUI() { | 576 policy::PolicyService* PolicyUIHandler::GetPolicyService() const { |
487 policy::PolicyService* service = GetPolicyService(); | 577 return Profile::FromWebUI(web_ui())->GetPolicyService(); |
488 bool any_policies_set = false; | |
489 base::ListValue* list = | |
490 GetPolicyStatusList(service->GetPolicies(policy::PolicyNamespace( | |
491 policy::POLICY_DOMAIN_CHROME, std::string())), | |
492 &any_policies_set).release(); | |
493 base::DictionaryValue results; | |
494 results.Set("policies", list); | |
495 results.SetBoolean("anyPoliciesSet", any_policies_set); | |
496 base::DictionaryValue* dict = GetStatusData(); | |
497 results.Set("status", dict); | |
498 web_ui()->CallJavascriptFunction("Policy.returnData", results); | |
499 } | 578 } |
500 | 579 |
501 policy::PolicyService* PolicyUIHandler::GetPolicyService() { | |
502 Profile* profile = Profile::FromWebUI(web_ui()); | |
503 return profile->GetPolicyService(); | |
504 } | |
505 | |
506 base::DictionaryValue* PolicyUIHandler::GetStatusData() { | |
507 base::DictionaryValue* results = new base::DictionaryValue(); | |
508 | |
509 scoped_ptr<base::DictionaryValue> user_status_dict( | |
510 new base::DictionaryValue()); | |
511 bool user_status_available = | |
512 user_status_provider_->GetStatus(user_status_dict.get()); | |
513 user_status_dict->SetBoolean("display", user_status_available); | |
514 results->Set("userStatus", user_status_dict.release()); | |
515 | |
516 scoped_ptr<base::DictionaryValue> device_status_dict( | |
517 new base::DictionaryValue()); | |
518 bool device_status_available = | |
519 device_status_provider_->GetStatus(device_status_dict.get()); | |
520 device_status_dict->SetString("domain", enterprise_domain_); | |
521 device_status_dict->SetBoolean("display", device_status_available); | |
522 results->Set("deviceStatus", device_status_dict.release()); | |
523 | |
524 results->SetBoolean("displayStatusSection", | |
525 user_status_available || device_status_available); | |
526 return results; | |
527 } | |
528 | |
529 //////////////////////////////////////////////////////////////////////////////// | |
530 // | |
531 // PolicyUI | |
532 // | |
533 //////////////////////////////////////////////////////////////////////////////// | |
534 | |
535 PolicyUI::PolicyUI(content::WebUI* web_ui) : WebUIController(web_ui) { | 580 PolicyUI::PolicyUI(content::WebUI* web_ui) : WebUIController(web_ui) { |
536 web_ui->AddMessageHandler(new PolicyUIHandler); | 581 web_ui->AddMessageHandler(new PolicyUIHandler); |
537 | 582 content::WebUIDataSource::Add(Profile::FromWebUI(web_ui), |
538 // Set up the chrome://policy/ source. | 583 CreatePolicyUIHTMLSource()); |
539 Profile* profile = Profile::FromWebUI(web_ui); | |
540 content::WebUIDataSource::Add(profile, CreatePolicyUIHTMLSource()); | |
541 } | 584 } |
542 | 585 |
543 PolicyUI::~PolicyUI() { | 586 PolicyUI::~PolicyUI() { |
544 } | 587 } |
OLD | NEW |