tizen beta release
[framework/web/webkit-efl.git] / Source / WebCore / plugins / PluginView.h
1 /*
2  * Copyright (C) 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
3  * Copyright (C) 2008 Collabora Ltd. All rights reserved.
4  * Copyright (C) 2009 Girish Ramakrishnan <girish@forwardbias.in>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
16  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
26  */
27
28 #ifndef PluginView_h
29 #define PluginView_h
30
31 #include "FrameLoadRequest.h"
32 #include "IntRect.h"
33 #include "MediaCanStartListener.h"
34 #include "PluginViewBase.h"
35 #include "ResourceRequest.h"
36 #include "Timer.h"
37 #include <wtf/HashMap.h>
38 #include <wtf/HashSet.h>
39 #include <wtf/OwnPtr.h>
40 #include <wtf/PassRefPtr.h>
41 #include <wtf/RefPtr.h>
42 #include <wtf/Vector.h>
43 #include <wtf/text/CString.h>
44 #if ENABLE(TOUCH_EVENTS) && PLATFORM(EFL) && ENABLE(TIZEN_PLUGIN_SNPEVENT)
45 #include "Touch.h"
46 #include "TouchEvent.h"
47 #include "TouchList.h"
48 #endif
49 #if ENABLE(NETSCAPE_PLUGIN_API)
50 #include "PluginStream.h"
51 #include "npruntime_internal.h"
52 #endif
53
54 #if OS(WINDOWS) && (PLATFORM(QT) || PLATFORM(WX))
55 typedef struct HWND__* HWND;
56 typedef HWND PlatformPluginWidget;
57 #elif ENABLE(TIZEN_SUPPORT_PLUGINS)
58 class X11EmbedContainer;
59 typedef X11EmbedContainer* PlatformPluginWidget;
60 #else
61 typedef PlatformWidget PlatformPluginWidget;
62 #if defined(XP_MACOSX) && PLATFORM(QT)
63 #include <QPixmap>
64 #endif
65 #endif
66 #if PLATFORM(QT)
67 #if USE(TEXTURE_MAPPER)
68 #include "TextureMapperPlatformLayer.h"
69 #endif
70
71 #include <QGraphicsItem>
72 #include <QImage>
73 QT_BEGIN_NAMESPACE
74 class QPainter;
75 QT_END_NAMESPACE
76 #endif
77 #if PLATFORM(QT) && USE(ACCELERATED_COMPOSITING) && ENABLE(NETSCAPE_PLUGIN_API) && defined(XP_UNIX)
78 #ifndef WTF_USE_ACCELERATED_COMPOSITING_PLUGIN_LAYER
79 #define WTF_USE_ACCELERATED_COMPOSITING_PLUGIN_LAYER 1
80 #endif
81 #endif
82 #if PLATFORM(GTK)
83 typedef struct _GtkSocket GtkSocket;
84 #endif
85
86 #if ENABLE(TIZEN_ACCELERATED_COMPOSITING_PLUGIN_LAYER_EFL)
87 #include "EflLayer.h"
88 #include "ImageBuffer.h"
89 #endif
90
91 #if USE(JSC)
92 //mariusz.g@samsung.com
93 #if ENABLE(TIZEN_EVAS_OBJECT_PLUGIN) && PLATFORM(EFL)
94 #include <Evas.h>
95 #endif
96
97 namespace JSC {
98     namespace Bindings {
99         class Instance;
100     }
101 }
102 #endif
103
104 #if ENABLE(TIZEN_EVAS_OBJECT_PLUGIN) && PLATFORM(EFL)
105 typedef struct _PluginStorage PluginStorage;
106 struct _PluginStorage {
107     Evas_Object *pluginCanvas;
108     Evas_Object *pluginClipper;
109     Evas_Object *parentWindow;
110     Evas_Object *webview;
111 };
112 #endif
113
114 namespace WebCore {
115     class Element;
116     class Frame;
117     class Image;
118     class KeyboardEvent;
119     class MouseEvent;
120     class KURL;
121 #if OS(WINDOWS) && ENABLE(NETSCAPE_PLUGIN_API)
122     class PluginMessageThrottlerWin;
123 #endif
124     class PluginPackage;
125     class PluginRequest;
126     class PluginStream;
127     class ResourceError;
128     class ResourceResponse;
129
130     enum PluginStatus {
131         PluginStatusCanNotFindPlugin,
132         PluginStatusCanNotLoadPlugin,
133         PluginStatusLoadedSuccessfully
134     };
135
136     class PluginRequest {
137         WTF_MAKE_NONCOPYABLE(PluginRequest); WTF_MAKE_FAST_ALLOCATED;
138     public:
139         PluginRequest(const FrameLoadRequest& frameLoadRequest, bool sendNotification, void* notifyData, bool shouldAllowPopups)
140             : m_frameLoadRequest(frameLoadRequest)
141             , m_notifyData(notifyData)
142             , m_sendNotification(sendNotification)
143             , m_shouldAllowPopups(shouldAllowPopups) { }
144     public:
145         const FrameLoadRequest& frameLoadRequest() const { return m_frameLoadRequest; }
146         void* notifyData() const { return m_notifyData; }
147         bool sendNotification() const { return m_sendNotification; }
148         bool shouldAllowPopups() const { return m_shouldAllowPopups; }
149     private:
150         FrameLoadRequest m_frameLoadRequest;
151         void* m_notifyData;
152         bool m_sendNotification;
153         bool m_shouldAllowPopups;
154     };
155
156     class PluginManualLoader {
157     public:
158         virtual ~PluginManualLoader() {}
159         virtual void didReceiveResponse(const ResourceResponse&) = 0;
160         virtual void didReceiveData(const char*, int) = 0;
161         virtual void didFinishLoading() = 0;
162         virtual void didFail(const ResourceError&) = 0;
163     };
164
165     class PluginView : public PluginViewBase
166 #if ENABLE(NETSCAPE_PLUGIN_API)
167                      , private PluginStreamClient
168 #endif
169                      , public PluginManualLoader
170                      , private MediaCanStartListener {
171     public:
172         static PassRefPtr<PluginView> create(Frame* parentFrame, const IntSize&, Element*, const KURL&, const Vector<String>& paramNames, const Vector<String>& paramValues, const String& mimeType, bool loadManually);
173         virtual ~PluginView();
174
175         PluginPackage* plugin() const { return m_plugin.get(); }
176 #if ENABLE(NETSCAPE_PLUGIN_API)
177         NPP instance() const { return m_instance; }
178 #endif
179
180         void setNPWindowRect(const IntRect&);
181         static PluginView* currentPluginView();
182
183 #if ENABLE(NETSCAPE_PLUGIN_API)
184         NPObject* npObject();
185 #endif
186 #if USE(JSC)
187         PassRefPtr<JSC::Bindings::Instance> bindingInstance();
188 #endif
189
190         PluginStatus status() const { return m_status; }
191
192 #if ENABLE(NETSCAPE_PLUGIN_API)
193         // NPN functions
194         NPError getURLNotify(const char* url, const char* target, void* notifyData);
195         NPError getURL(const char* url, const char* target);
196         NPError postURLNotify(const char* url, const char* target, uint32_t len, const char* but, NPBool file, void* notifyData);
197         NPError postURL(const char* url, const char* target, uint32_t len, const char* but, NPBool file);
198         NPError newStream(NPMIMEType type, const char* target, NPStream** stream);
199         int32_t write(NPStream* stream, int32_t len, void* buffer);
200         NPError destroyStream(NPStream* stream, NPReason reason);
201 #endif
202         const char* userAgent();
203 #if ENABLE(NETSCAPE_PLUGIN_API)
204         static const char* userAgentStatic();
205 #endif
206         void status(const char* message);
207         
208 #if ENABLE(NETSCAPE_PLUGIN_API)
209         NPError getValue(NPNVariable variable, void* value);
210         static NPError getValueStatic(NPNVariable variable, void* value);
211         NPError setValue(NPPVariable variable, void* value);
212         NPError getValueForURL(NPNURLVariable variable, const char* url, char** value, uint32_t* len);
213         NPError setValueForURL(NPNURLVariable variable, const char* url, const char* value, uint32_t len);
214         NPError getAuthenticationInfo(const char* protocol, const char* host, int32_t port, const char* scheme, const char* realm, char** username, uint32_t* ulen, char** password, uint32_t* plen);
215         void invalidateRect(NPRect*);
216         void invalidateRegion(NPRegion);
217 #endif
218         void forceRedraw();
219         void pushPopupsEnabledState(bool state);
220         void popPopupsEnabledState();
221
222         virtual void invalidateRect(const IntRect&);
223
224         bool arePopupsAllowed() const;
225
226         void setJavaScriptPaused(bool);
227
228         void privateBrowsingStateChanged(bool);
229
230         void disconnectStream(PluginStream*);
231         void streamDidFinishLoading(PluginStream* stream) { disconnectStream(stream); }
232
233         // Widget functions
234         virtual void setFrameRect(const IntRect&);
235         virtual void frameRectsChanged();
236         virtual void setFocus(bool);
237         virtual void show();
238         virtual void hide();
239         virtual void paint(GraphicsContext*, const IntRect&);
240
241         // This method is used by plugins on all platforms to obtain a clip rect that includes clips set by WebCore,
242         // e.g., in overflow:auto sections.  The clip rects coordinates are in the containing window's coordinate space.
243         // This clip includes any clips that the widget itself sets up for its children.
244         IntRect windowClipRect() const;
245
246         virtual void handleEvent(Event*);
247         virtual void setParent(ScrollView*);
248         virtual void setParentVisible(bool);
249
250         virtual bool isPluginView() const { return true; }
251
252         Frame* parentFrame() const { return m_parentFrame.get(); }
253
254         void focusPluginElement();
255
256         const String& pluginsPage() const { return m_pluginsPage; }
257         const String& mimeType() const { return m_mimeType; }
258         const KURL& url() const { return m_url; }
259
260 #if OS(WINDOWS) && ENABLE(NETSCAPE_PLUGIN_API)
261         static LRESULT CALLBACK PluginViewWndProc(HWND, UINT, WPARAM, LPARAM);
262         LRESULT wndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
263         WNDPROC pluginWndProc() const { return m_pluginWndProc; }
264 #endif
265
266         // Used for manual loading
267         void didReceiveResponse(const ResourceResponse&);
268         void didReceiveData(const char*, int);
269         void didFinishLoading();
270         void didFail(const ResourceError&);
271
272         static bool isCallingPlugin();
273
274         bool start();
275
276 #if ENABLE(NETSCAPE_PLUGIN_API)
277         static void keepAlive(NPP);
278 #endif
279         void keepAlive();
280
281 #if USE(ACCELERATED_COMPOSITING)
282 #if USE(ACCELERATED_COMPOSITING_PLUGIN_LAYER)
283         virtual PlatformLayer* platformLayer() const;
284         bool shouldUseAcceleratedCompositing() const;
285 #elif ENABLE(TIZEN_ACCELERATED_COMPOSITING_PLUGIN_LAYER_EFL)
286         virtual PlatformLayer* platformLayer() const;
287         ImageBuffer* buffer() const;
288 #else
289         virtual PlatformLayer* platformLayer() const { return 0; }
290 #endif
291 #endif
292
293 #if PLATFORM(EFL)
294 #if ENABLE(NETSCAPE_PLUGIN_API)
295         bool dispatchWheelEvent(NPEvent*);
296 #endif
297         bool isPluginStarted() const { return m_isStarted; }
298 #if ENABLE(TIZEN_SUPPORT_PLUGINS) && ENABLE(TIZEN_DONT_PAN_OVER_SOME_PLUGINS)
299         static bool isPluginHandlingEvents() { return s_isHandlingEvents; }
300         static void setPluginHandlingEvents(bool isHandling);
301         static PluginView* currentPluginHandlingEvents() { return s_currentPluginHandlingEvents; }
302 #endif // ENABLE(TIZEN_SUPPORT_PLUGINS) && ENABLE(TIZEN_DONT_PAN_OVER_SOME_PLUGINS)
303 #if ENABLE(TIZEN_EVAS_OBJECT_PLUGIN)
304         Evas_Object* getPluginCanvas() { return m_pluginEvasObject; }
305         Evas_Object* getPluginClipper() { return m_pluginEvasObjectClipper; }
306 #endif
307 #endif // PLATFORM(EFL)
308
309     private:
310         PluginView(Frame* parentFrame, const IntSize&, PluginPackage*, Element*, const KURL&, const Vector<String>& paramNames, const Vector<String>& paramValues, const String& mimeType, bool loadManually);
311
312         void setParameters(const Vector<String>& paramNames, const Vector<String>& paramValues);
313         bool startOrAddToUnstartedList();
314         void init();
315         bool platformStart();
316         void stop();
317         void platformDestroy();
318         static void setCurrentPluginView(PluginView*);
319 #if ENABLE(NETSCAPE_PLUGIN_API)
320         NPError load(const FrameLoadRequest&, bool sendNotification, void* notifyData);
321         NPError handlePost(const char* url, const char* target, uint32_t len, const char* buf, bool file, void* notifyData, bool sendNotification, bool allowHeaders);
322         NPError handlePostReadFile(Vector<char>& buffer, uint32_t len, const char* buf);
323 #endif
324         static void freeStringArray(char** stringArray, int length);
325         void setCallingPlugin(bool) const;
326
327         void invalidateWindowlessPluginRect(const IntRect&);
328
329         virtual void mediaCanStart();
330
331 #if OS(WINDOWS) && ENABLE(NETSCAPE_PLUGIN_API)
332         void paintWindowedPluginIntoContext(GraphicsContext*, const IntRect&);
333         static HDC WINAPI hookedBeginPaint(HWND, PAINTSTRUCT*);
334         static BOOL WINAPI hookedEndPaint(HWND, const PAINTSTRUCT*);
335 #endif
336
337 #if ENABLE(NETSCAPE_PLUGIN_API)
338         static bool platformGetValueStatic(NPNVariable variable, void* value, NPError* result);
339         bool platformGetValue(NPNVariable variable, void* value, NPError* result);
340 #endif
341
342         RefPtr<Frame> m_parentFrame;
343         RefPtr<PluginPackage> m_plugin;
344         Element* m_element;
345         bool m_isStarted;
346         KURL m_url;
347         KURL m_baseURL;
348         PluginStatus m_status;
349         Vector<IntRect> m_invalidRects;
350
351         void performRequest(PluginRequest*);
352         void scheduleRequest(PluginRequest*);
353         void requestTimerFired(Timer<PluginView>*);
354         void invalidateTimerFired(Timer<PluginView>*);
355         Timer<PluginView> m_requestTimer;
356         Timer<PluginView> m_invalidateTimer;
357
358         void popPopupsStateTimerFired(Timer<PluginView>*);
359         Timer<PluginView> m_popPopupsStateTimer;
360
361         void lifeSupportTimerFired(Timer<PluginView>*);
362         Timer<PluginView> m_lifeSupportTimer;
363
364 #ifndef NP_NO_CARBON
365 #if ENABLE(NETSCAPE_PLUGIN_API)
366 #if !ENABLE(TIZEN_EVAS_OBJECT_PLUGIN)
367         bool dispatchNPEvent(NPEvent&);
368 #else
369         public: bool dispatchNPEvent(NPEvent&);
370         private:
371 #endif // !ENABLE(TIZEN_EVAS_OBJECT_PLUGIN)
372 #endif // ENABLE(NETSCAPE_PLUGIN_API)
373 #endif
374         void updatePluginWidget();
375         void paintMissingPluginIcon(GraphicsContext*, const IntRect&);
376
377         void handleKeyboardEvent(KeyboardEvent*);
378         void handleMouseEvent(MouseEvent*);
379 #if ENABLE(TOUCH_EVENTS) && PLATFORM(EFL) && ENABLE(TIZEN_PLUGIN_SNPEVENT)
380         void handleTouchEvent(TouchEvent*);
381 #endif
382 #if defined(XP_UNIX) && ENABLE(NETSCAPE_PLUGIN_API)
383         void handleFocusInEvent();
384         void handleFocusOutEvent();
385 #endif
386
387 #if OS(WINDOWS)
388         void paintIntoTransformedContext(HDC);
389         PassRefPtr<Image> snapshot();
390 #endif
391
392         int m_mode;
393         int m_paramCount;
394         char** m_paramNames;
395         char** m_paramValues;
396         String m_pluginsPage;
397
398         String m_mimeType;
399         WTF::CString m_userAgent;
400
401 #if ENABLE(NETSCAPE_PLUGIN_API)
402         NPP m_instance;
403         NPP_t m_instanceStruct;
404         NPWindow m_npWindow;
405 #endif
406
407         Vector<bool, 4> m_popupStateStack;
408
409         HashSet<RefPtr<PluginStream> > m_streams;
410         Vector<PluginRequest*> m_requests;
411
412         bool m_isWindowed;
413         bool m_isTransparent;
414         bool m_haveInitialized;
415         bool m_isWaitingToStart;
416
417 #if defined(XP_UNIX)
418         bool m_needsXEmbed;
419 #endif
420
421 //mariusz.g@samsung.com
422 #if ENABLE(TIZEN_EVAS_OBJECT_PLUGIN) && PLATFORM(EFL)
423         Evas_Object *m_pluginEvasObject;
424         Evas_Object *m_pluginEvasObjectClipper;
425         Evas_Object *m_pluginParentWindow;
426         Evas_Object *m_pluginWebview;
427 #endif
428
429 #if OS(WINDOWS) && ENABLE(NETSCAPE_PLUGIN_API)
430         OwnPtr<PluginMessageThrottlerWin> m_messageThrottler;
431         WNDPROC m_pluginWndProc;
432         unsigned m_lastMessage;
433         bool m_isCallingPluginWndProc;
434         HDC m_wmPrintHDC;
435         bool m_haveUpdatedPluginWidget;
436 #endif
437
438 #if ENABLE(TIZEN_SUPPORT_PLUGINS) || ((PLATFORM(QT) || PLATFORM(WX)) && OS(WINDOWS)) || defined(XP_MACOSX)
439         // On Mac OSX and Qt/Windows the plugin does not have its own native widget,
440         // but is using the containing window as its reference for positioning/painting.
441         PlatformPluginWidget m_window;
442 public:
443         PlatformPluginWidget platformPluginWidget() const { return m_window; }
444         void setPlatformPluginWidget(PlatformPluginWidget widget) { m_window = widget; }
445 #else
446 public:
447         void setPlatformPluginWidget(PlatformPluginWidget widget) { setPlatformWidget(widget); }
448         PlatformPluginWidget platformPluginWidget() const { return platformWidget(); }
449 #endif
450
451 private:
452
453 #if defined(XP_UNIX) || PLATFORM(GTK)
454         void setNPWindowIfNeeded();
455 #elif defined(XP_MACOSX)
456         NP_CGContext m_npCgContext;
457         OwnPtr<Timer<PluginView> > m_nullEventTimer;
458         NPDrawingModel m_drawingModel;
459         NPEventModel m_eventModel;
460         CGContextRef m_contextRef;
461         WindowRef m_fakeWindow;
462 #if PLATFORM(QT)
463         QPixmap m_pixmap;
464 #endif
465
466         Point m_lastMousePos;
467         void setNPWindowIfNeeded();
468         void nullEventTimerFired(Timer<PluginView>*);
469         Point globalMousePosForPlugin() const;
470         Point mousePosForPlugin(MouseEvent* event = 0) const;
471 #endif
472
473 #if defined(XP_UNIX) && ENABLE(NETSCAPE_PLUGIN_API)
474         bool m_hasPendingGeometryChange;
475         Pixmap m_drawable;
476         Visual* m_visual;
477         Colormap m_colormap;
478         Display* m_pluginDisplay;
479
480         void initXEvent(XEvent* event);
481 #endif
482
483 #if PLATFORM(QT) 
484 #if defined(XP_UNIX) && ENABLE(NETSCAPE_PLUGIN_API)
485         void paintUsingXPixmap(QPainter* painter, const QRect &exposedRect);
486 #endif
487 #if USE(ACCELERATED_COMPOSITING_PLUGIN_LAYER)
488         OwnPtr<PlatformLayer> m_platformLayer;
489         friend class PluginGraphicsLayerQt;
490 #endif // USE(ACCELERATED_COMPOSITING_PLUGIN_LAYER)
491 #endif // PLATFORM(QT)
492
493 #if PLATFORM(GTK)
494         static gboolean plugRemovedCallback(GtkSocket*, PluginView*);
495         static void plugAddedCallback(GtkSocket*, PluginView*);
496         void updateWidgetAllocationAndClip();
497         bool m_plugAdded;
498         IntRect m_delayedAllocation;
499 #endif
500
501 #if ENABLE(TIZEN_ACCELERATED_COMPOSITING_PLUGIN_LAYER_EFL)
502         RefPtr<EflLayer> m_platformLayer;
503
504         mutable OwnPtr<ImageBuffer> m_imageBuffer;
505 #endif
506
507         IntRect m_clipRect; // The clip rect to apply to a windowed plug-in
508         IntRect m_windowRect; // Our window rect.
509
510         bool m_loadManually;
511         RefPtr<PluginStream> m_manualStream;
512
513         bool m_isJavaScriptPaused;
514
515         bool m_haveCalledSetWindow;
516
517         static PluginView* s_currentPluginView;
518
519 #if PLATFORM(EFL) && ENABLE(TIZEN_SUPPORT_PLUGINS) && ENABLE(TIZEN_DONT_PAN_OVER_SOME_PLUGINS)
520         static bool s_isHandlingEvents;
521         static PluginView* s_currentPluginHandlingEvents;
522 #if ENABLE(TIZEN_EVAS_OBJECT_PLUGIN)
523         bool m_lastEventWasHandled;
524 #endif
525 #endif
526     };
527
528 } // namespace WebCore
529
530 #endif