Attachment #8788562: Part 4: Sync event for bug #1217544

View | Details | Raw Unified | Return to bug 1217544
Collapse All | Expand All

(-)a/dom/backgroundsync/BackgroundSync.cpp (-5 / +13 lines)
Line     Link Here 
 Lines 145-173   private: Link Here 
145
};
145
};
146
146
147
// BackgroundSync
147
// BackgroundSync
148
148
149
// static
149
// static
150
already_AddRefed<BackgroundSync>
150
already_AddRefed<BackgroundSync>
151
BackgroundSync::CreateOnMainThread(nsIGlobalObject* aGlobal,
151
BackgroundSync::CreateOnMainThread(nsIGlobalObject* aGlobal,
152
                                   nsIPrincipal* aPrincipal,
152
                                   nsIPrincipal* aPrincipal,
153
                                   const nsAString& aScope,
153
                                   ErrorResult& aRv)
154
                                   ErrorResult& aRv)
154
{
155
{
155
  MOZ_ASSERT(aGlobal);
156
  MOZ_ASSERT(aGlobal);
156
  MOZ_ASSERT(aPrincipal);
157
  MOZ_ASSERT(aPrincipal);
158
  MOZ_ASSERT(!aScope.IsEmpty());
157
  MOZ_ASSERT(NS_IsMainThread());
159
  MOZ_ASSERT(NS_IsMainThread());
158
160
159
  PrincipalInfo principalInfo;
161
  PrincipalInfo principalInfo;
160
  aRv = PrincipalToPrincipalInfo(aPrincipal, &principalInfo);
162
  aRv = PrincipalToPrincipalInfo(aPrincipal, &principalInfo);
161
  if (NS_WARN_IF(aRv.Failed())) {
163
  if (NS_WARN_IF(aRv.Failed())) {
162
    return nullptr;
164
    return nullptr;
163
  }
165
  }
164
166
165
  RefPtr<BackgroundSync> ref = new BackgroundSync(aGlobal, principalInfo);
167
  RefPtr<BackgroundSync> ref = new BackgroundSync(aGlobal, principalInfo,
168
                                                  aScope);
166
169
167
  // Register as observer for inner-window-destroyed.
170
  // Register as observer for inner-window-destroyed.
168
  nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
171
  nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
169
  if (obs) {
172
  if (obs) {
170
    aRv = obs->AddObserver(ref, "inner-window-destroyed",
173
    aRv = obs->AddObserver(ref, "inner-window-destroyed",
171
                           false /* ownsWeak */);
174
                           false /* ownsWeak */);
172
    if (NS_WARN_IF(aRv.Failed())) {
175
    if (NS_WARN_IF(aRv.Failed())) {
173
      return nullptr;
176
      return nullptr;
 Lines 189-229   BackgroundSync::CreateOnMainThread(nsIGl Link Here 
189
  ref->mInnerID = window->WindowID();
192
  ref->mInnerID = window->WindowID();
190
193
191
  return ref.forget();
194
  return ref.forget();
192
}
195
}
193
196
194
// static
197
// static
195
already_AddRefed<BackgroundSync>
198
already_AddRefed<BackgroundSync>
196
BackgroundSync::CreateOnWorker(nsIGlobalObject* aGlobal,
199
BackgroundSync::CreateOnWorker(nsIGlobalObject* aGlobal,
197
                               WorkerPrivate* aWorkerPrivate)
200
                               WorkerPrivate* aWorkerPrivate,
201
                               const nsAString& aScope)
198
{
202
{
199
  MOZ_ASSERT(aWorkerPrivate);
203
  MOZ_ASSERT(aWorkerPrivate);
204
  MOZ_ASSERT(!aScope.IsEmpty());
200
  aWorkerPrivate->AssertIsOnWorkerThread();
205
  aWorkerPrivate->AssertIsOnWorkerThread();
201
206
202
  const PrincipalInfo& principalInfo = aWorkerPrivate->GetPrincipalInfo();
207
  const PrincipalInfo& principalInfo = aWorkerPrivate->GetPrincipalInfo();
203
208
204
  RefPtr<BackgroundSync> ref = new BackgroundSync(aGlobal,
209
  RefPtr<BackgroundSync> ref = new BackgroundSync(aGlobal,
205
                                                  principalInfo);
210
                                                  principalInfo,
211
                                                  aScope);
206
212
207
  ref->mWorkerHolder = new BackgroundSyncHolder(ref);
213
  ref->mWorkerHolder = new BackgroundSyncHolder(ref);
208
  if (NS_WARN_IF(!ref->mWorkerHolder->HoldWorker(aWorkerPrivate, Terminating))) {
214
  if (NS_WARN_IF(!ref->mWorkerHolder->HoldWorker(aWorkerPrivate, Terminating))) {
209
    ref->mWorkerHolder = nullptr;
215
    ref->mWorkerHolder = nullptr;
210
    return nullptr;
216
    return nullptr;
211
  }
217
  }
212
218
213
  return ref.forget();
219
  return ref.forget();
214
}
220
}
215
221
216
BackgroundSync::BackgroundSync(nsIGlobalObject* aGlobal,
222
BackgroundSync::BackgroundSync(nsIGlobalObject* aGlobal,
217
                               const PrincipalInfo& aPrincipalInfo)
223
                               const PrincipalInfo& aPrincipalInfo,
224
                               const nsAString& aScope)
218
  : mInnerID(0)
225
  : mInnerID(0)
219
  , mGlobal(aGlobal)
226
  , mGlobal(aGlobal)
220
  , mShuttingDown(false)
227
  , mShuttingDown(false)
221
  , mPrincipalInfo(MakeUnique<PrincipalInfo>(aPrincipalInfo))
228
  , mPrincipalInfo(MakeUnique<PrincipalInfo>(aPrincipalInfo))
229
  , mScope(aScope)
222
{
230
{
223
#ifdef DEBUG
231
#ifdef DEBUG
224
  mThread = do_GetCurrentThread();
232
  mThread = do_GetCurrentThread();
225
#endif
233
#endif
226
234
227
  MOZ_ASSERT(aGlobal);
235
  MOZ_ASSERT(aGlobal);
228
236
229
  // Register this component to PBackground.
237
  // Register this component to PBackground.
 Lines 413-429   BackgroundSync::ExecuteOp(const SyncOpAr Link Here 
413
421
414
// WebIDL interface methods.
422
// WebIDL interface methods.
415
423
416
already_AddRefed<Promise>
424
already_AddRefed<Promise>
417
BackgroundSync::Register(const nsAString& aTag, ErrorResult& aRv)
425
BackgroundSync::Register(const nsAString& aTag, ErrorResult& aRv)
418
{
426
{
419
  MOZ_ASSERT(IsBackgroundSyncThread());
427
  MOZ_ASSERT(IsBackgroundSyncThread());
420
428
421
  const SyncRegisterArgs args((nsString(aTag)));
429
  const SyncRegisterArgs args(mScope, (nsString(aTag)));
422
  return ExecuteOp(SyncOpArgs(args), aRv);
430
  return ExecuteOp(SyncOpArgs(args), aRv);
423
}
431
}
424
432
425
already_AddRefed<Promise>
433
already_AddRefed<Promise>
426
BackgroundSync::GetTags(ErrorResult& aRv)
434
BackgroundSync::GetTags(ErrorResult& aRv)
427
{
435
{
428
  MOZ_ASSERT(IsBackgroundSyncThread());
436
  MOZ_ASSERT(IsBackgroundSyncThread());
429
437
(-)a/dom/backgroundsync/BackgroundSync.h (-2 / +7 lines)
Line     Link Here 
 Lines 50-70   class BackgroundSync final : public nsII Link Here 
50
public:
50
public:
51
  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
51
  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
52
  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(
52
  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(
53
      BackgroundSync, nsIIPCBackgroundChildCreateCallback)
53
      BackgroundSync, nsIIPCBackgroundChildCreateCallback)
54
54
55
  static already_AddRefed<BackgroundSync>
55
  static already_AddRefed<BackgroundSync>
56
  CreateOnMainThread(nsIGlobalObject* aGlobal,
56
  CreateOnMainThread(nsIGlobalObject* aGlobal,
57
                     nsIPrincipal* aPrincipal,
57
                     nsIPrincipal* aPrincipal,
58
                     const nsAString& aScope,
58
                     ErrorResult& aRv);
59
                     ErrorResult& aRv);
59
60
60
  static already_AddRefed<BackgroundSync>
61
  static already_AddRefed<BackgroundSync>
61
  CreateOnWorker(nsIGlobalObject* aGlobal,
62
  CreateOnWorker(nsIGlobalObject* aGlobal,
62
                 workers::WorkerPrivate* aWorkerPrivate);
63
                 workers::WorkerPrivate* aWorkerPrivate,
64
                 const nsAString& aScope);
63
65
64
  // Binding methods.
66
  // Binding methods.
65
67
66
  nsIGlobalObject*
68
  nsIGlobalObject*
67
  GetParentObject() const
69
  GetParentObject() const
68
  {
70
  {
69
    return mGlobal;
71
    return mGlobal;
70
  }
72
  }
 Lines 80-96   public: Link Here 
80
  Register(const nsAString& aTag, ErrorResult& aRv);
82
  Register(const nsAString& aTag, ErrorResult& aRv);
81
83
82
  already_AddRefed<Promise>
84
  already_AddRefed<Promise>
83
  GetTags(ErrorResult& aRv);
85
  GetTags(ErrorResult& aRv);
84
86
85
  void Shutdown();
87
  void Shutdown();
86
private:
88
private:
87
  BackgroundSync(nsIGlobalObject* aGlobal,
89
  BackgroundSync(nsIGlobalObject* aGlobal,
88
                 const mozilla::ipc::PrincipalInfo& aPrincipalInfo);
90
                 const mozilla::ipc::PrincipalInfo& aPrincipalInfo,
91
                 const nsAString& aScope);
89
92
90
  ~BackgroundSync();
93
  ~BackgroundSync();
91
94
92
  already_AddRefed<Promise>
95
  already_AddRefed<Promise>
93
  ExecuteOp(const SyncOpArgs& aArgs, ErrorResult& aRv);
96
  ExecuteOp(const SyncOpArgs& aArgs, ErrorResult& aRv);
94
97
95
  uint64_t mInnerID;
98
  uint64_t mInnerID;
96
99
 Lines 103-118   private: Link Here 
103
  bool mShuttingDown;
106
  bool mShuttingDown;
104
107
105
  UniquePtr<mozilla::ipc::PrincipalInfo> mPrincipalInfo;
108
  UniquePtr<mozilla::ipc::PrincipalInfo> mPrincipalInfo;
106
109
107
  nsCOMPtr<nsIThread> mThread;
110
  nsCOMPtr<nsIThread> mThread;
108
111
109
  nsTArray<RefPtr<SyncOpRunnable>> mPendingOperations;
112
  nsTArray<RefPtr<SyncOpRunnable>> mPendingOperations;
110
113
114
  nsString mScope;
115
111
#ifdef DEBUG
116
#ifdef DEBUG
112
  bool IsBackgroundSyncThread();
117
  bool IsBackgroundSyncThread();
113
#endif
118
#endif
114
};
119
};
115
120
116
} // namespace backgroundsync
121
} // namespace backgroundsync
117
} // namespace dom
122
} // namespace dom
118
} // namespace mozilla
123
} // namespace mozilla
(-)a/dom/backgroundsync/BackgroundSyncIPCTypes.ipdlh (+1 lines)
Line     Link Here 
 Lines 4-19    Link Here 
4
4
5
include PBackgroundSharedTypes;
5
include PBackgroundSharedTypes;
6
6
7
namespace mozilla {
7
namespace mozilla {
8
namespace dom {
8
namespace dom {
9
9
10
struct SyncRegisterArgs
10
struct SyncRegisterArgs
11
{
11
{
12
  nsString mScope;
12
  nsString mTag;
13
  nsString mTag;
13
};
14
};
14
15
15
struct SyncGetTagsArgs
16
struct SyncGetTagsArgs
16
{
17
{
17
};
18
};
18
19
19
union SyncOpArgs
20
union SyncOpArgs
(-)a/dom/bindings/Bindings.conf (+6 lines)
Line     Link Here 
 Lines 94-109   DOMInterfaces = { Link Here 
94
    },
94
    },
95
},
95
},
96
96
97
'BackgroundSync': {
97
'BackgroundSync': {
98
    'nativeType': 'mozilla::dom::backgroundsync::BackgroundSync',
98
    'nativeType': 'mozilla::dom::backgroundsync::BackgroundSync',
99
    'headerFile': 'mozilla/dom/backgroundsync/BackgroundSync.h',
99
    'headerFile': 'mozilla/dom/backgroundsync/BackgroundSync.h',
100
},
100
},
101
101
102
'BackgroundSyncEvent': {
103
    'headerFile': 'mozilla/dom/ServiceWorkerEvents.h',
104
    'nativeType': 'mozilla::dom::workers::BackgroundSyncEvent',
105
    'workers': True
106
},
107
102
'BarProp': {
108
'BarProp': {
103
    'headerFile': 'mozilla/dom/BarProps.h',
109
    'headerFile': 'mozilla/dom/BarProps.h',
104
},
110
},
105
111
106
'Blob': {
112
'Blob': {
107
    'headerFile': 'mozilla/dom/File.h',
113
    'headerFile': 'mozilla/dom/File.h',
108
},
114
},
109
115
(-)a/dom/interfaces/base/nsIServiceWorkerManager.idl (-1 / +5 lines)
Line     Link Here 
 Lines 199-217   interface nsIServiceWorkerManager : nsIS Link Here 
199
                                  in AString aBehavior);
199
                                  in AString aBehavior);
200
200
201
  [optional_argc] void sendPushEvent(in ACString aOriginAttributes,
201
  [optional_argc] void sendPushEvent(in ACString aOriginAttributes,
202
                                     in ACString aScope,
202
                                     in ACString aScope,
203
                                     [optional] in uint32_t aDataLength,
203
                                     [optional] in uint32_t aDataLength,
204
                                     [optional, array, size_is(aDataLength)] in uint8_t aDataBytes);
204
                                     [optional, array, size_is(aDataLength)] in uint8_t aDataBytes);
205
  void sendPushSubscriptionChangeEvent(in ACString aOriginAttributes,
205
  void sendPushSubscriptionChangeEvent(in ACString aOriginAttributes,
206
                                       in ACString scope);
206
                                       in ACString scope);
207
208
  void addListener(in nsIServiceWorkerManagerListener aListener);
207
  void addListener(in nsIServiceWorkerManagerListener aListener);
209
208
210
  void removeListener(in nsIServiceWorkerManagerListener aListener);
209
  void removeListener(in nsIServiceWorkerManagerListener aListener);
211
210
212
  bool shouldReportToWindow(in mozIDOMWindowProxy aWindow, in ACString aScope);
211
  bool shouldReportToWindow(in mozIDOMWindowProxy aWindow, in ACString aScope);
212
213
  void sendBackgroundSyncEvent(in ACString aOriginAttributes,
214
                               in ACString scope,
215
                               in AString aTag,
216
                               in boolean aLastChance);
213
};
217
};
214
218
215
%{ C++
219
%{ C++
216
#define SERVICEWORKERMANAGER_CONTRACTID "@mozilla.org/serviceworkers/manager;1"
220
#define SERVICEWORKERMANAGER_CONTRACTID "@mozilla.org/serviceworkers/manager;1"
217
%}
221
%}
(-)a/dom/webidl/BackgroundSyncEvent.webidl (+20 lines)
Line     Link Here 
Line 0    Link Here 
1
/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* This Source Code Form is subject to the terms of the Mozilla Public
3
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
4
 * You can obtain one at http://mozilla.org/MPL/2.0/.
