OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/content_setting_bubble_model.h" | 5 #include "chrome/browser/content_setting_bubble_model.h" |
6 | 6 |
7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
8 #include "chrome/browser/blocked_popup_container.h" | 8 #include "chrome/browser/blocked_popup_container.h" |
9 #include "chrome/browser/host_content_settings_map.h" | 9 #include "chrome/browser/host_content_settings_map.h" |
10 #include "chrome/browser/pref_service.h" | 10 #include "chrome/browser/pref_service.h" |
11 #include "chrome/browser/profile.h" | 11 #include "chrome/browser/profile.h" |
12 #include "chrome/browser/tab_contents/tab_contents.h" | 12 #include "chrome/browser/tab_contents/tab_contents.h" |
13 #include "chrome/browser/tab_contents/tab_contents_delegate.h" | 13 #include "chrome/browser/tab_contents/tab_contents_delegate.h" |
14 #include "chrome/common/notification_service.h" | 14 #include "chrome/common/notification_service.h" |
15 #include "chrome/common/pref_names.h" | 15 #include "chrome/common/pref_names.h" |
16 #include "grit/generated_resources.h" | 16 #include "grit/generated_resources.h" |
17 #include "net/base/net_util.h" | 17 #include "net/base/net_util.h" |
18 | 18 |
19 class ContentSettingTitleAndLinkModel : public ContentSettingBubbleModel { | 19 class ContentSettingTitleAndLinkModel : public ContentSettingBubbleModel { |
20 public: | 20 public: |
21 ContentSettingTitleAndLinkModel(TabContents* tab_contents, Profile* profile, | 21 ContentSettingTitleAndLinkModel( |
22 ContentSettingsType content_type) | 22 TabContents* tab_contents, Profile* profile, |
23 : ContentSettingBubbleModel(tab_contents, profile, content_type) { | 23 ContentSettingsType content_type); |
24 SetTitle(); | 24 |
25 SetManageLink(); | 25 private: |
| 26 void SetTitle(); |
| 27 void SetManageLink(); |
| 28 |
| 29 virtual void OnManageLinkClicked(); |
| 30 }; |
| 31 |
| 32 class ContentSettingSingleRadioGroup : public ContentSettingTitleAndLinkModel { |
| 33 public: |
| 34 ContentSettingSingleRadioGroup( |
| 35 TabContents* tab_contents, Profile* profile, |
| 36 ContentSettingsType content_type); |
| 37 |
| 38 private: |
| 39 void SetRadioGroups(); |
| 40 |
| 41 virtual void OnRadioClicked(int radio_group, int radio_index); |
| 42 }; |
| 43 |
| 44 class ContentSettingPopupBubbleModel : public ContentSettingSingleRadioGroup { |
| 45 public: |
| 46 ContentSettingPopupBubbleModel(TabContents* tab_contents, Profile* profile); |
| 47 |
| 48 private: |
| 49 void SetPopups(); |
| 50 |
| 51 virtual void OnPopupClicked(int index); |
| 52 }; |
| 53 |
| 54 class ContentSettingGeolocationModel : public ContentSettingTitleAndLinkModel { |
| 55 public: |
| 56 ContentSettingGeolocationModel(TabContents* tab_contents, Profile* profile); |
| 57 |
| 58 private: |
| 59 void SetRadioGroups(); |
| 60 |
| 61 virtual void OnRadioClicked(int radio_group, int radio_index); |
| 62 }; |
| 63 |
| 64 ContentSettingTitleAndLinkModel::ContentSettingTitleAndLinkModel( |
| 65 TabContents* tab_contents, Profile* profile, |
| 66 ContentSettingsType content_type) |
| 67 : ContentSettingBubbleModel(tab_contents, profile, content_type) { |
| 68 SetTitle(); |
| 69 SetManageLink(); |
| 70 } |
| 71 |
| 72 void ContentSettingTitleAndLinkModel::SetTitle() { |
| 73 static const int kTitleIDs[CONTENT_SETTINGS_NUM_TYPES] = { |
| 74 IDS_BLOCKED_COOKIES_TITLE, |
| 75 IDS_BLOCKED_IMAGES_TITLE, |
| 76 IDS_BLOCKED_JAVASCRIPT_TITLE, |
| 77 IDS_BLOCKED_PLUGINS_TITLE, |
| 78 IDS_BLOCKED_POPUPS_TITLE, |
| 79 0, |
| 80 }; |
| 81 if (kTitleIDs[content_type()]) |
| 82 set_title(l10n_util::GetStringUTF8(kTitleIDs[content_type()])); |
| 83 } |
| 84 |
| 85 void ContentSettingTitleAndLinkModel::SetManageLink() { |
| 86 static const int kLinkIDs[CONTENT_SETTINGS_NUM_TYPES] = { |
| 87 IDS_BLOCKED_COOKIES_LINK, |
| 88 IDS_BLOCKED_IMAGES_LINK, |
| 89 IDS_BLOCKED_JAVASCRIPT_LINK, |
| 90 IDS_BLOCKED_PLUGINS_LINK, |
| 91 IDS_BLOCKED_POPUPS_LINK, |
| 92 IDS_GEOLOCATION_LINK, |
| 93 }; |
| 94 set_manage_link(l10n_util::GetStringUTF8(kLinkIDs[content_type()])); |
| 95 } |
| 96 |
| 97 void ContentSettingTitleAndLinkModel::OnManageLinkClicked() { |
| 98 if (tab_contents()) |
| 99 tab_contents()->delegate()->ShowContentSettingsWindow(content_type()); |
| 100 } |
| 101 |
| 102 ContentSettingSingleRadioGroup::ContentSettingSingleRadioGroup( |
| 103 TabContents* tab_contents, Profile* profile, |
| 104 ContentSettingsType content_type) |
| 105 : ContentSettingTitleAndLinkModel(tab_contents, profile, content_type) { |
| 106 SetRadioGroups(); |
| 107 } |
| 108 |
| 109 void ContentSettingSingleRadioGroup::SetRadioGroups() { |
| 110 GURL url = tab_contents()->GetURL(); |
| 111 std::wstring display_host_wide; |
| 112 net::AppendFormattedHost(url, |
| 113 profile()->GetPrefs()->GetString(prefs::kAcceptLanguages), |
| 114 &display_host_wide, NULL, NULL); |
| 115 std::string display_host(WideToUTF8(display_host_wide)); |
| 116 |
| 117 RadioGroup radio_group; |
| 118 radio_group.host = url.host(); |
| 119 |
| 120 static const int kAllowIDs[CONTENT_SETTINGS_NUM_TYPES] = { |
| 121 0, // We don't manage cookies here. |
| 122 IDS_BLOCKED_IMAGES_UNBLOCK, |
| 123 IDS_BLOCKED_JAVASCRIPT_UNBLOCK, |
| 124 IDS_BLOCKED_PLUGINS_UNBLOCK, |
| 125 IDS_BLOCKED_POPUPS_UNBLOCK, |
| 126 }; |
| 127 std::string radio_allow_label; |
| 128 radio_allow_label = l10n_util::GetStringFUTF8( |
| 129 kAllowIDs[content_type()], UTF8ToUTF16(display_host)); |
| 130 |
| 131 static const int kBlockIDs[CONTENT_SETTINGS_NUM_TYPES] = { |
| 132 0, // We don't manage cookies here. |
| 133 IDS_BLOCKED_IMAGES_NO_ACTION, |
| 134 IDS_BLOCKED_JAVASCRIPT_NO_ACTION, |
| 135 IDS_BLOCKED_PLUGINS_NO_ACTION, |
| 136 IDS_BLOCKED_POPUPS_NO_ACTION, |
| 137 }; |
| 138 std::string radio_block_label; |
| 139 radio_block_label = l10n_util::GetStringFUTF8( |
| 140 kBlockIDs[content_type()], UTF8ToUTF16(display_host)); |
| 141 |
| 142 radio_group.radio_items.push_back(radio_allow_label); |
| 143 radio_group.radio_items.push_back(radio_block_label); |
| 144 radio_group.default_item = |
| 145 profile()->GetHostContentSettingsMap()->GetContentSetting(url, |
| 146 content_type()) == CONTENT_SETTING_ALLOW ? 0 : 1; |
| 147 add_radio_group(radio_group); |
| 148 } |
| 149 |
| 150 void ContentSettingSingleRadioGroup::OnRadioClicked( |
| 151 int radio_group, int radio_index) { |
| 152 profile()->GetHostContentSettingsMap()->SetContentSetting( |
| 153 bubble_content().radio_groups[radio_group].host, |
| 154 content_type(), |
| 155 radio_index == 0 ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK); |
| 156 } |
| 157 |
| 158 |
| 159 ContentSettingPopupBubbleModel::ContentSettingPopupBubbleModel( |
| 160 TabContents* tab_contents, Profile* profile) |
| 161 : ContentSettingSingleRadioGroup( |
| 162 tab_contents, profile, CONTENT_SETTINGS_TYPE_POPUPS) { |
| 163 SetPopups(); |
| 164 } |
| 165 |
| 166 void ContentSettingPopupBubbleModel::SetPopups() { |
| 167 BlockedPopupContainer::BlockedContents blocked_contents; |
| 168 DCHECK(tab_contents()->blocked_popup_container()); |
| 169 tab_contents()->blocked_popup_container()->GetBlockedContents( |
| 170 &blocked_contents); |
| 171 for (BlockedPopupContainer::BlockedContents::const_iterator |
| 172 i(blocked_contents.begin()); i != blocked_contents.end(); ++i) { |
| 173 std::string title(UTF16ToUTF8((*i)->GetTitle())); |
| 174 // The popup may not have committed a load yet, in which case it won't |
| 175 // have a URL or title. |
| 176 if (title.empty()) |
| 177 title = l10n_util::GetStringUTF8(IDS_TAB_LOADING_TITLE); |
| 178 PopupItem popup_item; |
| 179 popup_item.title = title; |
| 180 popup_item.bitmap = (*i)->GetFavIcon(); |
| 181 popup_item.tab_contents = (*i); |
| 182 add_popup(popup_item); |
26 } | 183 } |
27 | 184 } |
28 private: | 185 |
29 void SetTitle() { | 186 void ContentSettingPopupBubbleModel::OnPopupClicked(int index) { |
30 static const int kTitleIDs[CONTENT_SETTINGS_NUM_TYPES] = { | 187 if (tab_contents() && tab_contents()->blocked_popup_container()) { |
31 IDS_BLOCKED_COOKIES_TITLE, | 188 tab_contents()->blocked_popup_container()->LaunchPopupForContents( |
32 IDS_BLOCKED_IMAGES_TITLE, | 189 bubble_content().popup_items[index].tab_contents); |
33 IDS_BLOCKED_JAVASCRIPT_TITLE, | |
34 IDS_BLOCKED_PLUGINS_TITLE, | |
35 IDS_BLOCKED_POPUPS_TITLE, | |
36 }; | |
37 set_title(l10n_util::GetStringUTF8(kTitleIDs[content_type()])); | |
38 } | 190 } |
39 | 191 } |
40 void SetManageLink() { | 192 |
41 static const int kLinkIDs[CONTENT_SETTINGS_NUM_TYPES] = { | 193 ContentSettingGeolocationModel::ContentSettingGeolocationModel( |
42 IDS_BLOCKED_COOKIES_LINK, | 194 TabContents* tab_contents, Profile* profile) |
43 IDS_BLOCKED_IMAGES_LINK, | 195 : ContentSettingTitleAndLinkModel( |
44 IDS_BLOCKED_JAVASCRIPT_LINK, | 196 tab_contents, profile, CONTENT_SETTINGS_TYPE_GEOLOCATION) { |
45 IDS_BLOCKED_PLUGINS_LINK, | 197 SetRadioGroups(); |
46 IDS_BLOCKED_POPUPS_LINK, | 198 } |
47 }; | 199 |
48 set_manage_link(l10n_util::GetStringUTF8(kLinkIDs[content_type()])); | 200 void ContentSettingGeolocationModel::SetRadioGroups() { |
49 } | 201 const TabContents::GeolocationContentSettings& settings = |
50 | 202 tab_contents()->geolocation_content_settings(); |
51 virtual void OnManageLinkClicked() { | 203 for (TabContents::GeolocationContentSettings::const_iterator i = |
52 if (tab_contents()) | 204 settings.begin(); i != settings.end(); ++i) { |
53 tab_contents()->delegate()->ShowContentSettingsWindow(content_type()); | |
54 } | |
55 }; | |
56 | |
57 class ContentSettingSingleRadioGroup : public ContentSettingTitleAndLinkModel { | |
58 public: | |
59 ContentSettingSingleRadioGroup(TabContents* tab_contents, Profile* profile, | |
60 ContentSettingsType content_type) | |
61 : ContentSettingTitleAndLinkModel(tab_contents, profile, content_type) { | |
62 SetRadioGroups(); | |
63 } | |
64 | |
65 private: | |
66 void SetRadioGroups() { | |
67 GURL url = tab_contents()->GetURL(); | |
68 std::wstring display_host_wide; | |
69 net::AppendFormattedHost(url, | |
70 profile()->GetPrefs()->GetString(prefs::kAcceptLanguages), | |
71 &display_host_wide, NULL, NULL); | |
72 std::string display_host(WideToUTF8(display_host_wide)); | |
73 | |
74 RadioGroup radio_group; | 205 RadioGroup radio_group; |
75 radio_group.host = url.host(); | 206 radio_group.host = i->first; |
76 | 207 radio_group.title = l10n_util::GetStringFUTF8(IDS_GEOLOCATION_RADIO_TITLE, |
77 static const int kAllowIDs[CONTENT_SETTINGS_NUM_TYPES] = { | 208 UTF8ToUTF16(i->first)); |
78 0, // We don't manage cookies here. | 209 radio_group.radio_items.push_back( |
79 IDS_BLOCKED_IMAGES_UNBLOCK, | 210 l10n_util::GetStringUTF8(IDS_GEOLOCATION_RADIO_ALLOW)); |
80 IDS_BLOCKED_JAVASCRIPT_UNBLOCK, | 211 radio_group.radio_items.push_back( |
81 IDS_BLOCKED_PLUGINS_UNBLOCK, | 212 l10n_util::GetStringUTF8(IDS_GEOLOCATION_RADIO_BLOCK)); |
82 IDS_BLOCKED_POPUPS_UNBLOCK, | 213 radio_group.default_item = i->second == CONTENT_SETTING_ALLOW ? 0 : 1; |
83 }; | |
84 std::string radio_allow_label; | |
85 radio_allow_label = l10n_util::GetStringFUTF8( | |
86 kAllowIDs[content_type()], UTF8ToUTF16(display_host)); | |
87 | |
88 static const int kBlockIDs[CONTENT_SETTINGS_NUM_TYPES] = { | |
89 0, // We don't manage cookies here. | |
90 IDS_BLOCKED_IMAGES_NO_ACTION, | |
91 IDS_BLOCKED_JAVASCRIPT_NO_ACTION, | |
92 IDS_BLOCKED_PLUGINS_NO_ACTION, | |
93 IDS_BLOCKED_POPUPS_NO_ACTION, | |
94 }; | |
95 std::string radio_block_label; | |
96 radio_block_label = l10n_util::GetStringFUTF8( | |
97 kBlockIDs[content_type()], UTF8ToUTF16(display_host)); | |
98 | |
99 radio_group.radio_items.push_back(radio_allow_label); | |
100 radio_group.radio_items.push_back(radio_block_label); | |
101 radio_group.default_item = | |
102 profile()->GetHostContentSettingsMap()->GetContentSetting(url, | |
103 content_type()) == CONTENT_SETTING_ALLOW ? 0 : 1; | |
104 add_radio_group(radio_group); | 214 add_radio_group(radio_group); |
105 } | 215 } |
106 | 216 } |
107 virtual void OnRadioClicked(int radio_group, int radio_index) { | 217 |
108 profile()->GetHostContentSettingsMap()->SetContentSetting( | 218 void ContentSettingGeolocationModel::OnRadioClicked( |
109 bubble_content().radio_groups[radio_group].host, | 219 int radio_group, int radio_index) { |
110 content_type(), | 220 profile()->GetHostContentSettingsMap()->SetContentSetting( |
111 radio_index == 0 ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK); | 221 bubble_content().radio_groups[radio_group].host, |
112 } | 222 CONTENT_SETTINGS_TYPE_GEOLOCATION, |
113 }; | 223 radio_index == 0 ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK); |
114 | |
115 class ContentSettingPopupBubbleModel : public ContentSettingSingleRadioGroup { | |
116 public: | |
117 ContentSettingPopupBubbleModel(TabContents* tab_contents, Profile* profile, | |
118 ContentSettingsType content_type) | |
119 : ContentSettingSingleRadioGroup(tab_contents, profile, content_type) { | |
120 SetPopups(); | |
121 } | |
122 | |
123 private: | |
124 void SetPopups() { | |
125 BlockedPopupContainer::BlockedContents blocked_contents; | |
126 DCHECK(tab_contents()->blocked_popup_container()); | |
127 tab_contents()->blocked_popup_container()->GetBlockedContents( | |
128 &blocked_contents); | |
129 for (BlockedPopupContainer::BlockedContents::const_iterator | |
130 i(blocked_contents.begin()); i != blocked_contents.end(); ++i) { | |
131 std::string title(UTF16ToUTF8((*i)->GetTitle())); | |
132 // The popup may not have committed a load yet, in which case it won't | |
133 // have a URL or title. | |
134 if (title.empty()) | |
135 title = l10n_util::GetStringUTF8(IDS_TAB_LOADING_TITLE); | |
136 PopupItem popup_item; | |
137 popup_item.title = title; | |
138 popup_item.bitmap = (*i)->GetFavIcon(); | |
139 popup_item.tab_contents = (*i); | |
140 add_popup(popup_item); | |
141 } | |
142 } | |
143 | |
144 virtual void OnPopupClicked(int index) { | |
145 if (tab_contents() && tab_contents()->blocked_popup_container()) { | |
146 tab_contents()->blocked_popup_container()->LaunchPopupForContents( | |
147 bubble_content().popup_items[index].tab_contents); | |
148 } | |
149 } | |
150 }; | |
151 | |
152 // static | |
153 ContentSettingBubbleModel* | |
154 ContentSettingBubbleModel::CreateContentSettingBubbleModel( | |
155 TabContents* tab_contents, | |
156 Profile* profile, | |
157 ContentSettingsType content_type) { | |
158 if (content_type == CONTENT_SETTINGS_TYPE_COOKIES) { | |
159 return new ContentSettingTitleAndLinkModel(tab_contents, profile, | |
160 content_type); | |
161 } | |
162 if (content_type == CONTENT_SETTINGS_TYPE_POPUPS) { | |
163 return new ContentSettingPopupBubbleModel(tab_contents, profile, | |
164 content_type); | |
165 } | |
166 return new ContentSettingSingleRadioGroup(tab_contents, profile, | |
167 content_type); | |
168 } | 224 } |
169 | 225 |
170 ContentSettingBubbleModel::ContentSettingBubbleModel( | 226 ContentSettingBubbleModel::ContentSettingBubbleModel( |
171 TabContents* tab_contents, Profile* profile, | 227 TabContents* tab_contents, Profile* profile, |
172 ContentSettingsType content_type) | 228 ContentSettingsType content_type) |
173 : tab_contents_(tab_contents), profile_(profile), | 229 : tab_contents_(tab_contents), profile_(profile), |
174 content_type_(content_type) { | 230 content_type_(content_type) { |
175 registrar_.Add(this, NotificationType::TAB_CONTENTS_DESTROYED, | 231 registrar_.Add(this, NotificationType::TAB_CONTENTS_DESTROYED, |
176 Source<TabContents>(tab_contents)); | 232 Source<TabContents>(tab_contents)); |
177 } | 233 } |
178 | 234 |
179 ContentSettingBubbleModel::~ContentSettingBubbleModel() { | 235 ContentSettingBubbleModel::~ContentSettingBubbleModel() { |
180 } | 236 } |
181 | 237 |
182 void ContentSettingBubbleModel::Observe(NotificationType type, | 238 void ContentSettingBubbleModel::Observe(NotificationType type, |
183 const NotificationSource& source, | 239 const NotificationSource& source, |
184 const NotificationDetails& details) { | 240 const NotificationDetails& details) { |
185 DCHECK(type == NotificationType::TAB_CONTENTS_DESTROYED); | 241 DCHECK(type == NotificationType::TAB_CONTENTS_DESTROYED); |
186 DCHECK(source == Source<TabContents>(tab_contents_)); | 242 DCHECK(source == Source<TabContents>(tab_contents_)); |
187 tab_contents_ = NULL; | 243 tab_contents_ = NULL; |
188 } | 244 } |
| 245 |
| 246 // static |
| 247 ContentSettingBubbleModel* |
| 248 ContentSettingBubbleModel::CreateContentSettingBubbleModel( |
| 249 TabContents* tab_contents, |
| 250 Profile* profile, |
| 251 ContentSettingsType content_type) { |
| 252 if (content_type == CONTENT_SETTINGS_TYPE_COOKIES) { |
| 253 return new ContentSettingTitleAndLinkModel(tab_contents, profile, |
| 254 content_type); |
| 255 } |
| 256 if (content_type == CONTENT_SETTINGS_TYPE_POPUPS) |
| 257 return new ContentSettingPopupBubbleModel(tab_contents, profile); |
| 258 if (content_type == CONTENT_SETTINGS_TYPE_GEOLOCATION) |
| 259 return new ContentSettingGeolocationModel(tab_contents, profile); |
| 260 return new ContentSettingSingleRadioGroup(tab_contents, profile, |
| 261 content_type); |
| 262 } |
OLD | NEW |