OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/plugins/renderer/loadable_plugin_placeholder.h" | 5 #include "components/plugins/renderer/loadable_plugin_placeholder.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/json/string_escape.h" | 10 #include "base/json/string_escape.h" |
11 #include "base/strings/string_piece.h" | 11 #include "base/strings/string_piece.h" |
| 12 #include "base/strings/stringprintf.h" |
12 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
13 #include "base/thread_task_runner_handle.h" | 14 #include "base/thread_task_runner_handle.h" |
14 #include "base/values.h" | 15 #include "base/values.h" |
15 #include "content/public/child/v8_value_converter.h" | 16 #include "content/public/child/v8_value_converter.h" |
16 #include "content/public/common/content_switches.h" | 17 #include "content/public/common/content_switches.h" |
17 #include "content/public/renderer/render_frame.h" | 18 #include "content/public/renderer/render_frame.h" |
18 #include "content/public/renderer/render_thread.h" | 19 #include "content/public/renderer/render_thread.h" |
19 #include "gin/handle.h" | 20 #include "gin/handle.h" |
20 #include "gin/object_template_builder.h" | 21 #include "gin/object_template_builder.h" |
21 #include "third_party/WebKit/public/web/WebDOMMessageEvent.h" | 22 #include "third_party/WebKit/public/web/WebDOMMessageEvent.h" |
22 #include "third_party/WebKit/public/web/WebDocument.h" | 23 #include "third_party/WebKit/public/web/WebDocument.h" |
23 #include "third_party/WebKit/public/web/WebElement.h" | 24 #include "third_party/WebKit/public/web/WebElement.h" |
24 #include "third_party/WebKit/public/web/WebInputEvent.h" | 25 #include "third_party/WebKit/public/web/WebInputEvent.h" |
25 #include "third_party/WebKit/public/web/WebKit.h" | 26 #include "third_party/WebKit/public/web/WebKit.h" |
26 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 27 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
27 #include "third_party/WebKit/public/web/WebPluginContainer.h" | 28 #include "third_party/WebKit/public/web/WebPluginContainer.h" |
28 #include "third_party/WebKit/public/web/WebScriptSource.h" | 29 #include "third_party/WebKit/public/web/WebScriptSource.h" |
29 #include "third_party/WebKit/public/web/WebSerializedScriptValue.h" | 30 #include "third_party/WebKit/public/web/WebSerializedScriptValue.h" |
30 #include "third_party/WebKit/public/web/WebView.h" | 31 #include "third_party/WebKit/public/web/WebView.h" |
31 | 32 |
32 using base::UserMetricsAction; | 33 using base::UserMetricsAction; |
33 using content::PluginInstanceThrottler; | 34 using content::PluginInstanceThrottler; |
34 using content::RenderThread; | 35 using content::RenderThread; |
35 | 36 |
36 namespace plugins { | 37 namespace plugins { |
37 | 38 |
38 // TODO(tommycli): After an unthrottling size update, re-check the size after | 39 // TODO(tommycli): After a size update, re-check the size after this delay, as |
39 // this delay, as Blink can report incorrect sizes to plugins while the | 40 // Blink can report incorrect sizes to plugins while the compositing state is |
40 // compositing state is dirty. Chosen because it seems to work. | 41 // dirty. Chosen because it seems to work. |
41 const int kSizeChangeRecheckDelayMilliseconds = 100; | 42 const int kSizeChangeRecheckDelayMilliseconds = 100; |
42 | 43 |
43 void LoadablePluginPlaceholder::BlockForPowerSaverPoster() { | 44 void LoadablePluginPlaceholder::BlockForPowerSaverPoster() { |
44 DCHECK(!is_blocked_for_power_saver_poster_); | 45 DCHECK(!is_blocked_for_power_saver_poster_); |
45 is_blocked_for_power_saver_poster_ = true; | 46 is_blocked_for_power_saver_poster_ = true; |
46 | 47 |
47 render_frame()->RegisterPeripheralPlugin( | 48 render_frame()->RegisterPeripheralPlugin( |
48 GURL(GetPluginParams().url).GetOrigin(), | 49 GURL(GetPluginParams().url).GetOrigin(), |
49 base::Bind(&LoadablePluginPlaceholder::MarkPluginEssential, | 50 base::Bind(&LoadablePluginPlaceholder::MarkPluginEssential, |
50 weak_factory_.GetWeakPtr(), | 51 weak_factory_.GetWeakPtr(), |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 | 172 |
172 v8::Local<v8::Object> LoadablePluginPlaceholder::GetV8ScriptableObject( | 173 v8::Local<v8::Object> LoadablePluginPlaceholder::GetV8ScriptableObject( |
173 v8::Isolate* isolate) const { | 174 v8::Isolate* isolate) const { |
174 // Pass through JavaScript access to the underlying throttled plugin. | 175 // Pass through JavaScript access to the underlying throttled plugin. |
175 if (premade_throttler_ && premade_throttler_->GetWebPlugin()) { | 176 if (premade_throttler_ && premade_throttler_->GetWebPlugin()) { |
176 return premade_throttler_->GetWebPlugin()->v8ScriptableObject(isolate); | 177 return premade_throttler_->GetWebPlugin()->v8ScriptableObject(isolate); |
177 } | 178 } |
178 return v8::Local<v8::Object>(); | 179 return v8::Local<v8::Object>(); |
179 } | 180 } |
180 | 181 |
181 void LoadablePluginPlaceholder::OnUnobscuredSizeUpdate( | 182 void LoadablePluginPlaceholder::OnUnobscuredRectUpdate( |
182 const gfx::Size& unobscured_size) { | 183 const gfx::Rect& unobscured_rect) { |
183 DCHECK( | 184 DCHECK( |
184 content::RenderThread::Get()->GetTaskRunner()->BelongsToCurrentThread()); | 185 content::RenderThread::Get()->GetTaskRunner()->BelongsToCurrentThread()); |
185 if (!power_saver_enabled_ || !premade_throttler_ || !finished_loading_) | 186 if (!power_saver_enabled_ || !premade_throttler_ || !finished_loading_) |
186 return; | 187 return; |
187 | 188 |
188 unobscured_size_ = unobscured_size; | 189 unobscured_rect_ = unobscured_rect; |
189 | 190 |
190 // During a size recheck, we will get another notification into this method. | 191 // During a size recheck, we will get another notification into this method. |
191 // Use this flag to early exit to prevent reentrancy issues. | 192 // Use this flag to early exit to prevent reentrancy issues. |
192 if (in_size_recheck_) | 193 if (in_size_recheck_) |
193 return; | 194 return; |
194 | 195 |
195 if (PluginInstanceThrottler::IsLargeContent(unobscured_size.width(), | 196 if (!size_update_timer_.IsRunning()) { |
196 unobscured_size.height())) { | 197 // TODO(tommycli): We have to post a delayed task to recheck the size, as |
197 if (!size_update_timer_.IsRunning()) { | 198 // Blink can report wrong sizes for partially obscured plugins while the |
198 // TODO(tommycli): We have to post a delayed task to recheck the size, as | 199 // compositing state is dirty. https://crbug.com/343769 |
199 // Blink can report wrong sizes for partially obscured plugins while the | 200 size_update_timer_.Start( |
200 // compositing state is dirty. https://crbug.com/343769 | 201 FROM_HERE, |
201 size_update_timer_.Start( | 202 base::TimeDelta::FromMilliseconds(kSizeChangeRecheckDelayMilliseconds), |
202 FROM_HERE, base::TimeDelta::FromMilliseconds( | 203 base::Bind(&LoadablePluginPlaceholder::RecheckSizeAndMaybeUnthrottle, |
203 kSizeChangeRecheckDelayMilliseconds), | 204 weak_factory_.GetWeakPtr())); |
204 base::Bind(&LoadablePluginPlaceholder::RecheckSizeAndMaybeUnthrottle, | |
205 weak_factory_.GetWeakPtr())); | |
206 } | |
207 } else { | |
208 // Cancel any pending unthrottle due to resize calls. | |
209 size_update_timer_.Stop(); | |
210 } | 205 } |
211 } | 206 } |
212 | 207 |
213 void LoadablePluginPlaceholder::WasShown() { | 208 void LoadablePluginPlaceholder::WasShown() { |
214 if (is_blocked_for_background_tab_) { | 209 if (is_blocked_for_background_tab_) { |
215 is_blocked_for_background_tab_ = false; | 210 is_blocked_for_background_tab_ = false; |
216 if (!LoadingBlocked()) | 211 if (!LoadingBlocked()) |
217 LoadPlugin(); | 212 LoadPlugin(); |
218 } | 213 } |
219 } | 214 } |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
333 DCHECK(!in_size_recheck_); | 328 DCHECK(!in_size_recheck_); |
334 | 329 |
335 if (!plugin()) | 330 if (!plugin()) |
336 return; | 331 return; |
337 | 332 |
338 in_size_recheck_ = true; | 333 in_size_recheck_ = true; |
339 | 334 |
340 // Re-check the size in case the reported size was incorrect. | 335 // Re-check the size in case the reported size was incorrect. |
341 plugin()->container()->reportGeometry(); | 336 plugin()->container()->reportGeometry(); |
342 | 337 |
343 if (PluginInstanceThrottler::IsLargeContent(unobscured_size_.width(), | 338 // Adjust padding using clip coordinates to center play button for plugins |
344 unobscured_size_.height())) { | 339 // that have their top or left portions obscured. |
| 340 if (is_blocked_for_power_saver_poster_) { |
| 341 std::string script = |
| 342 base::StringPrintf("window.setPosterMargin('%dpx', '%dpx')", |
| 343 unobscured_rect_.x(), unobscured_rect_.y()); |
| 344 plugin()->web_view()->mainFrame()->executeScript( |
| 345 blink::WebScriptSource(base::UTF8ToUTF16(script))); |
| 346 } |
| 347 |
| 348 if (PluginInstanceThrottler::IsLargeContent(unobscured_rect_.width(), |
| 349 unobscured_rect_.height())) { |
345 MarkPluginEssential( | 350 MarkPluginEssential( |
346 PluginInstanceThrottler::UNTHROTTLE_METHOD_BY_SIZE_CHANGE); | 351 PluginInstanceThrottler::UNTHROTTLE_METHOD_BY_SIZE_CHANGE); |
347 } | 352 } |
348 | 353 |
349 in_size_recheck_ = false; | 354 in_size_recheck_ = false; |
350 } | 355 } |
351 | 356 |
352 } // namespace plugins | 357 } // namespace plugins |
OLD | NEW |