Skip to content

Commit

Permalink
Bug 1324410 - fix dnd to the location bar for plaintext, r=mak
Browse files Browse the repository at this point in the history
MozReview-Commit-ID: 7md3hnxisDP

--HG--
extra : rebase_source : b37fc117474315a50963f682636f9ea13f53e8e2
  • Loading branch information
gijsk committed Dec 19, 2016
1 parent 8079695 commit 1df3298
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 18 deletions.
25 changes: 24 additions & 1 deletion browser/base/content/test/urlbar/browser_dragdropURL.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

const TEST_URL = "data:text/html,a test page";
const DRAG_URL = "http://www.example.com/";
const DRAG_FORBIDDEN_URL = "chrome://browser/content/aboutDialog.xul";
const DRAG_TEXT = "Firefox is awesome";
const DRAG_WORD = "Firefox";

add_task(function* checkURLBarUpdateForDrag() {
add_task(function* checkDragURL() {
yield BrowserTestUtils.withNewTab(TEST_URL, function* (browser) {
// Have to use something other than the URL bar as a source, so picking the
// downloads button somewhat arbitrarily:
Expand All @@ -13,3 +16,23 @@ add_task(function* checkURLBarUpdateForDrag() {
is(gBrowser.selectedBrowser.userTypedValue, null, "Stored URL bar value should not have changed");
});
});

add_task(function* checkDragForbiddenURL() {
yield BrowserTestUtils.withNewTab(TEST_URL, function* (browser) {
EventUtils.synthesizeDrop(document.getElementById("downloads-button"), gURLBar,
[[{type: "text/plain", data: DRAG_FORBIDDEN_URL}]], "copy", window);
isnot(gURLBar.value, DRAG_FORBIDDEN_URL, "Shouldn't be allowed to drop forbidden URL on URL bar");
});
});

add_task(function* checkDragText() {
yield BrowserTestUtils.withNewTab(TEST_URL, function* (browser) {
EventUtils.synthesizeDrop(document.getElementById("downloads-button"), gURLBar,
[[{type: "text/plain", data: DRAG_TEXT}]], "copy", window);
is(gURLBar.value, DRAG_TEXT, "Dragging normal text should replace the URL bar value");

EventUtils.synthesizeDrop(document.getElementById("downloads-button"), gURLBar,
[[{type: "text/plain", data: DRAG_WORD}]], "copy", window);
is(gURLBar.value, DRAG_WORD, "Dragging a single word should replace the URL bar value");
});
});
60 changes: 43 additions & 17 deletions browser/base/content/urlbarBindings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -701,10 +701,23 @@ file, You can obtain one at http://mozilla.org/MPL/2.0/.
]]></body>
</method>

<method name="_getDroppableLink">
<!-- Returns:
null if there's a security issue and we should do nothing.
a URL object if there is one that we're OK with loading,
a text value otherwise.
-->
<method name="_getDroppableItem">
<parameter name="aEvent"/>
<body><![CDATA[
let links = browserDragAndDrop.dropLinks(aEvent);
let links;
try {
links = browserDragAndDrop.dropLinks(aEvent);
} catch (ex) {
// this is possibly a security exception, in which case we should return
// null. Always return null because we can't *know* what exception is
// being returned.
return null;
}
// The URL bar automatically handles inputs with newline characters,
// so we can get away with treating text/x-moz-url flavours as text/plain.
if (links.length > 0 && links[0].url) {
Expand All @@ -715,24 +728,35 @@ file, You can obtain one at http://mozilla.org/MPL/2.0/.
aEvent.stopImmediatePropagation();
return null;
}
let urlObj;
try {
urlSecurityCheck(url,
gBrowser.contentPrincipal,
Ci.nsIScriptSecurityManager.DISALLOW_INHERIT_PRINCIPAL);
// If this throws, urlSecurityCheck would also throw, as that's what it
// does with things that don't pass the IO service's newURI constructor
// without fixup. It's conceivable we may want to relax this check in
// the future (so e.g. www.foo.com gets fixed up), but not right now.
urlObj = new URL(url);
// If we succeed, try to pass security checks. If this works, return the
// URL object. If the *security checks* fail, return null.
try {
urlSecurityCheck(url,
gBrowser.contentPrincipal,
Ci.nsIScriptSecurityManager.DISALLOW_INHERIT_PRINCIPAL);
return urlObj;
} catch (ex) {
return null;
}
} catch (ex) {
return null;
// We couldn't make a URL out of this. Continue on, and return text below.
}
return url;
}
return null;
return aEvent.dataTransfer.getData("text/unicode");
]]></body>
</method>

<method name="onDragOver">
<parameter name="aEvent"/>
<body><![CDATA[
// We don't need the link here, so we ignore the return value.
if (!this._getDroppableLink(aEvent)) {
if (!this._getDroppableItem(aEvent)) {
aEvent.dataTransfer.dropEffect = "none";
}
]]></body>
Expand All @@ -741,15 +765,17 @@ file, You can obtain one at http://mozilla.org/MPL/2.0/.
<method name="onDrop">
<parameter name="aEvent"/>
<body><![CDATA[
let url = this._getDroppableLink(aEvent);
if (url) {
this.value = url;
let droppedItem = this._getDroppableItem(aEvent);
if (droppedItem) {
this.value = droppedItem instanceof URL ? droppedItem.href : droppedItem;
SetPageProxyState("invalid");
this.focus();
this.handleCommand();
// Force not showing the dropped URI immediately.
gBrowser.userTypedValue = null;
URLBarSetURI();
if (droppedItem instanceof URL) {
this.handleCommand();
// Force not showing the dropped URI immediately.
gBrowser.userTypedValue = null;
URLBarSetURI();
}
}
]]></body>
</method>
Expand Down

0 comments on commit 1df3298

Please sign in to comment.