CLOSED TREE: TraceMonkey merge head. (a=blockers)
[mozilla-central.git] / widget / src / android / nsClipboard.cpp
blobad8e343ade0a2bcde9fc45b19406fb2a0c115012
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
14 * The Original Code is Mozilla Android code.
16 * The Initial Developer of the Original Code is Mozilla Foundation.
17 * Portions created by the Initial Developer are Copyright (C) 2010
18 * the Initial Developer. All Rights Reserved.
20 * Contributor(s):
21 * Brad Lassey <blassey@mozilla.com>
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
37 #include "nsClipboard.h"
38 #include "nsISupportsPrimitives.h"
39 #include "AndroidBridge.h"
40 #include "nsCOMPtr.h"
41 #include "nsComponentManagerUtils.h"
43 using namespace mozilla;
45 NS_IMPL_ISUPPORTS1(nsClipboard, nsIClipboard)
47 /* The Android clipboard only supports text and doesn't support mime types
48 * so we assume all clipboard data is text/unicode for now. Documentation
49 * indicates that support for other data types is planned for future
50 * releases.
53 nsClipboard::nsClipboard()
57 NS_IMETHODIMP
58 nsClipboard::SetData(nsITransferable *aTransferable,
59 nsIClipboardOwner *anOwner, PRInt32 aWhichClipboard)
61 if (aWhichClipboard != kGlobalClipboard)
62 return NS_ERROR_NOT_IMPLEMENTED;
63 if (!AndroidBridge::Bridge())
64 return NS_ERROR_NOT_IMPLEMENTED;
66 nsCOMPtr<nsISupports> tmp;
67 PRUint32 len;
68 nsresult rv = aTransferable->GetTransferData(kUnicodeMime, getter_AddRefs(tmp),
69 &len);
70 NS_ENSURE_SUCCESS(rv, rv);
71 nsCOMPtr<nsISupportsString> supportsString = do_QueryInterface(tmp);
72 // No support for non-text data
73 NS_ENSURE_TRUE(supportsString, NS_ERROR_NOT_IMPLEMENTED);
74 nsAutoString buffer;
75 supportsString->GetData(buffer);
76 AndroidBridge::Bridge()->SetClipboardText(buffer);
77 return NS_OK;
80 NS_IMETHODIMP
81 nsClipboard::GetData(nsITransferable *aTransferable, PRInt32 aWhichClipboard)
83 if (aWhichClipboard != kGlobalClipboard)
84 return NS_ERROR_NOT_IMPLEMENTED;
85 if (!AndroidBridge::Bridge())
86 return NS_ERROR_NOT_IMPLEMENTED;
88 nsAutoString buffer;
89 if (!AndroidBridge::Bridge()->GetClipboardText(buffer))
90 return NS_ERROR_UNEXPECTED;
92 nsresult rv;
93 nsCOMPtr<nsISupportsString> dataWrapper =
94 do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID, &rv);
95 NS_ENSURE_SUCCESS(rv, rv);
97 rv = dataWrapper->SetData(buffer);
98 NS_ENSURE_SUCCESS(rv, rv);
100 // If our data flavor has already been added, this will fail. But we don't care
101 aTransferable->AddDataFlavor(kUnicodeMime);
103 nsCOMPtr<nsISupports> nsisupportsDataWrapper =
104 do_QueryInterface(dataWrapper);
105 rv = aTransferable->SetTransferData(kUnicodeMime, nsisupportsDataWrapper,
106 buffer.Length() * sizeof(PRUnichar));
107 NS_ENSURE_SUCCESS(rv, rv);
109 return NS_OK;
112 NS_IMETHODIMP
113 nsClipboard::EmptyClipboard(PRInt32 aWhichClipboard)
115 if (aWhichClipboard != kGlobalClipboard)
116 return NS_ERROR_NOT_IMPLEMENTED;
117 if (AndroidBridge::Bridge())
118 AndroidBridge::Bridge()->EmptyClipboard();
119 return NS_OK;
122 NS_IMETHODIMP
123 nsClipboard::HasDataMatchingFlavors(const char **aFlavorList,
124 PRUint32 aLength, PRInt32 aWhichClipboard,
125 PRBool *aHasText NS_OUTPARAM)
127 *aHasText = PR_FALSE;
128 if (aWhichClipboard != kGlobalClipboard)
129 return NS_ERROR_NOT_IMPLEMENTED;
130 if (AndroidBridge::Bridge())
131 *aHasText = AndroidBridge::Bridge()->ClipboardHasText();
132 return NS_OK;
135 NS_IMETHODIMP
136 nsClipboard::SupportsSelectionClipboard(PRBool *aIsSupported NS_OUTPARAM)
138 *aIsSupported = PR_FALSE;
139 return NS_OK;