Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/commandhandler/nsBaseCommandController.cpp
Line
Count
Source (jump to first uncovered line)
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/. */
6
7
#include "nsString.h"
8
#include "nsIComponentManager.h"
9
#include "nsBaseCommandController.h"
10
#include "nsControllerCommandTable.h"
11
12
#include "nsString.h"
13
#include "nsWeakPtr.h"
14
15
NS_IMPL_ADDREF(nsBaseCommandController)
16
NS_IMPL_RELEASE(nsBaseCommandController)
17
18
0
NS_INTERFACE_MAP_BEGIN(nsBaseCommandController)
19
0
  NS_INTERFACE_MAP_ENTRY(nsIController)
20
0
  NS_INTERFACE_MAP_ENTRY(nsICommandController)
21
0
  NS_INTERFACE_MAP_ENTRY(nsIControllerContext)
22
0
  NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor)
23
0
  NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIControllerContext)
24
0
NS_INTERFACE_MAP_END
25
26
nsBaseCommandController::nsBaseCommandController()
27
  : mCommandContextRawPtr(nullptr)
28
0
{
29
0
}
30
31
nsBaseCommandController::~nsBaseCommandController()
32
0
{
33
0
}
34
35
NS_IMETHODIMP
36
nsBaseCommandController::Init(nsIControllerCommandTable* aCommandTable)
37
0
{
38
0
  if (aCommandTable) {
39
0
    mCommandTable = aCommandTable;
40
0
  } else {
41
0
    mCommandTable = new nsControllerCommandTable();
42
0
  }
43
0
44
0
  return NS_OK;
45
0
}
46
47
NS_IMETHODIMP
48
nsBaseCommandController::SetCommandContext(nsISupports* aCommandContext)
49
0
{
50
0
  mCommandContextWeakPtr = nullptr;
51
0
  mCommandContextRawPtr = nullptr;
52
0
53
0
  if (aCommandContext) {
54
0
    nsCOMPtr<nsISupportsWeakReference> weak = do_QueryInterface(aCommandContext);
55
0
    if (weak) {
56
0
      nsresult rv =
57
0
        weak->GetWeakReference(getter_AddRefs(mCommandContextWeakPtr));
58
0
      NS_ENSURE_SUCCESS(rv, rv);
59
0
    } else {
60
0
      mCommandContextRawPtr = aCommandContext;
61
0
    }
62
0
  }
63
0
64
0
  return NS_OK;
65
0
}
66
67
NS_IMETHODIMP
68
nsBaseCommandController::GetInterface(const nsIID& aIID, void** aResult)
69
0
{
70
0
  NS_ENSURE_ARG_POINTER(aResult);
71
0
72
0
  if (NS_SUCCEEDED(QueryInterface(aIID, aResult))) {
73
0
    return NS_OK;
74
0
  }
75
0
76
0
  if (aIID.Equals(NS_GET_IID(nsIControllerCommandTable))) {
77
0
    if (mCommandTable) {
78
0
      return mCommandTable->QueryInterface(aIID, aResult);
79
0
    }
80
0
    return NS_ERROR_NOT_INITIALIZED;
81
0
  }
82
0
83
0
  return NS_NOINTERFACE;
84
0
}
85
86
/* =======================================================================
87
 * nsIController
88
 * ======================================================================= */
89
90
NS_IMETHODIMP
91
nsBaseCommandController::IsCommandEnabled(const char* aCommand, bool* aResult)
92
0
{
93
0
  NS_ENSURE_ARG_POINTER(aCommand);
94
0
  NS_ENSURE_ARG_POINTER(aResult);
95
0
  NS_ENSURE_STATE(mCommandTable);
96
0
97
0
  nsISupports* context = mCommandContextRawPtr;
98
0
  nsCOMPtr<nsISupports> weak;
99
0
  if (!context) {
100
0
    weak = do_QueryReferent(mCommandContextWeakPtr);
101
0
    context = weak;
102
0
  }
103
0
  return mCommandTable->IsCommandEnabled(aCommand, context, aResult);
104
0
}
105
106
NS_IMETHODIMP
107
nsBaseCommandController::SupportsCommand(const char* aCommand, bool* aResult)
108
0
{
109
0
  NS_ENSURE_ARG_POINTER(aCommand);
110
0
  NS_ENSURE_ARG_POINTER(aResult);
111
0
  NS_ENSURE_STATE(mCommandTable);
112
0
113
0
  nsISupports* context = mCommandContextRawPtr;
114
0
  nsCOMPtr<nsISupports> weak;
115
0
  if (!context) {
116
0
    weak = do_QueryReferent(mCommandContextWeakPtr);
117
0
    context = weak;
118
0
  }
119
0
  return mCommandTable->SupportsCommand(aCommand, context, aResult);
120
0
}
121
122
NS_IMETHODIMP
123
nsBaseCommandController::DoCommand(const char* aCommand)
124
0
{
125
0
  NS_ENSURE_ARG_POINTER(aCommand);
126
0
  NS_ENSURE_STATE(mCommandTable);
127
0
128
0
  nsISupports* context = mCommandContextRawPtr;
129
0
  nsCOMPtr<nsISupports> weak;
130
0
  if (!context) {
131
0
    weak = do_QueryReferent(mCommandContextWeakPtr);
132
0
    context = weak;
133
0
  }
134
0
  return mCommandTable->DoCommand(aCommand, context);
135
0
}
136
137
NS_IMETHODIMP
138
nsBaseCommandController::DoCommandWithParams(const char* aCommand,
139
                                             nsICommandParams* aParams)
