Bug 1211665 - Save originAttributes in the console event messages. r=baku

Save the originAttributes of the current principal into the console events,
so that we can filter the console messages by originAttributes (e.g.
WebExtensions console events can be filtered using the addonId that the
ExtensionPages' originAttributes contain.

MozReview-Commit-ID: 5v9BWLbgskd

--HG--
extra : transplant_source : %2C%A1%D4%8AEp%E6G%8A%2C%DA%A8%EE%B3%EA%0E%84xk%FF
This commit is contained in:
Luca Greco 2016-04-01 13:55:40 +02:00
parent c0ccb93e52
commit 2a28dabb62
4 changed files with 134 additions and 0 deletions

View File

@ -149,6 +149,12 @@ public:
mIDType = eString;
}
void
SetOriginAttributes(const PrincipalOriginAttributes& aOriginAttributes)
{
mOriginAttributes = aOriginAttributes;
}
bool
PopulateArgumentsSequence(Sequence<JS::Value>& aSequence) const
{
@ -241,6 +247,8 @@ public:
uint64_t mInnerIDNumber;
nsString mInnerIDString;
PrincipalOriginAttributes mOriginAttributes;
nsString mMethodString;
// Stack management is complicated, because we want to do it as
@ -614,6 +622,20 @@ private:
if (aOuterWindow) {
mCallData->SetIDs(aOuterWindow->WindowID(), aInnerWindow->WindowID());
// Save the principal's OriginAttributes in the console event data
// so that we will be able to filter messages by origin attributes.
nsCOMPtr<nsIScriptObjectPrincipal> sop = do_QueryInterface(aInnerWindow);
if (NS_WARN_IF(!sop)) {
return;
}
nsCOMPtr<nsIPrincipal> principal = sop->GetPrincipal();
if (NS_WARN_IF(!principal)) {
return;
}
mCallData->SetOriginAttributes(BasePrincipal::Cast(principal)->OriginAttributesRef());
} else {
ConsoleStackEntry frame;
if (mCallData->mTopStackFrame) {
@ -634,6 +656,15 @@ private:
}
mCallData->SetIDs(id, innerID);
// Save the principal's OriginAttributes in the console event data
// so that we will be able to filter messages by origin attributes.
nsCOMPtr<nsIPrincipal> principal = mWorkerPrivate->GetPrincipal();
if (NS_WARN_IF(!principal)) {
return;
}
mCallData->SetOriginAttributes(BasePrincipal::Cast(principal)->OriginAttributesRef());
}
// Now we could have the correct window (if we are not window-less).
@ -1253,6 +1284,20 @@ Console::Method(JSContext* aCx, MethodName aMethodName,
MOZ_ASSERT(loadContext);
loadContext->GetUsePrivateBrowsing(&callData->mPrivate);
// Save the principal's OriginAttributes in the console event data
// so that we will be able to filter messages by origin attributes.
nsCOMPtr<nsIScriptObjectPrincipal> sop = do_QueryInterface(mWindow);
if (NS_WARN_IF(!sop)) {
return;
}
nsCOMPtr<nsIPrincipal> principal = sop->GetPrincipal();
if (NS_WARN_IF(!principal)) {
return;
}
callData->SetOriginAttributes(BasePrincipal::Cast(principal)->OriginAttributesRef());
}
uint32_t maxDepth = ShouldIncludeStackTrace(aMethodName) ?
@ -1505,6 +1550,13 @@ Console::PopulateConsoleNotificationInTheTargetScope(JSContext* aCx,
ClearException ce(aCx);
RootedDictionary<ConsoleEvent> event(aCx);
// Save the principal's OriginAttributes in the console event data
// so that we will be able to filter messages by origin attributes.
JS::Rooted<JS::Value> originAttributesValue(aCx);
if (ToJSValue(aCx, aData->mOriginAttributes, &originAttributesValue)) {
event.mOriginAttributes = originAttributesValue;
}
event.mID.Construct();
event.mInnerID.Construct();

View File

@ -17,6 +17,7 @@ skip-if = buildapp == 'mulet'
skip-if = e10s
[browser_ConsoleAPITests.js]
skip-if = e10s
[browser_ConsoleAPI_originAttributes.js]
[browser_ConsoleStorageAPITests.js]
skip-if = e10s
[browser_ConsoleStoragePBTest_perwindowpb.js]

View File

@ -0,0 +1,80 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
const ConsoleAPIStorage = Cc["@mozilla.org/consoleAPI-storage;1"]
.getService(Ci.nsIConsoleAPIStorage);
const FAKE_ADDON_ID = "test-webext-addon@mozilla.org";
const EXPECTED_CONSOLE_ID = `addon/${FAKE_ADDON_ID}`;
const EXPECTED_CONSOLE_MESSAGE_CONTENT = "fake-webext-addon-test-log-message";
const ConsoleObserver = {
QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver]),
init() {
Services.obs.addObserver(this, "console-api-log-event", false);
},
uninit() {
Services.obs.removeObserver(this, "console-api-log-event", false);
},
observe(aSubject, aTopic, aData) {
if (aTopic == "console-api-log-event") {
let consoleAPIMessage = aSubject.wrappedJSObject;
is(consoleAPIMessage.arguments[0], EXPECTED_CONSOLE_MESSAGE_CONTENT,
"the consoleAPIMessage contains the expected message");
ok(consoleAPIMessage.originAttributes, "the consoleAPImessage contains originattributes");
is(consoleAPIMessage.originAttributes.addonId, FAKE_ADDON_ID,
"the consoleAPImessage's originAttributes contains the expected addonId");
let cachedMessages = ConsoleAPIStorage.getEvents();
is(cachedMessages.length, 1, "found one console api messsage as expected");
ok(cachedMessages[0].originAttributes, "the consoleAPImessage contains originattributes");
is(cachedMessages[0].originAttributes.addonId, FAKE_ADDON_ID,
"the consoleAPImessage's originAttributes contains the expected addonId");
finish();
}
}
};
function test()
{
ConsoleObserver.init();
waitForExplicitFinish();
let uuidGenerator = Cc["@mozilla.org/uuid-generator;1"].getService(Ci.nsIUUIDGenerator);
let uuid = uuidGenerator.generateUUID().number;
uuid = uuid.slice(1, -1); // Strip { and } off the UUID.
let baseURI = Services.io.newURI("about:blank", null, null);
let originAttributes = {addonId: FAKE_ADDON_ID};
let principal = Services.scriptSecurityManager
.createCodebasePrincipal(baseURI, originAttributes);
let chromeWebNav = Services.appShell.createWindowlessBrowser(true);
let interfaceRequestor = chromeWebNav.QueryInterface(Ci.nsIInterfaceRequestor);
let docShell = interfaceRequestor.getInterface(Ci.nsIDocShell);
docShell.createAboutBlankContentViewer(principal);
info("fake webextension docShell created");
registerCleanupFunction(function() {
if (chromeWebNav) {
chromeWebNav.close();
chromeWebNav = null;
}
ConsoleObserver.uninit();
});
let window = docShell.contentViewer.DOMDocument.defaultView;
window.eval(`console.log("${EXPECTED_CONSOLE_MESSAGE_CONTENT}");`);
chromeWebNav.close();
chromeWebNav = null;
info("fake webextension page logged a console api message");
}

View File

@ -45,6 +45,7 @@ interface Console {
dictionary ConsoleEvent {
(unsigned long long or DOMString) ID;
(unsigned long long or DOMString) innerID;
any originAttributes = null;
DOMString level = "";
DOMString filename = "";
unsigned long lineNumber = 0;