Bug 722872 - Part 2: Move the nsClipboardPrivacyHandler implementation aways from using the global private browsing service; r=roc
authorEhsan Akhgari <ehsan@mozilla.com>
Tue, 26 Jun 2012 11:41:12 -0400
changeset 97792 dbf7b4c5e8fcb1f2b1ef57c52aa7f5a5f63d9e40
parent 97791 93e55dcf0e2ee8fcd9d21def6d9787317856e1ac
child 97793 50cb8318cf5050081df13770e7120ea9075012c3
push id883
push useremorley@mozilla.com
push dateThu, 28 Jun 2012 14:11:27 +0000
treeherderfx-team@b7d0f477f873 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersroc
bugs722872
milestone16.0a1
Bug 722872 - Part 2: Move the nsClipboardPrivacyHandler implementation aways from using the global private browsing service; r=roc
widget/nsITransferable.idl
widget/xpwidgets/nsClipboardPrivacyHandler.cpp
widget/xpwidgets/nsClipboardPrivacyHandler.h
widget/xpwidgets/nsTransferable.cpp
--- a/widget/nsITransferable.idl
+++ b/widget/nsITransferable.idl
@@ -76,17 +76,17 @@ interface nsIFlavorDataProvider : nsISup
     * @param  aFlavor (in parameter) the flavor of data to retrieve
     * @param  aData the data. Some variant of class in nsISupportsPrimitives.idl
     * @param  aDataLen the length of the data
     */
   void getFlavorData(in nsITransferable aTransferable, in string aFlavor, out nsISupports aData, out unsigned long aDataLen);
 };
 
 
-[scriptable, uuid(51fede5f-8d3f-4625-9402-e82c21110541)]
+[scriptable, uuid(983cb266-535b-4ca5-8ef2-2cee08d061e6)]
 interface nsITransferable : nsISupports
 {
   const long kFlavorHasDataProvider = 0;
 
   /**
    * Initializes a transferable object.  This should be called on alli
    * transferable objects.  Failure to do so will result in fatal assertions in
    * debug builds.
@@ -167,10 +167,12 @@ interface nsITransferable : nsISupports
     * that goes along with it.
     *
     * @param  aDataFlavor a data flavor to remove
     */
   void removeDataFlavor ( in string aDataFlavor ) ;
 
   attribute nsIFormatConverter converter;
 
+  [noscript] readonly attribute boolean isPrivateData;
+
 };
 
