Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(44)

Side by Side Diff: chrome/browser/ui/webui/policy_ui.cc

Issue 7828042: Implemented status section functionality for about:policy. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: . Created 9 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "chrome/browser/policy/policy_status_info.h" 7 #include "base/i18n/time_formatting.h"
8 #include "base/utf_string_conversions.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/chromeos/login/user_manager.h"
11 #include "chrome/browser/prefs/pref_service.h"
Mattias Nissler (ping if slow) 2011/09/06 11:19:30 alphabetize
simo 2011/09/07 12:18:16 Done.
12 #include "chrome/browser/policy/browser_policy_connector.h"
13 #include "chrome/browser/policy/cloud_policy_cache_base.h"
14 #include "chrome/browser/policy/cloud_policy_data_store.h"
8 #include "chrome/browser/profiles/profile.h" 15 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" 16 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h"
17 #include "chrome/common/time_format.h"
18 #include "chrome/common/pref_names.h"
Mattias Nissler (ping if slow) 2011/09/06 11:19:30 alphabetize
simo 2011/09/07 12:18:16 Done.
10 #include "chrome/common/url_constants.h" 19 #include "chrome/common/url_constants.h"
11 #include "content/browser/tab_contents/tab_contents.h" 20 #include "content/browser/tab_contents/tab_contents.h"
12 #include "grit/browser_resources.h" 21 #include "grit/browser_resources.h"
13 #include "grit/generated_resources.h" 22 #include "grit/generated_resources.h"
14 23
15 ChromeWebUIDataSource* CreatePolicyUIHTMLSource() { 24 ChromeWebUIDataSource* CreatePolicyUIHTMLSource() {
16 ChromeWebUIDataSource* source = 25 ChromeWebUIDataSource* source =
17 new ChromeWebUIDataSource(chrome::kChromeUIPolicyHost); 26 new ChromeWebUIDataSource(chrome::kChromeUIPolicyHost);
18 27
19 // Localized strings. 28 // Localized strings.
20 source->AddLocalizedString("policyTitle", IDS_POLICY_TITLE); 29 source->AddLocalizedString("policyTitle", IDS_POLICY_TITLE);
21 source->AddLocalizedString("statusPaneTitle", IDS_POLICY_STATUS_TITLE); 30 source->AddLocalizedString("statusPaneTitle", IDS_POLICY_STATUS_TITLE);
22 source->AddLocalizedString("fetchPoliciesText", IDS_POLICY_FETCH); 31 source->AddLocalizedString("fetchPoliciesText", IDS_POLICY_FETCH);
23 source->AddLocalizedString("devicePoliciesBoxTitle", 32 source->AddLocalizedString("devicePoliciesBoxTitle",
24 IDS_POLICY_DEVICE_POLICIES); 33 IDS_POLICY_DEVICE_POLICIES);
25 source->AddLocalizedString("userPoliciesBoxTitle", 34 source->AddLocalizedString("userPoliciesBoxTitle",
26 IDS_POLICY_USER_POLICIES); 35 IDS_POLICY_USER_POLICIES);
27 source->AddLocalizedString("enrollmentDomainText", 36 source->AddLocalizedString("enrollmentDomainText",
28 IDS_POLICY_ENROLLMENT_DOMAIN); 37 IDS_POLICY_ENROLLMENT_DOMAIN);
38 source->AddLocalizedString("clientIdText", IDS_POLICY_CLIENT_ID);
39 source->AddLocalizedString("usernameText",
40 IDS_POLICY_USERNAME);
James Hawkins 2011/09/05 22:10:38 This looks like it doesn't need to be wrapped.
simo 2011/09/07 12:18:16 Done.
29 source->AddLocalizedString("lastFetchedText", IDS_POLICY_LAST_FETCHED); 41 source->AddLocalizedString("lastFetchedText", IDS_POLICY_LAST_FETCHED);
30 source->AddLocalizedString("fetchIntervalText", IDS_POLICY_FETCH_INTERVAL); 42 source->AddLocalizedString("fetchIntervalText", IDS_POLICY_FETCH_INTERVAL);
31 source->AddLocalizedString("serverStatusText", IDS_POLICY_SERVER_STATUS); 43 source->AddLocalizedString("serverStatusText", IDS_POLICY_SERVER_STATUS);
32 source->AddLocalizedString("showUnsentPoliciesText", 44 source->AddLocalizedString("showUnsentPoliciesText",
33 IDS_POLICY_SHOW_UNSENT); 45 IDS_POLICY_SHOW_UNSENT);
34 source->AddLocalizedString("filterPoliciesText", IDS_POLICY_FILTER); 46 source->AddLocalizedString("filterPoliciesText", IDS_POLICY_FILTER);
35 source->AddLocalizedString("noPoliciesSet",IDS_POLICY_NO_POLICIES_SET); 47 source->AddLocalizedString("noPoliciesSet",IDS_POLICY_NO_POLICIES_SET);
36 source->AddLocalizedString("appliesToTableHeader", IDS_POLICY_APPLIES_TO); 48 source->AddLocalizedString("appliesToTableHeader", IDS_POLICY_APPLIES_TO);
37 source->AddLocalizedString("policyLevelTableHeader", IDS_POLICY_LEVEL); 49 source->AddLocalizedString("policyLevelTableHeader", IDS_POLICY_LEVEL);
38 source->AddLocalizedString("policyNameTableHeader", IDS_POLICY_ENTRY_NAME); 50 source->AddLocalizedString("policyNameTableHeader", IDS_POLICY_ENTRY_NAME);
39 source->AddLocalizedString("policyValueTableHeader", IDS_POLICY_ENTRY_VALUE); 51 source->AddLocalizedString("policyValueTableHeader", IDS_POLICY_ENTRY_VALUE);
40 source->AddLocalizedString("policyStatusTableHeader", 52 source->AddLocalizedString("policyStatusTableHeader",
41 IDS_POLICY_ENTRY_STATUS); 53 IDS_POLICY_ENTRY_STATUS);
54 source->AddLocalizedString("showMoreText", IDS_POLICY_SHOW_MORE);
55 source->AddLocalizedString("hideText", IDS_POLICY_HIDE);
42 source->set_json_path("strings.js"); 56 source->set_json_path("strings.js");
43 57
44 // Add required resources. 58 // Add required resources.
45 source->add_resource_path("policy.css", IDR_POLICY_CSS); 59 source->add_resource_path("policy.css", IDR_POLICY_CSS);
46 source->add_resource_path("policy.js", IDR_POLICY_JS); 60 source->add_resource_path("policy.js", IDR_POLICY_JS);
47 source->set_default_resource(IDR_POLICY_HTML); 61 source->set_default_resource(IDR_POLICY_HTML);
48 62
49 return source; 63 return source;
50 } 64 }
51 65
52 //////////////////////////////////////////////////////////////////////////////// 66 ////////////////////////////////////////////////////////////////////////////////
53 // 67 //
54 // PolicyUIHandler 68 // PolicyUIHandler
55 // 69 //
56 //////////////////////////////////////////////////////////////////////////////// 70 ////////////////////////////////////////////////////////////////////////////////
57 71
58 PolicyUIHandler::PolicyUIHandler() { 72 PolicyUIHandler::PolicyUIHandler() {
59 policy::ConfigurationPolicyReader* managed_platform = 73 policy::ConfigurationPolicyReader* managed_platform =
60 ConfigurationPolicyReader::CreateManagedPlatformPolicyReader(); 74 ConfigurationPolicyReader::CreateManagedPlatformPolicyReader();
61 policy::ConfigurationPolicyReader* managed_cloud = 75 policy::ConfigurationPolicyReader* managed_cloud =
62 ConfigurationPolicyReader::CreateManagedCloudPolicyReader(); 76 ConfigurationPolicyReader::CreateManagedCloudPolicyReader();
63 policy::ConfigurationPolicyReader* recommended_platform = 77 policy::ConfigurationPolicyReader* recommended_platform =
64 ConfigurationPolicyReader::CreateRecommendedPlatformPolicyReader(); 78 ConfigurationPolicyReader::CreateRecommendedPlatformPolicyReader();
65 policy::ConfigurationPolicyReader* recommended_cloud = 79 policy::ConfigurationPolicyReader* recommended_cloud =
66 ConfigurationPolicyReader::CreateRecommendedCloudPolicyReader(); 80 ConfigurationPolicyReader::CreateRecommendedCloudPolicyReader();
67 policy_status_.reset(new policy::PolicyStatus(managed_platform, 81 policy_status_.reset(new policy::PolicyStatus(managed_platform,
68 managed_cloud, 82 managed_cloud,
69 recommended_platform, 83 recommended_platform,
70 recommended_cloud)); 84 recommended_cloud));
85 policy_status_->AddObserver(this);
71 } 86 }
72 87
73 PolicyUIHandler::~PolicyUIHandler() { 88 PolicyUIHandler::~PolicyUIHandler() {
89 policy_status_->RemoveObserver(this);
74 } 90 }
75 91
76 WebUIMessageHandler* PolicyUIHandler::Attach(WebUI* web_ui) { 92 WebUIMessageHandler* PolicyUIHandler::Attach(WebUI* web_ui) {
77 return WebUIMessageHandler::Attach(web_ui); 93 return WebUIMessageHandler::Attach(web_ui);
78 } 94 }
79 95
80 void PolicyUIHandler::RegisterMessages() { 96 void PolicyUIHandler::RegisterMessages() {
81 web_ui_->RegisterMessageCallback("requestPolicyData", 97 web_ui_->RegisterMessageCallback("requestData",
82 NewCallback(this, &PolicyUIHandler::HandleRequestPolicyData)); 98 NewCallback(this, &PolicyUIHandler::HandleRequestData));
99 web_ui_->RegisterMessageCallback("fetchPolicy",
100 NewCallback(this, &PolicyUIHandler::HandleFetchPolicy));
83 } 101 }
84 102
85 void PolicyUIHandler::HandleRequestPolicyData(const ListValue* args) { 103 void PolicyUIHandler::OnPolicyValuesChanged() {
104 SendDataToUI(true);
105 }
106
107 void PolicyUIHandler::HandleRequestData(const ListValue* args) {
108 SendDataToUI(false);
109 }
110
111 void PolicyUIHandler::HandleFetchPolicy(const ListValue* args) {
112 if (FetchPolicyIfTokensAvailable())
113 SendDataToUI(true);
114 }
115
116 void PolicyUIHandler::SendDataToUI(bool is_policy_update) {
117 DictionaryValue results;
86 bool any_policies_sent; 118 bool any_policies_sent;
Mattias Nissler (ping if slow) 2011/09/06 11:19:30 should this rather be any_policies_set?
simo 2011/09/07 12:18:16 Done.
87 ListValue* list = policy_status_->GetPolicyStatusList(&any_policies_sent); 119 ListValue* list = policy_status_->GetPolicyStatusList(&any_policies_sent);
88 DictionaryValue results;
89 results.Set("policies", list); 120 results.Set("policies", list);
90 results.SetBoolean("anyPoliciesSet", any_policies_sent); 121 results.SetBoolean("anyPoliciesSet", any_policies_sent);
91 web_ui_->CallJavascriptFunction("Policy.returnPolicyData", results); 122 DictionaryValue* dict = GetStatusData();
James Hawkins 2011/09/05 22:10:38 Leak?
simo 2011/09/07 12:18:16 |dict| is added to the |results| DictionaryValue w
123 results.Set("status", dict);
124 results.SetBoolean("isPolicyUpdate", is_policy_update);
Mattias Nissler (ping if slow) 2011/09/06 11:19:30 do we need to pass this flag explicitly? I think i
simo 2011/09/07 12:18:16 is_policy_update is set to true after an OnPolicyV
Mattias Nissler (ping if slow) 2011/09/07 12:50:48 I see.
125
126 web_ui_->CallJavascriptFunction("Policy.returnData", results);
127 }
128
129 DictionaryValue* PolicyUIHandler::GetStatusData() {
130 DictionaryValue* results = new DictionaryValue();
131 policy::BrowserPolicyConnector* connector =
132 g_browser_process->browser_policy_connector();
133
134 policy::CloudPolicySubsystem* device_subsystem =
135 connector->device_cloud_policy_subsystem();
136 policy::CloudPolicySubsystem* user_subsystem =
137 connector->user_cloud_policy_subsystem();
138
139 // If no CloudPolicySubsystem is available, the status section is not
140 // displayed and we can return here.
141 if (!device_subsystem || !user_subsystem) {
142 results->SetBoolean("displayStatusSection", false);
143 return results;
144 } else {
145 results->SetBoolean("displayStatusSection", true);
146 }
147
148 // Get the server status message and the time at which policy was last fetched
149 // for both user and device policy from the appropriate CloudPolicySubsystem.
150 results->SetString("deviceStatusMessage",
151 CreateStatusMessageString(device_subsystem->error_details()));
152 results->SetString("deviceLastFetchTime", GetLastFetchTime(device_subsystem));
153 results->SetString("userStatusMessage",
154 CreateStatusMessageString(user_subsystem->error_details()));
155 results->SetString("userLastFetchTime", GetLastFetchTime(user_subsystem));
156
157 // Get enterprise domain and username (only on ChromeOS).
158 #if defined (OS_CHROMEOS)
159 chromeos::UserManager::User user =
160 chromeos::UserManager::Get()->logged_in_user();
161 results->SetString("devicePolicyDomain",
162 ASCIIToUTF16(connector->GetEnterpriseDomain()));
163 results->SetString("user", ASCIIToUTF16(user.email()));
164 #else
165 results->SetString("devicePolicyDomain", string16());
166 results->SetString("user", string16());
167 #endif
168
169 GetDeviceIds(results);
Mattias Nissler (ping if slow) 2011/09/06 11:19:30 In the previous comments, I was actually referring
simo 2011/09/07 12:18:16 Done.
170 GetPolicyFetchIntervals(results);
Mattias Nissler (ping if slow) 2011/09/06 11:19:30 Same here.
simo 2011/09/07 12:18:16 Done.
171
172 return results;
173 }
174
175 bool PolicyUIHandler::FetchPolicyIfTokensAvailable() {
176 policy::BrowserPolicyConnector* connector =
177 g_browser_process->browser_policy_connector();
178 const policy::CloudPolicyDataStore* device_data_store =
179 connector->GetDeviceCloudPolicyDataStore();
180 const policy::CloudPolicyDataStore* user_data_store =
181 connector->GetUserCloudPolicyDataStore();
182
183 bool fetch_device_policy =
184 device_data_store && !device_data_store->device_token().empty();
185 bool fetch_user_policy =
186 user_data_store && !user_data_store->device_token().empty();
187
188 if (fetch_device_policy)
189 connector->FetchDevicePolicy();
190 if (fetch_user_policy)
James Hawkins 2011/09/05 22:10:38 I'd add a blank line here for readability: I initi
simo 2011/09/07 12:18:16 Done.
191 connector->FetchUserPolicy();
192
193 return fetch_device_policy || fetch_user_policy;
194 }
195
196 string16 PolicyUIHandler::GetLastFetchTime(
197 policy::CloudPolicySubsystem* subsystem) {
198 policy::CloudPolicyCacheBase* cache_base =
199 subsystem->GetCloudPolicyCacheBase();
James Hawkins 2011/09/05 22:10:38 Indentation.
simo 2011/09/07 12:18:16 Done.
200 base::TimeDelta time_delta =
201 base::Time::NowFromSystemTime() - cache_base->last_policy_refresh_time();
202
203 return TimeFormat::TimeElapsed(time_delta);
204 }
205
206 void PolicyUIHandler::GetDeviceIds(DictionaryValue* results) {
207 policy::BrowserPolicyConnector* connector =
208 g_browser_process->browser_policy_connector();
209 const policy::CloudPolicyDataStore* device_data_store =
210 connector->GetDeviceCloudPolicyDataStore();
211 const policy::CloudPolicyDataStore* user_data_store =
212 connector->GetUserCloudPolicyDataStore();
213
214 if (device_data_store) {
215 results->SetString("deviceId",
216 ASCIIToUTF16(device_data_store->device_id()));
217 } else {
218 results->SetString("deviceId", string16());
219 }
220
221 if (user_data_store)
222 results->SetString("userId", ASCIIToUTF16(user_data_store->device_id()));
223 else
224 results->SetString("userId", string16());
225 }
226
227 void PolicyUIHandler::GetPolicyFetchIntervals(DictionaryValue* results) {
228 PrefService* prefs = g_browser_process->local_state();
229 base::TimeDelta device_fetch_interval = base::TimeDelta::FromMilliseconds(
230 prefs->GetInteger(prefs::kDevicePolicyRefreshRate));
231 base::TimeDelta user_fetch_interval = base::TimeDelta::FromMilliseconds(
232 prefs->GetInteger(prefs::kUserPolicyRefreshRate));
233
234 results->SetString("deviceFetchInterval",
235 TimeFormat::TimeRemainingShort(device_fetch_interval));
236 results->SetString("userFetchInterval",
237 TimeFormat::TimeRemainingShort(user_fetch_interval));
238 }
239
240 // static
241 string16 PolicyUIHandler::CreateStatusMessageString(
242 CloudPolicySubsystem::ErrorDetails error_details) {
243 static string16 error_to_string[] = { ASCIIToUTF16("OK."),
244 ASCIIToUTF16("Network error."),
245 ASCIIToUTF16("Network error."),
246 ASCIIToUTF16("Bad DMToken."),
247 ASCIIToUTF16("Local error."),
248 ASCIIToUTF16("Signature mismatch.") };
249 DCHECK(static_cast<size_t>(error_details) < arraysize(error_to_string));
250 return error_to_string[error_details];
92 } 251 }
93 252
94 //////////////////////////////////////////////////////////////////////////////// 253 ////////////////////////////////////////////////////////////////////////////////
95 // 254 //
96 // PolicyUI 255 // PolicyUI
97 // 256 //
98 //////////////////////////////////////////////////////////////////////////////// 257 ////////////////////////////////////////////////////////////////////////////////
99 258
100 PolicyUI::PolicyUI(TabContents* contents) : ChromeWebUI(contents) { 259 PolicyUI::PolicyUI(TabContents* contents) : ChromeWebUI(contents) {
101 AddMessageHandler((new PolicyUIHandler)->Attach(this)); 260 AddMessageHandler((new PolicyUIHandler)->Attach(this));
102 261
103 // Set up the chrome://policy/ source. 262 // Set up the chrome://policy/ source.
104 Profile* profile = Profile::FromBrowserContext(contents->browser_context()); 263 Profile* profile = Profile::FromBrowserContext(contents->browser_context());
105 profile->GetChromeURLDataManager()->AddDataSource(CreatePolicyUIHTMLSource()); 264 profile->GetChromeURLDataManager()->AddDataSource(CreatePolicyUIHTMLSource());
106 } 265 }
107 266
108 PolicyUI::~PolicyUI() { 267 PolicyUI::~PolicyUI() {
109 } 268 }
OLDNEW
« chrome/browser/ui/webui/policy_ui.h ('K') | « chrome/browser/ui/webui/policy_ui.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698