summaryrefslogtreecommitdiffstats
path: root/dom/serviceworkers/ServiceWorkerShutdownState.cpp
blob: 686f31487703ee490b7963c33d29e5bd7d5827d5 (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
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
/* -*- 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 "ServiceWorkerShutdownState.h"

#include <array>
#include <type_traits>

#include "MainThreadUtils.h"
#include "ServiceWorkerUtils.h"
#include "mozilla/Assertions.h"
#include "mozilla/SchedulerGroup.h"
#include "mozilla/Unused.h"
#include "mozilla/dom/ContentChild.h"
#include "mozilla/dom/RemoteWorkerService.h"
#include "mozilla/dom/ServiceWorkerManager.h"
#include "mozilla/dom/WorkerCommon.h"
#include "mozilla/ipc/BackgroundParent.h"
#include "nsDebug.h"
#include "nsThreadUtils.h"
#include "nsXULAppAPI.h"

namespace mozilla::dom {

using Progress = ServiceWorkerShutdownState::Progress;

namespace {

constexpr inline auto UnderlyingProgressValue(Progress aProgress) {
  return std::underlying_type_t<Progress>(aProgress);
}

constexpr std::array<const char*, UnderlyingProgressValue(Progress::EndGuard_)>
    gProgressStrings = {{
        // clang-format off
      "parent process main thread",
      "parent process IPDL background thread",
      "content process worker launcher thread",
      "content process main thread",
      "shutdown completed"
        // clang-format on
    }};

}  // anonymous namespace

ServiceWorkerShutdownState::ServiceWorkerShutdownState()
    : mProgress(Progress::ParentProcessMainThread) {
  MOZ_ASSERT(XRE_IsParentProcess());
  MOZ_ASSERT(NS_IsMainThread());
}

ServiceWorkerShutdownState::~ServiceWorkerShutdownState() {
  Unused << NS_WARN_IF(mProgress != Progress::ShutdownCompleted);
}

const char* ServiceWorkerShutdownState::GetProgressString() const {
  return gProgressStrings[UnderlyingProgressValue(mProgress)];
}

void ServiceWorkerShutdownState::SetProgress(Progress aProgress) {
  MOZ_ASSERT(aProgress != Progress::EndGuard_);
  MOZ_RELEASE_ASSERT(UnderlyingProgressValue(mProgress) + 1 ==
                     UnderlyingProgressValue(aProgress));

  mProgress = aProgress;
}

namespace {

void ReportProgressToServiceWorkerManager(uint32_t aShutdownStateId,
                                          Progress aProgress) {
  MOZ_ASSERT(XRE_IsParentProcess());
  MOZ_ASSERT(NS_IsMainThread());

  RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
  MOZ_RELEASE_ASSERT(swm, "ServiceWorkers should shutdown before SWM.");

  swm->ReportServiceWorkerShutdownProgress(aShutdownStateId, aProgress);
}

void ReportProgressToParentProcess(uint32_t aShutdownStateId,
                                   Progress aProgress) {
  MOZ_ASSERT(XRE_IsContentProcess());
  MOZ_ASSERT(NS_IsMainThread());

  ContentChild* contentChild = ContentChild::GetSingleton();
  MOZ_ASSERT(contentChild);

  contentChild->SendReportServiceWorkerShutdownProgress(aShutdownStateId,
                                                        aProgress);
}

void ReportServiceWorkerShutdownProgress(uint32_t aShutdownStateId,
                                         Progress aProgress) {
  MOZ_ASSERT(UnderlyingProgressValue(Progress::ParentProcessMainThread) <
             UnderlyingProgressValue(aProgress));
  MOZ_ASSERT(UnderlyingProgressValue(aProgress) <
             UnderlyingProgressValue(Progress::EndGuard_));

  nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction(
      __func__, [shutdownStateId = aShutdownStateId, progress = aProgress] {
        if (XRE_IsParentProcess()) {
          ReportProgressToServiceWorkerManager(shutdownStateId, progress);
        } else {
          ReportProgressToParentProcess(shutdownStateId, progress);
        }
      });

  if (NS_IsMainThread()) {
    MOZ_ALWAYS_SUCCEEDS(r->Run());
  } else {
    MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(r.forget()));
  }
}

void ReportServiceWorkerShutdownProgress(uint32_t aShutdownStateId) {
  Progress progress = Progress::EndGuard_;

  if (XRE_IsParentProcess()) {
    mozilla::ipc::AssertIsOnBackgroundThread();

    progress = Progress::ParentProcessIpdlBackgroundThread;
  } else {
    if (NS_IsMainThread()) {
      progress = Progress::ContentProcessMainThread;
    } else {
      MOZ_ASSERT(RemoteWorkerService::Thread()->IsOnCurrentThread());
      progress = Progress::ContentProcessWorkerLauncherThread;
    }
  }

  ReportServiceWorkerShutdownProgress(aShutdownStateId, progress);
}

}  // anonymous namespace

void MaybeReportServiceWorkerShutdownProgress(const ServiceWorkerOpArgs& aArgs,
                                              bool aShutdownCompleted) {
  if (XRE_IsParentProcess() && !XRE_IsE10sParentProcess()) {
    return;
  }

  if (aShutdownCompleted) {
    MOZ_ASSERT(aArgs.type() ==
               ServiceWorkerOpArgs::TServiceWorkerTerminateWorkerOpArgs);

    ReportServiceWorkerShutdownProgress(
        aArgs.get_ServiceWorkerTerminateWorkerOpArgs().shutdownStateId(),
        Progress::ShutdownCompleted);

    return;
  }

  if (aArgs.type() ==
      ServiceWorkerOpArgs::TServiceWorkerTerminateWorkerOpArgs) {
    ReportServiceWorkerShutdownProgress(
        aArgs.get_ServiceWorkerTerminateWorkerOpArgs().shutdownStateId());
  }
}

}  // namespace mozilla::dom