Bug 664717 - Part 1: Switch to requestingPrincipal in nsITransferable. r=ckerschb draft
authorHector Zhao <bzhao@mozilla.com>
Fri, 01 Jul 2016 16:01:11 +0800
changeset 383106 c4f15e1497aa6abab2caf5a868832df9844efba7
parent 382798 82e1f1b9c0559f38a8460e2f2f3044de4c7712d6
child 383107 d4002835c36390de9cd486867fd825bc71badfd4
push id21936
push userbmo:bzhao@mozilla.com
push dateFri, 01 Jul 2016 08:06:55 +0000
reviewersckerschb
bugs664717
milestone50.0a1
Bug 664717 - Part 1: Switch to requestingPrincipal in nsITransferable. r=ckerschb MozReview-Commit-ID: Hndb5ftbEuL
widget/nsITransferable.idl
widget/nsTransferable.cpp
widget/nsTransferable.h
widget/windows/nsDataObj.cpp
widget/windows/nsDataObj.h
widget/windows/nsDragService.cpp
--- a/widget/nsITransferable.idl
+++ b/widget/nsITransferable.idl
@@ -4,17 +4,17 @@
  * 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 "nsISupportsArray.idl"
 #include "nsIFormatConverter.idl"
 #include "nsIContentPolicyBase.idl"
 
-interface nsIDOMNode;
+interface nsIPrincipal;
 
 %{ C++
 
 // these probably shouldn't live here, but in some central repository shared
 // by the entire app.
 #define kTextMime                   "text/plain"
 #define kRTFMime                    "text/rtf"
 #define kUnicodeMime                "text/unicode"
@@ -195,18 +195,17 @@ interface nsITransferable : nsISupports
   /**
    * Use of the SetIsPrivateData() method generated by isPrivateData attribute should 
    * be avoided as much as possible because the value set may not reflect the status 
    * of the context in which the transferable was created.
    */
   [noscript] attribute boolean isPrivateData;
 
   /**
-   * The source dom node this transferable was created from and
-   * the contentPolicyType for the transferable.
+   * The principal of the source dom node this transferable was
+   * created from and the contentPolicyType for the transferable.
    * Note, currently only used on Windows for network principal and
-   * contentPolicyType information in drag operations.
+   * contentPolicyType information in clipboard and drag operations.
    */
-  [noscript] attribute nsIDOMNode requestingNode;
+  [noscript] attribute nsIPrincipal requestingPrincipal;
   [noscript] attribute nsContentPolicyType contentPolicyType;
 
 };
-
--- a/widget/nsTransferable.cpp
+++ b/widget/nsTransferable.cpp
@@ -628,27 +628,26 @@ nsTransferable::SetIsPrivateData(bool aI
   MOZ_ASSERT(mInitialized);
 
   mPrivateData = aIsPrivateData;
 
   return NS_OK;
 }
 
 NS_IMETHODIMP
-nsTransferable::GetRequestingNode(nsIDOMNode** outRequestingNode)
+nsTransferable::GetRequestingPrincipal(nsIPrincipal** outRequestingPrincipal)
 {
-  nsCOMPtr<nsIDOMNode> node = do_QueryReferent(mRequestingNode);
-  node.forget(outRequestingNode);
+  NS_IF_ADDREF(*outRequestingPrincipal = mRequestingPrincipal);
   return NS_OK;
 }
 
 NS_IMETHODIMP
