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_
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"
35 class WebPluginContainer;
42 class MultipartResponseDelegate;
43 class RenderFrameImpl;
45 class WebPluginDelegateProxy;
47 // This is the WebKit side of the plugin implementation that forwards calls,
48 // after changing out of WebCore types, to a delegate. The delegate may
49 // be in a different process.
50 class WebPluginImpl : public WebPlugin,
51 public blink::WebPlugin {
54 blink::WebFrame* frame,
55 const blink::WebPluginParams& params,
56 const base::FilePath& file_path,
57 const base::WeakPtr<RenderViewImpl>& render_view,
58 RenderFrameImpl* render_frame);
59 ~WebPluginImpl() override;
61 // Helper function for sorting post data.
62 CONTENT_EXPORT static bool SetPostData(blink::WebURLRequest* request,
66 blink::WebFrame* webframe() { return webframe_; }
68 // blink::WebPlugin methods:
69 virtual bool initialize(
70 blink::WebPluginContainer* container);
71 virtual void destroy();
72 virtual NPObject* scriptableObject();
73 virtual struct _NPP* pluginNPP();
74 virtual bool getFormValue(blink::WebString& value);
76 blink::WebCanvas* canvas, const blink::WebRect& paint_rect);
77 virtual void updateGeometry(
78 const blink::WebRect& frame_rect, const blink::WebRect& clip_rect,
79 const blink::WebVector<blink::WebRect>& cut_outs, bool is_visible);
80 virtual void updateFocus(bool focused);
81 virtual void updateVisibility(bool visible);
82 virtual bool acceptsInputEvents();
83 virtual bool handleInputEvent(
84 const blink::WebInputEvent& event, blink::WebCursorInfo& cursor_info);
85 virtual void didReceiveResponse(const blink::WebURLResponse& response);
86 virtual void didReceiveData(const char* data, int data_length);
87 virtual void didFinishLoading();
88 virtual void didFailLoading(const blink::WebURLError& error);
89 virtual void didFinishLoadingFrameRequest(
90 const blink::WebURL& url, void* notify_data);
91 virtual void didFailLoadingFrameRequest(
92 const blink::WebURL& url, void* notify_data,
93 const blink::WebURLError& error);
94 virtual bool isPlaceholder() override;
96 // WebPlugin implementation:
97 void SetWindow(gfx::PluginWindowHandle window) override;
98 void SetAcceptsInputEvents(bool accepts) override;
99 void WillDestroyWindow(gfx::PluginWindowHandle window) override;
100 void CancelResource(unsigned long id) override;
101 void Invalidate() override;
102 void InvalidateRect(const gfx::Rect& rect) override;
103 NPObject* GetWindowScriptNPObject() override;
104 NPObject* GetPluginElement() override;
105 bool FindProxyForUrl(const GURL& url, std::string* proxy_list) override;
106 void SetCookie(const GURL& url,
107 const GURL& first_party_for_cookies,
108 const std::string& cookie) override;
109 std::string GetCookies(const GURL& url,
110 const GURL& first_party_for_cookies) override;
111 void HandleURLRequest(const char* url,
118 bool notify_redirects) override;
119 void CancelDocumentLoad() override;
120 void InitiateHTTPRangeRequest(const char* url,
121 const char* range_info,
122 int pending_request_id) override;
123 void DidStartLoading() override;
124 void DidStopLoading() override;
125 bool IsOffTheRecord() override;
126 void SetDeferResourceLoading(unsigned long resource_id, bool defer) override;
127 void URLRedirectResponse(bool allow, int resource_id) override;
128 bool CheckIfRunInsecureContent(const GURL& url) override;
130 void SetWindowlessData(HANDLE pump_messages_event,
131 gfx::NativeViewId dummy_activation_window) { }
132 void ReparentPluginWindow(HWND window, HWND parent) { }
133 void ReportExecutableMemory(size_t size) { }
135 #if defined(OS_MACOSX)
136 WebPluginAcceleratedSurface* GetAcceleratedSurface(
137 gfx::GpuPreference gpu_preference) override;
138 void AcceleratedPluginEnabledRendering() override;
139 void AcceleratedPluginAllocatedIOSurface(int32 width,
141 uint32 surface_id) override;
142 void AcceleratedPluginSwappedIOSurface() override;
146 // Given a (maybe partial) url, completes using the base url.
147 GURL CompleteURL(const char* url);
156 // Determines the referrer value sent along with outgoing HTTP requests
157 // issued by plugins.
164 // Given a download request, check if we need to route the output to a frame.
165 // Returns ROUTED if the load is done and routed to a frame, NOT_ROUTED or
166 // corresponding error codes otherwise.
167 RoutingStatus RouteToFrame(const char* url,
168 bool is_javascript_url,
175 Referrer referrer_flag);
177 // Returns the next avaiable resource id. Returns 0 if the operation fails.
178 // It may fail if the page has already been closed.
179 unsigned long GetNextResourceId();
181 // Initiates HTTP GET/POST requests.
182 // Returns true on success.
183 bool InitiateHTTPRequest(unsigned long resource_id,
184 WebPluginResourceClient* client,
189 const char* range_info,
190 Referrer referrer_flag,
191 bool notify_redirects,
192 bool check_mixed_scripting);
194 gfx::Rect GetWindowClipRect(const gfx::Rect& rect);
196 // Sets the actual Widget for the plugin.
197 void SetContainer(blink::WebPluginContainer* container);
199 // Destroys the plugin instance.
200 // The response_handle_to_ignore parameter if not NULL indicates the
201 // resource handle to be left valid during plugin shutdown.
202 void TearDownPluginInstance(blink::WebURLLoader* loader_to_ignore);
204 // WebURLLoaderClient implementation. We implement this interface in the
205 // renderer process, and then use the simple WebPluginResourceClient interface
206 // to relay the callbacks to the plugin.
207 void willSendRequest(blink::WebURLLoader* loader,
208 blink::WebURLRequest& request,
209 const blink::WebURLResponse& response);
210 void didSendData(blink::WebURLLoader* loader,
211 unsigned long long bytes_sent,
212 unsigned long long total_bytes_to_be_sent);
213 void didReceiveResponse(blink::WebURLLoader* loader,
214 const blink::WebURLResponse& response);
216 void didReceiveData(blink::WebURLLoader* loader, const char *buffer,
217 int data_length, int encoded_data_length);
218 void didFinishLoading(blink::WebURLLoader* loader,
220 void didFail(blink::WebURLLoader* loader,
221 const blink::WebURLError& error);
223 // Helper function to remove the stored information about a resource
224 // request given its index in m_clients.
225 void RemoveClient(size_t i);
227 // Helper function to remove the stored information about a resource
228 // request given a handle.
229 void RemoveClient(blink::WebURLLoader* loader);
231 // Handles HTTP multipart responses, i.e. responses received with a HTTP
232 // status code of 206.
233 // Returns false if response is not multipart (may be if we requested
235 bool HandleHttpMultipartResponse(const blink::WebURLResponse& response,
236 WebPluginResourceClient* client);
238 void HandleURLRequestInternal(const char* url,
245 Referrer referrer_flag,
246 bool notify_redirects,
247 bool check_mixed_scripting);
249 // Tears down the existing plugin instance and creates a new plugin instance
250 // to handle the response identified by the loader parameter.
251 bool ReinitializePluginForResponse(blink::WebURLLoader* loader);
253 // Delayed task for downloading the plugin source URL.
254 void OnDownloadPluginSrcUrl();
259 WebPluginResourceClient* GetClientFromLoader(blink::WebURLLoader* loader);
260 ClientInfo* GetClientInfoFromLoader(blink::WebURLLoader* loader);
262 // Helper function to set the referrer on the request passed in.
263 void SetReferrer(blink::WebURLRequest* request, Referrer referrer_flag);
265 // Check for invalid chars like @, ;, \ before the first / (in path).
266 bool IsValidUrl(const GURL& url, Referrer referrer_flag);
268 std::vector<ClientInfo> clients_;
271 gfx::PluginWindowHandle window_;
272 #if defined(OS_MACOSX)
273 bool next_io_surface_allocated_;
274 int32 next_io_surface_width_;
275 int32 next_io_surface_height_;
276 uint32 next_io_surface_id_;
277 scoped_refptr<cc::IOSurfaceLayer> io_surface_layer_;
278 scoped_ptr<blink::WebLayer> web_layer_;
280 bool accepts_input_events_;
281 RenderFrameImpl* render_frame_;
282 base::WeakPtr<RenderViewImpl> render_view_;
283 blink::WebFrame* webframe_;
285 WebPluginDelegateProxy* delegate_;
287 // This is just a weak reference.
288 blink::WebPluginContainer* container_;
290 // Unique identifier for this plugin, used to track script objects.
293 typedef std::map<WebPluginResourceClient*, MultipartResponseDelegate*>
294 MultiPartResponseHandlerMap;
295 // Tracks HTTP multipart response handlers instantiated for
296 // a WebPluginResourceClient instance.
297 MultiPartResponseHandlerMap multi_part_response_map_;
299 // The plugin source URL.
302 // Indicates if the download would be initiated by the plugin or us.
305 // Indicates if this is the first geometry update received by the plugin.
306 bool first_geometry_update_;
308 // Set to true if the next response error should be ignored.
309 bool ignore_response_error_;
311 // The current plugin geometry and clip rectangle.
312 WebPluginGeometry geometry_;
314 // The location of the plugin on disk.
315 base::FilePath file_path_;
317 // The mime type of the plugin.
318 std::string mime_type_;
320 // Holds the list of argument names and values passed to the plugin. We keep
321 // these so that we can re-initialize the plugin if we need to.
322 std::vector<std::string> arg_names_;
323 std::vector<std::string> arg_values_;
325 class LoaderClient : public blink::WebURLLoaderClient {
327 LoaderClient(WebPluginImpl*);
329 virtual void willSendRequest(blink::WebURLLoader*,
330 blink::WebURLRequest&,
331 const blink::WebURLResponse&) override;
332 virtual void didSendData(blink::WebURLLoader*,
333 unsigned long long bytesSent,
334 unsigned long long totalBytesToBeSent) override;
335 virtual void didReceiveResponse(blink::WebURLLoader*,
336 const blink::WebURLResponse&) override;
337 virtual void didDownloadData(blink::WebURLLoader*,
339 int encodedDataLength) override;
340 virtual void didReceiveData(blink::WebURLLoader*,
343 int encodedDataLength) override;
344 virtual void didReceiveCachedMetadata(blink::WebURLLoader*,
346 int dataLength) override;
347 virtual void didFinishLoading(blink::WebURLLoader*,
349 int64_t total_encoded_data_length) override;
350 virtual void didFail(blink::WebURLLoader*,
351 const blink::WebURLError&) override;
354 WebPluginImpl* parent_;
357 LoaderClient loader_client_;
359 base::WeakPtrFactory<WebPluginImpl> weak_factory_;
361 DISALLOW_COPY_AND_ASSIGN(WebPluginImpl);
364 } // namespace content
366 #endif // CONTENT_RENDERER_NPAPI_WEBPLUGIN_IMPL_H_