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