--- a/widget/xpwidgets/nsClipboardPrivacyHandler.cpp
+++ b/widget/xpwidgets/nsClipboardPrivacyHandler.cpp
@@ -29,31 +29,33 @@ NS_IMPL_ISUPPORTS2(nsClipboardPrivacyHan
 
 nsresult
 nsClipboardPrivacyHandler::Init()
 {
   nsCOMPtr<nsIObserverService> observerService =
     mozilla::services::GetObserverService();
   if (!observerService)
     return NS_ERROR_FAILURE;
-  return observerService->AddObserver(this, NS_PRIVATE_BROWSING_SWITCH_TOPIC,
-                                      true);
+  return observerService->AddObserver(this, "last-pb-context-exited", true);
 }
 
 /**
   * Prepare the transferable object to be inserted into the clipboard
   *
   */
 nsresult
 nsClipboardPrivacyHandler::PrepareDataForClipboard(nsITransferable * aTransferable)
 {
   NS_ASSERTION(aTransferable, "clipboard given a null transferable");
 
+  bool isPrivateData = false;
+  aTransferable->GetIsPrivateData(&isPrivateData);
+
   nsresult rv = NS_OK;
-  if (InPrivateBrowsing()) {
+  if (isPrivateData) {
     nsCOMPtr<nsISupportsPRBool> data = do_CreateInstance(NS_SUPPORTS_PRBOOL_CONTRACTID);
     if (data) {
       rv = data->SetData(true);
       NS_ENSURE_SUCCESS(rv, rv);
 
       rv = aTransferable->AddDataFlavor(NS_MOZ_DATA_FROM_PRIVATEBROWSING);
       NS_ENSURE_SUCCESS(rv, rv);
 
@@ -63,73 +65,58 @@ nsClipboardPrivacyHandler::PrepareDataFo
   }
 
   return rv;
 }
 
 NS_IMETHODIMP
 nsClipboardPrivacyHandler::Observe(nsISupports *aSubject, char const *aTopic, PRUnichar const *aData)
 {
-  if (NS_LITERAL_STRING(NS_PRIVATE_BROWSING_LEAVE).Equals(aData)) {
-    nsresult rv;
-    nsCOMPtr<nsIClipboard> clipboard =
-      do_GetService("@mozilla.org/widget/clipboard;1", &rv);
-    NS_ENSURE_SUCCESS(rv, rv);
+  nsresult rv;
+  nsCOMPtr<nsIClipboard> clipboard =
+    do_GetService("@mozilla.org/widget/clipboard;1", &rv);
+  NS_ENSURE_SUCCESS(rv, rv);
 
-    const char * flavors[] = { NS_MOZ_DATA_FROM_PRIVATEBROWSING };
-    bool haveFlavors;
-    rv = clipboard->HasDataMatchingFlavors(flavors,
-                                           ArrayLength(flavors),
-                                           nsIClipboard::kGlobalClipboard,
-                                           &haveFlavors);
-    if (NS_SUCCEEDED(rv) && haveFlavors) {
+  const char * flavors[] = { NS_MOZ_DATA_FROM_PRIVATEBROWSING };
+  bool haveFlavors;
+  rv = clipboard->HasDataMatchingFlavors(flavors,
+                                         ArrayLength(flavors),
+                                         nsIClipboard::kGlobalClipboard,
+                                         &haveFlavors);
+  if (NS_SUCCEEDED(rv) && haveFlavors) {
 #if defined(XP_WIN)
-      // Workaround for bug 518412.  On Windows 7 x64, there is a bug
-      // in handling clipboard data without any formats between
-      // 32-bit/64-bit boundaries, which could lead Explorer to crash.
-      // We work around the problem by clearing the clipboard using
-      // the usual Win32 API.
-      NS_ENSURE_TRUE(SUCCEEDED(::OleSetClipboard(NULL)), NS_ERROR_FAILURE);
+    // Workaround for bug 518412.  On Windows 7 x64, there is a bug
+    // in handling clipboard data without any formats between
+    // 32-bit/64-bit boundaries, which could lead Explorer to crash.
+    // We work around the problem by clearing the clipboard using
+    // the usual Win32 API.
+    NS_ENSURE_TRUE(SUCCEEDED(::OleSetClipboard(NULL)), NS_ERROR_FAILURE);
 #else
-      // Empty the native clipboard by copying an empty transferable
-      nsCOMPtr<nsITransferable> nullData =
-        do_CreateInstance("@mozilla.org/widget/transferable;1", &rv);
-      NS_ENSURE_SUCCESS(rv, rv);
-      nullData->Init(nsnull);
-      rv = clipboard->SetData(nullData, nsnull,
-                              nsIClipboard::kGlobalClipboard);
-      NS_ENSURE_SUCCESS(rv, rv);
+    // Empty the native clipboard by copying an empty transferable
+    nsCOMPtr<nsITransferable> nullData =
+      do_CreateInstance("@mozilla.org/widget/transferable;1", &rv);
+    NS_ENSURE_SUCCESS(rv, rv);
+    nullData->Init(nsnull);
+    rv = clipboard->SetData(nullData, nsnull,
+                            nsIClipboard::kGlobalClipboard);
+    NS_ENSURE_SUCCESS(rv, rv);
 #endif
-    }
   }
 
   return NS_OK;
 }
 
-bool
-nsClipboardPrivacyHandler::InPrivateBrowsing()
-{
-  bool inPrivateBrowsingMode = false;
-  if (!mPBService)
-    mPBService = do_GetService(NS_PRIVATE_BROWSING_SERVICE_CONTRACTID);
-  if (mPBService)
-    mPBService->GetPrivateBrowsingEnabled(&inPrivateBrowsingMode);
-  return inPrivateBrowsingMode;
-}
-
 nsresult
 NS_NewClipboardPrivacyHandler(nsClipboardPrivacyHandler ** aHandler)
 {
   NS_PRECONDITION(aHandler != nsnull, "null ptr");
   if (!aHandler)
     return NS_ERROR_NULL_POINTER;
 
   *aHandler = new nsClipboardPrivacyHandler();
-  if (!*aHandler)
-    return NS_ERROR_OUT_OF_MEMORY;
 
   NS_ADDREF(*aHandler);
   nsresult rv = (*aHandler)->Init();
   if (NS_FAILED(rv))
     NS_RELEASE(*aHandler);
 
   return rv;
 }
--- a/widget/xpwidgets/nsClipboardPrivacyHandler.h
+++ b/widget/xpwidgets/nsClipboardPrivacyHandler.h
@@ -2,17 +2,16 @@
 /* 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/. */
 
 #ifndef nsClipboardPrivacyHandler_h__
 #define nsClipboardPrivacyHandler_h__
 
 #include "nsIObserver.h"
-#include "nsIPrivateBrowsingService.h"
 #include "nsWeakReference.h"
 #include "nsCOMPtr.h"
 #include "mozilla/Attributes.h"
 
 class nsITransferable;
 
 // nsClipboardPrivacyHandler makes sure that clipboard data copied during
 // the private browsing mode does not leak after exiting this mode.
@@ -30,20 +29,13 @@ public:
   // nsISupports
   NS_DECL_ISUPPORTS
 
   // nsIObserver  
   NS_DECL_NSIOBSERVER
 
   nsresult Init();
   nsresult PrepareDataForClipboard(nsITransferable * aTransferable);
-
-private:
-
-  bool InPrivateBrowsing();
-
-  nsCOMPtr<nsIPrivateBrowsingService> mPBService;
-
 };
 
 nsresult NS_NewClipboardPrivacyHandler(nsClipboardPrivacyHandler ** aHandler);
 
 #endif // nsClipboardPrivacyHandler_h__
--- a/widget/xpwidgets/nsTransferable.cpp
+++ b/widget/xpwidgets/nsTransferable.cpp
@@ -604,8 +604,20 @@ nsTransferable::FlavorsTransferableCanEx
             == mDataArray.NoIndex) // Don't append if already in intrinsic list
           (*_retval)->AppendElement (genericFlavor);
       } // foreach flavor that can be converted to
     }
   } // if a converter exists
 
   return NS_OK;
 } // FlavorsTransferableCanExport
+
+NS_IMETHODIMP
+nsTransferable::GetIsPrivateData(bool *aIsPrivateData)
+{
+  MOZ_ASSERT(mInitialized);
+
+  NS_ENSURE_ARG_POINTER(aIsPrivateData);
+
+  *aIsPrivateData = mPrivateData;
+
+  return NS_OK;
+}