Bug 1170097 - Part 2: Add originAttributesToCookieJar. r=bholley

This commit is contained in:
Yoshi Huang 2015-06-03 14:38:55 +08:00
parent 9a1d2d2925
commit be2c3de92d
7 changed files with 64 additions and 5 deletions

View File

@ -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

View File

@ -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);
};
/*

View File

@ -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);
};
/**

View File

@ -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

View File

@ -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

View File

@ -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, "");
}

View File

@ -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]