5
 *
6
 * The origin of this IDL file is
7
 * https://wicg.github.io/BackgroundSync/spec/
8
 */
9
10
[Constructor(DOMString type, optional BackgroundSyncEventInit eventInitDict),
11
 Exposed=ServiceWorker]
12
interface BackgroundSyncEvent : ExtendableEvent {
13
  readonly attribute DOMString tag;
14
  readonly attribute boolean lastChance;
15
};
16
17
dictionary BackgroundSyncEventInit : ExtendableEventInit {
18
  required DOMString tag;
19
  boolean lastChance = false;
20
};
(-)a/dom/webidl/moz.build (+1 lines)
Line     Link Here 
 Lines 45-60   WEBIDL_FILES = [ Link Here 
45
    'AudioParam.webidl',
45
    'AudioParam.webidl',
46
    'AudioProcessingEvent.webidl',
46
    'AudioProcessingEvent.webidl',
47
    'AudioStreamTrack.webidl',
47
    'AudioStreamTrack.webidl',
48
    'AudioTrack.webidl',
48
    'AudioTrack.webidl',
49
    'AudioTrackList.webidl',
49
    'AudioTrackList.webidl',
50
    'AutocompleteInfo.webidl',
50
    'AutocompleteInfo.webidl',
51
    'AVInputPort.webidl',
51
    'AVInputPort.webidl',
52
    'BackgroundSync.webidl',
52
    'BackgroundSync.webidl',
53
    'BackgroundSyncEvent.webidl',
53
    'BarProp.webidl',
54
    'BarProp.webidl',
54
    'BaseKeyframeTypes.webidl',
55
    'BaseKeyframeTypes.webidl',
55
    'BatteryManager.webidl',
56
    'BatteryManager.webidl',
56
    'BeforeAfterKeyboardEvent.webidl',
57
    'BeforeAfterKeyboardEvent.webidl',
57
    'BeforeUnloadEvent.webidl',
58
    'BeforeUnloadEvent.webidl',
58
    'BiquadFilterNode.webidl',
59
    'BiquadFilterNode.webidl',
59
    'Blob.webidl',
60
    'Blob.webidl',
60
    'BoxObject.webidl',
61
    'BoxObject.webidl',
(-)a/dom/workers/PServiceWorkerManager.ipdl (-2 / +6 lines)
Line     Link Here 
 Lines 28-45   parent: Link Here 
28
  async PropagateRemove(nsCString host);
28
  async PropagateRemove(nsCString host);
29
29
30
  async PropagateRemoveAll();
30
  async PropagateRemoveAll();
31
31
32
  async Shutdown();
32
  async Shutdown();
33
33
34
child:
34
child:
35
  async NotifyRegister(ServiceWorkerRegistrationData data);
35
  async NotifyRegister(ServiceWorkerRegistrationData data);
36
  async NotifySoftUpdate(PrincipalOriginAttributes originAttributes, nsString scope);
36
  async NotifySoftUpdate(PrincipalOriginAttributes originAttributes,
37
                         nsString scope);
37
  async NotifyUnregister(PrincipalInfo principalInfo, nsString scope);
38
  async NotifyUnregister(PrincipalInfo principalInfo, nsString scope);
38
  async NotifyRemove(nsCString host);
39
  async NotifyRemove(nsCString host);
39
  async NotifyRemoveAll();
40
  async NotifyRemoveAll();
40
41
  async NotifyBackgroundSyncEvent(PrincipalInfo principalInfo,
42
                                  nsString scope,
43
                                  nsString tag,
44
                                  bool lastChance);
41
  async __delete__();
45
  async __delete__();
42
};
46
};
43
47
44
} // namespace dom
48
} // namespace dom
45
} // namespace mozilla
49
} // namespace mozilla
(-)a/dom/workers/ServiceWorkerEvents.cpp (-1 / +35 lines)
Line     Link Here 
 Lines 1009-1025   ExtractBytesFromData(const OwningArrayBu Link Here 
1009
    return NS_OK;
1009
    return NS_OK;
1010
  }
