|
|
|
|
|
|
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 |
#include "nsArrayUtils.h" |
| 8 |
#include "nsIMutableArray.h" |
| 9 |
#include "nsIOService.h" |
| 10 |
#include "SyncManagerParent.h" |
| 11 |
#include "SyncService.h" |
| 12 |
#include "SyncTypes.h" |
| 13 |
|
| 14 |
namespace mozilla { |
| 15 |
|
| 16 |
namespace dom { |
| 17 |
|
| 18 |
namespace { |
| 19 |
SyncService* sInstance = nullptr; |
| 20 |
} // namespace |
| 21 |
|
| 22 |
class SyncRegistryResponseRunnable : public nsRunnable |
| 23 |
{ |
| 24 |
public: |
| 25 |
SyncRegistryResponseRunnable(SyncRegistryResponse* aResponse) |
| 26 |
: mResponse(aResponse) |
| 27 |
{ |
| 28 |
AssertIsOnMainThread(); |
| 29 |
} |
| 30 |
|
| 31 |
SyncRegistryResponseRunnable(SyncRegistryInternalResponse* aResponse) |
| 32 |
: mInternalResponse(aResponse) |
| 33 |
{ |
| 34 |
AssertIsOnMainThread(); |
| 35 |
} |
| 36 |
|
| 37 |
NS_IMETHODIMP Run() override |
| 38 |
{ |
| 39 |
AssertIsOnBackgroundThread(); |
| 40 |
|
| 41 |
if (mResponse) { |
| 42 |
sInstance->Response(mResponse); |
| 43 |
} else if (mInternalResponse) { |
| 44 |
sInstance->InternalResponse(mInternalResponse); |
| 45 |
} |
| 46 |
|
| 47 |
return NS_OK; |
| 48 |
} |
| 49 |
|
| 50 |
private: |
| 51 |
~SyncRegistryResponseRunnable() {} |
| 52 |
|
| 53 |
RefPtr<SyncRegistryResponse> mResponse; |
| 54 |
RefPtr<SyncRegistryInternalResponse> mInternalResponse; |
| 55 |
}; |
| 56 |
|
| 57 |
class SyncRegistryRequestRunnable final : public nsIRunnable |
| 58 |
, public nsISyncRegistryListener |
| 59 |
{ |
| 60 |
|
| 61 |
public: |
| 62 |
NS_DECL_THREADSAFE_ISUPPORTS |
| 63 |
|
| 64 |
SyncRegistryRequestRunnable(SyncRegistryRequest* aRequest) |
| 65 |
: mRequest(aRequest) |
| 66 |
, mBackgroundThread(NS_GetCurrentThread()) |
| 67 |
{ |
| 68 |
AssertIsOnBackgroundThread(); |
| 69 |
} |
| 70 |
|
| 71 |
NS_IMETHODIMP |
| 72 |
GetRequestId(nsID* aRequestId) |
| 73 |
{ |
| 74 |
AssertIsOnMainThread(); |
| 75 |
|
| 76 |
aRequestId = &(mRequest->mRequestId); |
| 77 |
return NS_OK; |
| 78 |
} |
| 79 |
|
| 80 |
NS_IMETHODIMP |
| 81 |
SetRequestId(const nsID aRequestId) |
| 82 |
{ |
| 83 |
return NS_OK; |
| 84 |
} |
| 85 |
|
| 86 |
NS_IMETHODIMP |
| 87 |
GetActorId(uint64_t* aActorId) |
| 88 |
{ |
| 89 |
AssertIsOnMainThread(); |
| 90 |
|
| 91 |
aActorId = &(mRequest->mActorId); |
| 92 |
return NS_OK; |
| 93 |
} |
| 94 |
|
| 95 |
NS_IMETHODIMP |
| 96 |
SetActorId(const uint64_t aActorId) |
| 97 |
{ |
| 98 |
return NS_OK; |
| 99 |
} |
| 100 |
|
| 101 |
NS_IMETHODIMP |
| 102 |
NotifyAddSuccess() |
| 103 |
{ |
| 104 |
AssertIsOnMainThread(); |
| 105 |
|
| 106 |
return Callback(SyncRegisterResponse()); |
| 107 |
} |
| 108 |
|
| 109 |
NS_IMETHODIMP |
| 110 |
NotifyGetAllSuccess(nsIArray* aRegistrations) |
| 111 |
{ |
| 112 |
AssertIsOnMainThread(); |
| 113 |
|
| 114 |
if (!aRegistrations) { |
| 115 |
return Callback(SyncOpError(static_cast<uint32_t>(NS_ERROR_INVALID_ARG))); |
| 116 |
} |
| 117 |
|
| 118 |
uint32_t length; |
| 119 |
nsresult rv = aRegistrations->GetLength(&length); |
| 120 |
if (NS_WARN_IF(NS_FAILED(rv))) { |
| 121 |
return Callback(SyncOpError(static_cast<uint32_t>(rv))); |
| 122 |
} |
| 123 |
|
| 124 |
if (mRequest->mActorId == 0) { |
| 125 |
// Internal request. |
| 126 |
nsTArray<nsCOMPtr<nsISyncRegistration>> registrations; |
| 127 |
for (uint32_t i = 0; i < length; i++) { |
| 128 |
nsCOMPtr<nsISyncRegistration> registration = |
| 129 |
do_QueryElementAt(aRegistrations, i); |
| 130 |
if (NS_WARN_IF(!registration)) { |
| 131 |
continue; |
| 132 |
} |
| 133 |
registrations.AppendElement(registration); |
| 134 |
} |
| 135 |
|
| 136 |
return Callback(new SyncRegistryInternalResponse( |
| 137 |
SyncRegistryInternalResponse::TGetAllResponse, registrations)); |
| 138 |
} |
| 139 |
|
| 140 |
// DOM request. |
| 141 |
nsTArray<nsString> tags; |
| 142 |
for (uint32_t i = 0; i < length; i++) { |
| 143 |
nsCOMPtr<nsISyncRegistration> registration = |
| 144 |
do_QueryElementAt(aRegistrations, i); |
| 145 |
if (NS_WARN_IF(!registration)) { |
| 146 |
continue; |
| 147 |
} |
| 148 |
nsString tag; |
| 149 |
registration->GetTag(tag); |
| 150 |
tags.AppendElement(tag); |
| 151 |
} |
| 152 |
|
| 153 |
return Callback(SyncGetTagsResponse(tags)); |
| 154 |
} |
| 155 |
|
| 156 |
NS_IMETHODIMP |
| 157 |
NotifyChangeStateSuccess(nsISyncRegistration* aRegistration) |
| 158 |
{ |
| 159 |
AssertIsOnMainThread(); |
| 160 |
|
| 161 |
nsTArray<nsCOMPtr<nsISyncRegistration>> registrations; |
| 162 |
registrations.AppendElement(aRegistration); |
| 163 |
|
| 164 |
return Callback(new SyncRegistryInternalResponse( |
| 165 |
SyncRegistryInternalResponse::TChangeStateResponse, registrations)); |
| 166 |
} |
| 167 |
|
| 168 |
NS_IMETHODIMP |
| 169 |
NotifyError(const uint32_t aError) |
| 170 |
{ |
| 171 |
AssertIsOnMainThread(); |
| 172 |
|
| 173 |
return Callback(SyncOpError(aError)); |
| 174 |
} |
| 175 |
|
| 176 |
NS_IMETHODIMP |
| 177 |
Run() override |
| 178 |
{ |
| 179 |
AssertIsOnMainThread(); |
| 180 |
|
| 181 |
nsCOMPtr<nsISyncRegistry> registry = |
| 182 |
do_CreateInstance("@mozilla.org/dom/sync/registry;1"); |
| 183 |
MOZ_ASSERT(registry); |
| 184 |
|
| 185 |
if (mRequest->mOpOrIOp.type() == SyncOpOrInternalOp::TSyncOp) { |
| 186 |
SyncOp op = mRequest->mOpOrIOp.get_SyncOp(); |
| 187 |
|
| 188 |
switch(op.mArgs().type()) { |
| 189 |
case SyncOpArgs::TSyncRegisterArgs: |
| 190 |
{ |
| 191 |
nsCOMPtr<nsISyncRegistration> registration = |
| 192 |
do_CreateInstance("@mozilla.org/dom/sync/registration;1"); |
| 193 |
MOZ_ASSERT(registration); |
| 194 |
|
| 195 |
nsCOMPtr<nsIPrincipal> principal = |
| 196 |
PrincipalInfoToPrincipal(op.mPrincipal()); |
| 197 |
if (NS_WARN_IF(!principal)) { |
| 198 |
return NotifyError(static_cast<uint32_t>(NS_ERROR_INVALID_ARG)); |
| 199 |
} |
| 200 |
|
| 201 |
nsAutoCString originSuffix; |
| 202 |
nsresult rv = principal->GetOriginSuffix(originSuffix); |
| 203 |
if (NS_WARN_IF(NS_FAILED(rv))) { |
| 204 |
return NotifyError(static_cast<uint32_t>(NS_ERROR_INVALID_ARG)); |
| 205 |
} |
| 206 |
|
| 207 |
registration->SetOriginSuffix(NS_ConvertUTF8toUTF16(originSuffix)); |
| 208 |
registration->SetScope(op.mArgs().get_SyncRegisterArgs().mScope()); |
| 209 |
registration->SetTag(op.mArgs().get_SyncRegisterArgs().mTag()); |
| 210 |
|
| 211 |
registry->Add(registration, this); |
| 212 |
|
| 213 |
break; |
| 214 |
} |
| 215 |
case SyncOpArgs::TSyncGetTagsArgs: |
| 216 |
registry->GetAll(this); |
| 217 |
break; |
| 218 |
default: |
| 219 |
MOZ_CRASH("Unknown BackgroundSync request"); |
| 220 |
} |
| 221 |
|
| 222 |
} else if (mRequest->mOpOrIOp.type() == SyncOpOrInternalOp::TSyncInternalOp) { |
| 223 |
|
| 224 |
SyncInternalOp op = mRequest->mOpOrIOp.get_SyncInternalOp(); |
| 225 |
|
| 226 |
switch (op.mArgs().type()) { |
| 227 |
case SyncInternalOpArgs::TSyncGetAllArgs: |
| 228 |
registry->GetAll(this); |
| 229 |
break; |
| 230 |
case SyncInternalOpArgs::TSyncChangeStateArgs: |
| 231 |
registry->ChangeState(op.mArgs().get_SyncChangeStateArgs().mId(), |
| 232 |
op.mArgs().get_SyncChangeStateArgs().mState(), |
| 233 |
this); |
| 234 |
break; |
| 235 |
case SyncInternalOpArgs::TSyncRemoveArgs: |
| 236 |
registry->Remove(op.mArgs().get_SyncRemoveArgs().mId(), this); |
| 237 |
break; |
| 238 |
default: |
| 239 |
MOZ_CRASH("Unknown BackgroundSync internal request"); |
| 240 |
} |
| 241 |
|
| 242 |
} else { |
| 243 |
MOZ_CRASH("Unknown BackgroundSync registry request"); |
| 244 |
} |
| 245 |
|
| 246 |
return NS_OK; |
| 247 |
} |
| 248 |
|
| 249 |
private: |
| 250 |
~SyncRegistryRequestRunnable() {} |
| 251 |
|
| 252 |
nsresult |
| 253 |
Callback(const SyncOpResponse& aResponse) |
| 254 |
{ |
| 255 |
AssertIsOnMainThread(); |
| 256 |
|
| 257 |
RefPtr<SyncRegistryResponse> response = |
| 258 |
new SyncRegistryResponse(mRequest->mActorId, |
| 259 |
mRequest->mRequestId, |
| 260 |
aResponse); |
| 261 |
RefPtr<SyncRegistryResponseRunnable> callback = |
| 262 |
new SyncRegistryResponseRunnable(response); |
| 263 |
MOZ_ASSERT(callback); |
| 264 |
|
| 265 |
mBackgroundThread->Dispatch(callback, NS_DISPATCH_NORMAL); |
| 266 |
|
| 267 |
return NS_OK; |
| 268 |
} |
| 269 |
|
| 270 |
nsresult |
| 271 |
Callback(SyncRegistryInternalResponse* aResponse) |
| 272 |
{ |
| 273 |
AssertIsOnMainThread(); |
| 274 |
|
| 275 |
RefPtr<SyncRegistryResponseRunnable> callback = |
| 276 |
new SyncRegistryResponseRunnable(aResponse); |
| 277 |
MOZ_ASSERT(callback); |
| 278 |
|
| 279 |
mBackgroundThread->Dispatch(callback, NS_DISPATCH_NORMAL); |
| 280 |
|
| 281 |
return NS_OK; |
| 282 |
} |
| 283 |
|
| 284 |
RefPtr<SyncRegistryRequest> mRequest; |
| 285 |
nsCOMPtr<nsIThread> mBackgroundThread; |
| 286 |
}; |
| 287 |
|
| 288 |
NS_IMPL_ISUPPORTS(SyncRegistryRequestRunnable, nsIRunnable, nsISyncRegistryListener); |
| 289 |
|
| 290 |
class SyncServiceNotifyOnlineStateRunnable final : public nsRunnable |
| 291 |
{ |
| 292 |
public: |
| 293 |
SyncServiceNotifyOnlineStateRunnable(const OnlineState aState) |
| 294 |
: mOnlineState(aState) |
| 295 |
{ |
| 296 |
AssertIsOnMainThread(); |
| 297 |
} |
| 298 |
|
| 299 |
NS_IMETHOD |
| 300 |
Run() override |
| 301 |
{ |
| 302 |
AssertIsOnBackgroundThread(); |
| 303 |
|
| 304 |
sInstance->OnlineStateChange(mOnlineState); |
| 305 |
|
| 306 |
return NS_OK; |
| 307 |
} |
| 308 |
|
| 309 |
private: |
| 310 |
~SyncServiceNotifyOnlineStateRunnable() {} |
| 311 |
|
| 312 |
OnlineState mOnlineState; |
| 313 |
}; |
| 314 |
|
| 315 |
class SyncServiceInitRunnable final : public nsIRunnable |
| 316 |
, public nsIObserver |
| 317 |
{ |
| 318 |
public: |
| 319 |
NS_DECL_THREADSAFE_ISUPPORTS |
| 320 |
|
| 321 |
SyncServiceInitRunnable() |
| 322 |
: mBackgroundThread(NS_GetCurrentThread()) |
| 323 |
{ |
| 324 |
AssertIsOnBackgroundThread(); |
| 325 |
} |
| 326 |
|
| 327 |
NS_IMETHOD |
| 328 |
Observe(nsISupports* aSubject, const char* aTopic, |
| 329 |
const char16_t* aData) override |
| 330 |
{ |
| 331 |
AssertIsOnMainThread(); |
| 332 |
|
| 333 |
OnlineState state = OFFLINE; |
| 334 |
if (nsDependentString(aData).EqualsLiteral(NS_IOSERVICE_ONLINE)) { |
| 335 |
state = ONLINE; |
| 336 |
} |
| 337 |
|
| 338 |
mBackgroundThread->Dispatch( |
| 339 |
new SyncServiceNotifyOnlineStateRunnable(state), NS_DISPATCH_NORMAL); |
| 340 |
|
| 341 |
return NS_OK; |
| 342 |
} |
| 343 |
|
| 344 |
NS_IMETHOD |
| 345 |
Run() override |
| 346 |
{ |
| 347 |
AssertIsOnMainThread(); |
| 348 |
|
| 349 |
nsCOMPtr<nsIObserverService> os = services::GetObserverService(); |
| 350 |
NS_ENSURE_STATE(os); |
| 351 |
// The observer service holds us alive. |
| 352 |
os->AddObserver(this, NS_IOSERVICE_OFFLINE_STATUS_TOPIC, |
| 353 |
/* holdsWeak */ false); |
| 354 |
|
| 355 |
bool offline = true; |
| 356 |
nsCOMPtr<nsIIOService> ioService = services::GetIOService(); |
| 357 |
NS_ENSURE_STATE(ioService); |
| 358 |
ioService->GetOffline(&offline); |
| 359 |
NS_WARN_IF(NS_FAILED(ioService->GetOffline(&offline))); |
| 360 |
|
| 361 |
OnlineState state = UNKNOWN; |
| 362 |
if (offline) { |
| 363 |
state = OFFLINE; |
| 364 |
} else { |
| 365 |
state = ONLINE; |
| 366 |
} |
| 367 |
|
| 368 |
mBackgroundThread->Dispatch( |
| 369 |
new SyncServiceNotifyOnlineStateRunnable(state), NS_DISPATCH_NORMAL); |
| 370 |
|
| 371 |
return NS_OK; |
| 372 |
} |
| 373 |
|
| 374 |
private: |
| 375 |
~SyncServiceInitRunnable() {} |
| 376 |
|
| 377 |
nsCOMPtr<nsIThread> mBackgroundThread; |
| 378 |
}; |
| 379 |
|
| 380 |
NS_IMPL_ISUPPORTS(SyncServiceInitRunnable, nsIRunnable, nsIObserver) |
| 381 |
|
| 382 |
SyncService::SyncService() |
| 383 |
: mOnlineState(UNKNOWN) |
| 384 |
{ |
| 385 |
AssertIsOnBackgroundThread(); |
| 386 |
|
| 387 |
// sInstance is a raw SyncService*. |
| 388 |
MOZ_ASSERT(!sInstance); |
| 389 |
sInstance = this; |
| 390 |
} |
| 391 |
|
| 392 |
SyncService::~SyncService() |
| 393 |
{ |
| 394 |
AssertIsOnBackgroundThread(); |
| 395 |
MOZ_ASSERT(sInstance == this); |
| 396 |
MOZ_ASSERT(mSyncManagerActors.Count() == 0); |
| 397 |
|
| 398 |
sInstance = nullptr; |
| 399 |
} |
| 400 |
|
| 401 |
// static |
| 402 |
already_AddRefed<SyncService> |
| 403 |
SyncService::GetOrCreate() |
| 404 |
{ |
| 405 |
AssertIsOnBackgroundThread(); |
| 406 |
|
| 407 |
RefPtr<SyncService> instance = sInstance; |
| 408 |
if (!instance) { |
| 409 |
instance = new SyncService(); |
| 410 |
instance->Init(); |
| 411 |
} |
| 412 |
return instance.forget(); |
| 413 |
} |
| 414 |
|
| 415 |
void |
| 416 |
SyncService::Init() |
| 417 |
{ |
| 418 |
NS_DispatchToMainThread(new SyncServiceInitRunnable()); |
| 419 |
} |
| 420 |
|
| 421 |
void |
| 422 |
SyncService::RegisterActor(SyncManagerParent* aParent) |
| 423 |
{ |
| 424 |
AssertIsOnBackgroundThread(); |
| 425 |
MOZ_ASSERT(aParent); |
| 426 |
MOZ_ASSERT(!mSyncManagerActors.Contains(aParent)); |
| 427 |
|
| 428 |
mSyncManagerActors.PutEntry(aParent); |
| 429 |
} |
| 430 |
|
| 431 |
void |
| 432 |
SyncService::UnregisterActor(SyncManagerParent* aParent) |
| 433 |
{ |
| 434 |
AssertIsOnBackgroundThread(); |
| 435 |
MOZ_ASSERT(aParent); |
| 436 |
MOZ_ASSERT(mSyncManagerActors.Contains(aParent)); |
| 437 |
|
| 438 |
mSyncManagerActors.RemoveEntry(aParent); |
| 439 |
} |
| 440 |
|
| 441 |
void |
| 442 |
SyncService::RegisterActor(workers::ServiceWorkerManagerParent* aParent) |
| 443 |
{ |
| 444 |
AssertIsOnBackgroundThread(); |
| 445 |
MOZ_ASSERT(aParent); |
| 446 |
MOZ_ASSERT(!mServiceWorkerManagerActors.Contains(aParent)); |
| 447 |
|
| 448 |
mServiceWorkerManagerActors.PutEntry(aParent); |
| 449 |
} |
| 450 |
|
| 451 |
void |
| 452 |
SyncService::UnregisterActor(workers::ServiceWorkerManagerParent* aParent) |
| 453 |
{ |
| 454 |
AssertIsOnBackgroundThread(); |
| 455 |
MOZ_ASSERT(aParent); |
| 456 |
MOZ_ASSERT(mServiceWorkerManagerActors.Contains(aParent)); |
| 457 |
|
| 458 |
mServiceWorkerManagerActors.RemoveEntry(aParent); |
| 459 |
} |
| 460 |
|
| 461 |
void |
| 462 |
SyncService::Request(const uint64_t aActorId, |
| 463 |
const nsID& aRequestId, |
| 464 |
const SyncOp& aOp) |
| 465 |
{ |
| 466 |
AssertIsOnBackgroundThread(); |
| 467 |
|
| 468 |
RefPtr<SyncRegistryRequestRunnable> runnable = |
| 469 |
new SyncRegistryRequestRunnable( |
| 470 |
new SyncRegistryRequest(aActorId, aRequestId, aOp)); |
| 471 |
NS_DispatchToMainThread(runnable); |
| 472 |
} |
| 473 |
|
| 474 |
void |
| 475 |
SyncService::Response(SyncRegistryResponse* aResponse) |
| 476 |
{ |
| 477 |
AssertIsOnBackgroundThread(); |
| 478 |
|
| 479 |
for (auto iter = mSyncManagerActors.Iter(); !iter.Done(); iter.Next()) { |
| 480 |
RefPtr<SyncManagerParent> parent = iter.Get()->GetKey(); |
| 481 |
MOZ_ASSERT(parent); |
| 482 |
if (parent->Id() != aResponse->mActorId || |
| 483 |
parent->ActorDestroyed()) { |
| 484 |
continue; |
| 485 |
} |
| 486 |
parent->NotifyResponse(aResponse->mRequestId, aResponse->mResponse); |
| 487 |
} |
| 488 |
|
| 489 |
// If we just successfully added a new sync request we try to trigger |
| 490 |
// the corresponding sync event. |
| 491 |
if (aResponse->mResponse.type() == SyncOpResponse::TSyncRegisterResponse) { |
| 492 |
StartTriggerSyncEvents(); |
| 493 |
} |
| 494 |
} |
| 495 |
|
| 496 |
void |
| 497 |
SyncService::InternalResponse(SyncRegistryInternalResponse* aResponse) |
| 498 |
{ |
| 499 |
AssertIsOnBackgroundThread(); |
| 500 |
MOZ_ASSERT(aResponse); |
| 501 |
|
| 502 |
nsTArray<nsCOMPtr<nsISyncRegistration>> registrations; |
| 503 |
aResponse->GetRegistrations(registrations); |
| 504 |
|
| 505 |
switch (aResponse->type()) { |
| 506 |
case SyncRegistryInternalResponse::TGetAllResponse: |
| 507 |
ContinueTriggerSyncEvents(registrations); |
| 508 |
break; |
| 509 |
case SyncRegistryInternalResponse::TChangeStateResponse: |
| 510 |
MOZ_ASSERT((registrations.Length() == 1)); |
| 511 |
|
| 512 |
uint16_t state; |
| 513 |
registrations[0]->GetState(&state); |
| 514 |
|
| 515 |
if (state == nsISyncRegistry::STATE_FIRING) { |
| 516 |
// After setting the state of the registration to STATE_FIRING |
| 517 |
// we try to actually trigger the sync event. |
| 518 |
TriggerSyncEvent(Move(registrations[0])); |
| 519 |
} |
| 520 |
break; |
| 521 |
default: |
| 522 |
MOZ_CRASH("Unknown BackgroundSync internal response"); |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
void SyncService::InternalRequest(const SyncInternalOpArgs& aArgs) |
| 527 |
{ |
| 528 |
AssertIsOnBackgroundThread(); |
| 529 |
|
| 530 |
const SyncInternalOp op(aArgs); |
| 531 |
|
| 532 |
RefPtr<SyncRegistryRequestRunnable> runnable = |
| 533 |
new SyncRegistryRequestRunnable( |
| 534 |
new SyncRegistryRequest(0 /* internal request */, nsID(), op)); |
| 535 |
NS_DispatchToMainThread(runnable); |
| 536 |
} |
| 537 |
|
| 538 |
void |
| 539 |
SyncService::StartTriggerSyncEvents() |
| 540 |
{ |
| 541 |
AssertIsOnBackgroundThread(); |
| 542 |
|
| 543 |
// We need to jump to the main thread to get the list of registered |
| 544 |
// sync requests. If we are still online while coming back to the |
| 545 |
// background thread with this list of sync requests, we will fire |
| 546 |
// a sync event for each of them. |
| 547 |
const SyncGetAllArgs args; |
| 548 |
InternalRequest(args); |
| 549 |
} |
| 550 |
|
| 551 |
void |
| 552 |
SyncService::RemoveSyncRegistration(const nsString& aId) |
| 553 |
{ |
| 554 |
AssertIsOnBackgroundThread(); |
| 555 |
|
| 556 |
const SyncRemoveArgs args(aId); |
| 557 |
InternalRequest(args); |
| 558 |
} |
| 559 |
|
| 560 |
void |
| 561 |
SyncService::ContinueTriggerSyncEvents( |
| 562 |
nsTArray<nsCOMPtr<nsISyncRegistration>>& aRegistrations) |
| 563 |
{ |
| 564 |
AssertIsOnBackgroundThread(); |
| 565 |
|
| 566 |
// At this point we got the list of registered sync requests from |
| 567 |
// the main thread. If we are still online, we try to trigger a sync |
| 568 |
// event for each request. |
| 569 |
if (mOnlineState != ONLINE) { |
| 570 |
return; |
| 571 |
} |
| 572 |
|
| 573 |
// Before actually triggering the sync event, we need to set each |
| 574 |
// registration entry state to STATE_FIRING. |
| 575 |
for (uint32_t i = 0; i < aRegistrations.Length(); i++) { |
| 576 |
nsCOMPtr<nsISyncRegistration> registration = aRegistrations[i]; |
| 577 |
if (NS_WARN_IF(!registration)) { |
| 578 |
continue; |
| 579 |
} |
| 580 |
|
| 581 |
// If the state of this registration entry is not 'pending', we ignore it. |
| 582 |
uint16_t state; |
| 583 |
registration->GetState(&state); |
| 584 |
if (state != nsISyncRegistry::STATE_PENDING) { |
| 585 |
continue; |
| 586 |
} |
| 587 |
|
| 588 |
// Otherwise, we set the registration state to 'firing'. |
| 589 |
nsString id; |
| 590 |
registration->GetId(id); |
| 591 |
MOZ_ASSERT(!id.IsEmpty()); |
| 592 |
const SyncInternalOp op( |
| 593 |
(SyncChangeStateArgs(id, nsISyncRegistry::STATE_FIRING))); |
| 594 |
RefPtr<SyncRegistryRequestRunnable> runnable = |
| 595 |
new SyncRegistryRequestRunnable( |
| 596 |
new SyncRegistryRequest(0 /* internal request */, nsID(), op)); |
| 597 |
NS_DispatchToMainThread(runnable); |
| 598 |
} |
| 599 |
} |
| 600 |
|
| 601 |
void |
| 602 |
SyncService::TriggerSyncEvent(nsISyncRegistration* aRegistration) |
| 603 |
{ |
| 604 |
AssertIsOnBackgroundThread(); |
| 605 |
MOZ_ASSERT(aRegistration); |
| 606 |
|
| 607 |
uint16_t state; |
| 608 |
aRegistration->GetState(&state); |
| 609 |
MOZ_ASSERT(state == nsISyncRegistry::STATE_FIRING); |
| 610 |
|
| 611 |
nsString originSuffix; |
| 612 |
aRegistration->GetOriginSuffix(originSuffix); |
| 613 |
|
| 614 |
nsString scope; |
| 615 |
aRegistration->GetScope(scope); |
| 616 |
MOZ_ASSERT(!scope.IsEmpty()); |
| 617 |
|
| 618 |
nsString tag; |
| 619 |
aRegistration->GetTag(tag); |
| 620 |
MOZ_ASSERT(!tag.IsEmpty()); |
| 621 |
|
| 622 |
bool lastChance; |
| 623 |
aRegistration->GetLastChance(&lastChance); |
| 624 |
|
| 625 |
for (auto iter = mServiceWorkerManagerActors.Iter(); !iter.Done(); |
| 626 |
iter.Next()) { |
| 627 |
RefPtr<ServiceWorkerManagerParent> parent = iter.Get()->GetKey(); |
| 628 |
MOZ_ASSERT(parent); |
| 629 |
Unused << parent->SendNotifySyncEvent(originSuffix, scope, tag, |
| 630 |
lastChance); |
| 631 |
// XXX Bug 1260141. For now we simply remove the sync registration, but |
| 632 |
// we need to implement the sync event handling logic. |
| 633 |
nsString id; |
| 634 |
aRegistration->GetId(id); |
| 635 |
RemoveSyncRegistration(id); |
| 636 |
} |
| 637 |
} |
| 638 |
|
| 639 |
void |
| 640 |
SyncService::OnlineStateChange(const OnlineState& aState) |
| 641 |
{ |
| 642 |
mOnlineState = aState; |
| 643 |
|
| 644 |
if (mOnlineState != ONLINE) { |
| 645 |
return; |
| 646 |
} |
| 647 |
|
| 648 |
// If we are online we need to get the list of registered sync requests |
| 649 |
// to trigger the corresponding 'sync' events. |
| 650 |
StartTriggerSyncEvents(); |
| 651 |
} |
| 652 |
|
| 653 |
} // namespace dom |
| 654 |
} // namespace mozilla |