summaryrefslogtreecommitdiffstats
path: root/dom/media/gmp/GMPStorageChild.cpp
blob: b08356e0d6b559316d9124f0bb374f6040d6cc43 (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
235
236
237
238
239
240
241
242
243
244
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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 "GMPStorageChild.h"
#include "GMPChild.h"
#include "gmp-storage.h"
#include "base/task.h"

#define ON_GMP_THREAD() (mPlugin->GMPMessageLoop() == MessageLoop::current())

#define CALL_ON_GMP_THREAD(_func, ...)                        \
  do {                                                        \
    if (ON_GMP_THREAD()) {                                    \
      _func(__VA_ARGS__);                                     \
    } else {                                                  \
      mPlugin->GMPMessageLoop()->PostTask(                    \
          dont_add_new_uses_of_this::NewRunnableMethod(       \
              this, &GMPStorageChild::_func, ##__VA_ARGS__)); \
    }                                                         \
  } while (false)

namespace mozilla::gmp {

static nsTArray<uint8_t> ToArray(const uint8_t* aData, uint32_t aDataSize) {
  nsTArray<uint8_t> data;
  data.AppendElements(aData, aDataSize);
  return data;
}

GMPRecordImpl::GMPRecordImpl(GMPStorageChild* aOwner, const nsCString& aName,
                             GMPRecordClient* aClient)
    : mName(aName), mClient(aClient), mOwner(aOwner) {}

GMPErr GMPRecordImpl::Open() { return mOwner->Open(this); }

void GMPRecordImpl::OpenComplete(GMPErr aStatus) {
  mClient->OpenComplete(aStatus);
}

GMPErr GMPRecordImpl::Read() { return mOwner->Read(this); }

void GMPRecordImpl::ReadComplete(GMPErr aStatus, const uint8_t* aBytes,
                                 uint32_t aLength) {
  mClient->ReadComplete(aStatus, aBytes, aLength);
}

GMPErr GMPRecordImpl::Write(const uint8_t* aData, uint32_t aDataSize) {
  return mOwner->Write(this, aData, aDataSize);
}

void GMPRecordImpl::WriteComplete(GMPErr aStatus) {
  mClient->WriteComplete(aStatus);
}

GMPErr GMPRecordImpl::Close() {
  RefPtr<GMPRecordImpl> kungfuDeathGrip(this);
  // Delete our self reference.
  Release();
  mOwner->Close(this->Name());
  return GMPNoErr;
}

GMPStorageChild::GMPStorageChild(GMPChild* aPlugin)
    : mMonitor("GMPStorageChild"), mPlugin(aPlugin), mShutdown(false) {
  MOZ_ASSERT(ON_GMP_THREAD());
}

GMPErr GMPStorageChild::CreateRecord(const nsCString& aRecordName,
                                     GMPRecord** aOutRecord,
                                     GMPRecordClient* aClient) {
  MonitorAutoLock lock(mMonitor);

  if (mShutdown) {
    NS_WARNING("GMPStorage used after it's been shutdown!");
    return GMPClosedErr;
  }

  MOZ_ASSERT(aRecordName.Length() && aOutRecord);

  if (HasRecord(aRecordName)) {
    return GMPRecordInUse;
  }

  RefPtr<GMPRecordImpl> record(new GMPRecordImpl(this, aRecordName, aClient));
  mRecords.InsertOrUpdate(aRecordName, RefPtr{record});  // Addrefs

  // The GMPRecord holds a self reference until the GMP calls Close() on
  // it. This means the object is always valid (even if neutered) while
  // the GMP expects it to be.
  record.forget(aOutRecord);

  return GMPNoErr;
}

bool GMPStorageChild::HasRecord(const nsCString& aRecordName) {
  mMonitor.AssertCurrentThreadOwns();
  return mRecords.Contains(aRecordName);
}

already_AddRefed<GMPRecordImpl> GMPStorageChild::GetRecord(
    const nsCString& aRecordName) {
  MonitorAutoLock lock(mMonitor);
  RefPtr<GMPRecordImpl> record;
  mRecords.Get(aRecordName, getter_AddRefs(record));
  return record.forget();
}

GMPErr GMPStorageChild::Open(GMPRecordImpl* aRecord) {
  MonitorAutoLock lock(mMonitor);

  if (mShutdown) {
    NS_WARNING("GMPStorage used after it's been shutdown!");
    return GMPClosedErr;
  }

  if (!HasRecord(aRecord->Name())) {
    // Trying to re-open a record that has already been closed.
    return GMPClosedErr;
  }

  CALL_ON_GMP_THREAD(SendOpen, aRecord->Name());

  return GMPNoErr;
}

GMPErr GMPStorageChild::Read(GMPRecordImpl* aRecord) {
  MonitorAutoLock lock(mMonitor);

  if (mShutdown) {
    NS_WARNING("GMPStorage used after it's been shutdown!");
    return GMPClosedErr;
  }

  if (!HasRecord(aRecord->Name())) {
    // Record not opened.
    return GMPClosedErr;
  }

  CALL_ON_GMP_THREAD(SendRead, aRecord->Name());

  return GMPNoErr;
}

GMPErr GMPStorageChild::Write(GMPRecordImpl* aRecord, const uint8_t* aData,
                              uint32_t aDataSize) {
  if (aDataSize > GMP_MAX_RECORD_SIZE) {
    return GMPQuotaExceededErr;
  }

  MonitorAutoLock lock(mMonitor);

  if (mShutdown) {
    NS_WARNING("GMPStorage used after it's been shutdown!");
    return GMPClosedErr;
  }

  if (!HasRecord(aRecord->Name())) {
    // Record not opened.
    return GMPClosedErr;
  }

  CALL_ON_GMP_THREAD(SendWrite, aRecord->Name(), ToArray(aData, aDataSize));

  return GMPNoErr;
}

GMPErr GMPStorageChild::Close(const nsCString& aRecordName) {
  MonitorAutoLock lock(mMonitor);

  if (!HasRecord(aRecordName)) {
    // Already closed.
    return GMPClosedErr;
  }

  mRecords.Remove(aRecordName);

  if (!mShutdown) {
    CALL_ON_GMP_THREAD(SendClose, aRecordName);
  }

  return GMPNoErr;
}

mozilla::ipc::IPCResult GMPStorageChild::RecvOpenComplete(
    const nsCString& aRecordName, const GMPErr& aStatus) {
  // We don't need a lock to read |mShutdown| since it is only changed in
  // the GMP thread.
  if (mShutdown) {
    return IPC_OK();
  }
  RefPtr<GMPRecordImpl> record = GetRecord(aRecordName);
  if (!record) {
    // Not fatal.
    return IPC_OK();
  }
  record->OpenComplete(aStatus);
  return IPC_OK();
}

mozilla::ipc::IPCResult GMPStorageChild::RecvReadComplete(
    const nsCString& aRecordName, const GMPErr& aStatus,
    nsTArray<uint8_t>&& aBytes) {
  if (mShutdown) {
    return IPC_OK();
  }
  RefPtr<GMPRecordImpl> record = GetRecord(aRecordName);
  if (!record) {
    // Not fatal.
    return IPC_OK();
  }
  record->ReadComplete(aStatus, aBytes.Elements(), aBytes.Length());
  return IPC_OK();
}

mozilla::ipc::IPCResult GMPStorageChild::RecvWriteComplete(
    const nsCString& aRecordName, const GMPErr& aStatus) {
  if (mShutdown) {
    return IPC_OK();
  }
  RefPtr<GMPRecordImpl> record = GetRecord(aRecordName);
  if (!record) {
    // Not fatal.
    return IPC_OK();
  }
  record->WriteComplete(aStatus);
  return IPC_OK();
}

mozilla::ipc::IPCResult GMPStorageChild::RecvShutdown() {
  // Block any new storage requests, and thus any messages back to the
  // parent. We don't delete any objects here, as that may invalidate
  // GMPRecord pointers held by the GMP.
  MonitorAutoLock lock(mMonitor);
  mShutdown = true;
  return IPC_OK();
}

}  // namespace mozilla::gmp

// avoid redefined macro in unified build
#undef ON_GMP_THREAD
#undef CALL_ON_GMP_THREAD