1 /* 2 * Copyright (C) 2008 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. ``AS IS'' AND ANY 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #if USE(PLUGIN_HOST_PROCESS) 27 28 #ifndef NetscapePluginInstanceProxy_h 29 #define NetscapePluginInstanceProxy_h 30 31 #include <JavaScriptCore/Protect.h> 32 #include <WebCore/Timer.h> 33 #include <WebKit/npapi.h> 34 #include <wtf/Deque.h> 35 #include <wtf/HashMap.h> 36 #include <wtf/PassRefPtr.h> 37 #include <wtf/RefCounted.h> 38 #include <wtf/RetainPtr.h> 39 #include "WebKitPluginHostTypes.h" 40 41 namespace WebCore { 42 class String; 43 } 44 45 namespace JSC { 46 namespace Bindings { 47 class Instance; 48 class RootObject; 49 } 50 } 51 @class WebHostedNetscapePluginView; 52 53 namespace WebKit { 54 55 class HostedNetscapePluginStream; 56 class NetscapePluginHostProxy; 57 class ProxyInstance; 58 59 class NetscapePluginInstanceProxy : public RefCounted<NetscapePluginInstanceProxy> { 60 public: create(NetscapePluginHostProxy * pluginHostProxy,WebHostedNetscapePluginView * pluginView,bool fullFramePlugin)61 static PassRefPtr<NetscapePluginInstanceProxy> create(NetscapePluginHostProxy* pluginHostProxy, WebHostedNetscapePluginView *pluginView, bool fullFramePlugin) 62 { 63 return adoptRef(new NetscapePluginInstanceProxy(pluginHostProxy, pluginView, fullFramePlugin)); 64 } 65 ~NetscapePluginInstanceProxy(); 66 pluginID()67 uint32_t pluginID() const 68 { 69 ASSERT(m_pluginID); 70 71 return m_pluginID; 72 } renderContextID()73 uint32_t renderContextID() const { return m_renderContextID; } setRenderContextID(uint32_t renderContextID)74 void setRenderContextID(uint32_t renderContextID) { m_renderContextID = renderContextID; } 75 useSoftwareRenderer()76 bool useSoftwareRenderer() const { return m_useSoftwareRenderer; } setUseSoftwareRenderer(bool useSoftwareRenderer)77 void setUseSoftwareRenderer(bool useSoftwareRenderer) { m_useSoftwareRenderer = useSoftwareRenderer; } 78 pluginView()79 WebHostedNetscapePluginView *pluginView() const { return m_pluginView; } hostProxy()80 NetscapePluginHostProxy* hostProxy() const { return m_pluginHostProxy; } 81 82 bool cancelStreamLoad(uint32_t streamID, NPReason); 83 void disconnectStream(HostedNetscapePluginStream*); 84 85 void setManualStream(PassRefPtr<HostedNetscapePluginStream>); manualStream()86 HostedNetscapePluginStream* manualStream() const { return m_manualStream.get(); } 87 88 void pluginHostDied(); 89 90 void resize(NSRect size, NSRect clipRect, bool sync); 91 void destroy(); 92 void focusChanged(bool hasFocus); 93 void windowFocusChanged(bool hasFocus); 94 void windowFrameChanged(NSRect frame); 95 96 void mouseEvent(NSView *pluginView, NSEvent *, NPCocoaEventType); 97 void keyEvent(NSView *pluginView, NSEvent *, NPCocoaEventType); 98 void insertText(NSString *); 99 bool wheelEvent(NSView *pluginView, NSEvent *); 100 void syntheticKeyDownWithCommandModifier(int keyCode, char character); 101 void flagsChanged(NSEvent *); 102 void print(CGContextRef, unsigned width, unsigned height); 103 104 void startTimers(bool throttleTimers); 105 void stopTimers(); 106 107 void invalidateRect(double x, double y, double width, double height); 108 109 // NPRuntime 110 bool getWindowNPObject(uint32_t& objectID); 111 bool getPluginElementNPObject(uint32_t& objectID); 112 void releaseObject(uint32_t objectID); 113 114 bool evaluate(uint32_t objectID, const WebCore::String& script, data_t& resultData, mach_msg_type_number_t& resultLength, bool allowPopups); 115 bool invoke(uint32_t objectID, const JSC::Identifier& methodName, data_t argumentsData, mach_msg_type_number_t argumentsLength, data_t& resultData, mach_msg_type_number_t& resultLength); 116 bool invokeDefault(uint32_t objectID, data_t argumentsData, mach_msg_type_number_t argumentsLength, data_t& resultData, mach_msg_type_number_t& resultLength); 117 bool construct(uint32_t objectID, data_t argumentsData, mach_msg_type_number_t argumentsLength, data_t& resultData, mach_msg_type_number_t& resultLength); 118 bool enumerate(uint32_t objectID, data_t& resultData, mach_msg_type_number_t& resultLength); 119 120 bool getProperty(uint32_t objectID, const JSC::Identifier& propertyName, data_t &resultData, mach_msg_type_number_t& resultLength); 121 bool getProperty(uint32_t objectID, unsigned propertyName, data_t &resultData, mach_msg_type_number_t& resultLength); 122 bool setProperty(uint32_t objectID, const JSC::Identifier& propertyName, data_t valueData, mach_msg_type_number_t valueLength); 123 bool setProperty(uint32_t objectID, unsigned propertyName, data_t valueData, mach_msg_type_number_t valueLength); 124 bool removeProperty(uint32_t objectID, const JSC::Identifier& propertyName); 125 bool removeProperty(uint32_t objectID, unsigned propertyName); 126 bool hasProperty(uint32_t objectID, const JSC::Identifier& propertyName); 127 bool hasProperty(uint32_t objectID, unsigned propertyName); 128 bool hasMethod(uint32_t objectID, const JSC::Identifier& methodName); 129 130 void status(const char* message); 131 NPError loadURL(const char* url, const char* target, const char* postData, uint32_t postDataLength, LoadURLFlags, uint32_t& requestID); 132 133 bool getCookies(data_t urlData, mach_msg_type_number_t urlLength, data_t& cookiesData, mach_msg_type_number_t& cookiesLength); 134 bool setCookies(data_t urlData, mach_msg_type_number_t urlLength, data_t cookiesData, mach_msg_type_number_t cookiesLength); 135 136 bool getProxy(data_t urlData, mach_msg_type_number_t urlLength, data_t& proxyData, mach_msg_type_number_t& proxyLength); 137 bool getAuthenticationInfo(data_t protocolData, data_t hostData, uint32_t port, data_t schemeData, data_t realmData, 138 data_t& usernameData, mach_msg_type_number_t& usernameLength, data_t& passwordData, mach_msg_type_number_t& passwordLength); 139 bool convertPoint(double sourceX, double sourceY, NPCoordinateSpace sourceSpace, 140 double& destX, double& destY, NPCoordinateSpace destSpace); 141 142 PassRefPtr<JSC::Bindings::Instance> createBindingsInstance(PassRefPtr<JSC::Bindings::RootObject>); 143 RetainPtr<NSData *> marshalValues(JSC::ExecState*, const JSC::ArgList& args); 144 void marshalValue(JSC::ExecState*, JSC::JSValue value, data_t& resultData, mach_msg_type_number_t& resultLength); 145 JSC::JSValue demarshalValue(JSC::ExecState*, const char* valueData, mach_msg_type_number_t valueLength); 146 147 void addInstance(ProxyInstance*); 148 void removeInstance(ProxyInstance*); 149 150 void cleanup(); 151 void invalidate(); 152 153 void willCallPluginFunction(); 154 void didCallPluginFunction(); 155 bool shouldStop(); 156 157 uint32_t nextRequestID(); 158 159 uint32_t checkIfAllowedToLoadURL(const char* url, const char* target); 160 void cancelCheckIfAllowedToLoadURL(uint32_t checkID); 161 void checkIfAllowedToLoadURLResult(uint32_t checkID, bool allowed); 162 163 void resolveURL(const char* url, const char* target, data_t& resolvedURLData, mach_msg_type_number_t& resolvedURLLength); 164 165 // Reply structs 166 struct Reply { 167 enum Type { 168 InstantiatePlugin, 169 GetScriptableNPObject, 170 BooleanAndData, 171 Boolean 172 }; 173 ReplyReply174 Reply(Type type) 175 : m_type(type) 176 { 177 } 178 ~ReplyReply179 virtual ~Reply() { } 180 181 Type m_type; 182 }; 183 184 struct InstantiatePluginReply : public Reply { 185 static const int ReplyType = InstantiatePlugin; 186 InstantiatePluginReplyInstantiatePluginReply187 InstantiatePluginReply(kern_return_t resultCode, uint32_t renderContextID, boolean_t useSoftwareRenderer) 188 : Reply(InstantiatePlugin) 189 , m_resultCode(resultCode) 190 , m_renderContextID(renderContextID) 191 , m_useSoftwareRenderer(useSoftwareRenderer) 192 { 193 } 194 195 kern_return_t m_resultCode; 196 uint32_t m_renderContextID; 197 boolean_t m_useSoftwareRenderer; 198 }; 199 200 struct GetScriptableNPObjectReply : public Reply { 201 static const Reply::Type ReplyType = GetScriptableNPObject; 202 GetScriptableNPObjectReplyGetScriptableNPObjectReply203 GetScriptableNPObjectReply(uint32_t objectID) 204 : Reply(ReplyType) 205 , m_objectID(objectID) 206 { 207 } 208 209 uint32_t m_objectID; 210 }; 211 212 struct BooleanReply : public Reply { 213 static const Reply::Type ReplyType = Boolean; 214 BooleanReplyBooleanReply215 BooleanReply(boolean_t result) 216 : Reply(ReplyType) 217 , m_result(result) 218 { 219 } 220 221 boolean_t m_result; 222 }; 223 224 struct BooleanAndDataReply : public Reply { 225 static const Reply::Type ReplyType = BooleanAndData; 226 BooleanAndDataReplyBooleanAndDataReply227 BooleanAndDataReply(boolean_t returnValue, RetainPtr<CFDataRef> result) 228 : Reply(ReplyType) 229 , m_returnValue(returnValue) 230 , m_result(result) 231 { 232 } 233 234 boolean_t m_returnValue; 235 RetainPtr<CFDataRef> m_result; 236 }; 237 setCurrentReply(uint32_t requestID,Reply * reply)238 void setCurrentReply(uint32_t requestID, Reply* reply) 239 { 240 ASSERT(!m_replies.contains(requestID)); 241 m_replies.set(requestID, reply); 242 } 243 244 template <typename T> waitForReply(uint32_t requestID)245 std::auto_ptr<T> waitForReply(uint32_t requestID) 246 { 247 m_waitingForReply = true; 248 249 Reply* reply = processRequestsAndWaitForReply(requestID); 250 if (reply) 251 ASSERT(reply->m_type == T::ReplyType); 252 253 m_waitingForReply = false; 254 return std::auto_ptr<T>(static_cast<T*>(reply)); 255 } 256 257 private: 258 NetscapePluginInstanceProxy(NetscapePluginHostProxy*, WebHostedNetscapePluginView *, bool fullFramePlugin); 259 260 NPError loadRequest(NSURLRequest *, const char* cTarget, bool currentEventIsUserGesture, uint32_t& streamID); 261 262 class PluginRequest; 263 void performRequest(PluginRequest*); 264 void evaluateJavaScript(PluginRequest*); 265 266 void stopAllStreams(); 267 Reply* processRequestsAndWaitForReply(uint32_t requestID); 268 269 NetscapePluginHostProxy* m_pluginHostProxy; 270 WebHostedNetscapePluginView *m_pluginView; 271 272 void requestTimerFired(WebCore::Timer<NetscapePluginInstanceProxy>*); 273 WebCore::Timer<NetscapePluginInstanceProxy> m_requestTimer; 274 Deque<PluginRequest*> m_pluginRequests; 275 276 HashMap<uint32_t, RefPtr<HostedNetscapePluginStream> > m_streams; 277 278 uint32_t m_currentURLRequestID; 279 280 uint32_t m_pluginID; 281 uint32_t m_renderContextID; 282 boolean_t m_useSoftwareRenderer; 283 284 bool m_waitingForReply; 285 HashMap<uint32_t, Reply*> m_replies; 286 287 // NPRuntime 288 uint32_t idForObject(JSC::JSObject*); 289 290 void addValueToArray(NSMutableArray *, JSC::ExecState* exec, JSC::JSValue value); 291 292 bool demarshalValueFromArray(JSC::ExecState*, NSArray *array, NSUInteger& index, JSC::JSValue& result); 293 void demarshalValues(JSC::ExecState*, data_t valuesData, mach_msg_type_number_t valuesLength, JSC::MarkedArgumentBuffer& result); 294 295 uint32_t m_objectIDCounter; 296 typedef HashMap<uint32_t, JSC::ProtectedPtr<JSC::JSObject> > ObjectMap; 297 ObjectMap m_objects; 298 299 typedef HashSet<ProxyInstance*> ProxyInstanceSet; 300 ProxyInstanceSet m_instances; 301 302 uint32_t m_urlCheckCounter; 303 typedef HashMap<uint32_t, RetainPtr<id> > URLCheckMap; 304 URLCheckMap m_urlChecks; 305 306 unsigned m_pluginFunctionCallDepth; 307 bool m_shouldStopSoon; 308 uint32_t m_currentRequestID; 309 bool m_inDestroy; 310 311 RefPtr<HostedNetscapePluginStream> m_manualStream; 312 }; 313 314 } // namespace WebKit 315 316 #endif // NetscapePluginInstanceProxy_h 317 #endif // USE(PLUGIN_HOST_PROCESS) 318