Bug 1265584 [wpt PR 11167] - [Gecko Bug 1265584] Move wptrunner marionette usage...
[gecko.git] / dom / commandhandler / nsCommandParams.cpp
blob86647d4ddb6355332eda42f46a2e03bbe575ea52
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 "xpcom-config.h"
8 #include <new>
9 #include "nscore.h"
10 #include "nsCRT.h"
12 #include "nsCommandParams.h"
13 #include "mozilla/HashFunctions.h"
15 using namespace mozilla;
17 const PLDHashTableOps nsCommandParams::sHashOps =
19 HashKey,
20 HashMatchEntry,
21 HashMoveEntry,
22 HashClearEntry
25 NS_IMPL_ISUPPORTS(nsCommandParams, nsICommandParams)
27 nsCommandParams::nsCommandParams()
28 : mValuesHash(&sHashOps, sizeof(HashEntry), 2)
32 nsCommandParams::~nsCommandParams()
36 NS_IMETHODIMP
37 nsCommandParams::GetValueType(const char* aName, int16_t* aRetVal)
39 NS_ENSURE_ARG_POINTER(aRetVal);
41 HashEntry* foundEntry = GetNamedEntry(aName);
42 if (foundEntry) {
43 *aRetVal = foundEntry->mEntryType;
44 return NS_OK;
46 *aRetVal = eNoType;
47 return NS_ERROR_FAILURE;
50 NS_IMETHODIMP
51 nsCommandParams::GetBooleanValue(const char* aName, bool* aRetVal)
53 NS_ENSURE_ARG_POINTER(aRetVal);
55 HashEntry* foundEntry = GetNamedEntry(aName);
56 if (foundEntry && foundEntry->mEntryType == eBooleanType) {
57 *aRetVal = foundEntry->mData.mBoolean;
58 return NS_OK;
60 *aRetVal = false;
61 return NS_ERROR_FAILURE;
64 NS_IMETHODIMP
65 nsCommandParams::GetLongValue(const char* aName, int32_t* aRetVal)
67 NS_ENSURE_ARG_POINTER(aRetVal);
69 HashEntry* foundEntry = GetNamedEntry(aName);
70 if (foundEntry && foundEntry->mEntryType == eLongType) {
71 *aRetVal = foundEntry->mData.mLong;
72 return NS_OK;
74 *aRetVal = false;
75 return NS_ERROR_FAILURE;
78 NS_IMETHODIMP
79 nsCommandParams::GetDoubleValue(const char* aName, double* aRetVal)
81 NS_ENSURE_ARG_POINTER(aRetVal);
83 HashEntry* foundEntry = GetNamedEntry(aName);
84 if (foundEntry && foundEntry->mEntryType == eDoubleType) {
85 *aRetVal = foundEntry->mData.mDouble;
86 return NS_OK;
88 *aRetVal = 0.0;
89 return NS_ERROR_FAILURE;
92 NS_IMETHODIMP
93 nsCommandParams::GetStringValue(const char* aName, nsAString& aRetVal)
95 HashEntry* foundEntry = GetNamedEntry(aName);
96 if (foundEntry && foundEntry->mEntryType == eWStringType) {
97 NS_ASSERTION(foundEntry->mData.mString, "Null string");
98 aRetVal.Assign(*foundEntry->mData.mString);
99 return NS_OK;
101 aRetVal.Truncate();
102 return NS_ERROR_FAILURE;
105 NS_IMETHODIMP
106 nsCommandParams::GetCStringValue(const char* aName, char** aRetVal)
108 NS_ENSURE_ARG_POINTER(aRetVal);
110 HashEntry* foundEntry = GetNamedEntry(aName);
111 if (foundEntry && foundEntry->mEntryType == eStringType) {
112 NS_ASSERTION(foundEntry->mData.mCString, "Null string");
113 *aRetVal = ToNewCString(*foundEntry->mData.mCString);
114 return NS_OK;
116 *aRetVal = nullptr;
117 return NS_ERROR_FAILURE;
120 NS_IMETHODIMP
121 nsCommandParams::GetISupportsValue(const char* aName, nsISupports** aRetVal)
123 NS_ENSURE_ARG_POINTER(aRetVal);
125 HashEntry* foundEntry = GetNamedEntry(aName);
126 if (foundEntry && foundEntry->mEntryType == eISupportsType) {
127 NS_IF_ADDREF(*aRetVal = foundEntry->mISupports.get());
128 return NS_OK;
130 *aRetVal = nullptr;
131 return NS_ERROR_FAILURE;
134 NS_IMETHODIMP
135 nsCommandParams::SetBooleanValue(const char* aName, bool aValue)
137 HashEntry* foundEntry = GetOrMakeEntry(aName, eBooleanType);
138 if (!foundEntry) {
139 return NS_ERROR_OUT_OF_MEMORY;
141 foundEntry->mData.mBoolean = aValue;
142 return NS_OK;
145 NS_IMETHODIMP
146 nsCommandParams::SetLongValue(const char* aName, int32_t aValue)
148 HashEntry* foundEntry = GetOrMakeEntry(aName, eLongType);
149 if (!foundEntry) {
150 return NS_ERROR_OUT_OF_MEMORY;
152 foundEntry->mData.mLong = aValue;
153 return NS_OK;
156 NS_IMETHODIMP
157 nsCommandParams::SetDoubleValue(const char* aName, double aValue)
159 HashEntry* foundEntry = GetOrMakeEntry(aName, eDoubleType);
160 if (!foundEntry) {
161 return NS_ERROR_OUT_OF_MEMORY;
163 foundEntry->mData.mDouble = aValue;
164 return NS_OK;
167 NS_IMETHODIMP
168 nsCommandParams::SetStringValue(const char* aName, const nsAString& aValue)
170 HashEntry* foundEntry = GetOrMakeEntry(aName, eWStringType);
171 if (!foundEntry) {
172 return NS_ERROR_OUT_OF_MEMORY;
174 foundEntry->mData.mString = new nsString(aValue);
175 return NS_OK;
178 NS_IMETHODIMP
179 nsCommandParams::SetCStringValue(const char* aName, const char* aValue)
181 HashEntry* foundEntry = GetOrMakeEntry(aName, eStringType);
182 if (!foundEntry) {
183 return NS_ERROR_OUT_OF_MEMORY;
185 foundEntry->mData.mCString = new nsCString(aValue);
186 return NS_OK;
189 NS_IMETHODIMP
190 nsCommandParams::SetISupportsValue(const char* aName, nsISupports* aValue)
192 HashEntry* foundEntry = GetOrMakeEntry(aName, eISupportsType);
193 if (!foundEntry) {
194 return NS_ERROR_OUT_OF_MEMORY;
196 foundEntry->mISupports = aValue; // addrefs
197 return NS_OK;
200 NS_IMETHODIMP
201 nsCommandParams::RemoveValue(const char* aName)
203 mValuesHash.Remove((void*)aName);
204 return NS_OK;
207 nsCommandParams::HashEntry*
208 nsCommandParams::GetNamedEntry(const char* aName)
210 return static_cast<HashEntry*>(mValuesHash.Search((void*)aName));
213 nsCommandParams::HashEntry*
214 nsCommandParams::GetOrMakeEntry(const char* aName, uint8_t aEntryType)
216 auto foundEntry = static_cast<HashEntry*>(mValuesHash.Search((void*)aName));
217 if (foundEntry) { // reuse existing entry
218 foundEntry->Reset(aEntryType);
219 return foundEntry;
222 foundEntry = static_cast<HashEntry*>(mValuesHash.Add((void*)aName, fallible));
223 if (!foundEntry) {
224 return nullptr;
227 // Use placement new. Our ctor does not clobber keyHash, which is important.
228 new (foundEntry) HashEntry(aEntryType, aName);
229 return foundEntry;
232 PLDHashNumber
233 nsCommandParams::HashKey(const void* aKey)
235 return HashString((const char*)aKey);
238 bool
239 nsCommandParams::HashMatchEntry(const PLDHashEntryHdr* aEntry, const void* aKey)
241 const char* keyString = (const char*)aKey;
242 const HashEntry* thisEntry = static_cast<const HashEntry*>(aEntry);
243 return thisEntry->mEntryName.Equals(keyString);
246 void
247 nsCommandParams::HashMoveEntry(PLDHashTable* aTable,
248 const PLDHashEntryHdr* aFrom,
249 PLDHashEntryHdr* aTo)
251 const HashEntry* fromEntry = static_cast<const HashEntry*>(aFrom);
252 HashEntry* toEntry = static_cast<HashEntry*>(aTo);
254 new (toEntry) HashEntry(*fromEntry);
256 fromEntry->~HashEntry();
259 void
260 nsCommandParams::HashClearEntry(PLDHashTable* aTable, PLDHashEntryHdr* aEntry)
262 HashEntry* thisEntry = static_cast<HashEntry*>(aEntry);
263 thisEntry->~HashEntry();