1010
  }
1011
  if (aDataInit.IsUSVString()) {
1011
  if (aDataInit.IsUSVString()) {
1012
    return ExtractBytesFromUSVString(aDataInit.GetAsUSVString(), aBytes);
1012
    return ExtractBytesFromUSVString(aDataInit.GetAsUSVString(), aBytes);
1013
  }
1013
  }
1014
  NS_NOTREACHED("Unexpected push message data");
1014
  NS_NOTREACHED("Unexpected push message data");
1015
  return NS_ERROR_FAILURE;
1015
  return NS_ERROR_FAILURE;
1016
}
1016
}
1017
}
1017
} // anonymous namespace
1018
1018
1019
PushMessageData::PushMessageData(nsISupports* aOwner,
1019
PushMessageData::PushMessageData(nsISupports* aOwner,
1020
                                 nsTArray<uint8_t>&& aBytes)
1020
                                 nsTArray<uint8_t>&& aBytes)
1021
  : mOwner(aOwner), mBytes(Move(aBytes)) {}
1021
  : mOwner(aOwner), mBytes(Move(aBytes)) {}
1022
1022
1023
PushMessageData::~PushMessageData()
1023
PushMessageData::~PushMessageData()
1024
{
1024
{
1025
}
1025
}
 Lines 1150-1165   NS_INTERFACE_MAP_END_INHERITING(Extendab Link Here 
1150
NS_IMPL_CYCLE_COLLECTION_INHERITED(PushEvent, ExtendableEvent, mData)
1150
NS_IMPL_CYCLE_COLLECTION_INHERITED(PushEvent, ExtendableEvent, mData)
1151
1151
1152
JSObject*
1152
JSObject*
1153
PushEvent::WrapObjectInternal(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
1153
PushEvent::WrapObjectInternal(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
1154
{
1154
{
1155
  return mozilla::dom::PushEventBinding::Wrap(aCx, this, aGivenProto);
1155
  return mozilla::dom::PushEventBinding::Wrap(aCx, this, aGivenProto);
1156
}
1156
}
1157
1157
1158
// Background Sync
1159
1160
BackgroundSyncEvent::BackgroundSyncEvent(EventTarget* aOwner)
1161
  : ExtendableEvent(aOwner)
1162
{
1163
}
1164
1165
already_AddRefed<BackgroundSyncEvent>
1166
BackgroundSyncEvent::Constructor(mozilla::dom::EventTarget* aOwner,
1167
                       const nsAString& aType,
1168
                       const BackgroundSyncEventInit& aOptions,
1169
                       ErrorResult& aRv)
1170
{
1171
  RefPtr<BackgroundSyncEvent> e = new BackgroundSyncEvent(aOwner);
1172
  bool trusted = e->Init(aOwner);
1173
  e->InitEvent(aType, aOptions.mBubbles, aOptions.mCancelable);
1174
  e->SetTrusted(trusted);
1175
  e->mTag = aOptions.mTag;
1176
  e->mLastChance = aOptions.mLastChance;
1177
  return e.forget();
1178
}
1179
1180
NS_IMPL_ADDREF_INHERITED(BackgroundSyncEvent, ExtendableEvent)
1181
NS_IMPL_RELEASE_INHERITED(BackgroundSyncEvent, ExtendableEvent)
1182
1183
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(BackgroundSyncEvent)
1184
NS_INTERFACE_MAP_END_INHERITING(ExtendableEvent)
1185
1186
NS_IMPL_CYCLE_COLLECTION_CLASS(BackgroundSyncEvent)
1187
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(BackgroundSyncEvent, ExtendableEvent)
1188
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
1189
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(BackgroundSyncEvent, ExtendableEvent)
1190
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
1191
1158
ExtendableMessageEvent::ExtendableMessageEvent(EventTarget* aOwner)
1192
ExtendableMessageEvent::ExtendableMessageEvent(EventTarget* aOwner)
1159
  : ExtendableEvent(aOwner)
1193
  : ExtendableEvent(aOwner)
1160
  , mData(JS::UndefinedValue())
1194
  , mData(JS::UndefinedValue())
1161
{
1195
{
1162
  mozilla::HoldJSObjects(this);
1196
  mozilla::HoldJSObjects(this);
1163
}
1197
}
1164
1198
1165
ExtendableMessageEvent::~ExtendableMessageEvent()
1199
ExtendableMessageEvent::~ExtendableMessageEvent()
(-)a/dom/workers/ServiceWorkerEvents.h (+52 lines)
Line     Link Here 
 Lines 11-26    Link Here 
11
#include "mozilla/dom/ExtendableEventBinding.h"
11
#include "mozilla/dom/ExtendableEventBinding.h"
12
#include "mozilla/dom/ExtendableMessageEventBinding.h"
12
#include "mozilla/dom/ExtendableMessageEventBinding.h"
13
#include "mozilla/dom/FetchEventBinding.h"
13
#include "mozilla/dom/FetchEventBinding.h"
14
#include "mozilla/dom/File.h"
14
#include "mozilla/dom/File.h"
15
#include "mozilla/dom/Promise.h"
15
#include "mozilla/dom/Promise.h"
16
#include "mozilla/dom/Response.h"
16
#include "mozilla/dom/Response.h"
17
#include "mozilla/dom/workers/bindings/ServiceWorker.h"
17
#include "mozilla/dom/workers/bindings/ServiceWorker.h"
18
18
19
#include "mozilla/dom/BackgroundSyncEventBinding.h"
20
19
#include "nsProxyRelease.h"
21
#include "nsProxyRelease.h"
20
#include "nsContentUtils.h"
22
#include "nsContentUtils.h"
21
23
22
class nsIInterceptedChannel;
24
class nsIInterceptedChannel;
23
25
24
namespace mozilla {
26
namespace mozilla {
25
namespace dom {
27
namespace dom {
26
class Blob;
28
class Blob;
 Lines 311-321   public: Link Here 
311
313
312
  void SetPorts(MessagePortList* aPorts);
314
  void SetPorts(MessagePortList* aPorts);
313
315
314
  void SetSource(ServiceWorkerClient* aClient);
316
  void SetSource(ServiceWorkerClient* aClient);
315
317
316
  void SetSource(ServiceWorker* aServiceWorker);
318
  void SetSource(ServiceWorker* aServiceWorker);
317
};
319
};
318
320
321
class BackgroundSyncEvent final : public ExtendableEvent
322
{
323
  nsString mTag;
324
  bool mLastChance;
325
326
protected:
327
  explicit BackgroundSyncEvent(mozilla::dom::EventTarget* aOwner);
328
  ~BackgroundSyncEvent() {}
329
330
public:
331
  NS_DECL_ISUPPORTS_INHERITED
332
  NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(BackgroundSyncEvent, ExtendableEvent)
333
  NS_FORWARD_TO_EVENT
334
335
  virtual JSObject* WrapObjectInternal(JSContext* aCx,
336
                                       JS::Handle<JSObject*> aGivenProto) override
337
  {
338
    return mozilla::dom::BackgroundSyncEventBinding::Wrap(aCx, this,
339
                                                          aGivenProto);
340
  }
341
342
  static already_AddRefed<BackgroundSyncEvent>
343
  Constructor(mozilla::dom::EventTarget* aOwner,
344
              const nsAString& aType,
345
              const BackgroundSyncEventInit& aOptions,
346
              ErrorResult& aRv);
347
348
  static already_AddRefed<BackgroundSyncEvent>
349
  Constructor(const GlobalObject& aGlobal,
350
              const nsAString& aType,
351
              const BackgroundSyncEventInit& aOptions,
352
              ErrorResult& aRv)
353
  {
354
    nsCOMPtr<EventTarget> owner = do_QueryInterface(aGlobal.GetAsSupports());
355
    return Constructor(owner, aType, aOptions, aRv);
356
  }
357
358
  void
359
  GetTag(nsAString& aTag)
360
  {
361
    aTag = mTag;
362
  }
363
364
  bool
365
  LastChance()
366
  {
367
    return mLastChance;
368
  }
369
};
370
319
END_WORKERS_NAMESPACE
371
END_WORKERS_NAMESPACE
320
372
321
#endif /* mozilla_dom_workers_serviceworkerevents_h__ */
373
#endif /* mozilla_dom_workers_serviceworkerevents_h__ */
(-)a/dom/workers/ServiceWorkerManager.cpp (+25 lines)
Line     Link Here 
 Lines 1020-1035   ServiceWorkerManager::SendNotificationCl Link Here 
1020
                                                 const nsAString& aBehavior)
1020
                                                 const nsAString& aBehavior)
1021
{
1021
{
1022
  return SendNotificationEvent(NS_LITERAL_STRING(NOTIFICATION_CLOSE_EVENT_NAME),
1022
  return SendNotificationEvent(NS_LITERAL_STRING(NOTIFICATION_CLOSE_EVENT_NAME),
1023
                               aOriginSuffix, aScope, aID, aTitle, aDir, aLang,
1023
                               aOriginSuffix, aScope, aID, aTitle, aDir, aLang,
1024
                               aBody, aTag, aIcon, aData, aBehavior);
1024
                               aBody, aTag, aIcon, aData, aBehavior);
1025
}
1025
}
1026
1026
1027
NS_IMETHODIMP
1027
NS_IMETHODIMP
1028
ServiceWorkerManager::SendBackgroundSyncEvent(const nsACString& aOriginAttributes,
1029
                                              const nsACString& aScope,
1030
                                              const nsAString& aTag,
1031
                                              const bool aLastChance)
1032
{
1033
  PrincipalOriginAttributes attrs;
1034
  if (!attrs.PopulateFromSuffix(aOriginAttributes)) {
1035
    return NS_ERROR_INVALID_ARG;
1036
  }
1037
1038
  ServiceWorkerInfo* serviceWorker =
1039
    GetActiveWorkerInfoForScope(attrs, aScope);
1040
  if (NS_WARN_IF(!serviceWorker)) {
1041
    return NS_ERROR_FAILURE;
1042
  }
1043
1044
  RefPtr<ServiceWorkerRegistrationInfo> registration =
1045
    GetRegistration(serviceWorker->GetPrincipal(), aScope);
1046
1047
  return serviceWorker->WorkerPrivate()->SendBackgroundSyncEvent(aTag,
1048
                                                                 aLastChance,
1049
                                                                 registration);
1050
}
1051
1052
NS_IMETHODIMP
1028
ServiceWorkerManager::GetReadyPromise(mozIDOMWindow* aWindow,
1053
ServiceWorkerManager::GetReadyPromise(mozIDOMWindow* aWindow,
1029
                                      nsISupports** aPromise)
1054
                                      nsISupports** aPromise)
1030
{
1055
{
1031
  AssertIsOnMainThread();
1056
  AssertIsOnMainThread();
1032
1057
1033
  if (NS_WARN_IF(!aWindow)) {
1058
  if (NS_WARN_IF(!aWindow)) {
1034
    return NS_ERROR_DOM_INVALID_STATE_ERR;
1059
    return NS_ERROR_DOM_INVALID_STATE_ERR;
1035
  }
1060
  }
(-)a/dom/workers/ServiceWorkerManagerChild.cpp (+29 lines)
Line     Link Here 
 Lines 90-100   ServiceWorkerManagerChild::RecvNotifyRem Link Here 
90
90
91
  RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
91
  RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
92
  MOZ_ASSERT(swm);
92
  MOZ_ASSERT(swm);
93
93
94
  swm->RemoveAll();
94
  swm->RemoveAll();
95
  return true;
95
  return true;
96
}
96
}
97
97
98
bool
99
ServiceWorkerManagerChild::RecvNotifyBackgroundSyncEvent(
100
    const PrincipalInfo& aPrincipalInfo, const nsString& aScope,
101
    const nsString& aTag, const bool& aLastChance)
102
{
103
  if (mShuttingDown) {
104
    return true;
105
  }
106
107
  nsCOMPtr<nsIPrincipal> principal = PrincipalInfoToPrincipal(aPrincipalInfo);
108
  if (NS_WARN_IF(!principal)) {
109
    return true;
110
  }
111
112
  nsAutoCString originSuffix;
113
  nsresult rv = principal->GetOriginSuffix(originSuffix);
114
  if (NS_WARN_IF(NS_FAILED(rv))) {
115
    return true;
116
  }
117
118
  RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
119
  MOZ_ASSERT(swm);
120
121
  swm->SendBackgroundSyncEvent(originSuffix,
122
                               NS_ConvertUTF16toUTF8(aScope),
123
                               aTag, aLastChance);
124
  return true;
125
}
126
98
} // namespace workers
127
} // namespace workers
99
} // namespace dom
128
} // namespace dom
100
} // namespace mozilla
129
} // namespace mozilla
(-)a/dom/workers/ServiceWorkerManagerChild.h (+5 lines)
Line     Link Here 
 Lines 41-56   public: Link Here 
