Bug 1167335 - Make manifest obtainer use Task.jsm. r=oyiptong.

---
 dom/ipc/manifestMessages.js                        | 78 ++++++++++------------
 dom/manifest/ManifestObtainer.js                   |  9 ++-
 .../test/browser_ManifestObtainer_obtain.js        | 45 +++++++++++++
 3 files changed, 90 insertions(+), 42 deletions(-)
This commit is contained in:
Marcos Caceres 2015-06-02 15:36:43 -04:00
parent 54216a90c9
commit c58028f897
3 changed files with 90 additions and 42 deletions

View File

@ -19,22 +19,25 @@ const {
const {
ManifestProcessor
} = Cu.import('resource://gre/modules/WebManifest.jsm', {});
const {
Task: {
spawn, async
}
} = Components.utils.import('resource://gre/modules/Task.jsm', {});
addMessageListener('DOM:ManifestObtainer:Obtain', (aMsg) => {
fetchManifest()
.then(
manifest => sendAsyncMessage('DOM:ManifestObtainer:Obtain', {
success: true,
result: manifest,
msgId: aMsg.data.msgId
}),
error => sendAsyncMessage('DOM:ManifestObtainer:Obtain', {
success: false,
result: cloneError(error),
msgId: aMsg.data.msgId
})
);
});
addMessageListener('DOM:ManifestObtainer:Obtain', async(function* (aMsg) {
const response = {
msgId: aMsg.data.msgId,
success: true,
result: undefined
};
try {
response.result = yield fetchManifest();
} catch (err) {
response.result = cloneError(err);
}
sendAsyncMessage('DOM:ManifestObtainer:Obtain', response);
}));
function cloneError(aError) {
const clone = {
@ -49,18 +52,17 @@ function cloneError(aError) {
}
function fetchManifest() {
const manifestQuery = 'link[rel~="manifest"]';
return new Promise((resolve, reject) => {
return spawn(function* () {
if (!content || content.top !== content) {
let msg = 'Content window must be a top-level browsing context.';
return reject(new Error(msg));
throw new Error(msg);
}
const elem = content.document.querySelector(manifestQuery);
const elem = content.document.querySelector('link[rel~="manifest"]');
if (!elem || !elem.getAttribute('href')) {
let msg = 'No manifest to fetch.';
return reject(new Error(msg));
throw new Error(msg);
}
// Will throw on "about:blank" and possibly other invalid URIs.
// Throws on malformed URLs
const manifestURL = new content.URL(elem.href, elem.baseURI);
const reqInit = {
mode: 'cors'
@ -70,34 +72,28 @@ function fetchManifest() {
}
const req = new content.Request(manifestURL, reqInit);
req.setContext('manifest');
content
.fetch(req)
.then(resp => processResponse(resp, content))
.then(resolve)
.catch(reject);
const response = yield content.fetch(req);
const manifest = yield processResponse(response, content);
return manifest;
});
}
function processResponse(aResp, aContentWindow) {
const manifestURL = aResp.url;
return new Promise((resolve, reject) => {
return spawn(function* () {
const badStatus = aResp.status < 200 || aResp.status >= 300;
if (aResp.type === 'error' || badStatus) {
let msg =
`Fetch error: ${aResp.status} - ${aResp.statusText} at ${aResp.url}`;
return reject(new Error(msg));
throw new Error(msg);
}
aResp
.text()
.then((text) => {
const args = {
jsonText: text,
manifestURL: manifestURL,
docURL: aContentWindow.location.href
};
const processor = new ManifestProcessor();
const manifest = processor.process(args);
resolve(Cu.cloneInto(manifest, content));
}, reject);
const text = yield aResp.text();
const args = {
jsonText: text,
manifestURL: aResp.url,
docURL: aContentWindow.location.href
};
const processor = new ManifestProcessor();
const manifest = processor.process(args);
return Cu.cloneInto(manifest, content);
});
}

View File

@ -74,7 +74,14 @@ ManifestObtainer.prototype = {
});
function toError(aErrorClone) {
const error = new Error();
let error;
switch (aErrorClone.name) {
case 'TypeError':
error = new TypeError();
break;
default:
error = new Error();
}
Object.getOwnPropertyNames(aErrorClone)
.forEach(name => error[name] = aErrorClone[name]);
return error;

View File

@ -109,6 +109,51 @@ const tests = [
href='${remoteURL}?${body}&${CORS}'>`;
return link;
}
},{
expected: 'Trying to load from about:whatever is a TypeError.',
get tabURL() {
let query = [
`body=<h1>${this.expected}</h1>`,
'Content-Type=text/html; charset=utf-8',
];
const URL = `${defaultURL}?${query.join('&')}`;
return URL;
},
run(err) {
Assert.strictEqual(err.name, 'TypeError', this.expected);
},
testData: `<link rel="manifest" href='about:whatever'>`
},
{
expected: 'Trying to load from file://whatever is a TypeError.',
get tabURL() {
let query = [
`body=<h1>${this.expected}</h1>`,
'Content-Type=text/html; charset=utf-8',
];
const URL = `${defaultURL}?${query.join('&')}`;
return URL;
},
run(err) {
Assert.strictEqual(err.name, 'TypeError', this.expected);
},
testData: `<link rel="manifest" href='file://manifest'>`
},
//URL parsing tests
{
expected: 'Trying to load invalid URL is a TypeError.',
get tabURL() {
let query = [
`body=<h1>${this.expected}</h1>`,
'Content-Type=text/html; charset=utf-8',
];
const URL = `${defaultURL}?${query.join('&')}`;
return URL;
},
run(err) {
Assert.strictEqual(err.name, 'TypeError', this.expected);
},
testData: `<link rel="manifest" href='http://[12.1212.21.21.12.21.12]'>`
},
];