summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/ExtensionPolicyService.cpp
blob: 833eedffa3b511db70a250fa91027dc2ba58976e (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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
/* 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 "mozilla/ExtensionPolicyService.h"
#include "mozilla/extensions/DocumentObserver.h"
#include "mozilla/extensions/WebExtensionContentScript.h"
#include "mozilla/extensions/WebExtensionPolicy.h"

#include "mozilla/BasePrincipal.h"
#include "mozilla/ClearOnShutdown.h"
#include "mozilla/Preferences.h"
#include "mozilla/ResultExtensions.h"
#include "mozilla/Services.h"
#include "mozilla/SimpleEnumerator.h"
#include "mozilla/StaticPrefs_extensions.h"
#include "mozilla/dom/BrowsingContext.h"
#include "mozilla/dom/BrowsingContextGroup.h"
#include "mozilla/dom/ContentChild.h"
#include "mozilla/dom/ContentParent.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/dom/Promise-inl.h"
#include "mozIExtensionProcessScript.h"
#include "nsEscape.h"
#include "nsGkAtoms.h"
#include "nsHashKeys.h"
#include "nsIChannel.h"
#include "nsIContentPolicy.h"
#include "mozilla/dom/Document.h"
#include "nsGlobalWindowOuter.h"
#include "nsILoadInfo.h"
#include "nsIXULRuntime.h"
#include "nsImportModule.h"
#include "nsNetUtil.h"
#include "nsPrintfCString.h"
#include "nsPIDOMWindow.h"
#include "nsXULAppAPI.h"
#include "nsQueryObject.h"

namespace mozilla {

using namespace extensions;

using dom::AutoJSAPI;
using dom::Document;
using dom::Promise;

#define DEFAULT_CSP_PREF \
  "extensions.webextensions.default-content-security-policy"
#define DEFAULT_DEFAULT_CSP "script-src 'self' 'wasm-unsafe-eval';"

#define DEFAULT_CSP_PREF_V3 \
  "extensions.webextensions.default-content-security-policy.v3"
#define DEFAULT_DEFAULT_CSP_V3 "script-src 'self'; upgrade-insecure-requests;"

#define RESTRICTED_DOMAINS_PREF "extensions.webextensions.restrictedDomains"

#define QUARANTINED_DOMAINS_PREF "extensions.quarantinedDomains.list"
#define QUARANTINED_DOMAINS_ENABLED "extensions.quarantinedDomains.enabled"

#define OBS_TOPIC_PRELOAD_SCRIPT "web-extension-preload-content-script"
#define OBS_TOPIC_LOAD_SCRIPT "web-extension-load-content-script"

static const char kDocElementInserted[] = "initial-document-element-inserted";

/*****************************************************************************
 * ExtensionPolicyService
 *****************************************************************************/

using CoreByHostMap = nsTHashMap<nsCStringASCIICaseInsensitiveHashKey,
                                 RefPtr<extensions::WebExtensionPolicyCore>>;

static StaticRWLock sEPSLock;
static StaticAutoPtr<CoreByHostMap> sCoreByHost MOZ_GUARDED_BY(sEPSLock);
static StaticRefPtr<AtomSet> sRestrictedDomains MOZ_GUARDED_BY(sEPSLock);
static StaticRefPtr<AtomSet> sQuarantinedDomains MOZ_GUARDED_BY(sEPSLock);

/* static */
mozIExtensionProcessScript& ExtensionPolicyService::ProcessScript() {
  static nsCOMPtr<mozIExtensionProcessScript> sProcessScript;

  MOZ_ASSERT(NS_IsMainThread());

  if (MOZ_UNLIKELY(!sProcessScript)) {
    sProcessScript =
        do_ImportModule("resource://gre/modules/ExtensionProcessScript.jsm",
                        "ExtensionProcessScript");
    ClearOnShutdown(&sProcessScript);
  }
  return *sProcessScript;
}

/* static */ ExtensionPolicyService& ExtensionPolicyService::GetSingleton() {
  MOZ_ASSERT(NS_IsMainThread());

  static RefPtr<ExtensionPolicyService> sExtensionPolicyService;

  if (MOZ_UNLIKELY(!sExtensionPolicyService)) {
    sExtensionPolicyService = new ExtensionPolicyService();
    RegisterWeakMemoryReporter(sExtensionPolicyService);
    ClearOnShutdown(&sExtensionPolicyService);
  }
  return *sExtensionPolicyService.get();
}

/* static */
RefPtr<extensions::WebExtensionPolicyCore>
ExtensionPolicyService::GetCoreByHost(const nsACString& aHost) {
  StaticAutoReadLock lock(sEPSLock);
  return sCoreByHost ? sCoreByHost->Get(aHost) : nullptr;
}

ExtensionPolicyService::ExtensionPolicyService() {
  mObs = services::GetObserverService();
  MOZ_RELEASE_ASSERT(mObs);

  mDefaultCSP.SetIsVoid(true);
  mDefaultCSPV3.SetIsVoid(true);

  RegisterObservers();

  {
    StaticAutoWriteLock lock(sEPSLock);
    MOZ_DIAGNOSTIC_ASSERT(!sCoreByHost,
                          "ExtensionPolicyService created twice?");
    sCoreByHost = new CoreByHostMap();
  }

  UpdateRestrictedDomains();
  UpdateQuarantinedDomains();
}

ExtensionPolicyService::~ExtensionPolicyService() {
  UnregisterWeakMemoryReporter(this);

  {
    StaticAutoWriteLock lock(sEPSLock);
    sCoreByHost = nullptr;
    sRestrictedDomains = nullptr;
    sQuarantinedDomains = nullptr;
  }
}

bool ExtensionPolicyService::UseRemoteExtensions() const {
  static Maybe<bool> sRemoteExtensions;
  if (MOZ_UNLIKELY(sRemoteExtensions.isNothing())) {
    sRemoteExtensions = Some(StaticPrefs::extensions_webextensions_remote());
  }
  return sRemoteExtensions.value() && BrowserTabsRemoteAutostart();
}

bool ExtensionPolicyService::IsExtensionProcess() const {
  bool isRemote = UseRemoteExtensions();

  if (isRemote && XRE_IsContentProcess()) {
    auto& remoteType = dom::ContentChild::GetSingleton()->GetRemoteType();
    return remoteType == EXTENSION_REMOTE_TYPE;
  }
  return !isRemote && XRE_IsParentProcess();
}

bool ExtensionPolicyService::GetQuarantinedDomainsEnabled() const {
  return Preferences::GetBool(QUARANTINED_DOMAINS_ENABLED);
}

WebExtensionPolicy* ExtensionPolicyService::GetByURL(const URLInfo& aURL) {
  if (aURL.Scheme() == nsGkAtoms::moz_extension) {
    return GetByHost(aURL.Host());
  }
  return nullptr;
}

WebExtensionPolicy* ExtensionPolicyService::GetByHost(
    const nsACString& aHost) const {
  AssertIsOnMainThread();
  RefPtr<WebExtensionPolicyCore> core = GetCoreByHost(aHost);
  return core ? core->GetMainThreadPolicy() : nullptr;
}

void ExtensionPolicyService::GetAll(
    nsTArray<RefPtr<WebExtensionPolicy>>& aResult) {
  AppendToArray(aResult, mExtensions.Values());
}

bool ExtensionPolicyService::RegisterExtension(WebExtensionPolicy& aPolicy) {
  bool ok =
      (!GetByID(aPolicy.Id()) && !GetByHost(aPolicy.MozExtensionHostname()));
  MOZ_ASSERT(ok);

  if (!ok) {
    return false;
  }

  mExtensions.InsertOrUpdate(aPolicy.Id(), RefPtr{&aPolicy});

  {
    StaticAutoWriteLock lock(sEPSLock);
    sCoreByHost->InsertOrUpdate(aPolicy.MozExtensionHostname(), aPolicy.Core());
  }
  return true;
}

bool ExtensionPolicyService::UnregisterExtension(WebExtensionPolicy& aPolicy) {
  bool ok = (GetByID(aPolicy.Id()) == &aPolicy &&
             GetByHost(aPolicy.MozExtensionHostname()) == &aPolicy);
  MOZ_ASSERT(ok);

  if (!ok) {
    return false;
  }

  mExtensions.Remove(aPolicy.Id());

  {
    StaticAutoWriteLock lock(sEPSLock);
    sCoreByHost->Remove(aPolicy.MozExtensionHostname());
  }
  return true;
}

bool ExtensionPolicyService::RegisterObserver(DocumentObserver& aObserver) {
  bool inserted = false;
  mObservers.LookupOrInsertWith(&aObserver, [&] {
    inserted = true;
    return RefPtr{&aObserver};
  });
  return inserted;
}

bool ExtensionPolicyService::UnregisterObserver(DocumentObserver& aObserver) {
  return mObservers.Remove(&aObserver);
}

/*****************************************************************************
 * nsIMemoryReporter
 *****************************************************************************/

NS_IMETHODIMP
ExtensionPolicyService::CollectReports(nsIHandleReportCallback* aHandleReport,
                                       nsISupports* aData, bool aAnonymize) {
  for (const auto& ext : mExtensions.Values()) {
    nsAtomCString id(ext->Id());

    NS_ConvertUTF16toUTF8 name(ext->Name());
    name.ReplaceSubstring("\"", "");
    name.ReplaceSubstring("\\", "");

    nsString url;
    MOZ_TRY_VAR(url, ext->GetURL(u""_ns));

    nsPrintfCString desc("Extension(id=%s, name=\"%s\", baseURL=%s)", id.get(),
                         name.get(), NS_ConvertUTF16toUTF8(url).get());
    desc.ReplaceChar('/', '\\');

    nsCString path("extensions/");
    path.Append(desc);

    aHandleReport->Callback(""_ns, path, KIND_NONHEAP, UNITS_COUNT, 1,
                            "WebExtensions that are active in this session"_ns,
                            aData);
  }

  return NS_OK;
}

/*****************************************************************************
 * Content script management
 *****************************************************************************/

void ExtensionPolicyService::RegisterObservers() {
  mObs->AddObserver(this, kDocElementInserted, false);
  if (XRE_IsContentProcess()) {
    mObs->AddObserver(this, "http-on-opening-request", false);
    mObs->AddObserver(this, "document-on-opening-request", false);
  }

  Preferences::AddStrongObserver(this, DEFAULT_CSP_PREF);
  Preferences::AddStrongObserver(this, DEFAULT_CSP_PREF_V3);
  Preferences::AddStrongObserver(this, RESTRICTED_DOMAINS_PREF);
  Preferences::AddStrongObserver(this, QUARANTINED_DOMAINS_PREF);
  Preferences::AddStrongObserver(this, QUARANTINED_DOMAINS_ENABLED);
}

void ExtensionPolicyService::UnregisterObservers() {
  mObs->RemoveObserver(this, kDocElementInserted);
  if (XRE_IsContentProcess()) {
    mObs->RemoveObserver(this, "http-on-opening-request");
    mObs->RemoveObserver(this, "document-on-opening-request");
  }

  Preferences::RemoveObserver(this, DEFAULT_CSP_PREF);
  Preferences::RemoveObserver(this, DEFAULT_CSP_PREF_V3);
  Preferences::RemoveObserver(this, RESTRICTED_DOMAINS_PREF);
  Preferences::RemoveObserver(this, QUARANTINED_DOMAINS_PREF);
  Preferences::RemoveObserver(this, QUARANTINED_DOMAINS_ENABLED);
}

nsresult ExtensionPolicyService::Observe(nsISupports* aSubject,
                                         const char* aTopic,
                                         const char16_t* aData) {
  if (!strcmp(aTopic, kDocElementInserted)) {
    nsCOMPtr<Document> doc = do_QueryInterface(aSubject);
    if (doc) {
      CheckDocument(doc);
    }
  } else if (!strcmp(aTopic, "http-on-opening-request") ||
             !strcmp(aTopic, "document-on-opening-request")) {
    nsCOMPtr<nsIChannel> chan = do_QueryInterface(aSubject);
    if (chan) {
      CheckRequest(chan);
    }
  } else if (!strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID)) {
    const nsCString converted = NS_ConvertUTF16toUTF8(aData);
    const char* pref = converted.get();
    if (!strcmp(pref, DEFAULT_CSP_PREF)) {
      mDefaultCSP.SetIsVoid(true);
    } else if (!strcmp(pref, DEFAULT_CSP_PREF_V3)) {
      mDefaultCSPV3.SetIsVoid(true);
    } else if (!strcmp(pref, RESTRICTED_DOMAINS_PREF)) {
      UpdateRestrictedDomains();
    } else if (!strcmp(pref, QUARANTINED_DOMAINS_PREF) ||
               !strcmp(pref, QUARANTINED_DOMAINS_ENABLED)) {
      UpdateQuarantinedDomains();
    }
  }
  return NS_OK;
}

