summaryrefslogtreecommitdiffstats
path: root/toolkit/components/cookiebanners/CookieBannerDomainPrefService.cpp
blob: 4b4912f1a15e0b00bda16f39b9b10d6e38a3a9e3 (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
/* 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 "CookieBannerDomainPrefService.h"

#include "mozilla/ClearOnShutdown.h"
#include "mozilla/DebugOnly.h"
#include "mozilla/Logging.h"
#include "mozilla/RefPtr.h"
#include "mozilla/Services.h"
#include "mozilla/SpinEventLoopUntil.h"
#include "mozilla/StaticPtr.h"

#include "nsIContentPrefService2.h"
#include "nsICookieBannerService.h"
#include "nsIObserverService.h"
#include "nsServiceManagerUtils.h"
#include "nsVariant.h"

#define COOKIE_BANNER_CONTENT_PREF_NAME u"cookiebanner"_ns
#define COOKIE_BANNER_CONTENT_PREF_NAME_PRIVATE u"cookiebannerprivate"_ns

namespace mozilla {

NS_IMPL_ISUPPORTS(CookieBannerDomainPrefService, nsIAsyncShutdownBlocker,
                  nsIObserver)

NS_IMPL_ISUPPORTS(CookieBannerDomainPrefService::DomainPrefData, nsISupports)

LazyLogModule gCookieBannerPerSitePrefLog("CookieBannerDomainPref");

static StaticRefPtr<CookieBannerDomainPrefService>
    sCookieBannerDomainPrefService;

namespace {

// A helper function to get the profile-before-change shutdown barrier.

nsCOMPtr<nsIAsyncShutdownClient> GetShutdownBarrier() {
  nsCOMPtr<nsIAsyncShutdownService> svc = services::GetAsyncShutdownService();
  NS_ENSURE_TRUE(svc, nullptr);

  nsCOMPtr<nsIAsyncShutdownClient> barrier;
  nsresult rv = svc->GetProfileBeforeChange(getter_AddRefs(barrier));
  NS_ENSURE_SUCCESS(rv, nullptr);

  return barrier;
}

}  // anonymous namespace

/* static */
already_AddRefed<CookieBannerDomainPrefService>
CookieBannerDomainPrefService::GetOrCreate() {
  if (!sCookieBannerDomainPrefService) {
    sCookieBannerDomainPrefService = new CookieBannerDomainPrefService();

    RunOnShutdown([] {
      MOZ_LOG(gCookieBannerPerSitePrefLog, LogLevel::Debug, ("RunOnShutdown."));

      sCookieBannerDomainPrefService->Shutdown();

      sCookieBannerDomainPrefService = nullptr;
    });
  }

  return do_AddRef(sCookieBannerDomainPrefService);
}

void CookieBannerDomainPrefService::Init() {
  // Make sure we won't init again.
  if (mIsInitialized) {
    return;
  }

  nsCOMPtr<nsIContentPrefService2> contentPrefService =
      do_GetService(NS_CONTENT_PREF_SERVICE_CONTRACTID);

  if (!contentPrefService) {
    return;
  }

  nsCOMPtr<nsIObserverService> obs = services::GetObserverService();

  if (!obs) {
    return;
  }

  // Register the observer to watch private browsing session ends. We will clean
  // the private domain prefs when this happens.
  nsresult rv = obs->AddObserver(this, "last-pb-context-exited", false);
  NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
                       "Fail to add observer for 'last-pb-context-exited'.");

  auto initCallback = MakeRefPtr<InitialLoadContentPrefCallback>(this, false);

  // Populate the content pref for cookie banner domain preferences.
  rv = contentPrefService->GetByName(COOKIE_BANNER_CONTENT_PREF_NAME, nullptr,
                                     initCallback);
  NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
                       "Fail to get all content prefs during init.");

  auto initPrivateCallback =
      MakeRefPtr<InitialLoadContentPrefCallback>(this, true);

  // Populate the content pref for the private browsing.
  rv = contentPrefService->GetByName(COOKIE_BANNER_CONTENT_PREF_NAME_PRIVATE,
                                     nullptr, initPrivateCallback);
  NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
                       "Fail to get all content prefs during init.");

  rv = AddShutdownBlocker();
  NS_ENSURE_SUCCESS_VOID(rv);

  mIsInitialized = true;
}

void CookieBannerDomainPrefService::Shutdown() {
  // Bail out early if the service never gets initialized.
  if (!mIsInitialized) {
    return;
  }

  nsCOMPtr<nsIObserverService> obs = services::GetObserverService();

  if (!obs) {
    return;
  }

  DebugOnly<nsresult> rv = obs->RemoveObserver(this, "last-pb-context-exited");
  NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
                       "Fail to remove observer for 'last-pb-context-exited'.");
}

