Bug 1170097 - Part 2: Add originAttributesToCookieJar. r=bholley
authorYoshi Huang <allstars.chh@mozilla.com>
Wed, 03 Jun 2015 14:38:55 +0800
changeset 279200 3119d57ebf1993696f824a405426a3d039022ec2
parent 279199 e0c3fc205ceb4a7213aed5740c40189ae2c98858
child 279201 55698478f5ba7a572e09104b7688e0c4b76dbb3b
push id4932
push userjlund@mozilla.com
push dateMon, 10 Aug 2015 18:23:06 +0000
treeherdermozilla-beta@6dd5a4f5f745 [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 2: 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
@@ -110,16 +110,22 @@ OriginAttributes::PopulateFromSuffix(con
 
   nsRefPtr<URLSearchParams> usp = new URLSearchParams();
   usp->ParseInput(Substring(aStr, 1, aStr.Length() - 1), nullptr);
 
   PopulateFromSuffixIterator iterator(this);
   return usp->ForEach(iterator);
 }
 
+void
+OriginAttributes::CookieJar(nsACString& aStr)
+{
+  mozilla::GetJarPrefix(mAppId, mInBrowser, aStr);
+}
+
 BasePrincipal::BasePrincipal()
 {}
 
 BasePrincipal::~BasePrincipal()
 {}
 
 NS_IMETHODIMP
 BasePrincipal::GetOrigin(nsACString& aOrigin)
@@ -218,17 +224,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;
@@ -241,20 +247,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
@@ -23,32 +23,36 @@ class OriginAttributes : public dom::Ori
 {
 public:
   OriginAttributes() {}
   OriginAttributes(uint32_t aAppId, bool aInBrowser)
   {
     mAppId = aAppId;
     mInBrowser = aInBrowser;
   }
+  explicit 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/Deserializes 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;
   bool PopulateFromSuffix(const nsACString& aStr);
+
+  void CookieJar(nsACString& aStr);
 };
 
 /*
  * Base class from which all nsIPrincipal implementations inherit. Use this for
  * default implementations and other commonalities between principal
  * implementations.
  *
  * We should merge nsJSPrincipals into this class at some point.
--- 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
@@ -75,16 +75,17 @@ support-files =
 [test_frameclient-02.js]
 [test_nativewrappers.js]
 [test_nodelistactor.js]
 [test_eval-01.js]
 [test_eval-02.js]
 [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]
 [test_promises_actor_onnewpromise.js]
 [test_promises_actor_onpromisesettled.js]
 [test_promises_client_getdependentpromises.js]
 [test_promises_object_creationtimestamp.js]
 [test_promises_object_timetosettle-01.js]