Bug 894736, pass clipboard type to data transfer so that the correct clipboard is used when pasting, r=ehsan

This commit is contained in:
Neil Deakin 2013-08-07 14:52:38 -04:00
Родитель 3452cf327a
Коммит 8e1b7d6683
17 изменённых файлов: 170 добавлений и 34 удалений

Просмотреть файл

@ -80,11 +80,14 @@ class nsCopySupport
* responsible for removing the content during a cut operation if true is
* returned.
*
* aClipboardType specifies which clipboard to use, from nsIClipboard.
*
* If the event is cancelled or an error occurs, false will be returned.
*/
static bool FireClipboardEvent(int32_t aType,
nsIPresShell* aPresShell,
nsISelection* aSelection);
int32_t aClipboardType,
nsIPresShell* aPresShell,
nsISelection* aSelection);
};
#endif

Просмотреть файл

@ -4960,7 +4960,7 @@ nsContentUtils::SetDataTransferInEvent(nsDragEvent* aDragEvent)
// means, for instance calling the drag service directly, or a drag
// from another application. In either case, a new dataTransfer should
// be created that reflects the data.
initialDataTransfer = new nsDOMDataTransfer(aDragEvent->message, true);
initialDataTransfer = new nsDOMDataTransfer(aDragEvent->message, true, -1);
NS_ENSURE_TRUE(initialDataTransfer, NS_ERROR_OUT_OF_MEMORY);

Просмотреть файл

