1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "QuotaObject.h"
9 #include "CanonicalQuotaObject.h"
10 #include "mozilla/dom/quota/IPCQuotaObject.h"
11 #include "mozilla/dom/quota/PRemoteQuotaObject.h"
12 #include "mozilla/dom/quota/RemoteQuotaObjectChild.h"
13 #include "mozilla/dom/quota/RemoteQuotaObjectParent.h"
14 #include "mozilla/dom/quota/RemoteQuotaObjectParentTracker.h"
15 #include "mozilla/ipc/BackgroundParent.h"
16 #include "nsIInterfaceRequestor.h"
17 #include "nsIInterfaceRequestorUtils.h"
18 #include "RemoteQuotaObject.h"
20 namespace mozilla::dom::quota
{
22 CanonicalQuotaObject
* QuotaObject::AsCanonicalQuotaObject() {
23 return mIsRemote
? nullptr : static_cast<CanonicalQuotaObject
*>(this);
26 RemoteQuotaObject
* QuotaObject::AsRemoteQuotaObject() {
27 return mIsRemote
? static_cast<RemoteQuotaObject
*>(this) : nullptr;
30 IPCQuotaObject
QuotaObject::Serialize(nsIInterfaceRequestor
* aCallbacks
) {
31 MOZ_RELEASE_ASSERT(XRE_IsParentProcess());
32 MOZ_RELEASE_ASSERT(!NS_IsMainThread());
33 MOZ_RELEASE_ASSERT(!mozilla::ipc::IsOnBackgroundThread());
34 MOZ_RELEASE_ASSERT(!GetCurrentThreadWorkerPrivate());
35 MOZ_RELEASE_ASSERT(!mIsRemote
);
37 mozilla::ipc::Endpoint
<PRemoteQuotaObjectParent
> parentEndpoint
;
38 mozilla::ipc::Endpoint
<PRemoteQuotaObjectChild
> childEndpoint
;
40 PRemoteQuotaObject::CreateEndpoints(&parentEndpoint
, &childEndpoint
));
42 nsCOMPtr
<RemoteQuotaObjectParentTracker
> tracker
=
43 do_GetInterface(aCallbacks
);
46 MakeRefPtr
<RemoteQuotaObjectParent
>(AsCanonicalQuotaObject(), tracker
);
49 tracker
->RegisterRemoteQuotaObjectParent(WrapNotNull(actor
));
52 parentEndpoint
.Bind(actor
);
54 IPCQuotaObject ipcQuotaObject
;
55 ipcQuotaObject
.childEndpoint() = std::move(childEndpoint
);
57 return ipcQuotaObject
;
61 RefPtr
<QuotaObject
> QuotaObject::Deserialize(IPCQuotaObject
& aQuotaObject
) {
62 MOZ_RELEASE_ASSERT(!NS_IsMainThread());
63 MOZ_RELEASE_ASSERT(!mozilla::ipc::IsOnBackgroundThread());
64 MOZ_RELEASE_ASSERT(!GetCurrentThreadWorkerPrivate());
66 auto actor
= MakeRefPtr
<RemoteQuotaObjectChild
>();
68 aQuotaObject
.childEndpoint().Bind(actor
);
70 return MakeRefPtr
<RemoteQuotaObject
>(actor
);
73 } // namespace mozilla::dom::quota