Tue, 06 Jan 2015 21:39:09 +0100
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: sw=2 ts=2 et lcs=trail\:.,tab\:>~ : |
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 |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "nsIXPConnect.h" |
michael@0 | 8 | #include "mozStorageStatement.h" |
michael@0 | 9 | #include "mozStorageService.h" |
michael@0 | 10 | |
michael@0 | 11 | #include "nsMemory.h" |
michael@0 | 12 | #include "nsString.h" |
michael@0 | 13 | #include "nsServiceManagerUtils.h" |
michael@0 | 14 | |
michael@0 | 15 | #include "mozStorageStatementJSHelper.h" |
michael@0 | 16 | |
michael@0 | 17 | #include "mozStorageStatementRow.h" |
michael@0 | 18 | #include "mozStorageStatementParams.h" |
michael@0 | 19 | |
michael@0 | 20 | #include "jsapi.h" |
michael@0 | 21 | |
michael@0 | 22 | namespace mozilla { |
michael@0 | 23 | namespace storage { |
michael@0 | 24 | |
michael@0 | 25 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 26 | //// Global Functions |
michael@0 | 27 | |
michael@0 | 28 | static |
michael@0 | 29 | bool |
michael@0 | 30 | stepFunc(JSContext *aCtx, |
michael@0 | 31 | uint32_t, |
michael@0 | 32 | jsval *_vp) |
michael@0 | 33 | { |
michael@0 | 34 | nsCOMPtr<nsIXPConnect> xpc(Service::getXPConnect()); |
michael@0 | 35 | nsCOMPtr<nsIXPConnectWrappedNative> wrapper; |
michael@0 | 36 | JSObject *obj = JS_THIS_OBJECT(aCtx, _vp); |
michael@0 | 37 | if (!obj) { |
michael@0 | 38 | return false; |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | nsresult rv = |
michael@0 | 42 | xpc->GetWrappedNativeOfJSObject(aCtx, obj, getter_AddRefs(wrapper)); |
michael@0 | 43 | if (NS_FAILED(rv)) { |
michael@0 | 44 | ::JS_ReportError(aCtx, "mozIStorageStatement::step() could not obtain native statement"); |
michael@0 | 45 | return false; |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | #ifdef DEBUG |
michael@0 | 49 | { |
michael@0 | 50 | nsCOMPtr<mozIStorageStatement> isStatement( |
michael@0 | 51 | do_QueryInterface(wrapper->Native()) |
michael@0 | 52 | ); |
michael@0 | 53 | NS_ASSERTION(isStatement, "How is this not a statement?!"); |
michael@0 | 54 | } |
michael@0 | 55 | #endif |
michael@0 | 56 | |
michael@0 | 57 | Statement *stmt = static_cast<Statement *>( |
michael@0 | 58 | static_cast<mozIStorageStatement *>(wrapper->Native()) |
michael@0 | 59 | ); |
michael@0 | 60 | |
michael@0 | 61 | bool hasMore = false; |
michael@0 | 62 | rv = stmt->ExecuteStep(&hasMore); |
michael@0 | 63 | if (NS_SUCCEEDED(rv) && !hasMore) { |
michael@0 | 64 | *_vp = JSVAL_FALSE; |
michael@0 | 65 | (void)stmt->Reset(); |
michael@0 | 66 | return true; |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | if (NS_FAILED(rv)) { |
michael@0 | 70 | ::JS_ReportError(aCtx, "mozIStorageStatement::step() returned an error"); |
michael@0 | 71 | return false; |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | *_vp = BOOLEAN_TO_JSVAL(hasMore); |
michael@0 | 75 | return true; |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 79 | //// StatementJSHelper |
michael@0 | 80 | |
michael@0 | 81 | nsresult |
michael@0 | 82 | StatementJSHelper::getRow(Statement *aStatement, |
michael@0 | 83 | JSContext *aCtx, |
michael@0 | 84 | JSObject *aScopeObj, |
michael@0 | 85 | jsval *_row) |
michael@0 | 86 | { |
michael@0 | 87 | nsresult rv; |
michael@0 | 88 | |
michael@0 | 89 | #ifdef DEBUG |
michael@0 | 90 | int32_t state; |
michael@0 | 91 | (void)aStatement->GetState(&state); |
michael@0 | 92 | NS_ASSERTION(state == mozIStorageStatement::MOZ_STORAGE_STATEMENT_EXECUTING, |
michael@0 | 93 | "Invalid state to get the row object - all calls will fail!"); |
michael@0 | 94 | #endif |
michael@0 | 95 | |
michael@0 | 96 | if (!aStatement->mStatementRowHolder) { |
michael@0 | 97 | JS::RootedObject scope(aCtx, aScopeObj); |
michael@0 | 98 | nsCOMPtr<mozIStorageStatementRow> row(new StatementRow(aStatement)); |
michael@0 | 99 | NS_ENSURE_TRUE(row, NS_ERROR_OUT_OF_MEMORY); |
michael@0 | 100 | |
michael@0 | 101 | nsCOMPtr<nsIXPConnect> xpc(Service::getXPConnect()); |
michael@0 | 102 | rv = xpc->WrapNative( |
michael@0 | 103 | aCtx, |
michael@0 | 104 | ::JS_GetGlobalForObject(aCtx, scope), |
michael@0 | 105 | row, |
michael@0 | 106 | NS_GET_IID(mozIStorageStatementRow), |
michael@0 | 107 | getter_AddRefs(aStatement->mStatementRowHolder) |
michael@0 | 108 | ); |
michael@0 | 109 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | JS::Rooted<JSObject*> obj(aCtx); |
michael@0 | 113 | obj = aStatement->mStatementRowHolder->GetJSObject(); |
michael@0 | 114 | NS_ENSURE_STATE(obj); |
michael@0 | 115 | |
michael@0 | 116 | *_row = OBJECT_TO_JSVAL(obj); |
michael@0 | 117 | return NS_OK; |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | nsresult |
michael@0 | 121 | StatementJSHelper::getParams(Statement *aStatement, |
michael@0 | 122 | JSContext *aCtx, |
michael@0 | 123 | JSObject *aScopeObj, |
michael@0 | 124 | jsval *_params) |
michael@0 | 125 | { |
michael@0 | 126 | nsresult rv; |
michael@0 | 127 | |
michael@0 | 128 | #ifdef DEBUG |
michael@0 | 129 | int32_t state; |
michael@0 | 130 | (void)aStatement->GetState(&state); |
michael@0 | 131 | NS_ASSERTION(state == mozIStorageStatement::MOZ_STORAGE_STATEMENT_READY, |
michael@0 | 132 | "Invalid state to get the params object - all calls will fail!"); |
michael@0 | 133 | #endif |
michael@0 | 134 | |
michael@0 | 135 | if (!aStatement->mStatementParamsHolder) { |
michael@0 | 136 | JS::RootedObject scope(aCtx, aScopeObj); |
michael@0 | 137 | nsCOMPtr<mozIStorageStatementParams> params = |
michael@0 | 138 | new StatementParams(aStatement); |
michael@0 | 139 | NS_ENSURE_TRUE(params, NS_ERROR_OUT_OF_MEMORY); |
michael@0 | 140 | |
michael@0 | 141 | nsCOMPtr<nsIXPConnect> xpc(Service::getXPConnect()); |
michael@0 | 142 | rv = xpc->WrapNative( |
michael@0 | 143 | aCtx, |
michael@0 | 144 | ::JS_GetGlobalForObject(aCtx, scope), |
michael@0 | 145 | params, |
michael@0 | 146 | NS_GET_IID(mozIStorageStatementParams), |
michael@0 | 147 | getter_AddRefs(aStatement->mStatementParamsHolder) |
michael@0 | 148 | ); |
michael@0 | 149 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | JS::Rooted<JSObject*> obj(aCtx); |
michael@0 | 153 | obj = aStatement->mStatementParamsHolder->GetJSObject(); |
michael@0 | 154 | NS_ENSURE_STATE(obj); |
michael@0 | 155 | |
michael@0 | 156 | *_params = OBJECT_TO_JSVAL(obj); |
michael@0 | 157 | return NS_OK; |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | NS_IMETHODIMP_(MozExternalRefCountType) StatementJSHelper::AddRef() { return 2; } |
michael@0 | 161 | NS_IMETHODIMP_(MozExternalRefCountType) StatementJSHelper::Release() { return 1; } |
michael@0 | 162 | NS_INTERFACE_MAP_BEGIN(StatementJSHelper) |
michael@0 | 163 | NS_INTERFACE_MAP_ENTRY(nsIXPCScriptable) |
michael@0 | 164 | NS_INTERFACE_MAP_ENTRY(nsISupports) |
michael@0 | 165 | NS_INTERFACE_MAP_END |
michael@0 | 166 | |
michael@0 | 167 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 168 | //// nsIXPCScriptable |
michael@0 | 169 | |
michael@0 | 170 | #define XPC_MAP_CLASSNAME StatementJSHelper |
michael@0 | 171 | #define XPC_MAP_QUOTED_CLASSNAME "StatementJSHelper" |
michael@0 | 172 | #define XPC_MAP_WANT_GETPROPERTY |
michael@0 | 173 | #define XPC_MAP_WANT_NEWRESOLVE |
michael@0 | 174 | #define XPC_MAP_FLAGS nsIXPCScriptable::ALLOW_PROP_MODS_DURING_RESOLVE |
michael@0 | 175 | #include "xpc_map_end.h" |
michael@0 | 176 | |
michael@0 | 177 | NS_IMETHODIMP |
michael@0 | 178 | StatementJSHelper::GetProperty(nsIXPConnectWrappedNative *aWrapper, |
michael@0 | 179 | JSContext *aCtx, |
michael@0 | 180 | JSObject *aScopeObj, |
michael@0 | 181 | jsid aId, |
michael@0 | 182 | jsval *_result, |
michael@0 | 183 | bool *_retval) |
michael@0 | 184 | { |
michael@0 | 185 | if (!JSID_IS_STRING(aId)) |
michael@0 | 186 | return NS_OK; |
michael@0 | 187 | |
michael@0 | 188 | JS::Rooted<JSObject*> scope(aCtx, aScopeObj); |
michael@0 | 189 | JS::Rooted<jsid> id(aCtx, aId); |
michael@0 | 190 | |
michael@0 | 191 | #ifdef DEBUG |
michael@0 | 192 | { |
michael@0 | 193 | nsCOMPtr<mozIStorageStatement> isStatement( |
michael@0 | 194 | do_QueryInterface(aWrapper->Native())); |
michael@0 | 195 | NS_ASSERTION(isStatement, "How is this not a statement?!"); |
michael@0 | 196 | } |
michael@0 | 197 | #endif |
michael@0 | 198 | |
michael@0 | 199 | Statement *stmt = static_cast<Statement *>( |
michael@0 | 200 | static_cast<mozIStorageStatement *>(aWrapper->Native()) |
michael@0 | 201 | ); |
michael@0 | 202 | |
michael@0 | 203 | JSFlatString *str = JSID_TO_FLAT_STRING(id); |
michael@0 | 204 | if (::JS_FlatStringEqualsAscii(str, "row")) |
michael@0 | 205 | return getRow(stmt, aCtx, scope, _result); |
michael@0 | 206 | |
michael@0 | 207 | if (::JS_FlatStringEqualsAscii(str, "params")) |
michael@0 | 208 | return getParams(stmt, aCtx, scope, _result); |
michael@0 | 209 | |
michael@0 | 210 | return NS_OK; |
michael@0 | 211 | } |
michael@0 | 212 | |
michael@0 | 213 | |
michael@0 | 214 | NS_IMETHODIMP |
michael@0 | 215 | StatementJSHelper::NewResolve(nsIXPConnectWrappedNative *aWrapper, |
michael@0 | 216 | JSContext *aCtx, |
michael@0 | 217 | JSObject *aScopeObj, |
michael@0 | 218 | jsid aId, |
michael@0 | 219 | JSObject **_objp, |
michael@0 | 220 | bool *_retval) |
michael@0 | 221 | { |
michael@0 | 222 | if (!JSID_IS_STRING(aId)) |
michael@0 | 223 | return NS_OK; |
michael@0 | 224 | |
michael@0 | 225 | JS::RootedObject scope(aCtx, aScopeObj); |
michael@0 | 226 | if (::JS_FlatStringEqualsAscii(JSID_TO_FLAT_STRING(aId), "step")) { |
michael@0 | 227 | *_retval = ::JS_DefineFunction(aCtx, scope, "step", stepFunc, |
michael@0 | 228 | 0, 0) != nullptr; |
michael@0 | 229 | *_objp = scope.get(); |
michael@0 | 230 | return NS_OK; |
michael@0 | 231 | } |
michael@0 | 232 | return NS_OK; |
michael@0 | 233 | } |
michael@0 | 234 | |
michael@0 | 235 | } // namespace storage |
michael@0 | 236 | } // namespace mozilla |