dom/bindings/ToJSValue.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/
michael@0 2 /* vim: set ts=2 sw=2 et tw=79: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #ifndef mozilla_dom_ToJSValue_h
michael@0 8 #define mozilla_dom_ToJSValue_h
michael@0 9
michael@0 10 #include "mozilla/TypeTraits.h"
michael@0 11 #include "mozilla/Assertions.h"
michael@0 12 #include "mozilla/dom/BindingUtils.h"
michael@0 13 #include "mozilla/dom/TypedArray.h"
michael@0 14 #include "jsapi.h"
michael@0 15 #include "nsISupports.h"
michael@0 16 #include "nsTArray.h"
michael@0 17 #include "nsWrapperCache.h"
michael@0 18
michael@0 19 namespace mozilla {
michael@0 20 namespace dom {
michael@0 21
michael@0 22 // If ToJSValue returns false, it must set an exception on the
michael@0 23 // JSContext.
michael@0 24
michael@0 25 // Accept strings.
michael@0 26 bool
michael@0 27 ToJSValue(JSContext* aCx,
michael@0 28 const nsAString& aArgument,
michael@0 29 JS::MutableHandle<JS::Value> aValue);
michael@0 30
michael@0 31 // Accept booleans.
michael@0 32 inline bool
michael@0 33 ToJSValue(JSContext* aCx,
michael@0 34 bool aArgument,
michael@0 35 JS::MutableHandle<JS::Value> aValue)
michael@0 36 {
michael@0 37 // Make sure we're called in a compartment
michael@0 38 MOZ_ASSERT(JS::CurrentGlobalOrNull(aCx));
michael@0 39
michael@0 40 aValue.setBoolean(aArgument);
michael@0 41 return true;
michael@0 42 }
michael@0 43
michael@0 44 // Accept integer types
michael@0 45 inline bool
michael@0 46 ToJSValue(JSContext* aCx,
michael@0 47 int32_t aArgument,
michael@0 48 JS::MutableHandle<JS::Value> aValue)
michael@0 49 {
michael@0 50 // Make sure we're called in a compartment
michael@0 51 MOZ_ASSERT(JS::CurrentGlobalOrNull(aCx));
michael@0 52
michael@0 53 aValue.setInt32(aArgument);
michael@0 54 return true;
michael@0 55 }
michael@0 56
michael@0 57 // The uint32_t version is disabled for now because on the super-old b2g
michael@0 58 // compiler nsresult and uint32_t are the same type. If someone needs this at
michael@0 59 // some point we'll need to figure out how to make it work (e.g. by switching to
michael@0 60 // traits structs and using the trick IPC's ParamTraits uses, where a traits
michael@0 61 // struct templated on the type inherits from a base traits struct of some sort,
michael@0 62 // templated on the same type, or something). Maybe b2g will update to a modern
michael@0 63 // compiler before that happens....
michael@0 64 #if 0
michael@0 65 inline bool
michael@0 66 ToJSValue(JSContext* aCx,
michael@0 67 uint32_t aArgument,
michael@0 68 JS::MutableHandle<JS::Value> aValue)
michael@0 69 {
michael@0 70 // Make sure we're called in a compartment
michael@0 71 MOZ_ASSERT(JS::CurrentGlobalOrNull(aCx));
michael@0 72
michael@0 73 aValue.setNumber(aArgument);
michael@0 74 return true;
michael@0 75 }
michael@0 76 #endif
michael@0 77
michael@0 78 inline bool
michael@0 79 ToJSValue(JSContext* aCx,
michael@0 80 int64_t aArgument,
michael@0 81 JS::MutableHandle<JS::Value> aValue)
michael@0 82 {
michael@0 83 // Make sure we're called in a compartment
michael@0 84 MOZ_ASSERT(JS::CurrentGlobalOrNull(aCx));
michael@0 85
michael@0 86 aValue.setNumber(double(aArgument));
michael@0 87 return true;
michael@0 88 }
michael@0 89
michael@0 90 inline bool
michael@0 91 ToJSValue(JSContext* aCx,
michael@0 92 uint64_t aArgument,
michael@0 93 JS::MutableHandle<JS::Value> aValue)
michael@0 94 {
michael@0 95 // Make sure we're called in a compartment
michael@0 96 MOZ_ASSERT(JS::CurrentGlobalOrNull(aCx));
michael@0 97
michael@0 98 aValue.setNumber(double(aArgument));
michael@0 99 return true;
michael@0 100 }
michael@0 101
michael@0 102 // accept floating point types
michael@0 103 inline bool
michael@0 104 ToJSValue(JSContext* aCx,
michael@0 105 float aArgument,
michael@0 106 JS::MutableHandle<JS::Value> aValue)
michael@0 107 {
michael@0 108 // Make sure we're called in a compartment
michael@0 109 MOZ_ASSERT(JS::CurrentGlobalOrNull(aCx));
michael@0 110
michael@0 111 aValue.setNumber(aArgument);
michael@0 112 return true;
michael@0 113 }
michael@0 114
michael@0 115 inline bool
michael@0 116 ToJSValue(JSContext* aCx,
michael@0 117 double aArgument,
michael@0 118 JS::MutableHandle<JS::Value> aValue)
michael@0 119 {
michael@0 120 // Make sure we're called in a compartment
michael@0 121 MOZ_ASSERT(JS::CurrentGlobalOrNull(aCx));
michael@0 122
michael@0 123 aValue.setNumber(aArgument);
michael@0 124 return true;
michael@0 125 }
michael@0 126
michael@0 127 // Accept objects that inherit from nsWrapperCache and nsISupports (e.g. most
michael@0 128 // DOM objects).
michael@0 129 template <class T>
michael@0 130 typename EnableIf<IsBaseOf<nsWrapperCache, T>::value &&
michael@0 131 IsBaseOf<nsISupports, T>::value, bool>::Type
michael@0 132 ToJSValue(JSContext* aCx,
michael@0 133 T& aArgument,
michael@0 134 JS::MutableHandle<JS::Value> aValue)
michael@0 135 {
michael@0 136 // Make sure we're called in a compartment
michael@0 137 MOZ_ASSERT(JS::CurrentGlobalOrNull(aCx));
michael@0 138 // Make sure non-webidl objects don't sneak in here
michael@0 139 MOZ_ASSERT(aArgument.IsDOMBinding());
michael@0 140
michael@0 141 return WrapNewBindingObject(aCx, aArgument, aValue);
michael@0 142 }
michael@0 143
michael@0 144 // Accept typed arrays built from appropriate nsTArray values
michael@0 145 template<typename T>
michael@0 146 typename EnableIf<IsBaseOf<AllTypedArraysBase, T>::value, bool>::Type
michael@0 147 ToJSValue(JSContext* aCx,
michael@0 148 const TypedArrayCreator<T>& aArgument,
michael@0 149 JS::MutableHandle<JS::Value> aValue)
michael@0 150 {
michael@0 151 // Make sure we're called in a compartment
michael@0 152 MOZ_ASSERT(JS::CurrentGlobalOrNull(aCx));
michael@0 153
michael@0 154 JSObject* obj = aArgument.Create(aCx);
michael@0 155 if (!obj) {
michael@0 156 return false;
michael@0 157 }
michael@0 158 aValue.setObject(*obj);
michael@0 159 return true;
michael@0 160 }
michael@0 161
michael@0 162 // We don't want to include nsContentUtils here, so use a helper
michael@0 163 // function for the nsISupports case.
michael@0 164 namespace tojsvalue_detail {
michael@0 165 bool
michael@0 166 ISupportsToJSValue(JSContext* aCx,
michael@0 167 nsISupports* aArgument,
michael@0 168 JS::MutableHandle<JS::Value> aValue);
michael@0 169 } // namespace tojsvalue_detail
michael@0 170
michael@0 171 // Accept objects that inherit from nsISupports but not nsWrapperCache (e.g.
michael@0 172 // nsIDOMFile).
michael@0 173 template <class T>
michael@0 174 typename EnableIf<!IsBaseOf<nsWrapperCache, T>::value &&
michael@0 175 IsBaseOf<nsISupports, T>::value, bool>::Type
michael@0 176 ToJSValue(JSContext* aCx,
michael@0 177 T& aArgument,
michael@0 178 JS::MutableHandle<JS::Value> aValue)
michael@0 179 {
michael@0 180 // Make sure we're called in a compartment
michael@0 181 MOZ_ASSERT(JS::CurrentGlobalOrNull(aCx));
michael@0 182
michael@0 183 return tojsvalue_detail::ISupportsToJSValue(aCx, &aArgument, aValue);
michael@0 184 }
michael@0 185
michael@0 186 // Accept nsRefPtr/nsCOMPtr
michael@0 187 template <typename T>
michael@0 188 bool
michael@0 189 ToJSValue(JSContext* aCx,
michael@0 190 const nsCOMPtr<T>& aArgument,
michael@0 191 JS::MutableHandle<JS::Value> aValue)
michael@0 192 {
michael@0 193 return ToJSValue(aCx, *aArgument.get(), aValue);
michael@0 194 }
michael@0 195
michael@0 196 template <typename T>
michael@0 197 bool
michael@0 198 ToJSValue(JSContext* aCx,
michael@0 199 const nsRefPtr<T>& aArgument,
michael@0 200 JS::MutableHandle<JS::Value> aValue)
michael@0 201 {
michael@0 202 return ToJSValue(aCx, *aArgument.get(), aValue);
michael@0 203 }
michael@0 204
michael@0 205 // Accept WebIDL dictionaries
michael@0 206 template <class T>
michael@0 207 typename EnableIf<IsBaseOf<DictionaryBase, T>::value, bool>::Type
michael@0 208 ToJSValue(JSContext* aCx,
michael@0 209 const T& aArgument,
michael@0 210 JS::MutableHandle<JS::Value> aValue)
michael@0 211 {
michael@0 212 return aArgument.ToObject(aCx, aValue);
michael@0 213 }
michael@0 214
michael@0 215 // Accept existing JS values (which may not be same-compartment with us
michael@0 216 inline bool
michael@0 217 ToJSValue(JSContext* aCx, JS::Handle<JS::Value> aArgument,
michael@0 218 JS::MutableHandle<JS::Value> aValue)
michael@0 219 {
michael@0 220 aValue.set(aArgument);
michael@0 221 return MaybeWrapValue(aCx, aValue);
michael@0 222 }
michael@0 223
michael@0 224 // Accept nsresult, for use in rejections, and create an XPCOM
michael@0 225 // exception object representing that nsresult.
michael@0 226 bool
michael@0 227 ToJSValue(JSContext* aCx,
michael@0 228 nsresult aArgument,
michael@0 229 JS::MutableHandle<JS::Value> aValue);
michael@0 230
michael@0 231 // Accept arrays of other things we accept
michael@0 232 template <typename T>
michael@0 233 bool
michael@0 234 ToJSValue(JSContext* aCx,
michael@0 235 T* aArguments,
michael@0 236 size_t aLength,
michael@0 237 JS::MutableHandle<JS::Value> aValue)
michael@0 238 {
michael@0 239 // Make sure we're called in a compartment
michael@0 240 MOZ_ASSERT(JS::CurrentGlobalOrNull(aCx));
michael@0 241
michael@0 242 JS::AutoValueVector v(aCx);
michael@0 243 if (!v.resize(aLength)) {
michael@0 244 return false;
michael@0 245 }
michael@0 246 for (size_t i = 0; i < aLength; ++i) {
michael@0 247 if (!ToJSValue(aCx, aArguments[i], v.handleAt(i))) {
michael@0 248 return false;
michael@0 249 }
michael@0 250 }
michael@0 251 JSObject* arrayObj = JS_NewArrayObject(aCx, v);
michael@0 252 if (!arrayObj) {
michael@0 253 return false;
michael@0 254 }
michael@0 255 aValue.setObject(*arrayObj);
michael@0 256 return true;
michael@0 257 }
michael@0 258
michael@0 259 template <typename T>
michael@0 260 bool
michael@0 261 ToJSValue(JSContext* aCx,
michael@0 262 const nsTArray<T>& aArgument,
michael@0 263 JS::MutableHandle<JS::Value> aValue)
michael@0 264 {
michael@0 265 return ToJSValue(aCx, aArgument.Elements(),
michael@0 266 aArgument.Length(), aValue);
michael@0 267 }
michael@0 268
michael@0 269 template <typename T>
michael@0 270 bool
michael@0 271 ToJSValue(JSContext* aCx,
michael@0 272 const FallibleTArray<T>& aArgument,
michael@0 273 JS::MutableHandle<JS::Value> aValue)
michael@0 274 {
michael@0 275 return ToJSValue(aCx, aArgument.Elements(),
michael@0 276 aArgument.Length(), aValue);
michael@0 277 }
michael@0 278
michael@0 279 template <typename T, int N>
michael@0 280 bool
michael@0 281 ToJSValue(JSContext* aCx,
michael@0 282 const T(&aArgument)[N],
michael@0 283 JS::MutableHandle<JS::Value> aValue)
michael@0 284 {
michael@0 285 return ToJSValue(aCx, aArgument, N, aValue);
michael@0 286 }
michael@0 287
michael@0 288 } // namespace dom
michael@0 289 } // namespace mozilla
michael@0 290
michael@0 291 #endif /* mozilla_dom_ToJSValue_h */

mercurial