41
41
42
  virtual bool RecvNotifyUnregister(const PrincipalInfo& aPrincipalInfo,
42
  virtual bool RecvNotifyUnregister(const PrincipalInfo& aPrincipalInfo,
43
                                    const nsString& aScope) override;
43
                                    const nsString& aScope) override;
44
44
45
  virtual bool RecvNotifyRemove(const nsCString& aHost) override;
45
  virtual bool RecvNotifyRemove(const nsCString& aHost) override;
46
46
47
  virtual bool RecvNotifyRemoveAll() override;
47
  virtual bool RecvNotifyRemoveAll() override;
48
48
49
  virtual bool RecvNotifyBackgroundSyncEvent(const PrincipalInfo& aPrincipalInfo,
50
                                             const nsString& aScope,
51
                                             const nsString& aTag,
52
                                             const bool& aLastChance) override;
53
49
private:
54
private:
50
  ServiceWorkerManagerChild()
55
  ServiceWorkerManagerChild()
51
    : mShuttingDown(false)
56
    : mShuttingDown(false)
52
  {}
57
  {}
53
58
54
  ~ServiceWorkerManagerChild() {}
59
  ~ServiceWorkerManagerChild() {}
55
60
56
  bool mShuttingDown;
61
  bool mShuttingDown;