already_AddRefed<Promise> ExtensionPolicyService::ExecuteContentScript(
    nsPIDOMWindowInner* aWindow, WebExtensionContentScript& aScript) {
  if (!aWindow->IsCurrentInnerWindow()) {
    return nullptr;
  }

  RefPtr<Promise> promise;
  ProcessScript().LoadContentScript(&aScript, aWindow, getter_AddRefs(promise));
  return promise.forget();
}

RefPtr<Promise> ExtensionPolicyService::ExecuteContentScripts(
    JSContext* aCx, nsPIDOMWindowInner* aWindow,
    const nsTArray<RefPtr<WebExtensionContentScript>>& aScripts) {
  AutoTArray<RefPtr<Promise>, 8> promises;

  for (auto& script : aScripts) {
    if (RefPtr<Promise> promise = ExecuteContentScript(aWindow, *script)) {
      promises.AppendElement(std::move(promise));
    }
  }

  RefPtr<Promise> promise = Promise::All(aCx, promises, IgnoreErrors());
  MOZ_RELEASE_ASSERT(promise);
  return promise;
}

// Use browser's MessageManagerGroup to decide if we care about it, to inject
// extension APIs or content scripts.  Tabs use "browsers", and all custom
// extension browsers use "webext-browsers", including popups & sidebars,
// background & options pages, and xpcshell tests.
static bool IsTabOrExtensionBrowser(dom::BrowsingContext* aBC) {
  const auto& group = aBC->Top()->GetMessageManagerGroup();
  bool rv = group == u"browsers"_ns || group == u"webext-browsers"_ns;

#ifdef MOZ_THUNDERBIRD
  // ...unless it's Thunderbird, which has extra groups for unrelated reasons.
  rv = rv || group == u"single-site"_ns || group == u"single-page"_ns;
#endif

  return rv;
}

