Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / content / renderer / npapi / webplugin_impl.h
blob918f578c1d78dd2831e5cad5423009629f3b3622
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.
5 #ifndef CONTENT_RENDERER_NPAPI_WEBPLUGIN_IMPL_H_
6 #define CONTENT_RENDERER_NPAPI_WEBPLUGIN_IMPL_H_
8 #include <map>
9 #include <string>
10 #include <vector>
12 #include "base/basictypes.h"
13 #include "base/files/file_path.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "content/child/npapi/webplugin.h"
17 #include "content/common/content_export.h"
18 #include "content/common/webplugin_geometry.h"
19 #include "third_party/WebKit/public/platform/WebRect.h"
20 #include "third_party/WebKit/public/platform/WebString.h"
21 #include "third_party/WebKit/public/platform/WebURLLoaderClient.h"
22 #include "third_party/WebKit/public/platform/WebURLRequest.h"
23 #include "third_party/WebKit/public/platform/WebVector.h"
24 #include "third_party/WebKit/public/web/WebPlugin.h"
25 #include "ui/gfx/native_widget_types.h"
26 #include "url/gurl.h"
28 namespace cc {
29 class IOSurfaceLayer;
32 namespace blink {
33 class WebFrame;
34 class WebLayer;
35 class WebPluginContainer;
36 class WebURLResponse;
37 class WebURLLoader;
38 class WebURLRequest;
39 struct WebPluginParams;
42 namespace content {
43 class MultipartResponseDelegate;
44 class RenderFrameImpl;
45 class RenderViewImpl;
46 class WebPluginDelegateProxy;
48 // This is the WebKit side of the plugin implementation that forwards calls,
49 // after changing out of WebCore types, to a delegate. The delegate may
50 // be in a different process.
51 class WebPluginImpl : public WebPlugin,
52 public blink::WebPlugin {
53 public:
54 WebPluginImpl(
55 blink::WebFrame* frame,
56 const blink::WebPluginParams& params,
57 const base::FilePath& file_path,
58 const base::WeakPtr<RenderViewImpl>& render_view,
59 RenderFrameImpl* render_frame);
60 ~WebPluginImpl() override;
62 // Helper function for sorting post data.
63 CONTENT_EXPORT static bool SetPostData(blink::WebURLRequest* request,
64 const char* buf,
65 uint32 length);
67 blink::WebFrame* webframe() { return webframe_; }
69 // blink::WebPlugin methods:
70 virtual bool initialize(
71 blink::WebPluginContainer* container);
72 virtual void destroy();
73 virtual NPObject* scriptableObject();
74 virtual struct _NPP* pluginNPP();
75 virtual bool getFormValue(blink::WebString& value);
76 virtual void layoutIfNeeded() override;
77 virtual void paint(
78 blink::WebCanvas* canvas, const blink::WebRect& paint_rect);
79 virtual void updateGeometry(
80 const blink::WebRect& window_rect,
81 const blink::WebRect& clip_rect,
82 const blink::WebRect& unobscured_rect,
83 const blink::WebVector<blink::WebRect>& cut_outs_rects,
84 bool is_visible);
85 virtual void updateFocus(bool focused, blink::WebFocusType focus_type);
86 virtual void updateVisibility(bool visible);
87 virtual bool acceptsInputEvents();
88 virtual bool handleInputEvent(
89 const blink::WebInputEvent& event, blink::WebCursorInfo& cursor_info);
90 virtual void didReceiveResponse(const blink::WebURLResponse& response);
91 virtual void didReceiveData(const char* data, int data_length);
92 virtual void didFinishLoading();
93 virtual void didFailLoading(const blink::WebURLError& error);
94 virtual void didFinishLoadingFrameRequest(
95 const blink::WebURL& url, void* notify_data);
96 virtual void didFailLoadingFrameRequest(
97 const blink::WebURL& url, void* notify_data,
98 const blink::WebURLError& error);
99 virtual bool isPlaceholder() override;
101 // WebPlugin implementation:
102 void SetWindow(gfx::PluginWindowHandle window) override;
103 void SetAcceptsInputEvents(bool accepts) override;
104 void WillDestroyWindow(gfx::PluginWindowHandle window) override;
105 void CancelResource(unsigned long id) override;
106 void Invalidate() override;
107 void InvalidateRect(const gfx::Rect& rect) override;
108 NPObject* GetWindowScriptNPObject() override;
109 NPObject* GetPluginElement() override;
110 bool FindProxyForUrl(const GURL& url, std::string* proxy_list) override;
111 void SetCookie(const GURL& url,
112 const GURL& first_party_for_cookies,
113 const std::string& cookie) override;
114 std::string GetCookies(const GURL& url,
115 const GURL& first_party_for_cookies) override;
116 void HandleURLRequest(const char* url,
117 const char* method,
118 const char* target,
119 const char* buf,
120 unsigned int len,
121 int notify_id,
122 bool popups_allowed,
123 bool notify_redirects) override;
124 void CancelDocumentLoad() override;
125 void InitiateHTTPRangeRequest(const char* url,
126 const char* range_info,
127 int pending_request_id) override;
128 void DidStartLoading() override;
129 void DidStopLoading() override;
130 bool IsOffTheRecord() override;
131 void SetDeferResourceLoading(unsigned long resource_id, bool defer) override;
132 void URLRedirectResponse(bool allow, int resource_id) override;
133 bool CheckIfRunInsecureContent(const GURL& url) override;
134 #if defined(OS_WIN)
135 void SetWindowlessData(HANDLE pump_messages_event,
136 gfx::NativeViewId dummy_activation_window) override {}
137 void ReparentPluginWindow(HWND window, HWND parent) { }
138 void ReportExecutableMemory(size_t size) { }
139 #endif
140 #if defined(OS_MACOSX)
141 WebPluginAcceleratedSurface* GetAcceleratedSurface(
142 gfx::GpuPreference gpu_preference) override;
143 void AcceleratedPluginEnabledRendering() override;
144 void AcceleratedPluginAllocatedIOSurface(int32 width,
145 int32 height,
146 uint32 surface_id) override;
147 void AcceleratedPluginSwappedIOSurface() override;
148 #endif
150 private:
151 // Given a (maybe partial) url, completes using the base url.
152 GURL CompleteURL(const char* url);
154 enum RoutingStatus {
155 ROUTED,
156 NOT_ROUTED,
157 INVALID_URL,
158 GENERAL_FAILURE
161 // Determines the referrer value sent along with outgoing HTTP requests
162 // issued by plugins.
163 enum ReferrerValue {
164 PLUGIN_SRC,
165 DOCUMENT_URL,
166 NO_REFERRER
169 // Given a download request, check if we need to route the output to a frame.
170 // Returns ROUTED if the load is done and routed to a frame, NOT_ROUTED or
171 // corresponding error codes otherwise.
172 RoutingStatus RouteToFrame(const char* url,
173 bool is_javascript_url,
174 bool popups_allowed,
175 const char* method,
176 const char* target,
177 const char* buf,
178 unsigned int len,
179 int notify_id,
180 ReferrerValue referrer_flag);
182 // Returns the next avaiable resource id. Returns 0 if the operation fails.
183 // It may fail if the page has already been closed.
184 unsigned long GetNextResourceId();
186 // Initiates HTTP GET/POST requests.
187 // Returns true on success.
188 bool InitiateHTTPRequest(unsigned long resource_id,
189 WebPluginResourceClient* client,
190 const GURL& url,
191 const char* method,
192 const char* buf,
193 int len,
194 const char* range_info,
195 ReferrerValue referrer_flag,
196 bool notify_redirects,
197 bool check_mixed_scripting);
199 gfx::Rect GetWindowClipRect(const gfx::Rect& rect);
201 // Sets the actual Widget for the plugin.
202 void SetContainer(blink::WebPluginContainer* container);
204 // Destroys the plugin instance.
205 // The response_handle_to_ignore parameter if not NULL indicates the
206 // resource handle to be left valid during plugin shutdown.
207 void TearDownPluginInstance(blink::WebURLLoader* loader_to_ignore);
209 // WebURLLoaderClient implementation. We implement this interface in the
210 // renderer process, and then use the simple WebPluginResourceClient interface
211 // to relay the callbacks to the plugin.
212 void willSendRequest(blink::WebURLLoader* loader,
213 blink::WebURLRequest& request,
214 const blink::WebURLResponse& response);
215 void didSendData(blink::WebURLLoader* loader,
216 unsigned long long bytes_sent,
217 unsigned long long total_bytes_to_be_sent);
218 void didReceiveResponse(blink::WebURLLoader* loader,
219 const blink::WebURLResponse& response);
221 void didReceiveData(blink::WebURLLoader* loader, const char *buffer,
222 int data_length, int encoded_data_length);
223 void didFinishLoading(blink::WebURLLoader* loader,
224 double finishTime);
225 void didFail(blink::WebURLLoader* loader,
226 const blink::WebURLError& error);
228 // Helper function to remove the stored information about a resource
229 // request given its index in m_clients.
230 void RemoveClient(size_t i);
232 // Helper function to remove the stored information about a resource
233 // request given a handle.
234 void RemoveClient(blink::WebURLLoader* loader);
236 // Handles HTTP multipart responses, i.e. responses received with a HTTP
237 // status code of 206.
238 // Returns false if response is not multipart (may be if we requested
239 // single range).
240 bool HandleHttpMultipartResponse(const blink::WebURLResponse& response,
241 WebPluginResourceClient* client);
243 void HandleURLRequestInternal(const char* url,
244 const char* method,
245 const char* target,
246 const char* buf,
247 unsigned int len,
248 int notify_id,
249 bool popups_allowed,
250 ReferrerValue referrer_flag,
251 bool notify_redirects,
252 bool check_mixed_scripting);
254 // Tears down the existing plugin instance and creates a new plugin instance
255 // to handle the response identified by the loader parameter.
256 bool ReinitializePluginForResponse(blink::WebURLLoader* loader);
258 // Delayed task for downloading the plugin source URL.
259 void OnDownloadPluginSrcUrl();
261 struct ClientInfo;
263 // Helper functions
264 WebPluginResourceClient* GetClientFromLoader(blink::WebURLLoader* loader);
265 ClientInfo* GetClientInfoFromLoader(blink::WebURLLoader* loader);
267 // Helper function to set the referrer on the request passed in.
268 void SetReferrer(blink::WebURLRequest* request, ReferrerValue referrer_flag);
270 // Check for invalid chars like @, ;, \ before the first / (in path).
271 bool IsValidUrl(const GURL& url, ReferrerValue referrer_flag);
273 std::vector<ClientInfo> clients_;
275 bool windowless_;
276 gfx::PluginWindowHandle window_;
277 #if defined(OS_MACOSX)
278 bool next_io_surface_allocated_;
279 int32 next_io_surface_width_;
280 int32 next_io_surface_height_;
281 uint32 next_io_surface_id_;
282 scoped_refptr<cc::IOSurfaceLayer> io_surface_layer_;
283 scoped_ptr<blink::WebLayer> web_layer_;
284 #endif
285 bool accepts_input_events_;
286 RenderFrameImpl* render_frame_;
287 base::WeakPtr<RenderViewImpl> render_view_;
288 blink::WebFrame* webframe_;
290 WebPluginDelegateProxy* delegate_;
292 // This is just a weak reference.
293 blink::WebPluginContainer* container_;
295 // Unique identifier for this plugin, used to track script objects.
296 struct _NPP* npp_;
298 typedef std::map<WebPluginResourceClient*, MultipartResponseDelegate*>
299 MultiPartResponseHandlerMap;
300 // Tracks HTTP multipart response handlers instantiated for
301 // a WebPluginResourceClient instance.
302 MultiPartResponseHandlerMap multi_part_response_map_;
304 // The plugin source URL.
305 GURL plugin_url_;
307 // Indicates if the download would be initiated by the plugin or us.
308 bool load_manually_;
310 // Indicates if this is the first geometry update received by the plugin.
311 bool first_geometry_update_;
313 // Set to true if the next response error should be ignored.
314 bool ignore_response_error_;
316 // The current plugin geometry and clip rectangle.
317 WebPluginGeometry geometry_;
319 // The location of the plugin on disk.
320 base::FilePath file_path_;
322 // The mime type of the plugin.
323 std::string mime_type_;
325 // Holds the list of argument names and values passed to the plugin. We keep
326 // these so that we can re-initialize the plugin if we need to.
327 std::vector<std::string> arg_names_;
328 std::vector<std::string> arg_values_;
330 class LoaderClient : public blink::WebURLLoaderClient {
331 public:
332 LoaderClient(WebPluginImpl*);
334 virtual void willSendRequest(blink::WebURLLoader*,
335 blink::WebURLRequest&,
336 const blink::WebURLResponse&) override;
337 virtual void didSendData(blink::WebURLLoader*,
338 unsigned long long bytesSent,
339 unsigned long long totalBytesToBeSent) override;
340 virtual void didReceiveResponse(blink::WebURLLoader*,
341 const blink::WebURLResponse&) override;
342 virtual void didDownloadData(blink::WebURLLoader*,
343 int dataLength,
344 int encodedDataLength) override;
345 virtual void didReceiveData(blink::WebURLLoader*,
346 const char* data,
347 int dataLength,
348 int encodedDataLength) override;
349 virtual void didReceiveCachedMetadata(blink::WebURLLoader*,
350 const char* data,
351 int dataLength) override;
352 virtual void didFinishLoading(blink::WebURLLoader*,
353 double finishTime,
354 int64_t total_encoded_data_length) override;
355 virtual void didFail(blink::WebURLLoader*,
356 const blink::WebURLError&) override;
358 private:
359 WebPluginImpl* parent_;
362 LoaderClient loader_client_;
364 base::WeakPtrFactory<WebPluginImpl> weak_factory_;
366 DISALLOW_COPY_AND_ASSIGN(WebPluginImpl);
369 } // namespace content
371 #endif // CONTENT_RENDERER_NPAPI_WEBPLUGIN_IMPL_H_