From f1f0f88418cc94f43b09a75145eb27ed5bdfd20d Mon Sep 17 00:00:00 2001 From: "cvshook%sicking.cc" Date: Thu, 27 Apr 2006 00:01:50 +0000 Subject: [PATCH] Bug 334515: Work with objects that don't support weak references. r/sr/a=dbaron git-svn-id: svn://10.0.0.236/branches/MOZILLA_1_8_BRANCH@195480 18797224-902f-48f8-a5cc-f745e15eee43 --- .../src/nsBaseCommandController.cpp | 57 +++++++++++++++++-- .../src/nsBaseCommandController.h | 3 +- 2 files changed, 53 insertions(+), 7 deletions(-) diff --git a/mozilla/embedding/components/commandhandler/src/nsBaseCommandController.cpp b/mozilla/embedding/components/commandhandler/src/nsBaseCommandController.cpp index cbb29b7ca0a..99d62f0bb0d 100644 --- a/mozilla/embedding/components/commandhandler/src/nsBaseCommandController.cpp +++ b/mozilla/embedding/components/commandhandler/src/nsBaseCommandController.cpp @@ -55,6 +55,7 @@ NS_INTERFACE_MAP_BEGIN(nsBaseCommandController) NS_INTERFACE_MAP_END nsBaseCommandController::nsBaseCommandController() + : mCommandContextRawPtr(nsnull) { } @@ -78,7 +79,21 @@ nsBaseCommandController::Init(nsIControllerCommandTable *aCommandTable) NS_IMETHODIMP nsBaseCommandController::SetCommandContext(nsISupports *aCommandContext) { - mCommandContext = do_GetWeakReference(aCommandContext); + mCommandContextWeakPtr = nsnull; + mCommandContextRawPtr = nsnull; + + if (aCommandContext) { + nsCOMPtr weak = do_QueryInterface(aCommandContext); + if (weak) { + nsresult rv = + weak->GetWeakReference(getter_AddRefs(mCommandContextWeakPtr)); + NS_ENSURE_SUCCESS(rv, rv); + } + else { + mCommandContextRawPtr = aCommandContext; + } + } + return NS_OK; } @@ -112,7 +127,13 @@ nsBaseCommandController::IsCommandEnabled(const char *aCommand, { NS_ENSURE_ARG_POINTER(aCommand); NS_ENSURE_ARG_POINTER(aResult); - nsCOMPtr context = do_QueryReferent(mCommandContext); + + nsISupports* context = mCommandContextRawPtr; + nsCOMPtr weak; + if (!context) { + weak = do_QueryReferent(mCommandContextWeakPtr); + context = weak; + } return mCommandTable->IsCommandEnabled(aCommand, context, aResult); } @@ -121,7 +142,13 @@ nsBaseCommandController::SupportsCommand(const char *aCommand, PRBool *aResult) { NS_ENSURE_ARG_POINTER(aCommand); NS_ENSURE_ARG_POINTER(aResult); - nsCOMPtr context = do_QueryReferent(mCommandContext); + + nsISupports* context = mCommandContextRawPtr; + nsCOMPtr weak; + if (!context) { + weak = do_QueryReferent(mCommandContextWeakPtr); + context = weak; + } return mCommandTable->SupportsCommand(aCommand, context, aResult); } @@ -129,7 +156,13 @@ NS_IMETHODIMP nsBaseCommandController::DoCommand(const char *aCommand) { NS_ENSURE_ARG_POINTER(aCommand); - nsCOMPtr context = do_QueryReferent(mCommandContext); + + nsISupports* context = mCommandContextRawPtr; + nsCOMPtr weak; + if (!context) { + weak = do_QueryReferent(mCommandContextWeakPtr); + context = weak; + } return mCommandTable->DoCommand(aCommand, context); } @@ -138,7 +171,13 @@ nsBaseCommandController::DoCommandWithParams(const char *aCommand, nsICommandParams *aParams) { NS_ENSURE_ARG_POINTER(aCommand); - nsCOMPtr context = do_QueryReferent(mCommandContext); + + nsISupports* context = mCommandContextRawPtr; + nsCOMPtr weak; + if (!context) { + weak = do_QueryReferent(mCommandContextWeakPtr); + context = weak; + } return mCommandTable->DoCommandParams(aCommand, aParams, context); } @@ -147,7 +186,13 @@ nsBaseCommandController::GetCommandStateWithParams(const char *aCommand, nsICommandParams *aParams) { NS_ENSURE_ARG_POINTER(aCommand); - nsCOMPtr context = do_QueryReferent(mCommandContext); + + nsISupports* context = mCommandContextRawPtr; + nsCOMPtr weak; + if (!context) { + weak = do_QueryReferent(mCommandContextWeakPtr); + context = weak; + } return mCommandTable->GetCommandState(aCommand, aParams, context); } diff --git a/mozilla/embedding/components/commandhandler/src/nsBaseCommandController.h b/mozilla/embedding/components/commandhandler/src/nsBaseCommandController.h index 0241ab54f0f..c2433642eac 100644 --- a/mozilla/embedding/components/commandhandler/src/nsBaseCommandController.h +++ b/mozilla/embedding/components/commandhandler/src/nsBaseCommandController.h @@ -81,7 +81,8 @@ public: private: - nsWeakPtr mCommandContext; + nsWeakPtr mCommandContextWeakPtr; + nsISupports* mCommandContextRawPtr; // Our reference to the command manager nsCOMPtr mCommandTable;