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