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

[fix] Use negotiated content to keep tabs with original URL

parent 6be668eec188
......@@ -32,7 +32,7 @@ import {
observeContent,
getAcceptRdf
} from "../common/data";
import { Message } from "../common/messages";
import { Message, activateNegotiated } from "../common/messages";
/// <reference path="./fallback.d.ts"/>
let F = require("./fallback");
import "chrome";
......@@ -133,10 +133,18 @@ function onSendingHeaders(
let observation = allObservations[details.tabId];
if (observation == null || observation == undefined) return {};
if (observation.preemptable) {
// this tab has been preemted
// this tab has been preempted
let headers = details.requestHeaders;
setHeader(headers, "Accept", getAcceptRdf(false));
return { requestHeaders: headers };
} else if (
observation.negotiated != null &&
observation.negotiated != undefined
) {
// this tab has negotiated content
let headers = details.requestHeaders;
setHeader(headers, "Accept", observation.negotiated.contentType);
return { requestHeaders: headers };
}
return {};
}
......@@ -189,26 +197,27 @@ function onHeadersReceived(
observation.primaryTopic = obs.primaryTopic;
observation.sources = obs.sources;
observation.preemptable = obs.preemptable;
observation.negotiated = obs.negotiated;
if (obs.preemptable) {
// modify header
let headers = details.responseHeaders;
setHeader(headers, "Content-Type", "text/plain");
return { responseHeaders: headers };
}
// if there are still nothing, try to probe with HTTP content negotiation
// if there is still nothing, try to probe with HTTP content negotiation
if (!hasDetectedData(observation)) {
// look for data through content negotiation
tryNegotiateData(details.url)
.then((source: DocumentSourceLinked) => {
observation.negotiated = source;
observation.sources.push(source);
onObservedTabUpdated(details.tabId);
})
.catch((reason: string) => {
// do nothing
onObservedTabUpdated(details.tabId);
});
} else {
onObservedTabUpdated(details.tabId);
}
onObservedTabUpdated(details.tabId);
return {};
}
......@@ -317,23 +326,51 @@ chrome.runtime.onMessage.addListener(
chrome.pageAction.onClicked.addListener((tab: chrome.tabs.Tab) => {
let observation = resolveObservationsForTab(allObservations, tab.id);
let url = chrome.extension.getURL(
"ldbrowser/index.html?target=" + encodeURIComponent(observation.url)
);
let callback = (openedTab: chrome.tabs.Tab) => {
allObservations[openedTab.id] = observation;
};
let promise: any = chrome.tabs.create(
{
windowId: tab.windowId,
index: tab.index + 1,
url: url,
active: true,
openerTabId: tab.id
},
callback
);
if (promise != undefined && promise != null) {
promise.then(callback);
if (observation.negotiated != null && observation.negotiated != undefined) {
// a dataset can be obtained through HTTP content negotiation
// => open a new tab with the same observations
let callback = (openedTab: chrome.tabs.Tab) => {
allObservations[openedTab.id] = observation;
chrome.pageAction.setPopup({
tabId: openedTab.id,
popup: chrome.extension.getURL("popup/index.html")
});
activateNegotiated(openedTab.id, observation);
};
let promise: any = chrome.tabs.create(
{
windowId: tab.windowId,
index: tab.index + 1,
url: observation.url,
active: true,
openerTabId: tab.id
},
callback
);
if (promise != undefined && promise != null) {
promise.then(callback);
}
} else {
// dataset is obtained from another source
// => open the linked-data browser as an extension page
let url = chrome.extension.getURL(
"ldbrowser/index.html?target=" + encodeURIComponent(observation.url)
);
let callback = (openedTab: chrome.tabs.Tab) => {
allObservations[openedTab.id] = observation;
};
let promise: any = chrome.tabs.create(
{
windowId: tab.windowId,
index: tab.index + 1,
url: url,
active: true,
openerTabId: tab.id
},
callback
);
if (promise != undefined && promise != null) {
promise.then(callback);
}
}
});
......@@ -530,6 +530,7 @@ export class DocumentObservations {
this.sources = [];
this.primaryTopic = "";
this.preemptable = false;
this.negotiated = null;
}
/**
* The document's url
......@@ -547,6 +548,10 @@ export class DocumentObservations {
* Flag whether the document can be pre-empted
*/
preemptable: boolean;
/**
* Dataset that can be obtained at the document's location through HTTP content negotiation
*/
negotiated: DocumentSourceLinked;
}
/**
......@@ -577,7 +582,8 @@ export function observeContent(
primaryTopic: primary == null ? "" : primary,
url: observable.url,
sources: [source],
preemptable: MIME.hasOwnProperty(observable.contentType)
preemptable: true,
negotiated: null
};
return observations;
} else {
......@@ -601,11 +607,15 @@ export function observeContent(
primary = detectTopicOnlinks(links);
}
let sources = detectDataOnLinks(links);
let negotiated = sources.find(
(source: DocumentSourceLinked) => source.url == observable.url
);
let observations: DocumentObservations = {
primaryTopic: primary == null || primary == undefined ? "" : primary,
url: observable.url,
sources: sources,
preemptable: MIME.hasOwnProperty(observable.contentType)
preemptable: MIME.hasOwnProperty(observable.contentType),
negotiated: negotiated != undefined ? negotiated : null
};
return observations;
}
......
......@@ -266,3 +266,18 @@ export function updateCurrentCommand(
payload: command
});
}
/**
* Activates a tab with negotiated content
* @param id The identifier of a tab
* @param observations The observations for the document
*/
export function activateNegotiated(
tabId: number,
observations: DocumentObservations
): void {
chrome.tabs.sendMessage(tabId, {
requestType: "ActivateNegotiated",
payload: observations
});
}
......@@ -45,7 +45,7 @@ function sendHead(observation: DocumentObservations): void {
/**
* Initializes the LD browser
*/
function initializeBrowser() {
function initializeBrowser(): LDBrowser {
// The application view to use
let viewer: Viewer = newViewer();
// The LD browser instance
......@@ -64,7 +64,19 @@ function initializeBrowser() {
browser.setCommand(request.payload);
}
});
browser.onReachedUri(window.location.href);
return browser;
}
function onActivatedByNegotiated(
request: Message<any>,
sender: any,
sendResponse: (response: any) => void
) {
if (request.requestType == "ActivateNegotiated") {
chrome.runtime.onMessage.removeListener(onActivatedByNegotiated);
let browser = initializeBrowser();
browser.onReachedUri(window.location.href);
}
}
/**
......@@ -75,7 +87,13 @@ function main() {
getObservationsFor(null).then((observation: DocumentObservations) => {
sendHead(observation);
if (observation.preemptable) {
initializeBrowser();
let browser = initializeBrowser();
browser.onReachedUri(window.location.href);
} else if (
observation.negotiated != null &&
observation.negotiated != undefined
) {
chrome.runtime.onMessage.addListener(onActivatedByNegotiated);
}
});
}
......
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