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