summaryrefslogtreecommitdiffstats
path: root/dom/serviceworkers/RemoteServiceWorkerImpl.cpp
blob: 9fa898cb55079450d45f7523fa4c4e35ba10f241 (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
/* -*- 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 "RemoteServiceWorkerImpl.h"

#include <utility>

#include "mozilla/dom/ClientInfo.h"
#include "mozilla/dom/ClientState.h"
#include "mozilla/ipc/BackgroundChild.h"
#include "mozilla/ipc/PBackgroundChild.h"
#include "ServiceWorkerChild.h"
#include "ServiceWorkerCloneData.h"

namespace mozilla {
namespace dom {

using mozilla::ipc::BackgroundChild;
using mozilla::ipc::PBackgroundChild;

RemoteServiceWorkerImpl::~RemoteServiceWorkerImpl() {
  MOZ_DIAGNOSTIC_ASSERT(!mWorker);
  Shutdown();
}

void RemoteServiceWorkerImpl::Shutdown() {
  if (mShutdown) {
    return;
  }
  mShutdown = true;

  if (mActor) {
    mActor->RevokeOwner(this);
    mActor->MaybeStartTeardown();
    mActor = nullptr;
  }
}

void RemoteServiceWorkerImpl::AddServiceWorker(ServiceWorker* aWorker) {
  NS_ASSERT_OWNINGTHREAD(RemoteServiceWorkerImpl);
  MOZ_DIAGNOSTIC_ASSERT(!mWorker);
  MOZ_DIAGNOSTIC_ASSERT(aWorker);
  mWorker = aWorker;
}

void RemoteServiceWorkerImpl::RemoveServiceWorker(ServiceWorker* aWorker) {
  NS_ASSERT_OWNINGTHREAD(RemoteServiceWorkerImpl);
  MOZ_DIAGNOSTIC_ASSERT(mWorker);
  MOZ_DIAGNOSTIC_ASSERT(aWorker == mWorker);
  mWorker = nullptr;
}

void RemoteServiceWorkerImpl::GetRegistration(
    ServiceWorkerRegistrationCallback&& aSuccessCB,
    ServiceWorkerFailureCallback&& aFailureCB) {
  // TODO
}

void RemoteServiceWorkerImpl::PostMessage(
    RefPtr<ServiceWorkerCloneData>&& aData, const ClientInfo& aClientInfo,
    const ClientState& aClientState) {
  NS_ASSERT_OWNINGTHREAD(RemoteServiceWorkerImpl);
  if (!mActor) {
    return;
  }

  ClonedOrErrorMessageData data;
  if (!aData->BuildClonedMessageDataForBackgroundChild(mActor->Manager(),
                                                       data)) {
    return;
  }

  mActor->SendPostMessage(
      data, ClientInfoAndState(aClientInfo.ToIPC(), aClientState.ToIPC()));
}

RemoteServiceWorkerImpl::RemoteServiceWorkerImpl(
    const ServiceWorkerDescriptor& aDescriptor)
    : mWorker(nullptr), mShutdown(false) {
  PBackgroundChild* parentActor =
      BackgroundChild::GetOrCreateForCurrentThread();
  if (NS_WARN_IF(!parentActor)) {
    Shutdown();
    return;
  }

  RefPtr<ServiceWorkerChild> actor = ServiceWorkerChild::Create();
  if (NS_WARN_IF(!actor)) {
    Shutdown();
    return;
  }

  PServiceWorkerChild* sentActor =
      parentActor->SendPServiceWorkerConstructor(actor, aDescriptor.ToIPC());
  if (NS_WARN_IF(!sentActor)) {
    Shutdown();
    return;
  }
  MOZ_DIAGNOSTIC_ASSERT(sentActor == actor);

  mActor = std::move(actor);
  mActor->SetOwner(this);
}

void RemoteServiceWorkerImpl::RevokeActor(ServiceWorkerChild* aActor) {
  MOZ_DIAGNOSTIC_ASSERT(mActor);
  MOZ_DIAGNOSTIC_ASSERT(mActor == aActor);
  mActor->RevokeOwner(this);
  mActor = nullptr;

  mShutdown = true;
}

}  // namespace dom
}  // namespace mozilla