Skip to content

Commit 463122d

Browse files
committed
Bug 1470651 - Support cookieStoreId option in contentScripts.register r=rpl,robwu
Differential Revision: https://phabricator.services.mozilla.com/D124537
1 parent d6767c7 commit 463122d

File tree

6 files changed

+301
-1
lines changed

6 files changed

+301
-1
lines changed

dom/chrome-webidl/WebExtensionContentScript.webidl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ interface MozDocumentMatcher {
8686
[Cached, Constant, Frozen]
8787
readonly attribute sequence<MatchGlob>? excludeGlobs;
8888

89+
/**
90+
* The originAttributesPattern for which this script should be enabled for.
91+
*/
92+
[Constant, Throws]
93+
readonly attribute any originAttributesPatterns;
94+
8995
/**
9096
* The policy object for the extension that this matcher belongs to.
9197
*/
@@ -96,6 +102,8 @@ interface MozDocumentMatcher {
96102
dictionary MozDocumentMatcherInit {
97103
boolean allFrames = false;
98104

105+
sequence<OriginAttributesPatternDictionary>? originAttributesPatterns = null;
106+
99107
boolean matchAboutBlank = false;
100108

101109
unsigned long long? frameID = null;

toolkit/components/extensions/WebExtensionContentScript.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ class MozDocumentMatcher : public nsISupports, public nsWrapperCache {
144144

145145
Nullable<uint64_t> GetFrameID() const { return mFrameID; }
146146

147+
void GetOriginAttributesPatterns(JSContext* aCx,
148+
JS::MutableHandle<JS::Value> aVal,
149+
ErrorResult& aError) const;
150+
147151
WebExtensionPolicy* GetParentObject() const { return mExtension; }
148152
virtual JSObject* WrapObject(JSContext* aCx,
149153
JS::HandleObject aGivenProto) override;
@@ -171,6 +175,7 @@ class MozDocumentMatcher : public nsISupports, public nsWrapperCache {
171175
bool mAllFrames;
172176
Nullable<uint64_t> mFrameID;
173177
bool mMatchAboutBlank;
178+
Nullable<dom::Sequence<OriginAttributesPattern>> mOriginAttributesPatterns;
174179

175180
private:
176181
template <typename T, typename U>

toolkit/components/extensions/WebExtensionPolicy.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,17 @@ MozDocumentMatcher::MozDocumentMatcher(GlobalObject& aGlobal,
667667
return;
668668
}
669669
}
670+
671+
if (!aInit.mOriginAttributesPatterns.IsNull()) {
672+
Sequence<OriginAttributesPattern>& arr =
673+
mOriginAttributesPatterns.SetValue();
674+
for (const auto& pattern : aInit.mOriginAttributesPatterns.Value()) {
675+
if (!arr.AppendElement(OriginAttributesPattern(pattern), fallible)) {
676+
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
677+
return;
678+
}
679+
}
680+
}
670681
}
671682

672683
WebExtensionContentScript::WebExtensionContentScript(
@@ -698,6 +709,21 @@ bool MozDocumentMatcher::Matches(const DocInfo& aDoc) const {
698709
return false;
699710
}
700711

712+
if (loadContext && !mOriginAttributesPatterns.IsNull()) {
713+
OriginAttributes docShellAttrs;
714+
loadContext->GetOriginAttributes(docShellAttrs);
715+
bool patternMatch = false;
716+
for (const auto& pattern : mOriginAttributesPatterns.Value()) {
717+
if (pattern.Matches(docShellAttrs)) {
718+
patternMatch = true;
719+
break;
720+
}
721+
}
722+
if (!patternMatch) {
723+
return false;
724+
}
725+
}
726+
701727
if (!mMatchAboutBlank && aDoc.URL().InheritsPrincipal()) {
702728
return false;
703729
}
@@ -760,6 +786,14 @@ bool MozDocumentMatcher::MatchesWindowGlobal(WindowGlobalChild& aWindow) const {
760786
return Matches(inner->GetOuterWindow());
761787
}
762788

789+
void MozDocumentMatcher::GetOriginAttributesPatterns(
790+
JSContext* aCx, JS::MutableHandle<JS::Value> aVal,
791+
ErrorResult& aError) const {
792+
if (!ToJSValue(aCx, mOriginAttributesPatterns, aVal)) {
793+
aError.NoteJSContextException(aCx);
794+
}
795+
}
796+
763797
JSObject* MozDocumentMatcher::WrapObject(JSContext* aCx,
764798
JS::HandleObject aGivenProto) {
765799
return MozDocumentMatcher_Binding::Wrap(aCx, this, aGivenProto);

toolkit/components/extensions/parent/ext-contentScripts.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,30 @@ var { ExtensionUtils } = ChromeUtils.import(
1515

1616
var { ExtensionError, getUniqueId } = ExtensionUtils;
1717

18+
function getOriginAttributesPatternForCookieStoreId(cookieStoreId) {
19+
if (isDefaultCookieStoreId(cookieStoreId)) {
20+
return {
21+
userContextId: Ci.nsIScriptSecurityManager.DEFAULT_USER_CONTEXT_ID,
22+
privateBrowsingId:
23+
Ci.nsIScriptSecurityManager.DEFAULT_PRIVATE_BROWSING_ID,
24+
};
25+
}
26+
if (isPrivateCookieStoreId(cookieStoreId)) {
27+
return {
28+
userContextId: Ci.nsIScriptSecurityManager.DEFAULT_USER_CONTEXT_ID,
29+
privateBrowsingId: 1,
30+
};
31+
}
32+
if (isContainerCookieStoreId(cookieStoreId)) {
33+
let userContextId = getContainerForCookieStoreId(cookieStoreId);
34+
if (userContextId !== null) {
35+
return { userContextId };
36+
}
37+
}
38+
39+
throw new ExtensionError("Invalid cookieStoreId");
40+
}
41+
1842
/**
1943
* Represents (in the main browser process) a content script registered
2044
* programmatically (instead of being included in the addon manifest).
@@ -74,8 +98,18 @@ class ContentScriptParent {
7498
runAt: details.runAt || "document_idle",
7599
jsPaths: [],
76100
cssPaths: [],
101+
originAttributesPatterns: null,
77102
};
78103

104+
if (details.cookieStoreId != null) {
105+
const cookieStoreIds = Array.isArray(details.cookieStoreId)
106+
? details.cookieStoreId
107+
: [details.cookieStoreId];
108+
options.originAttributesPatterns = cookieStoreIds.map(cookieStoreId =>
109+
getOriginAttributesPatternForCookieStoreId(cookieStoreId)
110+
);
111+
}
112+
79113
const convertCodeToURL = (data, mime) => {
80114
const blob = new context.cloneScope.Blob(data, { type: mime });
81115
const blobURL = context.cloneScope.URL.createObjectURL(blob);

toolkit/components/extensions/schemas/content_scripts.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@
5151
"$ref": "extensionTypes.RunAt",
5252
"optional": true,
5353
"description": "The soonest that the JavaScript or CSS will be injected into the tab. Defaults to \"document_idle\"."
54+
},
55+
"cookieStoreId": {
56+
"choices": [
57+
{
58+
"type": "array",
59+
"minItems": 1,
60+
"items": { "type": "string" }
61+
},
62+
{
63+
"type": "string"
64+
}
65+
],
66+
"optional": true,
67+
"description": "limit the set of matched tabs to those that belong to the given cookie store id"
5468
}
5569
}
5670
},

0 commit comments

Comments
 (0)