Bug 1210265 - Add a pref toggle for copying/pasting only plain text on B2G. r=timdream

Add a new preference, clibboard.plainTextOnly.
1. On:  we only support kUnicodeMIME in SetData/GetData.
2. Off: we can support different MIME types in SetData/GetData

BTW, "copy image" is dependent on non-text/rich text support, so this
menu item is also disabled after we turn the pref off.
This commit is contained in:
Boris Chiou 2015-10-02 00:26:00 +02:00
Родитель dc60677bbe
Коммит 50975ecf3c
7 изменённых файлов: 59 добавлений и 5 удалений

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

@ -63,8 +63,7 @@ function hasExpectedFlavors() {
cb.kGlobalClipboard);
}
SimpleTest.waitForExplicitFinish();
SimpleTest.waitForFocus(function nextTest() {
function nextTest() {
var div = document.querySelector("#content>div");
if (!div) {
SimpleTest.finish();
@ -85,6 +84,11 @@ SimpleTest.waitForFocus(function nextTest() {
ok(false, "failed to copy the expected content to the clipboard");
SimpleTest.finish();
});
}
SimpleTest.waitForExplicitFinish();
SimpleTest.waitForFocus(function() {
SpecialPowers.pushPrefEnv({"set": [["clipboard.plainTextOnly", false]]}, nextTest);
});
</script>

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

@ -77,7 +77,9 @@ function testCopyImage () {
}
SimpleTest.waitForExplicitFinish();
addLoadEvent(testCopyImage);
addLoadEvent(function() {
SpecialPowers.pushPrefEnv({"set": [["clipboard.plainTextOnly", false]]}, testCopyImage);
});
</script>
</pre>

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

@ -869,6 +869,7 @@ BrowserElementChild.prototype = {
var elem = e.target;
var menuData = {systemTargets: [], contextmenu: null};
var ctxMenuId = null;
var clipboardPlainTextOnly = Services.prefs.getBoolPref('clipboard.plainTextOnly');
var copyableElements = {
image: false,
link: false,
@ -896,7 +897,7 @@ BrowserElementChild.prototype = {
// Enable copy image/link option
if (elem.nodeName == 'IMG') {
copyableElements.image = true;
copyableElements.image = !clipboardPlainTextOnly;
} else if (elem.nodeName == 'A') {
copyableElements.link = true;
}

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

@ -57,6 +57,10 @@ const browserElementTestHelpers = {
);
},
setClipboardPlainTextOnlyPref: function(value) {
this._setPref('clipboard.plainTextOnly', value);
},
setEnabledPref: function(value) {
this._setPref('dom.mozBrowserFramesEnabled', value);
},

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

@ -2,6 +2,7 @@
SimpleTest.waitForExplicitFinish();
browserElementTestHelpers.setEnabledPref(true);
browserElementTestHelpers.setClipboardPlainTextOnlyPref(false);
browserElementTestHelpers.addPermission();
var audioUrl = 'http://mochi.test:8888/tests/dom/browser-element/mochitest/audio.ogg';

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

@ -2006,6 +2006,9 @@ pref("middlemouse.scrollbarPosition", false);
// Clipboard behavior
pref("clipboard.autocopy", false);
// Clipboard only supports text/plain
pref("clipboard.plainTextOnly", false);
// mouse wheel scroll transaction period of time (in milliseconds)
pref("mousewheel.transaction.timeout", 1500);
// mouse wheel scroll transaction is held even if the mouse cursor is moved.

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

@ -10,6 +10,7 @@
#include "imgIContainer.h"
#include "imgTools.h"
#include "mozilla/dom/ContentChild.h"
#include "mozilla/Preferences.h"
#include "nsClipboardProxy.h"
#include "nsISupportsPrimitives.h"
#include "nsComponentManagerUtils.h"
@ -48,9 +49,29 @@ nsClipboard::SetData(nsITransferable *aTransferable,
return clipboardProxy->SetData(aTransferable, anOwner, aWhichClipboard);
}
// Clear out the clipboard in order to set the new data
// Clear out the clipboard in order to set the new data.
EmptyClipboard(aWhichClipboard);
// Use a pref to toggle rich text/non-text support.
if (Preferences::GetBool("clipboard.plainTextOnly")) {
nsCOMPtr<nsISupports> clip;
uint32_t len;
nsresult rv = aTransferable->GetTransferData(kUnicodeMime,
getter_AddRefs(clip),
&len);
if (NS_FAILED(rv)) {
return rv;
}
nsCOMPtr<nsISupportsString> wideString = do_QueryInterface(clip);
if (!wideString) {
return NS_ERROR_NOT_IMPLEMENTED;
}
nsAutoString utf16string;
wideString->GetData(utf16string);
mClipboard->SetText(utf16string);
return NS_OK;
}
// Get the types of supported flavors.
nsCOMPtr<nsISupportsArray> flavorList;
nsresult rv = aTransferable->FlavorsTransferableCanExport(getter_AddRefs(flavorList));
@ -161,6 +182,24 @@ nsClipboard::GetData(nsITransferable *aTransferable,
return clipboardProxy->GetData(aTransferable, aWhichClipboard);
}
// Use a pref to toggle rich text/non-text support.
if (Preferences::GetBool("clipboard.plainTextOnly")) {
nsresult rv;
nsCOMPtr<nsISupportsString> dataWrapper =
do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID, &rv);
rv = dataWrapper->SetData(mClipboard->GetText());
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
nsCOMPtr<nsISupports> genericDataWrapper = do_QueryInterface(dataWrapper);
uint32_t len = mClipboard->GetText().Length() * sizeof(char16_t);
rv = aTransferable->SetTransferData(kUnicodeMime, genericDataWrapper, len);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
return NS_OK;
}
// Get flavor list that includes all acceptable flavors (including
// ones obtained through conversion).
// Note: We don't need to call nsITransferable::AddDataFlavor here