summaryrefslogtreecommitdiffstats
path: root/dom/storage/LocalStorageCache.cpp
blob: a06564a92c580322a9bf62bb9492495890f2ae91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "LocalStorageCache.h"

#include "Storage.h"
#include "StorageDBThread.h"
#include "StorageIPC.h"
#include "StorageUtils.h"
#include "LocalStorageManager.h"

#include "nsDOMString.h"
#include "nsXULAppAPI.h"
#include "mozilla/Unused.h"
#include "nsProxyRelease.h"
#include "nsThreadUtils.h"

namespace mozilla {
namespace dom {

#define DOM_STORAGE_CACHE_KEEP_ALIVE_TIME_MS 20000

namespace {

const uint32_t kDefaultSet = 0;
const uint32_t kSessionSet = 1;

inline uint32_t GetDataSetIndex(bool aPrivateBrowsing,
                                bool aSessionScopedOrLess) {
  if (!aPrivateBrowsing && aSessionScopedOrLess) {
    return kSessionSet;
  }

  return kDefaultSet;
}

inline uint32_t GetDataSetIndex(const LocalStorage* aStorage) {
  return GetDataSetIndex(aStorage->IsPrivateBrowsing(),
                         aStorage->IsSessionScopedOrLess());
}

}  // namespace

// LocalStorageCacheBridge

NS_IMPL_ADDREF(LocalStorageCacheBridge)

// Since there is no consumer of return value of Release, we can turn this
// method to void to make implementation of asynchronous
// LocalStorageCache::Release much simpler.
NS_IMETHODIMP_(void) LocalStorageCacheBridge::Release(void) {
  MOZ_ASSERT(int32_t(mRefCnt) > 0, "dup release");
  nsrefcnt count = --mRefCnt;
  NS_LOG_RELEASE(this, count, "LocalStorageCacheBridge");
  if (0 == count) {
    mRefCnt = 1; /* stabilize */
    /* enable this to find non-threadsafe destructors: */
    /* NS_ASSERT_OWNINGTHREAD(_class); */
    delete (this);
  }
}

// LocalStorageCache

LocalStorageCache::LocalStorageCache(const nsACString* aOriginNoSuffix)
    : mActor(nullptr),
      mOriginNoSuffix(*aOriginNoSuffix),
      mMonitor("LocalStorageCache"),
      mLoaded(false),
      mLoadResult(NS_OK),
      mInitialized(false),
      mPersistent(false),
      mPreloadTelemetryRecorded(false) {
  MOZ_COUNT_CTOR(LocalStorageCache);
}

LocalStorageCache::~LocalStorageCache() {
  if (mActor) {
    mActor->SendDeleteMeInternal();
    MOZ_ASSERT(!mActor, "SendDeleteMeInternal should have cleared!");
  }

  if (mManager) {
    mManager->DropCache(this);
  }

  MOZ_COUNT_DTOR(LocalStorageCache);
}

void LocalStorageCache::SetActor(LocalStorageCacheChild* aActor) {
  AssertIsOnOwningThread();
  MOZ_ASSERT(aActor);
  MOZ_ASSERT(!mActor);

  mActor = aActor;
}

NS_IMETHODIMP_(void)
LocalStorageCache::Release(void) {
  // We must actually release on the main thread since the cache removes it
  // self from the manager's hash table.  And we don't want to lock access to
  // that hash table.
  if (NS_IsMainThread()) {
    LocalStorageCacheBridge::Release();
    return;
  }

  RefPtr<nsRunnableMethod<LocalStorageCacheBridge, void, false>> event =
      NewNonOwningRunnableMethod("dom::LocalStorageCacheBridge::Release",
                                 static_cast<LocalStorageCacheBridge*>(this),
                                 &LocalStorageCacheBridge::Release);

  nsresult rv = NS_DispatchToMainThread(event);
  if (NS_FAILED(rv)) {
    NS_WARNING("LocalStorageCache::Release() on a non-main thread");
    LocalStorageCacheBridge::Release();
  }
}

void LocalStorageCache::Init(LocalStorageManager* aManager, bool aPersistent,
                             nsIPrincipal* aPrincipal,
                             const nsACString& aQuotaOriginScope) {
  MOZ_ASSERT(!aQuotaOriginScope.IsEmpty());

  if (mInitialized) {
    return;
  }

  mInitialized = true;
  aPrincipal->OriginAttributesRef().CreateSuffix(mOriginSuffix);
  mPrivateBrowsingId = aPrincipal->GetPrivateBrowsingId();
  mPersistent = aPersistent;
  mQuotaOriginScope = aQuotaOriginScope;

  if (mPersistent) {
    mManager = aManager;
    Preload();
  }

  // Check the quota string has (or has not) the identical origin suffix as
  // this storage cache is bound to.
  MOZ_ASSERT(StringBeginsWith(mQuotaOriginScope, mOriginSuffix));
  MOZ_ASSERT(mOriginSuffix.IsEmpty() !=
             StringBeginsWith(mQuotaOriginScope, "^"_ns));

  mUsage = aManager->GetOriginUsage(mQuotaOriginScope, mPrivateBrowsingId);
}

void LocalStorageCache::NotifyObservers(const LocalStorage* aStorage,
                                        const nsString& aKey,
                                        const nsString& aOldValue,
                                        const nsString& aNewValue) {
  AssertIsOnOwningThread();
  MOZ_ASSERT(aStorage);

  if (!mActor) {
    return;
  }

  // We want to send a message to the parent in order to broadcast the
  // StorageEvent correctly to any child process.

  Unused << mActor->SendNotify(aStorage->DocumentURI(), aKey, aOldValue,
                               aNewValue);
}

inline bool LocalStorageCache::Persist(const LocalStorage* aStorage) const {
  return mPersistent &&
         (aStorage->IsPrivateBrowsing() || !aStorage->IsSessionScopedOrLess());
}

const nsCString LocalStorageCache::Origin() const {
  return LocalStorageManager::CreateOrigin(mOriginSuffix, mOriginNoSuffix);
}

LocalStorageCache::Data& LocalStorageCache::DataSet(
    const LocalStorage* aStorage) {
  return mData[GetDataSetIndex(aStorage)];
}

bool LocalStorageCache::ProcessUsageDelta(const LocalStorage* aStorage,
                                          int64_t aDelta,
                                          const MutationSource aSource) {
  return ProcessUsageDelta(GetDataSetIndex(aStorage), aDelta, aSource);
}

bool LocalStorageCache::ProcessUsageDelta(uint32_t aGetDataSetIndex,
                                          const int64_t aDelta,
                                          const MutationSource aSource) {
  // Check limit per this origin
  Data& data = mData[aGetDataSetIndex];
  uint64_t newOriginUsage = data.mOriginQuotaUsage + aDelta;
  if (aSource == ContentMutation && aDelta > 0 &&
      newOriginUsage > LocalStorageManager::GetOriginQuota()) {
    return false;
  }

  // Now check eTLD+1 limit
  if (mUsage &&
      !mUsage->CheckAndSetETLD1UsageDelta(aGetDataSetIndex, aDelta, aSource)) {
    return false;
  }

  // Update size in our data set
  data.mOriginQuotaUsage = newOriginUsage;
  return true;
}

void LocalStorageCache::Preload() {
  if (mLoaded || !mPersistent) {
    return;
  }

  StorageDBChild* storageChild =
      StorageDBChild::GetOrCreate(mPrivateBrowsingId);
  if (!storageChild) {
    mLoaded = true;
    mLoadResult = NS_ERROR_FAILURE;
    return;
  }

  storageChild->AsyncPreload(this);
}

void LocalStorageCache::WaitForPreload(Telemetry::HistogramID aTelemetryID) {
  if (!mPersistent) {
    return;
  }

  bool loaded = mLoaded;

  // Telemetry of rates of pending preloads
  if (!mPreloadTelemetryRecorded) {
    mPreloadTelemetryRecorded = true;
    Telemetry::Accumulate(
        Telemetry::LOCALDOMSTORAGE_PRELOAD_PENDING_ON_FIRST_ACCESS, !loaded);
  }

  if (loaded) {
    return;
  }

  // Measure which operation blocks and for how long
  Telemetry::RuntimeAutoTimer timer(aTelemetryID);

  // If preload already started (i.e. we got some first data, but not all)
  // SyncPreload will just wait for it to finish rather then synchronously
  // read from the database.  It seems to me more optimal.

  // TODO place for A/B testing (force main thread load vs. let preload finish)

  // No need to check sDatabase for being non-null since preload is either
  // done before we've shut the DB down or when the DB could not start,
  // preload has not even be started.
  StorageDBChild::Get(mPrivateBrowsingId)->SyncPreload(this);
}

nsresult LocalStorageCache::GetLength(const LocalStorage* aStorage,
                                      uint32_t* aRetval) {
  if (Persist(aStorage)) {
    WaitForPreload(Telemetry::LOCALDOMSTORAGE_GETLENGTH_BLOCKING_MS);
    if (NS_FAILED(mLoadResult)) {
      return mLoadResult;
    }
  }

  *aRetval = DataSet(aStorage).mKeys.Count();
  return NS_OK;
}

nsresult LocalStorageCache::GetKey(const LocalStorage* aStorage,
                                   uint32_t aIndex, nsAString& aRetval) {
  // XXX: This does a linear search for the key at index, which would
  // suck if there's a large numer of indexes. Do we care? If so,
  // maybe we need to have a lazily populated key array here or
  // something?
  if (Persist(aStorage)) {
    WaitForPreload(Telemetry::LOCALDOMSTORAGE_GETKEY_BLOCKING_MS);
    if (NS_FAILED(mLoadResult)) {
      return mLoadResult;
    }
  }

  aRetval.SetIsVoid(true);
  for (auto iter = DataSet(aStorage).mKeys.Iter(); !iter.Done(); iter.Next()) {
    if (aIndex == 0) {
      aRetval = iter.Key();
      break;
    }
    aIndex--;
  }

  return NS_OK;
}

void LocalStorageCache::GetKeys(const LocalStorage* aStorage,
                                nsTArray<nsString>& aKeys) {
  if (Persist(aStorage)) {
    WaitForPreload(Telemetry::LOCALDOMSTORAGE_GETALLKEYS_BLOCKING_MS);
  }

  if (NS_FAILED(mLoadResult)) {
    return;
  }

  for (auto iter = DataSet(aStorage).mKeys.Iter(); !iter.Done(); iter.Next()) {
    aKeys.AppendElement(iter.Key());
  }
}

nsresult LocalStorageCache::GetItem(const LocalStorage* aStorage,
                                    const nsAString& aKey, nsAString& aRetval) {
  if (Persist(aStorage)) {
    WaitForPreload(Telemetry::LOCALDOMSTORAGE_GETVALUE_BLOCKING_MS);
    if (NS_FAILED(mLoadResult)) {
      return mLoadResult;
    }
  }

  // not using AutoString since we don't want to copy buffer to result
  nsString value;
  if (!DataSet(aStorage).mKeys.Get(aKey, &value)) {
    SetDOMStringToNull(value);
  }

  aRetval = value;

  return NS_OK;
}

nsresult LocalStorageCache::SetItem(const LocalStorage* aStorage,
                                    const nsAString& aKey,
                                    const nsString& aValue, nsString& aOld,
                                    const MutationSource aSource) {
  // Size of the cache that will change after this action.
  int64_t delta = 0;

  if (Persist(aStorage)) {
    WaitForPreload(Telemetry::LOCALDOMSTORAGE_SETVALUE_BLOCKING_MS);
    if (NS_FAILED(mLoadResult)) {
      return mLoadResult;
    }
  }

  Data& data = DataSet(aStorage);
  if (!data.mKeys.Get(aKey, &aOld)) {
    SetDOMStringToNull(aOld);

    // We only consider key size if the key doesn't exist before.
    delta += static_cast<int64_t>(aKey.Length());
  }

  delta += static_cast<int64_t>(aValue.Length()) -
           static_cast<int64_t>(aOld.Length());

  if (!ProcessUsageDelta(aStorage, delta, aSource)) {
    return NS_ERROR_DOM_QUOTA_EXCEEDED_ERR;
  }

  if (aValue == aOld && DOMStringIsNull(aValue) == DOMStringIsNull(aOld)) {
    return NS_SUCCESS_DOM_NO_OPERATION;
  }

  data.mKeys.Put(aKey, aValue);

  if (aSource != ContentMutation) {
    return NS_OK;
  }

#if !defined(MOZ_WIDGET_ANDROID)
  NotifyObservers(aStorage, nsString(aKey), aOld, aValue);
#endif

  if (Persist(aStorage)) {
    StorageDBChild* storageChild = StorageDBChild::Get(mPrivateBrowsingId);
    if (!storageChild) {
      NS_ERROR(
          "Writing to localStorage after the database has been shut down"
          ", data lose!");
      return NS_ERROR_NOT_INITIALIZED;
    }

    if (DOMStringIsNull(aOld)) {
      return storageChild->AsyncAddItem(this, aKey, aValue);
    }

    return storageChild->AsyncUpdateItem(this, aKey, aValue);
  }

  return NS_OK;
}

nsresult LocalStorageCache::RemoveItem(const LocalStorage* aStorage,
                                       const nsAString& aKey, nsString& aOld,
                                       const MutationSource aSource) {
  if (Persist(aStorage)) {
    WaitForPreload(Telemetry::LOCALDOMSTORAGE_REMOVEKEY_BLOCKING_MS);
    if (NS_FAILED(mLoadResult)) {
      return mLoadResult;
    }
  }

  Data& data = DataSet(aStorage);
  if (!data.mKeys.Get(aKey, &aOld)) {
    SetDOMStringToNull(aOld);
    return NS_SUCCESS_DOM_NO_OPERATION;
  }

  // Recalculate the cached data size
  const int64_t delta = -(static_cast<int64_t>(aOld.Length()) +
                          static_cast<int64_t>(aKey.Length()));
  Unused << ProcessUsageDelta(aStorage, delta, aSource);
  data.mKeys.Remove(aKey);

  if (aSource != ContentMutation) {
    return NS_OK;
  }

#if !defined(MOZ_WIDGET_ANDROID)
  NotifyObservers(aStorage, nsString(aKey), aOld, VoidString());
#endif

  if (Persist(aStorage)) {
    StorageDBChild* storageChild = StorageDBChild::Get(mPrivateBrowsingId);
    if (!storageChild) {
      NS_ERROR(
          "Writing to localStorage after the database has been shut down"
          ", data lose!");
      return NS_ERROR_NOT_INITIALIZED;
    }

    return storageChild->AsyncRemoveItem(this, aKey);
  }

  return NS_OK;
}

nsresult LocalStorageCache::Clear(const LocalStorage* aStorage,
                                  const MutationSource aSource) {
  bool refresh = false;
  if (Persist(aStorage)) {
    // We need to preload all data (know the size) before we can proceeed
    // to correctly decrease cached usage number.
    // XXX as in case of unload, this is not technically needed now, but
    // after super-scope quota introduction we have to do this.  Get telemetry
    // right now.
    WaitForPreload(Telemetry::LOCALDOMSTORAGE_CLEAR_BLOCKING_MS);
    if (NS_FAILED(mLoadResult)) {
      // When we failed to load data from the database, force delete of the
      // scope data and make use of the storage possible again.
      refresh = true;
      mLoadResult = NS_OK;
    }
  }

  Data& data = DataSet(aStorage);
  bool hadData = !!data.mKeys.Count();

  if (hadData) {
    Unused << ProcessUsageDelta(aStorage, -data.mOriginQuotaUsage, aSource);
    data.mKeys.Clear();
  }

  if (aSource != ContentMutation) {
    return hadData ? NS_OK : NS_SUCCESS_DOM_NO_OPERATION;
  }

#if !defined(MOZ_WIDGET_ANDROID)
  if (hadData) {
    NotifyObservers(aStorage, VoidString(), VoidString(), VoidString());
  }
#endif

  if (Persist(aStorage) && (refresh || hadData)) {
    StorageDBChild* storageChild = StorageDBChild::Get(mPrivateBrowsingId);
    if (!storageChild) {
      NS_ERROR(
          "Writing to localStorage after the database has been shut down"
          ", data lose!");
      return NS_ERROR_NOT_INITIALIZED;
    }

    return storageChild->AsyncClear(this);
  }

  return hadData ? NS_OK : NS_SUCCESS_DOM_NO_OPERATION;
}

int64_t LocalStorageCache::GetOriginQuotaUsage(
    const LocalStorage* aStorage) const {
  return mData[GetDataSetIndex(aStorage)].mOriginQuotaUsage;
}

void LocalStorageCache::UnloadItems(uint32_t aUnloadFlags) {
  if (aUnloadFlags & kUnloadDefault) {
    // Must wait for preload to pass correct usage to ProcessUsageDelta
    // XXX this is not technically needed right now since there is just
    // per-origin isolated quota handling, but when we introduce super-
    // -scope quotas, we have to do this.  Better to start getting
    // telemetry right now.
    WaitForPreload(Telemetry::LOCALDOMSTORAGE_UNLOAD_BLOCKING_MS);

    mData[kDefaultSet].mKeys.Clear();
    ProcessUsageDelta(kDefaultSet, -mData[kDefaultSet].mOriginQuotaUsage);
  }

  if (aUnloadFlags & kUnloadSession) {
    mData[kSessionSet].mKeys.Clear();
    ProcessUsageDelta(kSessionSet, -mData[kSessionSet].mOriginQuotaUsage);
  }

#ifdef DOM_STORAGE_TESTS
  if (aUnloadFlags & kTestReload) {
    WaitForPreload(Telemetry::LOCALDOMSTORAGE_UNLOAD_BLOCKING_MS);

    mData[kDefaultSet].mKeys.Clear();
    mLoaded = false;  // This is only used in testing code
    Preload();
  }
#endif
}

// LocalStorageCacheBridge

uint32_t LocalStorageCache::LoadedCount() {
  MonitorAutoLock monitor(mMonitor);
  Data& data = mData[kDefaultSet];
  return data.mKeys.Count();
}

bool LocalStorageCache::LoadItem(const nsAString& aKey,
                                 const nsString& aValue) {
  MonitorAutoLock monitor(mMonitor);
  if (mLoaded) {
    return false;
  }

  Data& data = mData[kDefaultSet];
  if (data.mKeys.Get(aKey, nullptr)) {
    return true;  // don't stop, just don't override
  }

  data.mKeys.Put(aKey, aValue);
  data.mOriginQuotaUsage += aKey.Length() + aValue.Length();
  return true;
}

void LocalStorageCache::LoadDone(nsresult aRv) {
  MonitorAutoLock monitor(mMonitor);
  mLoadResult = aRv;
  mLoaded = true;
  monitor.Notify();
}

void LocalStorageCache::LoadWait() {
  MonitorAutoLock monitor(mMonitor);
  while (!mLoaded) {
    monitor.Wait();
  }
}

// StorageUsage

StorageUsage::StorageUsage(const nsACString& aOriginScope)
    : mOriginScope(aOriginScope) {
  mUsage[kDefaultSet] = mUsage[kSessionSet] = 0LL;
}

namespace {

class LoadUsageRunnable : public Runnable {
 public:
  LoadUsageRunnable(int64_t* aUsage, const int64_t aDelta)
      : Runnable("dom::LoadUsageRunnable"), mTarget(aUsage), mDelta(aDelta) {}