Maybe<nsICookieBannerService::Modes> CookieBannerDomainPrefService::GetPref(
    const nsACString& aDomain, bool aIsPrivate) {
  bool isContentPrefLoaded =
      aIsPrivate ? mIsPrivateContentPrefLoaded : mIsContentPrefLoaded;

  // We return nothing if the first reading of the content pref is not completed
  // yet. Note that, we won't be able to get the domain pref for early loads.
  // But, we think this is acceptable because the cookie banners on the early
  // load tabs would have interacted before when the user disabled the banner
  // handling. So, there should be consent cookies in place to prevent banner
  // showing. In this case, our cookie injection and banner clicking won't do
  // anything.
  if (!isContentPrefLoaded) {
    return Nothing();
  }

  Maybe<RefPtr<DomainPrefData>> data =
      aIsPrivate ? mPrefsPrivate.MaybeGet(aDomain) : mPrefs.MaybeGet(aDomain);

  if (!data) {
    return Nothing();
  }

  return Some(data.ref()->mMode);
}

nsresult CookieBannerDomainPrefService::SetPref(
    const nsACString& aDomain, nsICookieBannerService::Modes aMode,
    bool aIsPrivate, bool aPersistInPrivateBrowsing) {
  MOZ_ASSERT(NS_IsMainThread());
  // Don't do anything if we are shutting down.
  if (NS_WARN_IF(mIsShuttingDown)) {
    MOZ_LOG(gCookieBannerPerSitePrefLog, LogLevel::Warning,
            ("Attempt to set a domain pref while shutting down."));
    return NS_OK;
  }

  EnsureInitCompleted(aIsPrivate);

  // Create the domain pref data. The data is always persistent for normal
  // windows. For private windows, the data is only persistent if requested.
  auto domainPrefData = MakeRefPtr<DomainPrefData>(
      aMode, aIsPrivate ? aPersistInPrivateBrowsing : true);
  bool wasPersistentInPrivate = false;

  // Update the in-memory domain preference map.
  if (aIsPrivate) {
    Maybe<RefPtr<DomainPrefData>> data = mPrefsPrivate.MaybeGet(aDomain);

    wasPersistentInPrivate = data ? data.ref()->mIsPersistent : false;
    Unused << mPrefsPrivate.InsertOrUpdate(aDomain, domainPrefData);
  } else {
    Unused << mPrefs.InsertOrUpdate(aDomain, domainPrefData);
  }

  // For private windows, the domain prefs will only be stored in memory.
  // Unless, this function is instructed to persist setting for private
  // browsing. To make the disk state consistent with the memory state, we need
  // to clear the domain pref in the disk when we no longer need to persist the
  // domain pref for the domain in PBM.
  if (!aPersistInPrivateBrowsing && aIsPrivate) {
    // Clear the domain pref in disk if it was persistent.
    if (wasPersistentInPrivate) {
      return RemoveContentPrefForDomain(aDomain, true);
    }
    return NS_OK;
  }

  // Set the preference to the content pref service.
  nsCOMPtr<nsIContentPrefService2> contentPrefService =
      do_GetService(NS_CONTENT_PREF_SERVICE_CONTRACTID);
  NS_ENSURE_TRUE(contentPrefService, NS_ERROR_FAILURE);

  RefPtr<nsVariant> variant = new nsVariant();
  nsresult rv = variant->SetAsUint8(aMode);
  NS_ENSURE_SUCCESS(rv, rv);

  auto callback = MakeRefPtr<WriteContentPrefCallback>(this);
  mWritingCount++;

  // Store the domain preference to the content pref service.
  rv = contentPrefService->Set(NS_ConvertUTF8toUTF16(aDomain),
                               aIsPrivate
                                   ? COOKIE_BANNER_CONTENT_PREF_NAME_PRIVATE
                                   : COOKIE_BANNER_CONTENT_PREF_NAME,
                               variant, nullptr, callback);
  NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
                       "Fail to set cookie banner domain pref.");

  return rv;
}

