2009-07-17 20:08:47 -07:00
|
|
|
// -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*-
|
|
|
|
/*
|
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Mobile Browser.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Mozilla Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2009
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Roy Frostig <rfrostig@mozilla.com>
|
2009-08-07 18:08:06 -07:00
|
|
|
* Ben Combee <bcombee@mozilla.com>
|
2010-04-27 11:22:38 -07:00
|
|
|
* Matt Brubeck <mbrubeck@mozilla.com>
|
2009-07-17 20:08:47 -07:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
// -----------------------------------------------------------
|
|
|
|
// General util/convenience tools
|
|
|
|
//
|
|
|
|
|
|
|
|
let Util = {
|
2009-09-04 22:14:59 -07:00
|
|
|
/** printf-like dump function */
|
|
|
|
dumpf: function dumpf(str) {
|
2010-09-03 02:01:25 -07:00
|
|
|
let args = arguments;
|
|
|
|
let i = 1;
|
2009-09-04 22:14:59 -07:00
|
|
|
dump(str.replace(/%s/g, function() {
|
|
|
|
if (i >= args.length) {
|
|
|
|
throw "dumps received too many placeholders and not enough arguments";
|
|
|
|
}
|
|
|
|
return args[i++].toString();
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
|
2009-08-12 14:40:43 -07:00
|
|
|
/** Like dump, but each arg is handled and there's an automatic newline */
|
2009-08-07 18:08:06 -07:00
|
|
|
dumpLn: function dumpLn() {
|
2010-09-03 02:01:25 -07:00
|
|
|
for (let i = 0; i < arguments.length; i++)
|
|
|
|
dump(arguments[i] + " ");
|
2009-08-07 18:08:06 -07:00
|
|
|
dump("\n");
|
2009-09-01 20:39:39 -07:00
|
|
|
},
|
|
|
|
|
2010-06-09 16:07:12 -07:00
|
|
|
getWindowUtils: function getWindowUtils(aWindow) {
|
|
|
|
return aWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils);
|
|
|
|
},
|
|
|
|
|
2009-09-01 20:39:39 -07:00
|
|
|
/** Executes aFunc after other events have been processed. */
|
|
|
|
executeSoon: function executeSoon(aFunc) {
|
2010-07-13 07:36:09 -07:00
|
|
|
Services.tm.mainThread.dispatch({
|
2009-09-01 20:39:39 -07:00
|
|
|
run: function() {
|
|
|
|
aFunc();
|
|
|
|
}
|
|
|
|
}, Ci.nsIThread.DISPATCH_NORMAL);
|
2009-10-23 09:02:40 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
getHrefForElement: function getHrefForElement(target) {
|
|
|
|
// XXX: This is kind of a hack to work around a Gecko bug (see bug 266932)
|
|
|
|
// We're going to walk up the DOM looking for a parent link node.
|
|
|
|
// This shouldn't be necessary, but we're matching the existing behaviour for left click
|
|
|
|
|
|
|
|
let link = null;
|
|
|
|
while (target) {
|
2010-06-29 11:15:07 -07:00
|
|
|
if (target instanceof Ci.nsIDOMHTMLAnchorElement ||
|
|
|
|
target instanceof Ci.nsIDOMHTMLAreaElement ||
|
|
|
|
target instanceof Ci.nsIDOMHTMLLinkElement) {
|
2009-10-23 09:02:40 -07:00
|
|
|
if (target.hasAttribute("href"))
|
|
|
|
link = target;
|
|
|
|
}
|
|
|
|
target = target.parentNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (link && link.hasAttribute("href"))
|
|
|
|
return link.href;
|
|
|
|
else
|
|
|
|
return null;
|
2009-10-28 21:37:58 -07:00
|
|
|
},
|
|
|
|
|
2010-06-29 11:15:07 -07:00
|
|
|
makeURI: function makeURI(aURL, aOriginCharset, aBaseURI) {
|
2010-07-13 07:36:09 -07:00
|
|
|
return Services.io.newURI(aURL, aOriginCharset, aBaseURI);
|
2010-06-29 11:15:07 -07:00
|
|
|
},
|
|
|
|
|
2010-03-24 11:22:18 -07:00
|
|
|
makeURLAbsolute: function makeURLAbsolute(base, url) {
|
|
|
|
// Note: makeURI() will throw if url is not a valid URI
|
2010-06-29 11:15:07 -07:00
|
|
|
return this.makeURI(url, null, this.makeURI(base)).spec;
|
2010-03-24 11:22:18 -07:00
|
|
|
},
|
|
|
|
|
2010-07-02 10:48:46 -07:00
|
|
|
isLocalScheme: function isLocalScheme(aURL) {
|
2010-12-09 07:33:43 -08:00
|
|
|
return (aURL.indexOf("about:") == 0 && aURL != "about:blank" && aURL != "about:empty") || aURL.indexOf("chrome:") == 0;
|
2010-07-02 10:48:46 -07:00
|
|
|
},
|
|
|
|
|
2011-02-28 14:46:40 -08:00
|
|
|
isOpenableScheme: function isShareableScheme(aProtocol) {
|
|
|
|
let dontOpen = /^(mailto|javascript|news|snews)$/;
|
|
|
|
return (aProtocol && !dontOpen.test(aProtocol));
|
|
|
|
},
|
|
|
|
|
2010-08-23 17:27:40 -07:00
|
|
|
isShareableScheme: function isShareableScheme(aProtocol) {
|
|
|
|
let dontShare = /^(chrome|about|file|javascript|resource)$/;
|
|
|
|
return (aProtocol && !dontShare.test(aProtocol));
|
|
|
|
},
|
|
|
|
|
2010-04-29 12:29:01 -07:00
|
|
|
clamp: function(num, min, max) {
|
|
|
|
return Math.max(min, Math.min(max, num));
|
|
|
|
},
|
|
|
|
|
2010-04-08 22:08:53 -07:00
|
|
|
/** Don't display anything in the urlbar for these special URIs. */
|
|
|
|
isURLEmpty: function isURLEmpty(aURL) {
|
2010-08-31 12:49:40 -07:00
|
|
|
return (!aURL || aURL == "about:blank" || aURL == "about:empty" || aURL == "about:home");
|
2010-04-08 22:08:53 -07:00
|
|
|
},
|
|
|
|
|
2010-01-22 09:52:24 -08:00
|
|
|
/** Recursively find all documents, including root document. */
|
|
|
|
getAllDocuments: function getAllDocuments(doc, resultSoFar) {
|
|
|
|
resultSoFar = resultSoFar || [doc];
|
|
|
|
if (!doc.defaultView)
|
|
|
|
return resultSoFar;
|
|
|
|
let frames = doc.defaultView.frames;
|
|
|
|
if (!frames)
|
|
|
|
return resultSoFar;
|
|
|
|
|
|
|
|
let i;
|
|
|
|
let currentDoc;
|
|
|
|
for (i = 0; i < frames.length; i++) {
|
|
|
|
currentDoc = frames[i].document;
|
|
|
|
resultSoFar.push(currentDoc);
|
|
|
|
this.getAllDocuments(currentDoc, resultSoFar);
|
|
|
|
}
|
|
|
|
|
|
|
|
return resultSoFar;
|
|
|
|
},
|
|
|
|
|
2009-11-24 10:23:41 -08:00
|
|
|
// Put the Mozilla networking code into a state that will kick the auto-connection
|
|
|
|
// process.
|
|
|
|
forceOnline: function forceOnline() {
|
2010-07-13 07:36:09 -07:00
|
|
|
Services.io.offline = false;
|
2010-04-30 12:30:28 -07:00
|
|
|
},
|
2010-06-09 16:07:12 -07:00
|
|
|
|
2010-09-15 23:28:12 -07:00
|
|
|
isParentProcess: function isInParentProcess() {
|
|
|
|
let appInfo = Cc["@mozilla.org/xre/app-info;1"];
|
|
|
|
return (!appInfo || appInfo.getService(Ci.nsIXULRuntime).processType == Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT);
|
|
|
|
},
|
|
|
|
|
2011-09-07 13:47:35 -07:00
|
|
|
isTablet: function isTablet(options) {
|
|
|
|
let forceUpdate = options && 'forceUpdate' in options && options.forceUpdate;
|
|
|
|
|
|
|
|
if ('_isTablet' in this && !forceUpdate)
|
|
|
|
return this._isTablet;
|
|
|
|
|
|
|
|
let tabletPref = Services.prefs.getIntPref("browser.ui.layout.tablet");
|
|
|
|
|
|
|
|
// Act according to user prefs if tablet mode has been
|
|
|
|
// explicitly disabled or enabled.
|
|
|
|
if (tabletPref == 0)
|
|
|
|
return this._isTablet = false;
|
|
|
|
else if (tabletPref == 1)
|
|
|
|
return this._isTablet = true;
|
|
|
|
|
2011-11-28 11:47:27 -08:00
|
|
|
#ifdef ANDROID
|
|
|
|
// Disable tablet mode on non-honeycomb devices because of theme bugs (bug 705026)
|
|
|
|
let sysInfo = Cc["@mozilla.org/system-info;1"].getService(Ci.nsIPropertyBag2);
|
|
|
|
let shellVersion = sysInfo.get("shellVersion") || "";
|
|
|
|
let matches = shellVersion.match(/\((\d+)\)$/);
|
|
|
|
if (matches) {
|
|
|
|
let sdkVersion = parseInt(matches[1]);
|
|
|
|
if (sdkVersion < 11 || sdkVersion > 13)
|
|
|
|
return this._isTablet = false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-06-13 13:32:17 -07:00
|
|
|
let dpi = this.displayDPI;
|
2011-05-20 03:18:19 -07:00
|
|
|
if (dpi <= 96)
|
2011-09-07 13:47:35 -07:00
|
|
|
return this._isTablet = (window.innerWidth > 1024);
|
2011-05-20 03:18:19 -07:00
|
|
|
|
2011-05-17 02:55:05 -07:00
|
|
|
// See the tablet_panel_minwidth from mobile/themes/core/defines.inc
|
|
|
|
let tablet_panel_minwidth = 124;
|
2011-05-20 03:18:19 -07:00
|
|
|
let dpmm = 25.4 * window.innerWidth / dpi;
|
2011-09-07 13:47:35 -07:00
|
|
|
return this._isTablet = (dpmm >= tablet_panel_minwidth);
|
2011-05-17 02:55:05 -07:00
|
|
|
},
|
|
|
|
|
2010-04-30 12:30:28 -07:00
|
|
|
isPortrait: function isPortrait() {
|
2010-12-15 11:15:42 -08:00
|
|
|
#ifdef MOZ_PLATFORM_MAEMO
|
|
|
|
return (screen.width <= screen.height);
|
|
|
|
#elifdef ANDROID
|
|
|
|
return (screen.width <= screen.height);
|
|
|
|
#else
|
|
|
|
return (window.innerWidth <= window.innerHeight);
|
|
|
|
#endif
|
2011-05-20 03:18:19 -07:00
|
|
|
},
|
|
|
|
|
2011-09-08 09:02:08 -07:00
|
|
|
modifierMaskFromEvent: function modifierMaskFromEvent(aEvent) {
|
|
|
|
return (aEvent.altKey ? Ci.nsIDOMNSEvent.ALT_MASK : 0) |
|
|
|
|
(aEvent.ctrlKey ? Ci.nsIDOMNSEvent.CONTROL_MASK : 0) |
|
|
|
|
(aEvent.shiftKey ? Ci.nsIDOMNSEvent.SHIFT_MASK : 0) |
|
|
|
|
(aEvent.metaKey ? Ci.nsIDOMNSEvent.META_MASK : 0);
|
|
|
|
},
|
|
|
|
|
2011-06-13 13:32:17 -07:00
|
|
|
get displayDPI() {
|
|
|
|
delete this.displayDPI;
|
|
|
|
return this.displayDPI = this.getWindowUtils(window).displayDPI;
|
2011-09-01 10:04:29 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
LOCALE_DIR_RTL: -1,
|
|
|
|
LOCALE_DIR_LTR: 1,
|
|
|
|
get localeDir() {
|
|
|
|
// determine browser dir first to know which direction to snap to
|
|
|
|
let chromeReg = Cc["@mozilla.org/chrome/chrome-registry;1"].getService(Ci.nsIXULChromeRegistry);
|
|
|
|
return chromeReg.isLocaleRTL("global") ? this.LOCALE_DIR_RTL : this.LOCALE_DIR_LTR;
|
2011-09-26 17:32:10 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
createShortcut: function Util_createShortcut(aTitle, aURL, aIconURL, aType) {
|
|
|
|
// The background images are 72px, but Android will resize as needed.
|
|
|
|
// Bigger is better than too small.
|
|
|
|
const kIconSize = 72;
|
|
|
|
const kOverlaySize = 32;
|
|
|
|
const kOffset = 20;
|
|
|
|
|
|
|
|
// We have to fallback to something
|
|
|
|
aTitle = aTitle || aURL;
|
|
|
|
|
|
|
|
let canvas = document.createElementNS("http://www.w3.org/1999/xhtml", "canvas");
|
|
|
|
canvas.setAttribute("style", "display: none");
|
|
|
|
|
|
|
|
function _createShortcut() {
|
|
|
|
let icon = canvas.toDataURL("image/png", "");
|
|
|
|
canvas = null;
|
|
|
|
try {
|
|
|
|
let shell = Cc["@mozilla.org/browser/shell-service;1"].createInstance(Ci.nsIShellService);
|
|
|
|
shell.createShortcut(aTitle, aURL, icon, aType);
|
|
|
|
} catch(e) {
|
|
|
|
Cu.reportError(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the main background image first
|
|
|
|
let image = new Image();
|
|
|
|
image.onload = function() {
|
|
|
|
canvas.width = canvas.height = kIconSize;
|
|
|
|
let ctx = canvas.getContext("2d");
|
|
|
|
ctx.drawImage(image, 0, 0, kIconSize, kIconSize);
|
|
|
|
|
|
|
|
// If we have a favicon, lets draw it next
|
|
|
|
if (aIconURL) {
|
|
|
|
let favicon = new Image();
|
|
|
|
favicon.onload = function() {
|
|
|
|
// Center the favicon and overlay it on the background
|
|
|
|
ctx.drawImage(favicon, kOffset, kOffset, kOverlaySize, kOverlaySize);
|
|
|
|
_createShortcut();
|
|
|
|
}
|
|
|
|
|
|
|
|
favicon.onerror = function() {
|
|
|
|
Cu.reportError("CreateShortcut: favicon image load error");
|
|
|
|
}
|
|
|
|
|
|
|
|
favicon.src = aIconURL;
|
|
|
|
} else {
|
|
|
|
_createShortcut();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
image.onerror = function() {
|
|
|
|
Cu.reportError("CreateShortcut: background image load error");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pick the right background
|
|
|
|
image.src = aIconURL ? "chrome://browser/skin/images/homescreen-blank-hdpi.png"
|
|
|
|
: "chrome://browser/skin/images/homescreen-default-hdpi.png";
|
|
|
|
},
|
2009-07-17 20:08:47 -07:00
|
|
|
};
|
|
|
|
|
2010-06-09 16:07:12 -07:00
|
|
|
|
2010-06-23 15:16:01 -07:00
|
|
|
/**
|
|
|
|
* Helper class to nsITimer that adds a little more pizazz. Callback can be an
|
|
|
|
* object with a notify method or a function.
|
|
|
|
*/
|
|
|
|
Util.Timeout = function(aCallback) {
|
|
|
|
this._callback = aCallback;
|
|
|
|
this._timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
|
2010-08-16 20:35:34 -07:00
|
|
|
this._type = null;
|
2010-07-20 13:49:31 -07:00
|
|
|
};
|
2010-06-23 15:16:01 -07:00
|
|
|
|
|
|
|
Util.Timeout.prototype = {
|
|
|
|
/** Timer callback. Don't call this manually. */
|
|
|
|
notify: function notify() {
|
2010-08-16 20:35:34 -07:00
|
|
|
if (this._type == this._timer.TYPE_ONE_SHOT)
|
|
|
|
this._type = null;
|
|
|
|
|
2010-06-23 15:16:01 -07:00
|
|
|
if (this._callback.notify)
|
|
|
|
this._callback.notify();
|
|
|
|
else
|
|
|
|
this._callback.apply(null);
|
|
|
|
},
|
|
|
|
|
2010-08-16 20:35:34 -07:00
|
|
|
/** Helper function for once and interval. */
|
|
|
|
_start: function _start(aDelay, aType, aCallback) {
|
2010-06-23 15:16:01 -07:00
|
|
|
if (aCallback)
|
|
|
|
this._callback = aCallback;
|
|
|
|
this.clear();
|
2010-08-16 20:35:34 -07:00
|
|
|
this._timer.initWithCallback(this, aDelay, aType);
|
|
|
|
this._type = aType;
|
2010-06-23 15:16:01 -07:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2010-08-16 20:35:34 -07:00
|
|
|
/** Do the callback once. Cancels other timeouts on this object. */
|
|
|
|
once: function once(aDelay, aCallback) {
|
|
|
|
return this._start(aDelay, this._timer.TYPE_ONE_SHOT, aCallback);
|
|
|
|
},
|
|
|
|
|
2010-06-23 15:16:01 -07:00
|
|
|
/** Do the callback every aDelay msecs. Cancels other timeouts on this object. */
|
|
|
|
interval: function interval(aDelay, aCallback) {
|
2010-08-16 20:35:34 -07:00
|
|
|
return this._start(aDelay, this._timer.TYPE_REPEATING_SLACK, aCallback);
|
2010-06-23 15:16:01 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/** Clear any pending timeouts. */
|
|
|
|
clear: function clear() {
|
2010-09-22 15:03:48 -07:00
|
|
|
if (this.isPending()) {
|
2010-06-23 15:16:01 -07:00
|
|
|
this._timer.cancel();
|
2010-08-16 20:35:34 -07:00
|
|
|
this._type = null;
|
2010-06-23 15:16:01 -07:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
/** If there is a pending timeout, call it and cancel the timeout. */
|
|
|
|
flush: function flush() {
|
2010-09-22 15:03:48 -07:00
|
|
|
if (this.isPending()) {
|
2010-06-23 15:16:01 -07:00
|
|
|
this.notify();
|
2010-08-16 20:35:34 -07:00
|
|
|
this.clear();
|
2010-06-23 15:16:01 -07:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
/** Return true iff we are waiting for a callback. */
|
|
|
|
isPending: function isPending() {
|
2010-09-22 15:03:48 -07:00
|
|
|
return this._type !== null;
|
2010-06-23 15:16:01 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|