Make sure webrtc::VideoSource is released when WebRtcVideoTrackAdapter is destroyed.
[chromium-blink-merge.git] / components / plugins / renderer / webview_plugin.cc
blob1e8399560f09b767cfc76cfb3f69ac336a70d1ae
1 // Copyright 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.
5 #include "components/plugins/renderer/webview_plugin.h"
7 #include "base/message_loop/message_loop.h"
8 #include "base/metrics/histogram.h"
9 #include "base/numerics/safe_conversions.h"
10 #include "content/public/renderer/web_preferences.h"
11 #include "skia/ext/platform_canvas.h"
12 #include "third_party/WebKit/public/platform/WebSize.h"
13 #include "third_party/WebKit/public/platform/WebURL.h"
14 #include "third_party/WebKit/public/platform/WebURLRequest.h"
15 #include "third_party/WebKit/public/platform/WebURLResponse.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/WebInputEvent.h"
19 #include "third_party/WebKit/public/web/WebLocalFrame.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"
24 using blink::WebCanvas;
25 using blink::WebCursorInfo;
26 using blink::WebDragData;
27 using blink::WebDragOperationsMask;
28 using blink::WebImage;
29 using blink::WebInputEvent;
30 using blink::WebLocalFrame;
31 using blink::WebMouseEvent;
32 using blink::WebPlugin;
33 using blink::WebPluginContainer;
34 using blink::WebPoint;
35 using blink::WebRect;
36 using blink::WebSize;
37 using blink::WebString;
38 using blink::WebURLError;
39 using blink::WebURLRequest;
40 using blink::WebURLResponse;
41 using blink::WebVector;
42 using blink::WebView;
44 WebViewPlugin::WebViewPlugin(WebViewPlugin::Delegate* delegate,
45 const WebPreferences& preferences)
46 : delegate_(delegate),
47 container_(NULL),
48 web_view_(WebView::create(this)),
49 finished_loading_(false),
50 focused_(false) {
51 // ApplyWebPreferences before making a WebLocalFrame so that the frame sees a
52 // consistent view of our preferences.
53 content::ApplyWebPreferences(preferences, web_view_);
54 web_frame_ = WebLocalFrame::create(this);
55 web_view_->setMainFrame(web_frame_);
58 // static
59 WebViewPlugin* WebViewPlugin::Create(WebViewPlugin::Delegate* delegate,
60 const WebPreferences& preferences,
61 const std::string& html_data,
62 const GURL& url) {
63 WebViewPlugin* plugin = new WebViewPlugin(delegate, preferences);
64 plugin->web_view()->mainFrame()->loadHTMLString(html_data, url);
65 return plugin;
68 WebViewPlugin::~WebViewPlugin() {
69 web_view_->close();
70 web_frame_->close();
73 void WebViewPlugin::ReplayReceivedData(WebPlugin* plugin) {
74 if (!response_.isNull()) {
75 plugin->didReceiveResponse(response_);
76 size_t total_bytes = 0;
77 for (std::list<std::string>::iterator it = data_.begin(); it != data_.end();
78 ++it) {
79 plugin->didReceiveData(
80 it->c_str(), base::checked_cast<int, size_t>(it->length()));
81 total_bytes += it->length();
83 UMA_HISTOGRAM_MEMORY_KB(
84 "PluginDocument.Memory",
85 (base::checked_cast<int, size_t>(total_bytes / 1024)));
86 UMA_HISTOGRAM_COUNTS(
87 "PluginDocument.NumChunks",
88 (base::checked_cast<int, size_t>(data_.size())));
90 // We need to transfer the |focused_| to new plugin after it loaded.
91 if (focused_) {
92 plugin->updateFocus(true);
94 if (finished_loading_) {
95 plugin->didFinishLoading();
97 if (error_) {
98 plugin->didFailLoading(*error_);
102 void WebViewPlugin::RestoreTitleText() {
103 if (container_)
104 container_->element().setAttribute("title", old_title_);
107 WebPluginContainer* WebViewPlugin::container() const { return container_; }
109 bool WebViewPlugin::initialize(WebPluginContainer* container) {
110 container_ = container;
111 if (container_)
112 old_title_ = container_->element().getAttribute("title");
113 return true;
116 void WebViewPlugin::destroy() {
117 if (delegate_) {
118 delegate_->PluginDestroyed();
119 delegate_ = NULL;
121 container_ = NULL;
122 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
125 NPObject* WebViewPlugin::scriptableObject() { return NULL; }
127 struct _NPP* WebViewPlugin::pluginNPP() { return NULL; }
129 bool WebViewPlugin::getFormValue(WebString& value) { return false; }
131 void WebViewPlugin::paint(WebCanvas* canvas, const WebRect& rect) {
132 gfx::Rect paint_rect = gfx::IntersectRects(rect_, rect);
133 if (paint_rect.IsEmpty())
134 return;
136 paint_rect.Offset(-rect_.x(), -rect_.y());
138 canvas->translate(SkIntToScalar(rect_.x()), SkIntToScalar(rect_.y()));
139 canvas->save();
141 web_view_->layout();
142 web_view_->paint(canvas, paint_rect);
144 canvas->restore();
147 // Coordinates are relative to the containing window.
148 void WebViewPlugin::updateGeometry(const WebRect& frame_rect,
149 const WebRect& clip_rect,
150 const WebVector<WebRect>& cut_out_rects,
151 bool is_visible) {
152 if (static_cast<gfx::Rect>(frame_rect) != rect_) {
153 rect_ = frame_rect;
154 WebSize newSize(frame_rect.width, frame_rect.height);
155 web_view_->setFixedLayoutSize(newSize);
156 web_view_->resize(newSize);
160 void WebViewPlugin::updateFocus(bool focused) {
161 focused_ = focused;
164 bool WebViewPlugin::acceptsInputEvents() { return true; }
166 bool WebViewPlugin::handleInputEvent(const WebInputEvent& event,
167 WebCursorInfo& cursor) {
168 // For tap events, don't handle them. They will be converted to
169 // mouse events later and passed to here.
170 if (event.type == WebInputEvent::GestureTap)
171 return false;
173 if (event.type == WebInputEvent::ContextMenu) {
174 if (delegate_) {
175 const WebMouseEvent& mouse_event =
176 reinterpret_cast<const WebMouseEvent&>(event);
177 delegate_->ShowContextMenu(mouse_event);
179 return true;
181 current_cursor_ = cursor;
182 bool handled = web_view_->handleInputEvent(event);
183 cursor = current_cursor_;
185 return handled;
188 void WebViewPlugin::didReceiveResponse(const WebURLResponse& response) {
189 DCHECK(response_.isNull());
190 response_ = response;
193 void WebViewPlugin::didReceiveData(const char* data, int data_length) {
194 data_.push_back(std::string(data, data_length));
197 void WebViewPlugin::didFinishLoading() {
198 DCHECK(!finished_loading_);
199 finished_loading_ = true;
202 void WebViewPlugin::didFailLoading(const WebURLError& error) {
203 DCHECK(!error_.get());
204 error_.reset(new WebURLError(error));
207 bool WebViewPlugin::acceptsLoadDrops() { return false; }
209 void WebViewPlugin::setToolTipText(const WebString& text,
210 blink::WebTextDirection hint) {
211 if (container_)
212 container_->element().setAttribute("title", text);
215 void WebViewPlugin::startDragging(WebLocalFrame*,
216 const WebDragData&,
217 WebDragOperationsMask,
218 const WebImage&,
219 const WebPoint&) {
220 // Immediately stop dragging.
221 web_view_->dragSourceSystemDragEnded();
224 bool WebViewPlugin::allowsBrokenNullLayerTreeView() const {
225 return true;
228 void WebViewPlugin::didInvalidateRect(const WebRect& rect) {
229 if (container_)
230 container_->invalidateRect(rect);
233 void WebViewPlugin::didChangeCursor(const WebCursorInfo& cursor) {
234 current_cursor_ = cursor;
237 void WebViewPlugin::didClearWindowObject(WebLocalFrame* frame) {
238 if (delegate_)
239 delegate_->BindWebFrame(frame);
242 void WebViewPlugin::didReceiveResponse(WebLocalFrame* frame,
243 unsigned identifier,
244 const WebURLResponse& response) {
245 WebFrameClient::didReceiveResponse(frame, identifier, response);