widget/gonk/nsClipboard.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/widget/gonk/nsClipboard.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,148 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#include "mozilla/dom/ContentChild.h"
     1.9 +#include "nsClipboard.h"
    1.10 +#include "nsISupportsPrimitives.h"
    1.11 +#include "nsCOMPtr.h"
    1.12 +#include "nsComponentManagerUtils.h"
    1.13 +#include "nsXULAppAPI.h"
    1.14 +
    1.15 +using namespace mozilla;
    1.16 +using mozilla::dom::ContentChild;
    1.17 +
    1.18 +#define LOG_TAG "Clipboard"
    1.19 +#define LOGI(args...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, ## args)
    1.20 +#define LOGE(args...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, ## args)
    1.21 +
    1.22 +NS_IMPL_ISUPPORTS(nsClipboard, nsIClipboard)
    1.23 +
    1.24 +nsClipboard::nsClipboard()
    1.25 +{
    1.26 +}
    1.27 +
    1.28 +NS_IMETHODIMP
    1.29 +nsClipboard::SetData(nsITransferable *aTransferable,
    1.30 +                     nsIClipboardOwner *anOwner, int32_t aWhichClipboard)
    1.31 +{
    1.32 +  if (aWhichClipboard != kGlobalClipboard) {
    1.33 +    return NS_ERROR_NOT_IMPLEMENTED;
    1.34 +  }
    1.35 +
    1.36 +  nsCOMPtr<nsISupports> tmp;
    1.37 +  uint32_t len;
    1.38 +  nsresult rv  = aTransferable->GetTransferData(kUnicodeMime, getter_AddRefs(tmp),
    1.39 +                                                &len);
    1.40 +  if (NS_WARN_IF(NS_FAILED(rv))) {
    1.41 +    return rv;
    1.42 +  }
    1.43 +  nsCOMPtr<nsISupportsString> supportsString = do_QueryInterface(tmp);
    1.44 +  // No support for non-text data
    1.45 +  if (NS_WARN_IF(!supportsString)) {
    1.46 +    LOGE("No support for non-text data. See bug 952456.");
    1.47 +    return NS_ERROR_NOT_IMPLEMENTED;
    1.48 +  }
    1.49 +  nsAutoString buffer;
    1.50 +  supportsString->GetData(buffer);
    1.51 +
    1.52 +  if (XRE_GetProcessType() == GeckoProcessType_Default) {
    1.53 +    mClipboard = buffer;
    1.54 +  } else {
    1.55 +    bool isPrivateData = false;
    1.56 +    aTransferable->GetIsPrivateData(&isPrivateData);
    1.57 +    ContentChild::GetSingleton()->SendSetClipboardText(buffer, isPrivateData,
    1.58 +                                                       aWhichClipboard);
    1.59 +  }
    1.60 +
    1.61 +  return NS_OK;
    1.62 +}
    1.63 +
    1.64 +NS_IMETHODIMP
    1.65 +nsClipboard::GetData(nsITransferable *aTransferable, int32_t aWhichClipboard)
    1.66 +{
    1.67 +  if (aWhichClipboard != kGlobalClipboard) {
    1.68 +    return NS_ERROR_NOT_IMPLEMENTED;
    1.69 +  }
    1.70 +
    1.71 +  nsAutoString buffer;
    1.72 +  if (XRE_GetProcessType() == GeckoProcessType_Default) {
    1.73 +    buffer = mClipboard;
    1.74 +  } else {
    1.75 +    ContentChild::GetSingleton()->SendGetClipboardText(aWhichClipboard, &buffer);
    1.76 +  }
    1.77 +
    1.78 +  nsresult rv;
    1.79 +  nsCOMPtr<nsISupportsString> dataWrapper =
    1.80 +    do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID, &rv);
    1.81 +  if (NS_WARN_IF(NS_FAILED(rv))) {
    1.82 +    return rv;
    1.83 +  }
    1.84 +
    1.85 +  rv = dataWrapper->SetData(buffer);
    1.86 +  if (NS_WARN_IF(NS_FAILED(rv))) {
    1.87 +    return rv;
    1.88 +  }
    1.89 +
    1.90 +  // If our data flavor has already been added, this will fail. But we don't care
    1.91 +  aTransferable->AddDataFlavor(kUnicodeMime);
    1.92 +
    1.93 +  nsCOMPtr<nsISupports> nsisupportsDataWrapper =
    1.94 +    do_QueryInterface(dataWrapper);
    1.95 +  rv = aTransferable->SetTransferData(kUnicodeMime, nsisupportsDataWrapper,
    1.96 +                                      buffer.Length() * sizeof(PRUnichar));
    1.97 +  if (NS_WARN_IF(NS_FAILED(rv))) {
    1.98 +    return rv;
    1.99 +  }
   1.100 +
   1.101 +  return NS_OK;
   1.102 +}
   1.103 +
   1.104 +NS_IMETHODIMP
   1.105 +nsClipboard::EmptyClipboard(int32_t aWhichClipboard)
   1.106 +{
   1.107 +  if (aWhichClipboard != kGlobalClipboard) {
   1.108 +    return NS_ERROR_NOT_IMPLEMENTED;
   1.109 +  }
   1.110 +  if (XRE_GetProcessType() == GeckoProcessType_Default) {
   1.111 +    mClipboard.Truncate(0);
   1.112 +  } else {
   1.113 +    ContentChild::GetSingleton()->SendEmptyClipboard(aWhichClipboard);
   1.114 +  }
   1.115 +
   1.116 +  return NS_OK;
   1.117 +}
   1.118 +
   1.119 +NS_IMETHODIMP
   1.120 +nsClipboard::HasDataMatchingFlavors(const char **aFlavorList,
   1.121 +                                    uint32_t aLength, int32_t aWhichClipboard,
   1.122 +                                    bool *aHasText)
   1.123 +{
   1.124 +  *aHasText = false;
   1.125 +  if (aWhichClipboard != kGlobalClipboard) {
   1.126 +    return NS_ERROR_NOT_IMPLEMENTED;
   1.127 +  }
   1.128 +  if (XRE_GetProcessType() == GeckoProcessType_Default) {
   1.129 +    *aHasText = !mClipboard.IsEmpty();
   1.130 +  } else {
   1.131 +    ContentChild::GetSingleton()->SendClipboardHasText(aWhichClipboard, aHasText);
   1.132 +  }
   1.133 +  return NS_OK;
   1.134 +}
   1.135 +
   1.136 +NS_IMETHODIMP
   1.137 +nsClipboard::SupportsSelectionClipboard(bool *aIsSupported)
   1.138 +{
   1.139 +  *aIsSupported = false;
   1.140 +  return NS_OK;
   1.141 +}
   1.142 +
   1.143 +NS_IMETHODIMP
   1.144 +nsClipboard::SupportsFindClipboard(bool* _retval)
   1.145 +{
   1.146 +  NS_ENSURE_ARG_POINTER(_retval);
   1.147 +
   1.148 +  *_retval = false;
   1.149 +  return NS_OK;
   1.150 +}
   1.151 +

mercurial