Chromium Code Reviews| OLD | NEW |
|---|---|
| 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" | |
| 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" |
| 16 #include "chrome/browser/profiles/profile_manager.h" | |
|
Mattias Nissler (ping if slow)
2011/09/02 11:16:09
What do you need ProfileManager for?
simo
2011/09/02 15:32:02
Oh, nothing, forgot to take that out.
| |
| 9 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" | 17 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" |
| 18 #include "chrome/common/time_format.h" | |
| 19 #include "chrome/common/pref_names.h" | |
| 10 #include "chrome/common/url_constants.h" | 20 #include "chrome/common/url_constants.h" |
| 11 #include "content/browser/tab_contents/tab_contents.h" | 21 #include "content/browser/tab_contents/tab_contents.h" |
| 12 #include "grit/browser_resources.h" | 22 #include "grit/browser_resources.h" |
| 13 #include "grit/generated_resources.h" | 23 #include "grit/generated_resources.h" |
| 14 | 24 |
| 15 ChromeWebUIDataSource* CreatePolicyUIHTMLSource() { | 25 ChromeWebUIDataSource* CreatePolicyUIHTMLSource() { |
| 16 ChromeWebUIDataSource* source = | 26 ChromeWebUIDataSource* source = |
| 17 new ChromeWebUIDataSource(chrome::kChromeUIPolicyHost); | 27 new ChromeWebUIDataSource(chrome::kChromeUIPolicyHost); |
| 18 | 28 |
| 19 // Localized strings. | 29 // Localized strings. |
| 20 source->AddLocalizedString("policyTitle", IDS_POLICY_TITLE); | 30 source->AddLocalizedString("policyTitle", IDS_POLICY_TITLE); |
| 21 source->AddLocalizedString("statusPaneTitle", IDS_POLICY_STATUS_TITLE); | 31 source->AddLocalizedString("statusPaneTitle", IDS_POLICY_STATUS_TITLE); |
| 22 source->AddLocalizedString("fetchPoliciesText", IDS_POLICY_FETCH); | 32 source->AddLocalizedString("fetchPoliciesText", IDS_POLICY_FETCH); |
| 23 source->AddLocalizedString("devicePoliciesBoxTitle", | 33 source->AddLocalizedString("devicePoliciesBoxTitle", |
| 24 IDS_POLICY_DEVICE_POLICIES); | 34 IDS_POLICY_DEVICE_POLICIES); |
| 25 source->AddLocalizedString("userPoliciesBoxTitle", | 35 source->AddLocalizedString("userPoliciesBoxTitle", |
| 26 IDS_POLICY_USER_POLICIES); | 36 IDS_POLICY_USER_POLICIES); |
| 27 source->AddLocalizedString("enrollmentDomainText", | 37 source->AddLocalizedString("enrollmentDomainText", |
| 28 IDS_POLICY_ENROLLMENT_DOMAIN); | 38 IDS_POLICY_ENROLLMENT_DOMAIN); |
| 39 source->AddLocalizedString("deviceIdText", IDS_POLICY_DEVICE_ID); | |
| 40 source->AddLocalizedString("usernameText", | |
| 41 IDS_POLICY_USERNAME); | |
| 42 source->AddLocalizedString("userIdText", IDS_POLICY_USER_ID); | |
| 29 source->AddLocalizedString("lastFetchedText", IDS_POLICY_LAST_FETCHED); | 43 source->AddLocalizedString("lastFetchedText", IDS_POLICY_LAST_FETCHED); |
| 30 source->AddLocalizedString("fetchIntervalText", IDS_POLICY_FETCH_INTERVAL); | 44 source->AddLocalizedString("fetchIntervalText", IDS_POLICY_FETCH_INTERVAL); |
| 31 source->AddLocalizedString("serverStatusText", IDS_POLICY_SERVER_STATUS); | 45 source->AddLocalizedString("serverStatusText", IDS_POLICY_SERVER_STATUS); |
| 32 source->AddLocalizedString("showUnsentPoliciesText", | 46 source->AddLocalizedString("showUnsentPoliciesText", |
| 33 IDS_POLICY_SHOW_UNSENT); | 47 IDS_POLICY_SHOW_UNSENT); |
| 34 source->AddLocalizedString("filterPoliciesText", IDS_POLICY_FILTER); | 48 source->AddLocalizedString("filterPoliciesText", IDS_POLICY_FILTER); |
| 35 source->AddLocalizedString("noPoliciesSet",IDS_POLICY_NO_POLICIES_SET); | 49 source->AddLocalizedString("noPoliciesSet",IDS_POLICY_NO_POLICIES_SET); |
| 36 source->AddLocalizedString("appliesToTableHeader", IDS_POLICY_APPLIES_TO); | 50 source->AddLocalizedString("appliesToTableHeader", IDS_POLICY_APPLIES_TO); |
| 37 source->AddLocalizedString("policyLevelTableHeader", IDS_POLICY_LEVEL); | 51 source->AddLocalizedString("policyLevelTableHeader", IDS_POLICY_LEVEL); |
| 38 source->AddLocalizedString("policyNameTableHeader", IDS_POLICY_ENTRY_NAME); | 52 source->AddLocalizedString("policyNameTableHeader", IDS_POLICY_ENTRY_NAME); |
| 39 source->AddLocalizedString("policyValueTableHeader", IDS_POLICY_ENTRY_VALUE); | 53 source->AddLocalizedString("policyValueTableHeader", IDS_POLICY_ENTRY_VALUE); |
| 40 source->AddLocalizedString("policyStatusTableHeader", | 54 source->AddLocalizedString("policyStatusTableHeader", |
| 41 IDS_POLICY_ENTRY_STATUS); | 55 IDS_POLICY_ENTRY_STATUS); |
| 56 source->AddLocalizedString("showMoreText", IDS_POLICY_SHOW_MORE); | |
| 57 source->AddLocalizedString("hideText", IDS_POLICY_HIDE); | |
| 42 source->set_json_path("strings.js"); | 58 source->set_json_path("strings.js"); |
| 43 | 59 |
| 44 // Add required resources. | 60 // Add required resources. |
| 45 source->add_resource_path("policy.css", IDR_POLICY_CSS); | 61 source->add_resource_path("policy.css", IDR_POLICY_CSS); |
| 46 source->add_resource_path("policy.js", IDR_POLICY_JS); | 62 source->add_resource_path("policy.js", IDR_POLICY_JS); |
| 47 source->set_default_resource(IDR_POLICY_HTML); | 63 source->set_default_resource(IDR_POLICY_HTML); |
| 48 | 64 |
| 49 return source; | 65 return source; |
| 50 } | 66 } |
| 51 | 67 |
| 52 //////////////////////////////////////////////////////////////////////////////// | 68 //////////////////////////////////////////////////////////////////////////////// |
| 53 // | 69 // |
| 54 // PolicyUIHandler | 70 // PolicyUIHandler |
| 55 // | 71 // |
| 56 //////////////////////////////////////////////////////////////////////////////// | 72 //////////////////////////////////////////////////////////////////////////////// |
| 57 | 73 |
| 58 PolicyUIHandler::PolicyUIHandler() { | 74 PolicyUIHandler::PolicyUIHandler() { |
| 59 policy::ConfigurationPolicyReader* managed_platform = | 75 policy::ConfigurationPolicyReader* managed_platform = |
| 60 ConfigurationPolicyReader::CreateManagedPlatformPolicyReader(); | 76 ConfigurationPolicyReader::CreateManagedPlatformPolicyReader(); |
| 61 policy::ConfigurationPolicyReader* managed_cloud = | 77 policy::ConfigurationPolicyReader* managed_cloud = |
| 62 ConfigurationPolicyReader::CreateManagedCloudPolicyReader(); | 78 ConfigurationPolicyReader::CreateManagedCloudPolicyReader(); |
| 63 policy::ConfigurationPolicyReader* recommended_platform = | 79 policy::ConfigurationPolicyReader* recommended_platform = |
| 64 ConfigurationPolicyReader::CreateRecommendedPlatformPolicyReader(); | 80 ConfigurationPolicyReader::CreateRecommendedPlatformPolicyReader(); |
| 65 policy::ConfigurationPolicyReader* recommended_cloud = | 81 policy::ConfigurationPolicyReader* recommended_cloud = |
| 66 ConfigurationPolicyReader::CreateRecommendedCloudPolicyReader(); | 82 ConfigurationPolicyReader::CreateRecommendedCloudPolicyReader(); |
| 67 policy_status_.reset(new policy::PolicyStatus(managed_platform, | 83 policy_status_.reset(new policy::PolicyStatus(managed_platform, |
| 68 managed_cloud, | 84 managed_cloud, |
| 69 recommended_platform, | 85 recommended_platform, |
| 70 recommended_cloud)); | 86 recommended_cloud)); |
| 87 policy_status_->AddObserver(this); | |
| 71 } | 88 } |
| 72 | 89 |
| 73 PolicyUIHandler::~PolicyUIHandler() { | 90 PolicyUIHandler::~PolicyUIHandler() { |
| 91 policy_status_->RemoveObserver(this); | |
| 74 } | 92 } |
| 75 | 93 |
| 76 WebUIMessageHandler* PolicyUIHandler::Attach(WebUI* web_ui) { | 94 WebUIMessageHandler* PolicyUIHandler::Attach(WebUI* web_ui) { |
| 77 return WebUIMessageHandler::Attach(web_ui); | 95 return WebUIMessageHandler::Attach(web_ui); |
| 78 } | 96 } |
| 79 | 97 |
| 80 void PolicyUIHandler::RegisterMessages() { | 98 void PolicyUIHandler::RegisterMessages() { |
| 81 web_ui_->RegisterMessageCallback("requestPolicyData", | 99 web_ui_->RegisterMessageCallback("requestData", |
| 82 NewCallback(this, &PolicyUIHandler::HandleRequestPolicyData)); | 100 NewCallback(this, &PolicyUIHandler::HandleRequestData)); |
| 101 web_ui_->RegisterMessageCallback("updateFetchTimes", | |
| 102 NewCallback(this, &PolicyUIHandler::HandleUpdateFetchTimes)); | |
| 103 web_ui_->RegisterMessageCallback("fetchPolicy", | |
| 104 NewCallback(this, &PolicyUIHandler::HandleFetchPolicy)); | |
| 83 } | 105 } |
| 84 | 106 |
| 85 void PolicyUIHandler::HandleRequestPolicyData(const ListValue* args) { | 107 void PolicyUIHandler::OnPolicyValuesChanged() { |
| 108 SendDataToUI(true); | |
| 109 } | |
| 110 | |
| 111 void PolicyUIHandler::HandleRequestData(const ListValue* args) { | |
| 112 SendDataToUI(false); | |
| 113 } | |
| 114 | |
| 115 void PolicyUIHandler::HandleUpdateFetchTimes(const ListValue* args) { | |
|
Mattias Nissler (ping if slow)
2011/09/02 11:16:09
I see that you poll for these from the JS side. I
simo
2011/09/02 15:32:02
Since I can't find any time formatting in javascri
| |
| 116 policy::BrowserPolicyConnector* connector = | |
| 117 g_browser_process->browser_policy_connector(); | |
| 118 policy::CloudPolicySubsystem* device_subsystem = | |
| 119 connector->device_cloud_policy_subsystem(); | |
| 120 policy::CloudPolicySubsystem* user_subsystem = | |
| 121 connector->user_cloud_policy_subsystem(); | |
| 122 DictionaryValue result; | |
| 123 | |
| 124 if (device_subsystem) | |
| 125 result.SetString("deviceLastFetchTime", | |
| 126 GetLastFetchTime(device_subsystem)); | |
| 127 if (user_subsystem) | |
| 128 result.SetString("userLastFetchTime", | |
| 129 GetLastFetchTime(user_subsystem)); | |
| 130 | |
| 131 web_ui_->CallJavascriptFunction("Policy.returnFetchTimes", result); | |
| 132 } | |
| 133 | |
| 134 void PolicyUIHandler::HandleFetchPolicy(const ListValue* args) { | |
| 135 policy::BrowserPolicyConnector* connector = | |
| 136 g_browser_process->browser_policy_connector(); | |
| 137 connector->FetchDevicePolicy(); | |
| 138 connector->FetchUserPolicy(); | |
| 139 SendDataToUI(true); | |
| 140 } | |
| 141 | |
| 142 void PolicyUIHandler::SendDataToUI(bool is_policy_update) { | |
| 143 DictionaryValue results; | |
| 86 bool any_policies_sent; | 144 bool any_policies_sent; |
| 87 ListValue* list = policy_status_->GetPolicyStatusList(&any_policies_sent); | 145 ListValue* list = policy_status_->GetPolicyStatusList(&any_policies_sent); |
| 88 DictionaryValue results; | |
| 89 results.Set("policies", list); | 146 results.Set("policies", list); |
| 90 results.SetBoolean("anyPoliciesSet", any_policies_sent); | 147 results.SetBoolean("anyPoliciesSet", any_policies_sent); |
| 91 web_ui_->CallJavascriptFunction("Policy.returnPolicyData", results); | 148 DictionaryValue* dict = GetStatusData(is_policy_update); |
| 149 results.Set("status", dict); | |
| 150 | |
| 151 if (is_policy_update) | |
| 152 web_ui_->CallJavascriptFunction("Policy.updatePolicyData", results); | |
| 153 else | |
| 154 web_ui_->CallJavascriptFunction("Policy.returnData", results); | |
| 155 } | |
| 156 | |
| 157 DictionaryValue* PolicyUIHandler::GetStatusData(bool is_policy_update) { | |
| 158 DictionaryValue* results = new DictionaryValue(); | |
| 159 policy::BrowserPolicyConnector* connector = | |
| 160 g_browser_process->browser_policy_connector(); | |
| 161 | |
| 162 policy::CloudPolicySubsystem* device_subsystem = | |
| 163 connector->device_cloud_policy_subsystem(); | |
| 164 policy::CloudPolicySubsystem* user_subsystem = | |
| 165 connector->user_cloud_policy_subsystem(); | |
| 166 | |
| 167 // If no CloudPolicySubsystem is available, the status section is not | |
| 168 // displayed and we can return here. | |
| 169 if (!device_subsystem || !user_subsystem) { | |
| 170 results->SetBoolean("displayStatusSection", false); | |
| 171 return results; | |
| 172 } else { | |
| 173 results->SetBoolean("displayStatusSection", true); | |
| 174 } | |
| 175 | |
| 176 // Get the server status message and the time at which policy was last fetched | |
| 177 // for both user and device policy from the appropriate CloudPolicySubsystem. | |
| 178 string16 device_status_message = | |
| 179 CreateStatusMessageString(device_subsystem->error_details()); | |
| 180 string16 user_status_message = | |
| 181 CreateStatusMessageString(user_subsystem->error_details()); | |
| 182 | |
| 183 string16 device_last_fetch_time = GetLastFetchTime(device_subsystem); | |
| 184 string16 user_last_fetch_time = GetLastFetchTime(user_subsystem); | |
|
Mattias Nissler (ping if slow)
2011/09/02 11:16:09
No need for all the local variables, just put the
simo
2011/09/02 15:32:02
Done.
| |
| 185 | |
| 186 results->SetString("deviceStatusMessage", device_status_message); | |
| 187 results->SetString("deviceLastFetchTime", device_last_fetch_time); | |
| 188 results->SetString("userStatusMessage", user_status_message); | |
| 189 results->SetString("userLastFetchTime", user_last_fetch_time); | |
| 190 | |
| 191 // Get device id and user id. | |
| 192 const policy::CloudPolicyDataStore* device_data_store = | |
| 193 connector->GetDeviceCloudPolicyDataStore(); | |
| 194 const policy::CloudPolicyDataStore* user_data_store = | |
| 195 connector->GetUserCloudPolicyDataStore(); | |
| 196 | |
| 197 string16 device_id = device_data_store ? | |
| 198 ASCIIToUTF16(device_data_store->device_id()) : string16(); | |
| 199 string16 user_id = user_data_store ? | |
| 200 ASCIIToUTF16(user_data_store->device_id()) : string16(); | |
| 201 results->SetString("deviceId", device_id); | |
| 202 results->SetString("userId", user_id); | |
|
Mattias Nissler (ping if slow)
2011/09/02 11:16:09
Looks like for the identifiers, you could also int
simo
2011/09/02 15:32:02
Done.
| |
| 203 | |
| 204 // Get enterprise domain and username (only on ChromeOS). | |
| 205 #if defined (OS_CHROMEOS) | |
| 206 string16 enterprise_domain = ASCIIToUTF16(connector->GetEnterpriseDomain()); | |
| 207 chromeos::UserManager::User user = | |
| 208 chromeos::UserManager::Get()->logged_in_user(); | |
| 209 string16 username = ASCIIToUTF16(user.email()); | |
| 210 results->SetString("devicePolicyDomain", enterprise_domain); | |
| 211 results->SetString("user", username); | |
|
Mattias Nissler (ping if slow)
2011/09/02 11:16:09
Again, no need for local variables.
simo
2011/09/02 15:32:02
Done.
| |
| 212 #else | |
| 213 results->SetString("devicePolicyDomain", string16()); | |
| 214 results->SetString("user", string16()); | |
| 215 #endif | |
| 216 | |
| 217 // Get policy fetch interval for both device and user policy. | |
| 218 PrefService* prefs = g_browser_process->local_state(); | |
| 219 | |
| 220 base::TimeDelta device_fetch_interval = base::TimeDelta::FromMilliseconds( | |
| 221 prefs->GetInteger(prefs::kDevicePolicyRefreshRate)); | |
|
Mattias Nissler (ping if slow)
2011/09/02 11:16:09
Another good case for a helper function that wraps
simo
2011/09/02 15:32:02
Done.
| |
| 222 base::TimeDelta user_fetch_interval = base::TimeDelta::FromMilliseconds( | |
| 223 prefs->GetInteger(prefs::kUserPolicyRefreshRate)); | |
| 224 | |
| 225 results->SetString("deviceFetchInterval", | |
| 226 TimeFormat::TimeRemainingShort(device_fetch_interval)); | |
| 227 results->SetString("userFetchInterval", | |
| 228 TimeFormat::TimeRemainingShort(user_fetch_interval)); | |
| 229 | |
| 230 return results; | |
| 231 } | |
| 232 | |
| 233 string16 PolicyUIHandler::GetLastFetchTime( | |
| 234 policy::CloudPolicySubsystem* subsystem) { | |
| 235 policy::CloudPolicyCacheBase* cache_base = | |
| 236 subsystem->GetCloudPolicyCacheBase(); | |
| 237 base::TimeDelta time_delta = | |
| 238 base::Time::NowFromSystemTime() - cache_base->last_policy_refresh_time(); | |
| 239 | |
| 240 return TimeFormat::TimeElapsed(time_delta); | |
| 241 } | |
| 242 | |
| 243 // static | |
| 244 string16 PolicyUIHandler::CreateStatusMessageString( | |
| 245 CloudPolicySubsystem::ErrorDetails error_details) { | |
| 246 static string16 error_to_string[] = { ASCIIToUTF16("OK."), | |
| 247 ASCIIToUTF16("Network error."), | |
| 248 ASCIIToUTF16("Network error."), | |
| 249 ASCIIToUTF16("Bad DMToken."), | |
| 250 ASCIIToUTF16("Local error."), | |
| 251 ASCIIToUTF16("Signature mismatch.") }; | |
| 252 DCHECK(static_cast<size_t>(error_details) < arraysize(error_to_string)); | |
| 253 return error_to_string[error_details]; | |
| 92 } | 254 } |
| 93 | 255 |
| 94 //////////////////////////////////////////////////////////////////////////////// | 256 //////////////////////////////////////////////////////////////////////////////// |
| 95 // | 257 // |
| 96 // PolicyUI | 258 // PolicyUI |
| 97 // | 259 // |
| 98 //////////////////////////////////////////////////////////////////////////////// | 260 //////////////////////////////////////////////////////////////////////////////// |
| 99 | 261 |
| 100 PolicyUI::PolicyUI(TabContents* contents) : ChromeWebUI(contents) { | 262 PolicyUI::PolicyUI(TabContents* contents) : ChromeWebUI(contents) { |
| 101 AddMessageHandler((new PolicyUIHandler)->Attach(this)); | 263 AddMessageHandler((new PolicyUIHandler)->Attach(this)); |
| 102 | 264 |
| 103 // Set up the chrome://policy/ source. | 265 // Set up the chrome://policy/ source. |
| 104 Profile* profile = Profile::FromBrowserContext(contents->browser_context()); | 266 Profile* profile = Profile::FromBrowserContext(contents->browser_context()); |
| 105 profile->GetChromeURLDataManager()->AddDataSource(CreatePolicyUIHTMLSource()); | 267 profile->GetChromeURLDataManager()->AddDataSource(CreatePolicyUIHTMLSource()); |
| 106 } | 268 } |
| 107 | 269 |
| 108 PolicyUI::~PolicyUI() { | 270 PolicyUI::~PolicyUI() { |
| 109 } | 271 } |
| OLD | NEW |