1/*
2 * Copyright (C) 2010 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef NetscapePlugin_h
27#define NetscapePlugin_h
28
29#include "NetscapePluginModule.h"
30#include "Plugin.h"
31#include "RunLoop.h"
32#include <WebCore/GraphicsLayer.h>
33#include <WebCore/IntRect.h>
34#include <wtf/HashMap.h>
35#include <wtf/text/CString.h>
36#include <wtf/text/StringHash.h>
37
38namespace WebCore {
39    class HTTPHeaderMap;
40}
41
42namespace WebKit {
43
44class NetscapePluginStream;
45
46class NetscapePlugin : public Plugin {
47public:
48    static PassRefPtr<NetscapePlugin> create(PassRefPtr<NetscapePluginModule> pluginModule);
49    virtual ~NetscapePlugin();
50
51    static PassRefPtr<NetscapePlugin> fromNPP(NPP);
52
53#if PLATFORM(MAC)
54    NPError setDrawingModel(NPDrawingModel);
55    NPError setEventModel(NPEventModel);
56    NPBool convertPoint(double sourceX, double sourceY, NPCoordinateSpace sourceSpace, double& destX, double& destY, NPCoordinateSpace destSpace);
57    NPError popUpContextMenu(NPMenu*);
58
59    mach_port_t compositingRenderServerPort();
60
61#ifndef NP_NO_CARBON
62    WindowRef windowRef() const;
63    bool isWindowActive() const { return m_windowHasFocus; }
64
65    static NetscapePlugin* netscapePluginFromWindow(WindowRef);
66    static unsigned buttonState();
67#endif
68
69#elif PLATFORM(WIN)
70    HWND containingWindow() const;
71#endif
72
73    PluginQuirks quirks() const { return m_pluginModule->pluginQuirks(); }
74
75    void invalidate(const NPRect*);
76    static const char* userAgent(NPP);
77    void loadURL(const String& method, const String& urlString, const String& target, const WebCore::HTTPHeaderMap& headerFields,
78                 const Vector<uint8_t>& httpBody, bool sendNotification, void* notificationData);
79    NPError destroyStream(NPStream*, NPReason);
80    void setIsWindowed(bool);
81    void setIsTransparent(bool);
82    void setStatusbarText(const String&);
83    static void setException(const String&);
84    bool evaluate(NPObject*, const String&scriptString, NPVariant* result);
85    bool isPrivateBrowsingEnabled();
86
87    // These return retained objects.
88    NPObject* windowScriptNPObject();
89    NPObject* pluginElementNPObject();
90
91    void cancelStreamLoad(NetscapePluginStream*);
92    void removePluginStream(NetscapePluginStream*);
93
94    bool isAcceleratedCompositingEnabled();
95
96    void pushPopupsEnabledState(bool enabled);
97    void popPopupsEnabledState();
98
99    String proxiesForURL(const String& urlString);
100    String cookiesForURL(const String& urlString);
101    void setCookiesForURL(const String& urlString, const String& cookieString);
102
103    // Member functions for calling into the plug-in.
104    NPError NPP_New(NPMIMEType pluginType, uint16_t mode, int16_t argc, char* argn[], char* argv[], NPSavedData*);
105    NPError NPP_Destroy(NPSavedData**);
106    NPError NPP_SetWindow(NPWindow*);
107    NPError NPP_NewStream(NPMIMEType, NPStream*, NPBool seekable, uint16_t* stype);
108    NPError NPP_DestroyStream(NPStream*, NPReason);
109    void NPP_StreamAsFile(NPStream*, const char* filename);
110    int32_t NPP_WriteReady(NPStream*);
111    int32_t NPP_Write(NPStream*, int32_t offset, int32_t len, void* buffer);
112    int16_t NPP_HandleEvent(void* event);
113    void NPP_URLNotify(const char* url, NPReason, void* notifyData);
114    NPError NPP_GetValue(NPPVariable, void *value);
115    NPError NPP_SetValue(NPNVariable, void *value);
116
117private:
118    NetscapePlugin(PassRefPtr<NetscapePluginModule> pluginModule);
119
120    void callSetWindow();
121    bool shouldLoadSrcURL();
122    NetscapePluginStream* streamFromID(uint64_t streamID);
123    void stopAllStreams();
124    bool allowPopups() const;
125
126    const char* userAgent();
127
128    bool platformPostInitialize();
129    void platformDestroy();
130    bool platformInvalidate(const WebCore::IntRect&);
131    void platformGeometryDidChange();
132    void platformPaint(WebCore::GraphicsContext*, const WebCore::IntRect& dirtyRect, bool isSnapshot = false);
133
134    bool platformHandleMouseEvent(const WebMouseEvent&);
135    bool platformHandleWheelEvent(const WebWheelEvent&);
136    bool platformHandleMouseEnterEvent(const WebMouseEvent&);
137    bool platformHandleMouseLeaveEvent(const WebMouseEvent&);
138    bool platformHandleKeyboardEvent(const WebKeyboardEvent&);
139    void platformSetFocus(bool);
140
141    // Plugin
142    virtual bool initialize(PluginController*, const Parameters&);
143    virtual void destroy();
144    virtual void paint(WebCore::GraphicsContext*, const WebCore::IntRect& dirtyRect);
145    virtual PassRefPtr<ShareableBitmap> snapshot();
146#if PLATFORM(MAC)
147    virtual PlatformLayer* pluginLayer();
148#endif
149    virtual bool isTransparent();
150    virtual void geometryDidChange(const WebCore::IntRect& frameRect, const WebCore::IntRect& clipRect);
151    virtual void frameDidFinishLoading(uint64_t requestID);
152    virtual void frameDidFail(uint64_t requestID, bool wasCancelled);
153    virtual void didEvaluateJavaScript(uint64_t requestID, const String& requestURLString, const String& result);
154    virtual void streamDidReceiveResponse(uint64_t streamID, const WebCore::KURL& responseURL, uint32_t streamLength,
155                                          uint32_t lastModifiedTime, const String& mimeType, const String& headers);
156    virtual void streamDidReceiveData(uint64_t streamID, const char* bytes, int length);
157    virtual void streamDidFinishLoading(uint64_t streamID);
158    virtual void streamDidFail(uint64_t streamID, bool wasCancelled);
159    virtual void manualStreamDidReceiveResponse(const WebCore::KURL& responseURL, uint32_t streamLength,
160                                                uint32_t lastModifiedTime, const String& mimeType, const String& headers);
161    virtual void manualStreamDidReceiveData(const char* bytes, int length);
162    virtual void manualStreamDidFinishLoading();
163    virtual void manualStreamDidFail(bool wasCancelled);
164
165    virtual bool handleMouseEvent(const WebMouseEvent&);
166    virtual bool handleWheelEvent(const WebWheelEvent&);
167    virtual bool handleMouseEnterEvent(const WebMouseEvent&);
168    virtual bool handleMouseLeaveEvent(const WebMouseEvent&);
169    virtual bool handleKeyboardEvent(const WebKeyboardEvent&);
170    virtual void setFocus(bool);
171    virtual NPObject* pluginScriptableNPObject();
172
173#if PLATFORM(MAC)
174    virtual void windowFocusChanged(bool);
175    virtual void windowAndViewFramesChanged(const WebCore::IntRect& windowFrameInScreenCoordinates, const WebCore::IntRect& viewFrameInWindowCoordinates);
176    virtual void windowVisibilityChanged(bool);
177
178    virtual uint64_t pluginComplexTextInputIdentifier() const;
179    virtual void sendComplexTextInput(const String& textInput);
180#endif
181
182    virtual void privateBrowsingStateChanged(bool);
183
184    bool supportsSnapshotting() const;
185
186    virtual PluginController* controller();
187
188    PluginController* m_pluginController;
189    uint64_t m_nextRequestID;
190
191    typedef HashMap<uint64_t, std::pair<String, void*> > PendingURLNotifyMap;
192    PendingURLNotifyMap m_pendingURLNotifications;
193
194    typedef HashMap<uint64_t, RefPtr<NetscapePluginStream> > StreamsMap;
195    StreamsMap m_streams;
196
197    RefPtr<NetscapePluginModule> m_pluginModule;
198    NPP_t m_npp;
199    NPWindow m_npWindow;
200
201    WebCore::IntRect m_frameRect;
202    WebCore::IntRect m_clipRect;
203
204    CString m_userAgent;
205
206    bool m_isStarted;
207    bool m_isWindowed;
208    bool m_isTransparent;
209    bool m_inNPPNew;
210    bool m_loadManually;
211    RefPtr<NetscapePluginStream> m_manualStream;
212    Vector<bool, 8> m_popupEnabledStates;
213
214#if PLUGIN_ARCHITECTURE(MAC)
215    NPDrawingModel m_drawingModel;
216    NPEventModel m_eventModel;
217    RetainPtr<PlatformLayer> m_pluginLayer;
218
219    NPCocoaEvent* m_currentMouseEvent;
220
221    bool m_pluginHasFocus;
222    bool m_windowHasFocus;
223
224    WebCore::IntRect m_windowFrameInScreenCoordinates;
225    WebCore::IntRect m_viewFrameInWindowCoordinates;
226
227#ifndef NP_NO_CARBON
228    void nullEventTimerFired();
229
230    // FIXME: It's a bit wasteful to have one null event timer per plug-in.
231    // We should investigate having one per window.
232    RunLoop::Timer<NetscapePlugin> m_nullEventTimer;
233    NP_CGContext m_npCGContext;
234#endif
235#elif PLUGIN_ARCHITECTURE(WIN)
236    HWND m_window;
237#elif PLUGIN_ARCHITECTURE(X11)
238    Pixmap m_drawable;
239    Display* m_pluginDisplay;
240#endif
241};
242
243} // namespace WebKit
244
245#endif // NetscapePlugin_h
246