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
3 // found in the LICENSE file.
5 #include "base/run_loop.h"
6 #include "content/common/frame_messages.h"
7 #include "content/common/view_message_enums.h"
8 #include "content/public/test/render_view_test.h"
9 #include "content/renderer/pepper/plugin_power_saver_helper.h"
10 #include "content/renderer/render_frame_impl.h"
11 #include "content/renderer/render_view_impl.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/WebKit/public/web/WebDocument.h"
14 #include "third_party/WebKit/public/web/WebLocalFrame.h"
15 #include "third_party/WebKit/public/web/WebPluginParams.h"
20 class PluginPowerSaverHelperTest
: public RenderViewTest
{
22 PluginPowerSaverHelperTest() : sink_(NULL
) {}
24 RenderFrameImpl
* frame() {
25 return static_cast<RenderFrameImpl
*>(view_
->GetMainRenderFrame());
28 void SetUp() override
{
29 RenderViewTest::SetUp();
30 sink_
= &render_thread_
->sink();
33 blink::WebPluginParams
MakeParams(const std::string
& url
,
34 const std::string
& poster
,
35 const std::string
& width
,
36 const std::string
& height
) {
37 const size_t size
= 3;
38 blink::WebVector
<blink::WebString
> names(size
);
39 blink::WebVector
<blink::WebString
> values(size
);
41 blink::WebPluginParams params
;
42 params
.url
= GURL(url
);
44 params
.attributeNames
.swap(names
);
45 params
.attributeValues
.swap(values
);
47 params
.attributeNames
[0] = "poster";
48 params
.attributeNames
[1] = "height";
49 params
.attributeNames
[2] = "width";
50 params
.attributeValues
[0] = blink::WebString::fromUTF8(poster
);
51 params
.attributeValues
[1] = blink::WebString::fromUTF8(height
);
52 params
.attributeValues
[2] = blink::WebString::fromUTF8(width
);
60 DISALLOW_COPY_AND_ASSIGN(PluginPowerSaverHelperTest
);
63 TEST_F(PluginPowerSaverHelperTest
, PosterImage
) {
65 blink::WebVector
<blink::WebString
> names(size
);
66 blink::WebVector
<blink::WebString
> values(size
);
68 blink::WebPluginParams params
;
69 params
.url
= GURL("http://b.com/foo.swf");
71 params
.attributeNames
.swap(names
);
72 params
.attributeValues
.swap(values
);
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";
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
);
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
);
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
);
101 TEST_F(PluginPowerSaverHelperTest
, AllowSameOrigin
) {
102 EXPECT_FALSE(frame()->ShouldThrottleContent(
103 MakeParams(std::string(), std::string(), "100", "100"), GURL(), nullptr,
105 EXPECT_FALSE(frame()->ShouldThrottleContent(
106 MakeParams(std::string(), std::string(), "1000", "1000"), GURL(), nullptr,
110 TEST_F(PluginPowerSaverHelperTest
, DisallowCrossOriginUnlessLarge
) {
111 bool cross_origin_main_content
= false;
112 EXPECT_TRUE(frame()->ShouldThrottleContent(
113 MakeParams("http://b.com/foo.swf", std::string(), "100", "100"), GURL(),
114 nullptr, &cross_origin_main_content
));
115 EXPECT_FALSE(cross_origin_main_content
);
117 EXPECT_FALSE(frame()->ShouldThrottleContent(
118 MakeParams("http://b.com/foo.swf", std::string(), "1000", "1000"), GURL(),
119 nullptr, &cross_origin_main_content
));
120 EXPECT_TRUE(cross_origin_main_content
);
123 TEST_F(PluginPowerSaverHelperTest
, AlwaysAllowTinyContent
) {
124 bool cross_origin_main_content
= false;
125 EXPECT_FALSE(frame()->ShouldThrottleContent(
126 MakeParams(std::string(), std::string(), "1", "1"), GURL(), nullptr,
127 &cross_origin_main_content
));
128 EXPECT_FALSE(cross_origin_main_content
);
130 EXPECT_FALSE(frame()->ShouldThrottleContent(
131 MakeParams("http://b.com/foo.swf", std::string(), "1", "1"), GURL(),
132 nullptr, &cross_origin_main_content
));
133 EXPECT_FALSE(cross_origin_main_content
);
135 EXPECT_FALSE(frame()->ShouldThrottleContent(
136 MakeParams("http://b.com/foo.swf", std::string(), "5", "5"), GURL(),
137 nullptr, &cross_origin_main_content
));
138 EXPECT_FALSE(cross_origin_main_content
);
140 EXPECT_TRUE(frame()->ShouldThrottleContent(
141 MakeParams("http://b.com/foo.swf", std::string(), "10", "10"), GURL(),
142 nullptr, &cross_origin_main_content
));
143 EXPECT_FALSE(cross_origin_main_content
);
146 TEST_F(PluginPowerSaverHelperTest
, TemporaryOriginWhitelist
) {
147 bool cross_origin_main_content
= false;
148 EXPECT_TRUE(frame()->ShouldThrottleContent(
149 MakeParams("http://b.com/foo.swf", std::string(), "100", "100"), GURL(),
150 nullptr, &cross_origin_main_content
));
151 EXPECT_FALSE(cross_origin_main_content
);
153 // Clear out other messages so we find just the plugin power saver IPCs.
154 sink_
->ClearMessages();
156 frame()->WhitelistContentOrigin(GURL("http://b.com"));
157 EXPECT_FALSE(frame()->ShouldThrottleContent(
158 MakeParams("http://b.com/foo.swf", std::string(), "100", "100"), GURL(),
159 nullptr, &cross_origin_main_content
));
160 EXPECT_FALSE(cross_origin_main_content
);
162 // Test that we've sent an IPC to the browser.
163 ASSERT_EQ(1u, sink_
->message_count());
164 const IPC::Message
* msg
= sink_
->GetMessageAt(0);
165 EXPECT_EQ(FrameHostMsg_PluginContentOriginAllowed::ID
, msg
->type());
166 FrameHostMsg_PluginContentOriginAllowed::Param params
;
167 FrameHostMsg_PluginContentOriginAllowed::Read(msg
, ¶ms
);
168 EXPECT_EQ(GURL("http://b.com"), get
<0>(params
));
171 TEST_F(PluginPowerSaverHelperTest
, UnthrottleOnExPostFactoWhitelist
) {
173 frame()->RegisterPeripheralPlugin(GURL("http://other.com"),
176 std::set
<GURL
> origin_whitelist
;
177 origin_whitelist
.insert(GURL("http://other.com"));
178 frame()->OnMessageReceived(FrameMsg_UpdatePluginContentOriginWhitelist(
179 frame()->GetRoutingID(), origin_whitelist
));
181 // Runs until the unthrottle closure is run.
185 TEST_F(PluginPowerSaverHelperTest
, ClearWhitelistOnNavigate
) {
186 frame()->WhitelistContentOrigin(GURL("http://b.com"));
188 EXPECT_FALSE(frame()->ShouldThrottleContent(
189 MakeParams("http://b.com/foo.swf", std::string(), "100", "100"), GURL(),
192 LoadHTML("<html></html>");
194 EXPECT_TRUE(frame()->ShouldThrottleContent(
195 MakeParams("http://b.com/foo.swf", std::string(), "100", "100"), GURL(),
199 } // namespace content