Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
initial set of changes in for extensions
  • Loading branch information
pmuellr committed May 6, 2011
1 parent 05e71f5 commit 45fcc79e9f00fef492f4c48e1cf5bacce0dc0ae9
Showing 15 changed files with 271 additions and 9 deletions.
@@ -18,6 +18,8 @@ module weinre {
void connectTarget(in string clientId, in string targetId);
void disconnectTarget(in string clientId);

void getExtensions(out string[] extensions)

void logDebug( in string message );
void logInfo( in string message );
void logWarning( in string message );
@@ -0,0 +1,91 @@
/*
* weinre is available under *either* the terms of the modified BSD license *or* the
* MIT License (2008). See http://opensource.org/licenses/alphabetical for full text.
*
* Copyright (c) 2011 IBM Corporation
*/

package weinre.server;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.jetty.util.resource.Resource;

//-------------------------------------------------------------------
public class ExtensionManager {
static private File weinreHomeDir = null;
static private File weinreExtDir = null;
static private long weinreExtDate = 0L;
static private String[] extensions = null;

static private String[] EMPTY_STRING_ARRAY = {};

//---------------------------------------------------------------
static {
initExtensions();
}

//---------------------------------------------------------------
static public String[] getExtensions() {
if (weinreExtDate != weinreExtDir.lastModified()) {
initExtensions();
}

return extensions;
}

//---------------------------------------------------------------
// path: /client/extensions/weinre-ext-sample/extension.html
static public Resource getResource(String path) throws MalformedURLException {
File resourceFile = new File(weinreExtDir, path.substring(18));
if (!resourceFile.exists()) return null;

try {
return Resource.newResource(resourceFile.toURI().toURL().toExternalForm(), false);
}
catch (IOException e) {
throw new MalformedURLException();
}
}

//---------------------------------------------------------------
static private void initExtensions() {

weinreHomeDir = new File(System.getProperty("user.home"), ".weinre");
if (!weinreHomeDir.isDirectory()) {
Main.info("extensions not enabled: ~/.weinre is not a directory");
return;
}

weinreExtDir = new File(weinreHomeDir, "extensions");
weinreExtDate = weinreExtDir.lastModified();
if (!weinreExtDir.isDirectory()) {
Main.info("extensions not enabled: ~/.weinre/extensions is not a directory");
return;
}

extensions = EMPTY_STRING_ARRAY;

List<String> extensionList = new ArrayList<String>();

String[] entries = weinreExtDir.list();
for (String entry: entries) {
if (entry.startsWith(".")) continue;

File extDir = new File(weinreExtDir, entry);
if (!extDir.isDirectory()) continue;

File extensionHtml = new File(extDir, "extension.html");
if (!extensionHtml.isFile()) continue;

extensionList.add(entry);
}

extensions = extensionList.toArray(EMPTY_STRING_ARRAY);
}

}
@@ -14,6 +14,8 @@
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.util.resource.Resource;

import weinre.server.ExtensionManager;

//-------------------------------------------------------------------
public class ClassPathResourceHandler extends ResourceHandler {

@@ -32,6 +34,11 @@ public Resource getResource(String path) throws MalformedURLException {
throw new MalformedURLException(path);
}

// handle extensions
if (path.startsWith("/client/extensions/")) {
return ExtensionManager.getResource(path);
}

path = pathPrefix + path;
URL url = getClass().getClassLoader().getResource(path);
if (url != null)
@@ -17,6 +17,7 @@
import weinre.server.Channel;
import weinre.server.Client;
import weinre.server.ConnectionManager;
import weinre.server.ExtensionManager;
import weinre.server.Main;
import weinre.server.Target;

@@ -57,6 +58,26 @@ public void getClients(Channel channel, String callbackId) throws IOException {
channel.sendCallback("WeinreClientEvents", callbackId, clientResults);
}

//---------------------------------------------------------------
public void getExtensions(Channel channel, String callbackId) throws IOException {
String[] extensions = ExtensionManager.getExtensions();
JSONArray result = new JSONArray();

try {
for (String extension: extensions) {
JSONObject extensionObject = new JSONObject();
extensionObject.put("startPage", "extensions/" + extension + "/extension.html");

result.add(extensionObject);
}
}
catch(JSONException e) {
throw new RuntimeException(e);
}

channel.sendCallback("WeinreClientEvents", callbackId, result);
}

//---------------------------------------------------------------
public void connectTarget(Channel channel, String clientId, String targetId, String callbackId) {
Client client = ConnectionManager.$.getClient(clientId);
@@ -10,7 +10,6 @@
import java.io.IOException;
import java.util.List;

import org.apache.wink.json4j.JSONException;
import org.apache.wink.json4j.JSONObject;

import weinre.server.Channel;
@@ -0,0 +1,11 @@
/*
* weinre is available under *either* the terms of the modified BSD license *or* the
* MIT License (2008). See http://opensource.org/licenses/alphabetical for full text.
*
* Copyright (c) 2011 IBM Corporation
*/

(function() {
var ExtensionRegistryImpl = require("weinre/client/ExtensionRegistryImpl").getClass()
window.InspectorExtensionRegistry = new ExtensionRegistryImpl()
})()
@@ -0,0 +1,8 @@
/*
* weinre is available under *either* the terms of the modified BSD license *or* the
* MIT License (2008). See http://opensource.org/licenses/alphabetical for full text.
*
* Copyright (c) 2011 IBM Corporation
*/

eval(window.top.installWebInspectorAPIsource())
@@ -13,6 +13,7 @@ requireClass ../common/Callback
requireClass ../common/Weinre
requireClass ../common/MessageDispatcher
requireClass ../common/Binding
requireClass ../common/IDGenerator

requireClass ./InspectorBackendImpl
requireClass ./InspectorFrontendHostImpl
@@ -80,6 +81,7 @@ method _installRemotePanel

var toolbar = document.getElementById("toolbar")
WebInspector.addPanelToolbarIcon(toolbar, panel, toolbar.childNodes[1])
WebInspector.panelOrder.unshift(WebInspector.panelOrder.pop())

WebInspector.currentPanel = panel

@@ -128,7 +130,28 @@ method cb_webSocketClosed
WebInspector.currentPanel = WebInspector.panels.remote
}, 1000)

//-----------------------------------------------------------------------------
function installWebInspectorAPIsource

if ("webInspector" in window) return

var extensionAPI = window.parent.InspectorFrontendHost.getExtensionAPI()
extensionAPI = extensionAPI.replace("location.hostname + location.port", "location.hostname + ':' + location.port")

// parms are: InjectedScriptHost, inspectedWindow, injectedScriptId
// InjectedScriptHost is not directly referenced
// inspectedWindow is not directly referenced
// injectedScriptId is used to scope object references, appears that
// each extension should have a unique value for this

var id = IDGenerator.next()
console.log("installing webInspector with injectedScriptId: " + id)
extensionAPI += "(null,null," + id + ")"
return extensionAPI

//-----------------------------------------------------------------------------
static method main
Weinre.client = new Client()
Weinre.client.initialize()

window.installWebInspectorAPIsource = installWebInspectorAPIsource
@@ -43,6 +43,10 @@ method add(connector)
this.ulConnectors.insertBefore(li, insertionPoint)
}

