Backed out changeset 5c8bf24bb247 (bug 1923942) for causing generate failures. CLOSED...
[gecko.git] / dom / localstorage / LSObserver.cpp
blobfda56bd1ed0d1edf1b63c3f7f7dcdda9d708b538
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=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 file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "LSObserver.h"
9 // Local includes
10 #include "ActorsChild.h"
12 // Global includes
13 #include <utility>
14 #include "mozilla/StaticPtr.h"
15 #include "nsTHashMap.h"
16 #include "nsHashKeys.h"
18 namespace mozilla::dom {
20 namespace {
22 using LSObserverHashtable = nsTHashMap<nsCStringHashKey, LSObserver*>;
24 StaticAutoPtr<LSObserverHashtable> gLSObservers;
26 } // namespace
28 LSObserver::LSObserver(const nsACString& aOrigin)
29 : mActor(nullptr), mOrigin(aOrigin) {
30 AssertIsOnOwningThread();
32 if (!gLSObservers) {
33 gLSObservers = new LSObserverHashtable();
36 MOZ_ASSERT(!gLSObservers->Contains(mOrigin));
37 gLSObservers->InsertOrUpdate(mOrigin, this);
40 LSObserver::~LSObserver() {
41 AssertIsOnOwningThread();
43 if (mActor) {
44 mActor->SendDeleteMeInternal();
45 MOZ_ASSERT(!mActor, "SendDeleteMeInternal should have cleared!");
48 MOZ_ASSERT(gLSObservers);
49 MOZ_ASSERT(gLSObservers->Get(mOrigin));
50 gLSObservers->Remove(mOrigin);
52 if (!gLSObservers->Count()) {
53 gLSObservers = nullptr;
57 // static
58 LSObserver* LSObserver::Get(const nsACString& aOrigin) {
59 return gLSObservers ? gLSObservers->Get(aOrigin) : nullptr;
62 void LSObserver::SetActor(LSObserverChild* aActor) {
63 AssertIsOnOwningThread();
64 MOZ_ASSERT(aActor);
65 MOZ_ASSERT(!mActor);
67 mActor = aActor;
70 } // namespace mozilla::dom