| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "base/run_loop.h" | 5 #include "base/run_loop.h" |
| 6 #include "content/common/frame_messages.h" | 6 #include "content/common/frame_messages.h" |
| 7 #include "content/common/view_message_enums.h" | 7 #include "content/common/view_message_enums.h" |
| 8 #include "content/public/common/content_constants.h" |
| 8 #include "content/public/test/render_view_test.h" | 9 #include "content/public/test/render_view_test.h" |
| 9 #include "content/renderer/pepper/plugin_power_saver_helper.h" | 10 #include "content/renderer/pepper/plugin_power_saver_helper.h" |
| 10 #include "content/renderer/render_frame_impl.h" | 11 #include "content/renderer/render_frame_impl.h" |
| 11 #include "content/renderer/render_view_impl.h" | 12 #include "content/renderer/render_view_impl.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "third_party/WebKit/public/web/WebDocument.h" | 14 #include "third_party/WebKit/public/web/WebDocument.h" |
| 14 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 15 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 15 #include "third_party/WebKit/public/web/WebPluginParams.h" | 16 #include "third_party/WebKit/public/web/WebPluginParams.h" |
| 16 #include "url/gurl.h" | 17 #include "url/gurl.h" |
| 17 | 18 |
| 18 namespace content { | 19 namespace content { |
| 19 | 20 |
| 20 class PluginPowerSaverHelperTest : public RenderViewTest { | 21 class PluginPowerSaverHelperTest : public RenderViewTest { |
| 21 public: | 22 public: |
| 22 PluginPowerSaverHelperTest() : sink_(NULL) {} | 23 PluginPowerSaverHelperTest() : sink_(NULL) {} |
| 23 | 24 |
| 24 RenderFrameImpl* frame() { | 25 RenderFrameImpl* frame() { |
| 25 return static_cast<RenderFrameImpl*>(view_->GetMainRenderFrame()); | 26 return static_cast<RenderFrameImpl*>(view_->GetMainRenderFrame()); |
| 26 } | 27 } |
| 27 | 28 |
| 29 PluginPowerSaverHelper* helper() { |
| 30 return frame()->plugin_power_saver_helper(); |
| 31 } |
| 32 |
| 28 void SetUp() override { | 33 void SetUp() override { |
| 29 RenderViewTest::SetUp(); | 34 RenderViewTest::SetUp(); |
| 30 sink_ = &render_thread_->sink(); | 35 sink_ = &render_thread_->sink(); |
| 31 } | 36 } |
| 32 | 37 |
| 33 blink::WebPluginParams MakeParams(const std::string& url, | 38 blink::WebPluginParams MakeParams(const std::string& url, |
| 34 const std::string& poster, | 39 const std::string& poster, |
| 35 const std::string& width, | 40 const std::string& width, |
| 36 const std::string& height) { | 41 const std::string& height) { |
| 37 const size_t size = 3; | 42 const size_t size = 3; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 53 | 58 |
| 54 return params; | 59 return params; |
| 55 } | 60 } |
| 56 | 61 |
| 57 protected: | 62 protected: |
| 58 IPC::TestSink* sink_; | 63 IPC::TestSink* sink_; |
| 59 | 64 |
| 60 DISALLOW_COPY_AND_ASSIGN(PluginPowerSaverHelperTest); | 65 DISALLOW_COPY_AND_ASSIGN(PluginPowerSaverHelperTest); |
| 61 }; | 66 }; |
| 62 | 67 |
| 63 TEST_F(PluginPowerSaverHelperTest, PosterImage) { | |
| 64 size_t size = 3; | |
| 65 blink::WebVector<blink::WebString> names(size); | |
| 66 blink::WebVector<blink::WebString> values(size); | |
| 67 | |
| 68 blink::WebPluginParams params; | |
| 69 params.url = GURL("http://b.com/foo.swf"); | |
| 70 | |
| 71 params.attributeNames.swap(names); | |
| 72 params.attributeValues.swap(values); | |
| 73 | |
| 74 params.attributeNames[0] = "poster"; | |
| 75 params.attributeNames[1] = "height"; | |
| 76 params.attributeNames[2] = "width"; | |
| 77 params.attributeValues[0] = "poster.jpg"; | |
| 78 params.attributeValues[1] = "100"; | |
| 79 params.attributeValues[2] = "100"; | |
| 80 | |
| 81 GURL poster_result; | |
| 82 | |
| 83 EXPECT_TRUE(frame()->ShouldThrottleContent( | |
| 84 MakeParams("http://b.com/foo.swf", "poster.jpg", "100", "100"), | |
| 85 GURL("http://a.com/page.html"), &poster_result, nullptr)); | |
| 86 EXPECT_EQ(GURL("http://a.com/poster.jpg"), poster_result); | |
| 87 | |
| 88 // Ignore empty poster paramaters. | |
| 89 EXPECT_TRUE(frame()->ShouldThrottleContent( | |
| 90 MakeParams("http://b.com/foo.swf", std::string(), "100", "100"), | |
| 91 GURL("http://a.com/page.html"), &poster_result, nullptr)); | |
| 92 EXPECT_EQ(GURL(), poster_result); | |
| 93 | |
| 94 // Ignore poster parameter when plugin is big (shouldn't be throttled). | |
| 95 EXPECT_FALSE(frame()->ShouldThrottleContent( | |
| 96 MakeParams("http://b.com/foo.swf", "poster.jpg", "500", "500"), | |
| 97 GURL("http://a.com/page.html"), &poster_result, nullptr)); | |
| 98 EXPECT_EQ(GURL(), poster_result); | |
| 99 } | |
| 100 | |
| 101 TEST_F(PluginPowerSaverHelperTest, AllowSameOrigin) { | 68 TEST_F(PluginPowerSaverHelperTest, AllowSameOrigin) { |
| 102 EXPECT_FALSE(frame()->ShouldThrottleContent( | 69 EXPECT_FALSE(helper()->ShouldThrottleContent(GURL(), kFlashPluginName, 100, |
| 103 MakeParams(std::string(), std::string(), "100", "100"), GURL(), nullptr, | 70 100, nullptr)); |
| 104 nullptr)); | 71 EXPECT_FALSE(helper()->ShouldThrottleContent(GURL(), kFlashPluginName, 1000, |
| 105 EXPECT_FALSE(frame()->ShouldThrottleContent( | 72 1000, nullptr)); |
| 106 MakeParams(std::string(), std::string(), "1000", "1000"), GURL(), nullptr, | |
| 107 nullptr)); | |
| 108 } | 73 } |
| 109 | 74 |
| 110 TEST_F(PluginPowerSaverHelperTest, DisallowCrossOriginUnlessLarge) { | 75 TEST_F(PluginPowerSaverHelperTest, DisallowCrossOriginUnlessLarge) { |
| 111 bool cross_origin_main_content = false; | 76 bool cross_origin_main_content = false; |
| 112 EXPECT_TRUE(frame()->ShouldThrottleContent( | 77 EXPECT_TRUE(helper()->ShouldThrottleContent(GURL("http://b.com"), |
| 113 MakeParams("http://b.com/foo.swf", std::string(), "100", "100"), GURL(), | 78 kFlashPluginName, 100, 100, |
| 114 nullptr, &cross_origin_main_content)); | 79 &cross_origin_main_content)); |
| 115 EXPECT_FALSE(cross_origin_main_content); | 80 EXPECT_FALSE(cross_origin_main_content); |
| 116 | 81 |
| 117 EXPECT_FALSE(frame()->ShouldThrottleContent( | 82 EXPECT_FALSE(helper()->ShouldThrottleContent(GURL("http://b.com"), |
| 118 MakeParams("http://b.com/foo.swf", std::string(), "1000", "1000"), GURL(), | 83 kFlashPluginName, 1000, 1000, |
| 119 nullptr, &cross_origin_main_content)); | 84 &cross_origin_main_content)); |
| 120 EXPECT_TRUE(cross_origin_main_content); | 85 EXPECT_TRUE(cross_origin_main_content); |
| 121 } | 86 } |
| 122 | 87 |
| 123 TEST_F(PluginPowerSaverHelperTest, AlwaysAllowTinyContent) { | 88 TEST_F(PluginPowerSaverHelperTest, AlwaysAllowTinyContent) { |
| 124 bool cross_origin_main_content = false; | 89 bool cross_origin_main_content = false; |
| 125 EXPECT_FALSE(frame()->ShouldThrottleContent( | 90 EXPECT_FALSE( |
| 126 MakeParams(std::string(), std::string(), "1", "1"), GURL(), nullptr, | 91 helper()->ShouldThrottleContent(GURL(), kFlashPluginName, 1, 1, nullptr)); |
| 127 &cross_origin_main_content)); | |
| 128 EXPECT_FALSE(cross_origin_main_content); | 92 EXPECT_FALSE(cross_origin_main_content); |
| 129 | 93 |
| 130 EXPECT_FALSE(frame()->ShouldThrottleContent( | 94 EXPECT_FALSE(helper()->ShouldThrottleContent(GURL("http://b.com"), |
| 131 MakeParams("http://b.com/foo.swf", std::string(), "1", "1"), GURL(), | 95 kFlashPluginName, 1, 1, |
| 132 nullptr, &cross_origin_main_content)); | 96 &cross_origin_main_content)); |
| 133 EXPECT_FALSE(cross_origin_main_content); | 97 EXPECT_FALSE(cross_origin_main_content); |
| 134 | 98 |
| 135 EXPECT_FALSE(frame()->ShouldThrottleContent( | 99 EXPECT_FALSE(helper()->ShouldThrottleContent(GURL("http://b.com"), |
| 136 MakeParams("http://b.com/foo.swf", std::string(), "5", "5"), GURL(), | 100 kFlashPluginName, 5, 5, |
| 137 nullptr, &cross_origin_main_content)); | 101 &cross_origin_main_content)); |
| 138 EXPECT_FALSE(cross_origin_main_content); | 102 EXPECT_FALSE(cross_origin_main_content); |
| 139 | 103 |
| 140 EXPECT_TRUE(frame()->ShouldThrottleContent( | 104 EXPECT_TRUE(helper()->ShouldThrottleContent(GURL("http://b.com"), |
| 141 MakeParams("http://b.com/foo.swf", std::string(), "10", "10"), GURL(), | 105 kFlashPluginName, 10, 10, |
| 142 nullptr, &cross_origin_main_content)); | 106 &cross_origin_main_content)); |
| 143 EXPECT_FALSE(cross_origin_main_content); | 107 EXPECT_FALSE(cross_origin_main_content); |
| 144 } | 108 } |
| 145 | 109 |
| 146 TEST_F(PluginPowerSaverHelperTest, TemporaryOriginWhitelist) { | 110 TEST_F(PluginPowerSaverHelperTest, TemporaryOriginWhitelist) { |
| 147 bool cross_origin_main_content = false; | 111 bool cross_origin_main_content = false; |
| 148 EXPECT_TRUE(frame()->ShouldThrottleContent( | 112 EXPECT_TRUE(helper()->ShouldThrottleContent(GURL("http://b.com"), |
| 149 MakeParams("http://b.com/foo.swf", std::string(), "100", "100"), GURL(), | 113 kFlashPluginName, 100, 100, |
| 150 nullptr, &cross_origin_main_content)); | 114 &cross_origin_main_content)); |
| 151 EXPECT_FALSE(cross_origin_main_content); | 115 EXPECT_FALSE(cross_origin_main_content); |
| 152 | 116 |
| 153 // Clear out other messages so we find just the plugin power saver IPCs. | 117 // Clear out other messages so we find just the plugin power saver IPCs. |
| 154 sink_->ClearMessages(); | 118 sink_->ClearMessages(); |
| 155 | 119 |
| 156 frame()->WhitelistContentOrigin(GURL("http://b.com")); | 120 helper()->WhitelistContentOrigin(GURL("http://b.com")); |
| 157 EXPECT_FALSE(frame()->ShouldThrottleContent( | 121 EXPECT_FALSE(helper()->ShouldThrottleContent(GURL("http://b.com"), |
| 158 MakeParams("http://b.com/foo.swf", std::string(), "100", "100"), GURL(), | 122 kFlashPluginName, 100, 100, |
| 159 nullptr, &cross_origin_main_content)); | 123 &cross_origin_main_content)); |
| 160 EXPECT_FALSE(cross_origin_main_content); | 124 EXPECT_FALSE(cross_origin_main_content); |
| 161 | 125 |
| 162 // Test that we've sent an IPC to the browser. | 126 // Test that we've sent an IPC to the browser. |
| 163 ASSERT_EQ(1u, sink_->message_count()); | 127 ASSERT_EQ(1u, sink_->message_count()); |
| 164 const IPC::Message* msg = sink_->GetMessageAt(0); | 128 const IPC::Message* msg = sink_->GetMessageAt(0); |
| 165 EXPECT_EQ(FrameHostMsg_PluginContentOriginAllowed::ID, msg->type()); | 129 EXPECT_EQ(FrameHostMsg_PluginContentOriginAllowed::ID, msg->type()); |
| 166 FrameHostMsg_PluginContentOriginAllowed::Param params; | 130 FrameHostMsg_PluginContentOriginAllowed::Param params; |
| 167 FrameHostMsg_PluginContentOriginAllowed::Read(msg, ¶ms); | 131 FrameHostMsg_PluginContentOriginAllowed::Read(msg, ¶ms); |
| 168 EXPECT_EQ(GURL("http://b.com"), get<0>(params)); | 132 EXPECT_EQ(GURL("http://b.com"), get<0>(params)); |
| 169 } | 133 } |
| 170 | 134 |
| 171 TEST_F(PluginPowerSaverHelperTest, UnthrottleOnExPostFactoWhitelist) { | 135 TEST_F(PluginPowerSaverHelperTest, UnthrottleOnExPostFactoWhitelist) { |
| 172 base::RunLoop loop; | 136 base::RunLoop loop; |
| 173 frame()->RegisterPeripheralPlugin(GURL("http://other.com"), | 137 frame()->RegisterPeripheralPlugin(GURL("http://other.com"), |
| 174 loop.QuitClosure()); | 138 loop.QuitClosure()); |
| 175 | 139 |
| 176 std::set<GURL> origin_whitelist; | 140 std::set<GURL> origin_whitelist; |
| 177 origin_whitelist.insert(GURL("http://other.com")); | 141 origin_whitelist.insert(GURL("http://other.com")); |
| 178 frame()->OnMessageReceived(FrameMsg_UpdatePluginContentOriginWhitelist( | 142 frame()->OnMessageReceived(FrameMsg_UpdatePluginContentOriginWhitelist( |
| 179 frame()->GetRoutingID(), origin_whitelist)); | 143 frame()->GetRoutingID(), origin_whitelist)); |
| 180 | 144 |
| 181 // Runs until the unthrottle closure is run. | 145 // Runs until the unthrottle closure is run. |
| 182 loop.Run(); | 146 loop.Run(); |
| 183 } | 147 } |
| 184 | 148 |
| 185 TEST_F(PluginPowerSaverHelperTest, ClearWhitelistOnNavigate) { | 149 TEST_F(PluginPowerSaverHelperTest, ClearWhitelistOnNavigate) { |
| 186 frame()->WhitelistContentOrigin(GURL("http://b.com")); | 150 helper()->WhitelistContentOrigin(GURL("http://b.com")); |
| 187 | 151 |
| 188 EXPECT_FALSE(frame()->ShouldThrottleContent( | 152 EXPECT_FALSE(helper()->ShouldThrottleContent( |
| 189 MakeParams("http://b.com/foo.swf", std::string(), "100", "100"), GURL(), | 153 GURL("http://b.com"), kFlashPluginName, 100, 100, nullptr)); |
| 190 nullptr, nullptr)); | |
| 191 | 154 |
| 192 LoadHTML("<html></html>"); | 155 LoadHTML("<html></html>"); |
| 193 | 156 |
| 194 EXPECT_TRUE(frame()->ShouldThrottleContent( | 157 EXPECT_TRUE(helper()->ShouldThrottleContent( |
| 195 MakeParams("http://b.com/foo.swf", std::string(), "100", "100"), GURL(), | 158 GURL("http://b.com"), kFlashPluginName, 100, 100, nullptr)); |
| 196 nullptr, nullptr)); | |
| 197 } | 159 } |
| 198 | 160 |
| 199 } // namespace content | 161 } // namespace content |
| OLD | NEW |