summaryrefslogtreecommitdiffstats
path: root/dom/serviceworkers/ServiceWorkerManagerParent.cpp
blob: d2d0a6427557aab58c599122bf7b84d6ad9658fe (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
/* -*- 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 "ServiceWorkerManagerParent.h"
#include "ServiceWorkerUtils.h"
#include "mozilla/dom/ContentParent.h"
#include "mozilla/dom/ServiceWorkerRegistrar.h"
#include "mozilla/ipc/BackgroundParent.h"
#include "mozilla/ipc/BackgroundUtils.h"
#include "mozilla/Unused.h"
#include "nsThreadUtils.h"

namespace mozilla {

using namespace ipc;

namespace dom {

ServiceWorkerManagerParent::ServiceWorkerManagerParent() {
  AssertIsOnBackgroundThread();
}

ServiceWorkerManagerParent::~ServiceWorkerManagerParent() {
  AssertIsOnBackgroundThread();
}

mozilla::ipc::IPCResult ServiceWorkerManagerParent::RecvRegister(
    const ServiceWorkerRegistrationData& aData) {
  AssertIsInMainProcess();
  AssertIsOnBackgroundThread();
  MOZ_ASSERT(!BackgroundParent::IsOtherProcessActor(Manager()));

  // Basic validation.
  if (aData.scope().IsEmpty() ||
      aData.principal().type() == PrincipalInfo::TNullPrincipalInfo ||
      aData.principal().type() == PrincipalInfo::TSystemPrincipalInfo) {
    return IPC_FAIL_NO_REASON(this);
  }

  // If false then we have shutdown during the process of trying to update the
  // registrar. We can give up on this modification.
  if (const RefPtr<dom::ServiceWorkerRegistrar> service =
          dom::ServiceWorkerRegistrar::Get()) {
    service->RegisterServiceWorker(aData);
  }

  return IPC_OK();
}

mozilla::ipc::IPCResult ServiceWorkerManagerParent::RecvUnregister(
    const PrincipalInfo& aPrincipalInfo, const nsString& aScope) {
  AssertIsInMainProcess();
  AssertIsOnBackgroundThread();
  MOZ_ASSERT(!BackgroundParent::IsOtherProcessActor(Manager()));

  // Basic validation.
  if (aScope.IsEmpty() ||
      aPrincipalInfo.type() == PrincipalInfo::TNullPrincipalInfo ||
      aPrincipalInfo.type() == PrincipalInfo::TSystemPrincipalInfo) {
    return IPC_FAIL_NO_REASON(this);
  }

  // If false then we have shutdown during the process of trying to update the
  // registrar. We can give up on this modification.
  if (const RefPtr<dom::ServiceWorkerRegistrar> service =
          dom::ServiceWorkerRegistrar::Get()) {
    service->UnregisterServiceWorker(aPrincipalInfo,
                                     NS_ConvertUTF16toUTF8(aScope));
  }

  return IPC_OK();
}

mozilla::ipc::IPCResult ServiceWorkerManagerParent::RecvPropagateUnregister(
    const PrincipalInfo& aPrincipalInfo, const nsString& aScope) {
  AssertIsOnBackgroundThread();

  RefPtr<dom::ServiceWorkerRegistrar> service =
      dom::ServiceWorkerRegistrar::Get();
  MOZ_ASSERT(service);

  // It's possible that we don't have any ServiceWorkerManager managing this
  // scope but we still need to unregister it from the ServiceWorkerRegistrar.
  service->UnregisterServiceWorker(aPrincipalInfo,
                                   NS_ConvertUTF16toUTF8(aScope));

  // There is no longer any point to propagating because the only sender is the
  // one and only ServiceWorkerManager, but it is necessary for us to have run
  // the unregister call above because until Bug 1183245 is fixed,
  // nsIServiceWorkerManager.propagateUnregister() is a de facto API for
  // clearing ServiceWorker registrations by Sanitizer.sys.mjs via
  // ServiceWorkerCleanUp.sys.mjs, as well as devtools "unregister" affordance
  // and the no-longer-relevant about:serviceworkers UI.

  return IPC_OK();
}

void ServiceWorkerManagerParent::ActorDestroy(ActorDestroyReason aWhy) {
  AssertIsOnBackgroundThread();
}

}  // namespace dom
}  // namespace mozilla