nsresult CookieBannerDomainPrefService::RemovePref(const nsACString& aDomain,
                                                   bool aIsPrivate) {
  MOZ_ASSERT(NS_IsMainThread());
  // Don't do anything if we are shutting down.
  if (NS_WARN_IF(mIsShuttingDown)) {
    MOZ_LOG(gCookieBannerPerSitePrefLog, LogLevel::Warning,
            ("Attempt to remove a domain pref while shutting down."));
    return NS_OK;
  }

  EnsureInitCompleted(aIsPrivate);

  // Clear in-memory domain pref.
  if (aIsPrivate) {
    mPrefsPrivate.Remove(aDomain);
  } else {
    mPrefs.Remove(aDomain);
  }

  return RemoveContentPrefForDomain(aDomain, aIsPrivate);
}

nsresult CookieBannerDomainPrefService::RemoveAll(bool aIsPrivate) {
  MOZ_ASSERT(NS_IsMainThread());
  // Don't do anything if we are shutting down.
  if (NS_WARN_IF(mIsShuttingDown)) {
    MOZ_LOG(gCookieBannerPerSitePrefLog, LogLevel::Warning,
            ("Attempt to remove all domain prefs while shutting down."));
    return NS_OK;
  }

  EnsureInitCompleted(aIsPrivate);

  // Clear in-memory domain pref.
  if (aIsPrivate) {
    mPrefsPrivate.Clear();
  } else {
    mPrefs.Clear();
  }

  nsCOMPtr<nsIContentPrefService2> contentPrefService =
      do_GetService(NS_CONTENT_PREF_SERVICE_CONTRACTID);
  NS_ENSURE_TRUE(contentPrefService, NS_ERROR_FAILURE);

  auto callback = MakeRefPtr<WriteContentPrefCallback>(this);
  mWritingCount++;

  // Remove all the domain preferences.
  nsresult rv = contentPrefService->RemoveByName(
      aIsPrivate ? COOKIE_BANNER_CONTENT_PREF_NAME_PRIVATE
                 : COOKIE_BANNER_CONTENT_PREF_NAME,
      nullptr, callback);
  NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
                       "Fail to remove all cookie banner domain prefs.");

  return rv;
}

void CookieBannerDomainPrefService::EnsureInitCompleted(bool aIsPrivate) {
  bool& isContentPrefLoaded =
      aIsPrivate ? mIsPrivateContentPrefLoaded : mIsContentPrefLoaded;
  if (isContentPrefLoaded) {
    return;
  }

  // Wait until the service is fully initialized.
  SpinEventLoopUntil("CookieBannerDomainPrefService::EnsureUpdateComplete"_ns,
                     [&] { return isContentPrefLoaded; });
}

nsresult CookieBannerDomainPrefService::AddShutdownBlocker() {
  MOZ_ASSERT(!mIsShuttingDown);

  nsCOMPtr<nsIAsyncShutdownClient> barrier = GetShutdownBarrier();
  NS_ENSURE_TRUE(barrier, NS_ERROR_FAILURE);

  return GetShutdownBarrier()->AddBlocker(
      this, NS_LITERAL_STRING_FROM_CSTRING(__FILE__), __LINE__,
      u"CookieBannerDomainPrefService: shutdown"_ns);
}

nsresult CookieBannerDomainPrefService::RemoveShutdownBlocker() {
  MOZ_ASSERT(mIsShuttingDown);

  nsCOMPtr<nsIAsyncShutdownClient> barrier = GetShutdownBarrier();
  NS_ENSURE_TRUE(barrier, NS_ERROR_FAILURE);

  return GetShutdownBarrier()->RemoveBlocker(this);
}

nsresult CookieBannerDomainPrefService::RemoveContentPrefForDomain(
    const nsACString& aDomain, bool aIsPrivate) {
  nsCOMPtr<nsIContentPrefService2> contentPrefService =
      do_GetService(NS_CONTENT_PREF_SERVICE_CONTRACTID);
  NS_ENSURE_TRUE(contentPrefService, NS_ERROR_FAILURE);

  auto callback = MakeRefPtr<WriteContentPrefCallback>(this);
  mWritingCount++;

  // Remove the domain preference from the content pref service.
  nsresult rv = contentPrefService->RemoveByDomainAndName(
      NS_ConvertUTF8toUTF16(aDomain),
      aIsPrivate ? COOKIE_BANNER_CONTENT_PREF_NAME_PRIVATE
                 : COOKIE_BANNER_CONTENT_PREF_NAME,
      nullptr, callback);
  NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
                       "Fail to remove cookie banner domain pref.");
  return rv;
}

NS_IMPL_ISUPPORTS(CookieBannerDomainPrefService::BaseContentPrefCallback,
                  nsIContentPrefCallback2)

