CrossSiteObject.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #pragma once
  6. namespace Js
  7. {
  8. template <typename T>
  9. class CrossSiteObject : public T
  10. {
  11. private:
  12. DEFINE_VTABLE_CTOR(CrossSiteObject<T>, T);
  13. public:
  14. virtual BOOL GetProperty(Var originalInstance, PropertyId propertyId, Var* value, PropertyValueInfo* info, ScriptContext* requestContext) override;
  15. virtual BOOL GetProperty(Var originalInstance, JavascriptString* propertyNameString, Var* value, PropertyValueInfo* info, ScriptContext* requestContext) override;
  16. virtual BOOL GetAccessors(PropertyId propertyId, Var* getter, Var* setter, ScriptContext * requestContext) override;
  17. virtual BOOL GetPropertyReference(Var originalInstance, PropertyId propertyId, Var* value, PropertyValueInfo* info, ScriptContext* requestContext) override;
  18. virtual BOOL SetProperty(PropertyId propertyId, Var value, PropertyOperationFlags flags, PropertyValueInfo* info) override;
  19. virtual BOOL SetProperty(JavascriptString* propertyNameString, Var value, PropertyOperationFlags flags, PropertyValueInfo* info) override;
  20. virtual BOOL InitProperty(PropertyId propertyId, Var value, PropertyOperationFlags flags = PropertyOperation_None, PropertyValueInfo* info = NULL) override;
  21. virtual BOOL SetPropertyWithAttributes(PropertyId propertyId, Var value, PropertyAttributes attributes, PropertyValueInfo* info, PropertyOperationFlags flags = PropertyOperation_None, SideEffects possibleSideEffects = SideEffects_Any) override;
  22. virtual BOOL InitPropertyScoped(PropertyId propertyId, Var value) override;
  23. virtual BOOL InitFuncScoped(PropertyId propertyId, Var value) override;
  24. virtual BOOL GetItem(Var originalInstance, uint32 index, Var* value, ScriptContext * requestContext) override;
  25. virtual BOOL GetItemReference(Var originalInstance, uint32 index, Var* value, ScriptContext * requestContext) override;
  26. virtual DescriptorFlags GetItemSetter(uint32 index, Var* setterValue, ScriptContext* requestContext) override;
  27. virtual BOOL SetItem(uint32 index, Var value, PropertyOperationFlags flags) override;
  28. virtual Var GetHostDispatchVar() override;
  29. virtual DescriptorFlags GetSetter(PropertyId propertyId, Var* setterValue, PropertyValueInfo* info, ScriptContext* requestContext) override;
  30. virtual DescriptorFlags GetSetter(JavascriptString* propertyNameString, Var* setterValue, PropertyValueInfo* info, ScriptContext* requestContext) override;
  31. virtual BOOL SetAccessors(PropertyId propertyId, Var getter, Var setter, PropertyOperationFlags flags) override;
  32. virtual void RemoveFromPrototype(ScriptContext * requestContext) override;
  33. virtual void AddToPrototype(ScriptContext * requestContext) override;
  34. virtual void SetPrototype(RecyclableObject* newPrototype) override;
  35. virtual BOOL IsCrossSiteObject() const override { return TRUE; }
  36. virtual void MarshalToScriptContext(ScriptContext * requestContext) override
  37. {
  38. AssertMsg(false, "CrossSite::MarshalVar should have handled this");
  39. }
  40. #if ENABLE_TTD
  41. virtual void MarshalCrossSite_TTDInflate() override
  42. {
  43. TTDAssert(false, "Should never call this!!!");
  44. }
  45. #endif
  46. };
  47. template <typename T>
  48. BOOL CrossSiteObject<T>::GetProperty(Var originalInstance, PropertyId propertyId, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
  49. {
  50. originalInstance = CrossSite::MarshalVar(this->GetScriptContext(), originalInstance);
  51. BOOL result = __super::GetProperty(originalInstance, propertyId, value, info, requestContext);
  52. if (result)
  53. {
  54. *value = CrossSite::MarshalVar(requestContext, *value);
  55. }
  56. return result;
  57. }
  58. template <typename T>
  59. BOOL CrossSiteObject<T>::GetProperty(Var originalInstance, JavascriptString* propertyNameString, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
  60. {
  61. BOOL result = __super::GetProperty(originalInstance, propertyNameString, value, info, requestContext);
  62. if (result)
  63. {
  64. *value = CrossSite::MarshalVar(requestContext, *value);
  65. }
  66. return result;
  67. }
  68. template <typename T>
  69. BOOL CrossSiteObject<T>::GetAccessors(PropertyId propertyId, Var* getter, Var* setter, ScriptContext * requestContext)
  70. {
  71. BOOL result = __super::GetAccessors(propertyId, getter, setter, requestContext);
  72. if (result)
  73. {
  74. if (*getter != nullptr)
  75. {
  76. *getter = CrossSite::MarshalVar(requestContext, *getter);
  77. }
  78. if (*setter != nullptr)
  79. {
  80. *setter = CrossSite::MarshalVar(requestContext, *setter);
  81. }
  82. }
  83. return result;
  84. }
  85. template <typename T>
  86. BOOL CrossSiteObject<T>::GetPropertyReference(Var originalInstance, PropertyId propertyId, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
  87. {
  88. originalInstance = CrossSite::MarshalVar(this->GetScriptContext(), originalInstance);
  89. BOOL result = __super::GetPropertyReference(originalInstance, propertyId, value, info, requestContext);
  90. if (result)
  91. {
  92. *value = CrossSite::MarshalVar(requestContext, *value);
  93. }
  94. return result;
  95. }
  96. template <typename T>
  97. BOOL CrossSiteObject<T>::SetProperty(PropertyId propertyId, Var value, PropertyOperationFlags flags, PropertyValueInfo* info)
  98. {
  99. value = CrossSite::MarshalVar(this->GetScriptContext(), value);
  100. return __super::SetProperty(propertyId, value, flags, info);
  101. }
  102. template <typename T>
  103. BOOL CrossSiteObject<T>::SetProperty(JavascriptString* propertyNameString, Var value, PropertyOperationFlags flags, PropertyValueInfo* info)
  104. {
  105. value = CrossSite::MarshalVar(this->GetScriptContext(), value);
  106. return __super::SetProperty(propertyNameString, value, flags, info);
  107. }
  108. template <typename T>
  109. BOOL CrossSiteObject<T>::InitProperty(PropertyId propertyId, Var value, PropertyOperationFlags flags, PropertyValueInfo* info)
  110. {
  111. value = CrossSite::MarshalVar(this->GetScriptContext(), value);
  112. return __super::InitProperty(propertyId, value, flags, info);
  113. }
  114. template <typename T>
  115. BOOL CrossSiteObject<T>::SetPropertyWithAttributes(PropertyId propertyId, Var value, PropertyAttributes attributes, PropertyValueInfo* info, PropertyOperationFlags flags, SideEffects possibleSideEffects /* = SideEffects_Any */)
  116. {
  117. value = CrossSite::MarshalVar(this->GetScriptContext(), value);
  118. return __super::SetPropertyWithAttributes(propertyId, value, attributes, info, flags, possibleSideEffects);
  119. }
  120. template <typename T>
  121. BOOL CrossSiteObject<T>::InitPropertyScoped(PropertyId propertyId, Var value)
  122. {
  123. value = CrossSite::MarshalVar(this->GetScriptContext(), value);
  124. return __super::InitPropertyScoped(propertyId, value);
  125. }
  126. template <typename T>
  127. BOOL CrossSiteObject<T>::InitFuncScoped(PropertyId propertyId, Var value)
  128. {
  129. value = CrossSite::MarshalVar(this->GetScriptContext(), value);
  130. return __super::InitFuncScoped(propertyId, value);
  131. }
  132. template <typename T>
  133. BOOL CrossSiteObject<T>::GetItem(Var originalInstance, uint32 index, Var* value, ScriptContext * requestContext)
  134. {
  135. originalInstance = CrossSite::MarshalVar(this->GetScriptContext(), originalInstance);
  136. BOOL result = __super::GetItem(originalInstance, index, value, requestContext);
  137. if (result)
  138. {
  139. *value = CrossSite::MarshalVar(requestContext, *value);
  140. }
  141. return result;
  142. }
  143. template <typename T>
  144. BOOL CrossSiteObject<T>::GetItemReference(Var originalInstance, uint32 index, Var* value, ScriptContext * requestContext)
  145. {
  146. originalInstance = CrossSite::MarshalVar(this->GetScriptContext(), originalInstance);
  147. BOOL result = __super::GetItemReference(originalInstance, index, value, requestContext);
  148. if (result)
  149. {
  150. *value = CrossSite::MarshalVar(requestContext, *value);
  151. }
  152. return result;
  153. }
  154. template <typename T>
  155. DescriptorFlags CrossSiteObject<T>::GetItemSetter(uint32 index, Var *setterValue, ScriptContext* requestContext)
  156. {
  157. DescriptorFlags flags = __super::GetItemSetter(index, setterValue, requestContext);
  158. if ((flags & Accessor) == Accessor && *setterValue)
  159. {
  160. *setterValue = CrossSite::MarshalVar(requestContext, *setterValue);
  161. }
  162. return flags;
  163. }
  164. template <typename T>
  165. BOOL CrossSiteObject<T>::SetItem(uint32 index, Var value, PropertyOperationFlags flags)
  166. {
  167. value = CrossSite::MarshalVar(this->GetScriptContext(), value);
  168. return __super::SetItem(index, value, flags);
  169. }
  170. template <typename T>
  171. DescriptorFlags CrossSiteObject<T>::GetSetter(PropertyId propertyId, Var* setterValue, PropertyValueInfo* info, ScriptContext* requestContext)
  172. {
  173. DescriptorFlags flags = __super::GetSetter(propertyId, setterValue, info, requestContext);
  174. if ((flags & Accessor) == Accessor && *setterValue)
  175. {
  176. PropertyValueInfo::SetNoCache(info, this);
  177. *setterValue = CrossSite::MarshalVar(requestContext, *setterValue);
  178. }
  179. return flags;
  180. }
  181. template <typename T>
  182. DescriptorFlags CrossSiteObject<T>::GetSetter(JavascriptString* propertyNameString, Var* setterValue, PropertyValueInfo* info, ScriptContext* requestContext)
  183. {
  184. DescriptorFlags flags = __super::GetSetter(propertyNameString, setterValue, info, requestContext);
  185. if ((flags & Accessor) == Accessor && *setterValue)
  186. {
  187. PropertyValueInfo::SetNoCache(info, this);
  188. *setterValue = CrossSite::MarshalVar(requestContext, *setterValue);
  189. }
  190. return flags;
  191. }
  192. template <typename T>
  193. BOOL CrossSiteObject<T>::SetAccessors(PropertyId propertyId, Var getter, Var setter, PropertyOperationFlags flags)
  194. {
  195. if (getter != nullptr)
  196. {
  197. getter = CrossSite::MarshalVar(this->GetScriptContext(), getter);
  198. }
  199. if (setter != nullptr)
  200. {
  201. setter = CrossSite::MarshalVar(this->GetScriptContext(), setter);
  202. }
  203. return __super::SetAccessors(propertyId, getter, setter, flags);
  204. }
  205. template <typename T>
  206. void CrossSiteObject<T>::RemoveFromPrototype(ScriptContext * requestContext)
  207. {
  208. __super::RemoveFromPrototype(this->GetScriptContext());
  209. }
  210. template <typename T>
  211. void CrossSiteObject<T>::AddToPrototype(ScriptContext * requestContext)
  212. {
  213. __super::AddToPrototype(this->GetScriptContext());
  214. }
  215. template <typename T>
  216. void CrossSiteObject<T>::SetPrototype(RecyclableObject* newPrototype)
  217. {
  218. newPrototype = (RecyclableObject*)CrossSite::MarshalVar(this->GetScriptContext(), newPrototype);
  219. __super::SetPrototype(newPrototype);
  220. }
  221. template <typename T>
  222. Var CrossSiteObject<T>::GetHostDispatchVar()
  223. {
  224. Var hostDispatch = __super::GetHostDispatchVar();
  225. AssertMsg(hostDispatch, "hostDispatch");
  226. hostDispatch = CrossSite::MarshalVar(this->GetScriptContext(), hostDispatch);
  227. return hostDispatch;
  228. }
  229. }