widget/android/nsClipboard.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/widget/android/nsClipboard.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,116 @@
     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 "AndroidBridge.h"
    1.12 +#include "nsCOMPtr.h"
    1.13 +#include "nsComponentManagerUtils.h"
    1.14 +#include "nsXULAppAPI.h"
    1.15 +
    1.16 +using namespace mozilla;
    1.17 +using mozilla::dom::ContentChild;
    1.18 +
    1.19 +NS_IMPL_ISUPPORTS(nsClipboard, nsIClipboard)
    1.20 +
    1.21 +/* The Android clipboard only supports text and doesn't support mime types
    1.22 + * so we assume all clipboard data is text/unicode for now. Documentation
    1.23 + * indicates that support for other data types is planned for future
    1.24 + * releases.
    1.25 + */
    1.26 +
    1.27 +nsClipboard::nsClipboard()
    1.28 +{
    1.29 +}
    1.30 +
    1.31 +NS_IMETHODIMP
    1.32 +nsClipboard::SetData(nsITransferable *aTransferable,
    1.33 +                     nsIClipboardOwner *anOwner, int32_t aWhichClipboard)
    1.34 +{
    1.35 +  if (aWhichClipboard != kGlobalClipboard)
    1.36 +    return NS_ERROR_NOT_IMPLEMENTED;
    1.37 +
    1.38 +  nsCOMPtr<nsISupports> tmp;
    1.39 +  uint32_t len;
    1.40 +  nsresult rv  = aTransferable->GetTransferData(kUnicodeMime, getter_AddRefs(tmp),
    1.41 +                                                &len);
    1.42 +  NS_ENSURE_SUCCESS(rv, rv);
    1.43 +  nsCOMPtr<nsISupportsString> supportsString = do_QueryInterface(tmp);
    1.44 +  // No support for non-text data
    1.45 +  NS_ENSURE_TRUE(supportsString, NS_ERROR_NOT_IMPLEMENTED);
    1.46 +  nsAutoString buffer;
    1.47 +  supportsString->GetData(buffer);
    1.48 +
    1.49 +  mozilla::widget::android::Clipboard::SetClipboardText(buffer);
    1.50 +  return NS_OK;
    1.51 +}
    1.52 +
    1.53 +NS_IMETHODIMP
    1.54 +nsClipboard::GetData(nsITransferable *aTransferable, int32_t aWhichClipboard)
    1.55 +{
    1.56 +  if (aWhichClipboard != kGlobalClipboard)
    1.57 +    return NS_ERROR_NOT_IMPLEMENTED;
    1.58 +
    1.59 +  nsAutoString buffer;
    1.60 +  if (!AndroidBridge::Bridge())
    1.61 +    return NS_ERROR_NOT_IMPLEMENTED;
    1.62 +  if (!AndroidBridge::Bridge()->GetClipboardText(buffer))
    1.63 +    return NS_ERROR_UNEXPECTED;
    1.64 +
    1.65 +  nsresult rv;
    1.66 +  nsCOMPtr<nsISupportsString> dataWrapper =
    1.67 +    do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID, &rv);
    1.68 +  NS_ENSURE_SUCCESS(rv, rv);
    1.69 +
    1.70 +  rv = dataWrapper->SetData(buffer);
    1.71 +  NS_ENSURE_SUCCESS(rv, rv);
    1.72 +
    1.73 +  // If our data flavor has already been added, this will fail. But we don't care
    1.74 +  aTransferable->AddDataFlavor(kUnicodeMime);
    1.75 +
    1.76 +  nsCOMPtr<nsISupports> nsisupportsDataWrapper =
    1.77 +    do_QueryInterface(dataWrapper);
    1.78 +  rv = aTransferable->SetTransferData(kUnicodeMime, nsisupportsDataWrapper,
    1.79 +                                      buffer.Length() * sizeof(char16_t));
    1.80 +  NS_ENSURE_SUCCESS(rv, rv);
    1.81 +
    1.82 +  return NS_OK;
    1.83 +}
    1.84 +
    1.85 +NS_IMETHODIMP
    1.86 +nsClipboard::EmptyClipboard(int32_t aWhichClipboard)
    1.87 +{
    1.88 +  if (aWhichClipboard != kGlobalClipboard)
    1.89 +    return NS_ERROR_NOT_IMPLEMENTED;
    1.90 +  mozilla::widget::android::Clipboard::ClearText();
    1.91 +  
    1.92 +  return NS_OK;
    1.93 +}
    1.94 +
    1.95 +NS_IMETHODIMP
    1.96 +nsClipboard::HasDataMatchingFlavors(const char **aFlavorList,
    1.97 +                                    uint32_t aLength, int32_t aWhichClipboard,
    1.98 +                                    bool *aHasText)
    1.99 +{
   1.100 +  *aHasText = false;
   1.101 +  if (aWhichClipboard != kGlobalClipboard)
   1.102 +    return NS_ERROR_NOT_IMPLEMENTED;
   1.103 +  *aHasText = mozilla::widget::android::Clipboard::HasText();
   1.104 +  return NS_OK;
   1.105 +}
   1.106 +
   1.107 +NS_IMETHODIMP
   1.108 +nsClipboard::SupportsSelectionClipboard(bool *aIsSupported)
   1.109 +{
   1.110 +  *aIsSupported = false;
   1.111 +  return NS_OK;
   1.112 +}
   1.113 +
   1.114 +NS_IMETHODIMP
   1.115 +nsClipboard::SupportsFindClipboard(bool* _retval)
   1.116 +{
   1.117 +  *_retval = false;
   1.118 +  return NS_OK;
   1.119 +}

mercurial