//-----------------------------------------------------------------------------
method get(channel)
return this.connectors[channel]

//-----------------------------------------------------------------------------
method getNewestConnectorChannel(ignoring)
var newest = 0
@@ -0,0 +1,35 @@

/*
* weinre is available under *either* the terms of the modified BSD license *or* the
* MIT License (2008). See http://opensource.org/licenses/alphabetical for full text.
*
* Copyright (c) 2011 IBM Corporation
*/

requireClass ../common/Ex
requireClass ../common/Binding
requireClass ../common/Weinre

//-----------------------------------------------------------------------------
class ExtensionRegistryImpl

//-----------------------------------------------------------------------------
init
var extensions = []

//-----------------------------------------------------------------------------
method getExtensionsAsync
if (extensions.length) return

Weinre.WeinreClientCommands.getExtensions(Binding(this, this._cb_getExtensions))

//-----------------------------------------------------------------------------
method _cb_getExtensions(extensionsResult)
extensions = extensionsResult
this._installExtensions()

//-----------------------------------------------------------------------------
method _installExtensions
WebInspector.addExtensions(extensions)


@@ -14,6 +14,10 @@ class InspectorFrontendHostImpl

this._getPlatformAndPort()

//-----------------------------------------------------------------------------
init
var _extensionAPI