static nsTArray<RefPtr<dom::BrowsingContext>> GetAllInProcessContentBCs() {
  nsTArray<RefPtr<dom::BrowsingContext>> contentBCs;
  nsTArray<RefPtr<dom::BrowsingContextGroup>> groups;
  dom::BrowsingContextGroup::GetAllGroups(groups);
  for (const auto& group : groups) {
    for (const auto& toplevel : group->Toplevels()) {
      if (!toplevel->IsContent() || toplevel->IsDiscarded() ||
          !IsTabOrExtensionBrowser(toplevel)) {
        continue;
      }

      toplevel->PreOrderWalk([&](dom::BrowsingContext* aContext) {
        contentBCs.AppendElement(aContext);
      });
    }
  }
  return contentBCs;
}

nsresult ExtensionPolicyService::InjectContentScripts(
    WebExtensionPolicy* aExtension) {
  AutoJSAPI jsapi;
  MOZ_ALWAYS_TRUE(jsapi.Init(xpc::PrivilegedJunkScope()));

  auto contentBCs = GetAllInProcessContentBCs();
  for (dom::BrowsingContext* bc : contentBCs) {
    auto* win = bc->GetDOMWindow();

    if (bc->Top()->IsDiscarded() || !win || !win->GetDocumentURI()) {
      continue;
    }
    DocInfo docInfo(win);

    using RunAt = dom::ContentScriptRunAt;
    namespace RunAtValues = dom::ContentScriptRunAtValues;
    using Scripts = AutoTArray<RefPtr<WebExtensionContentScript>, 8>;

    Scripts scripts[RunAtValues::Count];

    auto GetScripts = [&](RunAt aRunAt) -> Scripts&& {
      static_assert(sizeof(aRunAt) == 1, "Our cast is wrong");
      return std::move(scripts[uint8_t(aRunAt)]);
    };

    for (const auto& script : aExtension->ContentScripts()) {
      if (script->Matches(docInfo)) {
        GetScripts(script->RunAt()).AppendElement(script);
      }
    }

    nsCOMPtr<nsPIDOMWindowInner> inner = win->GetCurrentInnerWindow();

    MOZ_TRY(ExecuteContentScripts(jsapi.cx(), inner,
                                  GetScripts(RunAt::Document_start))
                ->ThenWithCycleCollectedArgs(
                    [](JSContext* aCx, JS::Handle<JS::Value> aValue,
                       ErrorResult& aRv, ExtensionPolicyService* aSelf,
                       nsPIDOMWindowInner* aInner, Scripts&& aScripts) {
                      return aSelf->ExecuteContentScripts(aCx, aInner, aScripts)
                          .forget();
                    },
                    this, inner, GetScripts(RunAt::Document_end))
                .andThen([&](auto aPromise) {
                  return aPromise->ThenWithCycleCollectedArgs(
                      [](JSContext* aCx, JS::Handle<JS::Value> aValue,
                         ErrorResult& aRv, ExtensionPolicyService* aSelf,
                         nsPIDOMWindowInner* aInner, Scripts&& aScripts) {
                        return aSelf
                            ->ExecuteContentScripts(aCx, aInner, aScripts)
                            .forget();
                      },
                      this, inner, GetScripts(RunAt::Document_idle));
                }));
  }
  return NS_OK;
}

