Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / renderer / npapi / webplugin_impl.h
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 CONTENT_RENDERER_NPAPI_WEBPLUGIN_IMPL_H_
6 #define CONTENT_RENDERER_NPAPI_WEBPLUGIN_IMPL_H_
7
8 #include <map>
9 #include <string>
10 #include <vector>
11
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"
27
28 namespace cc {
29 class IOSurfaceLayer;
30 }
31
32 namespace blink {
33 class WebFrame;
34 class WebLayer;
35 class WebPluginContainer;
36 class WebURLResponse;
37 class WebURLLoader;
38 class WebURLRequest;
39 }
40
41 namespace content {
42 class MultipartResponseDelegate;
43 class RenderFrameImpl;
44 class RenderViewImpl;
45 class WebPluginDelegateProxy;
46
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 {
52  public:
53   WebPluginImpl(
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;
60
61   // Helper function for sorting post data.
62   CONTENT_EXPORT static bool SetPostData(blink::WebURLRequest* request,
63                                          const char* buf,
64                                          uint32 length);
65
66   blink::WebFrame* webframe() { return webframe_; }
67
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);
75   virtual void paint(
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;
95
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,
112                         const char* method,
113                         const char* target,
114                         const char* buf,
115                         unsigned int len,
116                         int notify_id,
117                         bool popups_allowed,
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;
129 #if defined(OS_WIN)
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) { }
134 #endif
135 #if defined(OS_MACOSX)
136   WebPluginAcceleratedSurface* GetAcceleratedSurface(
137       gfx::GpuPreference gpu_preference) override;
138   void AcceleratedPluginEnabledRendering() override;
139   void AcceleratedPluginAllocatedIOSurface(int32 width,
140                                            int32 height,
141                                            uint32 surface_id) override;
142   void AcceleratedPluginSwappedIOSurface() override;
143 #endif
144
145  private:
146   // Given a (maybe partial) url, completes using the base url.
147   GURL CompleteURL(const char* url);
148
149   enum RoutingStatus {
150     ROUTED,
151     NOT_ROUTED,
152     INVALID_URL,
153     GENERAL_FAILURE
154   };
155
156   // Determines the referrer value sent along with outgoing HTTP requests
157   // issued by plugins.
158   enum Referrer {
159     PLUGIN_SRC,
160     DOCUMENT_URL,
161     NO_REFERRER
162   };
163
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,
169                              bool popups_allowed,
170                              const char* method,
171                              const char* target,
172                              const char* buf,
173                              unsigned int len,
174                              int notify_id,
175                              Referrer referrer_flag);
176
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();
180
181   // Initiates HTTP GET/POST requests.
182   // Returns true on success.
183   bool InitiateHTTPRequest(unsigned long resource_id,
184                            WebPluginResourceClient* client,
185                            const GURL& url,
186                            const char* method,
187                            const char* buf,
188                            int len,
189                            const char* range_info,
190                            Referrer referrer_flag,
191                            bool notify_redirects,
192                            bool check_mixed_scripting);
193
194   gfx::Rect GetWindowClipRect(const gfx::Rect& rect);
195
196   // Sets the actual Widget for the plugin.
197   void SetContainer(blink::WebPluginContainer* container);
198
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);
203
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);
215
216   void didReceiveData(blink::WebURLLoader* loader, const char *buffer,
217                       int data_length, int encoded_data_length);
218   void didFinishLoading(blink::WebURLLoader* loader,
219                         double finishTime);
220   void didFail(blink::WebURLLoader* loader,
221                const blink::WebURLError& error);
222
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);
226
227   // Helper function to remove the stored information about a resource
228   // request given a handle.
229   void RemoveClient(blink::WebURLLoader* loader);
230
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
234   // single range).
235   bool HandleHttpMultipartResponse(const blink::WebURLResponse& response,
236                                    WebPluginResourceClient* client);
237
238   void HandleURLRequestInternal(const char* url,
239                                 const char* method,
240                                 const char* target,
241                                 const char* buf,
242                                 unsigned int len,
243                                 int notify_id,
244                                 bool popups_allowed,
245                                 Referrer referrer_flag,
246                                 bool notify_redirects,
247                                 bool check_mixed_scripting);
248
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);
252
253   // Delayed task for downloading the plugin source URL.
254   void OnDownloadPluginSrcUrl();
255
256   struct ClientInfo;
257
258   // Helper functions
259   WebPluginResourceClient* GetClientFromLoader(blink::WebURLLoader* loader);
260   ClientInfo* GetClientInfoFromLoader(blink::WebURLLoader* loader);
261
262   // Helper function to set the referrer on the request passed in.
263   void SetReferrer(blink::WebURLRequest* request, Referrer referrer_flag);
264
265   // Check for invalid chars like @, ;, \ before the first / (in path).
266   bool IsValidUrl(const GURL& url, Referrer referrer_flag);
267
268   std::vector<ClientInfo> clients_;
269
270   bool windowless_;
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_;
279 #endif
280   bool accepts_input_events_;
281   RenderFrameImpl* render_frame_;
282   base::WeakPtr<RenderViewImpl> render_view_;
283   blink::WebFrame* webframe_;
284
285   WebPluginDelegateProxy* delegate_;
286
287   // This is just a weak reference.
288   blink::WebPluginContainer* container_;
289
290   // Unique identifier for this plugin, used to track script objects.
291   struct _NPP* npp_;
292
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_;
298
299   // The plugin source URL.
300   GURL plugin_url_;
301
302   // Indicates if the download would be initiated by the plugin or us.
303   bool load_manually_;
304
305   // Indicates if this is the first geometry update received by the plugin.
306   bool first_geometry_update_;
307
308   // Set to true if the next response error should be ignored.
309   bool ignore_response_error_;
310
311   // The current plugin geometry and clip rectangle.
312   WebPluginGeometry geometry_;
313
314   // The location of the plugin on disk.
315   base::FilePath file_path_;
316
317   // The mime type of the plugin.
318   std::string mime_type_;
319
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_;
324
325   class LoaderClient : public blink::WebURLLoaderClient {
326    public:
327     LoaderClient(WebPluginImpl*);
328
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*,
338                                  int dataLength,
339                                  int encodedDataLength) override;
340     virtual void didReceiveData(blink::WebURLLoader*,
341                                 const char* data,
342                                 int dataLength,
343                                 int encodedDataLength) override;
344     virtual void didReceiveCachedMetadata(blink::WebURLLoader*,
345                                           const char* data,
346                                           int dataLength) override;
347     virtual void didFinishLoading(blink::WebURLLoader*,
348                                   double finishTime,
349                                   int64_t total_encoded_data_length) override;
350     virtual void didFail(blink::WebURLLoader*,
351                          const blink::WebURLError&) override;
352
353    private:
354     WebPluginImpl* parent_;
355   };
356
357   LoaderClient loader_client_;
358
359   base::WeakPtrFactory<WebPluginImpl> weak_factory_;
360
361   DISALLOW_COPY_AND_ASSIGN(WebPluginImpl);
362 };
363
364 }  // namespace content
365
366 #endif  // CONTENT_RENDERER_NPAPI_WEBPLUGIN_IMPL_H_