140
0
{
141
0
  NS_ENSURE_ARG_POINTER(aCommand);
142
0
  NS_ENSURE_STATE(mCommandTable);
143
0
144
0
  nsISupports* context = mCommandContextRawPtr;
145
0
  nsCOMPtr<nsISupports> weak;
146
0
  if (!context) {
147
0
    weak = do_QueryReferent(mCommandContextWeakPtr);
148
0
    context = weak;
149
0
  }
150
0
  return mCommandTable->DoCommandParams(aCommand, aParams, context);
151
0
}
152
153
NS_IMETHODIMP
154
nsBaseCommandController::GetCommandStateWithParams(const char* aCommand,
155
                                                   nsICommandParams* aParams)
156
0
{
157
0
  NS_ENSURE_ARG_POINTER(aCommand);
158
0
  NS_ENSURE_STATE(mCommandTable);
159
0
160
0
  nsISupports* context = mCommandContextRawPtr;
161
0
  nsCOMPtr<nsISupports> weak;
162
0
  if (!context) {
163
0
    weak = do_QueryReferent(mCommandContextWeakPtr);
164
0
    context = weak;
165
0
  }
166
0
  return mCommandTable->GetCommandState(aCommand, aParams, context);
167
0
}
168
169
NS_IMETHODIMP
170
nsBaseCommandController::OnEvent(const char* aEventName)
171
0
{
172
0
  NS_ENSURE_ARG_POINTER(aEventName);
173
0
  return NS_OK;
174
0
}
175
176
NS_IMETHODIMP
177
nsBaseCommandController::GetSupportedCommands(uint32_t* aCount,
178
                                              char*** aCommands)
179
0
{
180
0
  NS_ENSURE_STATE(mCommandTable);
181
0
  return mCommandTable->GetSupportedCommands(aCount, aCommands);
182
0
}
183
184
typedef already_AddRefed<nsIControllerCommandTable> (*CommandTableCreatorFn)();
185
186
static already_AddRefed<nsIController>
187
CreateControllerWithSingletonCommandTable(CommandTableCreatorFn aCreatorFn)
188
0
{
189
0
  nsCOMPtr<nsIController> controller = new nsBaseCommandController();
190
0
191
0
  nsCOMPtr<nsIControllerCommandTable> commandTable = aCreatorFn();
192
0
  if (!commandTable) return nullptr;
193
0
194
0
  // this is a singleton; make it immutable
195
0
  commandTable->MakeImmutable();
196
0
197
0
  nsresult rv;
198
0
  nsCOMPtr<nsIControllerContext> controllerContext = do_QueryInterface(controller, &rv);
199
0
  if (NS_FAILED(rv)) return nullptr;
200
0
201
0
  rv = controllerContext->Init(commandTable);
202
0
  if (NS_FAILED(rv)) return nullptr;
203
0
204
0
  return controller.forget();
205
0
}
206
207
already_AddRefed<nsIController>
208
nsBaseCommandController::CreateWindowController()
209
0
{
210
0
  return CreateControllerWithSingletonCommandTable(
211
0
      nsControllerCommandTable::CreateWindowCommandTable);
212
0
}
213
214
already_AddRefed<nsIController>
215
nsBaseCommandController::CreateEditorController()
216
0
{
217
0
  return CreateControllerWithSingletonCommandTable(
218
0
      nsControllerCommandTable::CreateEditorCommandTable);
219
0
}
220
221
already_AddRefed<nsIController>
222
nsBaseCommandController::CreateEditingController()
223
0
{
224
0
  return CreateControllerWithSingletonCommandTable(
225
0
      nsControllerCommandTable::CreateEditingCommandTable);
226
0
}
227
228
already_AddRefed<nsIController>
229
nsBaseCommandController::CreateHTMLEditorController()
230
0
{
231
0
  return CreateControllerWithSingletonCommandTable(
232
0
      nsControllerCommandTable::CreateHTMLEditorCommandTable);
233
0
}
234
235
already_AddRefed<nsIController>
236
nsBaseCommandController::CreateHTMLEditorDocStateController()
237
0
{
238
0
  return CreateControllerWithSingletonCommandTable(
239
0
      nsControllerCommandTable::CreateHTMLEditorDocStateCommandTable);
240
0
}