-nsTransferable::SetRequestingNode(nsIDOMNode* aRequestingNode)
+nsTransferable::SetRequestingPrincipal(nsIPrincipal* aRequestingPrincipal)
 {
-  mRequestingNode = do_GetWeakReference(aRequestingNode);
+  mRequestingPrincipal = aRequestingPrincipal;
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsTransferable::GetContentPolicyType(nsContentPolicyType* outContentPolicyType)
 {
   NS_ENSURE_ARG_POINTER(outContentPolicyType);
   *outContentPolicyType = mContentPolicyType;
--- a/widget/nsTransferable.h
+++ b/widget/nsTransferable.h
@@ -7,17 +7,17 @@
 #define nsTransferable_h__
 
 #include "nsIContentPolicyBase.h"
 #include "nsIFormatConverter.h"
 #include "nsITransferable.h"
 #include "nsCOMPtr.h"
 #include "nsString.h"
 #include "nsTArray.h"
-#include "nsWeakPtr.h"
+#include "nsIPrincipal.h"
 
 class nsString;
 
 //
 // DataStruct
 //
 // Holds a flavor (a mime type) that describes the data and the associated data.
 //
@@ -69,17 +69,17 @@ protected:
   virtual ~nsTransferable();
 
     // get flavors w/out converter
   nsresult GetTransferDataFlavors(nsISupportsArray** aDataFlavorList);
  
   nsTArray<DataStruct> mDataArray;
   nsCOMPtr<nsIFormatConverter> mFormatConv;
   bool mPrivateData;
-  nsWeakPtr mRequestingNode;
+  nsCOMPtr<nsIPrincipal> mRequestingPrincipal;
   nsContentPolicyType mContentPolicyType;
 #if DEBUG
   bool mInitialized;
 #endif
 
 };
 
 #endif // nsTransferable_h__
--- a/widget/windows/nsDataObj.cpp
+++ b/widget/windows/nsDataObj.cpp
@@ -28,17 +28,17 @@
 #include "nsXPCOMStrings.h"
 #include "nscore.h"
 #include "nsDirectoryServiceDefs.h"
 #include "nsITimer.h"
 #include "nsThreadUtils.h"
 #include "mozilla/Preferences.h"
 #include "nsIContentPolicy.h"
 #include "nsContentUtils.h"
-#include "nsINode.h"
+#include "nsIPrincipal.h"
 
 #include "WinUtils.h"
 #include "mozilla/LazyIdleThread.h"
 #include "mozilla/WindowsVersion.h"
 #include <algorithm>
 
 
 using namespace mozilla;
@@ -60,26 +60,26 @@ nsDataObj::CStream::CStream() :
 nsDataObj::CStream::~CStream()
 {
 }
 
 //-----------------------------------------------------------------------------
 // helper - initializes the stream
 nsresult nsDataObj::CStream::Init(nsIURI *pSourceURI,
                                   uint32_t aContentPolicyType,
-                                  nsINode* aRequestingNode)
+                                  nsIPrincipal* aRequestingPrincipal)
 {
-  // we can not create a channel without a requestingNode
-  if (!aRequestingNode) {
+  // we can not create a channel without a requestingPrincipal
+  if (!aRequestingPrincipal) {
     return NS_ERROR_FAILURE;
   }
   nsresult rv;
   rv = NS_NewChannel(getter_AddRefs(mChannel),
                      pSourceURI,
-                     aRequestingNode,
+                     aRequestingPrincipal,
                      nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_INHERITS,
                      aContentPolicyType,
                      nullptr,   // loadGroup
                      nullptr,   // aCallbacks
                      nsIRequest::LOAD_FROM_CACHE);
 
   NS_ENSURE_SUCCESS(rv, rv);
   rv = mChannel->AsyncOpen2(this);
@@ -336,25 +336,24 @@ HRESULT nsDataObj::CreateStream(IStream 
   if(FAILED(res))
     return res;
 
   nsDataObj::CStream *pStream = new nsDataObj::CStream();
   NS_ENSURE_TRUE(pStream, E_OUTOFMEMORY);
 
   pStream->AddRef();
 
-  // query the requestingNode from the transferable and add it to the new channel
-  nsCOMPtr<nsIDOMNode> requestingDomNode;
-  mTransferable->GetRequestingNode(getter_AddRefs(requestingDomNode));
-  nsCOMPtr<nsINode> requestingNode = do_QueryInterface(requestingDomNode);
-  MOZ_ASSERT(requestingNode, "can not create channel without a node");
+  // query the requestingPrincipal from the transferable and add it to the new channel
+  nsCOMPtr<nsIPrincipal> requestingPrincipal;
+  mTransferable->GetRequestingPrincipal(getter_AddRefs(requestingPrincipal));
+  MOZ_ASSERT(requestingPrincipal, "can not create channel without a principal");
   // default transferable content policy is nsIContentPolicy::TYPE_OTHER
   uint32_t contentPolicyType = nsIContentPolicy::TYPE_OTHER;
   mTransferable->GetContentPolicyType(&contentPolicyType);
-  rv = pStream->Init(sourceURI, contentPolicyType, requestingNode);
+  rv = pStream->Init(sourceURI, contentPolicyType, requestingPrincipal);
   if (NS_FAILED(rv))
   {
     pStream->Release();
     return E_FAIL;
   }
   *outStream = pStream;
 
   return S_OK;
--- a/widget/windows/nsDataObj.h
+++ b/widget/windows/nsDataObj.h
@@ -15,17 +15,17 @@
 #include "nsIURI.h"
 #include "nsIInputStream.h"
 #include "nsIStreamListener.h"
 #include "nsIChannel.h"
 #include "nsCOMArray.h"
 #include "nsITimer.h"
 
 class nsIThread;
-class nsINode;
+class nsIPrincipal;
 
 // The SDK shipping with VC11 has renamed IAsyncOperation to
 // IDataObjectAsyncCapability.  We try to detect this, and rename this in our
 // code too to make sure that we pick the correct name when building.
 #ifdef __IDataObjectAsyncCapability_INTERFACE_DEFINED__
 #define IAsyncOperation IDataObjectAsyncCapability
 #define IID_IAsyncOperation IID_IDataObjectAsyncCapability
 #else
@@ -246,17 +246,17 @@ protected:
     protected:
       virtual ~CStream();
       nsresult WaitForCompletion();
 
     public:
       CStream();
       nsresult Init(nsIURI *pSourceURI,
                     uint32_t aContentPolicyType,
-                    nsINode* aRequestingNode);
+                    nsIPrincipal* aRequestingPrincipal);
 
       NS_DECL_ISUPPORTS
       NS_DECL_NSIREQUESTOBSERVER
       NS_DECL_NSISTREAMLISTENER
 
       // IUnknown
       STDMETHOD(QueryInterface)(REFIID refiid, void** ppvResult);
 
@@ -289,9 +289,8 @@ protected:
 
     bool LookupArbitraryFormat(FORMATETC *aFormat, LPDATAENTRY *aDataEntry, BOOL aAddorUpdate);
     bool CopyMediumData(STGMEDIUM *aMediumDst, STGMEDIUM *aMediumSrc, LPFORMATETC aFormat, BOOL aSetData);
     static void RemoveTempFile(nsITimer* aTimer, void* aClosure);
 };
 
 
 #endif  // _NSDATAOBJ_H_
-
--- a/widget/windows/nsDragService.cpp
+++ b/widget/windows/nsDragService.cpp
@@ -200,18 +200,19 @@ nsDragService::InvokeDragSessionImpl(nsI
     if (!dataObjCollection)
       return NS_ERROR_OUT_OF_MEMORY;
     itemToDrag = dataObjCollection;
     for (uint32_t i=0; i<numItemsToDrag; ++i) {
       nsCOMPtr<nsISupports> supports;
       anArrayTransferables->GetElementAt(i, getter_AddRefs(supports));
       nsCOMPtr<nsITransferable> trans(do_QueryInterface(supports));
       if (trans) {
-        // set the requestingNode on the transferable
-        trans->SetRequestingNode(mSourceNode);
+        // set the requestingPrincipal on the transferable
+        nsCOMPtr<nsINode> node = do_QueryInterface(mSourceNode);
+        trans->SetRequestingPrincipal(node->NodePrincipal());
         trans->SetContentPolicyType(mContentPolicyType);
         RefPtr<IDataObject> dataObj;
         rv = nsClipboard::CreateNativeDataObject(trans,
                                                  getter_AddRefs(dataObj), uri);
         NS_ENSURE_SUCCESS(rv, rv);
         // Add the flavors to the collection object too
         rv = nsClipboard::SetupNativeDataObject(trans, dataObjCollection);
         NS_ENSURE_SUCCESS(rv, rv);
@@ -220,18 +221,19 @@ nsDragService::InvokeDragSessionImpl(nsI
       }
     }
   } // if dragging multiple items
   else {
     nsCOMPtr<nsISupports> supports;
     anArrayTransferables->GetElementAt(0, getter_AddRefs(supports));
     nsCOMPtr<nsITransferable> trans(do_QueryInterface(supports));
     if (trans) {
-      // set the requestingNode on the transferable
-      trans->SetRequestingNode(mSourceNode);
+      // set the requestingPrincipal on the transferable
+      nsCOMPtr<nsINode> node = do_QueryInterface(mSourceNode);
+      trans->SetRequestingPrincipal(node->NodePrincipal());
       trans->SetContentPolicyType(mContentPolicyType);
       rv = nsClipboard::CreateNativeDataObject(trans,
                                                getter_AddRefs(itemToDrag),
                                                uri);
       NS_ENSURE_SUCCESS(rv, rv);
     }
   } // else dragging a single object