OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "base/callback.h" | |
6 #include "content/common/frame_messages.h" | |
7 #include "content/public/renderer/document_state.h" | |
8 #include "content/public/renderer/navigation_state.h" | |
9 #include "content/public/renderer/render_frame.h" | |
10 #include "content/renderer/pepper/plugin_power_saver_helper.h" | |
11 #include "third_party/WebKit/public/web/WebDocument.h" | |
12 #include "third_party/WebKit/public/web/WebLocalFrame.h" | |
13 #include "third_party/WebKit/public/web/WebView.h" | |
14 | |
15 namespace content { | |
16 | |
17 namespace { | |
18 | |
19 // Maximum dimensions plug-in content may have while still being considered | |
20 // peripheral content. These match the sizes used by Safari. | |
21 const int kPeripheralContentMaxWidth = 400; | |
22 const int kPeripheralContentMaxHeight = 300; | |
23 | |
24 } // namespace | |
25 | |
26 PluginPowerSaverHelper::PeripheralPlugin::PeripheralPlugin( | |
27 const GURL& content_origin, | |
28 const base::Closure& unthrottle_callback) | |
29 : content_origin(content_origin), unthrottle_callback(unthrottle_callback) { | |
30 } | |
31 | |
32 PluginPowerSaverHelper::PeripheralPlugin::~PeripheralPlugin() { | |
33 } | |
34 | |
35 PluginPowerSaverHelper::PluginPowerSaverHelper(RenderFrame* render_frame) | |
36 : RenderFrameObserver(render_frame) { | |
37 } | |
38 | |
39 PluginPowerSaverHelper::~PluginPowerSaverHelper() { | |
40 } | |
41 | |
42 void PluginPowerSaverHelper::DidCommitProvisionalLoad(bool is_new_navigation) { | |
43 blink::WebFrame* frame = render_frame()->GetWebFrame(); | |
44 if (frame->parent()) | |
45 return; // Not a top-level navigation. | |
46 | |
47 DocumentState* document_state = DocumentState::FromDataSource( | |
48 frame->dataSource()); | |
49 NavigationState* navigation_state = document_state->navigation_state(); | |
50 if (!navigation_state->was_within_same_page()) | |
51 origin_whitelist_.clear(); | |
52 } | |
53 | |
54 bool PluginPowerSaverHelper::OnMessageReceived(const IPC::Message& message) { | |
55 bool handled = true; | |
56 IPC_BEGIN_MESSAGE_MAP(PluginPowerSaverHelper, message) | |
57 IPC_MESSAGE_HANDLER(FrameMsg_UpdatePluginContentOriginWhitelist, | |
58 OnUpdatePluginContentOriginWhitelist) | |
59 IPC_MESSAGE_UNHANDLED(handled = false) | |
60 IPC_END_MESSAGE_MAP() | |
61 return handled; | |
62 } | |
63 | |
64 void PluginPowerSaverHelper::OnUpdatePluginContentOriginWhitelist( | |
65 const std::set<GURL>& origin_whitelist) { | |
66 origin_whitelist_ = origin_whitelist; | |
67 | |
68 // Check throttled plugin instances to see if any can be unthrottled. | |
69 auto it = peripheral_plugins_.begin(); | |
70 while (it != peripheral_plugins_.end()) { | |
71 if (origin_whitelist.count(it->content_origin)) { | |
72 it->unthrottle_callback.Run(); | |
73 it = peripheral_plugins_.erase(it); | |
74 } else { | |
75 ++it; | |
76 } | |
77 } | |
78 } | |
79 | |
80 bool PluginPowerSaverHelper::ShouldThrottleContent(const GURL& content_origin, | |
81 int width, | |
82 int height, | |
83 bool* cross_origin) const { | |
84 DCHECK(cross_origin); | |
85 *cross_origin = true; | |
86 | |
87 // TODO(alexmos): Update this to use the origin of the RemoteFrame when 426512 | |
88 // is fixed. For now, case 3 in the comment above doesn't work in | |
89 // --site-per-process mode. | |
90 blink::WebFrame* main_frame = | |
91 render_frame()->GetWebFrame()->view()->mainFrame(); | |
92 if (main_frame->isWebRemoteFrame()) | |
93 return true; | |
94 | |
95 // All same-origin plugin content is essential. | |
96 GURL main_frame_origin = GURL(main_frame->document().url()).GetOrigin(); | |
97 if (content_origin == main_frame_origin) { | |
98 *cross_origin = false; | |
99 return false; | |
100 } | |
101 | |
102 // Whitelisted plugin origins are also essential. | |
103 if (origin_whitelist_.count(content_origin)) | |
104 return false; | |
105 | |
106 // Cross-origin plugin content is peripheral if smaller than a maximum size. | |
107 bool content_is_small = width < kPeripheralContentMaxWidth || | |
108 height < kPeripheralContentMaxHeight; | |
109 | |
110 return content_is_small; | |
111 } | |
112 | |
113 void PluginPowerSaverHelper::RegisterPeripheralPlugin( | |
114 const GURL& content_origin, | |
115 const base::Closure& unthrottle_callback) { | |
116 peripheral_plugins_.push_back( | |
117 PeripheralPlugin(content_origin, unthrottle_callback)); | |
118 } | |
119 | |
120 void PluginPowerSaverHelper::WhitelistContentOrigin( | |
121 const GURL& content_origin) { | |
122 if (origin_whitelist_.count(content_origin) == 0) { | |
123 origin_whitelist_.insert(content_origin); | |
Lei Zhang
2014/10/29 22:31:23
nit: you can just always insert, and check the ret
tommycli
2014/10/29 22:53:23
Done.
| |
124 Send(new FrameHostMsg_PluginContentOriginAllowed( | |
125 render_frame()->GetRoutingID(), content_origin)); | |
126 } | |
127 } | |
128 | |
129 } // namespace content | |
OLD | NEW |