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