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.
5 #include "chrome/browser/ui/content_settings/content_setting_image_model.h"
7 #include "chrome/browser/content_settings/host_content_settings_map.h"
8 #include "chrome/browser/content_settings/tab_specific_content_settings.h"
9 #include "chrome/browser/prerender/prerender_manager.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "content/public/browser/web_contents.h"
12 #include "grit/generated_resources.h"
13 #include "grit/theme_resources.h"
14 #include "ui/base/l10n/l10n_util.h"
16 using content::WebContents;
18 class ContentSettingBlockedImageModel : public ContentSettingImageModel {
20 explicit ContentSettingBlockedImageModel(
21 ContentSettingsType content_settings_type);
23 virtual void UpdateFromWebContents(WebContents* web_contents) OVERRIDE;
26 class ContentSettingGeolocationImageModel : public ContentSettingImageModel {
28 ContentSettingGeolocationImageModel();
30 virtual void UpdateFromWebContents(WebContents* web_contents) OVERRIDE;
33 // Image model for displaying media icons in the location bar.
34 class ContentSettingMediaImageModel : public ContentSettingImageModel {
36 explicit ContentSettingMediaImageModel(ContentSettingsType type);
38 virtual void UpdateFromWebContents(WebContents* web_contents) OVERRIDE;
41 class ContentSettingRPHImageModel : public ContentSettingImageModel {
43 ContentSettingRPHImageModel();
45 virtual void UpdateFromWebContents(WebContents* web_contents) OVERRIDE;
48 class ContentSettingNotificationsImageModel : public ContentSettingImageModel {
50 ContentSettingNotificationsImageModel();
52 virtual void UpdateFromWebContents(WebContents* web_contents) OVERRIDE;
55 class ContentSettingMIDISysExImageModel : public ContentSettingImageModel {
57 ContentSettingMIDISysExImageModel();
59 virtual void UpdateFromWebContents(WebContents* web_contents) OVERRIDE;
62 class ContentSettingSavePasswordImageModel : public ContentSettingImageModel {
64 ContentSettingSavePasswordImageModel();
66 virtual void UpdateFromWebContents(WebContents* web_contents) OVERRIDE;
67 virtual bool ShouldShowBubbleOnBlockage() OVERRIDE;
72 struct ContentSettingsTypeIdEntry {
73 ContentSettingsType type;
77 int GetIdForContentType(const ContentSettingsTypeIdEntry* entries,
79 ContentSettingsType type) {
80 for (size_t i = 0; i < num_entries; ++i) {
81 if (entries[i].type == type)
89 ContentSettingBlockedImageModel::ContentSettingBlockedImageModel(
90 ContentSettingsType content_settings_type)
91 : ContentSettingImageModel(content_settings_type) {
94 void ContentSettingBlockedImageModel::UpdateFromWebContents(
95 WebContents* web_contents) {
100 static const ContentSettingsTypeIdEntry kBlockedIconIDs[] = {
101 {CONTENT_SETTINGS_TYPE_COOKIES, IDR_BLOCKED_COOKIES},
102 {CONTENT_SETTINGS_TYPE_IMAGES, IDR_BLOCKED_IMAGES},
103 {CONTENT_SETTINGS_TYPE_JAVASCRIPT, IDR_BLOCKED_JAVASCRIPT},
104 {CONTENT_SETTINGS_TYPE_PLUGINS, IDR_BLOCKED_PLUGINS},
105 {CONTENT_SETTINGS_TYPE_POPUPS, IDR_BLOCKED_POPUPS},
106 {CONTENT_SETTINGS_TYPE_MIXEDSCRIPT, IDR_BLOCKED_MIXED_CONTENT},
107 {CONTENT_SETTINGS_TYPE_PPAPI_BROKER, IDR_BLOCKED_PPAPI_BROKER},
108 {CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS, IDR_BLOCKED_DOWNLOADS},
110 static const ContentSettingsTypeIdEntry kBlockedTooltipIDs[] = {
111 {CONTENT_SETTINGS_TYPE_COOKIES, IDS_BLOCKED_COOKIES_TITLE},
112 {CONTENT_SETTINGS_TYPE_IMAGES, IDS_BLOCKED_IMAGES_TITLE},
113 {CONTENT_SETTINGS_TYPE_JAVASCRIPT, IDS_BLOCKED_JAVASCRIPT_TITLE},
114 {CONTENT_SETTINGS_TYPE_PLUGINS, IDS_BLOCKED_PLUGINS_MESSAGE},
115 {CONTENT_SETTINGS_TYPE_POPUPS, IDS_BLOCKED_POPUPS_TOOLTIP},
116 {CONTENT_SETTINGS_TYPE_MIXEDSCRIPT,
117 IDS_BLOCKED_DISPLAYING_INSECURE_CONTENT},
118 {CONTENT_SETTINGS_TYPE_PPAPI_BROKER, IDS_BLOCKED_PPAPI_BROKER_TITLE},
119 {CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS, IDS_BLOCKED_DOWNLOAD_TITLE},
121 static const ContentSettingsTypeIdEntry kBlockedExplanatoryTextIDs[] = {
122 {CONTENT_SETTINGS_TYPE_POPUPS, IDS_BLOCKED_POPUPS_EXPLANATORY_TEXT},
123 {CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS,
124 IDS_BLOCKED_DOWNLOADS_EXPLANATION},
127 ContentSettingsType type = get_content_settings_type();
128 int icon_id = GetIdForContentType(
129 kBlockedIconIDs, arraysize(kBlockedIconIDs), type);
130 int tooltip_id = GetIdForContentType(
131 kBlockedTooltipIDs, arraysize(kBlockedTooltipIDs), type);
132 int explanation_id = GetIdForContentType(
133 kBlockedExplanatoryTextIDs, arraysize(kBlockedExplanatoryTextIDs), type);
135 // If a content type is blocked by default and was accessed, display the
137 TabSpecificContentSettings* content_settings =
138 TabSpecificContentSettings::FromWebContents(web_contents);
139 if (!content_settings)
142 Profile::FromBrowserContext(web_contents->GetBrowserContext());
143 if (!content_settings->IsContentBlocked(get_content_settings_type())) {
144 if (!content_settings->IsContentAllowed(get_content_settings_type()))
147 // For cookies, only show the accessed bubble if cookies are blocked by
149 if (get_content_settings_type() == CONTENT_SETTINGS_TYPE_COOKIES &&
150 (profile->GetHostContentSettingsMap()->
151 GetDefaultContentSetting(CONTENT_SETTINGS_TYPE_COOKIES, NULL) !=
152 CONTENT_SETTING_BLOCK))
155 static const ContentSettingsTypeIdEntry kAccessedIconIDs[] = {
156 {CONTENT_SETTINGS_TYPE_COOKIES, IDR_ACCESSED_COOKIES},
157 {CONTENT_SETTINGS_TYPE_PPAPI_BROKER, IDR_BLOCKED_PPAPI_BROKER},
158 {CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS, IDR_ALLOWED_DOWNLOADS},
160 static const ContentSettingsTypeIdEntry kAccessedTooltipIDs[] = {
161 {CONTENT_SETTINGS_TYPE_COOKIES, IDS_ACCESSED_COOKIES_TITLE},
162 {CONTENT_SETTINGS_TYPE_PPAPI_BROKER, IDS_ALLOWED_PPAPI_BROKER_TITLE},
163 {CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS, IDS_ALLOWED_DOWNLOAD_TITLE},
165 icon_id = GetIdForContentType(
166 kAccessedIconIDs, arraysize(kAccessedIconIDs), type);
167 tooltip_id = GetIdForContentType(
168 kAccessedTooltipIDs, arraysize(kAccessedTooltipIDs), type);
174 set_explanatory_string_id(explanation_id);
176 set_tooltip(l10n_util::GetStringUTF8(tooltip_id));
179 ContentSettingGeolocationImageModel::ContentSettingGeolocationImageModel()
180 : ContentSettingImageModel(CONTENT_SETTINGS_TYPE_GEOLOCATION) {
183 void ContentSettingGeolocationImageModel::UpdateFromWebContents(
184 WebContents* web_contents) {
188 TabSpecificContentSettings* content_settings =
189 TabSpecificContentSettings::FromWebContents(web_contents);
190 if (!content_settings)
192 const ContentSettingsUsagesState& usages_state = content_settings->
193 geolocation_usages_state();
194 if (usages_state.state_map().empty())
198 // If any embedded site has access the allowed icon takes priority over the
200 unsigned int state_flags = 0;
201 usages_state.GetDetailedInfo(NULL, &state_flags);
203 !!(state_flags & ContentSettingsUsagesState::TABSTATE_HAS_ANY_ALLOWED);
204 set_icon(allowed ? IDR_ALLOWED_LOCATION : IDR_BLOCKED_LOCATION);
205 set_tooltip(l10n_util::GetStringUTF8(allowed ?
206 IDS_GEOLOCATION_ALLOWED_TOOLTIP : IDS_GEOLOCATION_BLOCKED_TOOLTIP));
209 ContentSettingSavePasswordImageModel::ContentSettingSavePasswordImageModel()
210 : ContentSettingImageModel(CONTENT_SETTINGS_TYPE_SAVE_PASSWORD) {
213 void ContentSettingSavePasswordImageModel::UpdateFromWebContents(
214 WebContents* web_contents) {
218 TabSpecificContentSettings* content_settings =
219 TabSpecificContentSettings::FromWebContents(web_contents);
220 if (!content_settings)
223 TabSpecificContentSettings::PasswordSavingState state =
224 content_settings->GetPasswordSavingState();
226 case TabSpecificContentSettings::PASSWORD_TO_BE_SAVED:
227 set_icon(IDR_SAVE_PASSWORD);
230 case TabSpecificContentSettings::NO_PASSWORD_TO_BE_SAVED:
231 set_icon(IDR_SAVE_PASSWORD);
237 bool ContentSettingSavePasswordImageModel::ShouldShowBubbleOnBlockage() {
241 ContentSettingMediaImageModel::ContentSettingMediaImageModel(
242 ContentSettingsType type)
243 : ContentSettingImageModel(type) {
246 void ContentSettingMediaImageModel::UpdateFromWebContents(
247 WebContents* web_contents) {
250 // As long as a single icon is used to display the status of the camera and
251 // microphone usage only display an icon for the
252 // CONTENT_SETTINGS_TYPE_MEDIASTREAM. Don't display anything for
253 // CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC,
254 // CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA.
255 // FIXME: Remove this hack and either display a two omnibox icons (one for
256 // camera and one for microphone), or don't create one image model per
257 // content type but per icon to display. The later is probably the right
258 // thing to do, bebacuse this also allows to add more content settings type
259 // for which no omnibox icon exists.
260 if (get_content_settings_type() == CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC ||
261 get_content_settings_type() == CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA) {
265 // The ContentSettingMediaImageModel must not be used with a content type
266 // other then: CONTENT_SETTINGS_TYPE_MEDIASTREAM,
267 // CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC,
268 // CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA.
269 DCHECK_EQ(get_content_settings_type(), CONTENT_SETTINGS_TYPE_MEDIASTREAM);
274 TabSpecificContentSettings* content_settings =
275 TabSpecificContentSettings::FromWebContents(web_contents);
276 if (!content_settings)
278 TabSpecificContentSettings::MicrophoneCameraState state =
279 content_settings->GetMicrophoneCameraState();
282 case TabSpecificContentSettings::MICROPHONE_CAMERA_NOT_ACCESSED:
283 // If neither the microphone nor the camera stream was accessed then no
284 // icon is displayed in the omnibox.
286 case TabSpecificContentSettings::MICROPHONE_ACCESSED:
287 set_icon(IDR_ASK_MEDIA);
288 set_tooltip(l10n_util::GetStringUTF8(IDS_MICROPHONE_ACCESSED));
290 case TabSpecificContentSettings::CAMERA_ACCESSED:
291 set_icon(IDR_ASK_MEDIA);
292 set_tooltip(l10n_util::GetStringUTF8(IDS_CAMERA_ACCESSED));
294 case TabSpecificContentSettings::MICROPHONE_CAMERA_ACCESSED:
295 set_icon(IDR_ASK_MEDIA);
296 set_tooltip(l10n_util::GetStringUTF8(IDS_MICROPHONE_CAMERA_ALLOWED));
298 case TabSpecificContentSettings::MICROPHONE_BLOCKED:
299 set_icon(IDR_BLOCKED_MEDIA);
300 set_tooltip(l10n_util::GetStringUTF8(IDS_MICROPHONE_BLOCKED));
302 case TabSpecificContentSettings::CAMERA_BLOCKED:
303 set_icon(IDR_BLOCKED_MEDIA);
304 set_tooltip(l10n_util::GetStringUTF8(IDS_CAMERA_BLOCKED));
306 case TabSpecificContentSettings::MICROPHONE_CAMERA_BLOCKED:
307 set_icon(IDR_BLOCKED_MEDIA);
308 set_tooltip(l10n_util::GetStringUTF8(IDS_MICROPHONE_CAMERA_BLOCKED));
314 ContentSettingRPHImageModel::ContentSettingRPHImageModel()
315 : ContentSettingImageModel(
316 CONTENT_SETTINGS_TYPE_PROTOCOL_HANDLERS) {
317 set_icon(IDR_REGISTER_PROTOCOL_HANDLER);
318 set_tooltip(l10n_util::GetStringUTF8(IDS_REGISTER_PROTOCOL_HANDLER_TOOLTIP));
321 void ContentSettingRPHImageModel::UpdateFromWebContents(
322 WebContents* web_contents) {
327 TabSpecificContentSettings* content_settings =
328 TabSpecificContentSettings::FromWebContents(web_contents);
329 if (!content_settings)
331 if (content_settings->pending_protocol_handler().IsEmpty())
337 ContentSettingNotificationsImageModel::ContentSettingNotificationsImageModel()
338 : ContentSettingImageModel(CONTENT_SETTINGS_TYPE_NOTIFICATIONS) {
341 void ContentSettingNotificationsImageModel::UpdateFromWebContents(
342 WebContents* web_contents) {
343 // Notifications do not have a bubble.
347 ContentSettingMIDISysExImageModel::ContentSettingMIDISysExImageModel()
348 : ContentSettingImageModel(CONTENT_SETTINGS_TYPE_MIDI_SYSEX) {
351 void ContentSettingMIDISysExImageModel::UpdateFromWebContents(
352 WebContents* web_contents) {
356 TabSpecificContentSettings* content_settings =
357 TabSpecificContentSettings::FromWebContents(web_contents);
358 if (!content_settings)
360 const ContentSettingsUsagesState& usages_state =
361 content_settings->midi_usages_state();
362 if (usages_state.state_map().empty())
366 // If any embedded site has access the allowed icon takes priority over the
368 unsigned int state_flags = 0;
369 usages_state.GetDetailedInfo(NULL, &state_flags);
371 !!(state_flags & ContentSettingsUsagesState::TABSTATE_HAS_ANY_ALLOWED);
372 set_icon(allowed ? IDR_ALLOWED_MIDI_SYSEX : IDR_BLOCKED_MIDI_SYSEX);
373 set_tooltip(l10n_util::GetStringUTF8(allowed ?
374 IDS_MIDI_SYSEX_ALLOWED_TOOLTIP : IDS_MIDI_SYSEX_BLOCKED_TOOLTIP));
377 ContentSettingImageModel::ContentSettingImageModel(
378 ContentSettingsType content_settings_type)
379 : content_settings_type_(content_settings_type),
382 explanatory_string_id_(0) {
385 bool ContentSettingImageModel::ShouldShowBubbleOnBlockage() {
390 ContentSettingImageModel*
391 ContentSettingImageModel::CreateContentSettingImageModel(
392 ContentSettingsType content_settings_type) {
393 switch (content_settings_type) {
394 case CONTENT_SETTINGS_TYPE_GEOLOCATION:
395 return new ContentSettingGeolocationImageModel();
396 case CONTENT_SETTINGS_TYPE_NOTIFICATIONS:
397 return new ContentSettingNotificationsImageModel();
398 case CONTENT_SETTINGS_TYPE_PROTOCOL_HANDLERS:
399 return new ContentSettingRPHImageModel();
400 case CONTENT_SETTINGS_TYPE_MEDIASTREAM:
401 case CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC:
402 case CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA:
403 return new ContentSettingMediaImageModel(content_settings_type);
404 case CONTENT_SETTINGS_TYPE_MIDI_SYSEX:
405 return new ContentSettingMIDISysExImageModel();
406 case CONTENT_SETTINGS_TYPE_SAVE_PASSWORD:
407 return new ContentSettingSavePasswordImageModel();
409 return new ContentSettingBlockedImageModel(content_settings_type);