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#if ENABLE(NETSCAPE_PLUGIN_API)
30
31#include "NetscapePluginModule.h"
32#include "Plugin.h"
33#include <WebCore/AffineTransform.h>
34#include <WebCore/GraphicsLayer.h>
35#include <WebCore/IntRect.h>
36#include <wtf/HashMap.h>
37#include <wtf/RunLoop.h>
38#include <wtf/text/CString.h>
39#include <wtf/text/StringHash.h>
40
41namespace WebCore {
42    class HTTPHeaderMap;
43    class ProtectionSpace;
44    class SharedBuffer;
45}
46
47OBJC_CLASS WKNPAPIPlugInContainer;
48
49namespace WebKit {
50
51class NetscapePluginStream;
52
53class NetscapePlugin : public Plugin {
54public:
55    static PassRefPtr<NetscapePlugin> create(PassRefPtr<NetscapePluginModule> pluginModule);
56    virtual ~NetscapePlugin();
57
58    static PassRefPtr<NetscapePlugin> fromNPP(NPP);
59
60    // In-process NetscapePlugins don't support asynchronous initialization.
61    virtual bool isBeingAsynchronouslyInitialized() const { return false; }
62
63#if PLATFORM(COCOA)
64    NPError setDrawingModel(NPDrawingModel);
65    NPError setEventModel(NPEventModel);
66    NPBool convertPoint(double sourceX, double sourceY, NPCoordinateSpace sourceSpace, double& destX, double& destY, NPCoordinateSpace destSpace);
67    NPError popUpContextMenu(NPMenu*);
68
69    void setPluginReturnsNonretainedLayer(bool pluginReturnsNonretainedLayer) { m_pluginReturnsNonretainedLayer = pluginReturnsNonretainedLayer; }
70    void setPluginWantsLegacyCocoaTextInput(bool pluginWantsLegacyCocoaTextInput) { m_pluginWantsLegacyCocoaTextInput = pluginWantsLegacyCocoaTextInput; }
71
72    bool hasHandledAKeyDownEvent() const { return m_hasHandledAKeyDownEvent; }
73
74    mach_port_t compositingRenderServerPort();
75    void openPluginPreferencePane();
76
77    // Computes an affine transform from the given coordinate space to the screen coordinate space.
78    bool getScreenTransform(NPCoordinateSpace sourceSpace, WebCore::AffineTransform&);
79
80    WKNPAPIPlugInContainer* plugInContainer();
81
82#ifndef NP_NO_CARBON
83    WindowRef windowRef() const;
84    bool isWindowActive() const { return m_windowHasFocus; }
85    void updateFakeWindowBounds();
86
87    static NetscapePlugin* netscapePluginFromWindow(WindowRef);
88    static unsigned buttonState();
89#endif
90
91#endif
92
93    PluginQuirks quirks() const { return m_pluginModule->pluginQuirks(); }
94
95    void invalidate(const NPRect*);
96    static const char* userAgent(NPP);
97    void loadURL(const String& method, const String& urlString, const String& target, const WebCore::HTTPHeaderMap& headerFields,
98                 const Vector<uint8_t>& httpBody, bool sendNotification, void* notificationData);
99    NPError destroyStream(NPStream*, NPReason);
100    void setIsWindowed(bool);
101    void setIsTransparent(bool);
102    void setStatusbarText(const String&);
103    static void setException(const String&);
104    bool evaluate(NPObject*, const String&scriptString, NPVariant* result);
105    bool isPrivateBrowsingEnabled();
106
107    static void setSetExceptionFunction(void (*)(const String&));
108
109    // These return retained objects.
110    NPObject* windowScriptNPObject();
111    NPObject* pluginElementNPObject();
112
113    void cancelStreamLoad(NetscapePluginStream*);
114    void removePluginStream(NetscapePluginStream*);
115
116    bool isAcceleratedCompositingEnabled();
117
118    void pushPopupsEnabledState(bool enabled);
119    void popPopupsEnabledState();
120
121    void pluginThreadAsyncCall(void (*function)(void*), void* userData);
122
123    unsigned scheduleTimer(unsigned interval, bool repeat, void (*timerFunc)(NPP, unsigned timerID));
124    void unscheduleTimer(unsigned timerID);
125
126    double contentsScaleFactor();
127    String proxiesForURL(const String& urlString);
128    String cookiesForURL(const String& urlString);
129    void setCookiesForURL(const String& urlString, const String& cookieString);
130    bool getAuthenticationInfo(const WebCore::ProtectionSpace&, String& username, String& password);
131
132    // Member functions for calling into the plug-in.
133    NPError NPP_New(NPMIMEType pluginType, uint16_t mode, int16_t argc, char* argn[], char* argv[], NPSavedData*);
134    NPError NPP_Destroy(NPSavedData**);
135    NPError NPP_SetWindow(NPWindow*);
136    NPError NPP_NewStream(NPMIMEType, NPStream*, NPBool seekable, uint16_t* stype);
137    NPError NPP_DestroyStream(NPStream*, NPReason);
138    void NPP_StreamAsFile(NPStream*, const char* filename);
139    int32_t NPP_WriteReady(NPStream*);
140    int32_t NPP_Write(NPStream*, int32_t offset, int32_t len, void* buffer);
141    int16_t NPP_HandleEvent(void* event);
142    void NPP_URLNotify(const char* url, NPReason, void* notifyData);
143    NPError NPP_GetValue(NPPVariable, void *value);
144    NPError NPP_SetValue(NPNVariable, void *value);
145
146private:
147    NetscapePlugin(PassRefPtr<NetscapePluginModule> pluginModule);
148
149    void callSetWindow();
150    void callSetWindowInvisible();
151    bool shouldLoadSrcURL();
152    NetscapePluginStream* streamFromID(uint64_t streamID);
153    void stopAllStreams();
154    bool allowPopups() const;
155
156    const char* userAgent();
157
158    void platformPreInitialize();
159    bool platformPostInitialize();
160    void platformDestroy();
161    bool platformInvalidate(const WebCore::IntRect&);
162    void platformGeometryDidChange();
163    void platformVisibilityDidChange();
164    void platformPaint(WebCore::GraphicsContext*, const WebCore::IntRect& dirtyRect, bool isSnapshot = false);
165
166    bool platformHandleMouseEvent(const WebMouseEvent&);
167    bool platformHandleWheelEvent(const WebWheelEvent&);
168    bool platformHandleMouseEnterEvent(const WebMouseEvent&);
169    bool platformHandleMouseLeaveEvent(const WebMouseEvent&);
170    bool platformHandleKeyboardEvent(const WebKeyboardEvent&);
171    void platformSetFocus(bool);
172
173    static bool wantsPluginRelativeNPWindowCoordinates();
174
175    // Plugin
176    virtual bool initialize(const Parameters&);
177    virtual void destroy();
178    virtual void paint(WebCore::GraphicsContext*, const WebCore::IntRect& dirtyRect);
179    virtual PassRefPtr<ShareableBitmap> snapshot();
180#if PLATFORM(COCOA)
181    virtual PlatformLayer* pluginLayer();
182#endif
183    virtual bool isTransparent();
184    virtual bool wantsWheelEvents() override;
185    virtual void geometryDidChange(const WebCore::IntSize& pluginSize, const WebCore::IntRect& clipRect, const WebCore::AffineTransform& pluginToRootViewTransform);
186    virtual void visibilityDidChange(bool isVisible);
187    virtual void frameDidFinishLoading(uint64_t requestID);
188    virtual void frameDidFail(uint64_t requestID, bool wasCancelled);
189    virtual void didEvaluateJavaScript(uint64_t requestID, const String& result);
190    virtual void streamDidReceiveResponse(uint64_t streamID, const WebCore::URL& responseURL, uint32_t streamLength,
191                                          uint32_t lastModifiedTime, const String& mimeType, const String& headers, const String& suggestedFileName);
192    virtual void streamDidReceiveData(uint64_t streamID, const char* bytes, int length);
193    virtual void streamDidFinishLoading(uint64_t streamID);
194    virtual void streamDidFail(uint64_t streamID, bool wasCancelled);
195    virtual void manualStreamDidReceiveResponse(const WebCore::URL& responseURL, uint32_t streamLength,
196                                                uint32_t lastModifiedTime, const String& mimeType, const String& headers, const String& suggestedFileName);
197    virtual void manualStreamDidReceiveData(const char* bytes, int length);
198    virtual void manualStreamDidFinishLoading();
199    virtual void manualStreamDidFail(bool wasCancelled);
200
201    virtual bool handleMouseEvent(const WebMouseEvent&);
202    virtual bool handleWheelEvent(const WebWheelEvent&);
203    virtual bool handleMouseEnterEvent(const WebMouseEvent&);
204    virtual bool handleMouseLeaveEvent(const WebMouseEvent&);
205    virtual bool handleContextMenuEvent(const WebMouseEvent&);
206    virtual bool handleKeyboardEvent(const WebKeyboardEvent&);
207    virtual void setFocus(bool);
208
209    virtual bool handleEditingCommand(const String& commandName, const String& argument) override;
210    virtual bool isEditingCommandEnabled(const String&) override;
211
212    virtual bool shouldAllowScripting() override;
213    virtual bool shouldAllowNavigationFromDrags() override;
214
215    virtual bool handlesPageScaleFactor() override;
216
217    virtual NPObject* pluginScriptableNPObject();
218
219    virtual unsigned countFindMatches(const String&, WebCore::FindOptions, unsigned maxMatchCount) override;
220    virtual bool findString(const String&, WebCore::FindOptions, unsigned maxMatchCount) override;
221
222#if PLATFORM(COCOA)
223    virtual void windowFocusChanged(bool);
224    virtual void windowAndViewFramesChanged(const WebCore::IntRect& windowFrameInScreenCoordinates, const WebCore::IntRect& viewFrameInWindowCoordinates);
225    virtual void windowVisibilityChanged(bool);
226
227    virtual uint64_t pluginComplexTextInputIdentifier() const;
228    virtual void sendComplexTextInput(const String& textInput);
229    virtual void setLayerHostingMode(LayerHostingMode) override;
230
231    void pluginFocusOrWindowFocusChanged();
232    void setComplexTextInputEnabled(bool);
233
234    void updatePluginLayer();
235#endif
236
237    virtual void contentsScaleFactorChanged(float);
238    virtual void storageBlockingStateChanged(bool);
239    virtual void privateBrowsingStateChanged(bool);
240    virtual bool getFormValue(String& formValue);
241    virtual bool handleScroll(WebCore::ScrollDirection, WebCore::ScrollGranularity);
242    virtual WebCore::Scrollbar* horizontalScrollbar();
243    virtual WebCore::Scrollbar* verticalScrollbar();
244
245    virtual bool supportsSnapshotting() const;
246
247    // Convert the given point from plug-in coordinates to root view coordinates.
248    virtual WebCore::IntPoint convertToRootView(const WebCore::IntPoint&) const override;
249
250    // Convert the given point from root view coordinates to plug-in coordinates. Returns false if the point can't be
251    // converted (if the transformation matrix isn't invertible).
252    bool convertFromRootView(const WebCore::IntPoint& pointInRootViewCoordinates, WebCore::IntPoint& pointInPluginCoordinates);
253
254    virtual PassRefPtr<WebCore::SharedBuffer> liveResourceData() const override;
255
256    virtual bool performDictionaryLookupAtLocation(const WebCore::FloatPoint&) override { return false; }
257
258    virtual String getSelectionString() const override { return String(); }
259
260    void updateNPNPrivateMode();
261
262#if PLUGIN_ARCHITECTURE(WIN)
263    static BOOL WINAPI hookedTrackPopupMenu(HMENU, UINT uFlags, int x, int y, int nReserved, HWND, const RECT*);
264    void scheduleWindowedGeometryUpdate();
265#endif
266
267#if PLUGIN_ARCHITECTURE(X11)
268    bool platformPostInitializeWindowed(bool needsXEmbed, uint64_t windowID);
269    bool platformPostInitializeWindowless();
270#endif
271
272    uint64_t m_nextRequestID;
273
274    typedef HashMap<uint64_t, std::pair<String, void*>> PendingURLNotifyMap;
275    PendingURLNotifyMap m_pendingURLNotifications;
276
277    typedef HashMap<uint64_t, RefPtr<NetscapePluginStream>> StreamsMap;
278    StreamsMap m_streams;
279
280    RefPtr<NetscapePluginModule> m_pluginModule;
281    NPP_t m_npp;
282    NPWindow m_npWindow;
283
284    WebCore::IntSize m_pluginSize;
285
286    // The clip rect in plug-in coordinates.
287    WebCore::IntRect m_clipRect;
288
289    // A transform that can be used to convert from root view coordinates to plug-in coordinates.
290    WebCore::AffineTransform m_pluginToRootViewTransform;
291
292    // FIXME: Get rid of these.
293    WebCore::IntRect m_frameRectInWindowCoordinates;
294
295    CString m_userAgent;
296
297    bool m_isStarted;
298    bool m_isWindowed;
299    bool m_isTransparent;
300    bool m_inNPPNew;
301    bool m_shouldUseManualLoader;
302    bool m_hasCalledSetWindow;
303    bool m_isVisible;
304
305    RefPtr<NetscapePluginStream> m_manualStream;
306    Vector<bool, 8> m_popupEnabledStates;
307
308    class Timer {
309        WTF_MAKE_NONCOPYABLE(Timer);
310
311    public:
312        typedef void (*TimerFunc)(NPP, uint32_t timerID);
313
314        Timer(NetscapePlugin*, unsigned timerID, unsigned interval, bool repeat, TimerFunc);
315        ~Timer();
316
317        void start();
318        void stop();
319
320    private:
321        void timerFired();
322
323        // This is a weak pointer since Timer objects are destroyed before the NetscapePlugin object itself is destroyed.
324        NetscapePlugin* m_netscapePlugin;
325
326        unsigned m_timerID;
327        unsigned m_interval;
328        bool m_repeat;
329        TimerFunc m_timerFunc;
330
331        RunLoop::Timer<Timer> m_timer;
332    };
333    typedef HashMap<unsigned, std::unique_ptr<Timer>> TimerMap;
334    TimerMap m_timers;
335    unsigned m_nextTimerID;
336
337    bool m_privateBrowsingState;
338    bool m_storageBlockingState;
339
340#if PLUGIN_ARCHITECTURE(MAC)
341    NPDrawingModel m_drawingModel;
342    NPEventModel m_eventModel;
343
344    RetainPtr<PlatformLayer> m_pluginLayer;
345    bool m_pluginReturnsNonretainedLayer;
346    LayerHostingMode m_layerHostingMode;
347
348    NPCocoaEvent* m_currentMouseEvent;
349
350    bool m_pluginHasFocus;
351    bool m_windowHasFocus;
352
353    // Whether the plug-in wants to use the legacy Cocoa text input handling that
354    // existed in WebKit1, or the updated Cocoa text input handling specified on
355    // https://wiki.mozilla.org/NPAPI:CocoaEventModel#Text_Input
356    bool m_pluginWantsLegacyCocoaTextInput;
357
358    // Whether complex text input is enabled.
359    bool m_isComplexTextInputEnabled;
360
361    // Whether the plug-in has handled a keydown event. This is used to determine
362    // if we can tell the plug-in that we support the updated Cocoa text input specification.
363    bool m_hasHandledAKeyDownEvent;
364
365    // The number of NPCocoaEventKeyUp events that  should be ignored.
366    unsigned m_ignoreNextKeyUpEventCounter;
367
368    WebCore::IntRect m_windowFrameInScreenCoordinates;
369    WebCore::IntRect m_viewFrameInWindowCoordinates;
370
371    RetainPtr<WKNPAPIPlugInContainer> m_plugInContainer;
372
373#ifndef NP_NO_CARBON
374    void nullEventTimerFired();
375
376    // FIXME: It's a bit wasteful to have one null event timer per plug-in.
377    // We should investigate having one per window.
378    RunLoop::Timer<NetscapePlugin> m_nullEventTimer;
379    NP_CGContext m_npCGContext;
380#endif
381#elif PLUGIN_ARCHITECTURE(WIN)
382    HWND m_window;
383    HWND m_contextMenuOwnerWindow;
384#elif PLUGIN_ARCHITECTURE(X11)
385    Pixmap m_drawable;
386    Display* m_pluginDisplay;
387#if PLATFORM(GTK)
388    GtkWidget* m_platformPluginWidget;
389#endif
390
391public: // Need to call it in the NPN_GetValue browser callback.
392    static Display* x11HostDisplay();
393#endif
394};
395
396} // namespace WebKit
397
398#endif // ENABLE(NETSCAPE_PLUGIN_API)
399
400#endif // NetscapePlugin_h
401