// Checks a request for matching content scripts, and begins pre-loading them
// if necessary.
void ExtensionPolicyService::CheckRequest(nsIChannel* aChannel) {
  nsCOMPtr<nsILoadInfo> loadInfo = aChannel->LoadInfo();
  auto loadType = loadInfo->GetExternalContentPolicyType();
  if (loadType != ExtContentPolicy::TYPE_DOCUMENT &&
      loadType != ExtContentPolicy::TYPE_SUBDOCUMENT) {
    return;
  }

  nsCOMPtr<nsIURI> uri;
  if (NS_FAILED(aChannel->GetURI(getter_AddRefs(uri)))) {
    return;
  }

  CheckContentScripts({uri.get(), loadInfo}, true);
}

static bool CheckParentFrames(nsPIDOMWindowOuter* aWindow,
                              WebExtensionPolicy& aPolicy) {
  nsCOMPtr<nsIURI> aboutAddons;
  if (NS_FAILED(NS_NewURI(getter_AddRefs(aboutAddons), "about:addons"))) {
    return false;
  }
  nsCOMPtr<nsIURI> htmlAboutAddons;
  if (NS_FAILED(
          NS_NewURI(getter_AddRefs(htmlAboutAddons),
                    "chrome://mozapps/content/extensions/aboutaddons.html"))) {
    return false;
  }

  dom::WindowContext* wc = aWindow->GetCurrentInnerWindow()->GetWindowContext();
  while ((wc = wc->GetParentWindowContext())) {
    if (!wc->IsInProcess()) {
      return false;
    }

    nsGlobalWindowInner* win = wc->GetInnerWindow();

    auto* principal = BasePrincipal::Cast(win->GetPrincipal());
    if (principal->IsSystemPrincipal()) {
      // The add-on manager is a special case, since it contains extension
      // options pages in same-type <browser> frames.
      nsIURI* uri = win->GetDocumentURI();
      bool equals;
      if ((NS_SUCCEEDED(uri->Equals(aboutAddons, &equals)) && equals) ||
          (NS_SUCCEEDED(uri->Equals(htmlAboutAddons, &equals)) && equals)) {
        return true;
      }
    }

    if (principal->AddonPolicy() != &aPolicy) {
      return false;
    }
  }

  return true;
}

