widget/gonk/nsClipboard.cpp

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 #include "mozilla/dom/ContentChild.h"
     6 #include "nsClipboard.h"
     7 #include "nsISupportsPrimitives.h"
     8 #include "nsCOMPtr.h"
     9 #include "nsComponentManagerUtils.h"
    10 #include "nsXULAppAPI.h"
    12 using namespace mozilla;
    13 using mozilla::dom::ContentChild;
    15 #define LOG_TAG "Clipboard"
    16 #define LOGI(args...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, ## args)
    17 #define LOGE(args...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, ## args)
    19 NS_IMPL_ISUPPORTS(nsClipboard, nsIClipboard)
    21 nsClipboard::nsClipboard()
    22 {
    23 }
    25 NS_IMETHODIMP
    26 nsClipboard::SetData(nsITransferable *aTransferable,
    27                      nsIClipboardOwner *anOwner, int32_t aWhichClipboard)
    28 {
    29   if (aWhichClipboard != kGlobalClipboard) {
    30     return NS_ERROR_NOT_IMPLEMENTED;
    31   }
    33   nsCOMPtr<nsISupports> tmp;
    34   uint32_t len;
    35   nsresult rv  = aTransferable->GetTransferData(kUnicodeMime, getter_AddRefs(tmp),
    36                                                 &len);
    37   if (NS_WARN_IF(NS_FAILED(rv))) {
    38     return rv;
    39   }
    40   nsCOMPtr<nsISupportsString> supportsString = do_QueryInterface(tmp);
    41   // No support for non-text data
    42   if (NS_WARN_IF(!supportsString)) {
    43     LOGE("No support for non-text data. See bug 952456.");
    44     return NS_ERROR_NOT_IMPLEMENTED;
    45   }
    46   nsAutoString buffer;
    47   supportsString->GetData(buffer);
    49   if (XRE_GetProcessType() == GeckoProcessType_Default) {
    50     mClipboard = buffer;
    51   } else {
    52     bool isPrivateData = false;
    53     aTransferable->GetIsPrivateData(&isPrivateData);
    54     ContentChild::GetSingleton()->SendSetClipboardText(buffer, isPrivateData,
    55                                                        aWhichClipboard);
    56   }
    58   return NS_OK;
    59 }
    61 NS_IMETHODIMP
    62 nsClipboard::GetData(nsITransferable *aTransferable, int32_t aWhichClipboard)
    63 {
    64   if (aWhichClipboard != kGlobalClipboard) {
    65     return NS_ERROR_NOT_IMPLEMENTED;
    66   }
    68   nsAutoString buffer;
    69   if (XRE_GetProcessType() == GeckoProcessType_Default) {
    70     buffer = mClipboard;
    71   } else {
    72     ContentChild::GetSingleton()->SendGetClipboardText(aWhichClipboard, &buffer);
    73   }
    75   nsresult rv;
    76   nsCOMPtr<nsISupportsString> dataWrapper =
    77     do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID, &rv);
    78   if (NS_WARN_IF(NS_FAILED(rv))) {
    79     return rv;
    80   }
    82   rv = dataWrapper->SetData(buffer);
    83   if (NS_WARN_IF(NS_FAILED(rv))) {
    84     return rv;
    85   }
    87   // If our data flavor has already been added, this will fail. But we don't care
    88   aTransferable->AddDataFlavor(kUnicodeMime);
    90   nsCOMPtr<nsISupports> nsisupportsDataWrapper =
    91     do_QueryInterface(dataWrapper);
    92   rv = aTransferable->SetTransferData(kUnicodeMime, nsisupportsDataWrapper,
    93                                       buffer.Length() * sizeof(PRUnichar));
    94   if (NS_WARN_IF(NS_FAILED(rv))) {
    95     return rv;
    96   }
    98   return NS_OK;
    99 }
   101 NS_IMETHODIMP
   102 nsClipboard::EmptyClipboard(int32_t aWhichClipboard)
   103 {
   104   if (aWhichClipboard != kGlobalClipboard) {
   105     return NS_ERROR_NOT_IMPLEMENTED;
   106   }
   107   if (XRE_GetProcessType() == GeckoProcessType_Default) {
   108     mClipboard.Truncate(0);
   109   } else {
   110     ContentChild::GetSingleton()->SendEmptyClipboard(aWhichClipboard);
   111   }
   113   return NS_OK;
   114 }
   116 NS_IMETHODIMP
   117 nsClipboard::HasDataMatchingFlavors(const char **aFlavorList,
   118                                     uint32_t aLength, int32_t aWhichClipboard,
   119                                     bool *aHasText)
   120 {
   121   *aHasText = false;
   122   if (aWhichClipboard != kGlobalClipboard) {
   123     return NS_ERROR_NOT_IMPLEMENTED;
   124   }
   125   if (XRE_GetProcessType() == GeckoProcessType_Default) {
   126     *aHasText = !mClipboard.IsEmpty();
   127   } else {
   128     ContentChild::GetSingleton()->SendClipboardHasText(aWhichClipboard, aHasText);
   129   }
   130   return NS_OK;
   131 }
   133 NS_IMETHODIMP
   134 nsClipboard::SupportsSelectionClipboard(bool *aIsSupported)
   135 {
   136   *aIsSupported = false;
   137   return NS_OK;
   138 }
   140 NS_IMETHODIMP
   141 nsClipboard::SupportsFindClipboard(bool* _retval)
   142 {
   143   NS_ENSURE_ARG_POINTER(_retval);
   145   *_retval = false;
   146   return NS_OK;
   147 }

mercurial