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