// Checks a document, just after the document element has been inserted, for
// matching content scripts or extension principals, and loads them if
// necessary.
void ExtensionPolicyService::CheckDocument(Document* aDocument) {
  nsCOMPtr<nsPIDOMWindowOuter> win = aDocument->GetWindow();
  if (win) {
    if (!IsTabOrExtensionBrowser(win->GetBrowsingContext())) {
      return;
    }

    if (win->GetDocumentURI()) {
      CheckContentScripts(win.get(), false);
    }

    nsIPrincipal* principal = aDocument->NodePrincipal();

    RefPtr<WebExtensionPolicy> policy =
        BasePrincipal::Cast(principal)->AddonPolicy();
    if (policy) {
      bool privileged = IsExtensionProcess() && CheckParentFrames(win, *policy);

      ProcessScript().InitExtensionDocument(policy, aDocument, privileged);
    }
  }
}

void ExtensionPolicyService::CheckContentScripts(const DocInfo& aDocInfo,
                                                 bool aIsPreload) {
  nsCOMPtr<nsPIDOMWindowInner> win;
  if (!aIsPreload) {
    win = aDocInfo.GetWindow()->GetCurrentInnerWindow();
  }

  nsTArray<RefPtr<WebExtensionContentScript>> scriptsToLoad;

  for (RefPtr<WebExtensionPolicy> policy : mExtensions.Values()) {
    for (auto& script : policy->ContentScripts()) {
      if (script->Matches(aDocInfo)) {
        if (aIsPreload) {
          ProcessScript().PreloadContentScript(script);
        } else {
          // Collect the content scripts to load instead of loading them
          // right away (to prevent a loaded content script from being
          // able to invalidate the iterator by triggering a call to
          // policy->UnregisterContentScript while we are still iterating
          // over all its content scripts). See Bug 1593240.
          scriptsToLoad.AppendElement(script);
        }
      }
    }

    for (auto& script : scriptsToLoad) {
      if (!win->IsCurrentInnerWindow()) {
        break;
      }

      RefPtr<Promise> promise;
      ProcessScript().LoadContentScript(script, win, getter_AddRefs(promise));
    }

    scriptsToLoad.ClearAndRetainStorage();
  }

  for (RefPtr<DocumentObserver> observer : mObservers.Values()) {
    for (auto& matcher : observer->Matchers()) {
      if (matcher->Matches(aDocInfo)) {
        if (aIsPreload) {
          observer->NotifyMatch(*matcher, aDocInfo.GetLoadInfo());
        } else {
          observer->NotifyMatch(*matcher, aDocInfo.GetWindow());
        }
      }
    }
  }
}

