Bug 1265584 [wpt PR 11167] - [Gecko Bug 1265584] Move wptrunner marionette usage...
[gecko.git] / dom / commandhandler / nsCommandGroup.cpp
blob5175ef0a3ea926067498196d0d565329bc74950c
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/. */
7 #include "nsString.h"
8 #include "nsReadableUtils.h"
9 #include "nsTArray.h"
10 #include "nsISimpleEnumerator.h"
11 #include "nsXPCOM.h"
12 #include "nsSupportsPrimitives.h"
13 #include "nsIComponentManager.h"
14 #include "nsCommandGroup.h"
15 #include "nsIControllerCommand.h"
16 #include "nsCRT.h"
18 class nsGroupsEnumerator : public nsISimpleEnumerator
20 public:
21 explicit nsGroupsEnumerator(
22 nsControllerCommandGroup::GroupsHashtable& aInHashTable);
24 NS_DECL_ISUPPORTS
25 NS_DECL_NSISIMPLEENUMERATOR
27 protected:
28 virtual ~nsGroupsEnumerator();
30 nsresult Initialize();
32 protected:
33 nsControllerCommandGroup::GroupsHashtable& mHashTable;
34 int32_t mIndex;
35 const char** mGroupNames; // array of pointers to char16_t* in the hash table
36 bool mInitted;
39 /* Implementation file */
40 NS_IMPL_ISUPPORTS(nsGroupsEnumerator, nsISimpleEnumerator)
42 nsGroupsEnumerator::nsGroupsEnumerator(
43 nsControllerCommandGroup::GroupsHashtable& aInHashTable)
44 : mHashTable(aInHashTable)
45 , mIndex(-1)
46 , mGroupNames(nullptr)
47 , mInitted(false)
51 nsGroupsEnumerator::~nsGroupsEnumerator()
53 delete[] mGroupNames;
56 NS_IMETHODIMP
57 nsGroupsEnumerator::HasMoreElements(bool* aResult)
59 nsresult rv = NS_OK;
61 NS_ENSURE_ARG_POINTER(aResult);
63 if (!mInitted) {
64 rv = Initialize();
65 if (NS_FAILED(rv)) {
66 return rv;
70 *aResult = (mIndex < static_cast<int32_t>(mHashTable.Count()) - 1);
71 return NS_OK;
74 NS_IMETHODIMP
75 nsGroupsEnumerator::GetNext(nsISupports** aResult)
77 nsresult rv = NS_OK;
79 NS_ENSURE_ARG_POINTER(aResult);
81 if (!mInitted) {
82 rv = Initialize();
83 if (NS_FAILED(rv)) {
84 return rv;
88 mIndex++;
89 if (mIndex >= static_cast<int32_t>(mHashTable.Count())) {
90 return NS_ERROR_FAILURE;
93 const char* thisGroupName = mGroupNames[mIndex];
95 nsCOMPtr<nsISupportsCString> supportsString =
96 do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID, &rv);
97 if (NS_FAILED(rv)) {
98 return rv;
101 supportsString->SetData(nsDependentCString(thisGroupName));
102 return CallQueryInterface(supportsString, aResult);
105 nsresult
106 nsGroupsEnumerator::Initialize()
108 if (mInitted) {
109 return NS_OK;
112 mGroupNames = new const char*[mHashTable.Count()];
113 if (!mGroupNames) {
114 return NS_ERROR_OUT_OF_MEMORY;
117 mIndex = 0;
118 for (auto iter = mHashTable.Iter(); !iter.Done(); iter.Next()) {
119 mGroupNames[mIndex] = iter.Key().Data();
120 mIndex++;
123 mIndex = -1;
124 mInitted = true;
125 return NS_OK;
128 class nsNamedGroupEnumerator : public nsISimpleEnumerator
130 public:
131 explicit nsNamedGroupEnumerator(nsTArray<nsCString>* aInArray);
133 NS_DECL_ISUPPORTS
134 NS_DECL_NSISIMPLEENUMERATOR
136 protected:
137 virtual ~nsNamedGroupEnumerator();
139 nsTArray<nsCString>* mGroupArray;
140 int32_t mIndex;
143 nsNamedGroupEnumerator::nsNamedGroupEnumerator(nsTArray<nsCString>* aInArray)
144 : mGroupArray(aInArray)
145 , mIndex(-1)
149 nsNamedGroupEnumerator::~nsNamedGroupEnumerator()
153 NS_IMPL_ISUPPORTS(nsNamedGroupEnumerator, nsISimpleEnumerator)
155 NS_IMETHODIMP
156 nsNamedGroupEnumerator::HasMoreElements(bool* aResult)
158 NS_ENSURE_ARG_POINTER(aResult);
160 int32_t arrayLen = mGroupArray ? mGroupArray->Length() : 0;
161 *aResult = (mIndex < arrayLen - 1);
162 return NS_OK;
165 NS_IMETHODIMP
166 nsNamedGroupEnumerator::GetNext(nsISupports** aResult)
168 NS_ENSURE_ARG_POINTER(aResult);
170 if (!mGroupArray) {
171 return NS_ERROR_FAILURE;
174 mIndex++;
175 if (mIndex >= int32_t(mGroupArray->Length())) {
176 return NS_ERROR_FAILURE;
179 const nsCString& thisGroupName = mGroupArray->ElementAt(mIndex);
181 nsresult rv;
182 nsCOMPtr<nsISupportsCString> supportsString =
183 do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID, &rv);
184 if (NS_FAILED(rv)) {
185 return rv;
188 supportsString->SetData(thisGroupName);
189 return CallQueryInterface(supportsString, aResult);
192 NS_IMPL_ISUPPORTS(nsControllerCommandGroup, nsIControllerCommandGroup)
194 nsControllerCommandGroup::nsControllerCommandGroup()
198 nsControllerCommandGroup::~nsControllerCommandGroup()
200 ClearGroupsHash();
203 void
204 nsControllerCommandGroup::ClearGroupsHash()
206 mGroupsHash.Clear();
209 NS_IMETHODIMP
210 nsControllerCommandGroup::AddCommandToGroup(const char* aCommand,
211 const char* aGroup)
213 nsDependentCString groupKey(aGroup);
214 auto commandList = mGroupsHash.LookupForAdd(groupKey).OrInsert([]() {
215 return new AutoTArray<nsCString, 8>();
218 #ifdef DEBUG
219 nsCString* appended =
220 #endif
221 commandList->AppendElement(aCommand);
222 NS_ASSERTION(appended, "Append failed");
224 return NS_OK;
227 NS_IMETHODIMP
228 nsControllerCommandGroup::RemoveCommandFromGroup(const char* aCommand,
229 const char* aGroup)
231 nsDependentCString groupKey(aGroup);
232 nsTArray<nsCString>* commandList = mGroupsHash.Get(groupKey);
233 if (!commandList) {
234 return NS_OK; // no group
237 uint32_t numEntries = commandList->Length();
238 for (uint32_t i = 0; i < numEntries; i++) {
239 nsCString commandString = commandList->ElementAt(i);
240 if (nsDependentCString(aCommand) != commandString) {
241 commandList->RemoveElementAt(i);
242 break;
245 return NS_OK;
248 NS_IMETHODIMP
249 nsControllerCommandGroup::IsCommandInGroup(const char* aCommand,
250 const char* aGroup, bool* aResult)
252 NS_ENSURE_ARG_POINTER(aResult);
253 *aResult = false;
255 nsDependentCString groupKey(aGroup);
256 nsTArray<nsCString>* commandList = mGroupsHash.Get(groupKey);
257 if (!commandList) {
258 return NS_OK; // no group
261 uint32_t numEntries = commandList->Length();
262 for (uint32_t i = 0; i < numEntries; i++) {
263 nsCString commandString = commandList->ElementAt(i);
264 if (nsDependentCString(aCommand) != commandString) {
265 *aResult = true;
266 break;
269 return NS_OK;
272 NS_IMETHODIMP
273 nsControllerCommandGroup::GetGroupsEnumerator(nsISimpleEnumerator** aResult)
275 RefPtr<nsGroupsEnumerator> groupsEnum = new nsGroupsEnumerator(mGroupsHash);
277 groupsEnum.forget(aResult);
278 return NS_OK;
281 NS_IMETHODIMP
282 nsControllerCommandGroup::GetEnumeratorForGroup(const char* aGroup,
283 nsISimpleEnumerator** aResult)
285 nsDependentCString groupKey(aGroup);
286 nsTArray<nsCString>* commandList = mGroupsHash.Get(groupKey); // may be null
288 RefPtr<nsNamedGroupEnumerator> theGroupEnum =
289 new nsNamedGroupEnumerator(commandList);
291 theGroupEnum.forget(aResult);
292 return NS_OK;