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"
26 class nsIObserverService
;
28 class nsIPIDOMWindowInner
;
29 class nsIPIDOMWindowOuter
;
35 namespace extensions
{
37 class DocumentObserver
;
38 class WebExtensionContentScript
;
39 } // namespace extensions
41 using extensions::DocInfo
;
42 using extensions::WebExtensionPolicy
;
44 class ExtensionPolicyService final
: public nsIAddonPolicyService
,
46 public nsIMemoryReporter
{
48 NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(ExtensionPolicyService
,
49 nsIAddonPolicyService
)
50 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
51 NS_DECL_NSIADDONPOLICYSERVICE
53 NS_DECL_NSIMEMORYREPORTER
55 static mozIExtensionProcessScript
& ProcessScript();
57 static ExtensionPolicyService
& GetSingleton();
59 // Heper for fetching an AtomSet of restricted domains as configured by the
60 // extensions.webextensions.restrictedDomains pref. Safe to call from any
62 static RefPtr
<extensions::AtomSet
> RestrictedDomains();
64 static already_AddRefed
<ExtensionPolicyService
> GetInstance() {
65 return do_AddRef(&GetSingleton());
68 // Unlike the other methods on the ExtensionPolicyService, this method is
69 // threadsafe, and can look up a WebExtensionPolicyCore by hostname on any
71 static RefPtr
<extensions::WebExtensionPolicyCore
> GetCoreByHost(
72 const nsACString
& aHost
);
74 WebExtensionPolicy
* GetByID(const nsAtom
* aAddonId
) {
75 return mExtensions
.GetWeak(aAddonId
);
78 WebExtensionPolicy
* GetByID(const nsAString
& aAddonId
) {
79 RefPtr
<nsAtom
> atom
= NS_AtomizeMainThread(aAddonId
);
83 WebExtensionPolicy
* GetByURL(const extensions::URLInfo
& aURL
);
85 WebExtensionPolicy
* GetByHost(const nsACString
& aHost
) const;
87 void GetAll(nsTArray
<RefPtr
<WebExtensionPolicy
>>& aResult
);
89 bool RegisterExtension(WebExtensionPolicy
& aPolicy
);
90 bool UnregisterExtension(WebExtensionPolicy
& aPolicy
);
92 bool RegisterObserver(extensions::DocumentObserver
& aPolicy
);
93 bool UnregisterObserver(extensions::DocumentObserver
& aPolicy
);
95 bool UseRemoteExtensions() const;
96 bool IsExtensionProcess() const;
98 nsresult
InjectContentScripts(WebExtensionPolicy
* aExtension
);
101 virtual ~ExtensionPolicyService();
104 ExtensionPolicyService();
106 void RegisterObservers();
107 void UnregisterObservers();
109 void CheckRequest(nsIChannel
* aChannel
);
110 void CheckDocument(dom::Document
* aDocument
);
112 void CheckContentScripts(const DocInfo
& aDocInfo
, bool aIsPreload
);
114 already_AddRefed
<dom::Promise
> ExecuteContentScript(
115 nsPIDOMWindowInner
* aWindow
,
116 extensions::WebExtensionContentScript
& aScript
);
118 RefPtr
<dom::Promise
> ExecuteContentScripts(
119 JSContext
* aCx
, nsPIDOMWindowInner
* aWindow
,
120 const nsTArray
<RefPtr
<extensions::WebExtensionContentScript
>>& aScripts
);
122 void UpdateRestrictedDomains();
124 nsRefPtrHashtable
<nsPtrHashKey
<const nsAtom
>, WebExtensionPolicy
> mExtensions
;
126 nsRefPtrHashtable
<nsPtrHashKey
<const extensions::DocumentObserver
>,
127 extensions::DocumentObserver
>
130 nsCOMPtr
<nsIObserverService
> mObs
;
132 nsString mDefaultCSP
;
133 nsString mDefaultCSPV3
;
136 } // namespace mozilla
138 #endif // mozilla_ExtensionPolicyService_h