Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/serviceworkers/ServiceWorkerProxy.cpp
Line
Count
Source (jump to first uncovered line)
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
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include "ServiceWorkerProxy.h"
8
9
#include "mozilla/ipc/BackgroundParent.h"
10
#include "ServiceWorkerInfo.h"
11
12
namespace mozilla {
13
namespace dom {
14
15
using mozilla::ipc::AssertIsOnBackgroundThread;
16
17
ServiceWorkerProxy::~ServiceWorkerProxy()
18
0
{
19
0
  // Any thread
20
0
  MOZ_DIAGNOSTIC_ASSERT(!mActor);
21
0
  MOZ_DIAGNOSTIC_ASSERT(!mInfo);
22
0
}
23
24
void
25
ServiceWorkerProxy::MaybeShutdownOnBGThread()
26
0
{
27
0
  AssertIsOnBackgroundThread();
28
0
  if (!mActor) {
29
0
    return;
30
0
  }
31
0
  mActor->MaybeSendDelete();
32
0
}
33
34
void
35
ServiceWorkerProxy::InitOnMainThread()
36
0
{
37
0
  AssertIsOnMainThread();
38
0
39
0
  auto scopeExit = MakeScopeExit([&] {
40
0
    MaybeShutdownOnMainThread();
41
0
  });
42
0
43
0
  RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
44
0
  NS_ENSURE_TRUE_VOID(swm);
45
0
46
0
  RefPtr<ServiceWorkerRegistrationInfo> reg =
47
0
    swm->GetRegistration(mDescriptor.PrincipalInfo(), mDescriptor.Scope());
48
0
  NS_ENSURE_TRUE_VOID(reg);
49
0
50
0
  RefPtr<ServiceWorkerInfo> info = reg->GetByDescriptor(mDescriptor);
51
0
  NS_ENSURE_TRUE_VOID(info);
52
0
53
0
  scopeExit.release();
54
0
55
0
  mInfo = new nsMainThreadPtrHolder<ServiceWorkerInfo>("ServiceWorkerProxy::mInfo",
56
0
                                                       info);
57
0
}
58
59
void
60
ServiceWorkerProxy::MaybeShutdownOnMainThread()
61
0
{
62
0
  AssertIsOnMainThread();
63
0
64
0
  nsCOMPtr<nsIRunnable> r =
65
0
    NewRunnableMethod(__func__, this,
66
0
                      &ServiceWorkerProxy::MaybeShutdownOnBGThread);
67
0
68
0
  MOZ_ALWAYS_SUCCEEDS(mEventTarget->Dispatch(r.forget(), NS_DISPATCH_NORMAL));
69
0
}
70
71
void
72
ServiceWorkerProxy::StopListeningOnMainThread()
73
0
{
74
0
  AssertIsOnMainThread();
75
0
  mInfo = nullptr;
76
0
}
77
78
ServiceWorkerProxy::ServiceWorkerProxy(const ServiceWorkerDescriptor& aDescriptor)
79
  : mActor(nullptr)
80
  , mEventTarget(GetCurrentThreadSerialEventTarget())
81
  , mDescriptor(aDescriptor)
82
0
{
83
0
}
84
85
void
86
ServiceWorkerProxy::Init(ServiceWorkerParent* aActor)
87
0
{
88
0
  AssertIsOnBackgroundThread();
89
0
  MOZ_DIAGNOSTIC_ASSERT(aActor);
90
0
  MOZ_DIAGNOSTIC_ASSERT(!mActor);
91
0
  MOZ_DIAGNOSTIC_ASSERT(mEventTarget);
92
0
93
0
  mActor = aActor;
94
0
95
0
  // Note, this must be done from a separate Init() method and not in
96
0
  // the constructor.  If done from the constructor the runnable can
97
0
  // execute, complete, and release its reference before the constructor
98
0
  // returns.
99
0
  nsCOMPtr<nsIRunnable> r = NewRunnableMethod("ServiceWorkerProxy::Init", this,
100
0
                                              &ServiceWorkerProxy::InitOnMainThread);
101
0
  MOZ_ALWAYS_SUCCEEDS(SystemGroup::Dispatch(TaskCategory::Other, r.forget()));
102
0
}
103
104
void
105
ServiceWorkerProxy::RevokeActor(ServiceWorkerParent* aActor)
106
0
{
107
0
  AssertIsOnBackgroundThread();
108
0
  MOZ_DIAGNOSTIC_ASSERT(mActor);
109
0
  MOZ_DIAGNOSTIC_ASSERT(mActor == aActor);
110
0
  mActor = nullptr;
111
0
112
0
  nsCOMPtr<nsIRunnable> r =
113
0
    NewRunnableMethod(__func__, this,
114
0
                      &ServiceWorkerProxy::StopListeningOnMainThread);
115
0
  MOZ_ALWAYS_SUCCEEDS(SystemGroup::Dispatch(TaskCategory::Other, r.forget()));
116
0
}
117
118
void
119
ServiceWorkerProxy::PostMessage(RefPtr<ServiceWorkerCloneData>&& aData,
120
                                const ClientInfo& aClientInfo,
121
                                const ClientState& aClientState)
122
0
{
123
0
  AssertIsOnBackgroundThread();
124
0
  RefPtr<ServiceWorkerProxy> self = this;
125
0
  nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction(__func__,
126
0
    [self, data = std::move(aData), aClientInfo, aClientState] () mutable {
127
0
      if (!self->mInfo) {
128
0
        return;
129
0
      }
130
0
      self->mInfo->PostMessage(std::move(data), aClientInfo, aClientState);
131
0
    });
132
0
  MOZ_ALWAYS_SUCCEEDS(SystemGroup::Dispatch(TaskCategory::Other, r.forget()));
133
0
}
134
135
} // namespace dom
136
} // namespace mozilla