Source code
Revision control
Copy as Markdown
Other Tools
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
// Must match the storage read by AIProtocolHandler
// (netwerk/protocol/ai/AIProtocolHandler.cpp).
const CACHE_NAME = "ai";
// The ai: origin of `host`, which must be the exact host of that origin.
function originFor(host) {
if (origin.host != host) {
throw new Error(`Invalid ai: host: ${host}`);
}
return origin;
}
// The URL of `path` in `origin`, which `path` cannot resolve outside of.
function resolveIn(origin, path) {
const url = Services.io.newURI(path, null, origin);
if (url.prePath != origin.prePath) {
throw new Error(`Path outside of ${origin.prePath}: ${path}`);
}
return url.spec;
}
function principalFor(origin) {
return Services.scriptSecurityManager.createContentPrincipal(origin, {});
}
/**
* Privileged API to store the content served by ai: URLs. Every host is a
* distinct origin, and its content lives in the chrome namespace of the Cache
* API of that origin's principal, which the origin's own documents cannot
* access.
*/
export const AIStorage = {
async put(host, path, body, contentType) {
const origin = originFor(host);
const url = resolveIn(origin, path);
const cache = await new CacheStorage("chrome", principalFor(origin)).open(
CACHE_NAME
);
await cache.put(
url,
new Response(body, { headers: { "Content-Type": contentType } })
);
},
// origin attributes are cleared, because that is where put() stores the
// content. Clearing any other storage of the origin would first need a
// decision on which origin attributes to cover.
async clear(host) {
const principal = principalFor(originFor(host));
return new Promise((resolve, reject) => {
Services.clearData.deleteDataFromPrincipal(
principal,
false,
Ci.nsIClearDataService.CLEAR_DOM_QUOTA,
failedFlags => {
if (failedFlags) {
} else {
resolve();
}
}
);
});
},
};