From e6cae211c443f58d34896ea652a23c221b9513f2 Mon Sep 17 00:00:00 2001 From: Simon Giesecke Date: Thu, 5 Mar 2020 16:51:14 +0000 Subject: [PATCH] Bug 1603703 - Part 1: Change mozIStorageAsyncConnection::CreateFunction to accept a nsCOMPtr rather than a raw pointer. r=asuth,mak Differential Revision: https://phabricator.services.mozilla.com/D57089 --HG-- extra : moz-landing-system : lando --- dom/cache/Connection.cpp | 2 +- dom/indexedDB/ActorsParent.cpp | 30 +++----- netwerk/cache/nsDiskCacheDeviceSQL.cpp | 6 +- netwerk/cookie/nsCookieService.cpp | 18 ++--- storage/mozIStorageAsyncConnection.idl | 11 ++- storage/mozStorageConnection.cpp | 10 ++- toolkit/components/places/SQLFunctions.cpp | 86 ++++++++++------------ 7 files changed, 74 insertions(+), 89 deletions(-) diff --git a/dom/cache/Connection.cpp b/dom/cache/Connection.cpp index 03cc341dce8d..e093c08b09c6 100644 --- a/dom/cache/Connection.cpp +++ b/dom/cache/Connection.cpp @@ -100,7 +100,7 @@ Connection::ExecuteSimpleSQLAsync(const nsACString&, NS_IMETHODIMP Connection::CreateFunction(const nsACString& aFunctionName, int32_t aNumArguments, - mozIStorageFunction* aFunction) { + nsCOMPtr aFunction) { // async methods are not supported return NS_ERROR_NOT_IMPLEMENTED; } diff --git a/dom/indexedDB/ActorsParent.cpp b/dom/indexedDB/ActorsParent.cpp index fff193016a84..37b602eb7dee 100644 --- a/dom/indexedDB/ActorsParent.cpp +++ b/dom/indexedDB/ActorsParent.cpp @@ -2847,23 +2847,19 @@ nsresult UpgradeSchemaFrom17_0To18_0Helper::DoUpgrade( MOZ_ASSERT(!aOrigin.IsEmpty()); // Register the |upgrade_key| function. - RefPtr updateFunction = new UpgradeKeyFunction(); - NS_NAMED_LITERAL_CSTRING(upgradeKeyFunctionName, "upgrade_key"); - nsresult rv = - aConnection->CreateFunction(upgradeKeyFunctionName, 1, updateFunction); + nsresult rv = aConnection->CreateFunction( + upgradeKeyFunctionName, 1, MakeAndAddRef()); if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } // Register the |insert_idv| function. - RefPtr insertIDVFunction = - new InsertIndexDataValuesFunction(); - NS_NAMED_LITERAL_CSTRING(insertIDVFunctionName, "insert_idv"); - rv = aConnection->CreateFunction(insertIDVFunctionName, 4, insertIDVFunction); + rv = aConnection->CreateFunction( + insertIDVFunctionName, 4, MakeAndAddRef()); if (NS_WARN_IF(NS_FAILED(rv))) { MOZ_ALWAYS_SUCCEEDS(aConnection->RemoveFunction(upgradeKeyFunctionName)); return rv; @@ -3494,7 +3490,7 @@ nsresult UpgradeSchemaFrom19_0To20_0(nsIFile* aFMDirectory, NS_NAMED_LITERAL_CSTRING(functionName, "upgrade"); - rv = aConnection->CreateFunction(functionName, 2, function); + rv = aConnection->CreateFunction(functionName, 2, std::move(function)); if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } @@ -3701,12 +3697,10 @@ nsresult UpgradeSchemaFrom20_0To21_0(mozIStorageConnection* aConnection) { AUTO_PROFILER_LABEL("UpgradeSchemaFrom20_0To21_0", DOM); - RefPtr function = - new UpgradeIndexDataValuesFunction(); - NS_NAMED_LITERAL_CSTRING(functionName, "upgrade_idv"); - nsresult rv = aConnection->CreateFunction(functionName, 1, function); + nsresult rv = aConnection->CreateFunction( + functionName, 1, MakeAndAddRef()); if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } @@ -10457,7 +10451,7 @@ nsresult DatabaseConnection::BeginWriteTransaction() { rv = (*mStorageConnection) ->CreateFunction(NS_LITERAL_CSTRING("update_refcount"), - /* aNumArguments */ 2, function); + /* aNumArguments */ 2, do_AddRef(function)); if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } @@ -23870,13 +23864,11 @@ nsresult CreateIndexOp::InsertDataFromObjectStore( aConnection->GetStorageConnection(); MOZ_ASSERT(storageConnection); - RefPtr updateFunction = - new UpdateIndexDataValuesFunction(this, aConnection); - NS_NAMED_LITERAL_CSTRING(updateFunctionName, "update_index_data_values"); - nsresult rv = - storageConnection->CreateFunction(updateFunctionName, 4, updateFunction); + nsresult rv = storageConnection->CreateFunction( + updateFunctionName, 4, + MakeAndAddRef(this, aConnection)); if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } diff --git a/netwerk/cache/nsDiskCacheDeviceSQL.cpp b/netwerk/cache/nsDiskCacheDeviceSQL.cpp index fd9e6d067915..19dfc18d6cbf 100644 --- a/netwerk/cache/nsDiskCacheDeviceSQL.cpp +++ b/netwerk/cache/nsDiskCacheDeviceSQL.cpp @@ -1164,7 +1164,7 @@ nsresult nsOfflineCacheDevice::InitWithSqlite(mozIStorageService* ss) { if (!mEvictionFunction) return NS_ERROR_OUT_OF_MEMORY; rv = mDB->CreateFunction(NS_LITERAL_CSTRING("cache_eviction_observer"), 3, - mEvictionFunction); + do_AddRef(mEvictionFunction)); NS_ENSURE_SUCCESS(rv, rv); // create all (most) of our statements up front @@ -2337,8 +2337,8 @@ nsresult nsOfflineCacheDevice::Evict( nsresult rv; - nsCOMPtr function1(new OriginMatch(aPattern)); - rv = mDB->CreateFunction(NS_LITERAL_CSTRING("ORIGIN_MATCH"), 1, function1); + rv = mDB->CreateFunction(NS_LITERAL_CSTRING("ORIGIN_MATCH"), 1, + MakeAndAddRef(aPattern)); NS_ENSURE_SUCCESS(rv, rv); class AutoRemoveFunc { diff --git a/netwerk/cookie/nsCookieService.cpp b/netwerk/cookie/nsCookieService.cpp index 0f5050c167c3..bd7e22950064 100644 --- a/netwerk/cookie/nsCookieService.cpp +++ b/netwerk/cookie/nsCookieService.cpp @@ -1161,24 +1161,18 @@ OpenDBResult nsCookieService::TryInitDB(bool aRecreateDB) { // Compute and populate the values of appId and inBrwoserElement from // originAttributes. - nsCOMPtr setAppId( - new SetAppIdFromOriginAttributesSQLFunction()); - NS_ENSURE_TRUE(setAppId, RESULT_RETRY); - NS_NAMED_LITERAL_CSTRING(setAppIdName, "SET_APP_ID"); - rv = mDefaultDBState->syncConn->CreateFunction(setAppIdName, 1, - setAppId); + rv = mDefaultDBState->syncConn->CreateFunction( + setAppIdName, 1, + MakeAndAddRef()); NS_ENSURE_SUCCESS(rv, RESULT_RETRY); - nsCOMPtr setInBrowser( - new SetInBrowserFromOriginAttributesSQLFunction()); - NS_ENSURE_TRUE(setInBrowser, RESULT_RETRY); - NS_NAMED_LITERAL_CSTRING(setInBrowserName, "SET_IN_BROWSER"); - rv = mDefaultDBState->syncConn->CreateFunction(setInBrowserName, 1, - setInBrowser); + rv = mDefaultDBState->syncConn->CreateFunction( + setInBrowserName, 1, + MakeAndAddRef()); NS_ENSURE_SUCCESS(rv, RESULT_RETRY); rv = mDefaultDBState->syncConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING( diff --git a/storage/mozIStorageAsyncConnection.idl b/storage/mozIStorageAsyncConnection.idl index 6c1396198c54..19e6e29da6de 100644 --- a/storage/mozIStorageAsyncConnection.idl +++ b/storage/mozIStorageAsyncConnection.idl @@ -5,6 +5,12 @@ #include "nsISupports.idl" +%{C++ +#include "nsCOMPtr.h" +%} + +native nsCOMPtr_mozIStorageFunction(nsCOMPtr); + interface mozIStorageAggregateFunction; interface mozIStorageCompletionCallback; interface mozIStorageFunction; @@ -265,11 +271,12 @@ interface mozIStorageAsyncConnection : nsISupports { * variable-argument functions. * @param aFunction * The instance of mozIStorageFunction, which implements the function - * in question. + * in question. Takes ownership of the passed function pointer, and + * discards it in case of an error. */ void createFunction(in AUTF8String aFunctionName, in long aNumArguments, - in mozIStorageFunction aFunction); + in nsCOMPtr_mozIStorageFunction aFunction); /** * Create a new SQL aggregate function. If you use your connection on diff --git a/storage/mozStorageConnection.cpp b/storage/mozStorageConnection.cpp index 8286cecfdd1a..b5446d0a6896 100644 --- a/storage/mozStorageConnection.cpp +++ b/storage/mozStorageConnection.cpp @@ -2057,7 +2057,7 @@ Connection::CreateTable(const char* aTableName, const char* aTableSchema) { NS_IMETHODIMP Connection::CreateFunction(const nsACString& aFunctionName, int32_t aNumArguments, - mozIStorageFunction* aFunction) { + nsCOMPtr aFunction) { if (!connectionReady()) { return NS_ERROR_NOT_INITIALIZED; } @@ -2076,8 +2076,12 @@ Connection::CreateFunction(const nsACString& aFunctionName, SQLITE_ANY, aFunction, basicFunctionHelper, nullptr, nullptr); if (srv != SQLITE_OK) return convertResultCode(srv); - FunctionInfo info = {aFunction, Connection::FunctionInfo::SIMPLE, - aNumArguments}; + // TODO (Bug 1620198): we can't use std::move(aFunction) here because + // FunctionInfo::function is a nsCOMPtr to allow for either + // mozIStorageFunction or mozIStorageAggregateFunction. When + // mozIStorageAggregateFunction is removed, this can be changed. + FunctionInfo info = {aFunction.forget().take(), + Connection::FunctionInfo::SIMPLE, aNumArguments}; mFunctions.Put(aFunctionName, info); return NS_OK; diff --git a/toolkit/components/places/SQLFunctions.cpp b/toolkit/components/places/SQLFunctions.cpp index 4b8ab7e71908..59cb0d749ae5 100644 --- a/toolkit/components/places/SQLFunctions.cpp +++ b/toolkit/components/places/SQLFunctions.cpp @@ -370,10 +370,9 @@ namespace places { /* static */ nsresult MatchAutoCompleteFunction::create(mozIStorageConnection* aDBConn) { - RefPtr function = new MatchAutoCompleteFunction(); - nsresult rv = aDBConn->CreateFunction( - NS_LITERAL_CSTRING("autocomplete_match"), kArgIndexLength, function); + NS_LITERAL_CSTRING("autocomplete_match"), kArgIndexLength, + MakeAndAddRef()); NS_ENSURE_SUCCESS(rv, rv); return NS_OK; @@ -549,10 +548,9 @@ MatchAutoCompleteFunction::OnFunctionCall(mozIStorageValueArray* aArguments, /* static */ nsresult CalculateFrecencyFunction::create(mozIStorageConnection* aDBConn) { - RefPtr function = new CalculateFrecencyFunction(); - - nsresult rv = aDBConn->CreateFunction( - NS_LITERAL_CSTRING("calculate_frecency"), -1, function); + nsresult rv = + aDBConn->CreateFunction(NS_LITERAL_CSTRING("calculate_frecency"), -1, + MakeAndAddRef()); NS_ENSURE_SUCCESS(rv, rv); return NS_OK; @@ -760,9 +758,8 @@ CalculateFrecencyFunction::OnFunctionCall(mozIStorageValueArray* aArguments, /* static */ nsresult GenerateGUIDFunction::create(mozIStorageConnection* aDBConn) { - RefPtr function = new GenerateGUIDFunction(); - nsresult rv = - aDBConn->CreateFunction(NS_LITERAL_CSTRING("generate_guid"), 0, function); + nsresult rv = aDBConn->CreateFunction(NS_LITERAL_CSTRING("generate_guid"), 0, + MakeAndAddRef()); NS_ENSURE_SUCCESS(rv, rv); return NS_OK; @@ -786,9 +783,8 @@ GenerateGUIDFunction::OnFunctionCall(mozIStorageValueArray* aArguments, /* static */ nsresult IsValidGUIDFunction::create(mozIStorageConnection* aDBConn) { - RefPtr function = new IsValidGUIDFunction(); return aDBConn->CreateFunction(NS_LITERAL_CSTRING("is_valid_guid"), 1, - function); + MakeAndAddRef()); } NS_IMPL_ISUPPORTS(IsValidGUIDFunction, mozIStorageFunction) @@ -813,9 +809,9 @@ IsValidGUIDFunction::OnFunctionCall(mozIStorageValueArray* aArguments, /* static */ nsresult GetUnreversedHostFunction::create(mozIStorageConnection* aDBConn) { - RefPtr function = new GetUnreversedHostFunction(); - nsresult rv = aDBConn->CreateFunction( - NS_LITERAL_CSTRING("get_unreversed_host"), 1, function); + nsresult rv = + aDBConn->CreateFunction(NS_LITERAL_CSTRING("get_unreversed_host"), 1, + MakeAndAddRef()); NS_ENSURE_SUCCESS(rv, rv); return NS_OK; @@ -851,9 +847,8 @@ GetUnreversedHostFunction::OnFunctionCall(mozIStorageValueArray* aArguments, /* static */ nsresult FixupURLFunction::create(mozIStorageConnection* aDBConn) { - RefPtr function = new FixupURLFunction(); - nsresult rv = - aDBConn->CreateFunction(NS_LITERAL_CSTRING("fixup_url"), 1, function); + nsresult rv = aDBConn->CreateFunction(NS_LITERAL_CSTRING("fixup_url"), 1, + MakeAndAddRef()); NS_ENSURE_SUCCESS(rv, rv); return NS_OK; @@ -894,10 +889,9 @@ FixupURLFunction::OnFunctionCall(mozIStorageValueArray* aArguments, /* static */ nsresult FrecencyNotificationFunction::create(mozIStorageConnection* aDBConn) { - RefPtr function = - new FrecencyNotificationFunction(); - nsresult rv = aDBConn->CreateFunction(NS_LITERAL_CSTRING("notify_frecency"), - 5, function); + nsresult rv = + aDBConn->CreateFunction(NS_LITERAL_CSTRING("notify_frecency"), 5, + MakeAndAddRef()); NS_ENSURE_SUCCESS(rv, rv); return NS_OK; @@ -943,10 +937,9 @@ FrecencyNotificationFunction::OnFunctionCall(mozIStorageValueArray* aArgs, /* static */ nsresult StoreLastInsertedIdFunction::create(mozIStorageConnection* aDBConn) { - RefPtr function = - new StoreLastInsertedIdFunction(); - nsresult rv = aDBConn->CreateFunction( - NS_LITERAL_CSTRING("store_last_inserted_id"), 2, function); + nsresult rv = + aDBConn->CreateFunction(NS_LITERAL_CSTRING("store_last_inserted_id"), 2, + MakeAndAddRef()); NS_ENSURE_SUCCESS(rv, rv); return NS_OK; @@ -993,9 +986,8 @@ StoreLastInsertedIdFunction::OnFunctionCall(mozIStorageValueArray* aArgs, /* static */ nsresult GetQueryParamFunction::create(mozIStorageConnection* aDBConn) { - RefPtr function = new GetQueryParamFunction(); return aDBConn->CreateFunction(NS_LITERAL_CSTRING("get_query_param"), 2, - function); + MakeAndAddRef()); } NS_IMPL_ISUPPORTS(GetQueryParamFunction, mozIStorageFunction) @@ -1024,8 +1016,8 @@ GetQueryParamFunction::OnFunctionCall(mozIStorageValueArray* aArguments, /* static */ nsresult HashFunction::create(mozIStorageConnection* aDBConn) { - RefPtr function = new HashFunction(); - return aDBConn->CreateFunction(NS_LITERAL_CSTRING("hash"), -1, function); + return aDBConn->CreateFunction(NS_LITERAL_CSTRING("hash"), -1, + MakeAndAddRef()); } NS_IMPL_ISUPPORTS(HashFunction, mozIStorageFunction) @@ -1064,9 +1056,8 @@ HashFunction::OnFunctionCall(mozIStorageValueArray* aArguments, /* static */ nsresult GetPrefixFunction::create(mozIStorageConnection* aDBConn) { - RefPtr function = new GetPrefixFunction(); - nsresult rv = - aDBConn->CreateFunction(NS_LITERAL_CSTRING("get_prefix"), 1, function); + nsresult rv = aDBConn->CreateFunction(NS_LITERAL_CSTRING("get_prefix"), 1, + MakeAndAddRef()); NS_ENSURE_SUCCESS(rv, rv); return NS_OK; @@ -1097,9 +1088,9 @@ GetPrefixFunction::OnFunctionCall(mozIStorageValueArray* aArgs, /* static */ nsresult GetHostAndPortFunction::create(mozIStorageConnection* aDBConn) { - RefPtr function = new GetHostAndPortFunction(); - nsresult rv = aDBConn->CreateFunction(NS_LITERAL_CSTRING("get_host_and_port"), - 1, function); + nsresult rv = + aDBConn->CreateFunction(NS_LITERAL_CSTRING("get_host_and_port"), 1, + MakeAndAddRef()); NS_ENSURE_SUCCESS(rv, rv); return NS_OK; @@ -1134,10 +1125,9 @@ GetHostAndPortFunction::OnFunctionCall(mozIStorageValueArray* aArgs, /* static */ nsresult StripPrefixAndUserinfoFunction::create( mozIStorageConnection* aDBConn) { - RefPtr function = - new StripPrefixAndUserinfoFunction(); nsresult rv = aDBConn->CreateFunction( - NS_LITERAL_CSTRING("strip_prefix_and_userinfo"), 1, function); + NS_LITERAL_CSTRING("strip_prefix_and_userinfo"), 1, + MakeAndAddRef()); NS_ENSURE_SUCCESS(rv, rv); return NS_OK; @@ -1170,10 +1160,9 @@ StripPrefixAndUserinfoFunction::OnFunctionCall(mozIStorageValueArray* aArgs, /* static */ nsresult IsFrecencyDecayingFunction::create(mozIStorageConnection* aDBConn) { - RefPtr function = - new IsFrecencyDecayingFunction(); - nsresult rv = aDBConn->CreateFunction( - NS_LITERAL_CSTRING("is_frecency_decaying"), 0, function); + nsresult rv = + aDBConn->CreateFunction(NS_LITERAL_CSTRING("is_frecency_decaying"), 0, + MakeAndAddRef()); NS_ENSURE_SUCCESS(rv, rv); return NS_OK; @@ -1206,9 +1195,8 @@ IsFrecencyDecayingFunction::OnFunctionCall(mozIStorageValueArray* aArgs, /* static */ nsresult SqrtFunction::create(mozIStorageConnection* aDBConn) { - RefPtr function = new SqrtFunction(); - nsresult rv = - aDBConn->CreateFunction(NS_LITERAL_CSTRING("sqrt"), 1, function); + nsresult rv = aDBConn->CreateFunction(NS_LITERAL_CSTRING("sqrt"), 1, + MakeAndAddRef()); NS_ENSURE_SUCCESS(rv, rv); return NS_OK; } @@ -1240,9 +1228,9 @@ SqrtFunction::OnFunctionCall(mozIStorageValueArray* aArgs, /* static */ nsresult NoteSyncChangeFunction::create(mozIStorageConnection* aDBConn) { - RefPtr function = new NoteSyncChangeFunction(); - nsresult rv = aDBConn->CreateFunction(NS_LITERAL_CSTRING("note_sync_change"), - 0, function); + nsresult rv = + aDBConn->CreateFunction(NS_LITERAL_CSTRING("note_sync_change"), 0, + MakeAndAddRef()); NS_ENSURE_SUCCESS(rv, rv); return NS_OK;