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 3 // found in the LICENSE file. 4 5 #include "chrome/browser/ui/website_settings/website_settings_ui.h" 6 7 #include "grit/generated_resources.h" 8 #include "grit/theme_resources.h" 9 #include "grit/ui_resources.h" 10 #include "ui/base/l10n/l10n_util.h" 11 #include "ui/base/resource/resource_bundle.h" 12 #include "ui/gfx/image/image.h" 13 14 namespace { 15 16 const int kInvalidResourceID = -1; 17 18 // The resource id's for the strings that are displayed on the permissions 19 // button if the permission setting is managed by policy. 20 const int kPermissionButtonTextIDPolicyManaged[] = { 21 kInvalidResourceID, 22 IDS_WEBSITE_SETTINGS_BUTTON_TEXT_ALLOWED_BY_POLICY, 23 IDS_WEBSITE_SETTINGS_BUTTON_TEXT_BLOCKED_BY_POLICY, 24 kInvalidResourceID, 25 kInvalidResourceID 26 }; 27 COMPILE_ASSERT(arraysize(kPermissionButtonTextIDPolicyManaged) == 28 CONTENT_SETTING_NUM_SETTINGS, 29 button_text_id_array_size_incorrect); 30 31 // The resource id's for the strings that are displayed on the permissions 32 // button if the permission setting is managed by an extension. 33 const int kPermissionButtonTextIDExtensionManaged[] = { 34 kInvalidResourceID, 35 IDS_WEBSITE_SETTINGS_BUTTON_TEXT_ALLOWED_BY_EXTENSION, 36 IDS_WEBSITE_SETTINGS_BUTTON_TEXT_BLOCKED_BY_EXTENSION, 37 kInvalidResourceID, 38 kInvalidResourceID 39 }; 40 COMPILE_ASSERT(arraysize(kPermissionButtonTextIDExtensionManaged) == 41 CONTENT_SETTING_NUM_SETTINGS, 42 button_text_id_array_size_incorrect); 43 44 // The resource id's for the strings that are displayed on the permissions 45 // button if the permission setting is managed by the user. 46 const int kPermissionButtonTextIDUserManaged[] = { 47 kInvalidResourceID, 48 IDS_WEBSITE_SETTINGS_BUTTON_TEXT_ALLOWED_BY_USER, 49 IDS_WEBSITE_SETTINGS_BUTTON_TEXT_BLOCKED_BY_USER, 50 kInvalidResourceID, 51 kInvalidResourceID 52 }; 53 COMPILE_ASSERT(arraysize(kPermissionButtonTextIDUserManaged) == 54 CONTENT_SETTING_NUM_SETTINGS, 55 button_text_id_array_size_incorrect); 56 57 // The resource id's for the strings that are displayed on the permissions 58 // button if the permission setting is the global default setting. 59 const int kPermissionButtonTextIDDefaultSetting[] = { 60 kInvalidResourceID, 61 IDS_WEBSITE_SETTINGS_BUTTON_TEXT_ALLOWED_BY_DEFAULT, 62 IDS_WEBSITE_SETTINGS_BUTTON_TEXT_BLOCKED_BY_DEFAULT, 63 IDS_WEBSITE_SETTINGS_BUTTON_TEXT_ASK_BY_DEFAULT, 64 kInvalidResourceID 65 }; 66 COMPILE_ASSERT(arraysize(kPermissionButtonTextIDDefaultSetting) == 67 CONTENT_SETTING_NUM_SETTINGS, 68 button_text_id_array_size_incorrect); 69 70 } // namespace 71 72 WebsiteSettingsUI::CookieInfo::CookieInfo() 73 : allowed(-1), blocked(-1) { 74 } 75 76 WebsiteSettingsUI::PermissionInfo::PermissionInfo() 77 : type(CONTENT_SETTINGS_TYPE_DEFAULT), 78 setting(CONTENT_SETTING_DEFAULT), 79 default_setting(CONTENT_SETTING_DEFAULT), 80 source(content_settings::SETTING_SOURCE_NONE) { 81 } 82 83 WebsiteSettingsUI::IdentityInfo::IdentityInfo() 84 : identity_status(WebsiteSettings::SITE_IDENTITY_STATUS_UNKNOWN), 85 cert_id(0), 86 connection_status(WebsiteSettings::SITE_CONNECTION_STATUS_UNKNOWN) { 87 } 88 89 string16 WebsiteSettingsUI::IdentityInfo::GetIdentityStatusText() const { 90 if (identity_status == WebsiteSettings::SITE_IDENTITY_STATUS_CERT || 91 identity_status == WebsiteSettings::SITE_IDENTITY_STATUS_EV_CERT) { 92 return l10n_util::GetStringUTF16(IDS_WEBSITE_SETTINGS_IDENTITY_VERIFIED); 93 } 94 if (identity_status == 95 WebsiteSettings::SITE_IDENTITY_STATUS_ADMIN_PROVIDED_CERT) { 96 return l10n_util::GetStringUTF16(IDS_CERT_POLICY_PROVIDED_CERT_HEADER); 97 } 98 return l10n_util::GetStringUTF16(IDS_WEBSITE_SETTINGS_IDENTITY_NOT_VERIFIED); 99 } 100 101 WebsiteSettingsUI::~WebsiteSettingsUI() { 102 } 103 104 // static 105 string16 WebsiteSettingsUI::PermissionTypeToUIString( 106 ContentSettingsType type) { 107 switch (type) { 108 case CONTENT_SETTINGS_TYPE_IMAGES: 109 return l10n_util::GetStringUTF16(IDS_WEBSITE_SETTINGS_TYPE_IMAGES); 110 case CONTENT_SETTINGS_TYPE_JAVASCRIPT: 111 return l10n_util::GetStringUTF16(IDS_WEBSITE_SETTINGS_TYPE_JAVASCRIPT); 112 case CONTENT_SETTINGS_TYPE_POPUPS: 113 return l10n_util::GetStringUTF16(IDS_WEBSITE_SETTINGS_TYPE_POPUPS); 114 case CONTENT_SETTINGS_TYPE_PLUGINS: 115 return l10n_util::GetStringUTF16(IDS_WEBSITE_SETTINGS_TYPE_PLUGINS); 116 case CONTENT_SETTINGS_TYPE_GEOLOCATION: 117 return l10n_util::GetStringUTF16(IDS_WEBSITE_SETTINGS_TYPE_LOCATION); 118 case CONTENT_SETTINGS_TYPE_NOTIFICATIONS: 119 return l10n_util::GetStringUTF16( 120 IDS_WEBSITE_SETTINGS_TYPE_NOTIFICATIONS); 121 case CONTENT_SETTINGS_TYPE_FULLSCREEN: 122 return l10n_util::GetStringUTF16(IDS_WEBSITE_SETTINGS_TYPE_FULLSCREEN); 123 case CONTENT_SETTINGS_TYPE_MOUSELOCK: 124 return l10n_util::GetStringUTF16(IDS_WEBSITE_SETTINGS_TYPE_MOUSELOCK); 125 case CONTENT_SETTINGS_TYPE_MEDIASTREAM: 126 return l10n_util::GetStringUTF16(IDS_WEBSITE_SETTINGS_TYPE_MEDIASTREAM); 127 case CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS: 128 return l10n_util::GetStringUTF16(IDS_AUTOMATIC_DOWNLOADS_TAB_LABEL); 129 case CONTENT_SETTINGS_TYPE_MIDI_SYSEX: 130 return l10n_util::GetStringUTF16(IDS_WEBSITE_SETTINGS_TYPE_MIDI_SYSEX); 131 default: 132 NOTREACHED(); 133 return string16(); 134 } 135 } 136 137 // static 138 string16 WebsiteSettingsUI::PermissionValueToUIString(ContentSetting value) { 139 switch (value) { 140 case CONTENT_SETTING_ALLOW: 141 return l10n_util::GetStringUTF16(IDS_WEBSITE_SETTINGS_PERMISSION_ALLOW); 142 case CONTENT_SETTING_BLOCK: 143 return l10n_util::GetStringUTF16(IDS_WEBSITE_SETTINGS_PERMISSION_BLOCK); 144 case CONTENT_SETTING_ASK: 145 return l10n_util::GetStringUTF16(IDS_WEBSITE_SETTINGS_PERMISSION_ASK); 146 default: 147 NOTREACHED(); 148 return string16(); 149 } 150 } 151 152 // static 153 string16 WebsiteSettingsUI::PermissionActionToUIString( 154 ContentSetting setting, 155 ContentSetting default_setting, 156 content_settings::SettingSource source) { 157 ContentSetting effective_setting = setting; 158 if (effective_setting == CONTENT_SETTING_DEFAULT) 159 effective_setting = default_setting; 160 161 const int* button_text_ids = NULL; 162 switch (source) { 163 case content_settings::SETTING_SOURCE_USER: 164 if (setting == CONTENT_SETTING_DEFAULT) 165 button_text_ids = kPermissionButtonTextIDDefaultSetting; 166 else 167 button_text_ids = kPermissionButtonTextIDUserManaged; 168 break; 169 case content_settings::SETTING_SOURCE_POLICY: 170 button_text_ids = kPermissionButtonTextIDPolicyManaged; 171 break; 172 case content_settings::SETTING_SOURCE_EXTENSION: 173 button_text_ids = kPermissionButtonTextIDExtensionManaged; 174 break; 175 case content_settings::SETTING_SOURCE_WHITELIST: 176 case content_settings::SETTING_SOURCE_NONE: 177 default: 178 NOTREACHED(); 179 return string16(); 180 } 181 int button_text_id = button_text_ids[effective_setting]; 182 DCHECK_NE(button_text_id, kInvalidResourceID); 183 return l10n_util::GetStringUTF16(button_text_id); 184 } 185 186 // static 187 int WebsiteSettingsUI::GetPermissionIconID(ContentSettingsType type, 188 ContentSetting setting) { 189 bool use_blocked = (setting == CONTENT_SETTING_BLOCK); 190 int resource_id = IDR_INFO; 191 switch (type) { 192 case CONTENT_SETTINGS_TYPE_IMAGES: 193 resource_id = use_blocked ? IDR_BLOCKED_IMAGES : IDR_ALLOWED_IMAGES; 194 break; 195 case CONTENT_SETTINGS_TYPE_JAVASCRIPT: 196 resource_id = 197 use_blocked ? IDR_BLOCKED_JAVASCRIPT : IDR_ALLOWED_JAVASCRIPT; 198 break; 199 case CONTENT_SETTINGS_TYPE_COOKIES: 200 resource_id = use_blocked ? IDR_BLOCKED_COOKIES : IDR_ACCESSED_COOKIES; 201 break; 202 case CONTENT_SETTINGS_TYPE_POPUPS: 203 resource_id = use_blocked ? IDR_BLOCKED_POPUPS : IDR_ALLOWED_POPUPS; 204 break; 205 case CONTENT_SETTINGS_TYPE_PLUGINS: 206 resource_id = use_blocked ? IDR_BLOCKED_PLUGINS : IDR_ALLOWED_PLUGINS; 207 break; 208 case CONTENT_SETTINGS_TYPE_GEOLOCATION: 209 resource_id = use_blocked ? IDR_BLOCKED_LOCATION : IDR_ALLOWED_LOCATION; 210 break; 211 case CONTENT_SETTINGS_TYPE_NOTIFICATIONS: 212 resource_id = 213 use_blocked ? IDR_BLOCKED_NOTIFICATION : IDR_ALLOWED_NOTIFICATION; 214 break; 215 case CONTENT_SETTINGS_TYPE_FULLSCREEN: 216 resource_id = IDR_ALLOWED_FULLSCREEN; 217 break; 218 case CONTENT_SETTINGS_TYPE_MOUSELOCK: 219 resource_id = 220 use_blocked ? IDR_BLOCKED_MOUSE_CURSOR : IDR_ALLOWED_MOUSE_CURSOR; 221 break; 222 case CONTENT_SETTINGS_TYPE_MEDIASTREAM: 223 resource_id = use_blocked ? IDR_BLOCKED_MEDIA : IDR_ASK_MEDIA; 224 break; 225 case CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS: 226 resource_id = use_blocked ? IDR_BLOCKED_DOWNLOADS 227 : IDR_ALLOWED_DOWNLOADS; 228 break; 229 case CONTENT_SETTINGS_TYPE_MIDI_SYSEX: 230 resource_id = use_blocked ? IDR_BLOCKED_MIDI_SYSEX 231 : IDR_ALLOWED_MIDI_SYSEX; 232 break; 233 default: 234 NOTREACHED(); 235 break; 236 } 237 return resource_id; 238 } 239 240 // static 241 const gfx::Image& WebsiteSettingsUI::GetPermissionIcon( 242 ContentSettingsType type, 243 ContentSetting setting) { 244 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); 245 return rb.GetNativeImageNamed(GetPermissionIconID(type, setting)); 246 } 247 248 // static 249 int WebsiteSettingsUI::GetIdentityIconID( 250 WebsiteSettings::SiteIdentityStatus status) { 251 int resource_id = IDR_PAGEINFO_INFO; 252 switch (status) { 253 case WebsiteSettings::SITE_IDENTITY_STATUS_UNKNOWN: 254 break; 255 case WebsiteSettings::SITE_IDENTITY_STATUS_CERT: 256 case WebsiteSettings::SITE_IDENTITY_STATUS_EV_CERT: 257 resource_id = IDR_PAGEINFO_GOOD; 258 break; 259 case WebsiteSettings::SITE_IDENTITY_STATUS_CERT_REVOCATION_UNKNOWN: 260 resource_id = IDR_PAGEINFO_WARNING_MINOR; 261 break; 262 case WebsiteSettings::SITE_IDENTITY_STATUS_NO_CERT: 263 resource_id = IDR_PAGEINFO_WARNING_MAJOR; 264 break; 265 case WebsiteSettings::SITE_IDENTITY_STATUS_ERROR: 266 resource_id = IDR_PAGEINFO_BAD; 267 break; 268 case WebsiteSettings::SITE_IDENTITY_STATUS_ADMIN_PROVIDED_CERT: 269 resource_id = IDR_CONTROLLED_SETTING_MANDATORY_LARGE; 270 break; 271 default: 272 NOTREACHED(); 273 break; 274 } 275 return resource_id; 276 } 277 278 // static 279 const gfx::Image& WebsiteSettingsUI::GetIdentityIcon( 280 WebsiteSettings::SiteIdentityStatus status) { 281 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); 282 return rb.GetNativeImageNamed(GetIdentityIconID(status)); 283 } 284 285 // static 286 int WebsiteSettingsUI::GetConnectionIconID( 287 WebsiteSettings::SiteConnectionStatus status) { 288 int resource_id = IDR_PAGEINFO_INFO; 289 switch (status) { 290 case WebsiteSettings::SITE_CONNECTION_STATUS_UNKNOWN: 291 break; 292 case WebsiteSettings::SITE_CONNECTION_STATUS_ENCRYPTED: 293 resource_id = IDR_PAGEINFO_GOOD; 294 break; 295 case WebsiteSettings::SITE_CONNECTION_STATUS_MIXED_CONTENT: 296 resource_id = IDR_PAGEINFO_WARNING_MINOR; 297 break; 298 case WebsiteSettings::SITE_CONNECTION_STATUS_UNENCRYPTED: 299 resource_id = IDR_PAGEINFO_WARNING_MAJOR; 300 break; 301 case WebsiteSettings::SITE_CONNECTION_STATUS_ENCRYPTED_ERROR: 302 resource_id = IDR_PAGEINFO_BAD; 303 break; 304 default: 305 NOTREACHED(); 306 break; 307 } 308 return resource_id; 309 } 310 311 // static 312 const gfx::Image& WebsiteSettingsUI::GetConnectionIcon( 313 WebsiteSettings::SiteConnectionStatus status) { 314 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); 315 return rb.GetNativeImageNamed(GetConnectionIconID(status)); 316 } 317 318 // static 319 int WebsiteSettingsUI::GetFirstVisitIconID(const string16& first_visit) { 320 // FIXME(markusheintz): Display a minor warning icon if the page is visited 321 // the first time. 322 return IDR_PAGEINFO_INFO; 323 } 324 325 // static 326 const gfx::Image& WebsiteSettingsUI::GetFirstVisitIcon( 327 const string16& first_visit) { 328 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); 329 return rb.GetNativeImageNamed(GetFirstVisitIconID(first_visit)); 330 } 331