 private:
  int64_t* mTarget;
  int64_t mDelta;

  NS_IMETHOD Run() override {
    *mTarget = mDelta;
    return NS_OK;
  }
};

}  // namespace

void StorageUsage::LoadUsage(const int64_t aUsage) {
  // Using kDefaultSet index since it is the index for the persitent data
  // stored in the database we have just loaded usage for.
  if (!NS_IsMainThread()) {
    // In single process scenario we get this call from the DB thread
    RefPtr<LoadUsageRunnable> r =
        new LoadUsageRunnable(mUsage + kDefaultSet, aUsage);
    NS_DispatchToMainThread(r);
  } else {
    // On a child process we get this on the main thread already
    mUsage[kDefaultSet] += aUsage;
  }
}

bool StorageUsage::CheckAndSetETLD1UsageDelta(
    uint32_t aDataSetIndex, const int64_t aDelta,
    const LocalStorageCache::MutationSource aSource) {
  MOZ_ASSERT(NS_IsMainThread());

  int64_t newUsage = mUsage[aDataSetIndex] + aDelta;
  if (aSource == LocalStorageCache::ContentMutation && aDelta > 0 &&
      newUsage > LocalStorageManager::GetSiteQuota()) {
    return false;
  }

  mUsage[aDataSetIndex] = newUsage;
  return true;
}

}  // namespace dom
}  // namespace mozilla