Commit d1dac40e authored by Laurent Wouters's avatar Laurent Wouters
Browse files

[fix] Requesting RDF content on preempted tabs

parent b8fe03ae259d
......@@ -25,11 +25,7 @@
declare module "fallback" {
/**
* Registers some navigation events
* @param onBeforeNavigate Event after tab navigation occurred
* @param onCompleted Event before a tab navigation occur
*/
export function registerNavigations(
onBeforeNavigate: any,
onCompleted: any
): void;
export function registerNavigations(onCompleted: any): void;
}
......@@ -25,13 +25,9 @@
"use strict";
(function() {
"use strict";
function registerNavigations(onBeforeNavigate, onCompleted) {
chrome.webNavigation.onBeforeNavigate.addListener(onBeforeNavigate, {
url: [{ urlMatches: "https?://.*" }], // firefox
//urls: ["<all_urls>"] // crome
});
function registerNavigations(onCompleted) {
chrome.webNavigation.onCompleted.addListener(onCompleted, {
url: [{ urlMatches: "https?://.*" }], // firefox
url: [{ urlMatches: "https?://.*" }] // firefox
//urls: ["<all_urls>"] // crome
});
}
......
......@@ -25,13 +25,12 @@ import {
ObservedResourceRegistry,
resolveObservationsForTab,
hasDetectedData,
DocumentObservations,
detectTopicOnlinks,
detectDataOnLinks,
fetchObservableAt,
ObservableContent,
observeContent,
MIME
getAcceptRdf
} from "../common/data";
import { Message } from "../common/messages";
/// <reference path="./fallback.d.ts"/>
......@@ -111,6 +110,31 @@ function setHeader(
headers.push({ name: name, value: value });
}
/**
* When headers are being sent for a request
* @param details The details
*/
function onSendingHeaders(
details: chrome.webRequest.WebRequestHeadersDetails
): chrome.webRequest.BlockingResponse {
if (
details.tabId == -1 || // invalid tab
details.frameId != 0 || // not the main top-level frame
details.method != "GET" || // not a GET method
details.type != "main_frame" // not the top-level document
)
return {};
let observation = allObservations[details.tabId];
if (observation == null || observation == undefined) return {};
if (observation.preemptable) {
// this tab has been preemted
let headers = details.requestHeaders;
setHeader(headers, "Accept", getAcceptRdf(false));
return { requestHeaders: headers };
}
return {};
}
/**
* When headers are received for a tab
* @param details The details
......@@ -141,6 +165,10 @@ function onHeadersReceived(
}
let contentType = getHeader(details.responseHeaders, "Content-Type");
if (contentType == null || contentType == undefined) {
// no content
return {};
}
index = contentType.indexOf(";");
if (index >= 0) contentType = contentType.substring(0, index);
let observable: ObservableContent = {
......@@ -184,20 +212,6 @@ function onHeadersReceived(
return {};
}
/**
* Event before a tab navigation occur
* @param details The event details
*/
function onBeforeNavigate(
details: chrome.webNavigation.WebNavigationFramedCallbackDetails
): void {
if (details.tabId == -1 || details.frameId != 0) return;
let observation = new DocumentObservations();
observation.url = details.url;
allObservations[details.tabId] = observation;
chrome.pageAction.hide(details.tabId);
}
/**
* Event after tab navigation occurred
* @param details the event details
......@@ -215,8 +229,14 @@ chrome.webRequest.onHeadersReceived.addListener(
{ urls: ["<all_urls>"] },
["blocking", "responseHeaders"]
);
// listen to sent headers
chrome.webRequest.onBeforeSendHeaders.addListener(
onSendingHeaders,
{ urls: ["<all_urls>"] },
["blocking", "requestHeaders"]
);
// listen to navigation
F.registerNavigations(onBeforeNavigate, onCompleted);
F.registerNavigations(onCompleted);
chrome.runtime.onMessage.addListener(
(request: Message<any>, sender, sendResponse) => {
......
......@@ -253,19 +253,6 @@ export interface ObservableContent extends RawContent {
* @param target The target URI
*/
export function fetchObservableAt(target: string): Promise<ObservableContent> {
let accept = Object.keys(MIME)
.map(key => {
return MIME[key];
})
.sort((x: MimeInfo, y: MimeInfo) => {
return x.priority - y.priority;
})
.reduce((acc: string, mime: MimeInfo, index: number) => {
if (acc.length == 0) return mime.mime;
return acc + ", " + mime.mime + ";q=" + (1 - 0.1 * index).toString();
}, "");
accept += ", text/html;q=0.1";
return new Promise<ObservableContent>(
(
resolve: (result: ObservableContent) => void,
......@@ -288,7 +275,7 @@ export function fetchObservableAt(target: string): Promise<ObservableContent> {
}
};
xmlHttp.open("GET", target, true);
xmlHttp.setRequestHeader("Accept", accept);
xmlHttp.setRequestHeader("Accept", getAcceptRdf(true));
xmlHttp.send();
}
);
......@@ -312,6 +299,26 @@ export const MIME: { [mime: string]: MimeInfo } = {
"application/ld+json": new MimeInfo("application/ld+json", "LD", 11, ".json")
};
/**
* Gets the value of the Accept header for requesting RDF
* @param includeHTML Whether to also require HTML (at a low priority)
*/
export function getAcceptRdf(includeHTML: boolean): string {
return (
Object.keys(MIME)
.map(key => {
return MIME[key];
})
.sort((x: MimeInfo, y: MimeInfo) => {
return x.priority - y.priority;
})
.reduce((acc: string, mime: MimeInfo, index: number) => {
if (acc.length == 0) return mime.mime;
return acc + ", " + mime.mime + ";q=" + (1 - 0.1 * index).toString();
}, "") + (includeHTML ? ", text/html;q=0.1" : "")
);
}
/**
* A source for a document
*/
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment