[email protected] | 51ffaf7 | 2012-05-09 21:00:49 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 5e56df8 | 2011-04-18 17:00:15 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
mukai | 077089f | 2014-09-11 18:41:52 | [diff] [blame] | 5 | #include "components/content_settings/core/common/content_settings.h" |
[email protected] | 5e56df8 | 2011-04-18 17:00:15 | [diff] [blame] | 6 | #include "chrome/common/render_messages.h" |
| 7 | #include "chrome/renderer/content_settings_observer.h" |
[email protected] | c6d068ff | 2011-10-14 17:28:23 | [diff] [blame] | 8 | #include "chrome/test/base/chrome_render_view_test.h" |
[email protected] | 068970d | 2011-10-11 00:05:16 | [diff] [blame] | 9 | #include "content/public/renderer/render_view.h" |
[email protected] | 4dad4a8 | 2011-04-27 15:44:52 | [diff] [blame] | 10 | #include "ipc/ipc_message_macros.h" |
| 11 | #include "testing/gmock/include/gmock/gmock.h" |
[email protected] | 5e56df8 | 2011-04-18 17:00:15 | [diff] [blame] | 12 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 2255a933 | 2013-06-17 05:12:31 | [diff] [blame] | 13 | #include "third_party/WebKit/public/web/WebView.h" |
[email protected] | 566fa0f | 2011-06-01 23:26:06 | [diff] [blame] | 14 | |
| 15 | using testing::_; |
| 16 | using testing::DeleteArg; |
[email protected] | 5e56df8 | 2011-04-18 17:00:15 | [diff] [blame] | 17 | |
[email protected] | 4dad4a8 | 2011-04-27 15:44:52 | [diff] [blame] | 18 | namespace { |
| 19 | |
| 20 | class MockContentSettingsObserver : public ContentSettingsObserver { |
| 21 | public: |
[email protected] | a86c0e96 | 2013-12-17 17:10:39 | [diff] [blame] | 22 | explicit MockContentSettingsObserver(content::RenderFrame* render_frame); |
[email protected] | 4dad4a8 | 2011-04-27 15:44:52 | [diff] [blame] | 23 | |
| 24 | virtual bool Send(IPC::Message* message); |
| 25 | |
wfh | 303f76b | 2014-12-09 22:52:59 | [diff] [blame^] | 26 | MOCK_METHOD2(OnContentBlocked, |
| 27 | void(ContentSettingsType, const base::string16&)); |
[email protected] | 566fa0f | 2011-06-01 23:26:06 | [diff] [blame] | 28 | |
| 29 | MOCK_METHOD5(OnAllowDOMStorage, |
| 30 | void(int, const GURL&, const GURL&, bool, IPC::Message*)); |
[email protected] | c1440609 | 2011-10-27 13:43:42 | [diff] [blame] | 31 | GURL image_url_; |
| 32 | std::string image_origin_; |
[email protected] | 4dad4a8 | 2011-04-27 15:44:52 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | MockContentSettingsObserver::MockContentSettingsObserver( |
[email protected] | a86c0e96 | 2013-12-17 17:10:39 | [diff] [blame] | 36 | content::RenderFrame* render_frame) |
| 37 | : ContentSettingsObserver(render_frame, NULL), |
[email protected] | c1440609 | 2011-10-27 13:43:42 | [diff] [blame] | 38 | image_url_("http://www.foo.com/image.jpg"), |
| 39 | image_origin_("http://www.foo.com") { |
[email protected] | 4dad4a8 | 2011-04-27 15:44:52 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | bool MockContentSettingsObserver::Send(IPC::Message* message) { |
| 43 | IPC_BEGIN_MESSAGE_MAP(MockContentSettingsObserver, *message) |
[email protected] | 2ccf45c | 2011-08-19 23:35:50 | [diff] [blame] | 44 | IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ContentBlocked, OnContentBlocked) |
| 45 | IPC_MESSAGE_HANDLER_DELAY_REPLY(ChromeViewHostMsg_AllowDOMStorage, |
[email protected] | 566fa0f | 2011-06-01 23:26:06 | [diff] [blame] | 46 | OnAllowDOMStorage) |
[email protected] | 4dad4a8 | 2011-04-27 15:44:52 | [diff] [blame] | 47 | IPC_MESSAGE_UNHANDLED(ADD_FAILURE()) |
| 48 | IPC_END_MESSAGE_MAP() |
| 49 | |
| 50 | // Our super class deletes the message. |
[email protected] | a86c0e96 | 2013-12-17 17:10:39 | [diff] [blame] | 51 | return RenderFrameObserver::Send(message); |
[email protected] | 4dad4a8 | 2011-04-27 15:44:52 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | } // namespace |
| 55 | |
[email protected] | c6d068ff | 2011-10-14 17:28:23 | [diff] [blame] | 56 | TEST_F(ChromeRenderViewTest, DidBlockContentType) { |
[email protected] | a86c0e96 | 2013-12-17 17:10:39 | [diff] [blame] | 57 | MockContentSettingsObserver observer(view_->GetMainRenderFrame()); |
wfh | 303f76b | 2014-12-09 22:52:59 | [diff] [blame^] | 58 | EXPECT_CALL(observer, OnContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES, |
| 59 | base::string16())); |
[email protected] | d49f6f7e | 2013-12-11 15:47:36 | [diff] [blame] | 60 | observer.DidBlockContentType(CONTENT_SETTINGS_TYPE_COOKIES); |
[email protected] | 4dad4a8 | 2011-04-27 15:44:52 | [diff] [blame] | 61 | |
| 62 | // Blocking the same content type a second time shouldn't send a notification. |
[email protected] | d49f6f7e | 2013-12-11 15:47:36 | [diff] [blame] | 63 | observer.DidBlockContentType(CONTENT_SETTINGS_TYPE_COOKIES); |
[email protected] | 4dad4a8 | 2011-04-27 15:44:52 | [diff] [blame] | 64 | ::testing::Mock::VerifyAndClearExpectations(&observer); |
[email protected] | 4dad4a8 | 2011-04-27 15:44:52 | [diff] [blame] | 65 | } |
| 66 | |
[email protected] | 566fa0f | 2011-06-01 23:26:06 | [diff] [blame] | 67 | // Tests that multiple invokations of AllowDOMStorage result in a single IPC. |
[email protected] | 2955280a | 2011-11-15 14:52:16 | [diff] [blame] | 68 | // Fails due to http://crbug.com/104300 |
[email protected] | 984aa49c | 2012-05-20 20:44:13 | [diff] [blame] | 69 | TEST_F(ChromeRenderViewTest, DISABLED_AllowDOMStorage) { |
[email protected] | 566fa0f | 2011-06-01 23:26:06 | [diff] [blame] | 70 | // Load some HTML, so we have a valid security origin. |
| 71 | LoadHTML("<html></html>"); |
[email protected] | a86c0e96 | 2013-12-17 17:10:39 | [diff] [blame] | 72 | MockContentSettingsObserver observer(view_->GetMainRenderFrame()); |
[email protected] | 566fa0f | 2011-06-01 23:26:06 | [diff] [blame] | 73 | ON_CALL(observer, |
| 74 | OnAllowDOMStorage(_, _, _, _, _)).WillByDefault(DeleteArg<4>()); |
| 75 | EXPECT_CALL(observer, |
| 76 | OnAllowDOMStorage(_, _, _, _, _)); |
[email protected] | 098ac6f | 2014-04-08 15:02:44 | [diff] [blame] | 77 | observer.allowStorage(true); |
[email protected] | 566fa0f | 2011-06-01 23:26:06 | [diff] [blame] | 78 | |
| 79 | // Accessing localStorage from the same origin again shouldn't result in a |
| 80 | // new IPC. |
[email protected] | 098ac6f | 2014-04-08 15:02:44 | [diff] [blame] | 81 | observer.allowStorage(true); |
[email protected] | 566fa0f | 2011-06-01 23:26:06 | [diff] [blame] | 82 | ::testing::Mock::VerifyAndClearExpectations(&observer); |
| 83 | } |
| 84 | |
[email protected] | 5e56df8 | 2011-04-18 17:00:15 | [diff] [blame] | 85 | // Regression test for http://crbug.com/35011 |
[email protected] | c6d068ff | 2011-10-14 17:28:23 | [diff] [blame] | 86 | TEST_F(ChromeRenderViewTest, JSBlockSentAfterPageLoad) { |
[email protected] | 5e56df8 | 2011-04-18 17:00:15 | [diff] [blame] | 87 | // 1. Load page with JS. |
| 88 | std::string html = "<html>" |
[email protected] | 4dad4a8 | 2011-04-27 15:44:52 | [diff] [blame] | 89 | "<head>" |
| 90 | "<script>document.createElement('div');</script>" |
| 91 | "</head>" |
| 92 | "<body>" |
| 93 | "</body>" |
| 94 | "</html>"; |
[email protected] | c6d068ff | 2011-10-14 17:28:23 | [diff] [blame] | 95 | render_thread_->sink().ClearMessages(); |
[email protected] | 5e56df8 | 2011-04-18 17:00:15 | [diff] [blame] | 96 | LoadHTML(html.c_str()); |
| 97 | |
| 98 | // 2. Block JavaScript. |
[email protected] | edece21 | 2011-11-16 11:56:56 | [diff] [blame] | 99 | RendererContentSettingRules content_setting_rules; |
| 100 | ContentSettingsForOneType& script_setting_rules = |
| 101 | content_setting_rules.script_rules; |
| 102 | script_setting_rules.push_back( |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 103 | ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), |
| 104 | ContentSettingsPattern::Wildcard(), |
| 105 | CONTENT_SETTING_BLOCK, |
| 106 | std::string(), |
| 107 | false)); |
[email protected] | a86c0e96 | 2013-12-17 17:10:39 | [diff] [blame] | 108 | ContentSettingsObserver* observer = ContentSettingsObserver::Get( |
| 109 | view_->GetMainRenderFrame()); |
[email protected] | edece21 | 2011-11-16 11:56:56 | [diff] [blame] | 110 | observer->SetContentSettingRules(&content_setting_rules); |
[email protected] | 5e56df8 | 2011-04-18 17:00:15 | [diff] [blame] | 111 | |
| 112 | // Make sure no pending messages are in the queue. |
| 113 | ProcessPendingMessages(); |
[email protected] | c6d068ff | 2011-10-14 17:28:23 | [diff] [blame] | 114 | render_thread_->sink().ClearMessages(); |
[email protected] | 5e56df8 | 2011-04-18 17:00:15 | [diff] [blame] | 115 | |
| 116 | // 3. Reload page. |
[email protected] | 5e56df8 | 2011-04-18 17:00:15 | [diff] [blame] | 117 | std::string url_str = "data:text/html;charset=utf-8,"; |
| 118 | url_str.append(html); |
| 119 | GURL url(url_str); |
[email protected] | bcd90b7 | 2011-10-17 23:19:57 | [diff] [blame] | 120 | Reload(url); |
[email protected] | 5e56df8 | 2011-04-18 17:00:15 | [diff] [blame] | 121 | ProcessPendingMessages(); |
| 122 | |
| 123 | // 4. Verify that the notification that javascript was blocked is sent after |
[email protected] | 6be50d7 | 2014-05-08 23:49:40 | [diff] [blame] | 124 | // the navigation notification is sent. |
[email protected] | 5e56df8 | 2011-04-18 17:00:15 | [diff] [blame] | 125 | int navigation_index = -1; |
| 126 | int block_index = -1; |
[email protected] | c6d068ff | 2011-10-14 17:28:23 | [diff] [blame] | 127 | for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) { |
| 128 | const IPC::Message* msg = render_thread_->sink().GetMessageAt(i); |
[email protected] | bcd90b7 | 2011-10-17 23:19:57 | [diff] [blame] | 129 | if (msg->type() == GetNavigationIPCType()) |
[email protected] | 5e56df8 | 2011-04-18 17:00:15 | [diff] [blame] | 130 | navigation_index = i; |
[email protected] | 2ccf45c | 2011-08-19 23:35:50 | [diff] [blame] | 131 | if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID) |
[email protected] | 5e56df8 | 2011-04-18 17:00:15 | [diff] [blame] | 132 | block_index = i; |
| 133 | } |
| 134 | EXPECT_NE(-1, navigation_index); |
| 135 | EXPECT_NE(-1, block_index); |
| 136 | EXPECT_LT(navigation_index, block_index); |
| 137 | } |
[email protected] | 134efc3 | 2011-08-02 18:14:21 | [diff] [blame] | 138 | |
[email protected] | c6d068ff | 2011-10-14 17:28:23 | [diff] [blame] | 139 | TEST_F(ChromeRenderViewTest, PluginsTemporarilyAllowed) { |
[email protected] | 134efc3 | 2011-08-02 18:14:21 | [diff] [blame] | 140 | // Load some HTML. |
| 141 | LoadHTML("<html>Foo</html>"); |
| 142 | |
[email protected] | 51ffaf7 | 2012-05-09 21:00:49 | [diff] [blame] | 143 | std::string foo_plugin = "foo"; |
| 144 | std::string bar_plugin = "bar"; |
[email protected] | 134efc3 | 2011-08-02 18:14:21 | [diff] [blame] | 145 | |
[email protected] | a86c0e96 | 2013-12-17 17:10:39 | [diff] [blame] | 146 | ContentSettingsObserver* observer = |
| 147 | ContentSettingsObserver::Get(view_->GetMainRenderFrame()); |
[email protected] | 51ffaf7 | 2012-05-09 21:00:49 | [diff] [blame] | 148 | EXPECT_FALSE(observer->IsPluginTemporarilyAllowed(foo_plugin)); |
| 149 | |
| 150 | // Temporarily allow the "foo" plugin. |
[email protected] | a86c0e96 | 2013-12-17 17:10:39 | [diff] [blame] | 151 | observer->OnLoadBlockedPlugins(foo_plugin); |
[email protected] | 51ffaf7 | 2012-05-09 21:00:49 | [diff] [blame] | 152 | EXPECT_TRUE(observer->IsPluginTemporarilyAllowed(foo_plugin)); |
| 153 | EXPECT_FALSE(observer->IsPluginTemporarilyAllowed(bar_plugin)); |
[email protected] | 134efc3 | 2011-08-02 18:14:21 | [diff] [blame] | 154 | |
| 155 | // Simulate a navigation within the page. |
[email protected] | 068970d | 2011-10-11 00:05:16 | [diff] [blame] | 156 | DidNavigateWithinPage(GetMainFrame(), true); |
[email protected] | 51ffaf7 | 2012-05-09 21:00:49 | [diff] [blame] | 157 | EXPECT_TRUE(observer->IsPluginTemporarilyAllowed(foo_plugin)); |
| 158 | EXPECT_FALSE(observer->IsPluginTemporarilyAllowed(bar_plugin)); |
[email protected] | 134efc3 | 2011-08-02 18:14:21 | [diff] [blame] | 159 | |
| 160 | // Navigate to a different page. |
| 161 | LoadHTML("<html>Bar</html>"); |
[email protected] | 51ffaf7 | 2012-05-09 21:00:49 | [diff] [blame] | 162 | EXPECT_FALSE(observer->IsPluginTemporarilyAllowed(foo_plugin)); |
| 163 | EXPECT_FALSE(observer->IsPluginTemporarilyAllowed(bar_plugin)); |
| 164 | |
| 165 | // Temporarily allow all plugins. |
[email protected] | a86c0e96 | 2013-12-17 17:10:39 | [diff] [blame] | 166 | observer->OnLoadBlockedPlugins(std::string()); |
[email protected] | 51ffaf7 | 2012-05-09 21:00:49 | [diff] [blame] | 167 | EXPECT_TRUE(observer->IsPluginTemporarilyAllowed(foo_plugin)); |
| 168 | EXPECT_TRUE(observer->IsPluginTemporarilyAllowed(bar_plugin)); |
[email protected] | 134efc3 | 2011-08-02 18:14:21 | [diff] [blame] | 169 | } |
[email protected] | c1440609 | 2011-10-27 13:43:42 | [diff] [blame] | 170 | |
| 171 | TEST_F(ChromeRenderViewTest, ImagesBlockedByDefault) { |
[email protected] | a86c0e96 | 2013-12-17 17:10:39 | [diff] [blame] | 172 | MockContentSettingsObserver mock_observer(view_->GetMainRenderFrame()); |
[email protected] | c1440609 | 2011-10-27 13:43:42 | [diff] [blame] | 173 | |
| 174 | // Load some HTML. |
| 175 | LoadHTML("<html>Foo</html>"); |
| 176 | |
| 177 | // Set the default image blocking setting. |
[email protected] | edece21 | 2011-11-16 11:56:56 | [diff] [blame] | 178 | RendererContentSettingRules content_setting_rules; |
| 179 | ContentSettingsForOneType& image_setting_rules = |
| 180 | content_setting_rules.image_rules; |
[email protected] | c1440609 | 2011-10-27 13:43:42 | [diff] [blame] | 181 | image_setting_rules.push_back( |
| 182 | ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), |
| 183 | ContentSettingsPattern::Wildcard(), |
| 184 | CONTENT_SETTING_BLOCK, |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 185 | std::string(), |
[email protected] | c1440609 | 2011-10-27 13:43:42 | [diff] [blame] | 186 | false)); |
| 187 | |
[email protected] | a86c0e96 | 2013-12-17 17:10:39 | [diff] [blame] | 188 | ContentSettingsObserver* observer = ContentSettingsObserver::Get( |
| 189 | view_->GetMainRenderFrame()); |
[email protected] | edece21 | 2011-11-16 11:56:56 | [diff] [blame] | 190 | observer->SetContentSettingRules(&content_setting_rules); |
[email protected] | c1440609 | 2011-10-27 13:43:42 | [diff] [blame] | 191 | EXPECT_CALL(mock_observer, |
wfh | 303f76b | 2014-12-09 22:52:59 | [diff] [blame^] | 192 | OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, base::string16())); |
[email protected] | 098ac6f | 2014-04-08 15:02:44 | [diff] [blame] | 193 | EXPECT_FALSE(observer->allowImage(true, mock_observer.image_url_)); |
[email protected] | c1440609 | 2011-10-27 13:43:42 | [diff] [blame] | 194 | ::testing::Mock::VerifyAndClearExpectations(&observer); |
| 195 | |
| 196 | // Create an exception which allows the image. |
| 197 | image_setting_rules.insert( |
| 198 | image_setting_rules.begin(), |
| 199 | ContentSettingPatternSource( |
| 200 | ContentSettingsPattern::Wildcard(), |
| 201 | ContentSettingsPattern::FromString(mock_observer.image_origin_), |
| 202 | CONTENT_SETTING_ALLOW, |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 203 | std::string(), |
[email protected] | c1440609 | 2011-10-27 13:43:42 | [diff] [blame] | 204 | false)); |
| 205 | |
wfh | 303f76b | 2014-12-09 22:52:59 | [diff] [blame^] | 206 | EXPECT_CALL(mock_observer, OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, |
| 207 | base::string16())).Times(0); |
[email protected] | 098ac6f | 2014-04-08 15:02:44 | [diff] [blame] | 208 | EXPECT_TRUE(observer->allowImage(true, mock_observer.image_url_)); |
[email protected] | c1440609 | 2011-10-27 13:43:42 | [diff] [blame] | 209 | ::testing::Mock::VerifyAndClearExpectations(&observer); |
| 210 | } |
| 211 | |
| 212 | TEST_F(ChromeRenderViewTest, ImagesAllowedByDefault) { |
[email protected] | a86c0e96 | 2013-12-17 17:10:39 | [diff] [blame] | 213 | MockContentSettingsObserver mock_observer(view_->GetMainRenderFrame()); |
[email protected] | c1440609 | 2011-10-27 13:43:42 | [diff] [blame] | 214 | |
| 215 | // Load some HTML. |
| 216 | LoadHTML("<html>Foo</html>"); |
| 217 | |
| 218 | // Set the default image blocking setting. |
[email protected] | edece21 | 2011-11-16 11:56:56 | [diff] [blame] | 219 | RendererContentSettingRules content_setting_rules; |
| 220 | ContentSettingsForOneType& image_setting_rules = |
| 221 | content_setting_rules.image_rules; |
[email protected] | c1440609 | 2011-10-27 13:43:42 | [diff] [blame] | 222 | image_setting_rules.push_back( |
| 223 | ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), |
| 224 | ContentSettingsPattern::Wildcard(), |
| 225 | CONTENT_SETTING_ALLOW, |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 226 | std::string(), |
[email protected] | c1440609 | 2011-10-27 13:43:42 | [diff] [blame] | 227 | false)); |
| 228 | |
[email protected] | a86c0e96 | 2013-12-17 17:10:39 | [diff] [blame] | 229 | ContentSettingsObserver* observer = |
| 230 | ContentSettingsObserver::Get(view_->GetMainRenderFrame()); |
[email protected] | edece21 | 2011-11-16 11:56:56 | [diff] [blame] | 231 | observer->SetContentSettingRules(&content_setting_rules); |
wfh | 303f76b | 2014-12-09 22:52:59 | [diff] [blame^] | 232 | EXPECT_CALL(mock_observer, OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, |
| 233 | base::string16())).Times(0); |
[email protected] | 098ac6f | 2014-04-08 15:02:44 | [diff] [blame] | 234 | EXPECT_TRUE(observer->allowImage(true, mock_observer.image_url_)); |
[email protected] | c1440609 | 2011-10-27 13:43:42 | [diff] [blame] | 235 | ::testing::Mock::VerifyAndClearExpectations(&observer); |
| 236 | |
| 237 | // Create an exception which blocks the image. |
| 238 | image_setting_rules.insert( |
| 239 | image_setting_rules.begin(), |
| 240 | ContentSettingPatternSource( |
| 241 | ContentSettingsPattern::Wildcard(), |
| 242 | ContentSettingsPattern::FromString(mock_observer.image_origin_), |
| 243 | CONTENT_SETTING_BLOCK, |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 244 | std::string(), |
[email protected] | c1440609 | 2011-10-27 13:43:42 | [diff] [blame] | 245 | false)); |
| 246 | EXPECT_CALL(mock_observer, |
wfh | 303f76b | 2014-12-09 22:52:59 | [diff] [blame^] | 247 | OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, base::string16())); |
[email protected] | 098ac6f | 2014-04-08 15:02:44 | [diff] [blame] | 248 | EXPECT_FALSE(observer->allowImage(true, mock_observer.image_url_)); |
[email protected] | c1440609 | 2011-10-27 13:43:42 | [diff] [blame] | 249 | ::testing::Mock::VerifyAndClearExpectations(&observer); |
| 250 | } |
[email protected] | edece21 | 2011-11-16 11:56:56 | [diff] [blame] | 251 | |
| 252 | TEST_F(ChromeRenderViewTest, ContentSettingsBlockScripts) { |
| 253 | // Set the content settings for scripts. |
| 254 | RendererContentSettingRules content_setting_rules; |
| 255 | ContentSettingsForOneType& script_setting_rules = |
| 256 | content_setting_rules.script_rules; |
| 257 | script_setting_rules.push_back( |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 258 | ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), |
| 259 | ContentSettingsPattern::Wildcard(), |
| 260 | CONTENT_SETTING_BLOCK, |
| 261 | std::string(), |
| 262 | false)); |
[email protected] | edece21 | 2011-11-16 11:56:56 | [diff] [blame] | 263 | |
[email protected] | a86c0e96 | 2013-12-17 17:10:39 | [diff] [blame] | 264 | ContentSettingsObserver* observer = |
| 265 | ContentSettingsObserver::Get(view_->GetMainRenderFrame()); |
[email protected] | edece21 | 2011-11-16 11:56:56 | [diff] [blame] | 266 | observer->SetContentSettingRules(&content_setting_rules); |
| 267 | |
| 268 | // Load a page which contains a script. |
| 269 | std::string html = "<html>" |
| 270 | "<head>" |
| 271 | "<script src='data:foo'></script>" |
| 272 | "</head>" |
| 273 | "<body>" |
| 274 | "</body>" |
| 275 | "</html>"; |
| 276 | LoadHTML(html.c_str()); |
| 277 | |
| 278 | // Verify that the script was blocked. |
| 279 | bool was_blocked = false; |
| 280 | for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) { |
| 281 | const IPC::Message* msg = render_thread_->sink().GetMessageAt(i); |
| 282 | if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID) |
| 283 | was_blocked = true; |
| 284 | } |
| 285 | EXPECT_TRUE(was_blocked); |
| 286 | } |
| 287 | |
| 288 | TEST_F(ChromeRenderViewTest, ContentSettingsAllowScripts) { |
| 289 | // Set the content settings for scripts. |
| 290 | RendererContentSettingRules content_setting_rules; |
| 291 | ContentSettingsForOneType& script_setting_rules = |
| 292 | content_setting_rules.script_rules; |
| 293 | script_setting_rules.push_back( |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 294 | ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), |
| 295 | ContentSettingsPattern::Wildcard(), |
| 296 | CONTENT_SETTING_ALLOW, |
| 297 | std::string(), |
| 298 | false)); |
[email protected] | edece21 | 2011-11-16 11:56:56 | [diff] [blame] | 299 | |
[email protected] | a86c0e96 | 2013-12-17 17:10:39 | [diff] [blame] | 300 | ContentSettingsObserver* observer = |
| 301 | ContentSettingsObserver::Get(view_->GetMainRenderFrame()); |
[email protected] | edece21 | 2011-11-16 11:56:56 | [diff] [blame] | 302 | observer->SetContentSettingRules(&content_setting_rules); |
| 303 | |
| 304 | // Load a page which contains a script. |
| 305 | std::string html = "<html>" |
| 306 | "<head>" |
| 307 | "<script src='data:foo'></script>" |
| 308 | "</head>" |
| 309 | "<body>" |
| 310 | "</body>" |
| 311 | "</html>"; |
| 312 | LoadHTML(html.c_str()); |
| 313 | |
| 314 | // Verify that the script was not blocked. |
| 315 | bool was_blocked = false; |
| 316 | for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) { |
| 317 | const IPC::Message* msg = render_thread_->sink().GetMessageAt(i); |
| 318 | if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID) |
| 319 | was_blocked = true; |
| 320 | } |
| 321 | EXPECT_FALSE(was_blocked); |
| 322 | } |
[email protected] | 6f9f7d8 | 2011-12-08 12:13:34 | [diff] [blame] | 323 | |
| 324 | TEST_F(ChromeRenderViewTest, ContentSettingsInterstitialPages) { |
[email protected] | a86c0e96 | 2013-12-17 17:10:39 | [diff] [blame] | 325 | MockContentSettingsObserver mock_observer(view_->GetMainRenderFrame()); |
[email protected] | 6f9f7d8 | 2011-12-08 12:13:34 | [diff] [blame] | 326 | // Block scripts. |
| 327 | RendererContentSettingRules content_setting_rules; |
| 328 | ContentSettingsForOneType& script_setting_rules = |
| 329 | content_setting_rules.script_rules; |
| 330 | script_setting_rules.push_back( |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 331 | ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), |
| 332 | ContentSettingsPattern::Wildcard(), |
| 333 | CONTENT_SETTING_BLOCK, |
| 334 | std::string(), |
| 335 | false)); |
[email protected] | 6f9f7d8 | 2011-12-08 12:13:34 | [diff] [blame] | 336 | // Block images. |
| 337 | ContentSettingsForOneType& image_setting_rules = |
| 338 | content_setting_rules.image_rules; |
| 339 | image_setting_rules.push_back( |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 340 | ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), |
| 341 | ContentSettingsPattern::Wildcard(), |
| 342 | CONTENT_SETTING_BLOCK, |
| 343 | std::string(), |
| 344 | false)); |
[email protected] | 6f9f7d8 | 2011-12-08 12:13:34 | [diff] [blame] | 345 | |
[email protected] | a86c0e96 | 2013-12-17 17:10:39 | [diff] [blame] | 346 | ContentSettingsObserver* observer = |
| 347 | ContentSettingsObserver::Get(view_->GetMainRenderFrame()); |
[email protected] | 6f9f7d8 | 2011-12-08 12:13:34 | [diff] [blame] | 348 | observer->SetContentSettingRules(&content_setting_rules); |
[email protected] | 85f0a57 | 2012-02-07 22:20:13 | [diff] [blame] | 349 | observer->OnSetAsInterstitial(); |
[email protected] | 6f9f7d8 | 2011-12-08 12:13:34 | [diff] [blame] | 350 | |
| 351 | // Load a page which contains a script. |
| 352 | std::string html = "<html>" |
| 353 | "<head>" |
| 354 | "<script src='data:foo'></script>" |
| 355 | "</head>" |
| 356 | "<body>" |
| 357 | "</body>" |
| 358 | "</html>"; |
| 359 | LoadHTML(html.c_str()); |
| 360 | |
| 361 | // Verify that the script was allowed. |
| 362 | bool was_blocked = false; |
| 363 | for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) { |
| 364 | const IPC::Message* msg = render_thread_->sink().GetMessageAt(i); |
| 365 | if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID) |
| 366 | was_blocked = true; |
| 367 | } |
| 368 | EXPECT_FALSE(was_blocked); |
| 369 | |
| 370 | // Verify that images are allowed. |
wfh | 303f76b | 2014-12-09 22:52:59 | [diff] [blame^] | 371 | EXPECT_CALL(mock_observer, OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, |
| 372 | base::string16())).Times(0); |
[email protected] | 098ac6f | 2014-04-08 15:02:44 | [diff] [blame] | 373 | EXPECT_TRUE(observer->allowImage(true, mock_observer.image_url_)); |
[email protected] | 6f9f7d8 | 2011-12-08 12:13:34 | [diff] [blame] | 374 | ::testing::Mock::VerifyAndClearExpectations(&observer); |
| 375 | } |