Bug 935259: Follow-up: Split DOMLinkAdded switch statement into methods. r=liuche

This commit is contained in:
Stéphane SCHMIDELY 2015-03-18 18:03:35 +01:00
Родитель c2d58f8397
Коммит 56bf426653
1 изменённых файлов: 134 добавлений и 109 удалений

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

@ -3977,6 +3977,130 @@ Tab.prototype = {
}
},
sanitizeRelString: function(linkRel) {
// Sanitize the rel string
let list = [];
if (linkRel) {
list = linkRel.toLowerCase().split(/\s+/);
let hash = {};
list.forEach(function(value) { hash[value] = true; });
list = [];
for (let rel in hash)
list.push("[" + rel + "]");
}
return list;
},
makeFaviconMessage: function(eventTarget) {
// We want to get the largest icon size possible for our UI.
let maxSize = 0;
// We use the sizes attribute if available
// see http://www.whatwg.org/specs/web-apps/current-work/multipage/links.html#rel-icon
if (eventTarget.hasAttribute("sizes")) {
let sizes = eventTarget.getAttribute("sizes").toLowerCase();
if (sizes == "any") {
// Since Java expects an integer, use -1 to represent icons with sizes="any"
maxSize = -1;
} else {
let tokens = sizes.split(" ");
tokens.forEach(function(token) {
// TODO: check for invalid tokens
let [w, h] = token.split("x");
maxSize = Math.max(maxSize, Math.max(w, h));
});
}
}
return {
type: "Link:Favicon",
tabID: this.id,
href: resolveGeckoURI(eventTarget.href),
size: maxSize,
mime: eventTarget.getAttribute("type") || ""
};
},
makeFeedMessage: function(eventTarget, targetType) {
try {
// urlSecurityCeck will throw if things are not OK
ContentAreaUtils.urlSecurityCheck(eventTarget.href,
eventTarget.ownerDocument.nodePrincipal,
Ci.nsIScriptSecurityManager.DISALLOW_INHERIT_PRINCIPAL);
if (!this.browser.feeds)
this.browser.feeds = [];
this.browser.feeds.push({
href: eventTarget.href,
title: eventTarget.title,
type: targetType
});
return {
type: "Link:Feed",
tabID: this.id
};
} catch (e) {
return null;
}
},
makeOpenSearchMessage: function(eventTarget) {
let type = eventTarget.type && eventTarget.type.toLowerCase();
// Replace all starting or trailing spaces or spaces before "*;" globally w/ "".
type = type.replace(/^\s+|\s*(?:;.*)?$/g, "");
// Check that type matches opensearch.
let isOpenSearch = (type == "application/opensearchdescription+xml");
if (isOpenSearch && eventTarget.title && /^(?:https?|ftp):/i.test(eventTarget.href)) {
Services.search.init(() => {
let visibleEngines = Services.search.getVisibleEngines();
// NOTE: Engines are currently identified by name, but this can be changed
// when Engines are identified by URL (see bug 335102).
if (visibleEngines.some(function(e) {
return e.name == eventTarget.title;
})) {
// This engine is already present, do nothing.
return null;
}
if (this.browser.engines) {
// This engine has already been handled, do nothing.
if (this.browser.engines.some(function(e) {
return e.url == eventTarget.href;
})) {
return null;
}
} else {
this.browser.engines = [];
}
// Get favicon.
let iconURL = eventTarget.ownerDocument.documentURIObject.prePath + "/favicon.ico";
let newEngine = {
title: eventTarget.title,
url: eventTarget.href,
iconURL: iconURL
};
this.browser.engines.push(newEngine);
// Don't send a message to display engines if we've already handled an engine.
if (this.browser.engines.length > 1)
return null;
// Broadcast message that this tab contains search engines that should be visible.
return {
type: "Link:OpenSearch",
tabID: this.id,
visible: true
};
});
}
},
handleEvent: function(aEvent) {
switch (aEvent.type) {
case "DOMContentLoaded": {
@ -4066,6 +4190,7 @@ Tab.prototype = {
case "DOMLinkAdded":
case "DOMLinkChanged": {
let jsonMessage = null;
let target = aEvent.originalTarget;
if (!target.href || target.disabled)
return;
@ -4074,47 +4199,10 @@ Tab.prototype = {
if (target.ownerDocument != this.browser.contentDocument)
return;
// Sanitize the rel string
let list = [];
if (target.rel) {
list = target.rel.toLowerCase().split(/\s+/);
let hash = {};
list.forEach(function(value) { hash[value] = true; });
list = [];
for (let rel in hash)
list.push("[" + rel + "]");
}
// Sanitize rel link
let list = this.sanitizeRelString(target.rel);
if (list.indexOf("[icon]") != -1) {
// We want to get the largest icon size possible for our UI.
let maxSize = 0;
// We use the sizes attribute if available
// see http://www.whatwg.org/specs/web-apps/current-work/multipage/links.html#rel-icon
if (target.hasAttribute("sizes")) {
let sizes = target.getAttribute("sizes").toLowerCase();
if (sizes == "any") {
// Since Java expects an integer, use -1 to represent icons with sizes="any"
maxSize = -1;
} else {
let tokens = sizes.split(" ");
tokens.forEach(function(token) {
// TODO: check for invalid tokens
let [w, h] = token.split("x");
maxSize = Math.max(maxSize, Math.max(w, h));
});
}
}
let json = {
type: "Link:Favicon",
tabID: this.id,
href: resolveGeckoURI(target.href),
size: maxSize,
mime: target.getAttribute("type") || ""
};
Messaging.sendRequest(json);
jsonMessage = this.makeFaviconMessage(target);
} else if (list.indexOf("[alternate]") != -1 && aEvent.type == "DOMLinkAdded") {
let type = target.type.toLowerCase().replace(/^\s+|\s*(?:;.*)?$/g, "");
let isFeed = (type == "application/rss+xml" || type == "application/atom+xml");
@ -4122,77 +4210,14 @@ Tab.prototype = {
if (!isFeed)
return;
try {
// urlSecurityCeck will throw if things are not OK
ContentAreaUtils.urlSecurityCheck(target.href, target.ownerDocument.nodePrincipal, Ci.nsIScriptSecurityManager.DISALLOW_INHERIT_PRINCIPAL);
if (!this.browser.feeds)
this.browser.feeds = [];
this.browser.feeds.push({ href: target.href, title: target.title, type: type });
let json = {
type: "Link:Feed",
tabID: this.id
};
Messaging.sendRequest(json);
} catch (e) {}
jsonMessage = this.makeFeedMessage(target, type);
} else if (list.indexOf("[search]" != -1) && aEvent.type == "DOMLinkAdded") {
let type = target.type && target.type.toLowerCase();
// Replace all starting or trailing spaces or spaces before "*;" globally w/ "".
type = type.replace(/^\s+|\s*(?:;.*)?$/g, "");
// Check that type matches opensearch.
let isOpenSearch = (type == "application/opensearchdescription+xml");
if (isOpenSearch && target.title && /^(?:https?|ftp):/i.test(target.href)) {
Services.search.init(() => {
let visibleEngines = Services.search.getVisibleEngines();
// NOTE: Engines are currently identified by name, but this can be changed
// when Engines are identified by URL (see bug 335102).
if (visibleEngines.some(function(e) {
return e.name == target.title;
})) {
// This engine is already present, do nothing.
return;
}
if (this.browser.engines) {
// This engine has already been handled, do nothing.
if (this.browser.engines.some(function(e) {
return e.url == target.href;
})) {
return;
}
} else {
this.browser.engines = [];
}
// Get favicon.
let iconURL = target.ownerDocument.documentURIObject.prePath + "/favicon.ico";
let newEngine = {
title: target.title,
url: target.href,
iconURL: iconURL
};
this.browser.engines.push(newEngine);
// Don't send a message to display engines if we've already handled an engine.
if (this.browser.engines.length > 1)
return;
// Broadcast message that this tab contains search engines that should be visible.
let newEngineMessage = {
type: "Link:OpenSearch",
tabID: this.id,
visible: true
};
Messaging.sendRequest(newEngineMessage);
});
}
jsonMessage = this.makeOpenSearchMessage(target);
}
if (!jsonMessage)
return;
Messaging.sendRequest(jsonMessage);
break;
}