//-----------------------------------------------------------------------------
method loaded

@@ -43,6 +47,17 @@ method sendMessageToBackend(message)
}
Weinre.logInfo(arguments.callee.signature + "(" + JSON.stringify(object,null,4) + ")")

//-----------------------------------------------------------------------------
method setExtensionAPI(extensionAPI)
_extensionAPI = extensionAPI

//-----------------------------------------------------------------------------
method getExtensionAPI
return _extensionAPI

//-----------------------------------------------------------------------------
method inspectedURLChanged

//-----------------------------------------------------------------------------
method _getPlatformAndPort
this._platform = "weinre"
@@ -64,6 +64,9 @@ method addClient(client)
method addTarget(target)
this.targetList.add(target)

method getTarget(channel)
return this.targetList.get(channel)

method removeClient(channel)
this.clientList.remove(channel)

@@ -13,6 +13,13 @@ requireClass ../common/Weinre
class WeinreClientEventsImpl(client)
this.client = client

//-----------------------------------------------------------------------------
init
var titleNotConnected = "weinre: target not connected"
var titleConnectedPrefix = "weinre: "

document.title = titleNotConnected

//-----------------------------------------------------------------------------
method clientRegistered(clientDescription)
if (this.client.uiAvailable()) {
@@ -54,15 +61,23 @@ method connectionCreated(/*int*/ clientChannel, /*int*/ targetChannel)
WebInspector.panels.elements.reset()
WebInspector.panels.timeline._clearPanel()
WebInspector.panels.resources.reset()

var target = WebInspector.panels.remote.getTarget(targetChannel)
document.title = titleConnectedPrefix + target.url

WebInspector.inspectedURLChanged(target.url)

//-----------------------------------------------------------------------------
method connectionDestroyed(/*int*/ clientChannel, /*int*/ targetChannel)

if (this.client.uiAvailable()) {
WebInspector.panels.remote.setClientState(clientChannel, "not-connected")
WebInspector.panels.remote.setTargetState(targetChannel, "not-connected")
}

if (clientChannel != Weinre.messageDispatcher.channel) return

document.title = titleNotConnected

if (!Weinre.client.autoConnect()) return
if (!this.client.uiAvailable()) return
@@ -11,8 +11,8 @@ class IDGenerator

//-----------------------------------------------------------------------------
init
var nextId = 1
var idName = "__weinre__id"
var nextIdValue = 1
var idName = "__weinre__id"

//-----------------------------------------------------------------------------
static method checkId(object)
@@ -23,7 +23,7 @@ static method getId(object, map)
var id = IDGenerator.checkId(object)

if (!id) {
id = next()
id = nextId()

// note:
// attempted to use Object.defineProperty() to make
@@ -40,10 +40,14 @@ static method getId(object, map)
}

return id


//-----------------------------------------------------------------------------
static method next
return nextId()

//-----------------------------------------------------------------------------
function next
var result = nextId
nextId += 1
function nextId
var result = nextIdValue
nextIdValue += 1
return result

0 comments on commit 45fcc79

Please sign in to comment.