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