OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "chrome/renderer/plugins/webview_plugin.h" | 5 #include "components/plugins/renderer/webview_plugin.h" |
6 | 6 |
7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
| 9 #include "base/safe_numerics.h" |
9 #include "content/public/renderer/web_preferences.h" | 10 #include "content/public/renderer/web_preferences.h" |
10 #include "skia/ext/platform_canvas.h" | 11 #include "skia/ext/platform_canvas.h" |
11 #include "third_party/WebKit/public/platform/WebSize.h" | 12 #include "third_party/WebKit/public/platform/WebSize.h" |
12 #include "third_party/WebKit/public/platform/WebURL.h" | 13 #include "third_party/WebKit/public/platform/WebURL.h" |
13 #include "third_party/WebKit/public/platform/WebURLRequest.h" | 14 #include "third_party/WebKit/public/platform/WebURLRequest.h" |
14 #include "third_party/WebKit/public/platform/WebURLResponse.h" | 15 #include "third_party/WebKit/public/platform/WebURLResponse.h" |
15 #include "third_party/WebKit/public/web/WebCursorInfo.h" | 16 #include "third_party/WebKit/public/web/WebCursorInfo.h" |
16 #include "third_party/WebKit/public/web/WebDocument.h" | 17 #include "third_party/WebKit/public/web/WebDocument.h" |
17 #include "third_party/WebKit/public/web/WebElement.h" | 18 #include "third_party/WebKit/public/web/WebElement.h" |
18 #include "third_party/WebKit/public/web/WebFrame.h" | 19 #include "third_party/WebKit/public/web/WebFrame.h" |
(...skipping 16 matching lines...) Expand all Loading... |
35 using WebKit::WebRect; | 36 using WebKit::WebRect; |
36 using WebKit::WebSize; | 37 using WebKit::WebSize; |
37 using WebKit::WebString; | 38 using WebKit::WebString; |
38 using WebKit::WebURLError; | 39 using WebKit::WebURLError; |
39 using WebKit::WebURLRequest; | 40 using WebKit::WebURLRequest; |
40 using WebKit::WebURLResponse; | 41 using WebKit::WebURLResponse; |
41 using WebKit::WebVector; | 42 using WebKit::WebVector; |
42 using WebKit::WebView; | 43 using WebKit::WebView; |
43 | 44 |
44 WebViewPlugin::WebViewPlugin(WebViewPlugin::Delegate* delegate) | 45 WebViewPlugin::WebViewPlugin(WebViewPlugin::Delegate* delegate) |
45 : delegate_(delegate), | 46 : delegate_(delegate), container_(NULL), finished_loading_(false) { |
46 container_(NULL), | |
47 finished_loading_(false) { | |
48 web_view_ = WebView::create(this); | 47 web_view_ = WebView::create(this); |
49 web_view_->initializeMainFrame(this); | 48 web_view_->initializeMainFrame(this); |
50 } | 49 } |
51 | 50 |
52 // static | 51 // static |
53 WebViewPlugin* WebViewPlugin::Create(WebViewPlugin::Delegate* delegate, | 52 WebViewPlugin* WebViewPlugin::Create(WebViewPlugin::Delegate* delegate, |
54 const WebPreferences& preferences, | 53 const WebPreferences& preferences, |
55 const std::string& html_data, | 54 const std::string& html_data, |
56 const GURL& url) { | 55 const GURL& url) { |
57 WebViewPlugin* plugin = new WebViewPlugin(delegate); | 56 WebViewPlugin* plugin = new WebViewPlugin(delegate); |
58 WebView* web_view = plugin->web_view(); | 57 WebView* web_view = plugin->web_view(); |
59 content::ApplyWebPreferences(preferences, web_view); | 58 content::ApplyWebPreferences(preferences, web_view); |
60 web_view->mainFrame()->loadHTMLString(html_data, url); | 59 web_view->mainFrame()->loadHTMLString(html_data, url); |
61 return plugin; | 60 return plugin; |
62 } | 61 } |
63 | 62 |
64 WebViewPlugin::~WebViewPlugin() { | 63 WebViewPlugin::~WebViewPlugin() { web_view_->close(); } |
65 web_view_->close(); | |
66 } | |
67 | 64 |
68 void WebViewPlugin::ReplayReceivedData(WebPlugin* plugin) { | 65 void WebViewPlugin::ReplayReceivedData(WebPlugin* plugin) { |
69 if (!response_.isNull()) { | 66 if (!response_.isNull()) { |
70 plugin->didReceiveResponse(response_); | 67 plugin->didReceiveResponse(response_); |
71 size_t total_bytes = 0; | 68 size_t total_bytes = 0; |
72 for (std::list<std::string>::iterator it = data_.begin(); | 69 for (std::list<std::string>::iterator it = data_.begin(); it != data_.end(); |
73 it != data_.end(); ++it) { | 70 ++it) { |
74 plugin->didReceiveData(it->c_str(), it->length()); | 71 plugin->didReceiveData( |
| 72 it->c_str(), base::checked_numeric_cast<int, size_t>(it->length())); |
75 total_bytes += it->length(); | 73 total_bytes += it->length(); |
76 } | 74 } |
77 UMA_HISTOGRAM_MEMORY_KB("PluginDocument.Memory", (total_bytes / 1024)); | 75 UMA_HISTOGRAM_MEMORY_KB( |
78 UMA_HISTOGRAM_COUNTS("PluginDocument.NumChunks", data_.size()); | 76 "PluginDocument.Memory", |
| 77 (base::checked_numeric_cast<int, size_t>(total_bytes / 1024))); |
| 78 UMA_HISTOGRAM_COUNTS( |
| 79 "PluginDocument.NumChunks", |
| 80 (base::checked_numeric_cast<int, size_t>(data_.size()))); |
79 } | 81 } |
80 if (finished_loading_) { | 82 if (finished_loading_) { |
81 plugin->didFinishLoading(); | 83 plugin->didFinishLoading(); |
82 } | 84 } |
83 if (error_) { | 85 if (error_) { |
84 plugin->didFailLoading(*error_); | 86 plugin->didFailLoading(*error_); |
85 } | 87 } |
86 } | 88 } |
87 | 89 |
88 void WebViewPlugin::RestoreTitleText() { | 90 void WebViewPlugin::RestoreTitleText() { |
89 if (container_) | 91 if (container_) |
90 container_->element().setAttribute("title", old_title_); | 92 container_->element().setAttribute("title", old_title_); |
91 } | 93 } |
92 | 94 |
93 WebPluginContainer* WebViewPlugin::container() const { | 95 WebPluginContainer* WebViewPlugin::container() const { return container_; } |
94 return container_; | |
95 } | |
96 | 96 |
97 bool WebViewPlugin::initialize(WebPluginContainer* container) { | 97 bool WebViewPlugin::initialize(WebPluginContainer* container) { |
98 container_ = container; | 98 container_ = container; |
99 if (container_) | 99 if (container_) |
100 old_title_ = container_->element().getAttribute("title"); | 100 old_title_ = container_->element().getAttribute("title"); |
101 return true; | 101 return true; |
102 } | 102 } |
103 | 103 |
104 void WebViewPlugin::destroy() { | 104 void WebViewPlugin::destroy() { |
105 if (delegate_) { | 105 if (delegate_) { |
106 delegate_->WillDestroyPlugin(); | 106 delegate_->WillDestroyPlugin(); |
107 delegate_ = NULL; | 107 delegate_ = NULL; |
108 } | 108 } |
109 container_ = NULL; | 109 container_ = NULL; |
110 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); | 110 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); |
111 } | 111 } |
112 | 112 |
113 NPObject* WebViewPlugin::scriptableObject() { | 113 NPObject* WebViewPlugin::scriptableObject() { return NULL; } |
114 return NULL; | |
115 } | |
116 | 114 |
117 struct _NPP* WebViewPlugin::pluginNPP() { | 115 struct _NPP* WebViewPlugin::pluginNPP() { return NULL; } |
118 return NULL; | |
119 } | |
120 | 116 |
121 bool WebViewPlugin::getFormValue(WebString& value) { | 117 bool WebViewPlugin::getFormValue(WebString& value) { return false; } |
122 return false; | |
123 } | |
124 | 118 |
125 void WebViewPlugin::paint(WebCanvas* canvas, const WebRect& rect) { | 119 void WebViewPlugin::paint(WebCanvas* canvas, const WebRect& rect) { |
126 gfx::Rect paint_rect = gfx::IntersectRects(rect_, rect); | 120 gfx::Rect paint_rect = gfx::IntersectRects(rect_, rect); |
127 if (paint_rect.IsEmpty()) | 121 if (paint_rect.IsEmpty()) |
128 return; | 122 return; |
129 | 123 |
130 paint_rect.Offset(-rect_.x(), -rect_.y()); | 124 paint_rect.Offset(-rect_.x(), -rect_.y()); |
131 | 125 |
132 canvas->translate(SkIntToScalar(rect_.x()), SkIntToScalar(rect_.y())); | 126 canvas->translate(SkIntToScalar(rect_.x()), SkIntToScalar(rect_.y())); |
133 canvas->save(); | 127 canvas->save(); |
134 | 128 |
135 web_view_->layout(); | 129 web_view_->layout(); |
136 web_view_->paint(canvas, paint_rect); | 130 web_view_->paint(canvas, paint_rect); |
137 | 131 |
138 canvas->restore(); | 132 canvas->restore(); |
139 } | 133 } |
140 | 134 |
141 // Coordinates are relative to the containing window. | 135 // Coordinates are relative to the containing window. |
142 void WebViewPlugin::updateGeometry( | 136 void WebViewPlugin::updateGeometry(const WebRect& frame_rect, |
143 const WebRect& frame_rect, const WebRect& clip_rect, | 137 const WebRect& clip_rect, |
144 const WebVector<WebRect>& cut_out_rects, bool is_visible) { | 138 const WebVector<WebRect>& cut_out_rects, |
| 139 bool is_visible) { |
145 if (static_cast<gfx::Rect>(frame_rect) != rect_) { | 140 if (static_cast<gfx::Rect>(frame_rect) != rect_) { |
146 rect_ = frame_rect; | 141 rect_ = frame_rect; |
147 web_view_->resize(WebSize(frame_rect.width, frame_rect.height)); | 142 web_view_->resize(WebSize(frame_rect.width, frame_rect.height)); |
148 } | 143 } |
149 } | 144 } |
150 | 145 |
151 bool WebViewPlugin::acceptsInputEvents() { | 146 bool WebViewPlugin::acceptsInputEvents() { return true; } |
152 return true; | |
153 } | |
154 | 147 |
155 bool WebViewPlugin::handleInputEvent(const WebInputEvent& event, | 148 bool WebViewPlugin::handleInputEvent(const WebInputEvent& event, |
156 WebCursorInfo& cursor) { | 149 WebCursorInfo& cursor) { |
157 // For tap events, don't handle them. They will be converted to | 150 // For tap events, don't handle them. They will be converted to |
158 // mouse events later and passed to here. | 151 // mouse events later and passed to here. |
159 if (event.type == WebInputEvent::GestureTap) | 152 if (event.type == WebInputEvent::GestureTap) |
160 return false; | 153 return false; |
161 | 154 |
162 if (event.type == WebInputEvent::ContextMenu) { | 155 if (event.type == WebInputEvent::ContextMenu) { |
163 if (delegate_) { | 156 if (delegate_) { |
(...skipping 22 matching lines...) Expand all Loading... |
186 void WebViewPlugin::didFinishLoading() { | 179 void WebViewPlugin::didFinishLoading() { |
187 DCHECK(!finished_loading_); | 180 DCHECK(!finished_loading_); |
188 finished_loading_ = true; | 181 finished_loading_ = true; |
189 } | 182 } |
190 | 183 |
191 void WebViewPlugin::didFailLoading(const WebURLError& error) { | 184 void WebViewPlugin::didFailLoading(const WebURLError& error) { |
192 DCHECK(!error_.get()); | 185 DCHECK(!error_.get()); |
193 error_.reset(new WebURLError(error)); | 186 error_.reset(new WebURLError(error)); |
194 } | 187 } |
195 | 188 |
196 bool WebViewPlugin::acceptsLoadDrops() { | 189 bool WebViewPlugin::acceptsLoadDrops() { return false; } |
197 return false; | |
198 } | |
199 | 190 |
200 void WebViewPlugin::setToolTipText(const WebString& text, | 191 void WebViewPlugin::setToolTipText(const WebString& text, |
201 WebKit::WebTextDirection hint) { | 192 WebKit::WebTextDirection hint) { |
202 if (container_) | 193 if (container_) |
203 container_->element().setAttribute("title", text); | 194 container_->element().setAttribute("title", text); |
204 } | 195 } |
205 | 196 |
206 void WebViewPlugin::startDragging(WebFrame*, | 197 void WebViewPlugin::startDragging(WebFrame*, |
207 const WebDragData&, | 198 const WebDragData&, |
208 WebDragOperationsMask, | 199 WebDragOperationsMask, |
(...skipping 15 matching lines...) Expand all Loading... |
224 void WebViewPlugin::didClearWindowObject(WebFrame* frame) { | 215 void WebViewPlugin::didClearWindowObject(WebFrame* frame) { |
225 if (delegate_) | 216 if (delegate_) |
226 delegate_->BindWebFrame(frame); | 217 delegate_->BindWebFrame(frame); |
227 } | 218 } |
228 | 219 |
229 void WebViewPlugin::didReceiveResponse(WebFrame* frame, | 220 void WebViewPlugin::didReceiveResponse(WebFrame* frame, |
230 unsigned identifier, | 221 unsigned identifier, |
231 const WebURLResponse& response) { | 222 const WebURLResponse& response) { |
232 WebFrameClient::didReceiveResponse(frame, identifier, response); | 223 WebFrameClient::didReceiveResponse(frame, identifier, response); |
233 } | 224 } |
OLD | NEW |