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/common/content_constants.h"
9 #include "content/public/test/frame_load_waiter.h"
10 #include "content/public/test/render_view_test.h"
11 #include "content/renderer/pepper/plugin_power_saver_helper.h"
12 #include "content/renderer/render_frame_impl.h"
13 #include "content/renderer/render_view_impl.h"
14 #include "content/test/test_render_frame.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "third_party/WebKit/public/web/WebDocument.h"
17 #include "third_party/WebKit/public/web/WebLocalFrame.h"
18 #include "third_party/WebKit/public/web/WebPluginParams.h"
23 class PluginPowerSaverHelperTest
: public RenderViewTest
{
25 PluginPowerSaverHelperTest() : sink_(NULL
) {}
27 void SetUp() override
{
28 RenderViewTest::SetUp();
29 sink_
= &render_thread_
->sink();
32 RenderFrameImpl
* frame() {
33 return static_cast<RenderFrameImpl
*>(view_
->GetMainRenderFrame());
36 PluginPowerSaverHelper
* helper() {
37 return frame()->plugin_power_saver_helper();
40 blink::WebPluginParams
MakeParams(const std::string
& url
,
41 const std::string
& poster
,
42 const std::string
& width
,
43 const std::string
& height
) {
44 const size_t size
= 3;
45 blink::WebVector
<blink::WebString
> names(size
);
46 blink::WebVector
<blink::WebString
> values(size
);
48 blink::WebPluginParams params
;
49 params
.url
= GURL(url
);
51 params
.attributeNames
.swap(names
);
52 params
.attributeValues
.swap(values
);
54 params
.attributeNames
[0] = "poster";
55 params
.attributeNames
[1] = "height";
56 params
.attributeNames
[2] = "width";
57 params
.attributeValues
[0] = blink::WebString::fromUTF8(poster
);
58 params
.attributeValues
[1] = blink::WebString::fromUTF8(height
);
59 params
.attributeValues
[2] = blink::WebString::fromUTF8(width
);
67 DISALLOW_COPY_AND_ASSIGN(PluginPowerSaverHelperTest
);
70 TEST_F(PluginPowerSaverHelperTest
, AllowSameOrigin
) {
72 helper()->ShouldThrottleContent(url::Origin(GURL("http://same.com")),
73 url::Origin(GURL("http://same.com")),
74 kFlashPluginName
, 100, 100, nullptr));
76 helper()->ShouldThrottleContent(url::Origin(GURL("http://same.com")),
77 url::Origin(GURL("http://same.com")),
78 kFlashPluginName
, 1000, 1000, nullptr));
81 TEST_F(PluginPowerSaverHelperTest
, DisallowCrossOriginUnlessLarge
) {
82 bool cross_origin_main_content
= false;
83 EXPECT_TRUE(helper()->ShouldThrottleContent(
84 url::Origin(GURL("http://same.com")),
85 url::Origin(GURL("http://other.com")), kFlashPluginName
, 100, 100,
86 &cross_origin_main_content
));
87 EXPECT_FALSE(cross_origin_main_content
);
89 EXPECT_FALSE(helper()->ShouldThrottleContent(
90 url::Origin(GURL("http://same.com")),
91 url::Origin(GURL("http://other.com")), kFlashPluginName
, 1000, 1000,
92 &cross_origin_main_content
));
93 EXPECT_TRUE(cross_origin_main_content
);
96 TEST_F(PluginPowerSaverHelperTest
, AlwaysAllowTinyContent
) {
97 bool cross_origin_main_content
= false;
98 EXPECT_FALSE(helper()->ShouldThrottleContent(
99 url::Origin(GURL("http://same.com")),
100 url::Origin(GURL("http://same.com")), kFlashPluginName
, 1, 1, nullptr));
101 EXPECT_FALSE(cross_origin_main_content
);
103 EXPECT_FALSE(helper()->ShouldThrottleContent(
104 url::Origin(GURL("http://same.com")),
105 url::Origin(GURL("http://other.com")), kFlashPluginName
, 1, 1,
106 &cross_origin_main_content
));
107 EXPECT_FALSE(cross_origin_main_content
);
109 EXPECT_FALSE(helper()->ShouldThrottleContent(
110 url::Origin(GURL("http://same.com")),
111 url::Origin(GURL("http://other.com")), kFlashPluginName
, 5, 5,
112 &cross_origin_main_content
));
113 EXPECT_FALSE(cross_origin_main_content
);
115 EXPECT_TRUE(helper()->ShouldThrottleContent(
116 url::Origin(GURL("http://same.com")),
117 url::Origin(GURL("http://other.com")), kFlashPluginName
, 10, 10,
118 &cross_origin_main_content
));
119 EXPECT_FALSE(cross_origin_main_content
);
122 TEST_F(PluginPowerSaverHelperTest
, TemporaryOriginWhitelist
) {
123 bool cross_origin_main_content
= false;
124 EXPECT_TRUE(helper()->ShouldThrottleContent(
125 url::Origin(GURL("http://same.com")),
126 url::Origin(GURL("http://other.com")), kFlashPluginName
, 100, 100,
127 &cross_origin_main_content
));
128 EXPECT_FALSE(cross_origin_main_content
);
130 // Clear out other messages so we find just the plugin power saver IPCs.
131 sink_
->ClearMessages();
133 helper()->WhitelistContentOrigin(url::Origin(GURL("http://other.com")));
134 EXPECT_FALSE(helper()->ShouldThrottleContent(
135 url::Origin(GURL("http://same.com")),
136 url::Origin(GURL("http://other.com")), kFlashPluginName
, 100, 100,
137 &cross_origin_main_content
));
138 EXPECT_FALSE(cross_origin_main_content
);
140 // Test that we've sent an IPC to the browser.
141 ASSERT_EQ(1u, sink_
->message_count());
142 const IPC::Message
* msg
= sink_
->GetMessageAt(0);
143 EXPECT_EQ(FrameHostMsg_PluginContentOriginAllowed::ID
, msg
->type());
144 FrameHostMsg_PluginContentOriginAllowed::Param params
;
145 FrameHostMsg_PluginContentOriginAllowed::Read(msg
, ¶ms
);
146 EXPECT_TRUE(url::Origin(GURL("http://other.com"))
147 .IsSameOriginWith(base::get
<0>(params
)));
150 TEST_F(PluginPowerSaverHelperTest
, UnthrottleOnExPostFactoWhitelist
) {
152 frame()->RegisterPeripheralPlugin(url::Origin(GURL("http://other.com")),
155 std::set
<url::Origin
> origin_whitelist
;
156 origin_whitelist
.insert(url::Origin(GURL("http://other.com")));
157 frame()->OnMessageReceived(FrameMsg_UpdatePluginContentOriginWhitelist(
158 frame()->GetRoutingID(), origin_whitelist
));
160 // Runs until the unthrottle closure is run.
164 TEST_F(PluginPowerSaverHelperTest
, ClearWhitelistOnNavigate
) {
165 helper()->WhitelistContentOrigin(url::Origin(GURL("http://other.com")));
168 helper()->ShouldThrottleContent(url::Origin(GURL("http://same.com")),
169 url::Origin(GURL("http://other.com")),
170 kFlashPluginName
, 100, 100, nullptr));
172 LoadHTML("<html></html>");
175 helper()->ShouldThrottleContent(url::Origin(GURL("http://same.com")),
176 url::Origin(GURL("http://other.com")),
177 kFlashPluginName
, 100, 100, nullptr));
180 } // namespace content