/* static */
RefPtr<AtomSet> ExtensionPolicyService::RestrictedDomains() {
  StaticAutoReadLock lock(sEPSLock);
  return sRestrictedDomains;
}

/* static */
RefPtr<AtomSet> ExtensionPolicyService::QuarantinedDomains() {
  StaticAutoReadLock lock(sEPSLock);
  return sQuarantinedDomains;
}

void ExtensionPolicyService::UpdateRestrictedDomains() {
  nsAutoCString eltsString;
  Unused << Preferences::GetCString(RESTRICTED_DOMAINS_PREF, eltsString);

  AutoTArray<nsString, 32> elts;
  for (const nsACString& elt : eltsString.Split(',')) {
    elts.AppendElement(NS_ConvertUTF8toUTF16(elt));
    elts.LastElement().StripWhitespace();
  }
  RefPtr<AtomSet> atomSet = new AtomSet(elts);

  StaticAutoWriteLock lock(sEPSLock);
  sRestrictedDomains = atomSet;
}

void ExtensionPolicyService::UpdateQuarantinedDomains() {
  if (!GetQuarantinedDomainsEnabled()) {
    StaticAutoWriteLock lock(sEPSLock);
    sQuarantinedDomains = nullptr;
    return;
  }

  nsAutoCString eltsString;
  AutoTArray<nsString, 32> elts;
  if (NS_SUCCEEDED(
          Preferences::GetCString(QUARANTINED_DOMAINS_PREF, eltsString))) {
    for (const nsACString& elt : eltsString.Split(',')) {
      elts.AppendElement(NS_ConvertUTF8toUTF16(elt));
      elts.LastElement().StripWhitespace();
    }
  }
  RefPtr<AtomSet> atomSet = new AtomSet(elts);

  StaticAutoWriteLock lock(sEPSLock);
  sQuarantinedDomains = atomSet;
}

/*****************************************************************************
 * nsIAddonPolicyService
 *****************************************************************************/

nsresult ExtensionPolicyService::GetDefaultCSP(nsAString& aDefaultCSP) {
  if (mDefaultCSP.IsVoid()) {
    nsresult rv = Preferences::GetString(DEFAULT_CSP_PREF, mDefaultCSP);
    if (NS_FAILED(rv)) {
      mDefaultCSP.AssignLiteral(DEFAULT_DEFAULT_CSP);
    }
    mDefaultCSP.SetIsVoid(false);
  }

  aDefaultCSP.Assign(mDefaultCSP);
  return NS_OK;
}

