Bug 1383293 - Adding a new readonly attribute principal in nsIPaymentRequest for UI support.
authorEden Chuang <echuang@mozilla.com>
Tue, 22 Aug 2017 11:21:16 +0800
changeset 1267013 336af8bec272ac70b74bcf1934ac8d4b66931ead
parent 1266813 128a79130ecd6f277190d031a623f991c73c5272
child 1267014 7d5a23f3e317e9dd4acf553c469f45e2e51d5a89
push id214928
push userechuang@mozilla.com
push dateTue, 22 Aug 2017 03:22:06 +0000
treeherdertry@12d2cd808704 [default view] [failures only]
bugs1383293
milestone57.0a1
Bug 1383293 - Adding a new readonly attribute principal in nsIPaymentRequest for UI support. Get top level principal when PaymentRequest constructor is called. Then send it to chrome process and saved it in nsIPaymentRequest. A new readonly attribute nsIPrincipal principal in nsIPaymentRequest for UI to query the top level principal information.
dom/interfaces/payments/nsIPaymentActionRequest.idl
dom/interfaces/payments/nsIPaymentRequest.idl
dom/payments/PaymentActionRequest.cpp
dom/payments/PaymentActionRequest.h
dom/payments/PaymentRequest.cpp
dom/payments/PaymentRequestData.cpp
dom/payments/PaymentRequestData.h
dom/payments/PaymentRequestManager.cpp
dom/payments/PaymentRequestManager.h
dom/payments/PaymentRequestService.cpp
dom/payments/ipc/PPaymentRequest.ipdl
dom/payments/ipc/PaymentRequestParent.cpp
--- a/dom/interfaces/payments/nsIPaymentActionRequest.idl
+++ b/dom/interfaces/payments/nsIPaymentActionRequest.idl
@@ -57,16 +57,21 @@ interface nsIPaymentActionRequest : nsIS
 interface nsIPaymentCreateActionRequest : nsIPaymentActionRequest
 {
   /*
    *  The tab identifier
    */
   readonly attribute uint64_t tabId;
 
   /*
+   *  The triggering principal
+   */
+  readonly attribute nsIPrincipal principal;
+
+  /*
    *  The methodData information of the payment request.
    */
   readonly attribute nsIArray methodData;
 
   /*
    *  The Details information of the payment request.
    */
   readonly attribute nsIPaymentDetails details;
@@ -77,16 +82,17 @@ interface nsIPaymentCreateActionRequest 
   readonly attribute nsIPaymentOptions options;
 
   /*
    *  Initialize function the this request.
    */
   void initRequest(in AString aRequestId,
                    in nsIPaymentActionCallback aCallback,
                    in uint64_t aTabId,
+                   in nsIPrincipal aPrincipal,
                    in nsIArray aMethodData,
                    in nsIPaymentDetails aDetails,
                    in nsIPaymentOptions aOptions);
 };
 
 [builtinclass, uuid(4429697d-1135-47de-a46e-5196d399ec55)]
 interface nsIPaymentCompleteActionRequest : nsIPaymentActionRequest
 {
--- a/dom/interfaces/payments/nsIPaymentRequest.idl
+++ b/dom/interfaces/payments/nsIPaymentRequest.idl
@@ -1,15 +1,16 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "nsISupports.idl"
 #include "nsIVariant.idl"
+#include "nsIPrincipal.idl"
 
 interface nsIArray;
 
 [scriptable, builtinclass, uuid(2fe296cc-d917-4820-b492-aa42df23f9b4)]
 interface nsIPaymentMethodData : nsISupports
 {
   readonly attribute AString supportedMethods;
   [implicit_jscontext]
@@ -72,15 +73,16 @@ interface nsIPaymentOptions : nsISupport
   readonly attribute boolean requestShipping;
   readonly attribute AString shippingType;
 };
 
 [scriptable, builtinclass, uuid(2fa36783-d684-4487-b7a8-9def6ae3128f)]
 interface nsIPaymentRequest : nsISupports
 {
   readonly attribute uint64_t tabId;
+  readonly attribute nsIPrincipal principal;
   readonly attribute AString requestId;
   readonly attribute nsIArray paymentMethods;
   readonly attribute nsIPaymentDetails paymentDetails;
   readonly attribute nsIPaymentOptions paymentOptions;
 
   void updatePaymentDetails(in nsIPaymentDetails aDetails);
 };
--- a/dom/payments/PaymentActionRequest.cpp
+++ b/dom/payments/PaymentActionRequest.cpp
@@ -69,44 +69,57 @@ PaymentCreateActionRequest::PaymentCreat
   : mTabId(0)
 {
 }
 
 NS_IMETHODIMP
 PaymentCreateActionRequest::InitRequest(const nsAString& aRequestId,
                                         nsIPaymentActionCallback* aCallback,
                                         const uint64_t aTabId,
+                                        nsIPrincipal* aPrincipal,
                                         nsIArray* aMethodData,
                                         nsIPaymentDetails* aDetails,
                                         nsIPaymentOptions* aOptions)
 {
   NS_ENSURE_ARG_POINTER(aCallback);
+  NS_ENSURE_ARG_POINTER(aPrincipal);
   NS_ENSURE_ARG_POINTER(aMethodData);
   NS_ENSURE_ARG_POINTER(aDetails);
   NS_ENSURE_ARG_POINTER(aOptions);
   nsresult rv = Init(aRequestId, nsIPaymentActionRequest::CREATE_ACTION, aCallback);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
   mTabId = aTabId;
+  mPrincipal = aPrincipal;
   mMethodData = aMethodData;
   mDetails = aDetails;
   mOptions = aOptions;
   return NS_OK;
 }
 
 NS_IMETHODIMP
 PaymentCreateActionRequest::GetTabId(uint64_t* aTabId)
 {
   NS_ENSURE_ARG_POINTER(aTabId);
   *aTabId = mTabId;
   return NS_OK;
 }
 
 NS_IMETHODIMP
+PaymentCreateActionRequest::GetPrincipal(nsIPrincipal** aPrincipal)
+{
+  NS_ENSURE_ARG_POINTER(aPrincipal);
+  MOZ_ASSERT(mPrincipal);
+  nsCOMPtr<nsIPrincipal> principal = mPrincipal;
+  principal.forget(aPrincipal);
+  return NS_OK;
+}
+
+NS_IMETHODIMP
 PaymentCreateActionRequest::GetMethodData(nsIArray** aMethodData)
 {
   NS_ENSURE_ARG_POINTER(aMethodData);
   MOZ_ASSERT(mMethodData);
   nsCOMPtr<nsIArray> methodData = mMethodData;
   methodData.forget(aMethodData);
   return NS_OK;
 }
--- a/dom/payments/PaymentActionRequest.h
+++ b/dom/payments/PaymentActionRequest.h
@@ -40,16 +40,17 @@ public:
   NS_DECL_NSIPAYMENTCREATEACTIONREQUEST
 
   PaymentCreateActionRequest();
 
 private:
   ~PaymentCreateActionRequest() = default;
 
   uint64_t mTabId;
+  nsCOMPtr<nsIPrincipal> mPrincipal;
   nsCOMPtr<nsIArray> mMethodData;
   nsCOMPtr<nsIPaymentDetails> mDetails;
   nsCOMPtr<nsIPaymentOptions> mOptions;
 };
 
 class PaymentCompleteActionRequest final : public nsIPaymentCompleteActionRequest
                                          , public PaymentActionRequest
 {
--- a/dom/payments/PaymentRequest.cpp
+++ b/dom/payments/PaymentRequest.cpp
@@ -251,29 +251,32 @@ PaymentRequest::Constructor(const Global
   // Otherwise, only allow-to-use this feature when the browsing context container is
   // an iframe with "allowpaymentrequest" attribute.
   nsCOMPtr<nsIDocument> doc = window->GetExtantDoc();
   nsINode* node = static_cast<nsINode*>(doc);
   if (!node) {
     aRv.Throw(NS_ERROR_UNEXPECTED);
     return nullptr;
   }
+
+  nsCOMPtr<nsIPrincipal> topLevelPrincipal;
   do {
     nsINode* parentNode = nsContentUtils::GetCrossDocParentNode(node);
     if (parentNode) {
       nsresult rv = nsContentUtils::CheckSameOrigin(node, parentNode);
       if (NS_FAILED(rv)) {
         nsIContent* content = static_cast<nsIContent*>(parentNode);
         if (!content->IsHTMLElement(nsGkAtoms::iframe) ||
             !content->HasAttr(kNameSpaceID_None, nsGkAtoms::allowpaymentrequest)) {
           aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
           return nullptr;
         }
       }
     }
+    topLevelPrincipal = node->NodePrincipal();
     node = parentNode;
   } while (node);
 
   // Check payment methods and details
   nsAutoString message;
   if (!IsValidMethodData(nsContentUtils::GetCurrentJSContext(),
                          aMethodData,
                          message) ||
@@ -284,18 +287,18 @@ PaymentRequest::Constructor(const Global
 
   RefPtr<PaymentRequestManager> manager = PaymentRequestManager::GetSingleton();
   if (NS_WARN_IF(!manager)) {
     return nullptr;
   }
 
   // Create PaymentRequest and set its |mId|
   RefPtr<PaymentRequest> request;
-  nsresult rv = manager->CreatePayment(window, aMethodData, aDetails,
-                                       aOptions, getter_AddRefs(request));
+  nsresult rv = manager->CreatePayment(window, topLevelPrincipal, aMethodData,
+                                       aDetails, aOptions, getter_AddRefs(request));
   if (NS_WARN_IF(NS_FAILED(rv))) {
     aRv.Throw(NS_ERROR_DOM_TYPE_ERR);
     return nullptr;
   }
 
   return request.forget();
 }
 
--- a/dom/payments/PaymentRequestData.cpp
+++ b/dom/payments/PaymentRequestData.cpp
@@ -585,36 +585,48 @@ PaymentOptions::GetShippingType(nsAStrin
 
 /* PaymentReqeust */
 
 NS_IMPL_ISUPPORTS(PaymentRequest,
                   nsIPaymentRequest)
 
 PaymentRequest::PaymentRequest(const uint64_t aTabId,
                                const nsAString& aRequestId,
+                               nsIPrincipal* aPrincipal,
                                nsIArray* aPaymentMethods,
                                nsIPaymentDetails* aPaymentDetails,
                                nsIPaymentOptions* aPaymentOptions)
   : mTabId(aTabId)
   , mRequestId(aRequestId)
+  , mPrincipal(aPrincipal)
   , mPaymentMethods(aPaymentMethods)
   , mPaymentDetails(aPaymentDetails)
   , mPaymentOptions(aPaymentOptions)
 {
 }
 
 NS_IMETHODIMP
 PaymentRequest::GetTabId(uint64_t* aTabId)
 {
   NS_ENSURE_ARG_POINTER(aTabId);
   *aTabId = mTabId;
   return NS_OK;
 }
 
 NS_IMETHODIMP
+PaymentRequest::GetPrincipal(nsIPrincipal** aPrincipal)
+{
+  NS_ENSURE_ARG_POINTER(aPrincipal);
+  MOZ_ASSERT(mPrincipal);
+  nsCOMPtr<nsIPrincipal> principal = mPrincipal;
+  principal.forget(aPrincipal);
+  return NS_OK;
+}
+
+NS_IMETHODIMP
 PaymentRequest::GetRequestId(nsAString& aRequestId)
 {
   aRequestId = mRequestId;
   return NS_OK;
 }
 
 NS_IMETHODIMP
 PaymentRequest::GetPaymentMethods(nsIArray** aPaymentMethods)
--- a/dom/payments/PaymentRequestData.h
+++ b/dom/payments/PaymentRequestData.h
@@ -174,25 +174,27 @@ private:
 class PaymentRequest final : public nsIPaymentRequest
 {
 public:
   NS_DECL_ISUPPORTS
   NS_DECL_NSIPAYMENTREQUEST
 
   PaymentRequest(const uint64_t aTabId,
                  const nsAString& aRequestId,
+                 nsIPrincipal* aPrincipal,
                  nsIArray* aPaymentMethods,
                  nsIPaymentDetails* aPaymentDetails,
                  nsIPaymentOptions* aPaymentOptions);
 
 private:
   ~PaymentRequest() = default;
 
   uint64_t mTabId;
   nsString mRequestId;
+  nsCOMPtr<nsIPrincipal> mPrincipal;
   nsCOMPtr<nsIArray> mPaymentMethods;
   nsCOMPtr<nsIPaymentDetails> mPaymentDetails;
   nsCOMPtr<nsIPaymentOptions> mPaymentOptions;
 };
 
 class PaymentAddress final : public nsIPaymentAddress
 {
 public:
--- a/dom/payments/PaymentRequestManager.cpp
+++ b/dom/payments/PaymentRequestManager.cpp
@@ -7,16 +7,17 @@
 #include "PaymentRequestManager.h"
 #include "PaymentRequestUtils.h"
 #include "mozilla/ClearOnShutdown.h"
 #include "mozilla/dom/ContentChild.h"
 #include "mozilla/dom/TabChild.h"
 #include "mozilla/dom/PaymentRequestChild.h"
 #include "nsContentUtils.h"
 #include "nsString.h"
+#include "nsIPrincipal.h"
 
 namespace mozilla {
 namespace dom {
 namespace {
 
 /*
  *  Following Convert* functions are used for convert PaymentRequest structs
  *  to transferable structs for IPC.
@@ -385,23 +386,25 @@ GetSelectedShippingOption(const PaymentD
     if (shippingOption.mSelected) {
       aOption = shippingOption.mId;
     }
   }
 }
 
 nsresult
 PaymentRequestManager::CreatePayment(nsPIDOMWindowInner* aWindow,
+                                     nsIPrincipal* aPrincipal,
                                      const Sequence<PaymentMethodData>& aMethodData,
                                      const PaymentDetailsInit& aDetails,
                                      const PaymentOptions& aOptions,
                                      PaymentRequest** aRequest)
 {
   MOZ_ASSERT(NS_IsMainThread());
   NS_ENSURE_ARG_POINTER(aRequest);
+  NS_ENSURE_ARG_POINTER(aPrincipal);
   *aRequest = nullptr;
   nsresult rv;
 
   RefPtr<PaymentRequest> request = PaymentRequest::CreatePaymentRequest(aWindow, rv);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
 
@@ -455,16 +458,17 @@ PaymentRequestManager::CreatePayment(nsP
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
 
   IPCPaymentOptions options;
   ConvertOptions(aOptions, options);
 
   IPCPaymentCreateActionRequest action(internalId,
+                                       IPC::Principal(aPrincipal),
                                        methodData,
                                        details,
                                        options);
 
   rv = SendRequestPayment(request, action, true);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
--- a/dom/payments/PaymentRequestManager.h
+++ b/dom/payments/PaymentRequestManager.h
@@ -37,16 +37,17 @@ public:
 
   /*
    *  This method is used to create PaymentRequest object and send corresponding
    *  data to chrome process for internal payment creation, such that content
    *  process can ask specific task by sending requestId only.
    */
   nsresult
   CreatePayment(nsPIDOMWindowInner* aWindow,
+                nsIPrincipal* aPrincipal,
                 const Sequence<PaymentMethodData>& aMethodData,
                 const PaymentDetailsInit& aDetails,
                 const PaymentOptions& aOptions,
                 PaymentRequest** aRequest);
 
   nsresult CanMakePayment(const nsAString& aRequestId);
   nsresult ShowPayment(const nsAString& aRequestId);
   nsresult AbortPayment(const nsAString& aRequestId);
--- a/dom/payments/PaymentRequestService.cpp
+++ b/dom/payments/PaymentRequestService.cpp
@@ -232,30 +232,34 @@ PaymentRequestService::RequestPayment(ns
     case nsIPaymentActionRequest::CREATE_ACTION: {
       nsCOMPtr<nsIPaymentCreateActionRequest> request =
         do_QueryInterface(aRequest);
       MOZ_ASSERT(request);
       uint64_t tabId;
       rv = request->GetTabId(&tabId);
       NS_ENSURE_SUCCESS(rv, rv);
 
+      nsCOMPtr<nsIPrincipal> principal;
+      rv = request->GetPrincipal(getter_AddRefs(principal));
+      NS_ENSURE_SUCCESS(rv, rv);
+
       nsCOMPtr<nsIArray> methodData;
       rv = request->GetMethodData(getter_AddRefs(methodData));
       NS_ENSURE_SUCCESS(rv, rv);
 
       nsCOMPtr<nsIPaymentDetails> details;
       rv = request->GetDetails(getter_AddRefs(details));
       NS_ENSURE_SUCCESS(rv, rv);
 
       nsCOMPtr<nsIPaymentOptions> options;
       rv = request->GetOptions(getter_AddRefs(options));
       NS_ENSURE_SUCCESS(rv, rv);
 
       nsCOMPtr<nsIPaymentRequest> payment =
-        new payments::PaymentRequest(tabId, requestId, methodData, details, options);
+        new payments::PaymentRequest(tabId, requestId, principal, methodData, details, options);
 
       if (!mRequestQueue.AppendElement(payment, mozilla::fallible)) {
         return NS_ERROR_OUT_OF_MEMORY;
       }
       break;
     }
     /*
      *  TODO: 1. Check third party payment app support by traversing all
--- a/dom/payments/ipc/PPaymentRequest.ipdl
+++ b/dom/payments/ipc/PPaymentRequest.ipdl
@@ -1,15 +1,16 @@
 /* -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 8 -*- */
 /* vim: set sw=4 ts=8 et tw=80 ft=cpp : */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 include protocol PBrowser;
+using class IPC::Principal from "mozilla/dom/PermissionMessageUtils.h";
 
 namespace mozilla {
 namespace dom {
 
 struct IPCPaymentMethodData
 {
   nsString supportedMethods;
   nsString data;
@@ -65,16 +66,17 @@ struct IPCPaymentOptions
   bool requestPayerPhone;
   bool requestShipping;
   nsString shippingType;
 };
 
 struct IPCPaymentCreateActionRequest
 {
   nsString requestId;
+  Principal principal;
   IPCPaymentMethodData[] methodData;
   IPCPaymentDetails details;
   IPCPaymentOptions options;
 };
 
 struct IPCPaymentCanMakeActionRequest
 {
   nsString requestId;
--- a/dom/payments/ipc/PaymentRequestParent.cpp
+++ b/dom/payments/ipc/PaymentRequestParent.cpp
@@ -68,16 +68,17 @@ PaymentRequestParent::RecvRequestPayment
       nsCOMPtr<nsIPaymentCreateActionRequest> createAction =
         do_CreateInstance(NS_PAYMENT_CREATE_ACTION_REQUEST_CONTRACT_ID);
       if (NS_WARN_IF(!createAction)) {
         return IPC_FAIL_NO_REASON(this);
       }
       rv = createAction->InitRequest(request.requestId(),
                                      callback,
                                      mTabId,
+                                     request.principal(),
                                      methodData,
                                      details,
                                      options);
       if (NS_WARN_IF(NS_FAILED(rv))) {
         return IPC_FAIL_NO_REASON(this);
       }
 
       action = do_QueryInterface(createAction);