NS_IMETHODIMP
CookieBannerDomainPrefService::InitialLoadContentPrefCallback::HandleResult(
    nsIContentPref* aPref) {
  NS_ENSURE_ARG_POINTER(aPref);
  MOZ_ASSERT(mService);

  nsAutoString domain;
  nsresult rv = aPref->GetDomain(domain);
  NS_ENSURE_SUCCESS(rv, rv);

  nsCOMPtr<nsIVariant> value;
  rv = aPref->GetValue(getter_AddRefs(value));
  NS_ENSURE_SUCCESS(rv, rv);

  if (!value) {
    return NS_OK;
  }

  uint8_t data;
  rv = value->GetAsUint8(&data);
  NS_ENSURE_SUCCESS(rv, rv);

  // Create the domain pref data and indicate it's persistent.
  auto domainPrefData =
      MakeRefPtr<DomainPrefData>(nsICookieBannerService::Modes(data), true);

  if (mIsPrivate) {
    Unused << mService->mPrefsPrivate.InsertOrUpdate(
        NS_ConvertUTF16toUTF8(domain), domainPrefData);
  } else {
    Unused << mService->mPrefs.InsertOrUpdate(NS_ConvertUTF16toUTF8(domain),
                                              domainPrefData);
  }

  return NS_OK;
}

NS_IMETHODIMP
CookieBannerDomainPrefService::InitialLoadContentPrefCallback::HandleCompletion(
    uint16_t aReason) {
  MOZ_ASSERT(mService);

  if (mIsPrivate) {
    mService->mIsPrivateContentPrefLoaded = true;
  } else {
    mService->mIsContentPrefLoaded = true;
  }

  return NS_OK;
}

NS_IMETHODIMP
CookieBannerDomainPrefService::InitialLoadContentPrefCallback::HandleError(
    nsresult error) {
  // We don't need to do anything here because HandleCompletion is always
  // called.

  if (NS_WARN_IF(NS_FAILED(error))) {
    MOZ_LOG(gCookieBannerPerSitePrefLog, LogLevel::Warning,
            ("Fail to get content pref during initiation."));
  }

  return NS_OK;
}

NS_IMETHODIMP
CookieBannerDomainPrefService::WriteContentPrefCallback::HandleResult(
    nsIContentPref* aPref) {
  return NS_OK;
}

NS_IMETHODIMP
CookieBannerDomainPrefService::WriteContentPrefCallback::HandleCompletion(
    uint16_t aReason) {
  MOZ_ASSERT(mService);
  MOZ_ASSERT(NS_IsMainThread());
  MOZ_ASSERT(mService->mWritingCount > 0);

  mService->mWritingCount--;

  // Remove the shutdown blocker after we complete writing to content pref.
  if (mService->mIsShuttingDown && mService->mWritingCount == 0) {
    mService->RemoveShutdownBlocker();
  }
  return NS_OK;
}

NS_IMETHODIMP
CookieBannerDomainPrefService::WriteContentPrefCallback::HandleError(
    nsresult error) {
  if (NS_WARN_IF(NS_FAILED(error))) {
    MOZ_LOG(gCookieBannerPerSitePrefLog, LogLevel::Warning,
            ("Fail to write content pref."));
    return NS_OK;
  }

  return NS_OK;
}

NS_IMETHODIMP
CookieBannerDomainPrefService::Observe(nsISupports* /*aSubject*/,
                                       const char* aTopic,
                                       const char16_t* /*aData*/) {
  if (strcmp(aTopic, "last-pb-context-exited") != 0) {
    MOZ_ASSERT_UNREACHABLE("unexpected topic");
    return NS_ERROR_UNEXPECTED;
  }

  // Clear the private browsing domain prefs that are not persistent when we
  // observe the private browsing session has ended.
  mPrefsPrivate.RemoveIf([](const auto& iter) {
    const RefPtr<DomainPrefData>& data = iter.Data();
    return !data->mIsPersistent;
  });

  return NS_OK;
}

NS_IMETHODIMP
CookieBannerDomainPrefService::GetName(nsAString& aName) {
  aName.AssignLiteral(
      "CookieBannerDomainPrefService: write content pref before "
      "profile-before-change.");
  return NS_OK;
}

NS_IMETHODIMP
CookieBannerDomainPrefService::BlockShutdown(nsIAsyncShutdownClient*) {
  MOZ_ASSERT(NS_IsMainThread());

  mIsShuttingDown = true;

  // If we are not writing the content pref, we can remove the shutdown blocker
  // directly.
  if (mWritingCount == 0) {
    RemoveShutdownBlocker();
  }
  return NS_OK;
}

NS_IMETHODIMP
CookieBannerDomainPrefService::GetState(nsIPropertyBag**) { return NS_OK; }

}  // namespace mozilla