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 #ifndef COMPONENTS_PLUGINS_RENDERER_WEBVIEW_PLUGIN_H_
6 #define COMPONENTS_PLUGINS_RENDERER_WEBVIEW_PLUGIN_H_
10 #include "base/memory/scoped_ptr.h"
11 #include "base/sequenced_task_runner_helpers.h"
12 #include "third_party/WebKit/public/platform/WebString.h"
13 #include "third_party/WebKit/public/platform/WebURLResponse.h"
14 #include "third_party/WebKit/public/web/WebCursorInfo.h"
15 #include "third_party/WebKit/public/web/WebFrameClient.h"
16 #include "third_party/WebKit/public/web/WebPlugin.h"
17 #include "third_party/WebKit/public/web/WebViewClient.h"
19 struct WebPreferences;
25 // This class implements the WebPlugin interface by forwarding drawing and
26 // handling input events to a WebView.
27 // It can be used as a placeholder for an actual plugin, using HTML for the UI.
28 // To show HTML data inside the WebViewPlugin,
29 // call web_view->mainFrame()->loadHTMLString() with the HTML data and a fake
30 // chrome:// URL as origin.
32 class WebViewPlugin : public WebKit::WebPlugin,
33 public WebKit::WebViewClient,
34 public WebKit::WebFrameClient {
38 // Bind |frame| to a Javascript object, enabling the delegate to receive
39 // callback methods from Javascript inside the WebFrame.
40 // This method is called from WebFrameClient::didClearWindowObject.
41 virtual void BindWebFrame(WebKit::WebFrame* frame) = 0;
43 // Called before the WebViewPlugin is destroyed. The delegate should delete
45 virtual void WillDestroyPlugin() = 0;
47 // Called upon a context menu event.
48 virtual void ShowContextMenu(const WebKit::WebMouseEvent&) = 0;
51 explicit WebViewPlugin(Delegate* delegate);
53 // Convenience method to set up a new WebViewPlugin using |preferences|
54 // and displaying |html_data|. |url| should be a (fake) chrome:// URL; it is
55 // only used for navigation and never actually resolved.
56 static WebViewPlugin* Create(Delegate* delegate,
57 const WebPreferences& preferences,
58 const std::string& html_data,
61 WebKit::WebView* web_view() { return web_view_; }
63 // When loading a plug-in document (i.e. a full page plug-in not embedded in
64 // another page), we save all data that has been received, and replay it with
65 // this method on the actual plug-in.
66 void ReplayReceivedData(WebKit::WebPlugin* plugin);
68 void RestoreTitleText();
71 virtual WebKit::WebPluginContainer* container() const;
72 virtual bool initialize(WebKit::WebPluginContainer*);
73 virtual void destroy();
75 virtual NPObject* scriptableObject();
76 virtual struct _NPP* pluginNPP();
78 virtual bool getFormValue(WebKit::WebString& value);
80 virtual void paint(WebKit::WebCanvas* canvas, const WebKit::WebRect& rect);
82 // Coordinates are relative to the containing window.
83 virtual void updateGeometry(
84 const WebKit::WebRect& frame_rect,
85 const WebKit::WebRect& clip_rect,
86 const WebKit::WebVector<WebKit::WebRect>& cut_out_rects,
89 virtual void updateFocus(bool) {}
90 virtual void updateVisibility(bool) {}
92 virtual bool acceptsInputEvents();
93 virtual bool handleInputEvent(const WebKit::WebInputEvent& event,
94 WebKit::WebCursorInfo& cursor_info);
96 virtual void didReceiveResponse(const WebKit::WebURLResponse& response);
97 virtual void didReceiveData(const char* data, int data_length);
98 virtual void didFinishLoading();
99 virtual void didFailLoading(const WebKit::WebURLError& error);
101 // Called in response to WebPluginContainer::loadFrameRequest
102 virtual void didFinishLoadingFrameRequest(const WebKit::WebURL& url,
104 virtual void didFailLoadingFrameRequest(const WebKit::WebURL& url,
106 const WebKit::WebURLError& error) {}
108 // WebViewClient methods:
109 virtual bool acceptsLoadDrops();
111 virtual void setToolTipText(const WebKit::WebString&,
112 WebKit::WebTextDirection);
114 virtual void startDragging(WebKit::WebFrame* frame,
115 const WebKit::WebDragData& drag_data,
116 WebKit::WebDragOperationsMask mask,
117 const WebKit::WebImage& image,
118 const WebKit::WebPoint& point);
120 // WebWidgetClient methods:
121 virtual void didInvalidateRect(const WebKit::WebRect&);
122 virtual void didChangeCursor(const WebKit::WebCursorInfo& cursor);
124 // WebFrameClient methods:
125 virtual void didClearWindowObject(WebKit::WebFrame* frame);
127 // This method is defined in WebPlugin as well as in WebFrameClient, but with
128 // different parameters. We only care about implementing the WebPlugin
129 // version, so we implement this method and call the default in WebFrameClient
130 // (which does nothing) to correctly overload it.
131 virtual void didReceiveResponse(WebKit::WebFrame* frame,
133 const WebKit::WebURLResponse& response);
136 friend class base::DeleteHelper<WebViewPlugin>;
137 virtual ~WebViewPlugin();
141 WebKit::WebCursorInfo current_cursor_;
143 WebKit::WebPluginContainer* container_;
144 // Owned by us, deleted via |close()|.
145 WebKit::WebView* web_view_;
148 WebKit::WebURLResponse response_;
149 std::list<std::string> data_;
150 bool finished_loading_;
151 scoped_ptr<WebKit::WebURLError> error_;
152 WebKit::WebString old_title_;
155 #endif // COMPONENTS_PLUGINS_RENDERER_WEBVIEW_PLUGIN_H_