summaryrefslogtreecommitdiffstats
path: root/dom/media/mediacapabilities/KeyValueStorage.cpp
blob: f0ac0aad7dbe7500a8084a112958d0bf1bb1a34a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "KeyValueStorage.h"

#include "nsAppDirectoryServiceDefs.h"
#include "nsDirectoryServiceUtils.h"
#include "nsServiceManagerUtils.h"
#include "nsVariant.h"

namespace mozilla {

class DatabaseCallback final : public nsIKeyValueDatabaseCallback {
 public:
  NS_DECL_ISUPPORTS

  explicit DatabaseCallback(RefPtr<nsIKeyValueDatabase>& aDatabase)
      : mDatabase(aDatabase) {}
  NS_IMETHOD Resolve(nsIKeyValueDatabase* aDatabase) override {
    if (!aDatabase) {
      mResultPromise.Reject(NS_ERROR_FAILURE, __func__);
    }
    mDatabase = aDatabase;
    mResultPromise.Resolve(true, __func__);
    return NS_OK;
  }
  NS_IMETHOD Reject(const nsACString&) override {
    mResultPromise.Reject(NS_ERROR_FAILURE, __func__);
    return NS_OK;
  }
  RefPtr<GenericPromise> Ensure() { return mResultPromise.Ensure(__func__); }

 protected:
  ~DatabaseCallback() = default;
  RefPtr<nsIKeyValueDatabase>& mDatabase;
  MozPromiseHolder<GenericPromise> mResultPromise;
};
NS_IMPL_ISUPPORTS(DatabaseCallback, nsIKeyValueDatabaseCallback);

RefPtr<GenericPromise> KeyValueStorage::Init() {
  MOZ_ASSERT(NS_IsMainThread());
  MOZ_ASSERT(!mDatabaseName.IsEmpty());
  nsresult rv;

  nsCOMPtr<nsIFile> profileDir;
  rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR,
                              getter_AddRefs(profileDir));
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return GenericPromise::CreateAndReject(rv, __func__);
  }
  MOZ_ASSERT(profileDir);

  rv = profileDir->AppendNative("mediacapabilities"_ns);
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return GenericPromise::CreateAndReject(rv, __func__);
  }

  rv = profileDir->Create(nsIFile::DIRECTORY_TYPE, 0700);
  if (rv != NS_ERROR_FILE_ALREADY_EXISTS && NS_WARN_IF(NS_FAILED(rv))) {
    return GenericPromise::CreateAndReject(rv, __func__);
  }

  nsCOMPtr<nsIKeyValueService> keyValueService =
      do_GetService("@mozilla.org/key-value-service;1", &rv);
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return GenericPromise::CreateAndReject(rv, __func__);
  }
  MOZ_ASSERT(keyValueService);

  auto callback = MakeRefPtr<DatabaseCallback>(mDatabase);

  nsString path;
  profileDir->GetPath(path);
  keyValueService->GetOrCreate(callback, NS_ConvertUTF16toUTF8(path),
                               mDatabaseName);
  return callback->Ensure();
}

class VoidCallback final : public nsIKeyValueVoidCallback {
 public:
  NS_DECL_ISUPPORTS

  explicit VoidCallback(const RefPtr<KeyValueStorage>& aOwner)
      : nsIKeyValueVoidCallback(), mOwner(aOwner) {}

  NS_IMETHOD Resolve() override {
    mResultPromise.Resolve(true, __func__);
    return NS_OK;
  }
  NS_IMETHOD Reject(const nsACString&) override {
    mResultPromise.Reject(NS_ERROR_FAILURE, __func__);
    return NS_OK;
  }
  RefPtr<GenericPromise> Ensure(const char* aMethodName) {
    return mResultPromise.Ensure(aMethodName);
  }

 protected:
  ~VoidCallback() = default;
  MozPromiseHolder<GenericPromise> mResultPromise;
  RefPtr<KeyValueStorage> mOwner;
};
NS_IMPL_ISUPPORTS(VoidCallback, nsIKeyValueVoidCallback);

