Bug 1170097 - Part 3: Add originAttributesToCookieJar. r=bholley
☠☠ backed out by e9285dba7ba6 ☠ ☠
authorYoshi Huang <allstars.chh@mozilla.com>
Wed, 03 Jun 2015 14:38:55 +0800
changeset 279997 78ef803f4f11e58365f16bbf88198fecf1b82fb0
parent 279996 c9c602344f561c51ebb71ef835b2a41c15b010a7
child 279998 14656f887ee0eadbedcdd6beafe8b83f306cd57e
push id897
push userjlund@mozilla.com
push dateMon, 14 Sep 2015 18:56:12 +0000
treeherdermozilla-release@9411e2d2b214 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbholley
bugs1170097
milestone41.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1170097 - Part 3: Add originAttributesToCookieJar. r=bholley
caps/BasePrincipal.cpp
caps/BasePrincipal.h
dom/webidl/ChromeUtils.webidl
toolkit/devtools/server/ChromeUtils.cpp
toolkit/devtools/server/ChromeUtils.h
toolkit/devtools/server/tests/unit/test_originAttributesToCookieJar.js
toolkit/devtools/server/tests/unit/xpcshell.ini
--- a/caps/BasePrincipal.cpp
+++ b/caps/BasePrincipal.cpp
@@ -33,16 +33,22 @@ OriginAttributes::CreateSuffix(nsACStrin
   }
 
   if (mInBrowser) {
     aStr.Append(attrCount++ ? "&inBrowser=1" : "!inBrowser=1");
   }
 }
 
 void
+OriginAttributes::CookieJar(nsACString& aStr)
+{
+  mozilla::GetJarPrefix(mAppId, mInBrowser, aStr);
+}
+
+void
 OriginAttributes::Serialize(nsIObjectOutputStream* aStream) const
 {
   aStream->Write32(mAppId);
   aStream->WriteBoolean(mInBrowser);
 }
 
 nsresult
 OriginAttributes::Deserialize(nsIObjectInputStream* aStream)
@@ -159,17 +165,17 @@ BasePrincipal::GetIsNullPrincipal(bool* 
   return NS_OK;
 }
 
 NS_IMETHODIMP
 BasePrincipal::GetJarPrefix(nsACString& aJarPrefix)
 {
   MOZ_ASSERT(AppId() != nsIScriptSecurityManager::UNKNOWN_APP_ID);
 
-  mozilla::GetJarPrefix(AppId(), IsInBrowserElement(), aJarPrefix);
+  mOriginAttributes.CookieJar(aJarPrefix);
   return NS_OK;
 }
 
 NS_IMETHODIMP
 BasePrincipal::GetOriginAttributes(JSContext* aCx, JS::MutableHandle<JS::Value> aVal)
 {
   if (NS_WARN_IF(!ToJSValue(aCx, mOriginAttributes, aVal))) {
     return NS_ERROR_FAILURE;
@@ -182,20 +188,18 @@ BasePrincipal::GetOriginSuffix(nsACStrin
 {
   mOriginAttributes.CreateSuffix(aOriginAttributes);
   return NS_OK;
 }
 
 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
 BasePrincipal::GetAppStatus(uint16_t* aAppStatus)
 {
   if (AppId() == nsIScriptSecurityManager::UNKNOWN_APP_ID) {
     NS_WARNING("Asking for app status on a principal with an unknown app id");
     *aAppStatus = nsIPrincipal::APP_STATUS_NOT_INSTALLED;
--- a/caps/BasePrincipal.h
+++ b/caps/BasePrincipal.h
@@ -22,32 +22,36 @@ class OriginAttributes : public dom::Ori
 {
 public:
   OriginAttributes() {}
   OriginAttributes(uint32_t aAppId, bool aInBrowser)
   {
     mAppId = aAppId;
     mInBrowser = aInBrowser;
   }
+  OriginAttributes(const OriginAttributesDictionary& aOther)
+    : OriginAttributesDictionary(aOther) {}
 
   bool operator==(const OriginAttributes& aOther) const
   {
     return mAppId == aOther.mAppId &&
            mInBrowser == aOther.mInBrowser;
   }
   bool operator!=(const OriginAttributes& aOther) const
   {
     return !(*this == aOther);
   }
 
   // Serializes non-default values into the suffix format, i.e.
   // |!key1=value1&key2=value2|. If there are no non-default attributes, this
   // returns an empty string.
   void CreateSuffix(nsACString& aStr) const;
 
+  void CookieJar(nsACString& aStr);
+
   void Serialize(nsIObjectOutputStream* aStream) const;
   nsresult Deserialize(nsIObjectInputStream* aStream);
 };
 
 /*
  * Base class from which all nsIPrincipal implementations inherit. Use this for
  * default implementations and other commonalities between principal
  * implementations.
--- a/dom/webidl/ChromeUtils.webidl
+++ b/dom/webidl/ChromeUtils.webidl
@@ -23,16 +23,25 @@ interface ChromeUtils {
 
   /**
    * Deserialize a core dump into a HeapSnapshot.
    *
    * @param filePath          The file path to read the core dump from.
    */
   [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);
 };
 
 /**
  * A JS object whose properties specify what portion of the heap graph to
  * write. The recognized properties are:
  *
  * * globals: [ global, ... ]
  *   Dump only nodes that either:
--- a/toolkit/devtools/server/ChromeUtils.cpp
+++ b/toolkit/devtools/server/ChromeUtils.cpp
@@ -6,16 +6,17 @@
 #include "ChromeUtils.h"
 
 #include <google/protobuf/io/coded_stream.h>
 #include <google/protobuf/io/gzip_stream.h>
 
 #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"
 #include "nsIOutputStream.h"
 #include "nsNetUtil.h"
 #include "prerror.h"
 #include "prio.h"
 #include "prtypes.h"
@@ -423,10 +424,19 @@ ChromeUtils::ReadHeapSnapshot(GlobalObje
       return nullptr;
     }
     bytesRead += bytesReadThisTime;
   }
 
   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
--- a/toolkit/devtools/server/ChromeUtils.h
+++ b/toolkit/devtools/server/ChromeUtils.h
@@ -68,14 +68,19 @@ public:
                                const nsAString& filePath,
                                const dom::HeapSnapshotBoundaries& boundaries,
                                ErrorResult& rv);
 
   static already_AddRefed<HeapSnapshot> ReadHeapSnapshot(dom::GlobalObject& global,
                                                          JSContext* cx,
                                                          const nsAString& filePath,
                                                          ErrorResult& rv);
+
+  static void
+  OriginAttributesToCookieJar(dom::GlobalObject& aGlobal,
+                              const dom::OriginAttributesDictionary& aAttrs,
+                              nsCString& aCookieJar);
 };
 
 } // namespace devtools
 } // namespace mozilla
 
 #endif // mozilla_devtools_ChromeUtils__
new file mode 100644
--- /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, "");
+}
--- a/toolkit/devtools/server/tests/unit/xpcshell.ini
+++ b/toolkit/devtools/server/tests/unit/xpcshell.ini
@@ -80,16 +80,17 @@ support-files =
 [test_eval-03.js]
 [test_eval-04.js]
 [test_eval-05.js]
 [test_promises_actor_attach.js]
 [test_promises_actor_exist.js]
 [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]
 [test_protocol_formtype.js]
 [test_protocol_longstring.js]
 [test_protocol_simple.js]
 [test_protocol_unregister.js]
 [test_breakpoint-01.js]