(-)a/dom/workers/ServiceWorkerPrivate.cpp (+86 lines)
Line     Link Here 
 Lines 1645-1660   ServiceWorkerPrivate::SendFetchEvent(nsI Link Here 
1645
1645
1646
  if (NS_WARN_IF(!r->Dispatch())) {
1646
  if (NS_WARN_IF(!r->Dispatch())) {
1647
    return NS_ERROR_FAILURE;
1647
    return NS_ERROR_FAILURE;
1648
  }
1648
  }
1649
1649
1650
  return NS_OK;
1650
  return NS_OK;
1651
}
1651
}
1652
1652
1653
namespace {
1654
class SendSyncEventRunnable final : public ExtendableFunctionalEventWorkerRunnable
1655
{
1656
  nsString mTag;
1657
  bool mLastChance;
1658
1659
public:
1660
  SendSyncEventRunnable(WorkerPrivate* aWorkerPrivate,
1661
                        KeepAliveToken* aKeepAliveToken,
1662
                        const nsAString& aTag,
1663
                        const bool aLastChance,
1664
                        nsMainThreadPtrHandle<ServiceWorkerRegistrationInfo> aRegistration)
1665
    : ExtendableFunctionalEventWorkerRunnable(
1666
        aWorkerPrivate, aKeepAliveToken, aRegistration)
1667
    , mTag(aTag)
1668
    , mLastChance(aLastChance)
1669
  {
1670
    AssertIsOnMainThread();
1671
    MOZ_ASSERT(aWorkerPrivate);
1672
    MOZ_ASSERT(aWorkerPrivate->IsServiceWorker());
1673
  }
1674
1675
  bool
1676
  WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
1677
  {
1678
    MOZ_ASSERT(aWorkerPrivate);
1679
    GlobalObject globalObj(aCx, aWorkerPrivate->GlobalScope()->GetWrapper());
1680
1681
    BackgroundSyncEventInit sei;
1682
    sei.mTag = mTag;
1683
    sei.mLastChance = mLastChance;
1684
    sei.mBubbles = false;
1685
    sei.mCancelable = false;
1686
1687
    ErrorResult result;
1688
    RefPtr<BackgroundSyncEvent> event =
1689
      BackgroundSyncEvent::Constructor(globalObj, NS_LITERAL_STRING("sync"),
1690
                                       sei, result);
1691
    if (NS_WARN_IF(result.Failed())) {
1692
      result.SuppressException();
1693
      return false;
1694
    }
1695
    event->SetTrusted(true);
1696
1697
    DispatchExtendableEventOnWorkerScope(aCx, aWorkerPrivate->GlobalScope(),
1698
                                         event, nullptr);
1699
1700
    return true;
1701
  }
1702
};
1703
} // anonymous namespace
1704
1705
nsresult
1706
ServiceWorkerPrivate::SendBackgroundSyncEvent(const nsAString& aTag,
1707
                                              const bool aLastChance,
1708
                                              ServiceWorkerRegistrationInfo* aRegistration)
