Bumping manifests a=b2g-bump
[gecko.git] / xpcom / ds / nsProperties.cpp
blobf6a3a6bf6df01aaa83d4fe655c38ec8c2fa9e208
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "nsProperties.h"
8 ////////////////////////////////////////////////////////////////////////////////
10 NS_IMPL_AGGREGATED(nsProperties)
11 NS_INTERFACE_MAP_BEGIN_AGGREGATED(nsProperties)
12 NS_INTERFACE_MAP_ENTRY(nsIProperties)
13 NS_INTERFACE_MAP_END
15 NS_IMETHODIMP
16 nsProperties::Get(const char* prop, const nsIID& uuid, void** result)
18 if (NS_WARN_IF(!prop)) {
19 return NS_ERROR_INVALID_ARG;
22 nsCOMPtr<nsISupports> value;
23 if (!nsProperties_HashBase::Get(prop, getter_AddRefs(value))) {
24 return NS_ERROR_FAILURE;
26 return (value) ? value->QueryInterface(uuid, result) : NS_ERROR_NO_INTERFACE;
29 NS_IMETHODIMP
30 nsProperties::Set(const char* prop, nsISupports* value)
32 if (NS_WARN_IF(!prop)) {
33 return NS_ERROR_INVALID_ARG;
35 Put(prop, value);
36 return NS_OK;
39 NS_IMETHODIMP
40 nsProperties::Undefine(const char* prop)
42 if (NS_WARN_IF(!prop)) {
43 return NS_ERROR_INVALID_ARG;
46 nsCOMPtr<nsISupports> value;
47 if (!nsProperties_HashBase::Get(prop, getter_AddRefs(value))) {
48 return NS_ERROR_FAILURE;
51 Remove(prop);
52 return NS_OK;
55 NS_IMETHODIMP
56 nsProperties::Has(const char* prop, bool* result)
58 if (NS_WARN_IF(!prop)) {
59 return NS_ERROR_INVALID_ARG;
62 nsCOMPtr<nsISupports> value;
63 *result = nsProperties_HashBase::Get(prop, getter_AddRefs(value));
64 return NS_OK;
67 struct GetKeysEnumData
69 char** keys;
70 uint32_t next;
71 nsresult res;
74 PLDHashOperator
75 GetKeysEnumerate(const char* aKey, nsISupports* aData, void* aArg)
77 GetKeysEnumData* gkedp = (GetKeysEnumData*)aArg;
78 gkedp->keys[gkedp->next] = strdup(aKey);
80 if (!gkedp->keys[gkedp->next]) {
81 gkedp->res = NS_ERROR_OUT_OF_MEMORY;
82 return PL_DHASH_STOP;
85 gkedp->next++;
86 return PL_DHASH_NEXT;
89 NS_IMETHODIMP
90 nsProperties::GetKeys(uint32_t* aCount, char*** aKeys)
92 if (NS_WARN_IF(!aCount) || NS_WARN_IF(!aKeys)) {
93 return NS_ERROR_INVALID_ARG;
96 uint32_t n = Count();
97 char** k = (char**)nsMemory::Alloc(n * sizeof(char*));
99 GetKeysEnumData gked;
100 gked.keys = k;
101 gked.next = 0;
102 gked.res = NS_OK;
104 EnumerateRead(GetKeysEnumerate, &gked);
106 nsresult rv = gked.res;
107 if (NS_FAILED(rv)) {
108 // Free 'em all
109 for (uint32_t i = 0; i < gked.next; i++) {
110 nsMemory::Free(k[i]);
112 nsMemory::Free(k);
113 return rv;
116 *aCount = n;
117 *aKeys = k;
118 return NS_OK;
121 ////////////////////////////////////////////////////////////////////////////////