Commit 67150aa0 authored by lukas.niegsch's avatar lukas.niegsch

wip: added wrapper for the devtools API

parent 84b7f17d
......@@ -85,7 +85,14 @@
})
.declareMethod("getVersion", function () {
var gadget = this;
return gadget.runBrowserCommand("Browser.getVersion");
return gadget.getDeclaredGadget("devtools")
.push(function (subgadget) {
return subgadget.getDevtools();
})
.push(function (devtools) {
console.log(devtools.Browser.getVersion());
return gadget.runBrowserCommand("Browser.getVersion");
})
})
.declareMethod("openNewPage", function (url) {
var gadget = this;
......
......@@ -43,6 +43,9 @@
page = result;
return browser.setPageContent(page, html);
})
.push(function () {
return browser.getVersion();
})
.push(function () {
return browser.enablePage(page);
})
......
......@@ -5,6 +5,52 @@ var BROWSER_PASSWORD = "ignored";
(function(window, rJS, RSVP) {
"use strict";
/**
* The proxy to a specific browser target. Each proxy makes a connection
* to the targets websocket and handles the outgoing and incoming messages.
* It also automatically reconnects on network errors.
*
* TODO: maybe make this work with async or RSVP?
*/
class TargetProxy
{
constructor (path, targetId) {
this.targetId = this.targetId;
this.path = path;
this.websocket = null;
this.messages = [];
}
connect (callback) {
this.disconnect();
this.websocket = new WebSocket(this.path);
this.websocket.onopen = callback;
this.websocket.onmessage = this.messages.push;
this.websocket.onerror = (error) => {
console.error(error);
this.connect(callback);
};
}
disconnect () {
if (this.websocket) {
this.websocket.close();
this.websocket = null;
};
}
clearMessages () {
this.messages = [];
}
sendCommand (command, params) {
this.clearMessages();
var message = {id: 0, method: command, params: params};
this.websocket.send(JSON.stringify(message));
}
waitMessage (response) {
// TODO: implement this function
}
debug () {
console.log(this.messages);
}
};
/**
* Makes an HTTP GET request to the browser to retrieve some JSON data.
*
......@@ -36,12 +82,16 @@ var BROWSER_PASSWORD = "ignored";
* @param {object} method The method data from the protocol.
*/
function addMethodAPI (object, method) {
var callback = (sender, params) => {
return sender(`${object.domain}.${method.name}`, params);
var callback = (proxy, params) => {
return;
proxy.clearMessages();
proxy.sendCommand(`${object.domain}.${method.name}`, params);
}
callback["description"] = method.description;
callback["experimental"] = method.experimental ? true : false;
callback["type"] = "method";
//callback["description"] = method.description;
//callback["experimental"] = method.experimental ? true : false;
//callback["type"] = "method";
object[method.name] = callback;
}
/**
......@@ -73,9 +123,9 @@ var BROWSER_PASSWORD = "ignored";
function getDevtoolsAPI (domain) {
var callback = (resolve) => {
var object = {};
object["domain"] = domain.domain;
object["description"] = domain.description;
object["experimental"] = domain.experimental ? true : false;
//object["domain"] = domain.domain;
//object["description"] = domain.description;
//object["experimental"] = domain.experimental ? true : false;
(domain.commands || []).forEach((method) => {
addMethodAPI(object, method);
});
......@@ -100,9 +150,18 @@ var BROWSER_PASSWORD = "ignored";
*/
function getWrappedDevtoolsAPI (gadget, devtoolsAPI) {
var callback = (resolve) => {
// handle connection over websocket
// wrap devtools function with sender
// special case for Browser/Target domain
// "map(partial(partial, proxy = gadget.proxy), leafs(devtoolsAPI))"
for (let [domain, methods] of Object.entries(devtoolsAPI)) {
for (let [method, callback] of Object.entries(methods)) {
devtoolsAPI[domain][method] = (params) => {
callback(gadget.proxy, params);
};
}
}
// TODO: special case for targets to make the abstraction work:
// TODO: attachTarget/detachFromTarget -> add/remove from target list
// TODO: activateTarget -> change current proxy target
console.log(devtoolsAPI);
resolve(devtoolsAPI);
}
return RSVP.Queue().push(function () {
......@@ -112,10 +171,9 @@ var BROWSER_PASSWORD = "ignored";
rJS(window)
.setState({
wsBrowser: null,
wsTargets: [],
targetIndex: null,
messages: {},
browser: null,
targets: [],
proxy: null,
devtools: {},
})
.declareService(function () {
......@@ -142,6 +200,6 @@ var BROWSER_PASSWORD = "ignored";
})
.onStateChange(function (ignored) {
var gadget = this;
console.log(gadget.state.devtools);
console.log(gadget.state.devtoolsAPI);
})
}(window, rJS, RSVP));
\ No newline at end of file
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