1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
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/. */
8 #include "nsIWeakReferenceUtils.h"
9 #include "nsBaseCommandController.h"
10 #include "nsControllerCommandTable.h"
12 NS_IMPL_ADDREF(nsBaseCommandController
)
13 NS_IMPL_RELEASE(nsBaseCommandController
)
15 NS_INTERFACE_MAP_BEGIN(nsBaseCommandController
)
16 NS_INTERFACE_MAP_ENTRY(nsIController
)
17 NS_INTERFACE_MAP_ENTRY(nsICommandController
)
18 NS_INTERFACE_MAP_ENTRY(nsIControllerContext
)
19 NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor
)
20 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports
, nsIControllerContext
)
23 nsBaseCommandController::nsBaseCommandController(
24 nsControllerCommandTable
* aControllerCommandTable
)
25 : mCommandContextRawPtr(nullptr), mCommandTable(aControllerCommandTable
) {}
27 nsBaseCommandController::~nsBaseCommandController() = default;
30 nsBaseCommandController::SetCommandContext(nsISupports
* aCommandContext
) {
31 mCommandContextWeakPtr
= nullptr;
32 mCommandContextRawPtr
= nullptr;
34 if (aCommandContext
) {
35 nsCOMPtr
<nsISupportsWeakReference
> weak
=
36 do_QueryInterface(aCommandContext
);
39 weak
->GetWeakReference(getter_AddRefs(mCommandContextWeakPtr
));
40 NS_ENSURE_SUCCESS(rv
, rv
);
42 mCommandContextRawPtr
= aCommandContext
;
50 nsBaseCommandController::GetInterface(const nsIID
& aIID
, void** aResult
) {
51 NS_ENSURE_ARG_POINTER(aResult
);
53 if (NS_SUCCEEDED(QueryInterface(aIID
, aResult
))) {
57 if (aIID
.Equals(NS_GET_IID(nsIControllerCommandTable
))) {
60 do_AddRef(static_cast<nsIControllerCommandTable
*>(mCommandTable
))
64 return NS_ERROR_NOT_INITIALIZED
;
67 return NS_NOINTERFACE
;
70 /* =======================================================================
72 * ======================================================================= */
75 nsBaseCommandController::IsCommandEnabled(const char* aCommand
, bool* aResult
) {
76 NS_ENSURE_ARG_POINTER(aCommand
);
77 NS_ENSURE_ARG_POINTER(aResult
);
78 NS_ENSURE_STATE(mCommandTable
);
80 nsISupports
* context
= mCommandContextRawPtr
;
81 nsCOMPtr
<nsISupports
> weak
;
83 weak
= do_QueryReferent(mCommandContextWeakPtr
);
86 return mCommandTable
->IsCommandEnabled(aCommand
, context
, aResult
);
90 nsBaseCommandController::SupportsCommand(const char* aCommand
, bool* aResult
) {
91 NS_ENSURE_ARG_POINTER(aCommand
);
92 NS_ENSURE_ARG_POINTER(aResult
);
93 NS_ENSURE_STATE(mCommandTable
);
95 nsISupports
* context
= mCommandContextRawPtr
;
96 nsCOMPtr
<nsISupports
> weak
;
98 weak
= do_QueryReferent(mCommandContextWeakPtr
);
101 return mCommandTable
->SupportsCommand(aCommand
, context
, aResult
);
105 nsBaseCommandController::DoCommand(const char* aCommand
) {
106 NS_ENSURE_ARG_POINTER(aCommand
);
107 NS_ENSURE_STATE(mCommandTable
);
109 nsCOMPtr
<nsISupports
> context
= mCommandContextRawPtr
;
111 context
= do_QueryReferent(mCommandContextWeakPtr
);
113 RefPtr
<nsControllerCommandTable
> commandTable(mCommandTable
);
114 return commandTable
->DoCommand(aCommand
, context
);
118 nsBaseCommandController::DoCommandWithParams(const char* aCommand
,
119 nsICommandParams
* aParams
) {
120 NS_ENSURE_ARG_POINTER(aCommand
);
121 NS_ENSURE_STATE(mCommandTable
);
123 nsCOMPtr
<nsISupports
> context
= mCommandContextRawPtr
;
125 context
= do_QueryReferent(mCommandContextWeakPtr
);
127 RefPtr
<nsControllerCommandTable
> commandTable(mCommandTable
);
128 return commandTable
->DoCommandParams(aCommand
, aParams
, context
);
132 nsBaseCommandController::GetCommandStateWithParams(const char* aCommand
,
133 nsICommandParams
* aParams
) {
134 NS_ENSURE_ARG_POINTER(aCommand
);
135 NS_ENSURE_STATE(mCommandTable
);
137 nsISupports
* context
= mCommandContextRawPtr
;
138 nsCOMPtr
<nsISupports
> weak
;
140 weak
= do_QueryReferent(mCommandContextWeakPtr
);
143 return mCommandTable
->GetCommandState(aCommand
, aParams
, context
);
147 nsBaseCommandController::OnEvent(const char* aEventName
) {
148 NS_ENSURE_ARG_POINTER(aEventName
);
153 nsBaseCommandController::GetSupportedCommands(nsTArray
<nsCString
>& aCommands
) {
154 NS_ENSURE_STATE(mCommandTable
);
155 return mCommandTable
->GetSupportedCommands(aCommands
);
158 typedef already_AddRefed
<nsControllerCommandTable
> (*CommandTableCreatorFn
)();
160 static already_AddRefed
<nsBaseCommandController
>
161 CreateControllerWithSingletonCommandTable(CommandTableCreatorFn aCreatorFn
) {
162 RefPtr
<nsControllerCommandTable
> commandTable
= aCreatorFn();
167 // this is a singleton; make it immutable
168 commandTable
->MakeImmutable();
170 RefPtr
<nsBaseCommandController
> commandController
=
171 new nsBaseCommandController(commandTable
);
172 return commandController
.forget();
175 already_AddRefed
<nsBaseCommandController
>
176 nsBaseCommandController::CreateWindowController() {
177 return CreateControllerWithSingletonCommandTable(
178 nsControllerCommandTable::CreateWindowCommandTable
);
181 already_AddRefed
<nsBaseCommandController
>
182 nsBaseCommandController::CreateEditorController() {
183 return CreateControllerWithSingletonCommandTable(
184 nsControllerCommandTable::CreateEditorCommandTable
);
187 already_AddRefed
<nsBaseCommandController
>
188 nsBaseCommandController::CreateEditingController() {
189 return CreateControllerWithSingletonCommandTable(
190 nsControllerCommandTable::CreateEditingCommandTable
);
193 already_AddRefed
<nsBaseCommandController
>
194 nsBaseCommandController::CreateHTMLEditorController() {
195 return CreateControllerWithSingletonCommandTable(
196 nsControllerCommandTable::CreateHTMLEditorCommandTable
);
199 already_AddRefed
<nsBaseCommandController
>
200 nsBaseCommandController::CreateHTMLEditorDocStateController() {
201 return CreateControllerWithSingletonCommandTable(
202 nsControllerCommandTable::CreateHTMLEditorDocStateCommandTable
);