Source code

Revision control

Other Tools

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
#ifndef mozilla_dom_serviceworkerinfo_h
8
#define mozilla_dom_serviceworkerinfo_h
9
10
#include "MainThreadUtils.h"
11
#include "mozilla/dom/ServiceWorkerBinding.h" // For ServiceWorkerState
12
#include "mozilla/dom/WorkerCommon.h"
13
#include "mozilla/OriginAttributes.h"
14
#include "nsIServiceWorkerManager.h"
15
16
namespace mozilla {
17
namespace dom {
18
19
class ClientInfoAndState;
20
class ClientState;
21
class ServiceWorkerCloneData;
22
class ServiceWorkerPrivate;
23
24
/*
25
* Wherever the spec treats a worker instance and a description of said worker
26
* as the same thing; i.e. "Resolve foo with
27
* _GetNewestWorker(serviceWorkerRegistration)", we represent the description
28
* by this class and spawn a ServiceWorker in the right global when required.
29
*/
30
class ServiceWorkerInfo final : public nsIServiceWorkerInfo {
31
private:
32
nsCOMPtr<nsIPrincipal> mPrincipal;
33
ServiceWorkerDescriptor mDescriptor;
34
const nsString mCacheName;
35
OriginAttributes mOriginAttributes;
36
const nsString mWorkerPrivateId;
37
38
// This LoadFlags is only applied to imported scripts, since the main script
39
// has already been downloaded when performing the bytecheck. This LoadFlag is
40
// composed of three parts:
41
// 1. nsIChannel::LOAD_BYPASS_SERVICE_WORKER
42
// 2. (Optional) nsIRequest::VALIDATE_ALWAYS
43
// depends on ServiceWorkerUpdateViaCache of its registration.
44
// 3. (optional) nsIRequest::LOAD_BYPASS_CACHE
45
// depends on whether the update timer is expired.
46
const nsLoadFlags mImportsLoadFlags;
47
48
// Timestamp to track SW's state
49
PRTime mCreationTime;
50
TimeStamp mCreationTimeStamp;
51
52
// The time of states are 0, if SW has not reached that state yet. Besides, we
53
// update each of them after UpdateState() is called in SWRegistrationInfo.
54
PRTime mInstalledTime;
55
PRTime mActivatedTime;
56
PRTime mRedundantTime;
57
58
RefPtr<ServiceWorkerPrivate> mServiceWorkerPrivate;
59
bool mSkipWaitingFlag;
60
61
enum { Unknown, Enabled, Disabled } mHandlesFetch;
62
63
~ServiceWorkerInfo();
64
65
// Generates a unique id for the service worker, with zero being treated as
66
// invalid.
67
uint64_t GetNextID() const;
68
69
public:
70
NS_DECL_ISUPPORTS
71
NS_DECL_NSISERVICEWORKERINFO
72
73
void PostMessage(RefPtr<ServiceWorkerCloneData>&& aData,
74
const ClientInfo& aClientInfo,
75
const ClientState& aClientState);
76
77
class ServiceWorkerPrivate* WorkerPrivate() const {
78
MOZ_ASSERT(mServiceWorkerPrivate);
79
return mServiceWorkerPrivate;
80
}
81
82
nsIPrincipal* Principal() const { return mPrincipal; }
83
84
const nsCString& ScriptSpec() const { return mDescriptor.ScriptURL(); }
85
86
const nsCString& Scope() const { return mDescriptor.Scope(); }
87
88
bool SkipWaitingFlag() const {
89
MOZ_ASSERT(NS_IsMainThread());
90
return mSkipWaitingFlag;
91
}
92
93
void SetSkipWaitingFlag() {
94
MOZ_ASSERT(NS_IsMainThread());
95
mSkipWaitingFlag = true;
96
}
97
98
ServiceWorkerInfo(nsIPrincipal* aPrincipal, const nsACString& aScope,
99
uint64_t aRegistrationId, uint64_t aRegistrationVersion,
100
const nsACString& aScriptSpec, const nsAString& aCacheName,
101
nsLoadFlags aLoadFlags);
102
103
ServiceWorkerState State() const { return mDescriptor.State(); }
104
105
const OriginAttributes& GetOriginAttributes() const {
106
return mOriginAttributes;
107
}
108
109
const nsString& CacheName() const { return mCacheName; }
110
111
nsLoadFlags GetImportsLoadFlags() const { return mImportsLoadFlags; }
112
113
uint64_t ID() const { return mDescriptor.Id(); }
114
115
const ServiceWorkerDescriptor& Descriptor() const { return mDescriptor; }
116
117
void UpdateState(ServiceWorkerState aState);
118
119
// Only used to set initial state when loading from disk!
120
void SetActivateStateUncheckedWithoutEvent(ServiceWorkerState aState) {
121
MOZ_ASSERT(NS_IsMainThread());
122
mDescriptor.SetState(aState);
123
}
124
125
void SetHandlesFetch(bool aHandlesFetch) {
126
MOZ_ASSERT(NS_IsMainThread());
127
MOZ_DIAGNOSTIC_ASSERT(mHandlesFetch == Unknown);
128
mHandlesFetch = aHandlesFetch ? Enabled : Disabled;
129
}
130
131
void SetRegistrationVersion(uint64_t aVersion);
132
133
bool HandlesFetch() const {
134
MOZ_ASSERT(NS_IsMainThread());
135
MOZ_DIAGNOSTIC_ASSERT(mHandlesFetch != Unknown);
136
return mHandlesFetch != Disabled;
137
}
138
139
void UpdateInstalledTime();
140
141
void UpdateActivatedTime();
142
143
void UpdateRedundantTime();
144
145
int64_t GetInstalledTime() const { return mInstalledTime; }
146
147
void SetInstalledTime(const int64_t aTime) {
148
if (aTime == 0) {
149
return;
150
}
151
152
mInstalledTime = aTime;
153
}
154
155
int64_t GetActivatedTime() const { return mActivatedTime; }
156
157
void SetActivatedTime(const int64_t aTime) {
158
if (aTime == 0) {
159
return;
160
}
161
162
mActivatedTime = aTime;
163
}
164
};
165
166
} // namespace dom
167
} // namespace mozilla
168
169
#endif // mozilla_dom_serviceworkerinfo_h