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(policy::PolicyDomain domain, | |
270 const std::string& component_id, | |
271 const policy::PolicyMap& previous, | |
272 const policy::PolicyMap& current) OVERRIDE; | |
273 | |
274 private: | |
275 // Send a dictionary containing the names of all known policies to the UI. | |
276 void SendPolicyNames() const; | |
Mattias Nissler (ping if slow)
2013/02/04 16:05:16
nit: newline
bartfab (slow)
2013/02/04 17:11:04
Done.
| |
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; | |
Mattias Nissler (ping if slow)
2013/02/04 16:05:16
nit: newline
bartfab (slow)
2013/02/04 17:11:04
Done.
| |
281 // Send the status of cloud policy to the UI. For each scope that has cloud | |
282 // policy enabled (device and/or user), a dictionary containing status | |
283 // information is sent. | |
284 void SendStatus() const; | |
285 | |
286 void HandleInitialized(const base::ListValue* args); | |
287 void HandleReloadPolicies(const base::ListValue* args); | |
288 | |
289 void OnRefreshPoliciesDone() const; | |
290 | |
291 policy::PolicyService* GetPolicyService() const; | |
292 | |
293 bool initialized_; | |
294 std::string device_domain_; | |
295 base::WeakPtrFactory<PolicyUIHandler> weak_factory_; | |
296 | |
297 // Providers that supply status dictionaries for user and device policy, | |
298 // respectively. These are created on initialization time as appropriate for | |
299 // the platform (Chrome OS / desktop) and type of policy that is in effect. | |
300 scoped_ptr<CloudPolicyStatusProvider> user_status_provider_; | |
301 scoped_ptr<CloudPolicyStatusProvider> device_status_provider_; | |
302 | |
303 DISALLOW_COPY_AND_ASSIGN(PolicyUIHandler); | |
304 }; | |
305 | |
306 CloudPolicyStatusProvider::CloudPolicyStatusProvider() { | |
307 } | |
308 | |
309 CloudPolicyStatusProvider::~CloudPolicyStatusProvider() { | |
310 } | |
311 | |
312 void CloudPolicyStatusProvider::SetStatusChangeCallback( | |
313 const base::Closure& callback) { | |
314 callback_ = callback; | |
315 } | |
316 | |
317 void CloudPolicyStatusProvider::GetStatus(base::DictionaryValue* dict) { | |
318 } | |
319 | |
320 void CloudPolicyStatusProvider::NotifyStatusChange() { | |
321 if (!callback_.is_null()) | |
322 callback_.Run(); | |
323 } | |
324 | |
325 CloudPolicyCoreStatusProvider::CloudPolicyCoreStatusProvider( | |
326 policy::CloudPolicyCore* core) : core_(core) { | |
327 core_->store()->AddObserver(this); | |
328 // TODO(bartfab): Add an observer that watches for client errors. Observing | |
329 // core_->client() directly is not safe as the client may be destroyed and | |
330 // (re-)created anytime if the user signs in or out on desktop platforms. | |
331 } | |
332 | |
333 CloudPolicyCoreStatusProvider::~CloudPolicyCoreStatusProvider() { | |
334 core_->store()->RemoveObserver(this); | |
335 } | |
336 | |
337 void CloudPolicyCoreStatusProvider::OnStoreLoaded( | |
338 policy::CloudPolicyStore* store) { | |
339 NotifyStatusChange(); | |
340 } | |
341 | |
342 void CloudPolicyCoreStatusProvider::OnStoreError( | |
343 policy::CloudPolicyStore* store) { | |
344 NotifyStatusChange(); | |
345 } | |
346 | |
347 UserPolicyStatusProvider::UserPolicyStatusProvider( | |
348 policy::CloudPolicyCore* core) : CloudPolicyCoreStatusProvider(core) { | |
349 } | |
350 | |
351 UserPolicyStatusProvider::~UserPolicyStatusProvider() { | |
352 } | |
353 | |
354 void UserPolicyStatusProvider::GetStatus(base::DictionaryValue* dict) { | |
355 if (!core_->store()->is_managed()) | |
356 return; | |
357 GetStatusFromCore(core_, dict); | |
358 ExtractDomainFromUsername(dict); | |
359 } | |
360 | |
361 #if defined(OS_CHROMEOS) | |
362 DevicePolicyStatusProvider::DevicePolicyStatusProvider( | |
363 policy::BrowserPolicyConnector* connector) | |
364 : CloudPolicyCoreStatusProvider( | |
365 connector->GetDeviceCloudPolicyManager()->core()) { | |
366 domain_ = connector->GetEnterpriseDomain(); | |
367 } | |
368 | |
369 DevicePolicyStatusProvider::~DevicePolicyStatusProvider() { | |
370 } | |
371 | |
372 void DevicePolicyStatusProvider::GetStatus(base::DictionaryValue* dict) { | |
373 GetStatusFromCore(core_, dict); | |
374 dict->SetString("domain", domain_); | |
375 } | |
376 | |
377 DeviceLocalAccountPolicyStatusProvider::DeviceLocalAccountPolicyStatusProvider( | |
378 const std::string& account_id, | |
379 policy::DeviceLocalAccountPolicyService* service) | |
380 : account_id_(account_id), | |
381 service_(service) { | |
382 service_->AddObserver(this); | |
383 } | |
384 | |
385 DeviceLocalAccountPolicyStatusProvider:: | |
386 ~DeviceLocalAccountPolicyStatusProvider() { | |
387 service_->RemoveObserver(this); | |
388 } | |
389 | |
390 void DeviceLocalAccountPolicyStatusProvider::GetStatus( | |
391 base::DictionaryValue* dict) { | |
392 const policy::DeviceLocalAccountPolicyBroker* broker = | |
393 service_->GetBrokerForAccount(account_id_); | |
394 if (broker) { | |
395 GetStatusFromCore(broker->core(), dict); | |
396 } else { | |
397 dict->SetBoolean("error", true); | |
398 dict->SetString("status", | |
399 policy::FormatStoreStatus( | |
400 policy::CloudPolicyStore::STATUS_BAD_STATE, | |
401 policy::CloudPolicyValidatorBase::VALIDATION_OK)); | |
402 dict->SetString("username", account_id_); | |
403 } | |
404 ExtractDomainFromUsername(dict); | |
405 dict->SetBoolean("publicAccount", true); | |
406 } | |
407 | |
408 void DeviceLocalAccountPolicyStatusProvider::OnPolicyUpdated( | |
409 const std::string& account_id) { | |
410 if (account_id == account_id_) | |
411 NotifyStatusChange(); | |
412 } | |
413 | |
414 void DeviceLocalAccountPolicyStatusProvider::OnDeviceLocalAccountsChanged() { | |
415 NotifyStatusChange(); | |
416 } | |
417 #endif | |
341 | 418 |
342 PolicyUIHandler::PolicyUIHandler() | 419 PolicyUIHandler::PolicyUIHandler() |
343 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {} | 420 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
421 } | |
344 | 422 |
345 PolicyUIHandler::~PolicyUIHandler() { | 423 PolicyUIHandler::~PolicyUIHandler() { |
346 GetPolicyService()->RemoveObserver(policy::POLICY_DOMAIN_CHROME, this); | 424 GetPolicyService()->RemoveObserver(policy::POLICY_DOMAIN_CHROME, this); |
347 } | 425 } |
348 | 426 |
349 void PolicyUIHandler::RegisterMessages() { | 427 void PolicyUIHandler::RegisterMessages() { |
350 #if defined(OS_CHROMEOS) | 428 #if defined(OS_CHROMEOS) |
351 policy::BrowserPolicyConnector* connector = | 429 policy::BrowserPolicyConnector* connector = |
352 g_browser_process->browser_policy_connector(); | 430 g_browser_process->browser_policy_connector(); |
353 if (connector->IsEnterpriseManaged()) { | 431 if (connector->IsEnterpriseManaged()) |
354 enterprise_domain_ = UTF8ToUTF16(connector->GetEnterpriseDomain()); | 432 device_status_provider_.reset(new DevicePolicyStatusProvider(connector)); |
355 device_status_provider_.reset( | |
356 new CloudPolicyCoreStatusProvider( | |
357 connector->GetDeviceCloudPolicyManager()->core())); | |
358 } | |
359 | 433 |
360 chromeos::UserManager* user_manager = chromeos::UserManager::Get(); | 434 const chromeos::UserManager* user_manager = chromeos::UserManager::Get(); |
361 if (user_manager->IsLoggedInAsPublicAccount()) { | 435 if (user_manager->IsLoggedInAsPublicAccount()) { |
362 policy::DeviceLocalAccountPolicyService* local_account_service = | 436 policy::DeviceLocalAccountPolicyService* local_account_service = |
363 connector->GetDeviceLocalAccountPolicyService(); | 437 connector->GetDeviceLocalAccountPolicyService(); |
364 if (local_account_service) { | 438 if (local_account_service) { |
365 user_status_provider_.reset( | 439 user_status_provider_.reset( |
366 new DeviceLocalAccountPolicyStatusProvider( | 440 new DeviceLocalAccountPolicyStatusProvider( |
367 user_manager->GetLoggedInUser()->email(), local_account_service)); | 441 user_manager->GetLoggedInUser()->email(), local_account_service)); |
368 } | 442 } |
369 } else { | 443 } else { |
370 policy::CloudPolicyManager* user_cloud_policy_manager = | 444 policy::CloudPolicyManager* user_cloud_policy_manager = |
371 connector->GetUserCloudPolicyManager(); | 445 connector->GetUserCloudPolicyManager(); |
372 if (user_cloud_policy_manager) { | 446 if (user_cloud_policy_manager) { |
373 user_status_provider_.reset( | 447 user_status_provider_.reset( |
374 new CloudPolicyCoreStatusProvider(user_cloud_policy_manager->core())); | 448 new UserPolicyStatusProvider(user_cloud_policy_manager->core())); |
375 } | 449 } |
376 } | 450 } |
377 #else | 451 #else |
378 policy::CloudPolicyManager* user_cloud_policy_manager = | 452 policy::CloudPolicyManager* user_cloud_policy_manager = |
379 policy::UserCloudPolicyManagerFactory::GetForProfile( | 453 policy::UserCloudPolicyManagerFactory::GetForProfile( |
380 Profile::FromWebUI(web_ui())); | 454 Profile::FromWebUI(web_ui())); |
381 if (user_cloud_policy_manager) { | 455 if (user_cloud_policy_manager) { |
382 user_status_provider_.reset( | 456 user_status_provider_.reset( |
383 new CloudPolicyCoreStatusProvider(user_cloud_policy_manager->core())); | 457 new UserPolicyStatusProvider(user_cloud_policy_manager->core())); |
384 } | 458 } |
385 #endif | 459 #endif |
386 | 460 |
387 if (!user_status_provider_.get()) | 461 if (!user_status_provider_.get()) |
388 user_status_provider_.reset(new CloudPolicyStatusProvider()); | 462 user_status_provider_.reset(new CloudPolicyStatusProvider()); |
389 if (!device_status_provider_.get()) | 463 if (!device_status_provider_.get()) |
390 device_status_provider_.reset(new CloudPolicyStatusProvider()); | 464 device_status_provider_.reset(new CloudPolicyStatusProvider()); |
391 | 465 |
392 base::Closure update_callback(base::Bind(&PolicyUIHandler::SendDataToUI, | 466 base::Closure update_callback(base::Bind(&PolicyUIHandler::SendStatus, |
393 base::Unretained(this))); | 467 base::Unretained(this))); |
394 user_status_provider_->SetStatusChangeCallback(update_callback); | 468 user_status_provider_->SetStatusChangeCallback(update_callback); |
395 device_status_provider_->SetStatusChangeCallback(update_callback); | 469 device_status_provider_->SetStatusChangeCallback(update_callback); |
396 GetPolicyService()->AddObserver(policy::POLICY_DOMAIN_CHROME, this); | 470 GetPolicyService()->AddObserver(policy::POLICY_DOMAIN_CHROME, this); |
397 | 471 |
398 web_ui()->RegisterMessageCallback( | 472 web_ui()->RegisterMessageCallback( |
399 "requestData", | 473 "initialized", |
400 base::Bind(&PolicyUIHandler::HandleRequestData, | 474 base::Bind(&PolicyUIHandler::HandleInitialized, base::Unretained(this))); |
401 base::Unretained(this))); | |
402 web_ui()->RegisterMessageCallback( | 475 web_ui()->RegisterMessageCallback( |
403 "fetchPolicy", | 476 "reloadPolicies", |
404 base::Bind(&PolicyUIHandler::HandleFetchPolicy, | 477 base::Bind(&PolicyUIHandler::HandleReloadPolicies, |
405 base::Unretained(this))); | 478 base::Unretained(this))); |
406 } | 479 } |
407 | 480 |
408 void PolicyUIHandler::OnPolicyUpdated(policy::PolicyDomain domain, | 481 void PolicyUIHandler::OnPolicyUpdated(policy::PolicyDomain domain, |
409 const std::string& component_id, | 482 const std::string& component_id, |
410 const policy::PolicyMap& previous, | 483 const policy::PolicyMap& previous, |
411 const policy::PolicyMap& current) { | 484 const policy::PolicyMap& current) { |
412 DCHECK(domain == policy::POLICY_DOMAIN_CHROME); | 485 DCHECK(domain == policy::POLICY_DOMAIN_CHROME); |
413 DCHECK(component_id.empty()); | 486 DCHECK(component_id.empty()); |
414 SendDataToUI(); | 487 SendPolicyValues(); |
415 } | 488 } |
416 | 489 |
417 // static | 490 void PolicyUIHandler::SendPolicyNames() const { |
418 scoped_ptr<base::ListValue> PolicyUIHandler::GetPolicyStatusList( | 491 base::DictionaryValue names; |
419 const policy::PolicyMap& policies, | 492 const policy::PolicyDefinitionList* list = |
420 bool* any_policies_set) { | 493 policy::GetChromePolicyDefinitionList(); |
421 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 { | |
422 // 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 |
423 // before being displayed. | 503 // before being displayed. |
424 policy::PolicyMap policy_map; | 504 policy::PolicyMap map; |
425 policy_map.CopyFrom(policies); | 505 map.CopyFrom(GetPolicyService()->GetPolicies(policy::POLICY_DOMAIN_CHROME, |
506 std::string())); | |
426 | 507 |
427 // Get a list of all the errors in the policy values. | 508 // Get a list of all the errors in the policy values. |
428 const policy::ConfigurationPolicyHandlerList* handler_list = | 509 const policy::ConfigurationPolicyHandlerList* handler_list = |
429 g_browser_process->browser_policy_connector()->GetHandlerList(); | 510 g_browser_process->browser_policy_connector()->GetHandlerList(); |
430 policy::PolicyErrorMap errors; | 511 policy::PolicyErrorMap errors; |
431 handler_list->ApplyPolicySettings(policy_map, NULL, &errors); | 512 handler_list->ApplyPolicySettings(map, NULL, &errors); |
432 handler_list->PrepareForDisplaying(&policy_map); | |
433 | 513 |
434 // Append each known policy and its status. | 514 // Convert dictionary values to strings for display. |
435 *any_policies_set = false; | 515 handler_list->PrepareForDisplaying(&map); |
436 std::vector<base::DictionaryValue*> unset_policies; | 516 |
437 base::hash_set<std::string> known_names; | 517 base::DictionaryValue values; |
438 const policy::PolicyDefinitionList* policy_list = | 518 for (policy::PolicyMap::const_iterator entry = map.begin(); |
439 policy::GetChromePolicyDefinitionList(); | 519 entry != map.end(); ++entry) { |
440 for (const policy::PolicyDefinitionList::Entry* policy = policy_list->begin; | 520 base::DictionaryValue* value = new base::DictionaryValue; |
441 policy != policy_list->end; ++policy) { | 521 value->Set("value", entry->second.value->DeepCopy()); |
442 known_names.insert(policy->name); | 522 if (entry->second.scope == policy::POLICY_SCOPE_USER) |
443 const policy::PolicyMap::Entry* entry = policy_map.Get(policy->name); | 523 value->SetString("scope", "user"); |
444 string16 error_message = errors.GetErrors(policy->name); | 524 else |
445 if (entry) { | 525 value->SetString("scope", "machine"); |
446 *any_policies_set = true; | 526 if (entry->second.level == policy::POLICY_LEVEL_RECOMMENDED) |
447 result->Append(GetPolicyDetails(policy, entry, error_message)); | 527 value->SetString("level", "recommended"); |
448 } else { | 528 else |
449 unset_policies.push_back(GetPolicyErrorDetails(policy->name, false)); | 529 value->SetString("level", "mandatory"); |
450 } | 530 string16 error = errors.GetErrors(entry->first); |
531 if (!error.empty()) | |
532 value->SetString("error", error); | |
533 values.Set(entry->first, value); | |
451 } | 534 } |
452 | 535 |
453 // Append unrecognized policy names. | 536 web_ui()->CallJavascriptFunction("policy.Page.setPolicyValues", values); |
454 for (policy::PolicyMap::const_iterator it = policy_map.begin(); | |
455 it != policy_map.end(); ++it) { | |
456 if (!ContainsKey(known_names, it->first)) | |
457 result->Append(GetPolicyErrorDetails(it->first, true)); | |
458 } | |
459 | |
460 // Append policies that aren't currently configured to the end. | |
461 for (std::vector<base::DictionaryValue*>::const_iterator it = | |
462 unset_policies.begin(); | |
463 it != unset_policies.end(); ++it) { | |
464 result->Append(*it); | |
465 } | |
466 | |
467 return result.Pass(); | |
468 } | 537 } |
469 | 538 |
470 void PolicyUIHandler::HandleRequestData(const base::ListValue* args) { | 539 void PolicyUIHandler::SendStatus() const { |
471 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); | |
472 } | 558 } |
473 | 559 |
474 void PolicyUIHandler::HandleFetchPolicy(const base::ListValue* args) { | 560 void PolicyUIHandler::HandleInitialized(const base::ListValue* args) { |
475 // Fetching policy can potentially take a while due to cloud policy fetches. | 561 SendPolicyNames(); |
476 // Use a WeakPtr to make sure the callback is invalidated if the tab is closed | 562 SendPolicyValues(); |
477 // before the fetching completes. | 563 SendStatus(); |
564 } | |
565 | |
566 void PolicyUIHandler::HandleReloadPolicies(const base::ListValue* args) { | |
478 GetPolicyService()->RefreshPolicies( | 567 GetPolicyService()->RefreshPolicies( |
479 base::Bind(&PolicyUIHandler::OnRefreshDone, | 568 base::Bind(&PolicyUIHandler::OnRefreshPoliciesDone, |
480 weak_factory_.GetWeakPtr())); | 569 weak_factory_.GetWeakPtr())); |
481 } | 570 } |
482 | 571 |
483 void PolicyUIHandler::OnRefreshDone() { | 572 void PolicyUIHandler::OnRefreshPoliciesDone() const { |
484 web_ui()->CallJavascriptFunction("Policy.refreshDone"); | 573 web_ui()->CallJavascriptFunction("policy.Page.reloadPoliciesDone"); |
485 } | 574 } |
486 | 575 |
487 void PolicyUIHandler::SendDataToUI() { | 576 policy::PolicyService* PolicyUIHandler::GetPolicyService() const { |
488 policy::PolicyService* service = GetPolicyService(); | 577 return Profile::FromWebUI(web_ui())->GetPolicyService(); |
489 bool any_policies_set = false; | |
490 base::ListValue* list = | |
491 GetPolicyStatusList( | |
492 service->GetPolicies(policy::POLICY_DOMAIN_CHROME, std::string()), | |
493 &any_policies_set).release(); | |
494 base::DictionaryValue results; | |
495 results.Set("policies", list); | |
496 results.SetBoolean("anyPoliciesSet", any_policies_set); | |
497 base::DictionaryValue* dict = GetStatusData(); | |
498 results.Set("status", dict); | |
499 web_ui()->CallJavascriptFunction("Policy.returnData", results); | |
500 } | 578 } |
501 | 579 |
502 policy::PolicyService* PolicyUIHandler::GetPolicyService() { | |
503 Profile* profile = Profile::FromWebUI(web_ui()); | |
504 return profile->GetPolicyService(); | |
505 } | |
506 | |
507 base::DictionaryValue* PolicyUIHandler::GetStatusData() { | |
508 base::DictionaryValue* results = new base::DictionaryValue(); | |
509 | |
510 scoped_ptr<base::DictionaryValue> user_status_dict( | |
511 new base::DictionaryValue()); | |
512 bool user_status_available = | |
513 user_status_provider_->GetStatus(user_status_dict.get()); | |
514 user_status_dict->SetBoolean("display", user_status_available); | |
515 results->Set("userStatus", user_status_dict.release()); | |
516 | |
517 scoped_ptr<base::DictionaryValue> device_status_dict( | |
518 new base::DictionaryValue()); | |
519 bool device_status_available = | |
520 device_status_provider_->GetStatus(device_status_dict.get()); | |
521 device_status_dict->SetString("domain", enterprise_domain_); | |
522 device_status_dict->SetBoolean("display", device_status_available); | |
523 results->Set("deviceStatus", device_status_dict.release()); | |
524 | |
525 results->SetBoolean("displayStatusSection", | |
526 user_status_available || device_status_available); | |
527 return results; | |
528 } | |
529 | |
530 //////////////////////////////////////////////////////////////////////////////// | |
531 // | |
532 // PolicyUI | |
533 // | |
534 //////////////////////////////////////////////////////////////////////////////// | |
535 | |
536 PolicyUI::PolicyUI(content::WebUI* web_ui) : WebUIController(web_ui) { | 580 PolicyUI::PolicyUI(content::WebUI* web_ui) : WebUIController(web_ui) { |
537 web_ui->AddMessageHandler(new PolicyUIHandler); | 581 web_ui->AddMessageHandler(new PolicyUIHandler); |
538 | 582 content::WebUIDataSource::Add(Profile::FromWebUI(web_ui), |
539 // Set up the chrome://policy/ source. | 583 CreatePolicyUIHTMLSource()); |
540 Profile* profile = Profile::FromWebUI(web_ui); | |
541 content::WebUIDataSource::Add(profile, CreatePolicyUIHTMLSource()); | |
542 } | 584 } |
543 | 585 |
544 PolicyUI::~PolicyUI() { | 586 PolicyUI::~PolicyUI() { |
545 } | 587 } |
OLD | NEW |