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/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/WebKit.h"
17 #include "third_party/WebKit/public/web/WebPlugin.h"
18 #include "third_party/WebKit/public/web/WebViewClient.h"
26 struct WebPreferences
;
33 // This class implements the WebPlugin interface by forwarding drawing and
34 // handling input events to a WebView.
35 // It can be used as a placeholder for an actual plugin, using HTML for the UI.
36 // To show HTML data inside the WebViewPlugin,
37 // call web_view->mainFrame()->loadHTMLString() with the HTML data and a fake
38 // chrome:// URL as origin.
40 class WebViewPlugin
: public blink::WebPlugin
,
41 public blink::WebViewClient
,
42 public blink::WebFrameClient
{
46 // Called to get the V8 handle used to bind the lifetime to the frame.
47 virtual v8::Local
<v8::Value
> GetV8Handle(v8::Isolate
*) = 0;
49 // Called upon a context menu event.
50 virtual void ShowContextMenu(const blink::WebMouseEvent
&) = 0;
52 // Called when the WebViewPlugin is destroyed.
53 virtual void PluginDestroyed() = 0;
55 // Called to enable JavaScript pass-through to a throttled plugin, which is
56 // loaded but idle. Doesn't work for blocked plugins, which is not loaded.
57 virtual v8::Local
<v8::Object
> GetV8ScriptableObject(v8::Isolate
*) const = 0;
59 // Called when the unobscured rect of the plugin is updated.
60 virtual void OnUnobscuredRectUpdate(const gfx::Rect
& unobscured_rect
) {}
63 // Convenience method to set up a new WebViewPlugin using |preferences|
64 // and displaying |html_data|. |url| should be a (fake) data:text/html URL;
65 // it is only used for navigation and never actually resolved.
66 static WebViewPlugin
* Create(Delegate
* delegate
,
67 const content::WebPreferences
& preferences
,
68 const std::string
& html_data
,
71 blink::WebView
* web_view() { return web_view_
; }
73 // When loading a plugin document (i.e. a full page plugin not embedded in
74 // another page), we save all data that has been received, and replay it with
75 // this method on the actual plugin.
76 void ReplayReceivedData(blink::WebPlugin
* plugin
);
78 void RestoreTitleText();
81 virtual blink::WebPluginContainer
* container() const;
82 virtual bool initialize(blink::WebPluginContainer
*);
83 virtual void destroy();
85 virtual v8::Local
<v8::Object
> v8ScriptableObject(v8::Isolate
* isolate
);
87 virtual void layoutIfNeeded() override
;
88 virtual void paint(blink::WebCanvas
* canvas
,
89 const blink::WebRect
& rect
) override
;
91 // Coordinates are relative to the containing window.
92 virtual void updateGeometry(
93 const blink::WebRect
& window_rect
,
94 const blink::WebRect
& clip_rect
,
95 const blink::WebRect
& unobscured_rect
,
96 const blink::WebVector
<blink::WebRect
>& cut_outs_rects
,
99 virtual void updateFocus(bool foucsed
, blink::WebFocusType focus_type
);
100 virtual void updateVisibility(bool) {}
102 virtual bool acceptsInputEvents();
103 virtual bool handleInputEvent(const blink::WebInputEvent
& event
,
104 blink::WebCursorInfo
& cursor_info
);
106 virtual void didReceiveResponse(const blink::WebURLResponse
& response
);
107 virtual void didReceiveData(const char* data
, int data_length
);
108 virtual void didFinishLoading();
109 virtual void didFailLoading(const blink::WebURLError
& error
);
111 // Called in response to WebPluginContainer::loadFrameRequest
112 virtual void didFinishLoadingFrameRequest(const blink::WebURL
& url
,
114 virtual void didFailLoadingFrameRequest(const blink::WebURL
& url
,
116 const blink::WebURLError
& error
) {}
118 // WebViewClient methods:
119 virtual bool acceptsLoadDrops();
121 virtual void setToolTipText(const blink::WebString
&,
122 blink::WebTextDirection
);
124 virtual void startDragging(blink::WebLocalFrame
* frame
,
125 const blink::WebDragData
& drag_data
,
126 blink::WebDragOperationsMask mask
,
127 const blink::WebImage
& image
,
128 const blink::WebPoint
& point
);
130 // TODO(ojan): Remove this override and have this class use a non-null
132 virtual bool allowsBrokenNullLayerTreeView() const;
134 // WebWidgetClient methods:
135 virtual void didInvalidateRect(const blink::WebRect
&);
136 virtual void didUpdateLayoutSize(const blink::WebSize
&);
137 virtual void didChangeCursor(const blink::WebCursorInfo
& cursor
);
138 virtual void scheduleAnimation();
140 // WebFrameClient methods:
141 virtual void didClearWindowObject(blink::WebLocalFrame
* frame
);
143 // This method is defined in WebPlugin as well as in WebFrameClient, but with
144 // different parameters. We only care about implementing the WebPlugin
145 // version, so we implement this method and call the default in WebFrameClient
146 // (which does nothing) to correctly overload it.
147 virtual void didReceiveResponse(blink::WebLocalFrame
* frame
,
149 const blink::WebURLResponse
& response
);
152 friend class base::DeleteHelper
<WebViewPlugin
>;
153 WebViewPlugin(Delegate
* delegate
, const content::WebPreferences
& preferences
);
154 virtual ~WebViewPlugin();
156 // Manages its own lifetime.
159 blink::WebCursorInfo current_cursor_
;
162 blink::WebPluginContainer
* container_
;
164 // Owned by us, deleted via |close()|.
165 blink::WebView
* web_view_
;
167 // Owned by us, deleted via |close()|.
168 blink::WebFrame
* web_frame_
;
171 blink::WebURLResponse response_
;
172 std::list
<std::string
> data_
;
173 scoped_ptr
<blink::WebURLError
> error_
;
174 blink::WebString old_title_
;
175 bool finished_loading_
;
179 #endif // COMPONENTS_PLUGINS_RENDERER_WEBVIEW_PLUGIN_H_