summaryrefslogtreecommitdiffstats
path: root/dom/serviceworkers/ServiceWorkerDescriptor.cpp
blob: 898f271feaf32e933be77d95d4c41fe952c4ea5d (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
/* -*- 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 "ServiceWorkerDescriptor.h"
#include "mozilla/dom/IPCServiceWorkerDescriptor.h"
#include "mozilla/dom/ServiceWorkerBinding.h"
#include "mozilla/ipc/PBackgroundSharedTypes.h"

namespace mozilla::dom {

using mozilla::ipc::PrincipalInfo;
using mozilla::ipc::PrincipalInfoToPrincipal;

ServiceWorkerDescriptor::ServiceWorkerDescriptor(
    uint64_t aId, uint64_t aRegistrationId, uint64_t aRegistrationVersion,
    nsIPrincipal* aPrincipal, const nsACString& aScope,
    const nsACString& aScriptURL, ServiceWorkerState aState)
    : mData(MakeUnique<IPCServiceWorkerDescriptor>()) {
  MOZ_ALWAYS_SUCCEEDS(
      PrincipalToPrincipalInfo(aPrincipal, &mData->principalInfo()));

  mData->id() = aId;
  mData->registrationId() = aRegistrationId;
  mData->registrationVersion() = aRegistrationVersion;
  mData->scope() = aScope;
  mData->scriptURL() = aScriptURL;
  mData->state() = aState;
  // Set HandlesFetch as true in default
  mData->handlesFetch() = true;
}

ServiceWorkerDescriptor::ServiceWorkerDescriptor(
    uint64_t aId, uint64_t aRegistrationId, uint64_t aRegistrationVersion,
    const mozilla::ipc::PrincipalInfo& aPrincipalInfo, const nsACString& aScope,
    const nsACString& aScriptURL, ServiceWorkerState aState)
    : mData(MakeUnique<IPCServiceWorkerDescriptor>(
          aId, aRegistrationId, aRegistrationVersion, aPrincipalInfo,
          nsCString(aScriptURL), nsCString(aScope), aState, true)) {}

ServiceWorkerDescriptor::ServiceWorkerDescriptor(
    const IPCServiceWorkerDescriptor& aDescriptor)
    : mData(MakeUnique<IPCServiceWorkerDescriptor>(aDescriptor)) {}

ServiceWorkerDescriptor::ServiceWorkerDescriptor(
    const ServiceWorkerDescriptor& aRight) {
  operator=(aRight);
}

ServiceWorkerDescriptor& ServiceWorkerDescriptor::operator=(
    const ServiceWorkerDescriptor& aRight) {
  if (this == &aRight) {
    return *this;
  }
  mData.reset();
  mData = MakeUnique<IPCServiceWorkerDescriptor>(*aRight.mData);
  return *this;
}

ServiceWorkerDescriptor::ServiceWorkerDescriptor(
    ServiceWorkerDescriptor&& aRight)
    : mData(std::move(aRight.mData)) {}

ServiceWorkerDescriptor& ServiceWorkerDescriptor::operator=(
    ServiceWorkerDescriptor&& aRight) {
  mData.reset();
  mData = std::move(aRight.mData);
  return *this;
}

ServiceWorkerDescriptor::~ServiceWorkerDescriptor() = default;

bool ServiceWorkerDescriptor::operator==(
    const ServiceWorkerDescriptor& aRight) const {
  return *mData == *aRight.mData;
}

uint64_t ServiceWorkerDescriptor::Id() const { return mData->id(); }

uint64_t ServiceWorkerDescriptor::RegistrationId() const {
  return mData->registrationId();
}

uint64_t ServiceWorkerDescriptor::RegistrationVersion() const {
  return mData->registrationVersion();
}

const mozilla::ipc::PrincipalInfo& ServiceWorkerDescriptor::PrincipalInfo()
    const {
  return mData->principalInfo();
}

Result<nsCOMPtr<nsIPrincipal>, nsresult> ServiceWorkerDescriptor::GetPrincipal()
    const {
  AssertIsOnMainThread();
  return PrincipalInfoToPrincipal(mData->principalInfo());
}

const nsCString& ServiceWorkerDescriptor::Scope() const {
  return mData->scope();
}

const nsCString& ServiceWorkerDescriptor::ScriptURL() const {
  return mData->scriptURL();
}

ServiceWorkerState ServiceWorkerDescriptor::State() const {
  return mData->state();
}

void ServiceWorkerDescriptor::SetState(ServiceWorkerState aState) {
  mData->state() = aState;
}

void ServiceWorkerDescriptor::SetRegistrationVersion(uint64_t aVersion) {
  MOZ_DIAGNOSTIC_ASSERT(aVersion > mData->registrationVersion());
  mData->registrationVersion() = aVersion;
}

bool ServiceWorkerDescriptor::HandlesFetch() const {
  return mData->handlesFetch();
}

void ServiceWorkerDescriptor::SetHandlesFetch(bool aHandlesFetch) {
  mData->handlesFetch() = aHandlesFetch;
}

bool ServiceWorkerDescriptor::Matches(
    const ServiceWorkerDescriptor& aDescriptor) const {
  return Id() == aDescriptor.Id() && Scope() == aDescriptor.Scope() &&
         ScriptURL() == aDescriptor.ScriptURL() &&
         PrincipalInfo() == aDescriptor.PrincipalInfo();
}

const IPCServiceWorkerDescriptor& ServiceWorkerDescriptor::ToIPC() const {
  return *mData;
}

}  // namespace mozilla::dom