webview_plugin.h revision c5cede9ae108bb15f6b7a8aea21c7e1fefa2834c
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. 4 5#ifndef COMPONENTS_PLUGINS_RENDERER_WEBVIEW_PLUGIN_H_ 6#define COMPONENTS_PLUGINS_RENDERER_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/WebCursorInfo.h" 13#include "third_party/WebKit/public/platform/WebString.h" 14#include "third_party/WebKit/public/platform/WebURLResponse.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 19struct WebPreferences; 20 21namespace blink { 22class 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 32class WebViewPlugin : public blink::WebPlugin, 33 public blink::WebViewClient, 34 public blink::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(blink::WebFrame* frame) = 0; 42 43 // Called upon a context menu event. 44 virtual void ShowContextMenu(const blink::WebMouseEvent&) = 0; 45 46 // Called when the WebViewPlugin is destroyed. 47 virtual void PluginDestroyed() = 0; 48 }; 49 50 explicit WebViewPlugin(Delegate* delegate); 51 52 // Convenience method to set up a new WebViewPlugin using |preferences| 53 // and displaying |html_data|. |url| should be a (fake) chrome:// URL; it is 54 // only used for navigation and never actually resolved. 55 static WebViewPlugin* Create(Delegate* delegate, 56 const WebPreferences& preferences, 57 const std::string& html_data, 58 const GURL& url); 59 60 blink::WebView* web_view() { return web_view_; } 61 62 // When loading a plug-in document (i.e. a full page plug-in not embedded in 63 // another page), we save all data that has been received, and replay it with 64 // this method on the actual plug-in. 65 void ReplayReceivedData(blink::WebPlugin* plugin); 66 67 void RestoreTitleText(); 68 69 // WebPlugin methods: 70 virtual blink::WebPluginContainer* container() const; 71 virtual bool initialize(blink::WebPluginContainer*); 72 virtual void destroy(); 73 74 virtual NPObject* scriptableObject(); 75 virtual struct _NPP* pluginNPP(); 76 77 virtual bool getFormValue(blink::WebString& value); 78 79 virtual void paint(blink::WebCanvas* canvas, const blink::WebRect& rect); 80 81 // Coordinates are relative to the containing window. 82 virtual void updateGeometry( 83 const blink::WebRect& frame_rect, 84 const blink::WebRect& clip_rect, 85 const blink::WebVector<blink::WebRect>& cut_out_rects, 86 bool is_visible); 87 88 virtual void updateFocus(bool); 89 virtual void updateVisibility(bool) {} 90 91 virtual bool acceptsInputEvents(); 92 virtual bool handleInputEvent(const blink::WebInputEvent& event, 93 blink::WebCursorInfo& cursor_info); 94 95 virtual void didReceiveResponse(const blink::WebURLResponse& response); 96 virtual void didReceiveData(const char* data, int data_length); 97 virtual void didFinishLoading(); 98 virtual void didFailLoading(const blink::WebURLError& error); 99 100 // Called in response to WebPluginContainer::loadFrameRequest 101 virtual void didFinishLoadingFrameRequest(const blink::WebURL& url, 102 void* notifyData) {} 103 virtual void didFailLoadingFrameRequest(const blink::WebURL& url, 104 void* notify_data, 105 const blink::WebURLError& error) {} 106 107 // WebViewClient methods: 108 virtual bool acceptsLoadDrops(); 109 110 virtual void setToolTipText(const blink::WebString&, 111 blink::WebTextDirection); 112 113 virtual void startDragging(blink::WebLocalFrame* frame, 114 const blink::WebDragData& drag_data, 115 blink::WebDragOperationsMask mask, 116 const blink::WebImage& image, 117 const blink::WebPoint& point); 118 119 // WebWidgetClient methods: 120 virtual void didInvalidateRect(const blink::WebRect&); 121 virtual void didChangeCursor(const blink::WebCursorInfo& cursor); 122 123 // WebFrameClient methods: 124 virtual void didClearWindowObject(blink::WebLocalFrame* frame, int world_id); 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(blink::WebLocalFrame* frame, 131 unsigned identifier, 132 const blink::WebURLResponse& response); 133 134 private: 135 friend class base::DeleteHelper<WebViewPlugin>; 136 virtual ~WebViewPlugin(); 137 138 // Manages its own lifetime. 139 Delegate* delegate_; 140 141 blink::WebCursorInfo current_cursor_; 142 143 // Owns us. 144 blink::WebPluginContainer* container_; 145 146 // Owned by us, deleted via |close()|. 147 blink::WebView* web_view_; 148 149 // Owned by us, deleted via |close()|. 150 blink::WebFrame* web_frame_; 151 gfx::Rect rect_; 152 153 blink::WebURLResponse response_; 154 std::list<std::string> data_; 155 bool finished_loading_; 156 scoped_ptr<blink::WebURLError> error_; 157 blink::WebString old_title_; 158 bool focused_; 159}; 160 161#endif // COMPONENTS_PLUGINS_RENDERER_WEBVIEW_PLUGIN_H_ 162