|
|
|
|
|
|
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 "DBAction.h" |
| 8 |
#include "StorageManager.h" |
| 9 |
|
| 10 |
#include "mozilla/AutoRestore.h" |
| 11 |
#include "mozilla/StaticMutex.h" |
| 12 |
#include "nsIThread.h" |
| 13 |
#include "nsThreadUtils.h" |
| 14 |
|
| 15 |
namespace mozilla { |
| 16 |
namespace dom { |
| 17 |
namespace backgroundsync { |
| 18 |
|
| 19 |
namespace { |
| 20 |
|
| 21 |
// ClientActions that are executed when a ClientContext is first created. |
| 22 |
// It ensures that the database is setup properly. |
| 23 |
// This lets other actions not worry about these details. |
| 24 |
class SetupAction final : public SyncDBAction |
| 25 |
{ |
| 26 |
public: |
| 27 |
SetupAction() |
| 28 |
: SyncDBAction(DBAction::Create) |
| 29 |
{ } |
| 30 |
|
| 31 |
virtual nsresult |
| 32 |
RunSyncOnTarget(const QuotaInfo& aQuotaInfo, |
| 33 |
mozIStorageConnection* aConn) override |
| 34 |
{ |
| 35 |
// Executes in its own transaction. |
| 36 |
nsresult rv = db::CreateOrMigrateSchema(aConn); |
| 37 |
if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } |
| 38 |
|
| 39 |
return rv; |
| 40 |
} |
| 41 |
}; |
| 42 |
|
| 43 |
} // anonymous namespace |
| 44 |
|
| 45 |
// --------------------------------------------------------------------------- |
| 46 |
|
| 47 |
// Singleton class to track StorageManager instances and ensure there is only |
| 48 |
// one for each unique StorageManagerId. |
| 49 |
class StorageManager::Factory |
| 50 |
{ |
| 51 |
public: |
| 52 |
friend class StaticAutoPtr<StorageManager::Factory>; |
| 53 |
|
| 54 |
static nsresult |
| 55 |
GetOrCreate(StorageManagerId* aManagerId, |
| 56 |
StorageManager** aManagerOut) |
| 57 |
{ |
| 58 |
AssertIsOnBackgroundThread(); |
| 59 |
|
| 60 |
// Ensure there is a factory instance. This forces the Get() call |
| 61 |
// below to use the same factory. |
| 62 |
nsresult rv = MaybeCreateInstance(); |
| 63 |
if (NS_WARN_IF(NS_FAILED(rv))) { |
| 64 |
return rv; |
| 65 |
} |
| 66 |
|
| 67 |
RefPtr<StorageManager> ref = Get(aManagerId); |
| 68 |
if (!ref) { |
| 69 |
nsCOMPtr<nsIThread> ioThread; |
| 70 |
rv = NS_NewNamedThread("BSyncIOThread", getter_AddRefs(ioThread)); |
| 71 |
if (NS_WARN_IF(NS_FAILED(rv))) { |
| 72 |
return rv; |
| 73 |
} |
| 74 |
|
| 75 |
ref = new StorageManager(aManagerId, ioThread); |
| 76 |
|
| 77 |
// There may be an old manager for this origin in the process of |
| 78 |
// cleaning up. We need to tell the new manager about this so |
| 79 |
// that it won't actually start until the old manager is done. |
| 80 |
RefPtr<StorageManager> oldManager = Get(aManagerId, Closing); |
| 81 |
ref->Init(oldManager); |
| 82 |
|
| 83 |
MOZ_ASSERT(!sFactory->mManagerList.Contains(ref)); |
| 84 |
sFactory->mManagerList.AppendElement(ref); |
| 85 |
} |
| 86 |
|
| 87 |
ref.forget(aManagerOut); |
| 88 |
|
| 89 |
return NS_OK; |
| 90 |
} |
| 91 |
|
| 92 |
static already_AddRefed<StorageManager> |
| 93 |
Get(StorageManagerId* aManagerId, State aState = Open) |
| 94 |
{ |
| 95 |
AssertIsOnBackgroundThread(); |
| 96 |
|
| 97 |
nsresult rv = MaybeCreateInstance(); |
| 98 |
if (NS_WARN_IF(NS_FAILED(rv))) { |
| 99 |
return nullptr; |
| 100 |
} |
| 101 |
|
| 102 |
// Iterate in reverse to find the most recent, matching Manager. This |
| 103 |
// is important when looking for a Closing Manager. If a new Manager |
| 104 |
// chains to an old Manager we want it to be the most recent one. |
| 105 |
ManagerList::BackwardIterator iter(sFactory->mManagerList); |
| 106 |
while (iter.HasMore()) { |
| 107 |
RefPtr<StorageManager> manager = iter.GetNext(); |
| 108 |
if (aState == manager->GetState() && *manager->mManagerId == *aManagerId) { |
| 109 |
return manager.forget(); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
return nullptr; |
| 114 |
} |
| 115 |
|
| 116 |
static void |
| 117 |
Remove(StorageManager* aManager) |
| 118 |
{ |
| 119 |
AssertIsOnBackgroundThread(); |
| 120 |
MOZ_ASSERT(aManager); |
| 121 |
MOZ_ASSERT(sFactory); |
| 122 |
|
| 123 |
MOZ_ALWAYS_TRUE(sFactory->mManagerList.RemoveElement(aManager)); |
| 124 |
|
| 125 |
// Clean up the factory singleton if there are no more managers. |
| 126 |
MaybeDestroyInstance(); |
| 127 |
} |
| 128 |
|
| 129 |
static void |
| 130 |
Abort(const nsACString& aOrigin) |
| 131 |
{ |
| 132 |
AssertIsOnBackgroundThread(); |
| 133 |
|
| 134 |
if (!sFactory) { |
| 135 |
return; |
| 136 |
} |
| 137 |
|
| 138 |
MOZ_ASSERT(!sFactory->mManagerList.IsEmpty()); |
| 139 |
|
| 140 |
{ |
| 141 |
ManagerList::ForwardIterator iter(sFactory->mManagerList); |
| 142 |
while (iter.HasMore()) { |
| 143 |
RefPtr<StorageManager> manager = iter.GetNext(); |
| 144 |
if (aOrigin.IsVoid() || |
| 145 |
manager->mManagerId->QuotaOrigin() == aOrigin) { |
| 146 |
manager->Abort(); |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
static void |
| 153 |
ShutdownAll() |
| 154 |
{ |
| 155 |
AssertIsOnBackgroundThread(); |
| 156 |
|
| 157 |
if (!sFactory) { |
| 158 |
return; |
| 159 |
} |
| 160 |
|
| 161 |
MOZ_ASSERT(!sFactory->mManagerList.IsEmpty()); |
| 162 |
|
| 163 |
{ |
| 164 |
// Note that we are synchronously calling shutdown code here. If any |
| 165 |
// of the shutdown code synchronously decides to delete the Factory |
| 166 |
// we need to delay that delete until the end of this method. |
| 167 |
AutoRestore<bool> restore(sFactory->mInSyncShutdown); |
| 168 |
sFactory->mInSyncShutdown = true; |
| 169 |
|
| 170 |
ManagerList::ForwardIterator iter(sFactory->mManagerList); |
| 171 |
while (iter.HasMore()) { |
| 172 |
RefPtr<StorageManager> manager = iter.GetNext(); |
| 173 |
manager->Shutdown(); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
MaybeDestroyInstance(); |
| 178 |
} |
| 179 |
|
| 180 |
static bool |
| 181 |
IsShutdownAllComplete() |
| 182 |
{ |
| 183 |
AssertIsOnBackgroundThread(); |
| 184 |
return !sFactory; |
| 185 |
} |
| 186 |
|
| 187 |
private: |
| 188 |
Factory() |
| 189 |
: mInSyncShutdown(false) |
| 190 |
{ |
| 191 |
MOZ_COUNT_CTOR(StorageManager::Factory); |
| 192 |
} |
| 193 |
|
| 194 |
~Factory() |
| 195 |
{ |
| 196 |
MOZ_COUNT_DTOR(StorageManager::Factory); |
| 197 |
MOZ_ASSERT(mManagerList.IsEmpty()); |
| 198 |
MOZ_ASSERT(!mInSyncShutdown); |
| 199 |
} |
| 200 |
|
| 201 |
static nsresult |
| 202 |
MaybeCreateInstance() |
| 203 |
{ |
| 204 |
AssertIsOnBackgroundThread(); |
| 205 |
|
| 206 |
if (!sFactory) { |
| 207 |
// Be clear about what we are locking. sFactory is bg thread only, so |
| 208 |
// we don't need to lock it here. Just protect sFactoryShutdown and |
| 209 |
// sBackgroundThread. |
| 210 |
{ |
| 211 |
StaticMutexAutoLock lock(sMutex); |
| 212 |
|
| 213 |
if (sFactoryShutdown) { |
| 214 |
return NS_ERROR_ILLEGAL_DURING_SHUTDOWN; |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
// We cannot use ClearOnShutdown() here because we're not on the main |
| 219 |
// thread. Instead, we delete sFactory in Factory::Remove() after the |
| 220 |
// last manager is removed. ShutdownObserver ensures this happens |
| 221 |
// before shutdown. |
| 222 |
sFactory = new Factory(); |
| 223 |
} |
| 224 |
|
| 225 |
// Never return sFactory to code outside Factory. We need to delete it |
| 226 |
// out from under ourselves just before we return from Remove(). This |
| 227 |
// would be (even more) dangerous if other code had a pointer to the |
| 228 |
// factory itself. |
| 229 |
|
| 230 |
return NS_OK; |
| 231 |
} |
| 232 |
|
| 233 |
static void |
| 234 |
MaybeDestroyInstance() |
| 235 |
{ |
| 236 |
AssertIsOnBackgroundThread(); |
| 237 |
MOZ_ASSERT(sFactory); |
| 238 |
|
| 239 |
// If the factory is still in use then we cannot delete yet. This |
| 240 |
// could be due to managers still existing or because we are in the |
| 241 |
// middle of shutting down. We need to be careful not to delete ourself |
| 242 |
// synchronously during shutdown. |
| 243 |
if (!sFactory->mManagerList.IsEmpty() || sFactory->mInSyncShutdown) { |
| 244 |
return; |
| 245 |
} |
| 246 |
|
| 247 |
sFactory = nullptr; |
| 248 |
} |
| 249 |
|
| 250 |
// Singleton created on demand and deleted when last Manager is cleared |
| 251 |
// in Remove(). PBackground thread only. |
| 252 |
static StaticAutoPtr<Factory> sFactory; |
| 253 |
|
| 254 |
// Protects following static attribute. |
| 255 |
static StaticMutex sMutex; |
| 256 |
|
| 257 |
// Indicate if shutdown has occurred to block re-creation of sFactory. |
| 258 |
// Must hold sMutex to access. |
| 259 |
static bool sFactoryShutdown; |
| 260 |
|
| 261 |
// Weak references as we don't want to keep StorageManager objects alive |
| 262 |
// forever. |
| 263 |
// When a Manager is destroyed it calls Factory::Remove() to clear itself. |
| 264 |
// PBackground thread only. |
| 265 |
typedef nsTObserverArray<StorageManager*> ManagerList; |
| 266 |
ManagerList mManagerList; |
| 267 |
|
| 268 |
// This flag is set when we are looping through the list and calling |
| 269 |
// Shutdown() on each Manager. We need to be careful not to synchronously |
| 270 |
// trigger the deletion of the factory while still executing this loop. |
| 271 |
bool mInSyncShutdown; |
| 272 |
}; |
| 273 |
|
| 274 |
// static |
| 275 |
StaticAutoPtr<StorageManager::Factory> StorageManager::Factory::sFactory; |
| 276 |
|
| 277 |
// static |
| 278 |
StaticMutex StorageManager::Factory::sMutex; |
| 279 |
|
| 280 |
// static |
| 281 |
bool StorageManager::Factory::sFactoryShutdown = false; |
| 282 |
|
| 283 |
// --------------------------------------------------------------------------- |
| 284 |
|
| 285 |
// Abstract class to help implement the varios ClientActions. |
| 286 |
class StorageManager::BaseAction : public SyncDBAction |
| 287 |
{ |
| 288 |
protected: |
| 289 |
BaseAction(const nsID& aRequestId, StorageManager* aManager) |
| 290 |
: SyncDBAction(DBAction::Existing) |
| 291 |
, mRequestId(aRequestId) |
| 292 |
, mManager(aManager) |
| 293 |
{} |
| 294 |
|
| 295 |
virtual void |
| 296 |
Complete(nsresult aRv) = 0; |
| 297 |
|
| 298 |
virtual void |
| 299 |
CompleteOnInitiatingThread(nsresult aRv) override |
| 300 |
{ |
| 301 |
NS_ASSERT_OWNINGTHREAD(StorageManager::BaseAction); |
| 302 |
|
| 303 |
Complete(aRv); |
| 304 |
|
| 305 |
// Ensure we release the manager on the initiating thread. |
| 306 |
mManager = nullptr; |
| 307 |
} |
| 308 |
|
| 309 |
const nsID mRequestId; |
| 310 |
RefPtr<StorageManager> mManager; |
| 311 |
}; |
| 312 |
|
| 313 |
// --------------------------------------------------------------------------- |
| 314 |
|
| 315 |
class StorageManager::RegisterAction final : public StorageManager::BaseAction |
| 316 |
{ |
| 317 |
public: |
| 318 |
RegisterAction(const nsID& aRequestId, |
| 319 |
StorageManager* aManager, |
| 320 |
const SyncRegisterArgs& aRegisterArgs) |
| 321 |
: BaseAction(aRequestId, aManager) |
| 322 |
, mArgs(aRegisterArgs) |
| 323 |
, mFirstRegistration(true) |
| 324 |
{} |
| 325 |
|
| 326 |
virtual nsresult |
| 327 |
RunSyncOnTarget(const QuotaInfo& aQuotaInfo, |
| 328 |
mozIStorageConnection* aConn) override |
| 329 |
{ |
| 330 |
nsresult rv = db::Register(aConn, mArgs, mFirstRegistration, |
| 331 |
mRegistration); |
| 332 |
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "db::Register failed"); |
| 333 |
return rv; |
| 334 |
} |
| 335 |
|
| 336 |
virtual void |
| 337 |
Complete(nsresult aRv) override |
| 338 |
{ |
| 339 |
if (NS_FAILED(aRv)) { |
| 340 |
mManager->OnRequestComplete(mRequestId, |
| 341 |
SyncOpError(static_cast<uint32_t>(aRv))); |
| 342 |
} else { |
| 343 |
mManager->OnRequestComplete(mRequestId, |
| 344 |
SyncRegisterResponse(mRegistration, mFirstRegistration)); |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
private: |
| 349 |
SyncRegisterArgs mArgs; |
| 350 |
bool mFirstRegistration; |
| 351 |
Registration mRegistration; |
| 352 |
}; |
| 353 |
|
| 354 |
// --------------------------------------------------------------------------- |
| 355 |
|
| 356 |
class StorageManager::GetTagsAction final : public StorageManager::BaseAction |
| 357 |
{ |
| 358 |
public: |
| 359 |
GetTagsAction(const nsID& aRequestId, |
| 360 |
StorageManager* aManager, |
| 361 |
const SyncGetTagsArgs& aGetTagsArgs) |
| 362 |
: BaseAction(aRequestId, aManager) |
| 363 |
, mArgs(aGetTagsArgs) |
| 364 |
{} |
| 365 |
|
| 366 |
virtual nsresult |
| 367 |
RunSyncOnTarget(const QuotaInfo& aQuotaInfo, |
| 368 |
mozIStorageConnection* aConn) override |
| 369 |
{ |
| 370 |
nsresult rv = db::GetTags(aConn, mArgs, mTags); |
| 371 |
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "GetTags failed"); |
| 372 |
return rv; |
| 373 |
} |
| 374 |
|
| 375 |
virtual void |
| 376 |
Complete(nsresult aRv) override |
| 377 |
{ |
| 378 |
if (NS_FAILED(aRv)) { |
| 379 |
mManager->OnRequestComplete(mRequestId, |
| 380 |
SyncOpError(static_cast<uint32_t>(aRv))); |
| 381 |
} else { |
| 382 |
mManager->OnRequestComplete(mRequestId, |
| 383 |
SyncGetTagsResponse(mTags)); |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
private: |
| 388 |
SyncGetTagsArgs mArgs; |
| 389 |
nsTArray<nsString> mTags; |
| 390 |
}; |
| 391 |
|
| 392 |
// --------------------------------------------------------------------------- |
| 393 |
|
| 394 |
class StorageManager::GetAllAction final : public StorageManager::BaseAction |
| 395 |
{ |
| 396 |
public: |
| 397 |
GetAllAction(const nsID& aRequestId, |
| 398 |
StorageManager* aManager) |
| 399 |
: BaseAction(aRequestId, aManager) |
| 400 |
{} |
| 401 |
|
| 402 |
virtual nsresult |
| 403 |
RunSyncOnTarget(const QuotaInfo& aQuotaInfo, |
| 404 |
mozIStorageConnection* aConn) override |
| 405 |
{ |
| 406 |
nsresult rv = db::GetAll(aConn, mRegistrations); |
| 407 |
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "GetAll failed"); |
| 408 |
return rv; |
| 409 |
} |
| 410 |
|
| 411 |
virtual void |
| 412 |
Complete(nsresult aRv) override |
| 413 |
{ |
| 414 |
NS_WARNING_ASSERTION(NS_SUCCEEDED(aRv), "GetAll failed"); |
| 415 |
// We need to remove the reference to the request independently of |
| 416 |
// its result so we can close the context. |
| 417 |
mManager->OnRequestComplete(mRequestId, |
| 418 |
SyncGetAllResponse(mRegistrations)); |
| 419 |
} |
| 420 |
|
| 421 |
private: |
| 422 |
nsTArray<Registration> mRegistrations; |
| 423 |
}; |
| 424 |
|
| 425 |
// --------------------------------------------------------------------------- |
| 426 |
|
| 427 |
class StorageManager::RemoveAction final : public StorageManager::BaseAction |
| 428 |
{ |
| 429 |
public: |
| 430 |
RemoveAction(const nsID& aRequestId, |
| 431 |
StorageManager* aManager, |
| 432 |
const SyncRemoveArgs& aRemoveArgs) |
| 433 |
: BaseAction(aRequestId, aManager) |
| 434 |
, mArgs(aRemoveArgs) |
| 435 |
{} |
| 436 |
|
| 437 |
virtual nsresult |
| 438 |
RunSyncOnTarget(const QuotaInfo& aQuotaInfo, |
| 439 |
mozIStorageConnection* aConn) override |
| 440 |
{ |
| 441 |
nsresult rv = db::Remove(aConn, mArgs, mOrigin); |
| 442 |
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Remove failed"); |
| 443 |
return rv; |
| 444 |
} |
| 445 |
|
| 446 |
virtual void |
| 447 |
Complete(nsresult aRv) override |
| 448 |
{ |
| 449 |
NS_WARNING_ASSERTION(NS_SUCCEEDED(aRv), "Remove failed"); |
| 450 |
// We need to remove the reference to the request independently of |
| 451 |
// its result so we can close the context. |
| 452 |
mManager->OnRequestComplete(mRequestId, |
| 453 |
SyncRemoveResponse(mOrigin)); |
| 454 |
} |
| 455 |
|
| 456 |
private: |
| 457 |
SyncRemoveArgs mArgs; |
| 458 |
nsString mOrigin; |
| 459 |
}; |
| 460 |
|
| 461 |
// --------------------------------------------------------------------------- |
| 462 |
|
| 463 |
class StorageManager::ChangeStateAction final : public StorageManager::BaseAction |
| 464 |
{ |
| 465 |
public: |
| 466 |
ChangeStateAction(const nsID& aRequestId, |
| 467 |
StorageManager* aManager, |
| 468 |
const SyncChangeStateArgs& aChangeStateArgs) |
| 469 |
: BaseAction(aRequestId, aManager) |
| 470 |
, mArgs(aChangeStateArgs) |
| 471 |
{} |
| 472 |
|
| 473 |
virtual nsresult |
| 474 |
RunSyncOnTarget(const QuotaInfo& aQuotaInfo, |
| 475 |
mozIStorageConnection* aConn) override |
| 476 |
{ |
| 477 |
nsresult rv = db::ChangeState(aConn, mArgs, mRegistration); |
| 478 |
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "ChangeState failed"); |
| 479 |
return rv; |
| 480 |
} |
| 481 |
|
| 482 |
virtual void |
| 483 |
Complete(nsresult aRv) override |
| 484 |
{ |
| 485 |
NS_WARNING_ASSERTION(NS_SUCCEEDED(aRv), "ChangeState failed"); |
| 486 |
// We need to remove the reference to the request independently of |
| 487 |
// its result so we can close the context. |
| 488 |
mManager->OnRequestComplete(mRequestId, |
| 489 |
SyncChangeStateResponse(mRegistration)); |
| 490 |
} |
| 491 |
|
| 492 |
private: |
| 493 |
SyncChangeStateArgs mArgs; |
| 494 |
Registration mRegistration; |
| 495 |
}; |
| 496 |
|
| 497 |
// --------------------------------------------------------------------------- |
| 498 |
|
| 499 |
// static |
| 500 |
nsresult |
| 501 |
StorageManager::GetOrCreate(StorageManagerId* aManagerId, |
| 502 |
StorageManager** aManagerOut) |
| 503 |
{ |
| 504 |
AssertIsOnBackgroundThread(); |
| 505 |
return Factory::GetOrCreate(aManagerId, aManagerOut); |
| 506 |
} |
| 507 |
|
| 508 |
// static |
| 509 |
void |
| 510 |
StorageManager::ShutdownAll() |
| 511 |
{ |
| 512 |
mozilla::ipc::AssertIsOnBackgroundThread(); |
| 513 |
|
| 514 |
Factory::ShutdownAll(); |
| 515 |
while (!Factory::IsShutdownAllComplete()) { |
| 516 |
if (!NS_ProcessNextEvent()) { |
| 517 |
NS_WARNING("Something bad happened!"); |
| 518 |
break; |
| 519 |
} |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
// static |
| 524 |
void |
| 525 |
StorageManager::Abort(const nsACString& aOrigin) |
| 526 |
{ |
| 527 |
mozilla::ipc::AssertIsOnBackgroundThread(); |
| 528 |
|
| 529 |
Factory::Abort(aOrigin); |
| 530 |
} |
| 531 |
|
| 532 |
StorageManager::StorageManager(StorageManagerId* aManagerId, |
| 533 |
nsIThread* aIOThread) |
| 534 |
: mManagerId(aManagerId) |
| 535 |
, mIOThread(aIOThread) |
| 536 |
, mContext(nullptr) |
| 537 |
, mShuttingDown(false) |
| 538 |
, mState(Open) |
| 539 |
{ |
| 540 |
MOZ_ASSERT(mManagerId); |
| 541 |
MOZ_ASSERT(mIOThread); |
| 542 |
} |
| 543 |
|
| 544 |
StorageManager::~StorageManager() |
| 545 |
{ |
| 546 |
NS_ASSERT_OWNINGTHREAD(StorageManager); |
| 547 |
MOZ_ASSERT(mState == Closing); |
| 548 |
MOZ_ASSERT(!mContext); |
| 549 |
|
| 550 |
nsCOMPtr<nsIThread> ioThread; |
| 551 |
mIOThread.swap(ioThread); |
| 552 |
MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread( |
| 553 |
NewRunnableMethod(ioThread, &nsIThread::Shutdown))); |
| 554 |
} |
| 555 |
|
| 556 |
void |
| 557 |
StorageManager::Init(StorageManager* aOldManager) |
| 558 |
{ |
| 559 |
NS_ASSERT_OWNINGTHREAD(StorageManager); |
| 560 |
|
| 561 |
RefPtr<ClientContext> oldContext; |
| 562 |
if (aOldManager) { |
| 563 |
oldContext = aOldManager->mContext; |
| 564 |
} |
| 565 |
|
| 566 |
// Create the context immediately. Since there can at most be one |
| 567 |
// ClientContext per StorageManager, this lets us cleanly call |
| 568 |
// Factory::Remove() once the ClientContext goes away. |
| 569 |
RefPtr<ClientAction> setupAction = new SetupAction(); |
| 570 |
RefPtr<ClientContext> ref = |
| 571 |
ClientContext::Create(this, quota::Client::BACKGROUNDSYNC, |
| 572 |
NS_LITERAL_STRING("backgroundsync"), |
| 573 |
mIOThread, setupAction, oldContext); |
| 574 |
mContext = ref; |
| 575 |
} |
| 576 |
|
| 577 |
void |
| 578 |
StorageManager::Abort() |
| 579 |
{ |
| 580 |
NS_ASSERT_OWNINGTHREAD(StorageManager); |
| 581 |
MOZ_ASSERT(mContext); |
| 582 |
|
| 583 |
// Note that we are closing to prevent any new requests from coming in and |
| 584 |
// creating a new ClientContext. We must ensure all Contexts and IO |
| 585 |
// operations are complete before shutdown proceeds. |
| 586 |
NoteClosing(); |
| 587 |
|
| 588 |
// Cancel and only note that we are done after the context is cleaned up. |
| 589 |
RefPtr<ClientContext> context = mContext; |
| 590 |
context->CancelAll(); |
| 591 |
} |
| 592 |
|
| 593 |
void |
| 594 |
StorageManager::Shutdown() |
| 595 |
{ |
| 596 |
NS_ASSERT_OWNINGTHREAD(StorageManager); |
| 597 |
|
| 598 |
// Ignore duplicate attempts to shutdown. This can occur when we start a |
| 599 |
// browser initiated shutdown and then run ~StorageManager() which also |
| 600 |
// calls Shutdown(). |
| 601 |
if (mShuttingDown) { |
| 602 |
return; |
| 603 |
} |
| 604 |
|
| 605 |
mShuttingDown = true; |
| 606 |
|
| 607 |
// Note that we are closing to prevent any new requests from coming in and |
| 608 |
// creating a new ClientContext. We must ensure all Contexts and IO |
| 609 |
// operations are complete before shutdown proceeds. |
| 610 |
NoteClosing(); |
| 611 |
|
| 612 |
// If there is a context, then cancel and only note that we are done after |
| 613 |
// its cleaned up. |
| 614 |
if (mContext) { |
| 615 |
RefPtr<ClientContext> context = mContext; |
| 616 |
context->CancelAll(); |
| 617 |
} |
| 618 |
} |
| 619 |
|
| 620 |
void |
| 621 |
StorageManager::NoteClosing() |
| 622 |
{ |
| 623 |
NS_ASSERT_OWNINGTHREAD(StorageManager); |
| 624 |
|
| 625 |
// This can be called more than once legitimately through different paths. |
| 626 |
mState = Closing; |
| 627 |
} |
| 628 |
|
| 629 |
void |
| 630 |
StorageManager::RemoveClientContext(ClientContext* aContext) |
| 631 |
{ |
| 632 |
NS_ASSERT_OWNINGTHREAD(StorageManager); |
| 633 |
MOZ_ASSERT(mContext); |
| 634 |
MOZ_ASSERT(mContext == aContext); |
| 635 |
|
| 636 |
// Wether the ClientContext destruction was triggered from the StorageManager |
| 637 |
// going idle or the underlying storage being invalidated, we should know we |
| 638 |
// are closing before the ClientContext is destroyed. |
| 639 |
MOZ_ASSERT(mState == Closing); |
| 640 |
|
| 641 |
mContext = nullptr; |
| 642 |
|
| 643 |
// Once the context is gone, we can immediately remove ourself from the |
| 644 |
// Factory list. We don't need to block shutdown by stayin in the list |
| 645 |
// any more. |
| 646 |
Factory::Remove(this); |
| 647 |
} |
| 648 |
|
| 649 |
already_AddRefed<nsIPrincipal> |
| 650 |
StorageManager::GetPrincipal() const |
| 651 |
{ |
| 652 |
MOZ_ASSERT(NS_IsMainThread()); |
| 653 |
|
| 654 |
nsCOMPtr<nsIPrincipal> ref = mManagerId->Principal(); |
| 655 |
return ref.forget(); |
| 656 |
} |
| 657 |
|
| 658 |
void |
| 659 |
StorageManager::MaybeAllowContextToClose() |
| 660 |
{ |
| 661 |
NS_ASSERT_OWNINGTHREAD(StorageManager); |
| 662 |
// If we have an active context, but we have no more pending requests, |
| 663 |
// then let it shut itself down. We must wait for all possible users |
| 664 |
// of state information to complete before doing this. |
| 665 |
RefPtr<ClientContext> context = mContext; |
| 666 |
if (context && mPendingRequests.IsEmpty()) { |
| 667 |
// Mark the StorageManager as invalid so that it won't get used again. |
| 668 |
NoteClosing(); |
| 669 |
|
| 670 |
context->AllowToClose(); |
| 671 |
} |
| 672 |
} |
| 673 |
|
| 674 |
// Common to DOM and internal requests. |
| 675 |
void |
| 676 |
StorageManager::ExecuteRequest(const nsID& aRequestId, ClientAction* aAction) |
| 677 |
{ |
| 678 |
NS_ASSERT_OWNINGTHREAD(StorageManager); |
| 679 |
MOZ_ASSERT(mContext); |
| 680 |
MOZ_ASSERT(aAction); |
| 681 |
|
| 682 |
mPendingRequests.AppendElement(aRequestId); |
| 683 |
|
| 684 |
if (NS_WARN_IF(mState == Closing)) { |
| 685 |
OnRequestComplete(aRequestId, |
| 686 |
SyncOpError(static_cast<uint32_t>(NS_ERROR_ABORT))); |
| 687 |
return; |
| 688 |
} |
| 689 |
|
| 690 |
RefPtr<ClientContext> context = mContext; |
| 691 |
MOZ_ASSERT(!context->IsCanceled()); |
| 692 |
|
| 693 |
context->Dispatch(aAction); |
| 694 |
} |
| 695 |
|
| 696 |
// DOM request. |
| 697 |
void |
| 698 |
StorageManager::ExecuteRequest(const nsID& aRequestId, const SyncOp& aOp) |
| 699 |
{ |
| 700 |
RefPtr<ClientAction> action; |
| 701 |
|
| 702 |
switch(aOp.mArgs().type()) { |
| 703 |
case SyncOpArgs::TSyncRegisterArgs: |
| 704 |
{ |
| 705 |
action = new RegisterAction(aRequestId, this, |
| 706 |
aOp.mArgs().get_SyncRegisterArgs()); |
| 707 |
break; |
| 708 |
} |
| 709 |
case SyncOpArgs::TSyncGetTagsArgs: |
| 710 |
{ |
| 711 |
action = new GetTagsAction(aRequestId, this, |
| 712 |
aOp.mArgs().get_SyncGetTagsArgs()); |
| 713 |
break; |
| 714 |
} |
| 715 |
default: |
| 716 |
{ |
| 717 |
MOZ_CRASH("Unknown BackgroundSync request"); |
| 718 |
} |
| 719 |
} |
| 720 |
|
| 721 |
ExecuteRequest(aRequestId, action); |
| 722 |
} |
| 723 |
|
| 724 |
// Internal request. |
| 725 |
void |
| 726 |
StorageManager::ExecuteRequest(const nsID& aRequestId, const SyncInternalOp& aOp) |
| 727 |
{ |
| 728 |
RefPtr<ClientAction> action; |
| 729 |
|
| 730 |
switch(aOp.mArgs().type()) { |
| 731 |
case SyncInternalOpArgs::TSyncGetAllArgs: |
| 732 |
{ |
| 733 |
action = new GetAllAction(aRequestId, this); |
| 734 |
break; |
| 735 |
} |
| 736 |
case SyncInternalOpArgs::TSyncChangeStateArgs: |
| 737 |
{ |
| 738 |
action = new ChangeStateAction(aRequestId, this, |
| 739 |
aOp.mArgs().get_SyncChangeStateArgs()); |
| 740 |
break; |
| 741 |
} |
| 742 |
case SyncInternalOpArgs::TSyncRemoveArgs: |
| 743 |
{ |
| 744 |
action = new RemoveAction(aRequestId, this, |
| 745 |
aOp.mArgs().get_SyncRemoveArgs()); |
| 746 |
break; |
| 747 |
} |
| 748 |
default: |
| 749 |
{ |
| 750 |
MOZ_CRASH("Unknown BackgroundSync internal request"); |
| 751 |
} |
| 752 |
} |
| 753 |
|
| 754 |
ExecuteRequest(aRequestId, action); |
| 755 |
} |
| 756 |
|
| 757 |
// Common to DOM and internal responses. |
| 758 |
void |
| 759 |
StorageManager::OnRequestComplete(const nsID& aRequestId) |
| 760 |
{ |
| 761 |
NS_ASSERT_OWNINGTHREAD(StorageManager); |
| 762 |
MOZ_ASSERT(mPendingRequests.Contains(aRequestId)); |
| 763 |
|
| 764 |
mPendingRequests.RemoveElement(aRequestId); |
| 765 |
|
| 766 |
MaybeAllowContextToClose(); |
| 767 |
} |
| 768 |
|
| 769 |
template<typename T> |
| 770 |
void |
| 771 |
StorageManager::OnRequestComplete(const nsID& aRequestId, const T& aResponse) |
| 772 |
{ |
| 773 |
// XXX Notify listener |
| 774 |
|
| 775 |
OnRequestComplete(aRequestId); |
| 776 |
} |
| 777 |
|
| 778 |
} // namespace backgroundsync |
| 779 |
} // namespace dom |
| 780 |
} // namespace mozilla |