1 // Copyright (c) 2012 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 #ifndef CHROME_RENDERER_PLUGINS_WEBVIEW_PLUGIN_H_ 6 #define CHROME_RENDERER_PLUGINS_WEBVIEW_PLUGIN_H_ 7 8 #include <list> 9 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" 18 19 struct WebPreferences; 20 21 namespace WebKit { 22 class WebMouseEvent; 23 } 24 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. 31 32 class WebViewPlugin : public WebKit::WebPlugin, 33 public WebKit::WebViewClient, 34 public WebKit::WebFrameClient { 35 public: 36 class Delegate { 37 public: 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; 42 43 // Called before the WebViewPlugin is destroyed. The delegate should delete 44 // itself here. 45 virtual void WillDestroyPlugin() = 0; 46 47 // Called upon a context menu event. 48 virtual void ShowContextMenu(const WebKit::WebMouseEvent&) = 0; 49 }; 50 51 explicit WebViewPlugin(Delegate* delegate); 52 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( 57 Delegate* delegate, 58 const WebPreferences& preferences, 59 const std::string& html_data, 60 const GURL& url); 61 62 WebKit::WebView* web_view() { return web_view_; } 63 64 // When loading a plug-in document (i.e. a full page plug-in not embedded in 65 // another page), we save all data that has been received, and replay it with 66 // this method on the actual plug-in. 67 void ReplayReceivedData(WebKit::WebPlugin* plugin); 68 69 void RestoreTitleText(); 70 71 // WebPlugin methods: 72 virtual WebKit::WebPluginContainer* container() const; 73 virtual bool initialize(WebKit::WebPluginContainer*); 74 virtual void destroy(); 75 76 virtual NPObject* scriptableObject(); 77 virtual struct _NPP* pluginNPP(); 78 79 virtual bool getFormValue(WebKit::WebString& value); 80 81 virtual void paint(WebKit::WebCanvas* canvas, const WebKit::WebRect& rect); 82 83 // Coordinates are relative to the containing window. 84 virtual void updateGeometry( 85 const WebKit::WebRect& frame_rect, const WebKit::WebRect& clip_rect, 86 const WebKit::WebVector<WebKit::WebRect>& cut_out_rects, bool is_visible); 87 88 virtual void updateFocus(bool) {} 89 virtual void updateVisibility(bool) {} 90 91 virtual bool acceptsInputEvents(); 92 virtual bool handleInputEvent(const WebKit::WebInputEvent& event, 93 WebKit::WebCursorInfo& cursor_info); 94 95 virtual void didReceiveResponse(const WebKit::WebURLResponse& response); 96 virtual void didReceiveData(const char* data, int data_length); 97 virtual void didFinishLoading(); 98 virtual void didFailLoading(const WebKit::WebURLError& error); 99 100 // Called in response to WebPluginContainer::loadFrameRequest 101 virtual void didFinishLoadingFrameRequest( 102 const WebKit::WebURL& url, void* notifyData) {} 103 virtual void didFailLoadingFrameRequest(const WebKit::WebURL& url, 104 void* notify_data, 105 const WebKit::WebURLError& error) {} 106 107 // WebViewClient methods: 108 virtual bool acceptsLoadDrops(); 109 110 virtual void setToolTipText(const WebKit::WebString&, 111 WebKit::WebTextDirection); 112 113 virtual void startDragging(WebKit::WebFrame* frame, 114 const WebKit::WebDragData& drag_data, 115 WebKit::WebDragOperationsMask mask, 116 const WebKit::WebImage& image, 117 const WebKit::WebPoint& point); 118 119 // WebWidgetClient methods: 120 virtual void didInvalidateRect(const WebKit::WebRect&); 121 virtual void didChangeCursor(const WebKit::WebCursorInfo& cursor); 122 123 // WebFrameClient methods: 124 virtual void didClearWindowObject(WebKit::WebFrame* frame); 125 126 // This method is defined in WebPlugin as well as in WebFrameClient, but with 127 // different parameters. We only care about implementing the WebPlugin 128 // version, so we implement this method and call the default in WebFrameClient 129 // (which does nothing) to correctly overload it. 130 virtual void didReceiveResponse(WebKit::WebFrame* frame, 131 unsigned identifier, 132 const WebKit::WebURLResponse& response); 133 134 private: 135 friend class base::DeleteHelper<WebViewPlugin>; 136 virtual ~WebViewPlugin(); 137 138 Delegate* delegate_; 139 // Destroys itself. 140 WebKit::WebCursorInfo current_cursor_; 141 // Owns us. 142 WebKit::WebPluginContainer* container_; 143 // Owned by us, deleted via |close()|. 144 WebKit::WebView* web_view_; 145 gfx::Rect rect_; 146 147 WebKit::WebURLResponse response_; 148 std::list<std::string> data_; 149 bool finished_loading_; 150 scoped_ptr<WebKit::WebURLError> error_; 151 WebKit::WebString old_title_; 152 }; 153 154 #endif // CHROME_RENDERER_PLUGINS_WEBVIEW_PLUGIN_H_ 155