// static nsresult IDBKeyRange::FromJSVal(JSContext* aCx, const jsval& aVal, IDBKeyRange** aKeyRange) { nsRefPtr<IDBKeyRange> keyRange; if (aVal.isNullOrUndefined()) { // undefined and null returns no IDBKeyRange. keyRange.forget(aKeyRange); return NS_OK; } JS::Rooted<JSObject*> obj(aCx, aVal.isObject() ? &aVal.toObject() : nullptr); if (aVal.isPrimitive() || JS_IsArrayObject(aCx, obj) || JS_ObjectIsDate(aCx, obj)) { // A valid key returns an 'only' IDBKeyRange. keyRange = new IDBKeyRange(nullptr, false, false, true); nsresult rv = GetKeyFromJSVal(aCx, aVal, keyRange->Lower()); if (NS_FAILED(rv)) { return rv; } } else { MOZ_ASSERT(aVal.isObject()); // An object is not permitted unless it's another IDBKeyRange. if (NS_FAILED(UNWRAP_OBJECT(IDBKeyRange, obj, keyRange))) { return NS_ERROR_DOM_INDEXEDDB_DATA_ERR; } } keyRange.forget(aKeyRange); return NS_OK; }
bool jsval_array_to_string(JSContext *cx, jsval v, std::string* ret) { JS::RootedObject jsobj(cx); bool ok = v.isObject() && JS_ValueToObject( cx, JS::RootedValue(cx, v), &jsobj ); JSB_PRECONDITION2( ok, cx, false, "Error converting value to object"); JSB_PRECONDITION2( jsobj && JS_IsArrayObject( cx, jsobj), cx, false, "Object must be an array"); uint32_t len; JS_GetArrayLength(cx, jsobj, &len); for( uint32_t i=0; i< len;i++ ) { JS::RootedValue valarg(cx); JS_GetElement(cx, jsobj, i, &valarg); std::string temp; ok = jsval_to_std_string(cx, valarg, &temp); JSB_PRECONDITION2(ok, cx, false, "Error processing arguments"); if(i != len -1) ret->append(temp + ","); else ret->append(temp); } return true; }
JSBool jsval_to_LogEventParamMap(JSContext *cx, jsval v, LogEventParamMap** ret) { JSBool jsret = JS_FALSE; if (v.isObject()) { LogEventParamMap* tmp = new LogEventParamMap(); jsret = jsval_to_TProductInfo(cx, v, tmp); if (jsret) { *ret = tmp; } } return jsret; }
JS_WriteTypedArray(JSStructuredCloneWriter *w, jsval v) { JS_ASSERT(v.isObject()); assertSameCompartment(w->context(), v); RootedObject obj(w->context(), &v.toObject()); // If the object is a security wrapper, see if we're allowed to unwrap it. // If we aren't, throw. if (obj->isWrapper()) obj = CheckedUnwrap(obj); if (!obj) { JS_ReportError(w->context(), "Permission denied to access object"); return false; } return w->writeTypedArray(obj); }
NS_IMETHODIMP SmsManager::Send(const jsval& aNumber, const nsAString& aMessage, jsval* aReturn) { nsresult rv; nsIScriptContext* sc = GetContextForEventHandlers(&rv); NS_ENSURE_STATE(sc); AutoPushJSContext cx(sc->GetNativeContext()); NS_ASSERTION(cx, "Failed to get a context!"); if (!aNumber.isString() && !(aNumber.isObject() && JS_IsArrayObject(cx, &aNumber.toObject()))) { return NS_ERROR_INVALID_ARG; } JSObject* global = sc->GetNativeGlobal(); NS_ASSERTION(global, "Failed to get global object!"); JSAutoRequest ar(cx); JSAutoCompartment ac(cx, global); if (aNumber.isString()) { return Send(cx, global, aNumber.toString(), aMessage, aReturn); } // Must be an array then. JSObject& numbers = aNumber.toObject(); uint32_t size; JS_ALWAYS_TRUE(JS_GetArrayLength(cx, &numbers, &size)); jsval* requests = new jsval[size]; for (uint32_t i=0; i<size; ++i) { jsval number; if (!JS_GetElement(cx, &numbers, i, &number)) { return NS_ERROR_INVALID_ARG; } nsresult rv = Send(cx, global, number.toString(), aMessage, &requests[i]); NS_ENSURE_SUCCESS(rv, rv); } aReturn->setObjectOrNull(JS_NewArrayObject(cx, size, requests)); NS_ENSURE_TRUE(aReturn->isObject(), NS_ERROR_FAILURE); return NS_OK; }
NS_IMETHODIMP SmsRequest::NotifyThreadList(const jsval& aThreadList, JSContext* aCx) { MOZ_ASSERT(aThreadList.isObject()); if (mParent) { JSObject* array = const_cast<JSObject*>(&aThreadList.toObject()); uint32_t length; bool ok = JS_GetArrayLength(aCx, array, &length); NS_ENSURE_TRUE(ok, NS_ERROR_FAILURE); ReplyThreadList reply; InfallibleTArray<ThreadListItem>& ipcItems = reply.items(); if (length) { ipcItems.SetCapacity(length); for (uint32_t i = 0; i < length; i++) { jsval arrayEntry; ok = JS_GetElement(aCx, array, i, &arrayEntry); NS_ENSURE_TRUE(ok, NS_ERROR_FAILURE); MOZ_ASSERT(arrayEntry.isObject()); mozilla::idl::SmsThreadListItem item; nsresult rv = item.Init(aCx, &arrayEntry); NS_ENSURE_SUCCESS(rv, rv); ThreadListItem* ipcItem = ipcItems.AppendElement(); ipcItem->senderOrReceiver() = item.senderOrReceiver; ipcItem->timestamp() = item.timestamp; ipcItem->body() = item.body; ipcItem->unreadCount() = item.unreadCount; } } return SendMessageReply(reply); } return NotifySuccess(aThreadList); }
NS_IMETHODIMP SmsManager::Delete(const jsval& aParam, nsIDOMMozSmsRequest** aRequest) { if (aParam.isInt32()) { return Delete(aParam.toInt32(), aRequest); } if (!aParam.isObject()) { return NS_ERROR_INVALID_ARG; } nsresult rv; nsIScriptContext* sc = GetContextForEventHandlers(&rv); AutoPushJSContext cx(sc->GetNativeContext()); NS_ENSURE_STATE(sc); nsCOMPtr<nsIDOMMozSmsMessage> message = do_QueryInterface(nsContentUtils::XPConnect()->GetNativeOfWrapper(cx, &aParam.toObject())); NS_ENSURE_TRUE(message, NS_ERROR_INVALID_ARG); int32_t id; message->GetId(&id); return Delete(id, aRequest); }
NS_IMETHODIMP SmsManager::Send(const jsval& aNumber, const nsAString& aMessage, jsval* aReturn) { nsresult rv; nsIScriptContext* sc = GetContextForEventHandlers(&rv); NS_ENSURE_STATE(sc); JSContext* cx = sc->GetNativeContext(); NS_ASSERTION(cx, "Failed to get a context!"); if (!aNumber.isString() && !(aNumber.isObject() && JS_IsArrayObject(cx, &aNumber.toObject()))) { return NS_ERROR_INVALID_ARG; } JSObject* global = sc->GetNativeGlobal(); NS_ASSERTION(global, "Failed to get global object!"); JSAutoRequest ar(cx); JSAutoCompartment ac(cx, global); if (aNumber.isString()) { return Send(cx, global, aNumber.toString(), aMessage, aReturn); } // Must be an object then. if (!aNumber.isObject()) { return NS_ERROR_FAILURE; } JSObject& numbers = aNumber.toObject(); uint32_t size; if (!JS_GetArrayLength(cx, &numbers, &size)) { return NS_ERROR_FAILURE; } JS::AutoValueVector requests(cx); if (!requests.resize(size)) { return NS_ERROR_FAILURE; } JSString *str; for (uint32_t i = 0; i < size; ++i) { jsval number; if (!JS_GetElement(cx, &numbers, i, &number)) { return NS_ERROR_INVALID_ARG; } str = JS_ValueToString(cx, number); if (!str) { return NS_ERROR_FAILURE; } nsresult rv = Send(cx, global, str, aMessage, &requests[i]); NS_ENSURE_SUCCESS(rv, rv); } JSObject* obj = JS_NewArrayObject(cx, requests.length(), requests.begin()); if (!obj) { return NS_ERROR_FAILURE; } aReturn->setObject(*obj); return NS_OK; }