Skip to content

Commit

Permalink
Bug 1556849 - Enable ESLint for dom/base/. r=Standard8,mccr8
Browse files Browse the repository at this point in the history
# ignore-this-changeset

These are all automatic changes via ESLint/prettier.

Differential Revision: https://phabricator.services.mozilla.com/D37978

--HG--
extra : moz-landing-system : lando
  • Loading branch information
soniasingla committed Jul 15, 2019
1 parent 790ab48 commit f52cea0
Show file tree
Hide file tree
Showing 6 changed files with 270 additions and 159 deletions.
1 change: 0 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
obj*/**

# dom/ exclusions which should be removed (aka ESLint enabled)
dom/base/*.*
dom/media/test/**
!dom/media/test/marionette/yttest/*.js
dom/xhr/**
Expand Down
211 changes: 125 additions & 86 deletions dom/base/ContentAreaDropListener.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
const {OS} = ChromeUtils.import("resource://gre/modules/osfile.jsm");
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
const { OS } = ChromeUtils.import("resource://gre/modules/osfile.jsm");

// This component is used for handling dragover and drop of urls.
//
Expand All @@ -12,20 +12,17 @@ const {OS} = ChromeUtils.import("resource://gre/modules/osfile.jsm");
// access the uri. This prevents, for example, a source document from tricking
// the user into dragging a chrome url.

function ContentAreaDropListener() { };
function ContentAreaDropListener() {}

ContentAreaDropListener.prototype =
{
classID: Components.ID("{1f34bc80-1bc7-11d6-a384-d705dd0746fc}"),
QueryInterface: ChromeUtils.generateQI([Ci.nsIDroppedLinkHandler]),
ContentAreaDropListener.prototype = {
classID: Components.ID("{1f34bc80-1bc7-11d6-a384-d705dd0746fc}"),
QueryInterface: ChromeUtils.generateQI([Ci.nsIDroppedLinkHandler]),

_addLink : function(links, url, name, type)
{
_addLink: function(links, url, name, type) {
links.push({ url, name, type });
},

_addLinksFromItem: function(links, dt, i)
{
_addLinksFromItem: function(links, dt, i) {
let types = dt.mozTypesAt(i);
let type, data;

Expand All @@ -36,8 +33,9 @@ ContentAreaDropListener.prototype =
let urls = data.split("\n");
for (let url of urls) {
// lines beginning with # are comments
if (url.startsWith("#"))
if (url.startsWith("#")) {
continue;
}
url = url.replace(/^\s+|\s+$/g, "");
this._addLink(links, url, url, type);
}
Expand All @@ -61,7 +59,7 @@ ContentAreaDropListener.prototype =
if (types.contains(type)) {
data = dt.mozGetDataAt(type, i);
if (data) {
let lines = data.replace(/^\s+|\s+$/mg, "").split("\n");
let lines = data.replace(/^\s+|\s+$/gm, "").split("\n");
if (!lines.length) {
return;
}
Expand All @@ -74,8 +72,9 @@ ContentAreaDropListener.prototype =
// Add the entire text as a single entry, so that the entire
// text is searched.
let hasURI = false;
let flags = Ci.nsIURIFixup.FIXUP_FLAG_FIX_SCHEME_TYPOS |
Ci.nsIURIFixup.FIXUP_FLAG_ALLOW_KEYWORD_LOOKUP;
let flags =
Ci.nsIURIFixup.FIXUP_FLAG_FIX_SCHEME_TYPOS |
Ci.nsIURIFixup.FIXUP_FLAG_ALLOW_KEYWORD_LOOKUP;
for (let line of lines) {
let info = Services.uriFixup.getFixupURIInfo(line, flags);
if (info.fixedURI) {
Expand All @@ -99,49 +98,59 @@ ContentAreaDropListener.prototype =
// type, which points to the actual file.
let files = dt.files;
if (files && i < files.length) {
this._addLink(links, OS.Path.toFileURI(files[i].mozFullPath),
files[i].name, "application/x-moz-file");
this._addLink(
links,
OS.Path.toFileURI(files[i].mozFullPath),
files[i].name,
"application/x-moz-file"
);
}
},

_getDropLinks : function (dt)
{
_getDropLinks: function(dt) {
let links = [];
for (let i = 0; i < dt.mozItemCount; i++) {
this._addLinksFromItem(links, dt, i);
}
return links;
},

_validateURI: function(dataTransfer, uriString, disallowInherit,
triggeringPrincipal)
{
if (!uriString)
_validateURI: function(
dataTransfer,
uriString,
disallowInherit,
triggeringPrincipal
) {
if (!uriString) {
return "";
}

// Strip leading and trailing whitespace, then try to create a
// URI from the dropped string. If that succeeds, we're
// dropping a URI and we need to do a security check to make
// sure the source document can load the dropped URI.
uriString = uriString.replace(/^\s*|\s*$/g, '');
uriString = uriString.replace(/^\s*|\s*$/g, "");

// Apply URI fixup so that this validation prevents bad URIs even if the
// similar fixup is applied later, especialy fixing typos up will convert
// non-URI to URI.
let fixupFlags = Ci.nsIURIFixup.FIXUP_FLAG_FIX_SCHEME_TYPOS |
Ci.nsIURIFixup.FIXUP_FLAG_ALLOW_KEYWORD_LOOKUP;
let fixupFlags =
Ci.nsIURIFixup.FIXUP_FLAG_FIX_SCHEME_TYPOS |
Ci.nsIURIFixup.FIXUP_FLAG_ALLOW_KEYWORD_LOOKUP;
let info = Services.uriFixup.getFixupURIInfo(uriString, fixupFlags);
if (!info.fixedURI || info.keywordProviderName) {
// Loading a keyword search should always be fine for all cases.
return uriString;
}
let uri = info.fixedURI;

let secMan = Cc["@mozilla.org/scriptsecuritymanager;1"].
getService(Ci.nsIScriptSecurityManager);
let secMan = Cc["@mozilla.org/scriptsecuritymanager;1"].getService(
Ci.nsIScriptSecurityManager
);
let flags = secMan.STANDARD;
if (disallowInherit)
if (disallowInherit) {
flags |= secMan.DISALLOW_INHERIT_PRINCIPAL;
}

secMan.checkLoadURIWithPrincipal(triggeringPrincipal, uri, flags);

Expand All @@ -150,13 +159,17 @@ ContentAreaDropListener.prototype =
return uri.spec;
},

_getTriggeringPrincipalFromDataTransfer: function(aDataTransfer,
fallbackToSystemPrincipal)
{
_getTriggeringPrincipalFromDataTransfer: function(
aDataTransfer,
fallbackToSystemPrincipal
) {
let sourceNode = aDataTransfer.mozSourceNode;
if (sourceNode &&
(sourceNode.localName !== "browser" ||
sourceNode.namespaceURI !== "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul")) {
if (
sourceNode &&
(sourceNode.localName !== "browser" ||
sourceNode.namespaceURI !==
"http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul")
) {
// Use sourceNode's principal only if the sourceNode is not browser.
//
// If sourceNode is browser, the actual triggering principal may be
Expand All @@ -179,33 +192,39 @@ ContentAreaDropListener.prototype =
// TODO: Investigate and describe the difference between them,
// or use only one principal. (Bug 1367038)
if (fallbackToSystemPrincipal) {
let secMan = Cc["@mozilla.org/scriptsecuritymanager;1"].
getService(Ci.nsIScriptSecurityManager);
let secMan = Cc["@mozilla.org/scriptsecuritymanager;1"].getService(
Ci.nsIScriptSecurityManager
);
return secMan.getSystemPrincipal();
} else {
principalURISpec = "file:///";
}
}
let ioService = Cc["@mozilla.org/network/io-service;1"]
.getService(Ci.nsIIOService);
let secMan = Cc["@mozilla.org/scriptsecuritymanager;1"].
getService(Ci.nsIScriptSecurityManager);
return secMan.createContentPrincipal(ioService.newURI(principalURISpec), {});
let ioService = Cc["@mozilla.org/network/io-service;1"].getService(
Ci.nsIIOService
);
let secMan = Cc["@mozilla.org/scriptsecuritymanager;1"].getService(
Ci.nsIScriptSecurityManager
);
return secMan.createContentPrincipal(
ioService.newURI(principalURISpec),
{}
);
},

getTriggeringPrincipal: function(aEvent)
{
getTriggeringPrincipal: function(aEvent) {
let dataTransfer = aEvent.dataTransfer;
return this._getTriggeringPrincipalFromDataTransfer(dataTransfer, true);

},

getCSP: function(aEvent)
{
getCSP: function(aEvent) {
let sourceNode = aEvent.dataTransfer.mozSourceNode;
if (sourceNode &&
(sourceNode.localName !== "browser" ||
sourceNode.namespaceURI !== "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul")) {
if (
sourceNode &&
(sourceNode.localName !== "browser" ||
sourceNode.namespaceURI !==
"http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul")
) {
// Use sourceNode's csp only if the sourceNode is not browser.
//
// If sourceNode is browser, the actual triggering csp may be differ than sourceNode's csp,
Expand All @@ -216,74 +235,89 @@ ContentAreaDropListener.prototype =
return null;
},

canDropLink: function(aEvent, aAllowSameDocument)
{
if (this._eventTargetIsDisabled(aEvent))
canDropLink: function(aEvent, aAllowSameDocument) {
if (this._eventTargetIsDisabled(aEvent)) {
return false;
}

let dataTransfer = aEvent.dataTransfer;
let types = dataTransfer.types;
if (!types.includes("application/x-moz-file") &&
!types.includes("text/x-moz-url") &&
!types.includes("text/uri-list") &&
!types.includes("text/x-moz-text-internal") &&
!types.includes("text/plain"))
if (
!types.includes("application/x-moz-file") &&
!types.includes("text/x-moz-url") &&
!types.includes("text/uri-list") &&
!types.includes("text/x-moz-text-internal") &&
!types.includes("text/plain")
) {
return false;
}

if (aAllowSameDocument)
if (aAllowSameDocument) {
return true;
}

let sourceNode = dataTransfer.mozSourceNode;
if (!sourceNode)
if (!sourceNode) {
return true;
}

// don't allow a drop of a node from the same document onto this one
let sourceDocument = sourceNode.ownerDocument;
let eventDocument = aEvent.originalTarget.ownerDocument;
if (sourceDocument == eventDocument)
if (sourceDocument == eventDocument) {
return false;
}

// also check for nodes in other child or sibling frames by checking
// if both have the same top window.
if (sourceDocument && eventDocument) {
if (sourceDocument.defaultView == null)
if (sourceDocument.defaultView == null) {
return true;
}
let sourceRoot = sourceDocument.defaultView.top;
if (sourceRoot && sourceRoot == eventDocument.defaultView.top)
if (sourceRoot && sourceRoot == eventDocument.defaultView.top) {
return false;
}
}

return true;
},

dropLink: function(aEvent, aName, aDisallowInherit)
{
dropLink: function(aEvent, aName, aDisallowInherit) {
aName.value = "";
let links = this.dropLinks(aEvent, aDisallowInherit);
let url = "";
if (links.length > 0) {
url = links[0].url;
let name = links[0].name;
if (name)
if (name) {
aName.value = name;
}
}

return url;
},

dropLinks: function(aEvent, aDisallowInherit)
{
if (aEvent && this._eventTargetIsDisabled(aEvent))
dropLinks: function(aEvent, aDisallowInherit) {
if (aEvent && this._eventTargetIsDisabled(aEvent)) {
return [];
}

let dataTransfer = aEvent.dataTransfer;
let links = this._getDropLinks(dataTransfer);
let triggeringPrincipal = this._getTriggeringPrincipalFromDataTransfer(dataTransfer, false);
let triggeringPrincipal = this._getTriggeringPrincipalFromDataTransfer(
dataTransfer,
false
);

for (let link of links) {
try {
link.url = this._validateURI(dataTransfer, link.url, aDisallowInherit,
triggeringPrincipal);
link.url = this._validateURI(
dataTransfer,
link.url,
aDisallowInherit,
triggeringPrincipal
);
} catch (ex) {
// Prevent the drop entirely if any of the links are invalid even if
// one of them is valid.
Expand All @@ -296,32 +330,37 @@ ContentAreaDropListener.prototype =
return links;
},

validateURIsForDrop: function(aEvent, aURIs, aDisallowInherit)
{
validateURIsForDrop: function(aEvent, aURIs, aDisallowInherit) {
let dataTransfer = aEvent.dataTransfer;
let triggeringPrincipal = this._getTriggeringPrincipalFromDataTransfer(dataTransfer, false);
let triggeringPrincipal = this._getTriggeringPrincipalFromDataTransfer(
dataTransfer,
false
);

for (let uri of aURIs) {
this._validateURI(dataTransfer, uri, aDisallowInherit,
triggeringPrincipal);
this._validateURI(
dataTransfer,
uri,
aDisallowInherit,
triggeringPrincipal
);
}
},

queryLinks: function(aDataTransfer)
{
queryLinks: function(aDataTransfer) {
return this._getDropLinks(aDataTransfer);
},

_eventTargetIsDisabled: function(aEvent)
{
_eventTargetIsDisabled: function(aEvent) {
let ownerDoc = aEvent.originalTarget.ownerDocument;
if (!ownerDoc || !ownerDoc.defaultView)
if (!ownerDoc || !ownerDoc.defaultView) {
return false;
}

return ownerDoc.defaultView
.windowUtils
.isNodeDisabledForEvents(aEvent.originalTarget);
}
return ownerDoc.defaultView.windowUtils.isNodeDisabledForEvents(
aEvent.originalTarget
);
},
};

var EXPORTED_SYMBOLS = ["ContentAreaDropListener"];
Loading

0 comments on commit f52cea0

Please sign in to comment.