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

This commit is contained in:
Yoshi Huang 2015-06-03 14:38:55 +08:00
Родитель eae6056f52
Коммит e3ed4b8f44
7 изменённых файлов: 64 добавлений и 5 удалений

Просмотреть файл

@ -37,6 +37,12 @@ OriginAttributes::CreateSuffix(nsACString& aStr) const
}
}
void
OriginAttributes::CookieJar(nsACString& aStr)
{
mozilla::GetJarPrefix(mAppId, mInBrowser, aStr);
}
void
OriginAttributes::Serialize(nsIObjectOutputStream* aStream) const
{
@ -164,7 +170,7 @@ BasePrincipal::GetJarPrefix(nsACString& aJarPrefix)
{
MOZ_ASSERT(AppId() != nsIScriptSecurityManager::UNKNOWN_APP_ID);
mozilla::GetJarPrefix(AppId(), IsInBrowserElement(), aJarPrefix);
mOriginAttributes.CookieJar(aJarPrefix);
return NS_OK;
}
@ -187,10 +193,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

Просмотреть файл

@ -27,6 +27,8 @@ public:
mAppId = aAppId;
mInBrowser = aInBrowser;
}
OriginAttributes(const OriginAttributesDictionary& aOther)
: OriginAttributesDictionary(aOther) {}
bool operator==(const OriginAttributes& aOther) const
{
@ -43,6 +45,8 @@ public:
// returns an empty string.
void CreateSuffix(nsACString& aStr) const;
void CookieJar(nsACString& aStr);
void Serialize(nsIObjectOutputStream* aStream) const;
nsresult Deserialize(nsIObjectInputStream* aStream);
};

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

@ -85,6 +85,7 @@ support-files =
[test_promises_actor_list_promises.js]
[test_promises_actor_onnewpromise.js]
[test_promises_actor_onpromisesettled.js]
[test_originAttributesToCookieJar.js]
[test_protocol_abort.js]
[test_protocol_async.js]
[test_protocol_children.js]