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