File: ActorsChild.cpp

package info (click to toggle)
firefox 147.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,683,324 kB
  • sloc: cpp: 7,607,156; javascript: 6,532,492; ansic: 3,775,158; python: 1,415,368; xml: 634,556; asm: 438,949; java: 186,241; sh: 62,751; makefile: 18,079; objc: 13,092; perl: 12,808; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (238 lines) | stat: -rw-r--r-- 6,177 bytes parent folder | download | duplicates (11)
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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "ActorsChild.h"

// Local includes
#include "SDBConnection.h"
#include "SDBRequest.h"
#include "SDBResults.h"

// Global includes
#include "mozilla/Assertions.h"
#include "mozilla/dom/PBackgroundSDBRequest.h"
#include "nsError.h"
#include "nsID.h"
#include "nsISDBResults.h"
#include "nsVariant.h"

namespace mozilla::dom {

/*******************************************************************************
 * SDBConnectionChild
 ******************************************************************************/

SDBConnectionChild::SDBConnectionChild(SDBConnection* aConnection)
    : mConnection(aConnection) {
  AssertIsOnOwningThread();
  MOZ_ASSERT(aConnection);

  MOZ_COUNT_CTOR(SDBConnectionChild);
}

SDBConnectionChild::~SDBConnectionChild() {
  AssertIsOnOwningThread();

  MOZ_COUNT_DTOR(SDBConnectionChild);
}

void SDBConnectionChild::SendDeleteMeInternal() {
  AssertIsOnOwningThread();

  if (mConnection) {
    mConnection->ClearBackgroundActor();
    mConnection = nullptr;

    MOZ_ALWAYS_TRUE(PBackgroundSDBConnectionChild::SendDeleteMe());
  }
}

void SDBConnectionChild::ActorDestroy(ActorDestroyReason aWhy) {
  AssertIsOnOwningThread();

  if (mConnection) {
    mConnection->ClearBackgroundActor();
#ifdef DEBUG
    mConnection = nullptr;
#endif
  }
}

PBackgroundSDBRequestChild* SDBConnectionChild::AllocPBackgroundSDBRequestChild(
    const SDBRequestParams& aParams) {
  AssertIsOnOwningThread();

  MOZ_CRASH(
      "PBackgroundSDBRequestChild actors should be manually "
      "constructed!");
}

bool SDBConnectionChild::DeallocPBackgroundSDBRequestChild(
    PBackgroundSDBRequestChild* aActor) {
  AssertIsOnOwningThread();
  MOZ_ASSERT(aActor);

  delete static_cast<SDBRequestChild*>(aActor);
  return true;
}

mozilla::ipc::IPCResult SDBConnectionChild::RecvAllowToClose() {
  AssertIsOnOwningThread();

  if (mConnection) {
    mConnection->AllowToClose();
  }

  return IPC_OK();
}

mozilla::ipc::IPCResult SDBConnectionChild::RecvClosed() {
  AssertIsOnOwningThread();

  if (mConnection) {
    mConnection->OnClose(/* aAbnormal */ true);
  }

  return IPC_OK();
}

/*******************************************************************************
 * SDBRequestChild
 ******************************************************************************/

SDBRequestChild::SDBRequestChild(SDBRequest* aRequest)
    : mConnection(aRequest->GetConnection()), mRequest(aRequest) {
  AssertIsOnOwningThread();
  MOZ_ASSERT(aRequest);

  MOZ_COUNT_CTOR(SDBRequestChild);
}

SDBRequestChild::~SDBRequestChild() {
  AssertIsOnOwningThread();

  MOZ_COUNT_DTOR(SDBRequestChild);
}

#ifdef DEBUG

void SDBRequestChild::AssertIsOnOwningThread() const {
  MOZ_ASSERT(mRequest);
  mRequest->AssertIsOnOwningThread();
}

#endif  // DEBUG

void SDBRequestChild::HandleResponse(nsresult aResponse) {
  AssertIsOnOwningThread();
  MOZ_ASSERT(NS_FAILED(aResponse));
  MOZ_ASSERT(mRequest);

  mRequest->SetError(aResponse);
}

void SDBRequestChild::HandleResponse() {
  AssertIsOnOwningThread();
  MOZ_ASSERT(mRequest);

  RefPtr<nsVariant> variant = new nsVariant();
  variant->SetAsVoid();

  mRequest->SetResult(variant);
}

void SDBRequestChild::HandleResponse(const nsCString& aResponse) {
  AssertIsOnOwningThread();
  MOZ_ASSERT(mRequest);

  RefPtr<SDBResult> result = new SDBResult(aResponse);

  RefPtr<nsVariant> variant = new nsVariant();
  variant->SetAsInterface(NS_GET_IID(nsISDBResult), result);

  mRequest->SetResult(variant);
}

void SDBRequestChild::ActorDestroy(ActorDestroyReason aWhy) {
  AssertIsOnOwningThread();

  if (mConnection) {
    mConnection->AssertIsOnOwningThread();

    mConnection->OnRequestFinished();
#ifdef DEBUG
    mConnection = nullptr;
#endif
  }
}

mozilla::ipc::IPCResult SDBRequestChild::Recv__delete__(
    const SDBRequestResponse& aResponse) {
  AssertIsOnOwningThread();
  MOZ_ASSERT(mRequest);
  MOZ_ASSERT(mConnection);

  switch (aResponse.type()) {
    case SDBRequestResponse::Tnsresult:
      HandleResponse(aResponse.get_nsresult());
      break;

    case SDBRequestResponse::TSDBRequestOpenResponse:
      if (mConnection->IsAllowedToClose()) {
        // If the connection is allowed to close already, then we shouldn't set
        // a result here. Instead we set an abort error.
        HandleResponse(NS_ERROR_ABORT);
      } else {
        HandleResponse();
      }

      // SDBConnection::OnOpen (which sets the SDBConnection::mOpen flag) must
      // be called even when we set an abort error above. The parent is about
      // to send the Closed message to the child and that ends up calling
      // SDBConnection::OnClose which expects the SDBConnection::mOpen flag to
      // be set. It's ok if the SDBConnection::mOpen flag is set to true for
      // a short time after erroring out the open request because if the method
      // SDBConnection::IsAllowToClose returns true it means that the flag
      // SDBConnection::mAllowedToClose is set to true and that prevents any
      // other operation from starting and the SDBConnection is basically
      // unusable.
      mConnection->OnOpen();

      break;

    case SDBRequestResponse::TSDBRequestSeekResponse:
      HandleResponse();
      break;

    case SDBRequestResponse::TSDBRequestReadResponse:
      HandleResponse(aResponse.get_SDBRequestReadResponse().data());
      break;

    case SDBRequestResponse::TSDBRequestWriteResponse:
      HandleResponse();
      break;

    case SDBRequestResponse::TSDBRequestCloseResponse:
      HandleResponse();

      mConnection->OnClose(/* aAbnormal */ false);

      break;

    default:
      MOZ_CRASH("Unknown response type!");
  }

  mConnection->OnRequestFinished();

  // Null this out so that we don't try to call OnRequestFinished() again in
  // ActorDestroy.
  mConnection = nullptr;

  return IPC_OK();
}

}  // namespace mozilla::dom