Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/media/gmp/GMPStorageParent.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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/. */
5
6
#include "GMPStorageParent.h"
7
#include "GMPParent.h"
8
#include "gmp-storage.h"
9
#include "mozilla/Unused.h"
10
#include "mozIGeckoMediaPluginService.h"
11
12
namespace mozilla {
13
14
#ifdef LOG
15
#undef LOG
16
#endif
17
18
extern LogModule* GetGMPLog();
19
20
0
#define LOGD(msg) MOZ_LOG(GetGMPLog(), mozilla::LogLevel::Debug, msg)
21
#define LOG(level, msg) MOZ_LOG(GetGMPLog(), (level), msg)
22
23
namespace gmp {
24
25
GMPStorageParent::GMPStorageParent(const nsCString& aNodeId,
26
                                   GMPParent* aPlugin)
27
  : mNodeId(aNodeId)
28
  , mPlugin(aPlugin)
29
  , mShutdown(true)
30
0
{
31
0
}
32
33
nsresult
34
GMPStorageParent::Init()
35
0
{
36
0
  LOGD(("GMPStorageParent[%p]::Init()", this));
37
0
38
0
  if (NS_WARN_IF(mNodeId.IsEmpty())) {
39
0
    return NS_ERROR_FAILURE;
40
0
  }
41
0
  RefPtr<GeckoMediaPluginServiceParent> mps(GeckoMediaPluginServiceParent::GetSingleton());
42
0
  if (NS_WARN_IF(!mps)) {
43
0
    return NS_ERROR_FAILURE;
44
0
  }
45
0
46
0
  bool persistent = false;
47
0
  if (NS_WARN_IF(NS_FAILED(mps->IsPersistentStorageAllowed(mNodeId, &persistent)))) {
48
0
    return NS_ERROR_FAILURE;
49
0
  }
50
0
  if (persistent) {
51
0
    mStorage = CreateGMPDiskStorage(mNodeId, mPlugin->GetPluginBaseName());
52
0
  } else {
53
0
    mStorage = mps->GetMemoryStorageFor(mNodeId);
54
0
  }
55
0
  if (!mStorage) {
56
0
    return NS_ERROR_FAILURE;
57
0
  }
58
0
59
0
  mShutdown = false;
60
0
  return NS_OK;
61
0
}
62
63
mozilla::ipc::IPCResult
64
GMPStorageParent::RecvOpen(const nsCString& aRecordName)
65
0
{
66
0
  LOGD(("GMPStorageParent[%p]::RecvOpen(record='%s')",
67
0
       this, aRecordName.get()));
68
0
69
0
  if (mShutdown) {
70
0
    return IPC_FAIL_NO_REASON(this);
71
0
  }
72
0
73
0
  if (mNodeId.EqualsLiteral("null")) {
74
0
    // Refuse to open storage if the page is opened from local disk,
75
0
    // or shared across origin.
76
0
    LOGD(("GMPStorageParent[%p]::RecvOpen(record='%s') failed; null nodeId",
77
0
          this, aRecordName.get()));
78
0
    Unused << SendOpenComplete(aRecordName, GMPGenericErr);
79
0
    return IPC_OK();
80
0
  }
81
0
82
0
  if (aRecordName.IsEmpty()) {
83
0
    LOGD(("GMPStorageParent[%p]::RecvOpen(record='%s') failed; record name empty",
84
0
          this, aRecordName.get()));
85
0
    Unused << SendOpenComplete(aRecordName, GMPGenericErr);
86
0
    return IPC_OK();
87
0
  }
88
0
89
0
  if (mStorage->IsOpen(aRecordName)) {
90
0
    LOGD(("GMPStorageParent[%p]::RecvOpen(record='%s') failed; record in use",
91
0
          this, aRecordName.get()));
92
0
    Unused << SendOpenComplete(aRecordName, GMPRecordInUse);
93
0
    return IPC_OK();
94
0
  }
95
0
96
0
  auto err = mStorage->Open(aRecordName);
97
0
  MOZ_ASSERT(GMP_FAILED(err) || mStorage->IsOpen(aRecordName));
98
0
  LOGD(("GMPStorageParent[%p]::RecvOpen(record='%s') complete; rv=%d",
99
0
        this, aRecordName.get(), err));
100
0
  Unused << SendOpenComplete(aRecordName, err);
101
0
102
0
  return IPC_OK();
103
0
}
104
105
mozilla::ipc::IPCResult
106
GMPStorageParent::RecvRead(const nsCString& aRecordName)
107
0
{
108
0
  LOGD(("GMPStorageParent[%p]::RecvRead(record='%s')",
109
0
       this, aRecordName.get()));
110
0
111
0
  if (mShutdown) {
112
0
    return IPC_FAIL_NO_REASON(this);
113
0
  }
114
0
115
0
  nsTArray<uint8_t> data;
116
0
  if (!mStorage->IsOpen(aRecordName)) {
117
0
    LOGD(("GMPStorageParent[%p]::RecvRead(record='%s') failed; record not open",
118
0
         this, aRecordName.get()));
119
0
    Unused << SendReadComplete(aRecordName, GMPClosedErr, data);
120
0
  } else {
121
0
    GMPErr rv = mStorage->Read(aRecordName, data);
122
0
    LOGD(("GMPStorageParent[%p]::RecvRead(record='%s') read %zu bytes rv=%" PRIu32,
123
0
          this, aRecordName.get(), data.Length(), static_cast<uint32_t>(rv)));
124
0
    Unused << SendReadComplete(aRecordName, rv, data);
125
0
  }
126
0
127
0
  return IPC_OK();
128
0
}
129
130
mozilla::ipc::IPCResult
131
GMPStorageParent::RecvWrite(const nsCString& aRecordName,
132
                            InfallibleTArray<uint8_t>&& aBytes)
133
0
{
134
0
  LOGD(("GMPStorageParent[%p]::RecvWrite(record='%s') %zu bytes",
135
0
        this, aRecordName.get(), aBytes.Length()));
136
0
137
0
  if (mShutdown) {
138
0
    return IPC_FAIL_NO_REASON(this);
139
0
  }
140
0
141
0
  if (!mStorage->IsOpen(aRecordName)) {
142
0
    LOGD(("GMPStorageParent[%p]::RecvWrite(record='%s') failed record not open",
143
0
          this, aRecordName.get()));
144
0
    Unused << SendWriteComplete(aRecordName, GMPClosedErr);
145
0
    return IPC_OK();
146
0
  }
147
0
148
0
  if (aBytes.Length() > GMP_MAX_RECORD_SIZE) {
149
0
    LOGD(("GMPStorageParent[%p]::RecvWrite(record='%s') failed record too big",
150
0
          this, aRecordName.get()));
151
0
    Unused << SendWriteComplete(aRecordName, GMPQuotaExceededErr);
152
0
    return IPC_OK();
153
0
  }
154
0
155
0
  GMPErr rv = mStorage->Write(aRecordName, aBytes);
156
0
  LOGD(("GMPStorageParent[%p]::RecvWrite(record='%s') write complete rv=%d",
157
0
        this, aRecordName.get(), rv));
158
0
159
0
  Unused << SendWriteComplete(aRecordName, rv);
160
0
161
0
  return IPC_OK();
162
0
}
163
164
mozilla::ipc::IPCResult
165
GMPStorageParent::RecvClose(const nsCString& aRecordName)
166
0
{
167
0
  LOGD(("GMPStorageParent[%p]::RecvClose(record='%s')",
168
0
        this, aRecordName.get()));
169
0
170
0
  if (mShutdown) {
171
0
    return IPC_OK();
172
0
  }
173
0
174
0
  mStorage->Close(aRecordName);
175
0
176
0
  return IPC_OK();
177
0
}
178
179
void
180
GMPStorageParent::ActorDestroy(ActorDestroyReason aWhy)
181
0
{
182
0
  LOGD(("GMPStorageParent[%p]::ActorDestroy(reason=%d)", this, aWhy));
183
0
  Shutdown();
184
0
}
185
186
void
187
GMPStorageParent::Shutdown()
188
0
{
189
0
  LOGD(("GMPStorageParent[%p]::Shutdown()", this));
190
0
191
0
  if (mShutdown) {
192
0
    return;
193
0
  }
194
0
  mShutdown = true;
195
0
  Unused << SendShutdown();
196
0
197
0
  mStorage = nullptr;
198
0
199
0
}
200
201
} // namespace gmp
202
} // namespace mozilla