diff --git a/caps/BasePrincipal.cpp b/caps/BasePrincipal.cpp index ae328a8de84b..93a39fbfd544 100644 --- a/caps/BasePrincipal.cpp +++ b/caps/BasePrincipal.cpp @@ -115,6 +115,12 @@ OriginAttributes::PopulateFromSuffix(const nsACString& aStr) return usp->ForEach(iterator); } +void +OriginAttributes::CookieJar(nsACString& aStr) +{ + mozilla::GetJarPrefix(mAppId, mInBrowser, aStr); +} + BasePrincipal::BasePrincipal() {} @@ -223,7 +229,7 @@ BasePrincipal::GetJarPrefix(nsACString& aJarPrefix) { MOZ_ASSERT(AppId() != nsIScriptSecurityManager::UNKNOWN_APP_ID); - mozilla::GetJarPrefix(AppId(), IsInBrowserElement(), aJarPrefix); + mOriginAttributes.CookieJar(aJarPrefix); return NS_OK; } @@ -246,10 +252,8 @@ BasePrincipal::GetOriginSuffix(nsACString& aOriginAttributes) NS_IMETHODIMP BasePrincipal::GetCookieJar(nsACString& aCookieJar) { - // We just forward to .jarPrefix for now, which is a nice compact - // stringification of the (appId, inBrowser) tuple. This will eventaully be - // swapped out for an origin attribute - see the comment in nsIPrincipal.idl. - return GetJarPrefix(aCookieJar); + mOriginAttributes.CookieJar(aCookieJar); + return NS_OK; } NS_IMETHODIMP diff --git a/caps/BasePrincipal.h b/caps/BasePrincipal.h index 377869b55264..feb4aa9db127 100644 --- a/caps/BasePrincipal.h +++ b/caps/BasePrincipal.h @@ -28,6 +28,8 @@ public: mAppId = aAppId; mInBrowser = aInBrowser; } + explicit OriginAttributes(const OriginAttributesDictionary& aOther) + : OriginAttributesDictionary(aOther) {} bool operator==(const OriginAttributes& aOther) const { @@ -44,6 +46,8 @@ public: // returns an empty string. void CreateSuffix(nsACString& aStr) const; bool PopulateFromSuffix(const nsACString& aStr); + + void CookieJar(nsACString& aStr); }; /* diff --git a/dom/webidl/ChromeUtils.webidl b/dom/webidl/ChromeUtils.webidl index 11683eb76a74..64cab34a1555 100644 --- a/dom/webidl/ChromeUtils.webidl +++ b/dom/webidl/ChromeUtils.webidl @@ -28,6 +28,15 @@ interface ChromeUtils { */ [Throws, NewObject] static HeapSnapshot readHeapSnapshot(DOMString filePath); + + /** + * A helper that converts OriginAttributesDictionary to cookie jar opaque + * identfier. + * + * @param originAttrs The originAttributes from the caller. + */ + static ByteString + originAttributesToCookieJar(optional OriginAttributesDictionary originAttrs); }; /** diff --git a/toolkit/devtools/server/ChromeUtils.cpp b/toolkit/devtools/server/ChromeUtils.cpp index 4e5968356422..b2eb58c9c0f0 100644 --- a/toolkit/devtools/server/ChromeUtils.cpp +++ b/toolkit/devtools/server/ChromeUtils.cpp @@ -11,6 +11,7 @@ #include "mozilla/devtools/HeapSnapshot.h" #include "mozilla/devtools/ZeroCopyNSIOutputStream.h" #include "mozilla/Attributes.h" +#include "mozilla/BasePrincipal.h" #include "mozilla/UniquePtr.h" #include "nsCRTGlue.h" @@ -428,5 +429,14 @@ ChromeUtils::ReadHeapSnapshot(GlobalObject& global, return HeapSnapshot::Create(cx, global, buffer.get(), size, rv); } +/* static */ void +ChromeUtils::OriginAttributesToCookieJar(GlobalObject& aGlobal, + const OriginAttributesDictionary& aAttrs, + nsCString& aCookieJar) +{ + OriginAttributes attrs(aAttrs); + attrs.CookieJar(aCookieJar); +} + } // namespace devtools } // namespace mozilla diff --git a/toolkit/devtools/server/ChromeUtils.h b/toolkit/devtools/server/ChromeUtils.h index 047194b3d3d3..ef4e2180bc92 100644 --- a/toolkit/devtools/server/ChromeUtils.h +++ b/toolkit/devtools/server/ChromeUtils.h @@ -73,6 +73,11 @@ public: JSContext* cx, const nsAString& filePath, ErrorResult& rv); + + static void + OriginAttributesToCookieJar(dom::GlobalObject& aGlobal, + const dom::OriginAttributesDictionary& aAttrs, + nsCString& aCookieJar); }; } // namespace devtools diff --git a/toolkit/devtools/server/tests/unit/test_originAttributesToCookieJar.js b/toolkit/devtools/server/tests/unit/test_originAttributesToCookieJar.js new file mode 100644 index 000000000000..e46057075906 --- /dev/null +++ b/toolkit/devtools/server/tests/unit/test_originAttributesToCookieJar.js @@ -0,0 +1,26 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +Cu.import("resource://gre/modules/Services.jsm"); +var ssm = Services.scriptSecurityManager; + +function run_test() { + const appId = 12; + var browserAttrs = {appId: appId, inBrowser: true}; + + // ChromeUtils.originAttributesToCookieJar should return the same value with + // the cookieJar of the principal created from the same origin attribute. + var cookieJar_1 = ChromeUtils.originAttributesToCookieJar(browserAttrs); + var dummy = Services.io.newURI("http://example.com", null, null); + var cookieJar_2 = ssm.createCodebasePrincipal(dummy, browserAttrs).cookieJar; + do_check_eq(cookieJar_1, cookieJar_2); + + // App and mozbrowser shouldn't have the same cookieJar identifier. + var appAttrs = {appId: appId, inBrowser: false}; + var cookieJar_3 = ChromeUtils.originAttributesToCookieJar(appAttrs); + do_check_neq(cookieJar_1, cookieJar_3); + + // If the attribute is null the cookieJar identifier should be empty. + var cookieJar_4 = ChromeUtils.originAttributesToCookieJar(); + do_check_eq(cookieJar_4, ""); +} diff --git a/toolkit/devtools/server/tests/unit/xpcshell.ini b/toolkit/devtools/server/tests/unit/xpcshell.ini index 9e92fa92c5da..2bb264090192 100644 --- a/toolkit/devtools/server/tests/unit/xpcshell.ini +++ b/toolkit/devtools/server/tests/unit/xpcshell.ini @@ -80,6 +80,7 @@ support-files = [test_eval-03.js] [test_eval-04.js] [test_eval-05.js] +[test_originAttributesToCookieJar.js] [test_promises_actor_attach.js] [test_promises_actor_exist.js] [test_promises_actor_list_promises.js]