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

wip: added wrapper for the devtools API

parent 84b7f17d
...@@ -85,8 +85,15 @@ ...@@ -85,8 +85,15 @@
}) })
.declareMethod("getVersion", function () { .declareMethod("getVersion", function () {
var gadget = this; var gadget = this;
return gadget.getDeclaredGadget("devtools")
.push(function (subgadget) {
return subgadget.getDevtools();
})
.push(function (devtools) {
console.log(devtools.Browser.getVersion());
return gadget.runBrowserCommand("Browser.getVersion"); return gadget.runBrowserCommand("Browser.getVersion");
}) })
})
.declareMethod("openNewPage", function (url) { .declareMethod("openNewPage", function (url) {
var gadget = this; var gadget = this;
return gadget.runBrowserCommand("Target.createTarget", {url: "" + url}) return gadget.runBrowserCommand("Target.createTarget", {url: "" + url})
......
...@@ -43,6 +43,9 @@ ...@@ -43,6 +43,9 @@
page = result; page = result;
return browser.setPageContent(page, html); return browser.setPageContent(page, html);
}) })
.push(function () {
return browser.getVersion();
})
.push(function () { .push(function () {
return browser.enablePage(page); return browser.enablePage(page);
}) })
......
...@@ -5,6 +5,52 @@ var BROWSER_PASSWORD = "ignored"; ...@@ -5,6 +5,52 @@ var BROWSER_PASSWORD = "ignored";
(function(window, rJS, RSVP) { (function(window, rJS, RSVP) {
"use strict"; "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. * Makes an HTTP GET request to the browser to retrieve some JSON data.
* *
...@@ -36,12 +82,16 @@ var BROWSER_PASSWORD = "ignored"; ...@@ -36,12 +82,16 @@ var BROWSER_PASSWORD = "ignored";
* @param {object} method The method data from the protocol. * @param {object} method The method data from the protocol.
*/ */
function addMethodAPI (object, method) { function addMethodAPI (object, method) {
var callback = (sender, params) => { var callback = (proxy, params) => {
return sender(`${object.domain}.${method.name}`, params);
return;
proxy.clearMessages();
proxy.sendCommand(`${object.domain}.${method.name}`, params);
} }
callback["description"] = method.description; //callback["description"] = method.description;
callback["experimental"] = method.experimental ? true : false; //callback["experimental"] = method.experimental ? true : false;
callback["type"] = "method"; //callback["type"] = "method";
object[method.name] = callback; object[method.name] = callback;
} }
/** /**
...@@ -73,9 +123,9 @@ var BROWSER_PASSWORD = "ignored"; ...@@ -73,9 +123,9 @@ var BROWSER_PASSWORD = "ignored";
function getDevtoolsAPI (domain) { function getDevtoolsAPI (domain) {
var callback = (resolve) => { var callback = (resolve) => {
var object = {}; var object = {};
object["domain"] = domain.domain; //object["domain"] = domain.domain;
object["description"] = domain.description; //object["description"] = domain.description;
object["experimental"] = domain.experimental ? true : false; //object["experimental"] = domain.experimental ? true : false;
(domain.commands || []).forEach((method) => { (domain.commands || []).forEach((method) => {
addMethodAPI(object, method); addMethodAPI(object, method);
}); });
...@@ -100,9 +150,18 @@ var BROWSER_PASSWORD = "ignored"; ...@@ -100,9 +150,18 @@ var BROWSER_PASSWORD = "ignored";
*/ */
function getWrappedDevtoolsAPI (gadget, devtoolsAPI) { function getWrappedDevtoolsAPI (gadget, devtoolsAPI) {
var callback = (resolve) => { var callback = (resolve) => {
// handle connection over websocket // "map(partial(partial, proxy = gadget.proxy), leafs(devtoolsAPI))"
// wrap devtools function with sender for (let [domain, methods] of Object.entries(devtoolsAPI)) {
// special case for Browser/Target domain 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); resolve(devtoolsAPI);
} }
return RSVP.Queue().push(function () { return RSVP.Queue().push(function () {
...@@ -112,10 +171,9 @@ var BROWSER_PASSWORD = "ignored"; ...@@ -112,10 +171,9 @@ var BROWSER_PASSWORD = "ignored";
rJS(window) rJS(window)
.setState({ .setState({
wsBrowser: null, browser: null,
wsTargets: [], targets: [],
targetIndex: null, proxy: null,
messages: {},
devtools: {}, devtools: {},
}) })
.declareService(function () { .declareService(function () {
...@@ -142,6 +200,6 @@ var BROWSER_PASSWORD = "ignored"; ...@@ -142,6 +200,6 @@ var BROWSER_PASSWORD = "ignored";
}) })
.onStateChange(function (ignored) { .onStateChange(function (ignored) {
var gadget = this; var gadget = this;
console.log(gadget.state.devtools); console.log(gadget.state.devtoolsAPI);
}) })
}(window, rJS, RSVP)); }(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