mozStorageAsyncStatementParams.cpp (3861B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- 2 * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ : 3 * This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "mozStorageAsyncStatementParams.h" 8 9 #include "nsJSUtils.h" 10 #include "nsString.h" 11 12 #include "jsapi.h" 13 14 #include "mozilla/ErrorResult.h" 15 #include "mozilla/dom/MozStorageAsyncStatementParamsBinding.h" 16 #include "mozStorageAsyncStatement.h" 17 #include "mozStoragePrivateHelpers.h" 18 19 namespace mozilla { 20 namespace storage { 21 22 //////////////////////////////////////////////////////////////////////////////// 23 //// AsyncStatementParams 24 25 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(AsyncStatementParams, mWindow) 26 27 NS_INTERFACE_TABLE_HEAD(AsyncStatementParams) 28 NS_WRAPPERCACHE_INTERFACE_TABLE_ENTRY 29 NS_INTERFACE_TABLE(AsyncStatementParams, nsISupports) 30 NS_INTERFACE_TABLE_TO_MAP_SEGUE_CYCLE_COLLECTION(AsyncStatementParams) 31 NS_INTERFACE_MAP_END 32 33 NS_IMPL_CYCLE_COLLECTING_ADDREF(AsyncStatementParams) 34 NS_IMPL_CYCLE_COLLECTING_RELEASE(AsyncStatementParams) 35 36 AsyncStatementParams::AsyncStatementParams(nsPIDOMWindowInner* aWindow, 37 AsyncStatement* aStatement) 38 : mWindow(aWindow), mStatement(aStatement) { 39 NS_ASSERTION(mStatement != nullptr, "mStatement is null"); 40 } 41 42 JSObject* AsyncStatementParams::WrapObject(JSContext* aCx, 43 JS::Handle<JSObject*> aGivenProto) { 44 return dom::MozStorageAsyncStatementParams_Binding::Wrap(aCx, this, 45 aGivenProto); 46 } 47 48 void AsyncStatementParams::NamedGetter(JSContext* aCx, const nsAString& aName, 49 bool& aFound, 50 JS::MutableHandle<JS::Value> aResult, 51 mozilla::ErrorResult& aRv) { 52 if (!mStatement) { 53 aRv.Throw(NS_ERROR_NOT_INITIALIZED); 54 return; 55 } 56 57 // Unfortunately there's no API that lets us return the parameter value. 58 aFound = false; 59 } 60 61 void AsyncStatementParams::NamedSetter(JSContext* aCx, const nsAString& aName, 62 JS::Handle<JS::Value> aValue, 63 mozilla::ErrorResult& aRv) { 64 if (!mStatement) { 65 aRv.Throw(NS_ERROR_NOT_INITIALIZED); 66 return; 67 } 68 69 NS_ConvertUTF16toUTF8 name(aName); 70 71 nsCOMPtr<nsIVariant> variant(convertJSValToVariant(aCx, aValue)); 72 if (!variant) { 73 aRv.Throw(NS_ERROR_UNEXPECTED); 74 return; 75 } 76 77 aRv = mStatement->BindByName(name, variant); 78 } 79 80 void AsyncStatementParams::GetSupportedNames(nsTArray<nsString>& aNames) { 81 // We don't know how many params there are, so we can't implement this for 82 // AsyncStatementParams. 83 } 84 85 void AsyncStatementParams::IndexedGetter(JSContext* aCx, uint32_t aIndex, 86 bool& aFound, 87 JS::MutableHandle<JS::Value> aResult, 88 mozilla::ErrorResult& aRv) { 89 if (!mStatement) { 90 aRv.Throw(NS_ERROR_NOT_INITIALIZED); 91 return; 92 } 93 94 // Unfortunately there's no API that lets us return the parameter value. 95 aFound = false; 96 } 97 98 void AsyncStatementParams::IndexedSetter(JSContext* aCx, uint32_t aIndex, 99 JS::Handle<JS::Value> aValue, 100 mozilla::ErrorResult& aRv) { 101 if (!mStatement) { 102 aRv.Throw(NS_ERROR_NOT_INITIALIZED); 103 return; 104 } 105 106 nsCOMPtr<nsIVariant> variant(convertJSValToVariant(aCx, aValue)); 107 if (!variant) { 108 aRv.Throw(NS_ERROR_UNEXPECTED); 109 return; 110 } 111 112 aRv = mStatement->BindByIndex(aIndex, variant); 113 } 114 115 } // namespace storage 116 } // namespace mozilla