Skip to content
This repository has been archived by the owner on Feb 26, 2022. It is now read-only.

Bug 706590 - API for implementing XPCOM interfaces #305

Merged
merged 37 commits into from Feb 5, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
71938a6
Merge branch 'bug/compose-702835' into bug/shared.modules-706590
Gozala Dec 5, 2011
aac050b
Define module for generating UUID's.
Gozala Nov 18, 2011
fd9afb3
Merge remote-tracking branch 'upstream/master' into bug/shared.module…
Gozala Dec 6, 2011
132b1fa
Implement Unknown and Factory base XPCOM components.
Gozala Dec 7, 2011
8d7a486
Update tests for xpcom module.
Gozala Dec 7, 2011
c757842
Update timer implementation that depended on old `xpcom`
Gozala Dec 7, 2011
6b54275
Update observer-service that depended on old xpcom.
Gozala Dec 7, 2011
7fa3ba7
Update memory module that assumed that every object has constructor p…
Gozala Dec 7, 2011
42bd88a
Update widget module that depended on old xpcom module.
Gozala Dec 7, 2011
d8b8018
Update selection module that depended on old xpcom module.
Gozala Dec 7, 2011
4968bd5
Update request module that depended on old xpcom module.
Gozala Dec 7, 2011
f7bbcfa
Add self into contributors of the modified files.
Gozala Dec 15, 2011
40936fc
Merge remote-tracking branch 'upstream/master' into bug/xpcom-706590
Gozala Dec 16, 2011
6ef1eae
Merge remote-tracking branch 'upstream/master' into bug/xpcom-706590
Gozala Dec 17, 2011
164801e
Remove unused sdkVersion variable.
Gozala Dec 22, 2011
c081ee6
Adding uuid tests.
Gozala Dec 22, 2011
1863cb6
Merge remote-tracking branch 'upstream/master' into bug/xpcom-706590
Gozala Jan 13, 2012
d662597
Use `ns` function exported by `Namespace` module in order to avoid co…
Gozala Jan 13, 2012
775e99e
Factor out `Service` exemplar from the `Factory` and change `createIn…
Gozala Jan 13, 2012
d6405a7
Update tests for xpcom module to include ones suggested by @ochameau
Gozala Jan 16, 2012
18153c4
Update XPCOM module documentation.
Gozala Jan 16, 2012
1cc7603
Merge remote-tracking branch 'upstream/master' into bug/xpcom-706590
Gozala Jan 31, 2012
04cb16f
Update API as suggested by @ochameau
Gozala Feb 1, 2012
29b511d
Fix regression introduced by moving `wrappedJSObject` to an `Unknown`.
Gozala Feb 1, 2012
72f26c7
Refine implementation so that `Service` inherits from `Factory` that …
Gozala Feb 1, 2012
a685c27
Allow `null` contractID.
Gozala Feb 1, 2012
f28dbf6
Update tests.
Gozala Feb 1, 2012
80d1a3b
Simplify observer service.
Gozala Feb 1, 2012
12721cf
Rename `getClass` to more descriptive `factoryByContract`.
Gozala Feb 2, 2012
666df10
Update implementation to use shorter property names.
Gozala Feb 2, 2012
393ea33
Update documentation.
Gozala Feb 2, 2012
cf21e2b
Adding better description to the factoryByContract.
Gozala Feb 2, 2012
7de95cb
Update tests.
Gozala Feb 2, 2012
992bf09
Add suggested test case.
Gozala Feb 4, 2012
3c45576
fix indentation.
Gozala Feb 4, 2012
ef89c9b
Use shorter function syntax.
Gozala Feb 4, 2012
7e56d38
improve code comments as suggested by ochameau.
Gozala Feb 4, 2012
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
57 changes: 28 additions & 29 deletions packages/addon-kit/lib/request.js
Expand Up @@ -3,11 +3,15 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";
const xpcom = require("api-utils/xpcom");

const { Base } = require("api-utils/base");
const { ns } = require("api-utils/namespace");
const xhr = require("api-utils/xhr");
const errors = require("api-utils/errors");
const apiUtils = require("api-utils/api-utils");

const response = ns();

// Ugly but will fix with: https://bugzilla.mozilla.org/show_bug.cgi?id=596248
const EventEmitter = require('api-utils/events').EventEmitter.compose({
constructor: function EventEmitter() this
Expand Down Expand Up @@ -88,7 +92,7 @@ function Request(options) {
// handle the readystate, create the response, and call the callback
request.onreadystatechange = function () {
if (request.readyState == 4) {
response = new Response(request);
response = Response.new(request);
errors.catchAndLog(function () {
self._emit('complete', response);
})();
Expand Down Expand Up @@ -195,34 +199,29 @@ function fixedEncodeURIComponent (str) {
replace(/\)/g, "%29").replace(/\*/g, "%2A");
}

function Response(request) {
// Define the straight mappings of our value to original request value
xpcom.utils.defineLazyGetter(this, "text", function () request.responseText);
xpcom.utils.defineLazyGetter(this, "xml", function () {
const Response = Base.extend({
initialize: function initialize(request) {
response(this).request = request;
},
get text() response(this).request.responseText,
get xml() {
throw new Error("Sorry, the 'xml' property is no longer available. " +
"see bug 611042 for more information.");
});
xpcom.utils.defineLazyGetter(this, "status", function () request.status);
xpcom.utils.defineLazyGetter(this, "statusText", function () request.statusText);

// this.json should be the JS object, so we need to attempt to parse it.
xpcom.utils.defineLazyGetter(this, "json", function () {
let _json = null;
},
get status() response(this).request.status,
get statusText() response(this).request.statusText,
get json() {
try {
_json = JSON.parse(this.text);
return JSON.parse(this.text);
} catch(error) {
return null;
}
catch (e) {}
return _json;
});

// this.headers also should be a JS object, so we need to split up the raw
// headers string provided by the request.
xpcom.utils.defineLazyGetter(this, "headers", function () {
let _headers = {};
let lastKey;
},
get headers() {
let headers = {}, lastKey;
// Since getAllResponseHeaders() will return null if there are no headers,
// defend against it by defaulting to ""
let rawHeaders = request.getAllResponseHeaders() || "";
let rawHeaders = response(this).request.getAllResponseHeaders() || "";
rawHeaders.split("\n").forEach(function (h) {
// According to the HTTP spec, the header string is terminated by an empty
// line, so we can just skip it.
Expand All @@ -241,16 +240,16 @@ function Response(request) {
// new line. We'll assume lastKey will be set because there should never
// be an empty key on the first pass.
if (key) {
_headers[key] = val;
headers[key] = val;
lastKey = key;
}
else {
_headers[lastKey] += "\n" + val;
headers[lastKey] += "\n" + val;
}
});
return _headers;
})
}
return headers;
}
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: defineLazyGetter is special, it is used to fetch and set an attribute lazily and only once:
https://developer.mozilla.org/en/JavaScript_code_modules/XPCOMUtils.jsm#defineLazyGetter%28%29
I'd imagine it has been used for performances reason. And it doesn't necessary worth its noise nor using mozilla-dark-feature use!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly it also assumes that this represents a scope, which is valid now, but in of my loader prototypes it was not, so I'd prefer not to use it. If we need to be lazy just use a function that caches return value.


// apiUtils.validateOptions doesn't give the ability to easily validate single
// options, so this is a wrapper that provides that ability.
Expand Down
22 changes: 11 additions & 11 deletions packages/addon-kit/lib/selection.js
Expand Up @@ -12,9 +12,14 @@ if (!require("api-utils/xul-app").is("Firefox")) {
].join(""));
}

let { Ci } = require("chrome"),
let { Ci, Cc } = require("chrome"),
{ setTimeout } = require("api-utils/timer"),
{ EventEmitter } = require("api-utils/events");
{ EventEmitter } = require("api-utils/events"),
{ Unknown } = require("api-utils/xpcom");

const windowMediator = Cc["@mozilla.org/appshell/window-mediator;1"].
getService(Ci.nsIWindowMediator);


// The selection type HTML
const HTML = 0x01;
Expand Down Expand Up @@ -66,9 +71,6 @@ function Selection(rangeNumber) {
});
}

require("api-utils/xpcom").utils.defineLazyServiceGetter(this, "windowMediator",
"@mozilla.org/appshell/window-mediator;1", "nsIWindowMediator");

/**
* Returns the most recent content window
*/
Expand Down Expand Up @@ -262,10 +264,8 @@ function onSelect() {
SelectionListenerManager.onSelect();
}

let SelectionListenerManager = {
QueryInterface: require("api-utils/xpcom").utils.
generateQI([Ci.nsISelectionListener]),

let SelectionListenerManager = Unknown.extend({
interfaces: [ 'nsISelectionListener' ],
// The collection of listeners wanting to be notified of selection changes
listeners: EventEmitter.compose({
emit: function emit(type) this._emitOnObject(exports, type),
Expand Down Expand Up @@ -325,7 +325,7 @@ let SelectionListenerManager = {
let self = this;
function wrap(count, func) {
if (count-- > 0)
require("api-utils/timer").setTimeout(wrap, 0);
setTimeout(wrap, 0);
else
self.addSelectionListener(window);
}
Expand Down Expand Up @@ -382,7 +382,7 @@ let SelectionListenerManager = {
browser.removeEventListener("load", onLoad, true);
browser.removeEventListener("unload", onUnload, true);
}
};
});
SelectionListenerManager.listeners.on('error', console.error);

/**
Expand Down
9 changes: 5 additions & 4 deletions packages/addon-kit/lib/widget.js
Expand Up @@ -40,10 +40,11 @@ const { EventEmitter, EventEmitterTrait } = require("api-utils/events");
const { Trait } = require("api-utils/traits");
const LightTrait = require('api-utils/light-traits').Trait;
const { Loader, Symbiont } = require("api-utils/content");
const timer = require("api-utils/timer");
const { Cortex } = require('api-utils/cortex');
const windowsAPI = require("./windows");
const { setTimeout } = require("api-utils/timer");
const unload = require("api-utils/unload");
const { uuid } = require("api-utils/uuid");

// Data types definition
const valid = {
Expand Down Expand Up @@ -692,7 +693,7 @@ WidgetChrome.prototype.update = function WC_update(updatedItem, property, value)
WidgetChrome.prototype._createNode = function WC__createNode() {
// XUL element container for widget
let node = this._doc.createElement("toolbaritem");
let guid = require("api-utils/xpcom").makeUuid().toString();
let guid = String(uuid());

// Temporary work around require("self") failing on unit-test execution ...
let jetpackID = "testID";
Expand Down Expand Up @@ -796,7 +797,7 @@ WidgetChrome.prototype.setContent = function WC_setContent() {
contentScriptWhen: this._widget.contentScriptWhen,
allow: this._widget.allow,
onMessage: function(message) {
timer.setTimeout(function() {
setTimeout(function() {
self._widget._onEvent("message", message);
}, 0);
}
Expand Down Expand Up @@ -826,7 +827,7 @@ WidgetChrome.prototype.addEventHandlers = function WC_addEventHandlers() {
return;

// Proxy event to the widget
timer.setTimeout(function() {
setTimeout(function() {
self._widget._onEvent(EVENTS[e.type], null, self.node);
}, 0);
};
Expand Down