1709
{
1710
  nsresult rv = SpawnWorkerIfNeeded(BackgroundSyncEvent, nullptr);
1711
  NS_ENSURE_SUCCESS(rv, rv);
1712
1713
  RefPtr<KeepAliveToken> token = CreateEventKeepAliveToken();
1714
1715
  nsMainThreadPtrHandle<ServiceWorkerRegistrationInfo> regInfo(
1716
      new nsMainThreadPtrHolder<ServiceWorkerRegistrationInfo>(aRegistration, false));
1717
1718
1719
  RefPtr<WorkerRunnable> runnable = new SendSyncEventRunnable(mWorkerPrivate,
1720
                                                              token,
1721
                                                              aTag,
1722
                                                              aLastChance,
1723
                                                              regInfo);
1724
1725
  if (mInfo->State() == ServiceWorkerState::Activating) {
1726
    mPendingFunctionalEvents.AppendElement(runnable.forget());
1727
    return NS_OK;
1728
  }
1729
1730
  MOZ_ASSERT(mInfo->State() == ServiceWorkerState::Activated);
1731
1732
  if (NS_WARN_IF(!runnable->Dispatch())) {
1733
    return NS_ERROR_FAILURE;
1734
  }
1735
1736
  return NS_OK;
1737
}
1738
1653
nsresult
1739
nsresult
1654
ServiceWorkerPrivate::SpawnWorkerIfNeeded(WakeUpReason aWhy,
1740
ServiceWorkerPrivate::SpawnWorkerIfNeeded(WakeUpReason aWhy,
1655
                                          nsIRunnable* aLoadFailedRunnable,
1741
                                          nsIRunnable* aLoadFailedRunnable,
1656
                                          nsILoadGroup* aLoadGroup)
1742
                                          nsILoadGroup* aLoadGroup)
