Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "DOMStorageIPC.h" |
michael@0 | 7 | |
michael@0 | 8 | #include "DOMStorageManager.h" |
michael@0 | 9 | |
michael@0 | 10 | #include "mozilla/dom/ContentChild.h" |
michael@0 | 11 | #include "mozilla/dom/ContentParent.h" |
michael@0 | 12 | #include "mozilla/unused.h" |
michael@0 | 13 | #include "nsIDiskSpaceWatcher.h" |
michael@0 | 14 | #include "nsThreadUtils.h" |
michael@0 | 15 | |
michael@0 | 16 | namespace mozilla { |
michael@0 | 17 | namespace dom { |
michael@0 | 18 | |
michael@0 | 19 | // ---------------------------------------------------------------------------- |
michael@0 | 20 | // Child |
michael@0 | 21 | // ---------------------------------------------------------------------------- |
michael@0 | 22 | |
michael@0 | 23 | NS_IMPL_ADDREF(DOMStorageDBChild) |
michael@0 | 24 | |
michael@0 | 25 | NS_IMETHODIMP_(MozExternalRefCountType) DOMStorageDBChild::Release(void) |
michael@0 | 26 | { |
michael@0 | 27 | NS_PRECONDITION(0 != mRefCnt, "dup release"); |
michael@0 | 28 | nsrefcnt count = --mRefCnt; |
michael@0 | 29 | NS_LOG_RELEASE(this, count, "DOMStorageDBChild"); |
michael@0 | 30 | if (count == 1 && mIPCOpen) { |
michael@0 | 31 | Send__delete__(this); |
michael@0 | 32 | return 0; |
michael@0 | 33 | } |
michael@0 | 34 | if (count == 0) { |
michael@0 | 35 | mRefCnt = 1; |
michael@0 | 36 | delete this; |
michael@0 | 37 | return 0; |
michael@0 | 38 | } |
michael@0 | 39 | return count; |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | void |
michael@0 | 43 | DOMStorageDBChild::AddIPDLReference() |
michael@0 | 44 | { |
michael@0 | 45 | NS_ABORT_IF_FALSE(!mIPCOpen, "Attempting to retain multiple IPDL references"); |
michael@0 | 46 | mIPCOpen = true; |
michael@0 | 47 | AddRef(); |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | void |
michael@0 | 51 | DOMStorageDBChild::ReleaseIPDLReference() |
michael@0 | 52 | { |
michael@0 | 53 | NS_ABORT_IF_FALSE(mIPCOpen, "Attempting to release non-existent IPDL reference"); |
michael@0 | 54 | mIPCOpen = false; |
michael@0 | 55 | Release(); |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | DOMStorageDBChild::DOMStorageDBChild(DOMLocalStorageManager* aManager) |
michael@0 | 59 | : mManager(aManager) |
michael@0 | 60 | , mStatus(NS_OK) |
michael@0 | 61 | , mIPCOpen(false) |
michael@0 | 62 | { |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | DOMStorageDBChild::~DOMStorageDBChild() |
michael@0 | 66 | { |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | nsTHashtable<nsCStringHashKey>& |
michael@0 | 70 | DOMStorageDBChild::ScopesHavingData() |
michael@0 | 71 | { |
michael@0 | 72 | if (!mScopesHavingData) { |
michael@0 | 73 | mScopesHavingData = new nsTHashtable<nsCStringHashKey>; |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | return *mScopesHavingData; |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | nsresult |
michael@0 | 80 | DOMStorageDBChild::Init() |
michael@0 | 81 | { |
michael@0 | 82 | ContentChild* child = ContentChild::GetSingleton(); |
michael@0 | 83 | AddIPDLReference(); |
michael@0 | 84 | child->SendPStorageConstructor(this); |
michael@0 | 85 | return NS_OK; |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | nsresult |
michael@0 | 89 | DOMStorageDBChild::Shutdown() |
michael@0 | 90 | { |
michael@0 | 91 | // There is nothing to do here, IPC will release automatically and |
michael@0 | 92 | // the actual thread running on the parent process will also stop |
michael@0 | 93 | // automatically in profile-before-change topic observer. |
michael@0 | 94 | return NS_OK; |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | void |
michael@0 | 98 | DOMStorageDBChild::AsyncPreload(DOMStorageCacheBridge* aCache, bool aPriority) |
michael@0 | 99 | { |
michael@0 | 100 | if (mIPCOpen) { |
michael@0 | 101 | // Adding ref to cache for the time of preload. This ensures a reference to |
michael@0 | 102 | // to the cache and that all keys will load into this cache object. |
michael@0 | 103 | mLoadingCaches.PutEntry(aCache); |
michael@0 | 104 | SendAsyncPreload(aCache->Scope(), aPriority); |
michael@0 | 105 | } else { |
michael@0 | 106 | // No IPC, no love. But the LoadDone call is expected. |
michael@0 | 107 | aCache->LoadDone(NS_ERROR_UNEXPECTED); |
michael@0 | 108 | } |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | void |
michael@0 | 112 | DOMStorageDBChild::AsyncGetUsage(DOMStorageUsageBridge* aUsage) |
michael@0 | 113 | { |
michael@0 | 114 | if (mIPCOpen) { |
michael@0 | 115 | SendAsyncGetUsage(aUsage->Scope()); |
michael@0 | 116 | } |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | void |
michael@0 | 120 | DOMStorageDBChild::SyncPreload(DOMStorageCacheBridge* aCache, bool aForceSync) |
michael@0 | 121 | { |
michael@0 | 122 | if (NS_FAILED(mStatus)) { |
michael@0 | 123 | aCache->LoadDone(mStatus); |
michael@0 | 124 | return; |
michael@0 | 125 | } |
michael@0 | 126 | |
michael@0 | 127 | if (!mIPCOpen) { |
michael@0 | 128 | aCache->LoadDone(NS_ERROR_UNEXPECTED); |
michael@0 | 129 | return; |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | // There is no way to put the child process to a wait state to receive all |
michael@0 | 133 | // incoming async responses from the parent, hence we have to do a sync preload |
michael@0 | 134 | // instead. We are smart though, we only demand keys that are left to load in |
michael@0 | 135 | // case the async preload has already loaded some keys. |
michael@0 | 136 | InfallibleTArray<nsString> keys, values; |
michael@0 | 137 | nsresult rv; |
michael@0 | 138 | SendPreload(aCache->Scope(), aCache->LoadedCount(), &keys, &values, &rv); |
michael@0 | 139 | |
michael@0 | 140 | for (uint32_t i = 0; i < keys.Length(); ++i) { |
michael@0 | 141 | aCache->LoadItem(keys[i], values[i]); |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | aCache->LoadDone(rv); |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | nsresult |
michael@0 | 148 | DOMStorageDBChild::AsyncAddItem(DOMStorageCacheBridge* aCache, |
michael@0 | 149 | const nsAString& aKey, |
michael@0 | 150 | const nsAString& aValue) |
michael@0 | 151 | { |
michael@0 | 152 | if (NS_FAILED(mStatus) || !mIPCOpen) { |
michael@0 | 153 | return mStatus; |
michael@0 | 154 | } |
michael@0 | 155 | |
michael@0 | 156 | SendAsyncAddItem(aCache->Scope(), nsString(aKey), nsString(aValue)); |
michael@0 | 157 | ScopesHavingData().PutEntry(aCache->Scope()); |
michael@0 | 158 | return NS_OK; |
michael@0 | 159 | } |
michael@0 | 160 | |
michael@0 | 161 | nsresult |
michael@0 | 162 | DOMStorageDBChild::AsyncUpdateItem(DOMStorageCacheBridge* aCache, |
michael@0 | 163 | const nsAString& aKey, |
michael@0 | 164 | const nsAString& aValue) |
michael@0 | 165 | { |
michael@0 | 166 | if (NS_FAILED(mStatus) || !mIPCOpen) { |
michael@0 | 167 | return mStatus; |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | SendAsyncUpdateItem(aCache->Scope(), nsString(aKey), nsString(aValue)); |
michael@0 | 171 | ScopesHavingData().PutEntry(aCache->Scope()); |
michael@0 | 172 | return NS_OK; |
michael@0 | 173 | } |
michael@0 | 174 | |
michael@0 | 175 | nsresult |
michael@0 | 176 | DOMStorageDBChild::AsyncRemoveItem(DOMStorageCacheBridge* aCache, |
michael@0 | 177 | const nsAString& aKey) |
michael@0 | 178 | { |
michael@0 | 179 | if (NS_FAILED(mStatus) || !mIPCOpen) { |
michael@0 | 180 | return mStatus; |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | SendAsyncRemoveItem(aCache->Scope(), nsString(aKey)); |
michael@0 | 184 | return NS_OK; |
michael@0 | 185 | } |
michael@0 | 186 | |
michael@0 | 187 | nsresult |
michael@0 | 188 | DOMStorageDBChild::AsyncClear(DOMStorageCacheBridge* aCache) |
michael@0 | 189 | { |
michael@0 | 190 | if (NS_FAILED(mStatus) || !mIPCOpen) { |
michael@0 | 191 | return mStatus; |
michael@0 | 192 | } |
michael@0 | 193 | |
michael@0 | 194 | SendAsyncClear(aCache->Scope()); |
michael@0 | 195 | ScopesHavingData().RemoveEntry(aCache->Scope()); |
michael@0 | 196 | return NS_OK; |
michael@0 | 197 | } |
michael@0 | 198 | |
michael@0 | 199 | bool |
michael@0 | 200 | DOMStorageDBChild::ShouldPreloadScope(const nsACString& aScope) |
michael@0 | 201 | { |
michael@0 | 202 | // Return true if we didn't receive the aScope list yet. |
michael@0 | 203 | // I tend to rather preserve a bit of early-after-start performance |
michael@0 | 204 | // then a bit of memory here. |
michael@0 | 205 | return !mScopesHavingData || mScopesHavingData->Contains(aScope); |
michael@0 | 206 | } |
michael@0 | 207 | |
michael@0 | 208 | bool |
michael@0 | 209 | DOMStorageDBChild::RecvObserve(const nsCString& aTopic, |
michael@0 | 210 | const nsCString& aScopePrefix) |
michael@0 | 211 | { |
michael@0 | 212 | DOMStorageObserver::Self()->Notify(aTopic.get(), aScopePrefix); |
michael@0 | 213 | return true; |
michael@0 | 214 | } |
michael@0 | 215 | |
michael@0 | 216 | bool |
michael@0 | 217 | DOMStorageDBChild::RecvScopesHavingData(const InfallibleTArray<nsCString>& aScopes) |
michael@0 | 218 | { |
michael@0 | 219 | for (uint32_t i = 0; i < aScopes.Length(); ++i) { |
michael@0 | 220 | ScopesHavingData().PutEntry(aScopes[i]); |
michael@0 | 221 | } |
michael@0 | 222 | |
michael@0 | 223 | return true; |
michael@0 | 224 | } |
michael@0 | 225 | |
michael@0 | 226 | bool |
michael@0 | 227 | DOMStorageDBChild::RecvLoadItem(const nsCString& aScope, |
michael@0 | 228 | const nsString& aKey, |
michael@0 | 229 | const nsString& aValue) |
michael@0 | 230 | { |
michael@0 | 231 | DOMStorageCache* aCache = mManager->GetCache(aScope); |
michael@0 | 232 | if (aCache) { |
michael@0 | 233 | aCache->LoadItem(aKey, aValue); |
michael@0 | 234 | } |
michael@0 | 235 | |
michael@0 | 236 | return true; |
michael@0 | 237 | } |
michael@0 | 238 | |
michael@0 | 239 | bool |
michael@0 | 240 | DOMStorageDBChild::RecvLoadDone(const nsCString& aScope, const nsresult& aRv) |
michael@0 | 241 | { |
michael@0 | 242 | DOMStorageCache* aCache = mManager->GetCache(aScope); |
michael@0 | 243 | if (aCache) { |
michael@0 | 244 | aCache->LoadDone(aRv); |
michael@0 | 245 | |
michael@0 | 246 | // Just drop reference to this cache now since the load is done. |
michael@0 | 247 | mLoadingCaches.RemoveEntry(static_cast<DOMStorageCacheBridge*>(aCache)); |
michael@0 | 248 | } |
michael@0 | 249 | |
michael@0 | 250 | return true; |
michael@0 | 251 | } |
michael@0 | 252 | |
michael@0 | 253 | bool |
michael@0 | 254 | DOMStorageDBChild::RecvLoadUsage(const nsCString& aScope, const int64_t& aUsage) |
michael@0 | 255 | { |
michael@0 | 256 | nsRefPtr<DOMStorageUsageBridge> scopeUsage = mManager->GetScopeUsage(aScope); |
michael@0 | 257 | scopeUsage->LoadUsage(aUsage); |
michael@0 | 258 | return true; |
michael@0 | 259 | } |
michael@0 | 260 | |
michael@0 | 261 | bool |
michael@0 | 262 | DOMStorageDBChild::RecvError(const nsresult& aRv) |
michael@0 | 263 | { |
michael@0 | 264 | mStatus = aRv; |
michael@0 | 265 | return true; |
michael@0 | 266 | } |
michael@0 | 267 | |
michael@0 | 268 | // ---------------------------------------------------------------------------- |
michael@0 | 269 | // Parent |
michael@0 | 270 | // ---------------------------------------------------------------------------- |
michael@0 | 271 | |
michael@0 | 272 | NS_IMPL_ADDREF(DOMStorageDBParent) |
michael@0 | 273 | NS_IMPL_RELEASE(DOMStorageDBParent) |
michael@0 | 274 | |
michael@0 | 275 | void |
michael@0 | 276 | DOMStorageDBParent::AddIPDLReference() |
michael@0 | 277 | { |
michael@0 | 278 | NS_ABORT_IF_FALSE(!mIPCOpen, "Attempting to retain multiple IPDL references"); |
michael@0 | 279 | mIPCOpen = true; |
michael@0 | 280 | AddRef(); |
michael@0 | 281 | } |
michael@0 | 282 | |
michael@0 | 283 | void |
michael@0 | 284 | DOMStorageDBParent::ReleaseIPDLReference() |
michael@0 | 285 | { |
michael@0 | 286 | NS_ABORT_IF_FALSE(mIPCOpen, "Attempting to release non-existent IPDL reference"); |
michael@0 | 287 | mIPCOpen = false; |
michael@0 | 288 | Release(); |
michael@0 | 289 | } |
michael@0 | 290 | |
michael@0 | 291 | namespace { // anon |
michael@0 | 292 | |
michael@0 | 293 | class SendInitialChildDataRunnable : public nsRunnable |
michael@0 | 294 | { |
michael@0 | 295 | public: |
michael@0 | 296 | SendInitialChildDataRunnable(DOMStorageDBParent* aParent) |
michael@0 | 297 | : mParent(aParent) |
michael@0 | 298 | {} |
michael@0 | 299 | |
michael@0 | 300 | private: |
michael@0 | 301 | NS_IMETHOD Run() |
michael@0 | 302 | { |
michael@0 | 303 | if (!mParent->IPCOpen()) { |
michael@0 | 304 | return NS_OK; |
michael@0 | 305 | } |
michael@0 | 306 | |
michael@0 | 307 | DOMStorageDBBridge* db = DOMStorageCache::GetDatabase(); |
michael@0 | 308 | if (db) { |
michael@0 | 309 | InfallibleTArray<nsCString> scopes; |
michael@0 | 310 | db->GetScopesHavingData(&scopes); |
michael@0 | 311 | mozilla::unused << mParent->SendScopesHavingData(scopes); |
michael@0 | 312 | } |
michael@0 | 313 | |
michael@0 | 314 | // We need to check if the device is in a low disk space situation, so |
michael@0 | 315 | // we can forbid in that case any write in localStorage. |
michael@0 | 316 | nsCOMPtr<nsIDiskSpaceWatcher> diskSpaceWatcher = |
michael@0 | 317 | do_GetService("@mozilla.org/toolkit/disk-space-watcher;1"); |
michael@0 | 318 | if (!diskSpaceWatcher) { |
michael@0 | 319 | NS_WARNING("Could not get disk information from DiskSpaceWatcher"); |
michael@0 | 320 | return NS_OK; |
michael@0 | 321 | } |
michael@0 | 322 | bool lowDiskSpace = false; |
michael@0 | 323 | diskSpaceWatcher->GetIsDiskFull(&lowDiskSpace); |
michael@0 | 324 | if (lowDiskSpace) { |
michael@0 | 325 | mozilla::unused << mParent->SendObserve( |
michael@0 | 326 | nsDependentCString("low-disk-space"), EmptyCString()); |
michael@0 | 327 | } |
michael@0 | 328 | |
michael@0 | 329 | return NS_OK; |
michael@0 | 330 | } |
michael@0 | 331 | |
michael@0 | 332 | nsRefPtr<DOMStorageDBParent> mParent; |
michael@0 | 333 | }; |
michael@0 | 334 | |
michael@0 | 335 | } // anon |
michael@0 | 336 | |
michael@0 | 337 | DOMStorageDBParent::DOMStorageDBParent() |
michael@0 | 338 | : mIPCOpen(false) |
michael@0 | 339 | { |
michael@0 | 340 | DOMStorageObserver* observer = DOMStorageObserver::Self(); |
michael@0 | 341 | if (observer) { |
michael@0 | 342 | observer->AddSink(this); |
michael@0 | 343 | } |
michael@0 | 344 | |
michael@0 | 345 | // We are always open by IPC only |
michael@0 | 346 | AddIPDLReference(); |
michael@0 | 347 | |
michael@0 | 348 | // Cannot send directly from here since the channel |
michael@0 | 349 | // is not completely built at this moment. |
michael@0 | 350 | nsRefPtr<SendInitialChildDataRunnable> r = |
michael@0 | 351 | new SendInitialChildDataRunnable(this); |
michael@0 | 352 | NS_DispatchToCurrentThread(r); |
michael@0 | 353 | } |
michael@0 | 354 | |
michael@0 | 355 | DOMStorageDBParent::~DOMStorageDBParent() |
michael@0 | 356 | { |
michael@0 | 357 | DOMStorageObserver* observer = DOMStorageObserver::Self(); |
michael@0 | 358 | if (observer) { |
michael@0 | 359 | observer->RemoveSink(this); |
michael@0 | 360 | } |
michael@0 | 361 | } |
michael@0 | 362 | |
michael@0 | 363 | mozilla::ipc::IProtocol* |
michael@0 | 364 | DOMStorageDBParent::CloneProtocol(Channel* aChannel, |
michael@0 | 365 | mozilla::ipc::ProtocolCloneContext* aCtx) |
michael@0 | 366 | { |
michael@0 | 367 | ContentParent* contentParent = aCtx->GetContentParent(); |
michael@0 | 368 | nsAutoPtr<PStorageParent> actor(contentParent->AllocPStorageParent()); |
michael@0 | 369 | if (!actor || !contentParent->RecvPStorageConstructor(actor)) { |
michael@0 | 370 | return nullptr; |
michael@0 | 371 | } |
michael@0 | 372 | return actor.forget(); |
michael@0 | 373 | } |
michael@0 | 374 | |
michael@0 | 375 | DOMStorageDBParent::CacheParentBridge* |
michael@0 | 376 | DOMStorageDBParent::NewCache(const nsACString& aScope) |
michael@0 | 377 | { |
michael@0 | 378 | return new CacheParentBridge(this, aScope); |
michael@0 | 379 | } |
michael@0 | 380 | |
michael@0 | 381 | bool |
michael@0 | 382 | DOMStorageDBParent::RecvAsyncPreload(const nsCString& aScope, const bool& aPriority) |
michael@0 | 383 | { |
michael@0 | 384 | DOMStorageDBBridge* db = DOMStorageCache::StartDatabase(); |
michael@0 | 385 | if (!db) { |
michael@0 | 386 | return false; |
michael@0 | 387 | } |
michael@0 | 388 | |
michael@0 | 389 | db->AsyncPreload(NewCache(aScope), aPriority); |
michael@0 | 390 | return true; |
michael@0 | 391 | } |
michael@0 | 392 | |
michael@0 | 393 | bool |
michael@0 | 394 | DOMStorageDBParent::RecvAsyncGetUsage(const nsCString& aScope) |
michael@0 | 395 | { |
michael@0 | 396 | DOMStorageDBBridge* db = DOMStorageCache::StartDatabase(); |
michael@0 | 397 | if (!db) { |
michael@0 | 398 | return false; |
michael@0 | 399 | } |
michael@0 | 400 | |
michael@0 | 401 | // The object releases it self in LoadUsage method |
michael@0 | 402 | nsRefPtr<UsageParentBridge> usage = new UsageParentBridge(this, aScope); |
michael@0 | 403 | db->AsyncGetUsage(usage); |
michael@0 | 404 | return true; |
michael@0 | 405 | } |
michael@0 | 406 | |
michael@0 | 407 | namespace { // anon |
michael@0 | 408 | |
michael@0 | 409 | // We need another implementation of DOMStorageCacheBridge to do |
michael@0 | 410 | // synchronous IPC preload. This class just receives Load* notifications |
michael@0 | 411 | // and fills the returning arguments of RecvPreload with the database |
michael@0 | 412 | // values for us. |
michael@0 | 413 | class SyncLoadCacheHelper : public DOMStorageCacheBridge |
michael@0 | 414 | { |
michael@0 | 415 | public: |
michael@0 | 416 | SyncLoadCacheHelper(const nsCString& aScope, |
michael@0 | 417 | uint32_t aAlreadyLoadedCount, |
michael@0 | 418 | InfallibleTArray<nsString>* aKeys, |
michael@0 | 419 | InfallibleTArray<nsString>* aValues, |
michael@0 | 420 | nsresult* rv) |
michael@0 | 421 | : mMonitor("DOM Storage SyncLoad IPC") |
michael@0 | 422 | , mScope(aScope) |
michael@0 | 423 | , mKeys(aKeys) |
michael@0 | 424 | , mValues(aValues) |
michael@0 | 425 | , mRv(rv) |
michael@0 | 426 | , mLoaded(false) |
michael@0 | 427 | , mLoadedCount(aAlreadyLoadedCount) |
michael@0 | 428 | { |
michael@0 | 429 | // Precaution |
michael@0 | 430 | *mRv = NS_ERROR_UNEXPECTED; |
michael@0 | 431 | } |
michael@0 | 432 | |
michael@0 | 433 | virtual const nsCString& Scope() const { return mScope; } |
michael@0 | 434 | virtual bool Loaded() { return mLoaded; } |
michael@0 | 435 | virtual uint32_t LoadedCount() { return mLoadedCount; } |
michael@0 | 436 | virtual bool LoadItem(const nsAString& aKey, const nsString& aValue) |
michael@0 | 437 | { |
michael@0 | 438 | // Called on the aCache background thread |
michael@0 | 439 | if (mLoaded) { |
michael@0 | 440 | return false; |
michael@0 | 441 | } |
michael@0 | 442 | |
michael@0 | 443 | ++mLoadedCount; |
michael@0 | 444 | mKeys->AppendElement(aKey); |
michael@0 | 445 | mValues->AppendElement(aValue); |
michael@0 | 446 | return true; |
michael@0 | 447 | } |
michael@0 | 448 | |
michael@0 | 449 | virtual void LoadDone(nsresult aRv) |
michael@0 | 450 | { |
michael@0 | 451 | // Called on the aCache background thread |
michael@0 | 452 | MonitorAutoLock monitor(mMonitor); |
michael@0 | 453 | mLoaded = true; |
michael@0 | 454 | *mRv = aRv; |
michael@0 | 455 | monitor.Notify(); |
michael@0 | 456 | } |
michael@0 | 457 | |
michael@0 | 458 | virtual void LoadWait() |
michael@0 | 459 | { |
michael@0 | 460 | // Called on the main thread, exits after LoadDone() call |
michael@0 | 461 | MonitorAutoLock monitor(mMonitor); |
michael@0 | 462 | while (!mLoaded) { |
michael@0 | 463 | monitor.Wait(); |
michael@0 | 464 | } |
michael@0 | 465 | } |
michael@0 | 466 | |
michael@0 | 467 | private: |
michael@0 | 468 | Monitor mMonitor; |
michael@0 | 469 | nsCString mScope; |
michael@0 | 470 | InfallibleTArray<nsString>* mKeys; |
michael@0 | 471 | InfallibleTArray<nsString>* mValues; |
michael@0 | 472 | nsresult* mRv; |
michael@0 | 473 | bool mLoaded; |
michael@0 | 474 | uint32_t mLoadedCount; |
michael@0 | 475 | }; |
michael@0 | 476 | |
michael@0 | 477 | } // anon |
michael@0 | 478 | |
michael@0 | 479 | bool |
michael@0 | 480 | DOMStorageDBParent::RecvPreload(const nsCString& aScope, |
michael@0 | 481 | const uint32_t& aAlreadyLoadedCount, |
michael@0 | 482 | InfallibleTArray<nsString>* aKeys, |
michael@0 | 483 | InfallibleTArray<nsString>* aValues, |
michael@0 | 484 | nsresult* aRv) |
michael@0 | 485 | { |
michael@0 | 486 | DOMStorageDBBridge* db = DOMStorageCache::StartDatabase(); |
michael@0 | 487 | if (!db) { |
michael@0 | 488 | return false; |
michael@0 | 489 | } |
michael@0 | 490 | |
michael@0 | 491 | nsRefPtr<SyncLoadCacheHelper> cache( |
michael@0 | 492 | new SyncLoadCacheHelper(aScope, aAlreadyLoadedCount, aKeys, aValues, aRv)); |
michael@0 | 493 | |
michael@0 | 494 | db->SyncPreload(cache, true); |
michael@0 | 495 | return true; |
michael@0 | 496 | } |
michael@0 | 497 | |
michael@0 | 498 | bool |
michael@0 | 499 | DOMStorageDBParent::RecvAsyncAddItem(const nsCString& aScope, |
michael@0 | 500 | const nsString& aKey, |
michael@0 | 501 | const nsString& aValue) |
michael@0 | 502 | { |
michael@0 | 503 | DOMStorageDBBridge* db = DOMStorageCache::StartDatabase(); |
michael@0 | 504 | if (!db) { |
michael@0 | 505 | return false; |
michael@0 | 506 | } |
michael@0 | 507 | |
michael@0 | 508 | nsresult rv = db->AsyncAddItem(NewCache(aScope), aKey, aValue); |
michael@0 | 509 | if (NS_FAILED(rv) && mIPCOpen) { |
michael@0 | 510 | mozilla::unused << SendError(rv); |
michael@0 | 511 | } |
michael@0 | 512 | |
michael@0 | 513 | return true; |
michael@0 | 514 | } |
michael@0 | 515 | |
michael@0 | 516 | bool |
michael@0 | 517 | DOMStorageDBParent::RecvAsyncUpdateItem(const nsCString& aScope, |
michael@0 | 518 | const nsString& aKey, |
michael@0 | 519 | const nsString& aValue) |
michael@0 | 520 | { |
michael@0 | 521 | DOMStorageDBBridge* db = DOMStorageCache::StartDatabase(); |
michael@0 | 522 | if (!db) { |
michael@0 | 523 | return false; |
michael@0 | 524 | } |
michael@0 | 525 | |
michael@0 | 526 | nsresult rv = db->AsyncUpdateItem(NewCache(aScope), aKey, aValue); |
michael@0 | 527 | if (NS_FAILED(rv) && mIPCOpen) { |
michael@0 | 528 | mozilla::unused << SendError(rv); |
michael@0 | 529 | } |
michael@0 | 530 | |
michael@0 | 531 | return true; |
michael@0 | 532 | } |
michael@0 | 533 | |
michael@0 | 534 | bool |
michael@0 | 535 | DOMStorageDBParent::RecvAsyncRemoveItem(const nsCString& aScope, |
michael@0 | 536 | const nsString& aKey) |
michael@0 | 537 | { |
michael@0 | 538 | DOMStorageDBBridge* db = DOMStorageCache::StartDatabase(); |
michael@0 | 539 | if (!db) { |
michael@0 | 540 | return false; |
michael@0 | 541 | } |
michael@0 | 542 | |
michael@0 | 543 | nsresult rv = db->AsyncRemoveItem(NewCache(aScope), aKey); |
michael@0 | 544 | if (NS_FAILED(rv) && mIPCOpen) { |
michael@0 | 545 | mozilla::unused << SendError(rv); |
michael@0 | 546 | } |
michael@0 | 547 | |
michael@0 | 548 | return true; |
michael@0 | 549 | } |
michael@0 | 550 | |
michael@0 | 551 | bool |
michael@0 | 552 | DOMStorageDBParent::RecvAsyncClear(const nsCString& aScope) |
michael@0 | 553 | { |
michael@0 | 554 | DOMStorageDBBridge* db = DOMStorageCache::StartDatabase(); |
michael@0 | 555 | if (!db) { |
michael@0 | 556 | return false; |
michael@0 | 557 | } |
michael@0 | 558 | |
michael@0 | 559 | nsresult rv = db->AsyncClear(NewCache(aScope)); |
michael@0 | 560 | if (NS_FAILED(rv) && mIPCOpen) { |
michael@0 | 561 | mozilla::unused << SendError(rv); |
michael@0 | 562 | } |
michael@0 | 563 | |
michael@0 | 564 | return true; |
michael@0 | 565 | } |
michael@0 | 566 | |
michael@0 | 567 | bool |
michael@0 | 568 | DOMStorageDBParent::RecvAsyncFlush() |
michael@0 | 569 | { |
michael@0 | 570 | DOMStorageDBBridge* db = DOMStorageCache::GetDatabase(); |
michael@0 | 571 | if (!db) { |
michael@0 | 572 | return false; |
michael@0 | 573 | } |
michael@0 | 574 | |
michael@0 | 575 | db->AsyncFlush(); |
michael@0 | 576 | return true; |
michael@0 | 577 | } |
michael@0 | 578 | |
michael@0 | 579 | // DOMStorageObserverSink |
michael@0 | 580 | |
michael@0 | 581 | nsresult |
michael@0 | 582 | DOMStorageDBParent::Observe(const char* aTopic, |
michael@0 | 583 | const nsACString& aScopePrefix) |
michael@0 | 584 | { |
michael@0 | 585 | if (mIPCOpen) { |
michael@0 | 586 | mozilla::unused << SendObserve(nsDependentCString(aTopic), |
michael@0 | 587 | nsCString(aScopePrefix)); |
michael@0 | 588 | } |
michael@0 | 589 | |
michael@0 | 590 | return NS_OK; |
michael@0 | 591 | } |
michael@0 | 592 | |
michael@0 | 593 | namespace { // anon |
michael@0 | 594 | |
michael@0 | 595 | // Results must be sent back on the main thread |
michael@0 | 596 | class LoadRunnable : public nsRunnable |
michael@0 | 597 | { |
michael@0 | 598 | public: |
michael@0 | 599 | enum TaskType { |
michael@0 | 600 | loadItem, |
michael@0 | 601 | loadDone |
michael@0 | 602 | }; |
michael@0 | 603 | |
michael@0 | 604 | LoadRunnable(DOMStorageDBParent* aParent, |
michael@0 | 605 | TaskType aType, |
michael@0 | 606 | const nsACString& aScope, |
michael@0 | 607 | const nsAString& aKey = EmptyString(), |
michael@0 | 608 | const nsAString& aValue = EmptyString()) |
michael@0 | 609 | : mParent(aParent) |
michael@0 | 610 | , mType(aType) |
michael@0 | 611 | , mScope(aScope) |
michael@0 | 612 | , mKey(aKey) |
michael@0 | 613 | , mValue(aValue) |
michael@0 | 614 | { } |
michael@0 | 615 | |
michael@0 | 616 | LoadRunnable(DOMStorageDBParent* aParent, |
michael@0 | 617 | TaskType aType, |
michael@0 | 618 | const nsACString& aScope, |
michael@0 | 619 | nsresult aRv) |
michael@0 | 620 | : mParent(aParent) |
michael@0 | 621 | , mType(aType) |
michael@0 | 622 | , mScope(aScope) |
michael@0 | 623 | , mRv(aRv) |
michael@0 | 624 | { } |
michael@0 | 625 | |
michael@0 | 626 | private: |
michael@0 | 627 | nsRefPtr<DOMStorageDBParent> mParent; |
michael@0 | 628 | TaskType mType; |
michael@0 | 629 | nsCString mScope; |
michael@0 | 630 | nsString mKey; |
michael@0 | 631 | nsString mValue; |
michael@0 | 632 | nsresult mRv; |
michael@0 | 633 | |
michael@0 | 634 | NS_IMETHOD Run() |
michael@0 | 635 | { |
michael@0 | 636 | if (!mParent->IPCOpen()) { |
michael@0 | 637 | return NS_OK; |
michael@0 | 638 | } |
michael@0 | 639 | |
michael@0 | 640 | switch (mType) |
michael@0 | 641 | { |
michael@0 | 642 | case loadItem: |
michael@0 | 643 | mozilla::unused << mParent->SendLoadItem(mScope, mKey, mValue); |
michael@0 | 644 | break; |
michael@0 | 645 | case loadDone: |
michael@0 | 646 | mozilla::unused << mParent->SendLoadDone(mScope, mRv); |
michael@0 | 647 | break; |
michael@0 | 648 | } |
michael@0 | 649 | |
michael@0 | 650 | return NS_OK; |
michael@0 | 651 | } |
michael@0 | 652 | }; |
michael@0 | 653 | |
michael@0 | 654 | } // anon |
michael@0 | 655 | |
michael@0 | 656 | // DOMStorageDBParent::CacheParentBridge |
michael@0 | 657 | |
michael@0 | 658 | bool |
michael@0 | 659 | DOMStorageDBParent::CacheParentBridge::LoadItem(const nsAString& aKey, const nsString& aValue) |
michael@0 | 660 | { |
michael@0 | 661 | if (mLoaded) { |
michael@0 | 662 | return false; |
michael@0 | 663 | } |
michael@0 | 664 | |
michael@0 | 665 | ++mLoadedCount; |
michael@0 | 666 | |
michael@0 | 667 | nsRefPtr<LoadRunnable> r = |
michael@0 | 668 | new LoadRunnable(mParent, LoadRunnable::loadItem, mScope, aKey, aValue); |
michael@0 | 669 | NS_DispatchToMainThread(r); |
michael@0 | 670 | return true; |
michael@0 | 671 | } |
michael@0 | 672 | |
michael@0 | 673 | void |
michael@0 | 674 | DOMStorageDBParent::CacheParentBridge::LoadDone(nsresult aRv) |
michael@0 | 675 | { |
michael@0 | 676 | // Prevent send of duplicate LoadDone. |
michael@0 | 677 | if (mLoaded) { |
michael@0 | 678 | return; |
michael@0 | 679 | } |
michael@0 | 680 | |
michael@0 | 681 | mLoaded = true; |
michael@0 | 682 | |
michael@0 | 683 | nsRefPtr<LoadRunnable> r = |
michael@0 | 684 | new LoadRunnable(mParent, LoadRunnable::loadDone, mScope, aRv); |
michael@0 | 685 | NS_DispatchToMainThread(r); |
michael@0 | 686 | } |
michael@0 | 687 | |
michael@0 | 688 | void |
michael@0 | 689 | DOMStorageDBParent::CacheParentBridge::LoadWait() |
michael@0 | 690 | { |
michael@0 | 691 | // Should never be called on this implementation |
michael@0 | 692 | MOZ_ASSERT(false); |
michael@0 | 693 | } |
michael@0 | 694 | |
michael@0 | 695 | // DOMStorageDBParent::UsageParentBridge |
michael@0 | 696 | |
michael@0 | 697 | namespace { // anon |
michael@0 | 698 | |
michael@0 | 699 | class UsageRunnable : public nsRunnable |
michael@0 | 700 | { |
michael@0 | 701 | public: |
michael@0 | 702 | UsageRunnable(DOMStorageDBParent* aParent, const nsACString& aScope, const int64_t& aUsage) |
michael@0 | 703 | : mParent(aParent) |
michael@0 | 704 | , mScope(aScope) |
michael@0 | 705 | , mUsage(aUsage) |
michael@0 | 706 | {} |
michael@0 | 707 | |
michael@0 | 708 | private: |
michael@0 | 709 | NS_IMETHOD Run() |
michael@0 | 710 | { |
michael@0 | 711 | if (!mParent->IPCOpen()) { |
michael@0 | 712 | return NS_OK; |
michael@0 | 713 | } |
michael@0 | 714 | |
michael@0 | 715 | mozilla::unused << mParent->SendLoadUsage(mScope, mUsage); |
michael@0 | 716 | return NS_OK; |
michael@0 | 717 | } |
michael@0 | 718 | |
michael@0 | 719 | nsRefPtr<DOMStorageDBParent> mParent; |
michael@0 | 720 | nsCString mScope; |
michael@0 | 721 | int64_t mUsage; |
michael@0 | 722 | }; |
michael@0 | 723 | |
michael@0 | 724 | } // anon |
michael@0 | 725 | |
michael@0 | 726 | void |
michael@0 | 727 | DOMStorageDBParent::UsageParentBridge::LoadUsage(const int64_t aUsage) |
michael@0 | 728 | { |
michael@0 | 729 | nsRefPtr<UsageRunnable> r = new UsageRunnable(mParent, mScope, aUsage); |
michael@0 | 730 | NS_DispatchToMainThread(r); |
michael@0 | 731 | } |
michael@0 | 732 | |
michael@0 | 733 | } // ::dom |
michael@0 | 734 | } // ::mozilla |