File: ServiceWorkerInfo.h

package info (click to toggle)
thunderbird 1:60.8.0-1~deb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,339,208 kB
  • sloc: cpp: 5,456,704; ansic: 2,360,384; python: 596,095; asm: 340,963; java: 326,291; xml: 258,664; sh: 84,366; makefile: 23,702; perl: 17,317; objc: 3,768; yacc: 1,766; ada: 1,681; lex: 1,364; pascal: 1,264; cs: 879; exp: 527; php: 436; lisp: 258; ruby: 153; awk: 152; sed: 53; csh: 27
file content (181 lines) | stat: -rw-r--r-- 5,798 bytes parent folder | download | duplicates (7)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* -*- 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/. */

#ifndef mozilla_dom_serviceworkerinfo_h
#define mozilla_dom_serviceworkerinfo_h

#include "MainThreadUtils.h"
#include "mozilla/dom/ServiceWorkerBinding.h"  // For ServiceWorkerState
#include "mozilla/dom/WorkerCommon.h"
#include "mozilla/OriginAttributes.h"
#include "nsIServiceWorkerManager.h"
#include "ServiceWorker.h"

namespace mozilla {
namespace dom {

class ServiceWorkerPrivate;

/*
 * Wherever the spec treats a worker instance and a description of said worker
 * as the same thing; i.e. "Resolve foo with
 * _GetNewestWorker(serviceWorkerRegistration)", we represent the description
 * by this class and spawn a ServiceWorker in the right global when required.
 */
class ServiceWorkerInfo final : public nsIServiceWorkerInfo,
                                public ServiceWorker::Inner {
 private:
  nsCOMPtr<nsIPrincipal> mPrincipal;
  ServiceWorkerDescriptor mDescriptor;
  const nsString mCacheName;
  OriginAttributes mOriginAttributes;

  // This LoadFlags is only applied to imported scripts, since the main script
  // has already been downloaded when performing the bytecheck. This LoadFlag is
  // composed of three parts:
  //   1. nsIChannel::LOAD_BYPASS_SERVICE_WORKER
  //   2. (Optional) nsIRequest::VALIDATE_ALWAYS
  //      depends on ServiceWorkerUpdateViaCache of its registration.
  //   3. (optional) nsIRequest::LOAD_BYPASS_CACHE
  //      depends on whether the update timer is expired.
  const nsLoadFlags mImportsLoadFlags;

  // Timestamp to track SW's state
  PRTime mCreationTime;
  TimeStamp mCreationTimeStamp;

  // The time of states are 0, if SW has not reached that state yet. Besides, we
  // update each of them after UpdateState() is called in SWRegistrationInfo.
  PRTime mInstalledTime;
  PRTime mActivatedTime;
  PRTime mRedundantTime;

  // Track the list of known binding objects so we can fire events on them
  // when appropriate.  These are held using strong references so that they
  // are not GC'd while an event handler is registered and could observe an
  // event.  This reference will create a cycle with the binding object.  The
  // cycle is broken when either the global is detached or the service worker
  // transitions to the redundant state.
  //
  // There is a high chance of there being at least one ServiceWorker
  // associated with this all the time.
  AutoTArray<RefPtr<ServiceWorker>, 1> mInstances;

  RefPtr<ServiceWorkerPrivate> mServiceWorkerPrivate;
  bool mSkipWaitingFlag;

  enum { Unknown, Enabled, Disabled } mHandlesFetch;

  ~ServiceWorkerInfo();

  // Generates a unique id for the service worker, with zero being treated as
  // invalid.
  uint64_t GetNextID() const;

  // ServiceWorker::Inner implementation
  virtual void AddServiceWorker(ServiceWorker* aWorker) override;

  virtual void RemoveServiceWorker(ServiceWorker* aWorker) override;

  virtual void PostMessage(nsIGlobalObject* aGlobal, JSContext* aCx,
                           JS::Handle<JS::Value> aMessage,
                           const Sequence<JSObject*>& aTransferable,
                           ErrorResult& aRv) override;

 public:
  NS_DECL_ISUPPORTS
  NS_DECL_NSISERVICEWORKERINFO

  class ServiceWorkerPrivate* WorkerPrivate() const {
    MOZ_ASSERT(mServiceWorkerPrivate);
    return mServiceWorkerPrivate;
  }

  nsIPrincipal* Principal() const { return mPrincipal; }

  const nsCString& ScriptSpec() const { return mDescriptor.ScriptURL(); }

  const nsCString& Scope() const { return mDescriptor.Scope(); }

  bool SkipWaitingFlag() const {
    MOZ_ASSERT(NS_IsMainThread());
    return mSkipWaitingFlag;
  }

  void SetSkipWaitingFlag() {
    MOZ_ASSERT(NS_IsMainThread());
    mSkipWaitingFlag = true;
  }

  ServiceWorkerInfo(nsIPrincipal* aPrincipal, const nsACString& aScope,
                    const nsACString& aScriptSpec, const nsAString& aCacheName,
                    nsLoadFlags aLoadFlags);

  ServiceWorkerState State() const { return mDescriptor.State(); }

  const OriginAttributes& GetOriginAttributes() const {
    return mOriginAttributes;
  }

  const nsString& CacheName() const { return mCacheName; }

  nsLoadFlags GetImportsLoadFlags() const { return mImportsLoadFlags; }

  uint64_t ID() const { return mDescriptor.Id(); }

  const ServiceWorkerDescriptor& Descriptor() const { return mDescriptor; }

  void UpdateState(ServiceWorkerState aState);

  // Only used to set initial state when loading from disk!
  void SetActivateStateUncheckedWithoutEvent(ServiceWorkerState aState) {
    MOZ_ASSERT(NS_IsMainThread());
    mDescriptor.SetState(aState);
  }

  void SetHandlesFetch(bool aHandlesFetch) {
    MOZ_ASSERT(NS_IsMainThread());
    MOZ_DIAGNOSTIC_ASSERT(mHandlesFetch == Unknown);
    mHandlesFetch = aHandlesFetch ? Enabled : Disabled;
  }

  bool HandlesFetch() const {
    MOZ_ASSERT(NS_IsMainThread());
    MOZ_DIAGNOSTIC_ASSERT(mHandlesFetch != Unknown);
    return mHandlesFetch != Disabled;
  }

  void UpdateInstalledTime();

  void UpdateActivatedTime();

  void UpdateRedundantTime();

  int64_t GetInstalledTime() const { return mInstalledTime; }

  void SetInstalledTime(const int64_t aTime) {
    if (aTime == 0) {
      return;
    }

    mInstalledTime = aTime;
  }

  int64_t GetActivatedTime() const { return mActivatedTime; }

  void SetActivatedTime(const int64_t aTime) {
    if (aTime == 0) {
      return;
    }

    mActivatedTime = aTime;
  }
};

}  // namespace dom
}  // namespace mozilla

#endif  // mozilla_dom_serviceworkerinfo_h