1657
{
1743
{
1658
  AssertIsOnMainThread();
1744
  AssertIsOnMainThread();
1659
1745
1660
  // XXXcatalinb: We need to have a separate load group that's linked to
1746
  // XXXcatalinb: We need to have a separate load group that's linked to
(-)a/dom/workers/ServiceWorkerPrivate.h (-1 / +7 lines)
Line     Link Here 
 Lines 109-124   public: Link Here 
109
                        const nsAString& aScope);
109
                        const nsAString& aScope);
110
110
111
  nsresult
111
  nsresult
112
  SendFetchEvent(nsIInterceptedChannel* aChannel,
112
  SendFetchEvent(nsIInterceptedChannel* aChannel,
113
                 nsILoadGroup* aLoadGroup,
113
                 nsILoadGroup* aLoadGroup,
114
                 const nsAString& aDocumentId,
114
                 const nsAString& aDocumentId,
115
                 bool aIsReload);
115
                 bool aIsReload);
116
116
117
  nsresult
118
  SendBackgroundSyncEvent(const nsAString& aTag,
119
                          const bool aLastChance,
120
                          ServiceWorkerRegistrationInfo* aRegistration);
121
117
  void
122
  void
118
  StoreISupports(nsISupports* aSupports);
123
  StoreISupports(nsISupports* aSupports);
