content_setting_image_model.cc revision 3345a6884c488ff3a535c2c9acdd33d74b37e311
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 "app/l10n_util.h"
8#include "base/command_line.h"
9#include "chrome/browser/host_content_settings_map.h"
10#include "chrome/browser/profile.h"
11#include "chrome/browser/tab_contents/tab_contents.h"
12#include "chrome/common/chrome_switches.h"
13#include "grit/generated_resources.h"
14#include "grit/theme_resources.h"
15
16class ContentSettingBlockedImageModel : public ContentSettingImageModel {
17 public:
18  explicit ContentSettingBlockedImageModel(
19      ContentSettingsType content_settings_type);
20
21  virtual void UpdateFromTabContents(const TabContents* tab_contents);
22
23 private:
24  static const int kAccessedIconIDs[];
25  static const int kBlockedIconIDs[];
26  static const int kAccessedTooltipIDs[];
27  static const int kBlockedTooltipIDs[];
28};
29
30class ContentSettingGeolocationImageModel : public ContentSettingImageModel {
31 public:
32  ContentSettingGeolocationImageModel();
33
34  virtual void UpdateFromTabContents(const TabContents* tab_contents);
35};
36
37class ContentSettingNotificationsImageModel : public ContentSettingImageModel {
38 public:
39  ContentSettingNotificationsImageModel();
40
41  virtual void UpdateFromTabContents(const TabContents* tab_contents);
42};
43
44const int ContentSettingBlockedImageModel::kBlockedIconIDs[] = {
45    IDR_BLOCKED_COOKIES,
46    IDR_BLOCKED_IMAGES,
47    IDR_BLOCKED_JAVASCRIPT,
48    IDR_BLOCKED_PLUGINS,
49    IDR_BLOCKED_POPUPS,
50};
51
52const int ContentSettingBlockedImageModel::kAccessedIconIDs[] = {
53    IDR_ACCESSED_COOKIES,
54    0,
55    0,
56    0,
57    0,
58};
59
60const int ContentSettingBlockedImageModel::kBlockedTooltipIDs[] = {
61    IDS_BLOCKED_COOKIES_TITLE,
62    IDS_BLOCKED_IMAGES_TITLE,
63    IDS_BLOCKED_JAVASCRIPT_TITLE,
64    IDS_BLOCKED_PLUGINS_MESSAGE,
65    IDS_BLOCKED_POPUPS_TOOLTIP,
66};
67
68const int ContentSettingBlockedImageModel::kAccessedTooltipIDs[] = {
69    IDS_ACCESSED_COOKIES_TITLE,
70    0,
71    0,
72    0,
73    0,
74};
75
76ContentSettingBlockedImageModel::ContentSettingBlockedImageModel(
77    ContentSettingsType content_settings_type)
78    : ContentSettingImageModel(content_settings_type) {
79}
80
81void ContentSettingBlockedImageModel::UpdateFromTabContents(
82    const TabContents* tab_contents) {
83  TabSpecificContentSettings* content_settings = tab_contents ?
84      tab_contents->GetTabSpecificContentSettings() : NULL;
85  const int* icon_ids;
86  const int* tooltip_ids;
87
88  if (!content_settings) {
89    set_visible(false);
90    return;
91  }
92  if (content_settings->IsContentBlocked(get_content_settings_type())) {
93    icon_ids = kBlockedIconIDs;
94    tooltip_ids = kBlockedTooltipIDs;
95  } else if (tab_contents->profile()->GetHostContentSettingsMap()->
96                 GetDefaultContentSetting(get_content_settings_type()) ==
97                 CONTENT_SETTING_BLOCK &&
98             content_settings->IsContentAccessed(get_content_settings_type())) {
99    // If a content type is blocked by default and was accessed, display the
100    // accessed icon.
101    icon_ids = kAccessedIconIDs;
102    tooltip_ids = kAccessedTooltipIDs;
103  } else {
104    set_visible(false);
105    return;
106  }
107  set_icon(icon_ids[get_content_settings_type()]);
108  set_tooltip(
109      l10n_util::GetStringUTF8(tooltip_ids[get_content_settings_type()]));
110  set_visible(true);
111}
112
113ContentSettingGeolocationImageModel::ContentSettingGeolocationImageModel()
114    : ContentSettingImageModel(CONTENT_SETTINGS_TYPE_GEOLOCATION) {
115}
116
117void ContentSettingGeolocationImageModel::UpdateFromTabContents(
118    const TabContents* tab_contents) {
119  if (!tab_contents) {
120    set_visible(false);
121    return;
122  }
123  TabSpecificContentSettings* content_settings =
124      tab_contents->GetTabSpecificContentSettings();
125  const GeolocationSettingsState& settings_state =
126      content_settings->geolocation_settings_state();
127  if (settings_state.state_map().empty()) {
128    set_visible(false);
129    return;
130  }
131  set_visible(true);
132  unsigned int tab_state_flags = 0;
133  settings_state.GetDetailedInfo(NULL, &tab_state_flags);
134  // If any embedded site has access the allowed icon takes priority over the
135  // blocked icon.
136  if (tab_state_flags & GeolocationSettingsState::TABSTATE_HAS_ANY_ALLOWED) {
137    set_icon(IDR_GEOLOCATION_ALLOWED_LOCATIONBAR_ICON);
138    set_tooltip(l10n_util::GetStringUTF8(IDS_GEOLOCATION_ALLOWED_TOOLTIP));
139    return;
140  }
141  set_icon(IDR_GEOLOCATION_DENIED_LOCATIONBAR_ICON);
142  set_tooltip(l10n_util::GetStringUTF8(IDS_GEOLOCATION_BLOCKED_TOOLTIP));
143}
144
145ContentSettingNotificationsImageModel::ContentSettingNotificationsImageModel()
146    : ContentSettingImageModel(CONTENT_SETTINGS_TYPE_NOTIFICATIONS) {
147}
148
149void ContentSettingNotificationsImageModel::UpdateFromTabContents(
150    const TabContents* tab_contents) {
151  // Notifications do not have a bubble.
152  set_visible(false);
153}
154
155ContentSettingImageModel::ContentSettingImageModel(
156    ContentSettingsType content_settings_type)
157    : content_settings_type_(content_settings_type),
158      is_visible_(false),
159      icon_(0) {
160}
161
162// static
163ContentSettingImageModel*
164    ContentSettingImageModel::CreateContentSettingImageModel(
165    ContentSettingsType content_settings_type) {
166  if (content_settings_type == CONTENT_SETTINGS_TYPE_GEOLOCATION)
167    return new ContentSettingGeolocationImageModel();
168  if (content_settings_type == CONTENT_SETTINGS_TYPE_NOTIFICATIONS)
169    return new ContentSettingNotificationsImageModel();
170  return new ContentSettingBlockedImageModel(content_settings_type);
171}
172