|
|
|
|
| 56 |
|
56 |
|
| 57 |
RefPtr<ServiceWorkerPrivate> mPrivate; |
57 |
RefPtr<ServiceWorkerPrivate> mPrivate; |
| 58 |
}; |
58 |
}; |
| 59 |
|
59 |
|
| 60 |
NS_IMPL_ISUPPORTS0(KeepAliveToken) |
60 |
NS_IMPL_ISUPPORTS0(KeepAliveToken) |
| 61 |
|
61 |
|
| 62 |
ServiceWorkerPrivate::ServiceWorkerPrivate(ServiceWorkerInfo* aInfo) |
62 |
ServiceWorkerPrivate::ServiceWorkerPrivate(ServiceWorkerInfo* aInfo) |
| 63 |
: mInfo(aInfo) |
63 |
: mInfo(aInfo) |
| 64 |
, mIsPushWorker(false) |
64 |
, mIsPushOrSyncWorker(false) |
| 65 |
, mDebuggerCount(0) |
65 |
, mDebuggerCount(0) |
| 66 |
, mTokenCount(0) |
66 |
, mTokenCount(0) |
| 67 |
{ |
67 |
{ |
| 68 |
AssertIsOnMainThread(); |
68 |
AssertIsOnMainThread(); |
| 69 |
MOZ_ASSERT(aInfo); |
69 |
MOZ_ASSERT(aInfo); |
| 70 |
|
70 |
|
| 71 |
mIdleWorkerTimer = do_CreateInstance(NS_TIMER_CONTRACTID); |
71 |
mIdleWorkerTimer = do_CreateInstance(NS_TIMER_CONTRACTID); |
| 72 |
MOZ_ASSERT(mIdleWorkerTimer); |
72 |
MOZ_ASSERT(mIdleWorkerTimer); |
|
|
| 478 |
{ |
478 |
{ |
| 479 |
MOZ_ASSERT(GetCurrentThreadWorkerPrivate() == mWorkerPrivate); |
479 |
MOZ_ASSERT(GetCurrentThreadWorkerPrivate() == mWorkerPrivate); |
| 480 |
mWorkerPrivate->AssertIsOnWorkerThread(); |
480 |
mWorkerPrivate->AssertIsOnWorkerThread(); |
| 481 |
|
481 |
|
| 482 |
ReportResult(aCx, false); |
482 |
ReportResult(aCx, false); |
| 483 |
|
483 |
|
| 484 |
// Note, all WaitUntil() rejections are reported to client consoles |
484 |
// Note, all WaitUntil() rejections are reported to client consoles |
| 485 |
// by the WaitUntilHandler in ServiceWorkerEvents. This ensures that |
485 |
// by the WaitUntilHandler in ServiceWorkerEvents. This ensures that |
| 486 |
// errors in non-lifecycle events like FetchEvent and PushEvent are |
486 |
// errors in non-lifecycle events like FetchEvent, PushEvent or SyncEvent |
| 487 |
// reported properly. |
487 |
// are reported properly. |
| 488 |
} |
488 |
} |
| 489 |
}; |
489 |
}; |
| 490 |
|
490 |
|
| 491 |
NS_IMPL_ISUPPORTS0(LifeCycleEventWatcher) |
491 |
NS_IMPL_ISUPPORTS0(LifeCycleEventWatcher) |
| 492 |
|
492 |
|
| 493 |
bool |
493 |
bool |
| 494 |
LifecycleEventWorkerRunnable::DispatchLifecycleEvent(JSContext* aCx, |
494 |
LifecycleEventWorkerRunnable::DispatchLifecycleEvent(JSContext* aCx, |
| 495 |
WorkerPrivate* aWorkerPrivate) |
495 |
WorkerPrivate* aWorkerPrivate) |
|
Lines 1357-1372
ServiceWorkerPrivate::SendFetchEvent(nsI
|
Link Here
|
|---|
|
| 1357 |
jsapi.Init(); |
1357 |
jsapi.Init(); |
| 1358 |
if (NS_WARN_IF(!r->Dispatch(jsapi.cx()))) { |
1358 |
if (NS_WARN_IF(!r->Dispatch(jsapi.cx()))) { |
| 1359 |
return NS_ERROR_FAILURE; |
1359 |
return NS_ERROR_FAILURE; |
| 1360 |
} |
1360 |
} |
| 1361 |
|
1361 |
|
| 1362 |
return NS_OK; |
1362 |
return NS_OK; |
| 1363 |
} |
1363 |
} |
| 1364 |
|
1364 |
|
|
|
1365 |
namespace { |
| 1366 |
class SendSyncEventRunnable final : public ExtendableFunctionalEventWorkerRunnable |
| 1367 |
{ |
| 1368 |
nsString mTag; |
| 1369 |
bool mLastChance; |
| 1370 |
|
| 1371 |
public: |
| 1372 |
SendSyncEventRunnable(WorkerPrivate* aWorkerPrivate, |
| 1373 |
KeepAliveToken* aKeepAliveToken, |
| 1374 |
const nsAString& aTag, |
| 1375 |
const bool aLastChance, |
| 1376 |
nsMainThreadPtrHandle<ServiceWorkerRegistrationInfo> aRegistration) |
| 1377 |
: ExtendableFunctionalEventWorkerRunnable( |
| 1378 |
aWorkerPrivate, aKeepAliveToken, aRegistration) |
| 1379 |
, mTag(aTag) |
| 1380 |
, mLastChance(aLastChance) |
| 1381 |
{ |
| 1382 |
AssertIsOnMainThread(); |
| 1383 |
MOZ_ASSERT(aWorkerPrivate); |
| 1384 |
MOZ_ASSERT(aWorkerPrivate->IsServiceWorker()); |
| 1385 |
} |
| 1386 |
|
| 1387 |
bool |
| 1388 |
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override |
| 1389 |
{ |
| 1390 |
MOZ_ASSERT(aWorkerPrivate); |
| 1391 |
GlobalObject globalObj(aCx, aWorkerPrivate->GlobalScope()->GetWrapper()); |
| 1392 |
|
| 1393 |
SyncEventInit sei; |
| 1394 |
sei.mTag = mTag; |
| 1395 |
sei.mLastChance = mLastChance; |
| 1396 |
sei.mBubbles = false; |
| 1397 |
sei.mCancelable = false; |
| 1398 |
|
| 1399 |
ErrorResult result; |
| 1400 |
RefPtr<SyncEvent> event = |
| 1401 |
SyncEvent::Constructor(globalObj, NS_LITERAL_STRING("sync"), sei, result); |
| 1402 |
if (NS_WARN_IF(result.Failed())) { |
| 1403 |
result.SuppressException(); |
| 1404 |
return false; |
| 1405 |
} |
| 1406 |
event->SetTrusted(true); |
| 1407 |
|
| 1408 |
DispatchExtendableEventOnWorkerScope(aCx, aWorkerPrivate->GlobalScope(), |
| 1409 |
event, nullptr); |
| 1410 |
|
| 1411 |
return true; |
| 1412 |
} |
| 1413 |
}; |
| 1414 |
} // anonymous namespace |
| 1415 |
|
| 1416 |
nsresult |
| 1417 |
ServiceWorkerPrivate::SendSyncEvent(const nsAString& aTag, |
| 1418 |
const bool aLastChance, |
| 1419 |
ServiceWorkerRegistrationInfo* aRegistration) |
| 1420 |
{ |
| 1421 |
nsresult rv = SpawnWorkerIfNeeded(SyncEvent, nullptr); |
| 1422 |
NS_ENSURE_SUCCESS(rv, rv); |
| 1423 |
|
| 1424 |
MOZ_ASSERT(mKeepAliveToken); |
| 1425 |
|
| 1426 |
nsMainThreadPtrHandle<ServiceWorkerRegistrationInfo> regInfo( |
| 1427 |
new nsMainThreadPtrHolder<ServiceWorkerRegistrationInfo>(aRegistration, false)); |
| 1428 |
|
| 1429 |
|
| 1430 |
RefPtr<WorkerRunnable> runnable = new SendSyncEventRunnable(mWorkerPrivate, |
| 1431 |
mKeepAliveToken, |
| 1432 |
aTag, |
| 1433 |
aLastChance, |
| 1434 |
regInfo); |
| 1435 |
|
| 1436 |
if (mInfo->State() == ServiceWorkerState::Activating) { |
| 1437 |
mPendingFunctionalEvents.AppendElement(runnable.forget()); |
| 1438 |
return NS_OK; |
| 1439 |
} |
| 1440 |
|
| 1441 |
MOZ_ASSERT(mInfo->State() == ServiceWorkerState::Activated); |
| 1442 |
|
| 1443 |
AutoJSAPI jsapi; |
| 1444 |
jsapi.Init(); |
| 1445 |
if (NS_WARN_IF(!runnable->Dispatch(jsapi.cx()))) { |
| 1446 |
return NS_ERROR_FAILURE; |
| 1447 |
} |
| 1448 |
|
| 1449 |
return NS_OK; |
| 1450 |
} |
| 1451 |
|
| 1365 |
nsresult |
1452 |
nsresult |
| 1366 |
ServiceWorkerPrivate::SpawnWorkerIfNeeded(WakeUpReason aWhy, |
1453 |
ServiceWorkerPrivate::SpawnWorkerIfNeeded(WakeUpReason aWhy, |
| 1367 |
nsIRunnable* aLoadFailedRunnable, |
1454 |
nsIRunnable* aLoadFailedRunnable, |
| 1368 |
nsILoadGroup* aLoadGroup) |
1455 |
nsILoadGroup* aLoadGroup) |
| 1369 |
{ |
1456 |
{ |
| 1370 |
AssertIsOnMainThread(); |
1457 |
AssertIsOnMainThread(); |
| 1371 |
|
1458 |
|
| 1372 |
// XXXcatalinb: We need to have a separate load group that's linked to |
1459 |
// XXXcatalinb: We need to have a separate load group that's linked to |
|
Lines 1451-1467
ServiceWorkerPrivate::SpawnWorkerIfNeede
|
Link Here
|
|---|
|
| 1451 |
mWorkerPrivate = WorkerPrivate::Constructor(jsapi.cx(), |
1538 |
mWorkerPrivate = WorkerPrivate::Constructor(jsapi.cx(), |
| 1452 |
scriptSpec, |
1539 |
scriptSpec, |
| 1453 |
false, WorkerTypeService, |
1540 |
false, WorkerTypeService, |
| 1454 |
mInfo->Scope(), &info, error); |
1541 |
mInfo->Scope(), &info, error); |
| 1455 |
if (NS_WARN_IF(error.Failed())) { |
1542 |
if (NS_WARN_IF(error.Failed())) { |
| 1456 |
return error.StealNSResult(); |
1543 |
return error.StealNSResult(); |
| 1457 |
} |
1544 |
} |
| 1458 |
|
1545 |
|
| 1459 |
mIsPushWorker = false; |
1546 |
mIsPushOrSyncWorker = false; |
| 1460 |
RenewKeepAliveToken(aWhy); |
1547 |
RenewKeepAliveToken(aWhy); |
| 1461 |
|
1548 |
|
| 1462 |
return NS_OK; |
1549 |
return NS_OK; |
| 1463 |
} |
1550 |
} |
| 1464 |
|
1551 |
|
| 1465 |
void |
1552 |
void |
| 1466 |
ServiceWorkerPrivate::StoreISupports(nsISupports* aSupports) |
1553 |
ServiceWorkerPrivate::StoreISupports(nsISupports* aSupports) |
| 1467 |
{ |
1554 |
{ |
|
Lines 1518-1534
ServiceWorkerPrivate::NoteDeadServiceWor
|
Link Here
|
|---|
|
| 1518 |
mInfo = nullptr; |
1605 |
mInfo = nullptr; |
| 1519 |
TerminateWorker(); |
1606 |
TerminateWorker(); |
| 1520 |
} |
1607 |
} |
| 1521 |
|
1608 |
|
| 1522 |
void |
1609 |
void |
| 1523 |
ServiceWorkerPrivate::NoteStoppedControllingDocuments() |
1610 |
ServiceWorkerPrivate::NoteStoppedControllingDocuments() |
| 1524 |
{ |
1611 |
{ |
| 1525 |
AssertIsOnMainThread(); |
1612 |
AssertIsOnMainThread(); |
| 1526 |
if (mIsPushWorker || mDebuggerCount) { |
1613 |
if (mIsPushOrSyncWorker || mDebuggerCount) { |
| 1527 |
return; |
1614 |
return; |
| 1528 |
} |
1615 |
} |
| 1529 |
|
1616 |
|
| 1530 |
TerminateWorker(); |
1617 |
TerminateWorker(); |
| 1531 |
} |
1618 |
} |
| 1532 |
|
1619 |
|
| 1533 |
void |
1620 |
void |
| 1534 |
ServiceWorkerPrivate::Activated() |
1621 |
ServiceWorkerPrivate::Activated() |
|
Lines 1658-1675
ServiceWorkerPrivate::TerminateWorkerCal
|
Link Here
|
|---|
|
| 1658 |
} |
1745 |
} |
| 1659 |
|
1746 |
|
| 1660 |
void |
1747 |
void |
| 1661 |
ServiceWorkerPrivate::RenewKeepAliveToken(WakeUpReason aWhy) |
1748 |
ServiceWorkerPrivate::RenewKeepAliveToken(WakeUpReason aWhy) |
| 1662 |
{ |
1749 |
{ |
| 1663 |
// We should have an active worker if we're renewing the keep alive token. |
1750 |
// We should have an active worker if we're renewing the keep alive token. |
| 1664 |
MOZ_ASSERT(mWorkerPrivate); |
1751 |
MOZ_ASSERT(mWorkerPrivate); |
| 1665 |
|
1752 |
|
| 1666 |
if (aWhy == PushEvent || aWhy == PushSubscriptionChangeEvent) { |
1753 |
if (aWhy == PushEvent || aWhy == PushSubscriptionChangeEvent || |
| 1667 |
mIsPushWorker = true; |
1754 |
aWhy == SyncEvent) { |
|
|
1755 |
mIsPushOrSyncWorker = true; |
| 1668 |
} |
1756 |
} |
| 1669 |
|
1757 |
|
| 1670 |
// If there is at least one debugger attached to the worker, the idle worker |
1758 |
// If there is at least one debugger attached to the worker, the idle worker |
| 1671 |
// timeout was canceled when the first debugger attached to the worker. It |
1759 |
// timeout was canceled when the first debugger attached to the worker. It |
| 1672 |
// should not be reset until the last debugger detaches from the worker. |
1760 |
// should not be reset until the last debugger detaches from the worker. |
| 1673 |
if (!mDebuggerCount) { |
1761 |
if (!mDebuggerCount) { |
| 1674 |
ResetIdleTimeout(); |
1762 |
ResetIdleTimeout(); |
| 1675 |
} |
1763 |
} |