Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/storage/StorageObserver.cpp
Line
Count
Source (jump to first uncovered line)
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 "StorageObserver.h"
8
9
#include "LocalStorageCache.h"
10
#include "StorageDBThread.h"
11
#include "StorageUtils.h"
12
13
#include "mozilla/BasePrincipal.h"
14
#include "nsIObserverService.h"
15
#include "nsIURI.h"
16
#include "nsIURL.h"
17
#include "nsIScriptSecurityManager.h"
18
#include "nsIPermission.h"
19
#include "nsIIDNService.h"
20
#include "nsICookiePermission.h"
21
22
#include "nsPrintfCString.h"
23
#include "nsXULAppAPI.h"
24
#include "nsEscape.h"
25
#include "nsNetCID.h"
26
#include "mozilla/Preferences.h"
27
#include "mozilla/Services.h"
28
#include "nsServiceManagerUtils.h"
29
30
namespace mozilla {
31
namespace dom {
32
33
using namespace StorageUtils;
34
35
static const char kStartupTopic[] = "sessionstore-windows-restored";
36
static const uint32_t kStartupDelay = 0;
37
38
const char kTestingPref[] = "dom.storage.testing";
39
40
NS_IMPL_ISUPPORTS(StorageObserver,
41
                  nsIObserver,
42
                  nsISupportsWeakReference)
43
44
StorageObserver* StorageObserver::sSelf = nullptr;
45
46
// static
47
nsresult
48
StorageObserver::Init()
49
3
{
50
3
  if (sSelf) {
51
0
    return NS_OK;
52
0
  }
53
3
54
3
  nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
55
3
  if (!obs) {
56
0
    return NS_ERROR_UNEXPECTED;
57
0
  }
58
3
59
3
  sSelf = new StorageObserver();
60
3
  NS_ADDREF(sSelf);
61
3
62
3
  // Chrome clear operations.
63
3
  obs->AddObserver(sSelf, kStartupTopic, true);
64
3
  obs->AddObserver(sSelf, "cookie-changed", true);
65
3
  obs->AddObserver(sSelf, "perm-changed", true);
66
3
  obs->AddObserver(sSelf, "browser:purge-domain-data", true);
67
3
  obs->AddObserver(sSelf, "last-pb-context-exited", true);
68
3
  obs->AddObserver(sSelf, "clear-origin-attributes-data", true);
69
3
  obs->AddObserver(sSelf, "extension:purge-localStorage", true);
70
3
71
3
  // Shutdown
72
3
  obs->AddObserver(sSelf, "profile-after-change", true);
73
3
  if (XRE_IsParentProcess()) {
74
3
    obs->AddObserver(sSelf, "profile-before-change", true);
75
3
  }
76
3
77
3
  // Testing
78
3
#ifdef DOM_STORAGE_TESTS
79
3
  Preferences::RegisterCallbackAndCall(TestingPrefChanged, kTestingPref);
80
3
#endif
81
3
82
3
  return NS_OK;
83
3
}
84
85
// static
86
nsresult
87
StorageObserver::Shutdown()
88
0
{
89
0
  if (!sSelf) {
90
0
    return NS_ERROR_NOT_INITIALIZED;
91
0
  }
92
0
93
0
  NS_RELEASE(sSelf);
94
0
  return NS_OK;
95
0
}
96
97
// static
98
void
99
StorageObserver::TestingPrefChanged(const char* aPrefName, void* aClosure)
100
3
{
101
3
  nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
102
3
  if (!obs) {
103
0
    return;
104
0
  }
105
3
106
3
  if (Preferences::GetBool(kTestingPref)) {
107
0
    obs->AddObserver(sSelf, "domstorage-test-flush-force", true);
108
0
    if (XRE_IsParentProcess()) {
109
0
      // Only to forward to child process.
110
0
      obs->AddObserver(sSelf, "domstorage-test-flushed", true);
111
0
    }
112
0
    obs->AddObserver(sSelf, "domstorage-test-reload", true);
113
3
  } else {
114
3
    obs->RemoveObserver(sSelf, "domstorage-test-flush-force");
115
3
    if (XRE_IsParentProcess()) {
116
3
      // Only to forward to child process.
117
3
      obs->RemoveObserver(sSelf, "domstorage-test-flushed");
118
3
    }
119
3
    obs->RemoveObserver(sSelf, "domstorage-test-reload");
120
3
  }
121
3
}
122
123
void
124
StorageObserver::AddSink(StorageObserverSink* aObs)
125
0
{
126
0
  mSinks.AppendElement(aObs);
127
0
}
128
129
void
130
StorageObserver::RemoveSink(StorageObserverSink* aObs)
131
0
{
132
0
  mSinks.RemoveElement(aObs);
133
0
}
134
135
void
136
StorageObserver::Notify(const char* aTopic,
137
                        const nsAString& aOriginAttributesPattern,
138
                        const nsACString& aOriginScope)
139
0
{
140
0
  nsTObserverArray<StorageObserverSink*>::ForwardIterator iter(mSinks);
141
0
  while (iter.HasMore()) {
142
0
    StorageObserverSink* sink = iter.GetNext();
143
0
    sink->Observe(aTopic, aOriginAttributesPattern, aOriginScope);
144
0
  }
145
0
}
146
147
void
148
StorageObserver::NoteBackgroundThread(nsIEventTarget* aBackgroundThread)
149
0
{
150
0
  mBackgroundThread = aBackgroundThread;
151
0
}
152
153
nsresult
154
StorageObserver::ClearMatchingOrigin(const char16_t* aData,
155
                                     nsACString& aOriginScope)
156
0
{
157
0
  nsresult rv;
158
0
159
0
  NS_ConvertUTF16toUTF8 domain(aData);
160
0
161
0
  nsAutoCString convertedDomain;
162
0
  nsCOMPtr<nsIIDNService> converter = do_GetService(NS_IDNSERVICE_CONTRACTID);
163
0
  if (converter) {
164
0
    // Convert the domain name to the ACE format
165
0
    rv = converter->ConvertUTF8toACE(domain, convertedDomain);
166
0
  } else {
167
0
    // In case the IDN service is not available, this is the best we can come
168
0
    // up with!
169
0
    rv = NS_EscapeURL(domain,
170
0
                      esc_OnlyNonASCII | esc_AlwaysCopy,
171
0
                      convertedDomain,
172
0
                      fallible);
173
0
  }
174
0
  if (NS_WARN_IF(NS_FAILED(rv))) {
175
0
    return rv;
176
0
  }
177
0
178
0
  nsCString originScope;
179
0
  rv = CreateReversedDomain(convertedDomain, originScope);
180
0
  if (NS_WARN_IF(NS_FAILED(rv))) {
181
0
    return rv;
182
0
  }
183
0
184
0
  if (XRE_IsParentProcess()) {
185
0
    StorageDBChild* storageChild = StorageDBChild::GetOrCreate();
186
0
    if (NS_WARN_IF(!storageChild)) {
187
0
      return NS_ERROR_FAILURE;
188
0
    }
189
0
190
0
    storageChild->SendClearMatchingOrigin(originScope);
191
0
  }
192
0
193
0
  aOriginScope = originScope;
194
0
  return NS_OK;
195
0
}
196
197
NS_IMETHODIMP
198
StorageObserver::Observe(nsISupports* aSubject,
199
                         const char* aTopic,
200
                         const char16_t* aData)
201
0
{
202
0
  nsresult rv;
203
0
204
0
  // Start the thread that opens the database.
205
0
  if (!strcmp(aTopic, kStartupTopic)) {
206
0
    MOZ_ASSERT(XRE_IsParentProcess());
207
0
208
0
    nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
209
0
    obs->RemoveObserver(this, kStartupTopic);
210
0
211
0
    return NS_NewTimerWithObserver(getter_AddRefs(mDBThreadStartDelayTimer),
212
0
                                   this, nsITimer::TYPE_ONE_SHOT, kStartupDelay);
213
0
  }
214
0
215
0
  // Timer callback used to start the database a short timer after startup
216
0
  if (!strcmp(aTopic, NS_TIMER_CALLBACK_TOPIC)) {
217
0
    MOZ_ASSERT(XRE_IsParentProcess());
218
0
219
0
    nsCOMPtr<nsITimer> timer = do_QueryInterface(aSubject);
220
0
    if (!timer) {
221
0
      return NS_ERROR_UNEXPECTED;
222
0
    }
223
0
224
0
    if (timer == mDBThreadStartDelayTimer) {
225
0
      mDBThreadStartDelayTimer = nullptr;
226
0
227
0
      StorageDBChild* storageChild = StorageDBChild::GetOrCreate();
228
0
      if (NS_WARN_IF(!storageChild)) {
229
0
        return NS_ERROR_FAILURE;
230
0
      }
231
0
232
0
      storageChild->SendStartup();
233
0
    }
234
0
235
0
    return NS_OK;
236
0
  }
237
0
238
0
  // Clear everything, caches + database
239
0
  if (!strcmp(aTopic, "cookie-changed")) {
240
0
    if (!NS_LITERAL_STRING("cleared").Equals(aData)) {
241
0
      return NS_OK;
242
0
    }
243
0
244
0
    StorageDBChild* storageChild = StorageDBChild::GetOrCreate();
245
0
    if (NS_WARN_IF(!storageChild)) {
246
0
      return NS_ERROR_FAILURE;
247
0
    }
248
0
249
0
    storageChild->AsyncClearAll();
250
0
251
0
    if (XRE_IsParentProcess()) {
252
0
      storageChild->SendClearAll();
253
0
    }
254
0
255
0
    Notify("cookie-cleared");
256
0
257
0
    return NS_OK;
258
0
  }
259
0
260
0
  // Clear from caches everything that has been stored
261
0
  // while in session-only mode
262
0
  if (!strcmp(aTopic, "perm-changed")) {
263
0
    // Check for cookie permission change
264
0
    nsCOMPtr<nsIPermission> perm(do_QueryInterface(aSubject));
265
0
    if (!perm) {
266
0
      return NS_OK;
267
0
    }
268
0
269
0
    nsAutoCString type;
270
0
    perm->GetType(type);
271
0
    if (type != NS_LITERAL_CSTRING("cookie")) {
272
0
      return NS_OK;
273
0
    }
274
0
275
0
    uint32_t cap = 0;
276
0
    perm->GetCapability(&cap);
277
0
    if (!(cap & nsICookiePermission::ACCESS_SESSION) ||
278
0
        !NS_LITERAL_STRING("deleted").Equals(nsDependentString(aData))) {
279
0
      return NS_OK;
280
0
    }
281
0
282
0
    nsCOMPtr<nsIPrincipal> principal;
283
0
    perm->GetPrincipal(getter_AddRefs(principal));
284
0
    if (!principal) {
285
0
      return NS_OK;
286
0
    }
287
0
288
0
    nsAutoCString originSuffix;
289
0
    BasePrincipal::Cast(principal)->OriginAttributesRef().CreateSuffix(originSuffix);
290
0
291
0
    nsCOMPtr<nsIURI> origin;
292
0
    principal->GetURI(getter_AddRefs(origin));
293
0
    if (!origin) {
294
0
      return NS_OK;
295
0
    }
296
0
297
0
    nsAutoCString host;
298
0
    origin->GetHost(host);
299
0
    if (host.IsEmpty()) {
300
0
      return NS_OK;
301
0
    }
302
0
303
0
    nsAutoCString originScope;
304
0
    rv = CreateReversedDomain(host, originScope);
305
0
    NS_ENSURE_SUCCESS(rv, rv);
306
0
307
0
    Notify("session-only-cleared", NS_ConvertUTF8toUTF16(originSuffix),
308
0
           originScope);
309
0
310
0
    return NS_OK;
311
0
  }
312
0
313
0
  if (!strcmp(aTopic, "extension:purge-localStorage")) {
314
0
    const char topic[] = "extension:purge-localStorage-caches";
315
0
316
0
    if (aData) {
317
0
      nsCString originScope;
318
0
      rv = ClearMatchingOrigin(aData, originScope);
319
0
      if (NS_WARN_IF(NS_FAILED(rv))) {
320
0
        return rv;
321
0
      }
322
0
323
0
      Notify(topic, EmptyString(), originScope);
324
0
    } else {
325
0
      StorageDBChild* storageChild = StorageDBChild::GetOrCreate();
326
0
      if (NS_WARN_IF(!storageChild)) {
327
0
        return NS_ERROR_FAILURE;
328
0
      }
329
0
330
0
      storageChild->AsyncClearAll();
331
0
332
0
      if (XRE_IsParentProcess()) {
333
0
        storageChild->SendClearAll();
334
0
      }
335
0
336
0
      Notify(topic);
337
0
    }
338
0
339
0
    return NS_OK;
340
0
  }
341
0
342
0
  // Clear everything (including so and pb data) from caches and database
343
0
  // for the given domain and subdomains.
344
0
  if (!strcmp(aTopic, "browser:purge-domain-data")) {
345
0
    nsCString originScope;
346
0
    rv = ClearMatchingOrigin(aData, originScope);
347
0
    if (NS_WARN_IF(NS_FAILED(rv))) {
348
0
      return rv;
349
0
    }
350
0
351
0
    Notify("domain-data-cleared", EmptyString(), originScope);
352
0
353
0
    return NS_OK;
354
0
  }
355
0
356
0
  // Clear all private-browsing caches
357
0
  if (!strcmp(aTopic, "last-pb-context-exited")) {
358
0
    Notify("private-browsing-data-cleared");
359
0
360
0
    return NS_OK;
361
0
  }
362
0
363
0
  // Clear data of the origins whose prefixes will match the suffix.
364
0
  if (!strcmp(aTopic, "clear-origin-attributes-data")) {
365
0
    MOZ_ASSERT(XRE_IsParentProcess());
366
0
367
0
    OriginAttributesPattern pattern;
368
0
    if (!pattern.Init(nsDependentString(aData))) {
369
0
      NS_ERROR("Cannot parse origin attributes pattern");
370
0
      return NS_ERROR_FAILURE;
371
0
    }
372
0
373
0
    StorageDBChild* storageChild = StorageDBChild::GetOrCreate();
374
0
    if (NS_WARN_IF(!storageChild)) {
375
0
      return NS_ERROR_FAILURE;
376
0
    }
377
0
378
0
    storageChild->SendClearMatchingOriginAttributes(pattern);
379
0
380
0
    Notify("origin-attr-pattern-cleared", nsDependentString(aData));
381
0
382
0
    return NS_OK;
383
0
  }
384
0
385
0
  if (!strcmp(aTopic, "profile-after-change")) {
386
0
    Notify("profile-change");
387
0
388
0
    return NS_OK;
389
0
  }
390
0
391
0
  if (!strcmp(aTopic, "profile-before-change")) {
392
0
    MOZ_ASSERT(XRE_IsParentProcess());
393
0
394
0
    if (mBackgroundThread) {
395
0
      bool done = false;
396
0
397
0
      RefPtr<StorageDBThread::ShutdownRunnable> shutdownRunnable =
398
0
        new StorageDBThread::ShutdownRunnable(done);
399
0
      MOZ_ALWAYS_SUCCEEDS(
400
0
        mBackgroundThread->Dispatch(shutdownRunnable, NS_DISPATCH_NORMAL));
401
0
402
0
      MOZ_ALWAYS_TRUE(SpinEventLoopUntil([&]() { return done; }));
403
0
404
0
      mBackgroundThread = nullptr;
405
0
    }
406
0
407
0
    return NS_OK;
408
0
  }
409
0
410
0
#ifdef DOM_STORAGE_TESTS
411
0
  if (!strcmp(aTopic, "domstorage-test-flush-force")) {
412
0
    StorageDBChild* storageChild = StorageDBChild::GetOrCreate();
413
0
    if (NS_WARN_IF(!storageChild)) {
414
0
      return NS_ERROR_FAILURE;
415
0
    }
416
0
417
0
    storageChild->SendAsyncFlush();
418
0
419
0
    return NS_OK;
420
0
  }
421
0
422
0
  if (!strcmp(aTopic, "domstorage-test-flushed")) {
423
0
    // Only used to propagate to IPC children
424
0
    Notify("test-flushed");
425
0
426
0
    return NS_OK;
427
0
  }
428
0
429
0
  if (!strcmp(aTopic, "domstorage-test-reload")) {
430
0
    Notify("test-reload");
431
0
432
0
    return NS_OK;
433
0
  }
434
0
#endif
435
0
436
0
  NS_ERROR("Unexpected topic");
437
0
  return NS_ERROR_UNEXPECTED;
438
0
}
439
440
} // namespace dom
441
} // namespace mozilla