RefPtr<GenericPromise> KeyValueStorage::Put(const nsACString& aKey,
                                            int32_t aValue) {
  MOZ_ASSERT(NS_IsMainThread());
  MOZ_ASSERT(mDatabase);
  MOZ_ASSERT(!mDatabaseName.IsEmpty());

  auto value = MakeRefPtr<nsVariant>();
  nsresult rv = value->SetAsInt32(aValue);
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return GenericPromise::CreateAndReject(rv, __func__);
  }

  auto callback = MakeRefPtr<VoidCallback>(this);
  rv = mDatabase->Put(callback, aKey, value);
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return GenericPromise::CreateAndReject(rv, __func__);
  }
  return callback->Ensure(__func__);
}

RefPtr<GenericPromise> KeyValueStorage::Put(const nsACString& aName,
                                            const nsACString& aKey,
                                            int32_t aValue) {
  if (!mDatabase || !mDatabaseName.Equals(aName)) {
    mDatabaseName = aName;
    RefPtr<KeyValueStorage> self = this;
    const nsCString key(aKey);
    return Init()->Then(
        GetCurrentSerialEventTarget(), __func__,
        [self, key, aValue](bool) { return self->Put(key, aValue); },
        [](nsresult rv) {
          return GenericPromise::CreateAndReject(rv, __func__);
        });
  }
  return Put(aKey, aValue);
}

class GetValueCallback final : public nsIKeyValueVariantCallback {
 public:
  NS_DECL_ISUPPORTS

  NS_IMETHOD Resolve(nsIVariant* aResult) override {
    int32_t value = 0;
    Unused << aResult->GetAsInt32(&value);
    mResultPromise.Resolve(value, __func__);
    return NS_OK;
  }
  NS_IMETHOD Reject(const nsACString&) override {
    mResultPromise.Reject(NS_ERROR_FAILURE, __func__);
    return NS_OK;
  }
  RefPtr<KeyValueStorage::GetPromise> Ensure() {
    return mResultPromise.Ensure(__func__);
  }

 protected:
  ~GetValueCallback() = default;

 private:
  MozPromiseHolder<KeyValueStorage::GetPromise> mResultPromise;
};
NS_IMPL_ISUPPORTS(GetValueCallback, nsIKeyValueVariantCallback);

RefPtr<KeyValueStorage::GetPromise> KeyValueStorage::Get(
    const nsACString& aKey) {
  MOZ_ASSERT(NS_IsMainThread());
  MOZ_ASSERT(mDatabase);
  MOZ_ASSERT(!mDatabaseName.IsEmpty());

  RefPtr<nsVariant> defaultValue = new nsVariant;
  nsresult rv = defaultValue->SetAsInt32(-1);
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return KeyValueStorage::GetPromise::CreateAndReject(rv, __func__);
  }

  auto callback = MakeRefPtr<GetValueCallback>();
  rv = mDatabase->Get(callback, aKey, defaultValue);
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return KeyValueStorage::GetPromise::CreateAndReject(rv, __func__);
  }
  return callback->Ensure();
}

RefPtr<KeyValueStorage::GetPromise> KeyValueStorage::Get(
    const nsACString& aName, const nsACString& aKey) {
  if (!mDatabase || !mDatabaseName.Equals(aName)) {
    mDatabaseName = aName;
    RefPtr<KeyValueStorage> self = this;
    const nsCString key(aKey);
    return Init()->Then(
        GetCurrentSerialEventTarget(), __func__,
        [self, key](bool) { return self->Get(key); },
        [](nsresult rv) {
          return KeyValueStorage::GetPromise::CreateAndReject(rv, __func__);
        });
  }
  return Get(aKey);
}

RefPtr<GenericPromise> KeyValueStorage::Clear() {
  MOZ_ASSERT(NS_IsMainThread());
  MOZ_ASSERT(mDatabase);
  MOZ_ASSERT(!mDatabaseName.IsEmpty());

  auto callback = MakeRefPtr<VoidCallback>(this);
  nsresult rv = mDatabase->Clear(callback);
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return GenericPromise::CreateAndReject(rv, __func__);
  }
  return callback->Ensure(__func__);
}

RefPtr<GenericPromise> KeyValueStorage::Clear(const nsACString& aName) {
  if (!mDatabase || !mDatabaseName.Equals(aName)) {
    mDatabaseName = aName;
    RefPtr<KeyValueStorage> self = this;
    return Init()->Then(
        GetCurrentSerialEventTarget(), __func__,
        [self](bool) { return self->Clear(); },
        [](nsresult rv) {
          return GenericPromise::CreateAndReject(rv, __func__);
        });
  }
  return Clear();
}

}  // namespace mozilla