nsresult ExtensionPolicyService::GetDefaultCSPV3(nsAString& aDefaultCSP) {
  if (mDefaultCSPV3.IsVoid()) {
    nsresult rv = Preferences::GetString(DEFAULT_CSP_PREF_V3, mDefaultCSPV3);
    if (NS_FAILED(rv)) {
      mDefaultCSPV3.AssignLiteral(DEFAULT_DEFAULT_CSP_V3);
    }
    mDefaultCSPV3.SetIsVoid(false);
  }

  aDefaultCSP.Assign(mDefaultCSPV3);
  return NS_OK;
}

nsresult ExtensionPolicyService::GetBaseCSP(const nsAString& aAddonId,
                                            nsAString& aResult) {
  if (WebExtensionPolicy* policy = GetByID(aAddonId)) {
    policy->GetBaseCSP(aResult);
    return NS_OK;
  }
  return NS_ERROR_INVALID_ARG;
}

nsresult ExtensionPolicyService::GetExtensionPageCSP(const nsAString& aAddonId,
                                                     nsAString& aResult) {
  if (WebExtensionPolicy* policy = GetByID(aAddonId)) {
    policy->GetExtensionPageCSP(aResult);
    return NS_OK;
  }
  return NS_ERROR_INVALID_ARG;
}

nsresult ExtensionPolicyService::GetGeneratedBackgroundPageUrl(
    const nsACString& aHostname, nsACString& aResult) {
  if (WebExtensionPolicy* policy = GetByHost(aHostname)) {
    nsAutoCString url("data:text/html,");

    nsCString html = policy->BackgroundPageHTML();
    nsAutoCString escaped;

    url.Append(NS_EscapeURL(html, esc_Minimal, escaped));

    aResult = url;
    return NS_OK;
  }
  return NS_ERROR_INVALID_ARG;
}

nsresult ExtensionPolicyService::AddonHasPermission(const nsAString& aAddonId,
                                                    const nsAString& aPerm,
                                                    bool* aResult) {
  if (WebExtensionPolicy* policy = GetByID(aAddonId)) {
    *aResult = policy->HasPermission(aPerm);
    return NS_OK;
  }
  return NS_ERROR_INVALID_ARG;
}

nsresult ExtensionPolicyService::AddonMayLoadURI(const nsAString& aAddonId,
                                                 nsIURI* aURI, bool aExplicit,
                                                 bool* aResult) {
  if (WebExtensionPolicy* policy = GetByID(aAddonId)) {
    *aResult = policy->CanAccessURI(aURI, aExplicit);
    return NS_OK;
  }
  return NS_ERROR_INVALID_ARG;
}

nsresult ExtensionPolicyService::GetExtensionName(const nsAString& aAddonId,
                                                  nsAString& aName) {
  if (WebExtensionPolicy* policy = GetByID(aAddonId)) {
    aName.Assign(policy->Name());
    return NS_OK;
  }
  return NS_ERROR_INVALID_ARG;
}

nsresult ExtensionPolicyService::SourceMayLoadExtensionURI(
    nsIURI* aSourceURI, nsIURI* aExtensionURI, bool* aResult) {
  URLInfo source(aSourceURI);
  URLInfo url(aExtensionURI);
  if (WebExtensionPolicy* policy = GetByURL(url)) {
    *aResult = policy->SourceMayAccessPath(source, url.FilePath());
    return NS_OK;
  }
  return NS_ERROR_INVALID_ARG;
}

nsresult ExtensionPolicyService::ExtensionURIToAddonId(nsIURI* aURI,
                                                       nsAString& aResult) {
  if (WebExtensionPolicy* policy = GetByURL(aURI)) {
    policy->GetId(aResult);
  } else {
    aResult.SetIsVoid(true);
  }
  return NS_OK;
}

NS_IMPL_CYCLE_COLLECTION(ExtensionPolicyService, mExtensions, mObservers)

NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ExtensionPolicyService)
  NS_INTERFACE_MAP_ENTRY(nsIAddonPolicyService)
  NS_INTERFACE_MAP_ENTRY(nsIObserver)
  NS_INTERFACE_MAP_ENTRY(nsIMemoryReporter)
  NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIAddonPolicyService)
NS_INTERFACE_MAP_END

NS_IMPL_CYCLE_COLLECTING_ADDREF(ExtensionPolicyService)
NS_IMPL_CYCLE_COLLECTING_RELEASE(ExtensionPolicyService)

}  // namespace mozilla