@ -573,7 +573,7 @@ nsCopySupport::CanCopy(nsIDocument* aDocument)
}
bool
nsCopySupport::FireClipboardEvent(int32_t aType, nsIPresShell* aPresShell, nsISelection* aSelection)
nsCopySupport::FireClipboardEvent(int32_t aType, int32_t aClipboardType, nsIPresShell* aPresShell, nsISelection* aSelection)
{
NS_ASSERTION(aType == NS_CUT || aType == NS_COPY || aType == NS_PASTE,
"Invalid clipboard event type");
@ -633,7 +633,7 @@ nsCopySupport::FireClipboardEvent(int32_t aType, nsIPresShell* aPresShell, nsISe
bool doDefault = true;
nsRefPtr<nsDOMDataTransfer> clipboardData;
if (Preferences::GetBool("dom.event.clipboardevents.enabled", true)) {
clipboardData = new nsDOMDataTransfer(aType, aType == NS_PASTE);
clipboardData = new nsDOMDataTransfer(aType, aType == NS_PASTE, aClipboardType);
nsEventStatus status = nsEventStatus_eIgnore;
nsClipboardEvent evt(true, aType);
@ -675,7 +675,7 @@ nsCopySupport::FireClipboardEvent(int32_t aType, nsIPresShell* aPresShell, nsISe
return false;
}
// call the copy code
rv = HTMLCopy(sel, doc, nsIClipboard::kGlobalClipboard);
rv = HTMLCopy(sel, doc, aClipboardType);
if (NS_FAILED(rv)) {
return false;
}
@ -692,7 +692,7 @@ nsCopySupport::FireClipboardEvent(int32_t aType, nsIPresShell* aPresShell, nsISe
NS_ENSURE_TRUE(transferable, false);
// put the transferable on the clipboard
rv = clipboard->SetData(transferable, nullptr, nsIClipboard::kGlobalClipboard);
rv = clipboard->SetData(transferable, nullptr, aClipboardType);
if (NS_FAILED(rv)) {
return false;
}

Просмотреть файл

@ -7,6 +7,7 @@
#include "nsContentUtils.h"
#include "nsClientRect.h"
#include "nsDOMDataTransfer.h"
#include "nsIClipboard.h"
nsDOMClipboardEvent::nsDOMClipboardEvent(mozilla::dom::EventTarget* aOwner,
nsPresContext* aPresContext,
@ -68,7 +69,7 @@ nsDOMClipboardEvent::Constructor(const mozilla::dom::GlobalObject& aGlobal,
// Always create a clipboardData for the copy event. If this is changed to
// support other types of events, make sure that read/write privileges are
// checked properly within nsDOMDataTransfer.
clipboardData = new nsDOMDataTransfer(NS_COPY, false);
clipboardData = new nsDOMDataTransfer(NS_COPY, false, -1);
clipboardData->SetData(aParam.mDataType, aParam.mData);
}
}
@ -93,10 +94,10 @@ nsDOMClipboardEvent::GetClipboardData()
if (!event->clipboardData) {
if (mEventIsInternal) {
event->clipboardData = new nsDOMDataTransfer(NS_COPY, false);
event->clipboardData = new nsDOMDataTransfer(NS_COPY, false, -1);
} else {
event->clipboardData =
new nsDOMDataTransfer(event->message, event->message == NS_PASTE);
new nsDOMDataTransfer(event->message, event->message == NS_PASTE, nsIClipboard::kGlobalClipboard);
}
}

Просмотреть файл

@ -63,7 +63,7 @@ const char nsDOMDataTransfer::sEffects[8][9] = {
"none", "copy", "move", "copyMove", "link", "copyLink", "linkMove", "all"
};
nsDOMDataTransfer::nsDOMDataTransfer(uint32_t aEventType, bool aIsExternal)
nsDOMDataTransfer::nsDOMDataTransfer(uint32_t aEventType, bool aIsExternal, int32_t aClipboardType)
: mEventType(aEventType),
mDropEffect(nsIDragService::DRAGDROP_ACTION_NONE),
mEffectAllowed(nsIDragService::DRAGDROP_ACTION_UNINITIALIZED),
@ -72,6 +72,7 @@ nsDOMDataTransfer::nsDOMDataTransfer(uint32_t aEventType, bool aIsExternal)
mIsExternal(aIsExternal),
mUserCancelled(false),
mIsCrossDomainSubFrameDrop(false),
mClipboardType(aClipboardType),
mDragImageX(0),
mDragImageY(0)
{
@ -98,6 +99,7 @@ nsDOMDataTransfer::nsDOMDataTransfer(uint32_t aEventType,
bool aIsExternal,
bool aUserCancelled,
bool aIsCrossDomainSubFrameDrop,
int32_t aClipboardType,
nsTArray<nsTArray<TransferItem> >& aItems,
nsIDOMElement* aDragImage,
uint32_t aDragImageX,
@ -110,6 +112,7 @@ nsDOMDataTransfer::nsDOMDataTransfer(uint32_t aEventType,
mIsExternal(aIsExternal),
mUserCancelled(aUserCancelled),
mIsCrossDomainSubFrameDrop(aIsCrossDomainSubFrameDrop),
mClipboardType(aClipboardType),
mItems(aItems),
mDragImage(aDragImage),
mDragImageX(aDragImageX),
@ -653,7 +656,7 @@ nsDOMDataTransfer::Clone(uint32_t aEventType, bool aUserCancelled,
nsDOMDataTransfer* newDataTransfer =
new nsDOMDataTransfer(aEventType, mEffectAllowed, mCursorState,
mIsExternal, aUserCancelled, aIsCrossDomainSubFrameDrop,
mItems, mDragImage, mDragImageX, mDragImageY);
mClipboardType, mItems, mDragImage, mDragImageX, mDragImageY);
NS_ENSURE_TRUE(newDataTransfer, NS_ERROR_OUT_OF_MEMORY);
*aNewDataTransfer = newDataTransfer;
@ -979,7 +982,7 @@ nsDOMDataTransfer::CacheExternalClipboardFormats()
// data will only be retrieved when needed.
nsCOMPtr<nsIClipboard> clipboard = do_GetService("@mozilla.org/widget/clipboard;1");
if (!clipboard) {
if (!clipboard || mClipboardType < 0) {
return;
}
@ -994,8 +997,7 @@ nsDOMDataTransfer::CacheExternalClipboardFormats()
for (uint32_t f = 0; f < mozilla::ArrayLength(formats); ++f) {
// check each format one at a time
bool supported;
clipboard->HasDataMatchingFlavors(&(formats[f]), 1,
nsIClipboard::kGlobalClipboard, &supported);
clipboard->HasDataMatchingFlavors(&(formats[f]), 1, mClipboardType, &supported);
// if the format is supported, add an item to the array with null as
// the data. When retrieved, GetRealData will read the data.
if (supported) {
@ -1036,11 +1038,11 @@ nsDOMDataTransfer::FillInExternalData(TransferItem& aItem, uint32_t aIndex)
MOZ_ASSERT(aIndex == 0, "index in clipboard must be 0");
nsCOMPtr<nsIClipboard> clipboard = do_GetService("@mozilla.org/widget/clipboard;1");
if (!clipboard) {
if (!clipboard || mClipboardType < 0) {
return;
}
clipboard->GetData(trans, nsIClipboard::kGlobalClipboard);
clipboard->GetData(trans, mClipboardType);
} else {
nsCOMPtr<nsIDragSession> dragSession = nsContentUtils::GetDragSession();
if (!dragSession) {

Просмотреть файл

@ -58,6 +58,7 @@ protected:
bool aIsExternal,
bool aUserCancelled,
bool aIsCrossDomainSubFrameDrop,
int32_t aClipboardType,
nsTArray<nsTArray<TransferItem> >& aItems,
nsIDOMElement* aDragImage,
uint32_t aDragImageX,
@ -82,8 +83,10 @@ public:
// paste or a drag that was started without using a data transfer. The
// latter will occur when an external drag occurs, that is, a drag where the
// source is another application, or a drag is started by calling the drag
// service directly.
nsDOMDataTransfer(uint32_t aEventType, bool aIsExternal);
// service directly. For clipboard operations, aClipboardType indicates
// which clipboard to use, from nsIClipboard, or -1 for non-clipboard operations,
// or if access to the system clipboard should not be allowed.
nsDOMDataTransfer(uint32_t aEventType, bool aIsExternal, int32_t aClipboardType);
void GetDragTarget(nsIDOMElement** aDragTarget)
{
@ -178,6 +181,10 @@ protected:
// data should be prevented
bool mIsCrossDomainSubFrameDrop;
// Indicates which clipboard type to use for clipboard operations. Ignored for
// drag and drop.
int32_t mClipboardType;
// array of items, each containing an array of format->data pairs
nsTArray<nsTArray<TransferItem> > mItems;

Просмотреть файл

@ -2027,7 +2027,7 @@ nsEventStateManager::GenerateDragGesture(nsPresContext* aPresContext,
}
nsRefPtr<nsDOMDataTransfer> dataTransfer =
new nsDOMDataTransfer(NS_DRAGDROP_START, false);
new nsDOMDataTransfer(NS_DRAGDROP_START, false, -1);
if (!dataTransfer)
return;

Просмотреть файл

@ -25,6 +25,7 @@
#include "nsIContentViewer.h"
#include "nsFocusManager.h"
#include "nsCopySupport.h"
#include "nsIClipboard.h"
#include "nsGUIEvent.h"
#include "mozilla/Attributes.h"
@ -365,7 +366,7 @@ nsClipboardCommand::DoCommand(const char *aCommandName, nsISupports *aContext)
nsCOMPtr<nsIPresShell> presShell = docShell->GetPresShell();
NS_ENSURE_TRUE(presShell, NS_ERROR_FAILURE);
nsCopySupport::FireClipboardEvent(NS_COPY, presShell, nullptr);
nsCopySupport::FireClipboardEvent(NS_COPY, nsIClipboard::kGlobalClipboard, presShell, nullptr);
return NS_OK;
}

Просмотреть файл

@ -53,6 +53,7 @@ MOCHITEST_FILES = \
test_stylesheetPI.html \
test_showModalDialog.html \
file_showModalDialog.html \
test_paste_selection.html \
$(NULL)
# Disable this test until bug 795711 is fixed.

Просмотреть файл

@ -0,0 +1,114 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Test for middle-click to paste selection with paste events</title>
<script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="application/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<p id="display"></p>
<input id="copy-area" value="CLIPBOARD">
<input id="paste-selection-area" value="" onpaste="pastedSelection(event)">
<input id="paste-global-area" value="" onpaste="pastedGlobal(event)">
<script>
var supportsSelectionClipboard = SpecialPowers.supportsSelectionClipboard();
function checkSelectionClipboardText(count)
{
if ((!supportsSelectionClipboard ||
SpecialPowers.getClipboardData("text/unicode", SpecialPowers.Ci.nsIClipboard.kSelectionClipboard) == "COPY TEXT") &&
SpecialPowers.getClipboardData("text/unicode", SpecialPowers.Ci.nsIClipboard.kGlobalClipboard) == "CLIPBOARD") {
pasteArea();
return;
}
if (count > 10) {
ok(false, "could not set clipboards");
pasteArea();
SimpleTest.finish();
return;
}
setTimeout(checkSelectionClipboardText, 5, count + 1);
}
function selectArea()
{
var copyArea = document.getElementById("copy-area");
copyArea.focus();
copyArea.select();
synthesizeKey("x", { accelKey: true });
if (supportsSelectionClipboard) {
var clipboardHelper = SpecialPowers.Cc["@mozilla.org/widget/clipboardhelper;1"]
.getService(SpecialPowers.Ci.nsIClipboardHelper);
clipboardHelper.copyStringToClipboard("COPY TEXT",
SpecialPowers.Ci.nsIClipboard.kSelectionClipboard,
document);
}
setTimeout(checkSelectionClipboardText, 50, 0);
}
var selectionPasted = false;
var globalPasted = false;
function pasteArea()
{
var pasteArea = document.getElementById("paste-selection-area");
pasteArea.focus();
synthesizeMouse(pasteArea, 8, 8, { button: 1 });
var usesMouseButtonPaste = SpecialPowers.getBoolPref("middlemouse.paste");
if (usesMouseButtonPaste) {
// The data transfer should contain the selection data when the selection clipboard is supported,
// not the global clipboard data.
var expectedText = supportsSelectionClipboard ? "COPY TEXT" : "CLIPBOARD";
is(document.getElementById("paste-selection-area").value, expectedText, "data pasted properly from selection");
ok(selectionPasted, "selection event fired");
}
else {
is(pasteArea.value, "", "data not pasted when middle click not supported");
}
var pasteArea = document.getElementById("paste-global-area");
pasteArea.focus();
synthesizeKey("v", { accelKey: true });
ok(globalPasted, "global event fired");
is(document.getElementById("paste-global-area").value, "CLIPBOARD", "data pasted properly from global clipboard");
SimpleTest.finish();
}
function pastedSelection(event)
{
ok(SpecialPowers.getBoolPref("middlemouse.paste"), "paste on middle click is valid");
// Mac and Windows shouldn't get here as the middle mouse preference is false by default
ok(navigator.platform.indexOf("Mac") == -1 && navigator.platform.indexOf("Win") == -1, "middle click enabled on right platforms");
var expectedText = supportsSelectionClipboard ? "COPY TEXT" : "CLIPBOARD";
is(event.clipboardData.getData("text/plain"), expectedText, "data pasted properly from selection");
selectionPasted = true;
}
function pastedGlobal(event)
{
// The data transfer should contain the global data.
is(event.clipboardData.getData("text/plain"), "CLIPBOARD", "data correct in global clipboard data transfer");
globalPasted = true;
}
SimpleTest.waitForExplicitFinish();
SimpleTest.waitForFocus(selectArea);
</script>
</pre>
</body>
</html>

Просмотреть файл

@ -1341,7 +1341,7 @@ bool nsHTMLEditor::HavePrivateHTMLFlavor(nsIClipboard *aClipboard)
NS_IMETHODIMP nsHTMLEditor::Paste(int32_t aSelectionType)
{
if (!FireClipboardEvent(NS_PASTE))
if (!FireClipboardEvent(NS_PASTE, aSelectionType))
return NS_OK;
// Get Clipboard Service
@ -1422,7 +1422,9 @@ NS_IMETHODIMP nsHTMLEditor::Paste(int32_t aSelectionType)
NS_IMETHODIMP nsHTMLEditor::PasteTransferable(nsITransferable *aTransferable)
{
if (!FireClipboardEvent(NS_PASTE))
// Use an invalid value for the clipboard type as data comes from aTransferable
// and we don't currently implement a way to put that in the data transfer yet.
if (!FireClipboardEvent(NS_PASTE, nsIClipboard::kGlobalClipboard))
return NS_OK;
// handle transferable hooks
@ -1440,7 +1442,7 @@ NS_IMETHODIMP nsHTMLEditor::PasteTransferable(nsITransferable *aTransferable)
//
NS_IMETHODIMP nsHTMLEditor::PasteNoFormatting(int32_t aSelectionType)
{
if (!FireClipboardEvent(NS_PASTE))
if (!FireClipboardEvent(NS_PASTE, aSelectionType))
return NS_OK;
ForceCompositionEnd();

Просмотреть файл

@ -322,7 +322,7 @@ nsresult nsPlaintextEditor::InsertFromDrop(nsIDOMEvent* aDropEvent)
NS_IMETHODIMP nsPlaintextEditor::Paste(int32_t aSelectionType)
{
if (!FireClipboardEvent(NS_PASTE))
if (!FireClipboardEvent(NS_PASTE, aSelectionType))
return NS_OK;
// Get Clipboard Service
@ -353,7 +353,9 @@ NS_IMETHODIMP nsPlaintextEditor::Paste(int32_t aSelectionType)
NS_IMETHODIMP nsPlaintextEditor::PasteTransferable(nsITransferable *aTransferable)
{
if (!FireClipboardEvent(NS_PASTE))
// Use an invalid value for the clipboard type as data comes from aTransferable
// and we don't currently implement a way to put that in the data transfer yet.
if (!FireClipboardEvent(NS_PASTE, -1))
return NS_OK;
if (!IsModifiable())

Просмотреть файл

@ -1137,7 +1137,7 @@ nsPlaintextEditor::CanCutOrCopy()
}
bool
nsPlaintextEditor::FireClipboardEvent(int32_t aType)
nsPlaintextEditor::FireClipboardEvent(int32_t aType, int32_t aSelectionType)
{
if (aType == NS_PASTE)
ForceCompositionEnd();
@ -1149,7 +1149,7 @@ nsPlaintextEditor::FireClipboardEvent(int32_t aType)
if (NS_FAILED(GetSelection(getter_AddRefs(selection))))
return false;
if (!nsCopySupport::FireClipboardEvent(aType, presShell, selection))
if (!nsCopySupport::FireClipboardEvent(aType, aSelectionType, presShell, selection))
return false;
// If the event handler caused the editor to be destroyed, return false.
@ -1159,7 +1159,7 @@ nsPlaintextEditor::FireClipboardEvent(int32_t aType)
NS_IMETHODIMP nsPlaintextEditor::Cut()
{
if (FireClipboardEvent(NS_CUT))
if (FireClipboardEvent(NS_CUT, nsIClipboard::kGlobalClipboard))
return DeleteSelection(eNone, eStrip);
return NS_OK;
}
@ -1173,7 +1173,7 @@ NS_IMETHODIMP nsPlaintextEditor::CanCut(bool *aCanCut)
NS_IMETHODIMP nsPlaintextEditor::Copy()
{
FireClipboardEvent(NS_COPY);
FireClipboardEvent(NS_COPY, nsIClipboard::kGlobalClipboard);
return NS_OK;
}

Просмотреть файл

@ -196,7 +196,7 @@ protected:
bool IsModifiable();
bool CanCutOrCopy();
bool FireClipboardEvent(int32_t aType);
bool FireClipboardEvent(int32_t aType, int32_t aSelectionType);
bool UpdateMetaCharset(nsIDOMDocument* aDocument,
const nsACString& aCharacterSet);

Просмотреть файл

@ -2496,7 +2496,7 @@ NS_IMETHODIMP nsDocumentViewer::SelectAll()
NS_IMETHODIMP nsDocumentViewer::CopySelection()
{
nsCopySupport::FireClipboardEvent(NS_COPY, mPresShell, nullptr);
nsCopySupport::FireClipboardEvent(NS_COPY, nsIClipboard::kGlobalClipboard, mPresShell, nullptr);
return NS_OK;
}

Просмотреть файл

@ -299,6 +299,7 @@
"dom/tests/mochitest/general/test_idleapi_permissions.html":"",
"dom/tests/mochitest/general/test_interfaces.html":"",
"dom/tests/mochitest/general/test_showModalDialog.html": "Don't run modal tests on Android",
"dom/tests/mochitest/general/test_paste_selection.html":"",
"dom/tests/mochitest/geolocation/test_allowCurrent.html":"",
"dom/tests/mochitest/geolocation/test_allowWatch.html":"",
"dom/tests/mochitest/geolocation/test_cachedPosition.html":"",

Просмотреть файл

@ -1346,17 +1346,19 @@ SpecialPowersAPI.prototype = {
sendAsyncMessage("SpecialPowers.Focus", {});
},
getClipboardData: function(flavor) {
getClipboardData: function(flavor, whichClipboard) {
if (this._cb == null)
this._cb = Components.classes["@mozilla.org/widget/clipboard;1"].
getService(Components.interfaces.nsIClipboard);
if (whichClipboard === undefined)
whichClipboard = this._cb.kGlobalClipboard;
var xferable = Components.classes["@mozilla.org/widget/transferable;1"].
createInstance(Components.interfaces.nsITransferable);
xferable.init(this._getDocShell(content.window)
.QueryInterface(Components.interfaces.nsILoadContext));
xferable.addDataFlavor(flavor);
this._cb.getData(xferable, this._cb.kGlobalClipboard);
this._cb.getData(xferable, whichClipboard);
var data = {};
try {
xferable.getTransferData(flavor, data, {});