initial import
[vuplus_webkit] / Source / WebKit2 / WebProcess / Plugins / Netscape / NetscapePlugin.h
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
38 namespace WebCore {
39     class HTTPHeaderMap;
40     class ProtectionSpace;
41 }
42
43 namespace WebKit {
44
45 class NetscapePluginStream;
46     
47 class NetscapePlugin : public Plugin {
48 public:
49     static PassRefPtr<NetscapePlugin> create(PassRefPtr<NetscapePluginModule> pluginModule);
50     virtual ~NetscapePlugin();
51
52     static PassRefPtr<NetscapePlugin> fromNPP(NPP);
53
54 #if PLATFORM(MAC)
55     NPError setDrawingModel(NPDrawingModel);
56     NPError setEventModel(NPEventModel);
57     NPBool convertPoint(double sourceX, double sourceY, NPCoordinateSpace sourceSpace, double& destX, double& destY, NPCoordinateSpace destSpace);
58     NPError popUpContextMenu(NPMenu*);
59
60     void setPluginReturnsNonretainedLayer(bool pluginReturnsNonretainedLayer) { m_pluginReturnsNonretainedLayer = pluginReturnsNonretainedLayer; }
61     void setPluginWantsLegacyCocoaTextInput(bool pluginWantsLegacyCocoaTextInput) { m_pluginWantsLegacyCocoaTextInput = pluginWantsLegacyCocoaTextInput; }
62
63     bool hasHandledAKeyDownEvent() const { return m_hasHandledAKeyDownEvent; }
64
65     mach_port_t compositingRenderServerPort();
66
67 #ifndef NP_NO_CARBON
68     WindowRef windowRef() const;
69     bool isWindowActive() const { return m_windowHasFocus; }
70
71     static NetscapePlugin* netscapePluginFromWindow(WindowRef);
72     static unsigned buttonState();
73 #endif
74
75 #elif PLATFORM(WIN)
76     HWND containingWindow();
77 #endif
78
79     PluginQuirks quirks() const { return m_pluginModule->pluginQuirks(); }
80
81     void invalidate(const NPRect*);
82     static const char* userAgent(NPP);
83     void loadURL(const String& method, const String& urlString, const String& target, const WebCore::HTTPHeaderMap& headerFields,
84                  const Vector<uint8_t>& httpBody, bool sendNotification, void* notificationData);
85     NPError destroyStream(NPStream*, NPReason);
86     void setIsWindowed(bool);
87     void setIsTransparent(bool);
88     void setStatusbarText(const String&);
89     static void setException(const String&);
90     bool evaluate(NPObject*, const String&scriptString, NPVariant* result);
91     bool isPrivateBrowsingEnabled();
92
93     static void setSetExceptionFunction(void (*)(const String&));
94
95     // These return retained objects.
96     NPObject* windowScriptNPObject();
97     NPObject* pluginElementNPObject();
98
99     bool tryToShortCircuitInvoke(NPObject*, NPIdentifier methodName, const NPVariant* arguments, uint32_t argumentCount, bool& returnValue, NPVariant& result);
100
101     void cancelStreamLoad(NetscapePluginStream*);
102     void removePluginStream(NetscapePluginStream*);
103
104     bool isAcceleratedCompositingEnabled();
105
106     void pushPopupsEnabledState(bool enabled);
107     void popPopupsEnabledState();
108
109     void pluginThreadAsyncCall(void (*function)(void*), void* userData);
110
111     // Called on the plug-in run loop (which is currently the main thread run loop).
112     void handlePluginThreadAsyncCall(void (*function)(void*), void* userData);
113
114     unsigned scheduleTimer(unsigned interval, bool repeat, void (*timerFunc)(NPP, unsigned timerID));
115     void unscheduleTimer(unsigned timerID);
116
117     String proxiesForURL(const String& urlString);
118     String cookiesForURL(const String& urlString);
119     void setCookiesForURL(const String& urlString, const String& cookieString);
120     bool getAuthenticationInfo(const WebCore::ProtectionSpace&, String& username, String& password);
121
122     // Member functions for calling into the plug-in.
123     NPError NPP_New(NPMIMEType pluginType, uint16_t mode, int16_t argc, char* argn[], char* argv[], NPSavedData*);
124     NPError NPP_Destroy(NPSavedData**);
125     NPError NPP_SetWindow(NPWindow*);
126     NPError NPP_NewStream(NPMIMEType, NPStream*, NPBool seekable, uint16_t* stype);
127     NPError NPP_DestroyStream(NPStream*, NPReason);
128     void NPP_StreamAsFile(NPStream*, const char* filename);
129     int32_t NPP_WriteReady(NPStream*);
130     int32_t NPP_Write(NPStream*, int32_t offset, int32_t len, void* buffer);
131     int16_t NPP_HandleEvent(void* event);
132     void NPP_URLNotify(const char* url, NPReason, void* notifyData);
133     NPError NPP_GetValue(NPPVariable, void *value);
134     NPError NPP_SetValue(NPNVariable, void *value);
135
136 private:
137     NetscapePlugin(PassRefPtr<NetscapePluginModule> pluginModule);
138
139     void callSetWindow();
140     bool shouldLoadSrcURL();
141     NetscapePluginStream* streamFromID(uint64_t streamID);
142     void stopAllStreams();
143     bool allowPopups() const;
144
145     const char* userAgent();
146
147     bool platformPostInitialize();
148     void platformDestroy();
149     bool platformInvalidate(const WebCore::IntRect&);
150     void platformGeometryDidChange();
151     void platformVisibilityDidChange();
152     void platformPaint(WebCore::GraphicsContext*, const WebCore::IntRect& dirtyRect, bool isSnapshot = false);
153
154     bool platformHandleMouseEvent(const WebMouseEvent&);
155     bool platformHandleWheelEvent(const WebWheelEvent&);
156     bool platformHandleMouseEnterEvent(const WebMouseEvent&);
157     bool platformHandleMouseLeaveEvent(const WebMouseEvent&);
158     bool platformHandleKeyboardEvent(const WebKeyboardEvent&);
159     void platformSetFocus(bool);
160
161     // Plugin
162     virtual bool initialize(const Parameters&);
163     virtual void destroy();
164     virtual void paint(WebCore::GraphicsContext*, const WebCore::IntRect& dirtyRect);
165     virtual PassRefPtr<ShareableBitmap> snapshot();
166 #if PLATFORM(MAC)
167     virtual PlatformLayer* pluginLayer();
168 #endif
169     virtual bool isTransparent();
170     virtual void geometryDidChange(const WebCore::IntRect& frameRect, const WebCore::IntRect& clipRect);
171     virtual void visibilityDidChange();
172     virtual void frameDidFinishLoading(uint64_t requestID);
173     virtual void frameDidFail(uint64_t requestID, bool wasCancelled);
174     virtual void didEvaluateJavaScript(uint64_t requestID, const String& result);
175     virtual void streamDidReceiveResponse(uint64_t streamID, const WebCore::KURL& responseURL, uint32_t streamLength, 
176                                           uint32_t lastModifiedTime, const String& mimeType, const String& headers);
177     virtual void streamDidReceiveData(uint64_t streamID, const char* bytes, int length);
178     virtual void streamDidFinishLoading(uint64_t streamID);
179     virtual void streamDidFail(uint64_t streamID, bool wasCancelled);
180     virtual void manualStreamDidReceiveResponse(const WebCore::KURL& responseURL, uint32_t streamLength, 
181                                                 uint32_t lastModifiedTime, const String& mimeType, const String& headers);
182     virtual void manualStreamDidReceiveData(const char* bytes, int length);
183     virtual void manualStreamDidFinishLoading();
184     virtual void manualStreamDidFail(bool wasCancelled);
185     
186     virtual bool handleMouseEvent(const WebMouseEvent&);
187     virtual bool handleWheelEvent(const WebWheelEvent&);
188     virtual bool handleMouseEnterEvent(const WebMouseEvent&);
189     virtual bool handleMouseLeaveEvent(const WebMouseEvent&);
190     virtual bool handleKeyboardEvent(const WebKeyboardEvent&);
191     virtual void setFocus(bool);
192     virtual NPObject* pluginScriptableNPObject();
193
194 #if PLATFORM(MAC)
195     virtual void windowFocusChanged(bool);
196     virtual void windowAndViewFramesChanged(const WebCore::IntRect& windowFrameInScreenCoordinates, const WebCore::IntRect& viewFrameInWindowCoordinates);
197     virtual void windowVisibilityChanged(bool);
198
199     virtual uint64_t pluginComplexTextInputIdentifier() const;
200     virtual void sendComplexTextInput(const String& textInput);
201
202     void pluginFocusOrWindowFocusChanged();
203     void setComplexTextInputEnabled(bool);
204 #endif
205
206     virtual void privateBrowsingStateChanged(bool);
207     virtual bool getFormValue(String& formValue);
208
209     bool supportsSnapshotting() const;
210
211 #if PLUGIN_ARCHITECTURE(WIN)
212     static BOOL WINAPI hookedTrackPopupMenu(HMENU, UINT uFlags, int x, int y, int nReserved, HWND, const RECT*);
213     void scheduleWindowedGeometryUpdate();
214 #endif
215
216     uint64_t m_nextRequestID;
217
218     typedef HashMap<uint64_t, std::pair<String, void*> > PendingURLNotifyMap;
219     PendingURLNotifyMap m_pendingURLNotifications;
220
221     typedef HashMap<uint64_t, RefPtr<NetscapePluginStream> > StreamsMap;
222     StreamsMap m_streams;
223
224     RefPtr<NetscapePluginModule> m_pluginModule;
225     NPP_t m_npp;
226     NPWindow m_npWindow;
227
228     WebCore::IntRect m_frameRect;
229     WebCore::IntRect m_clipRect;
230
231     CString m_userAgent;
232
233     bool m_isStarted;
234     bool m_isWindowed;
235     bool m_isTransparent;
236     bool m_inNPPNew;
237     bool m_loadManually;
238     RefPtr<NetscapePluginStream> m_manualStream;
239     Vector<bool, 8> m_popupEnabledStates;
240
241     class Timer {
242         WTF_MAKE_NONCOPYABLE(Timer);
243
244     public:
245         typedef void (*TimerFunc)(NPP, uint32_t timerID);
246
247         static PassOwnPtr<Timer> create(NetscapePlugin*, unsigned timerID, unsigned interval, bool repeat, TimerFunc);
248         ~Timer();
249
250         void start();
251         void stop();
252
253     private:
254         Timer(NetscapePlugin*, unsigned timerID, unsigned interval, bool repeat, TimerFunc);
255
256         void timerFired();
257
258         // This is a weak pointer since Timer objects are destroyed before the NetscapePlugin object itself is destroyed.
259         NetscapePlugin* m_netscapePlugin;
260
261         unsigned m_timerID;
262         unsigned m_interval;
263         bool m_repeat;
264         TimerFunc m_timerFunc;
265
266         RunLoop::Timer<Timer> m_timer;
267     };
268     typedef HashMap<unsigned, Timer*> TimerMap;
269     TimerMap m_timers;
270     unsigned m_nextTimerID;
271
272 #if PLUGIN_ARCHITECTURE(MAC)
273     NPDrawingModel m_drawingModel;
274     NPEventModel m_eventModel;
275
276     RetainPtr<PlatformLayer> m_pluginLayer;
277     bool m_pluginReturnsNonretainedLayer;
278
279     NPCocoaEvent* m_currentMouseEvent;
280
281     bool m_pluginHasFocus;
282     bool m_windowHasFocus;
283
284     // Whether the plug-in wants to use the legacy Cocoa text input handling that
285     // existed in WebKit1, or the updated Cocoa text input handling specified on
286     // https://wiki.mozilla.org/NPAPI:CocoaEventModel#Text_Input
287     bool m_pluginWantsLegacyCocoaTextInput;
288
289     // Whether complex text input is enabled.
290     bool m_isComplexTextInputEnabled;
291
292     // Whether the plug-in has handled a keydown event. This is used to determine
293     // if we can tell the plug-in that we support the updated Cocoa text input specification.
294     bool m_hasHandledAKeyDownEvent;
295
296     // The number of NPCocoaEventKeyUp events that  should be ignored.
297     unsigned m_ignoreNextKeyUpEventCounter;
298
299     WebCore::IntRect m_windowFrameInScreenCoordinates;
300     WebCore::IntRect m_viewFrameInWindowCoordinates;
301
302 #ifndef NP_NO_CARBON
303     void nullEventTimerFired();
304
305     // FIXME: It's a bit wasteful to have one null event timer per plug-in.
306     // We should investigate having one per window.
307     RunLoop::Timer<NetscapePlugin> m_nullEventTimer;
308     NP_CGContext m_npCGContext;
309 #endif
310 #elif PLUGIN_ARCHITECTURE(WIN)
311     HWND m_window;
312     HWND m_contextMenuOwnerWindow;
313 #elif PLUGIN_ARCHITECTURE(X11)
314     Pixmap m_drawable;
315     Display* m_pluginDisplay;
316 #endif
317 };
318
319 } // namespace WebKit
320
321 #endif // NetscapePlugin_h