119
124
120
  void
125
  void
121
  RemoveISupports(nsISupports* aSupports);
126
  RemoveISupports(nsISupports* aSupports);
122
127
123
  // This will terminate the current running worker thread and drop the
128
  // This will terminate the current running worker thread and drop the
124
  // workerPrivate reference.
129
  // workerPrivate reference.
 Lines 153-169   private: Link Here 
153
  enum WakeUpReason {
158
  enum WakeUpReason {
154
    FetchEvent = 0,
159
    FetchEvent = 0,
155
    PushEvent,
160
    PushEvent,
156
    PushSubscriptionChangeEvent,
161
    PushSubscriptionChangeEvent,
157
    MessageEvent,
162
    MessageEvent,
158
    NotificationClickEvent,
163
    NotificationClickEvent,
159
    NotificationCloseEvent,
164
    NotificationCloseEvent,
160
    LifeCycleEvent,
165
    LifeCycleEvent,
161
    AttachEvent
166
    AttachEvent,
167
    BackgroundSyncEvent
162
  };
168
  };
163
169
164
  // Timer callbacks
170
  // Timer callbacks
165
  static void
171
  static void
166
  NoteIdleWorkerCallback(nsITimer* aTimer, void* aPrivate);
172
  NoteIdleWorkerCallback(nsITimer* aTimer, void* aPrivate);
167
173
168
  static void
174
  static void
169
  TerminateWorkerCallback(nsITimer* aTimer, void *aPrivate);
175
  TerminateWorkerCallback(nsITimer* aTimer, void *aPrivate);
(-)a/dom/workers/ServiceWorkerRegistration.cpp (-2 / +2 lines)
Line     Link Here 
 Lines 876-892   ServiceWorkerRegistrationMainThread::Get Link Here 
876
876
877
    nsCOMPtr<nsIPrincipal> principal = document->NodePrincipal();
877
    nsCOMPtr<nsIPrincipal> principal = document->NodePrincipal();
878
    if (!principal) {
878
    if (!principal) {
879
      aRv.Throw(NS_ERROR_FAILURE);
879
      aRv.Throw(NS_ERROR_FAILURE);
880
      return nullptr;
880
      return nullptr;
881
    }
881
    }
882
882
883
    mBackgroundSync = BackgroundSync::CreateOnMainThread(globalObject, principal,
883
    mBackgroundSync = BackgroundSync::CreateOnMainThread(globalObject, principal,
884
                                                         aRv);
884
                                                         mScope, aRv);
885
  }
885
  }
886
886
887
  return mBackgroundSync;
887
  return mBackgroundSync;
888
}
888
}
889
889
890
////////////////////////////////////////////////////
890
////////////////////////////////////////////////////
891
// Worker Thread implementation
891
// Worker Thread implementation
892
892
 Lines 1389-1405   ServiceWorkerRegistrationWorkerThread::G Link Here 
1389
  return ret.forget();
1389
  return ret.forget();
1390
}
1390
}
1391
1391
1392
BackgroundSync*
1392
BackgroundSync*
1393
ServiceWorkerRegistrationWorkerThread::GetSync(ErrorResult& aRv)
1393
ServiceWorkerRegistrationWorkerThread::GetSync(ErrorResult& aRv)
1394
{
1394
{
1395
  if (!mBackgroundSync) {
1395
  if (!mBackgroundSync) {
1396
    mBackgroundSync = BackgroundSync::CreateOnWorker(
1396
    mBackgroundSync = BackgroundSync::CreateOnWorker(
1397
        mWorkerPrivate->GlobalScope(), mWorkerPrivate);
1397
        mWorkerPrivate->GlobalScope(), mWorkerPrivate, mScope);
1398
  }
1398
  }
1399
1399
1400
  return mBackgroundSync;
1400
  return mBackgroundSync;
1401
}
1401
}
1402
1402
1403
////////////////////////////////////////////////////
1403
////////////////////////////////////////////////////
1404
// Base class implementation
1404
// Base class implementation
1405
1405

Return to bug 1217544