Sirikata
liboh/plugins/js/JSObjectStructs/JSSystemStruct.hpp
Go to the documentation of this file.
00001 #ifndef __SIRIKATA_JS_SYSTEM_STRUCT_HPP__
00002 #define __SIRIKATA_JS_SYSTEM_STRUCT_HPP__
00003 
00004 #include <sirikata/oh/HostedObject.hpp>
00005 #include <v8.h>
00006 #include "../JSEntityCreateInfo.hpp"
00007 #include <sirikata/oh/Storage.hpp>
00008 #include "JSPresenceStruct.hpp"
00009 #include "JSContextStruct.hpp"
00010 #include "JSCapabilitiesConsts.hpp"
00011 
00012 
00013 namespace Sirikata {
00014 namespace JS {
00015 
00016 //need to forward-declare this so that can reference this inside
00017 class JSEventHandlerStruct;
00018 
00019 
00020 //Most calls in this class just go straight through into associated context to
00021 //make a sibling call.  Split system into intermediate layer between v8-bound
00022 //calls and jscontextstruct to make tracking of capabilities explicit, and easy
00023 //to check without having to dig through a lot of other code.
00024 struct JSSystemStruct
00025 {
00026     JSSystemStruct(JSContextStruct* jscont, Capabilities::CapNum capNum);
00027     ~JSSystemStruct();
00028 
00029     static JSSystemStruct* decodeSystemStruct(v8::Handle<v8::Value> toDecode ,std::string& errorMessage);
00030 
00031     v8::Handle<v8::Value> proxAddedHandlerCallallback(v8::Handle<v8::Function>cb);
00032     v8::Handle<v8::Value> proxRemovedHandlerCallallback(v8::Handle<v8::Function>cb);
00033 
00034     //regular members
00035     v8::Handle<v8::Value> struct_canSendMessage();
00036     v8::Handle<v8::Value> struct_canRecvMessage();
00037     v8::Handle<v8::Value> struct_canProxCallback();
00038     v8::Handle<v8::Value> struct_canProxChangeQuery();
00039     v8::Handle<v8::Value> struct_canImport();
00040 
00041     v8::Handle<v8::Value> pushEvalContextScopeDirectory(const String& newDir);
00042     v8::Handle<v8::Value> popEvalContextScopeDirectory();
00043     
00044     v8::Handle<v8::Value> checkResources();
00045     v8::Handle<v8::Value> struct_evalInGlobal(const String& native_contents, ScriptOrigin* sOrigin);
00046     v8::Handle<v8::Value> checkHeadless();
00047 
00048     v8::Handle<v8::Value> getAssociatedPresence();
00049 
00050     v8::Handle<v8::Value> storageBeginTransaction();
00051     v8::Handle<v8::Value> storageCommit(v8::Handle<v8::Function> cb);
00052     v8::Handle<v8::Value> storageWrite(const OH::Storage::Key& key, const String& toWrite, v8::Handle<v8::Function> cb);
00053     v8::Handle<v8::Value> storageErase(const OH::Storage::Key& key, v8::Handle<v8::Function> cb);
00054     v8::Handle<v8::Value> storageRead(const OH::Storage::Key& key, v8::Handle<v8::Function> cb);
00055     v8::Handle<v8::Value> storageRangeRead(const OH::Storage::Key& start, const OH::Storage::Key& finish, v8::Handle<v8::Function> cb);
00056     v8::Handle<v8::Value> storageRangeErase(const OH::Storage::Key& start, const OH::Storage::Key& finish, v8::Handle<v8::Function> cb);
00057     v8::Handle<v8::Value> storageCount(const OH::Storage::Key& start, const OH::Storage::Key& finish, v8::Handle<v8::Function> cb);
00058 
00059 
00060     v8::Handle<v8::Value> setRestoreScript(const String& key, v8::Handle<v8::Function> cb);
00061     v8::Handle<v8::Value> killEntity();
00062 
00063     v8::Handle<v8::Value> struct_canCreatePres();
00064     v8::Handle<v8::Value> struct_canCreateEnt();
00065     v8::Handle<v8::Value> struct_canEval();
00066 
00076     v8::Handle<v8::Value> sendSandbox(const String& msgToSend, JSContextStruct* destination);
00077 
00078     v8::Handle<v8::Value> setSandboxMessageCallback(v8::Persistent<v8::Function> callback);
00079     v8::Handle<v8::Value> setPresenceMessageCallback(v8::Persistent<v8::Function> callback);
00080 
00081     v8::Handle<v8::Value> emersonCompileString(const String& toCompile);
00082 
00083     v8::Handle<v8::Value> struct_create_vis(const SpaceObjectReference& sporefWatching, JSVisibleDataPtr addParams)
00084     {
00085         return associatedContext->struct_create_vis(sporefWatching,addParams);
00086     }
00087 
00088 
00089     v8::Handle<v8::Value> restorePresence(PresStructRestoreParams& psrp);
00090 
00091 
00092     v8::Handle<v8::Value> debug_fileWrite(String& strToWrite,String& filename);
00093     v8::Handle<v8::Value> debug_fileRead(String& filename);
00094 
00095     v8::Handle<v8::Value> httpRequest(Sirikata::Network::Address addr, Transfer::HttpManager::HTTP_METHOD method, String request, v8::Persistent<v8::Function> cb);
00096 
00097 
00098     v8::Handle<v8::Value> struct_print(const String& msg);
00099     v8::Handle<v8::Value> struct_sendHome(const String& toSend);
00100     v8::Handle<v8::Value> struct_import(const String& toImportFrom,bool isJS);
00101     v8::Handle<v8::Value> struct_require(const String& toRequireFrom,bool isJS);
00102 
00103 
00104     //if have the capability to create presences, create a new presence with
00105     //mesh newMesh and executes initFunc, which gets executed onConnected.
00106     //if do not have the capability, throws an error.
00107     v8::Handle<v8::Value> struct_createEntity(EntityCreateInfo& eci);
00108 
00109     v8::Handle<v8::Value> struct_createContext(JSPresenceStruct* jspres,const SpaceObjectReference& canSendTo, Capabilities::CapNum permNum);
00110 
00111     JSContextStruct* getContext();
00112 
00113     v8::Handle<v8::Value> struct_registerOnPresenceConnectedHandler(v8::Persistent<v8::Function> cb_persist);
00114     v8::Handle<v8::Value> struct_registerOnPresenceDisconnectedHandler(v8::Persistent<v8::Function> cb_persist);
00115 
00116     //last bool indicates whether to send message reliably or unreliably.
00117     v8::Handle<v8::Value> sendMessageNoErrorHandler(JSPresenceStruct* jspres, const String& serialized_message,JSPositionListener* jspl,bool reliable);
00118 
00119 
00120     v8::Handle<v8::Value> deserialize(const String& toDeserialize);
00121 
00122     // Trigger an event handler
00123     v8::Handle<v8::Value> struct_event(v8::Persistent<v8::Function>& cb);
00124 
00125     //create a timer that will fire in dur seconds from now, that will bind the
00126     //this parameter to target and that will fire the callback cb.
00127     v8::Handle<v8::Value> struct_createTimeout(double period, v8::Persistent<v8::Function>& cb);
00128 
00129     v8::Handle<v8::Value> struct_createTimeout(double period,v8::Persistent<v8::Function>& cb, uint32 contID,double timeRemaining, bool isSuspended, bool isCleared);
00130 
00131     v8::Handle<v8::Value> struct_setScript(const String& script);
00132     v8::Handle<v8::Value> struct_getScript();
00133     v8::Handle<v8::Value> struct_reset(const std::map<SpaceObjectReference, std::vector<SpaceObjectReference> > & proxResSet);
00134 
00135 
00136     Capabilities::CapNum getCapNum();
00137 
00138 private:
00139 
00150     void stripCapEscalation(Capabilities::CapNum& permNum, Capabilities::Caps capRequesting, JSPresenceStruct* jspres, const String& capRequestingName);
00151 
00152 
00153     //returns true if you have capability to perform the operation associated with
00154     //capRequesting on jspres, false otherwise.  Note: pass null to jspres if
00155     //requesting a capability not associated with a presence.  (See list of
00156     //these in JSCapabilitiesConsts.hpp.)
00157     bool checkCurCtxtHasCapability(JSPresenceStruct* jspres, Capabilities::Caps capRequesting);
00158 
00159     //associated data
00160     JSContextStruct* associatedContext;
00161     uint32 mCapNum;
00162 };
00163 
00164 
00165 
00166 
00167 #define INLINE_SYSTEM_CONV_ERROR(toConvert,whereError,whichArg,whereWriteTo)   \
00168     JSSystemStruct* whereWriteTo;                                                   \
00169     {                                                                      \
00170         String _errMsg = "In " #whereError "cannot convert " #whichArg " to system struct";     \
00171         whereWriteTo = JSSystemStruct::decodeSystemStruct(toConvert,_errMsg); \
00172         if (whereWriteTo == NULL) \
00173             return v8::ThrowException(v8::Exception::Error(v8::String::New(_errMsg.c_str(), _errMsg.length()))); \
00174     }
00175 
00176 
00177 }//end namespace js
00178 }//end namespace sirikata
00179 
00180 #endif