1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef mozilla_ExtensionPolicyService_h
7 #define mozilla_ExtensionPolicyService_h
9 #include "mozilla/MemoryReporting.h"
10 #include "mozilla/extensions/WebExtensionPolicy.h"
11 #include "mozIExtensionProcessScript.h"
13 #include "nsCycleCollectionParticipant.h"
14 #include "nsHashKeys.h"
15 #include "nsIAddonPolicyService.h"
17 #include "nsIMemoryReporter.h"
18 #include "nsIObserver.h"
19 #include "nsIObserverService.h"
20 #include "nsISupports.h"
21 #include "nsPointerHashKeys.h"
22 #include "nsRefPtrHashtable.h"
23 #include "nsTHashSet.h"
24 #include "nsAtomHashKeys.h"
27 class nsIObserverService
;
29 class nsIPIDOMWindowInner
;
30 class nsIPIDOMWindowOuter
;
36 namespace extensions
{
38 class DocumentObserver
;
39 class WebExtensionContentScript
;
40 } // namespace extensions
42 using extensions::DocInfo
;
43 using extensions::WebExtensionPolicy
;
45 class ExtensionPolicyService final
: public nsIAddonPolicyService
,
47 public nsIMemoryReporter
{
49 NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(ExtensionPolicyService
,
50 nsIAddonPolicyService
)
51 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
52 NS_DECL_NSIADDONPOLICYSERVICE
54 NS_DECL_NSIMEMORYREPORTER
56 static mozIExtensionProcessScript
& ProcessScript();
58 static ExtensionPolicyService
& GetSingleton();
60 // Helper for fetching an AtomSet of restricted domains as configured by the
61 // extensions.webextensions.restrictedDomains pref. Safe to call from any
63 static RefPtr
<extensions::AtomSet
> RestrictedDomains();
65 // Thread-safe AtomSet from extensions.quarantinedDomains.list.
66 static RefPtr
<extensions::AtomSet
> QuarantinedDomains();
68 static already_AddRefed
<ExtensionPolicyService
> GetInstance() {
69 return do_AddRef(&GetSingleton());
72 // Unlike the other methods on the ExtensionPolicyService, this method is
73 // threadsafe, and can look up a WebExtensionPolicyCore by hostname on any
75 static RefPtr
<extensions::WebExtensionPolicyCore
> GetCoreByHost(
76 const nsACString
& aHost
);
78 static RefPtr
<extensions::WebExtensionPolicyCore
> GetCoreByURL(
79 const extensions::URLInfo
& aURL
);
81 WebExtensionPolicy
* GetByID(nsAtom
* aAddonId
) {
82 return mExtensions
.GetWeak(aAddonId
);
85 WebExtensionPolicy
* GetByID(const nsAString
& aAddonId
) {
86 RefPtr
<nsAtom
> atom
= NS_AtomizeMainThread(aAddonId
);
90 WebExtensionPolicy
* GetByURL(const extensions::URLInfo
& aURL
);
92 WebExtensionPolicy
* GetByHost(const nsACString
& aHost
) const;
94 void GetAll(nsTArray
<RefPtr
<WebExtensionPolicy
>>& aResult
);
96 bool RegisterExtension(WebExtensionPolicy
& aPolicy
);
97 bool UnregisterExtension(WebExtensionPolicy
& aPolicy
);
99 bool RegisterObserver(extensions::DocumentObserver
& aPolicy
);
100 bool UnregisterObserver(extensions::DocumentObserver
& aPolicy
);
102 bool UseRemoteExtensions() const;
103 bool IsExtensionProcess() const;
104 bool GetQuarantinedDomainsEnabled() const;
106 nsresult
InjectContentScripts(WebExtensionPolicy
* aExtension
);
109 virtual ~ExtensionPolicyService();
112 ExtensionPolicyService();
114 void RegisterObservers();
115 void UnregisterObservers();
117 void CheckRequest(nsIChannel
* aChannel
);
118 void CheckDocument(dom::Document
* aDocument
);
120 void CheckContentScripts(const DocInfo
& aDocInfo
, bool aIsPreload
);
122 already_AddRefed
<dom::Promise
> ExecuteContentScript(
123 nsPIDOMWindowInner
* aWindow
,
124 extensions::WebExtensionContentScript
& aScript
);
126 RefPtr
<dom::Promise
> ExecuteContentScripts(
127 JSContext
* aCx
, nsPIDOMWindowInner
* aWindow
,
128 const nsTArray
<RefPtr
<extensions::WebExtensionContentScript
>>& aScripts
);
130 void UpdateRestrictedDomains();
131 void UpdateQuarantinedDomains();
133 // The WebExtensionPolicy object keeps the key alive.
134 nsRefPtrHashtable
<nsWeakAtomHashKey
, WebExtensionPolicy
> mExtensions
;
136 nsRefPtrHashtable
<nsPtrHashKey
<const extensions::DocumentObserver
>,
137 extensions::DocumentObserver
>
140 nsCOMPtr
<nsIObserverService
> mObs
;
142 nsString mDefaultCSP
;
143 nsString mDefaultCSPV3
;
146 } // namespace mozilla
148 #endif // mozilla_ExtensionPolicyService_h