summaryrefslogtreecommitdiffstats
path: root/dom/storage/StorageNotifierService.cpp
blob: 57b1ef6bc37609f33d0a1574293d387b24fd84b7 (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
/* -*- 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 "StorageNotifierService.h"
#include "StorageUtils.h"
#include "mozilla/dom/StorageEvent.h"
#include "mozilla/ClearOnShutdown.h"
#include "mozilla/StaticPtr.h"
#include "nsThreadUtils.h"

namespace mozilla::dom {

namespace {

// This boolean is used to avoid the creation of the service after been
// distroyed on shutdown.
bool gStorageShuttingDown = false;

StaticRefPtr<StorageNotifierService> gStorageNotifierService;

}  // namespace

/* static */
StorageNotifierService* StorageNotifierService::GetOrCreate() {
  MOZ_ASSERT(NS_IsMainThread());
  if (!gStorageNotifierService && !gStorageShuttingDown) {
    gStorageNotifierService = new StorageNotifierService();
    ClearOnShutdown(&gStorageNotifierService);
  }

  return gStorageNotifierService;
}

StorageNotifierService::StorageNotifierService() {
  MOZ_ASSERT(NS_IsMainThread());
  MOZ_ASSERT(!gStorageNotifierService);
}

StorageNotifierService::~StorageNotifierService() {
  MOZ_ASSERT(NS_IsMainThread());
  MOZ_ASSERT(!gStorageNotifierService);
  gStorageShuttingDown = true;
}

/* static */
void StorageNotifierService::Broadcast(StorageEvent* aEvent,
                                       const char16_t* aStorageType,
                                       bool aPrivateBrowsing,
                                       bool aImmediateDispatch) {
  MOZ_ASSERT(NS_IsMainThread());

  RefPtr<StorageNotifierService> service = gStorageNotifierService;
  if (!service) {
    return;
  }

  RefPtr<StorageEvent> event = aEvent;

  for (const auto& observer : service->mObservers.ForwardRange()) {
    // Enforce that the source storage area's private browsing state matches
    // this window's state.  These flag checks and their maintenance independent
    // from the principal's OriginAttributes matter because chrome docshells
    // that are part of private browsing windows can be private browsing without
    // having their OriginAttributes set (because they have the system
    // principal).
    if (aPrivateBrowsing != observer->IsPrivateBrowsing()) {
      continue;
    }

    // No reasons to continue if the principal of the event doesn't match with
    // the window's one.
    if (!StorageUtils::PrincipalsEqual(
            aEvent->GetPrincipal(), observer->GetEffectiveStoragePrincipal())) {
      continue;
    }

    const auto pinnedObserver = observer;

    RefPtr<Runnable> r = NS_NewRunnableFunction(
        "StorageNotifierService::Broadcast",
        [pinnedObserver, event, aStorageType, aPrivateBrowsing,
         aImmediateDispatch]() {
          // Check principals again. EffectiveStoragePrincipal may be changed
          // when relaxed.
          if (!aImmediateDispatch &&
              !StorageUtils::PrincipalsEqual(
                  event->GetPrincipal(),
                  pinnedObserver->GetEffectiveStoragePrincipal())) {
            return;
          }

          pinnedObserver->ObserveStorageNotification(event, aStorageType,
                                                     aPrivateBrowsing);
        });

    if (aImmediateDispatch) {
      r->Run();
    } else {
      nsCOMPtr<nsIEventTarget> et = pinnedObserver->GetEventTarget();
      if (et) {
        et->Dispatch(r.forget());
      }
    }
  }
}

void StorageNotifierService::Register(StorageNotificationObserver* aObserver) {
  MOZ_ASSERT(NS_IsMainThread());
  MOZ_ASSERT(aObserver);
  MOZ_ASSERT(!mObservers.Contains(aObserver));

  mObservers.AppendElement(aObserver);
}

void StorageNotifierService::Unregister(
    StorageNotificationObserver* aObserver) {
  MOZ_ASSERT(NS_IsMainThread());
  MOZ_ASSERT(aObserver);

  // No assertion about mObservers containing aObserver because window calls
  // this method multiple times.

  mObservers.RemoveElement(aObserver);
}

}  // namespace mozilla::dom