Home | History | Annotate | Download | only in browser
      1 // Copyright (c) 2010 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/content_setting_image_model.h"
      6 
      7 #include "base/command_line.h"
      8 #include "chrome/browser/content_settings/host_content_settings_map.h"
      9 #include "chrome/browser/prerender/prerender_manager.h"
     10 #include "chrome/browser/profiles/profile.h"
     11 #include "content/browser/tab_contents/tab_contents.h"
     12 #include "grit/generated_resources.h"
     13 #include "grit/theme_resources.h"
     14 #include "ui/base/l10n/l10n_util.h"
     15 
     16 class ContentSettingBlockedImageModel : public ContentSettingImageModel {
     17  public:
     18   explicit ContentSettingBlockedImageModel(
     19       ContentSettingsType content_settings_type);
     20 
     21   virtual void UpdateFromTabContents(TabContents* tab_contents);
     22 
     23  private:
     24   static const int kAccessedIconIDs[];
     25   static const int kBlockedIconIDs[];
     26   static const int kBlockedExplanatoryTextIDs[];
     27   static const int kAccessedExplanatoryTextIDs[];
     28   static const int kAccessedTooltipIDs[];
     29   static const int kBlockedTooltipIDs[];
     30 };
     31 
     32 class ContentSettingGeolocationImageModel : public ContentSettingImageModel {
     33  public:
     34   ContentSettingGeolocationImageModel();
     35 
     36   virtual void UpdateFromTabContents(TabContents* tab_contents) OVERRIDE;
     37 };
     38 
     39 class ContentSettingNotificationsImageModel : public ContentSettingImageModel {
     40  public:
     41   ContentSettingNotificationsImageModel();
     42 
     43   virtual void UpdateFromTabContents(TabContents* tab_contents) OVERRIDE;
     44 };
     45 
     46 class ContentSettingPrerenderImageModel : public ContentSettingImageModel {
     47  public:
     48   ContentSettingPrerenderImageModel();
     49 
     50   virtual void UpdateFromTabContents(TabContents* tab_contents) OVERRIDE;
     51 };
     52 
     53 const int ContentSettingBlockedImageModel::kBlockedIconIDs[] = {
     54     IDR_BLOCKED_COOKIES,
     55     IDR_BLOCKED_IMAGES,
     56     IDR_BLOCKED_JAVASCRIPT,
     57     IDR_BLOCKED_PLUGINS,
     58     IDR_BLOCKED_POPUPS,
     59 };
     60 
     61 const int ContentSettingBlockedImageModel::kAccessedIconIDs[] = {
     62     IDR_ACCESSED_COOKIES,
     63     0,
     64     0,
     65     0,
     66     0,
     67 };
     68 
     69 const int ContentSettingBlockedImageModel::kBlockedExplanatoryTextIDs[] = {
     70     0,
     71     0,
     72     0,
     73     0,
     74     IDS_BLOCKED_POPUPS_EXPLANATORY_TEXT,
     75 };
     76 
     77 const int ContentSettingBlockedImageModel::kAccessedExplanatoryTextIDs[] = {
     78     0,
     79     0,
     80     0,
     81     0,
     82     0,
     83 };
     84 
     85 
     86 const int ContentSettingBlockedImageModel::kBlockedTooltipIDs[] = {
     87     IDS_BLOCKED_COOKIES_TITLE,
     88     IDS_BLOCKED_IMAGES_TITLE,
     89     IDS_BLOCKED_JAVASCRIPT_TITLE,
     90     IDS_BLOCKED_PLUGINS_MESSAGE,
     91     IDS_BLOCKED_POPUPS_TOOLTIP,
     92 };
     93 
     94 const int ContentSettingBlockedImageModel::kAccessedTooltipIDs[] = {
     95     IDS_ACCESSED_COOKIES_TITLE,
     96     0,
     97     0,
     98     0,
     99     0,
    100 };
    101 
    102 ContentSettingBlockedImageModel::ContentSettingBlockedImageModel(
    103     ContentSettingsType content_settings_type)
    104     : ContentSettingImageModel(content_settings_type) {
    105 }
    106 
    107 void ContentSettingBlockedImageModel::UpdateFromTabContents(
    108     TabContents* tab_contents) {
    109   set_visible(false);
    110   if (!tab_contents)
    111     return;
    112 
    113   const int* icon_ids = kBlockedIconIDs;
    114   const int* tooltip_ids = kBlockedTooltipIDs;
    115   const int* explanatory_string_ids = kBlockedExplanatoryTextIDs;
    116   // If a content type is blocked by default and was accessed, display the
    117   // accessed icon.
    118   TabSpecificContentSettings* content_settings =
    119       tab_contents->GetTabSpecificContentSettings();
    120   if (!content_settings->IsContentBlocked(get_content_settings_type())) {
    121     if (!content_settings->IsContentAccessed(get_content_settings_type()) ||
    122         (tab_contents->profile()->GetHostContentSettingsMap()->
    123             GetDefaultContentSetting(get_content_settings_type()) !=
    124                 CONTENT_SETTING_BLOCK))
    125       return;
    126     icon_ids = kAccessedIconIDs;
    127     tooltip_ids = kAccessedTooltipIDs;
    128     explanatory_string_ids = kAccessedExplanatoryTextIDs;
    129   }
    130   set_visible(true);
    131   set_icon(icon_ids[get_content_settings_type()]);
    132   set_explanatory_string_id(
    133       explanatory_string_ids[get_content_settings_type()]);
    134   set_tooltip(
    135       l10n_util::GetStringUTF8(tooltip_ids[get_content_settings_type()]));
    136 }
    137 
    138 ContentSettingGeolocationImageModel::ContentSettingGeolocationImageModel()
    139     : ContentSettingImageModel(CONTENT_SETTINGS_TYPE_GEOLOCATION) {
    140 }
    141 
    142 void ContentSettingGeolocationImageModel::UpdateFromTabContents(
    143     TabContents* tab_contents) {
    144   set_visible(false);
    145   if (!tab_contents)
    146     return;
    147   const GeolocationSettingsState& settings_state = tab_contents->
    148       GetTabSpecificContentSettings()->geolocation_settings_state();
    149   if (settings_state.state_map().empty())
    150     return;
    151   set_visible(true);
    152 
    153   // If any embedded site has access the allowed icon takes priority over the
    154   // blocked icon.
    155   unsigned int tab_state_flags = 0;
    156   settings_state.GetDetailedInfo(NULL, &tab_state_flags);
    157   bool allowed =
    158       !!(tab_state_flags & GeolocationSettingsState::TABSTATE_HAS_ANY_ALLOWED);
    159   set_icon(allowed ? IDR_GEOLOCATION_ALLOWED_LOCATIONBAR_ICON :
    160       IDR_GEOLOCATION_DENIED_LOCATIONBAR_ICON);
    161   set_tooltip(l10n_util::GetStringUTF8(allowed ?
    162       IDS_GEOLOCATION_ALLOWED_TOOLTIP : IDS_GEOLOCATION_BLOCKED_TOOLTIP));
    163 }
    164 
    165 ContentSettingNotificationsImageModel::ContentSettingNotificationsImageModel()
    166     : ContentSettingImageModel(CONTENT_SETTINGS_TYPE_NOTIFICATIONS) {
    167 }
    168 
    169 void ContentSettingNotificationsImageModel::UpdateFromTabContents(
    170     TabContents* tab_contents) {
    171   // Notifications do not have a bubble.
    172   set_visible(false);
    173 }
    174 
    175 ContentSettingPrerenderImageModel::ContentSettingPrerenderImageModel()
    176     : ContentSettingImageModel(CONTENT_SETTINGS_TYPE_PRERENDER) {
    177   set_tooltip(l10n_util::GetStringUTF8(IDS_PRERENDER_SUCCEED_TOOLTIP));
    178   set_icon(IDR_PRERENDER_SUCCEED_ICON);
    179 }
    180 
    181 void ContentSettingPrerenderImageModel::UpdateFromTabContents(
    182     TabContents* tab_contents) {
    183   bool visibility = false;
    184   if (tab_contents) {
    185     prerender::PrerenderManager* pm =
    186         tab_contents->profile()->GetPrerenderManager();
    187     if (pm && pm->IsTabContentsPrerendered(tab_contents))
    188       visibility = true;
    189   }
    190   set_visible(visibility);
    191 }
    192 
    193 ContentSettingImageModel::ContentSettingImageModel(
    194     ContentSettingsType content_settings_type)
    195     : content_settings_type_(content_settings_type),
    196       is_visible_(false),
    197       icon_(0),
    198       explanatory_string_id_(0) {
    199 }
    200 
    201 // static
    202 ContentSettingImageModel*
    203     ContentSettingImageModel::CreateContentSettingImageModel(
    204     ContentSettingsType content_settings_type) {
    205   switch (content_settings_type) {
    206     case CONTENT_SETTINGS_TYPE_GEOLOCATION:
    207       return new ContentSettingGeolocationImageModel();
    208     case CONTENT_SETTINGS_TYPE_NOTIFICATIONS:
    209       return new ContentSettingNotificationsImageModel();
    210     case CONTENT_SETTINGS_TYPE_PRERENDER:
    211       return new ContentSettingPrerenderImageModel();
    212     default:
    213       return new ContentSettingBlockedImageModel(content_settings_type);
    214   }
    215 }
    216