2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 WebInspector.injectedExtensionAPI = function(InjectedScriptHost, inspectedWindow, injectedScriptId)
34 // Here and below, all constructors are private to API implementation.
35 // For a public type Foo, if internal fields are present, these are on
36 // a private FooImpl type, an instance of FooImpl is used in a closure
37 // by Foo consutrctor to re-bind publicly exported members to an instance
40 function EventSinkImpl(type, customDispatch)
44 this._customDispatch = customDispatch;
47 EventSinkImpl.prototype = {
48 addListener: function(callback)
50 if (typeof callback != "function")
51 throw new "addListener: callback is not a function";
52 if (this._listeners.length === 0)
53 extensionServer.sendRequest({ command: "subscribe", type: this._type });
54 this._listeners.push(callback);
55 extensionServer.registerHandler("notify-" + this._type, bind(this._dispatch, this));
58 removeListener: function(callback)
60 var listeners = this._listeners;
62 for (var i = 0; i < listeners.length; ++i) {
63 if (listeners[i] === callback) {
64 listeners.splice(i, 1);
68 if (this._listeners.length === 0)
69 extensionServer.sendRequest({ command: "unsubscribe", type: this._type });
74 var listeners = this._listeners.slice();
75 for (var i = 0; i < listeners.length; ++i)
76 listeners[i].apply(null, arguments);
79 _dispatch: function(request)
81 if (this._customDispatch)
82 this._customDispatch.call(this, request);
84 this._fire.apply(this, request.arguments);
88 function InspectorExtensionAPI()
90 this.audits = new Audits();
91 this.inspectedWindow = new InspectedWindow();
92 this.panels = new Panels();
93 this.resources = new Resources();
95 this.onReset = new EventSink("reset");
98 InspectorExtensionAPI.prototype = {
99 log: function(message)
101 extensionServer.sendRequest({ command: "log", message: message });
107 function resourceDispatch(request)
109 var resource = request.arguments[1];
110 resource.__proto__ = new Resource(request.arguments[0]);
111 this._fire(resource);
113 this.onFinished = new EventSink("resource-finished", resourceDispatch);
116 Resources.prototype = {
117 getHAR: function(callback)
119 function callbackWrapper(result)
121 var entries = (result && result.entries) || [];
122 for (var i = 0; i < entries.length; ++i) {
123 entries[i].__proto__ = new Resource(entries[i]._resourceId);
124 delete entries[i]._resourceId;
128 return extensionServer.sendRequest({ command: "getHAR" }, callback && callbackWrapper);
132 function ResourceImpl(id)
137 ResourceImpl.prototype = {
138 getContent: function(callback)
140 function callbackWrapper(response)
142 callback(response.content, response.encoding);
144 extensionServer.sendRequest({ command: "getResourceContent", id: this._id }, callback && callbackWrapper);
151 elements: new ElementsPanel()
154 function panelGetter(name)
158 for (var panel in panels)
159 this.__defineGetter__(panel, bind(panelGetter, null, panel));
163 create: function(title, iconURL, pageURL, callback)
165 var id = "extension-panel-" + extensionServer.nextObjectId();
167 command: "createPanel",
170 icon: expandURL(iconURL),
171 url: expandURL(pageURL)
173 extensionServer.sendRequest(request, callback && bind(callback, this, new ExtensionPanel(id)));
177 function PanelImpl(id)
182 function PanelWithSidebarImpl(id)
184 PanelImpl.call(this, id);
187 PanelWithSidebarImpl.prototype = {
188 createSidebarPane: function(title, url, callback)
190 var id = "extension-sidebar-" + extensionServer.nextObjectId();
192 command: "createSidebarPane",
198 function callbackWrapper()
200 callback(new ExtensionSidebarPane(id));
202 extensionServer.sendRequest(request, callback && callbackWrapper);
205 createWatchExpressionSidebarPane: function(title, callback)
207 var id = "watch-sidebar-" + extensionServer.nextObjectId();
209 command: "createWatchExpressionSidebarPane",
214 function callbackWrapper()
216 callback(new WatchExpressionSidebarPane(id));
218 extensionServer.sendRequest(request, callback && callbackWrapper);
222 PanelWithSidebarImpl.prototype.__proto__ = PanelImpl.prototype;
224 function ElementsPanel()
227 PanelWithSidebar.call(this, id);
228 this.onSelectionChanged = new EventSink("panel-objectSelected-" + id);
231 function ExtensionPanel(id)
233 Panel.call(this, id);
234 this.onSearch = new EventSink("panel-search-" + id);
237 function ExtensionSidebarPaneImpl(id)
242 ExtensionSidebarPaneImpl.prototype = {
243 setHeight: function(height)
245 extensionServer.sendRequest({ command: "setSidebarHeight", id: this._id, height: height });
249 function WatchExpressionSidebarPaneImpl(id)
251 ExtensionSidebarPaneImpl.call(this, id);
252 this.onUpdated = new EventSink("watch-sidebar-updated-" + id);
255 WatchExpressionSidebarPaneImpl.prototype = {
256 setExpression: function(expression, rootTitle)
258 extensionServer.sendRequest({ command: "setWatchSidebarContent", id: this._id, expression: expression, rootTitle: rootTitle, evaluateOnPage: true });
261 setObject: function(jsonObject, rootTitle)
263 extensionServer.sendRequest({ command: "setWatchSidebarContent", id: this._id, expression: jsonObject, rootTitle: rootTitle });
267 WatchExpressionSidebarPaneImpl.prototype.__proto__ = ExtensionSidebarPaneImpl.prototype;
269 function WatchExpressionSidebarPane(id)
271 var impl = new WatchExpressionSidebarPaneImpl(id);
272 ExtensionSidebarPane.call(this, id, impl);
280 addCategory: function(displayName, resultCount)
282 var id = "extension-audit-category-" + extensionServer.nextObjectId();
283 extensionServer.sendRequest({ command: "addAuditCategory", id: id, displayName: displayName, resultCount: resultCount });
284 return new AuditCategory(id);
288 function AuditCategoryImpl(id)
290 function auditResultDispatch(request)
292 var auditResult = new AuditResult(request.arguments[0]);
294 this._fire(auditResult);
296 console.error("Uncaught exception in extension audit event handler: " + e);
301 this.onAuditStarted = new EventSink("audit-started-" + id, auditResultDispatch);
304 function AuditResultImpl(id)
308 var formatterTypes = [
313 for (var i = 0; i < formatterTypes.length; ++i)
314 this[formatterTypes[i]] = bind(this._nodeFactory, null, formatterTypes[i]);
317 AuditResultImpl.prototype = {
318 addResult: function(displayName, description, severity, details)
320 // shorthand for specifying details directly in addResult().
321 if (details && !(details instanceof AuditResultNode))
322 details = details instanceof Array ? this.createNode.apply(this, details) : this.createNode(details);
325 command: "addAuditResult",
327 displayName: displayName,
328 description: description,
332 extensionServer.sendRequest(request);
335 createResult: function()
337 var node = new AuditResultNode();
338 node.contents = Array.prototype.slice.call(arguments);
344 extensionServer.sendRequest({ command: "stopAuditCategoryRun", resultId: this._id });
349 return apiPrivate.audits.Severity;
352 _nodeFactory: function(type)
356 arguments: Array.prototype.slice.call(arguments, 1)
361 function AuditResultNode(contents)
363 this.contents = contents;
365 this.expanded = false;
368 AuditResultNode.prototype = {
371 var node = AuditResultImpl.prototype.createResult.apply(null, arguments);
372 this.children.push(node);
377 function InspectedWindow()
379 this.onDOMContentLoaded = new EventSink("inspectedPageDOMContentLoaded");
380 this.onLoaded = new EventSink("inspectedPageLoaded");
381 this.onNavigated = new EventSink("inspectedURLChanged");
384 InspectedWindow.prototype = {
387 return extensionServer.sendRequest({ command: "reload" });
390 eval: function(expression, callback)
392 function callbackWrapper(result)
394 var value = result.value;
395 if (!result.isException)
396 value = value === "undefined" ? undefined : JSON.parse(value);
397 callback(value, result.isException);
399 return extensionServer.sendRequest({ command: "evaluateOnInspectedPage", expression: expression }, callback && callbackWrapper);
403 function ExtensionServerClient()
405 this._callbacks = {};
407 this._lastRequestId = 0;
408 this._lastObjectId = 0;
410 this.registerHandler("callback", bind(this._onCallback, this));
412 var channel = new MessageChannel();
413 this._port = channel.port1;
414 this._port.addEventListener("message", bind(this._onMessage, this), false);
417 top.postMessage("registerExtension", [ channel.port2 ], "*");
420 ExtensionServerClient.prototype = {
421 sendRequest: function(message, callback)
423 if (typeof callback === "function")
424 message.requestId = this._registerCallback(callback);
425 return this._port.postMessage(message);
428 registerHandler: function(command, handler)
430 this._handlers[command] = handler;
433 nextObjectId: function()
435 return injectedScriptId + "_" + ++this._lastObjectId;
438 _registerCallback: function(callback)
440 var id = ++this._lastRequestId;
441 this._callbacks[id] = callback;
445 _onCallback: function(request)
447 if (request.requestId in this._callbacks) {
448 this._callbacks[request.requestId](request.result);
449 delete this._callbacks[request.requestId];
453 _onMessage: function(event)
455 var request = event.data;
456 var handler = this._handlers[request.command];
458 handler.call(this, request);
462 function expandURL(url)
466 if (/^[^/]+:/.exec(url)) // See if url has schema.
468 var baseURL = location.protocol + "//" + location.hostname + location.port;
470 return baseURL + url;
471 return baseURL + location.pathname.replace(/\/[^/]*$/,"/") + url;
474 function bind(func, thisObject)
476 var args = Array.prototype.slice.call(arguments, 2);
477 return function() { return func.apply(thisObject, args.concat(Array.prototype.slice.call(arguments, 0))); };
480 function populateInterfaceClass(interface, implementation)
482 for (var member in implementation) {
483 if (member.charAt(0) === "_")
485 var value = implementation[member];
486 interface[member] = typeof value === "function" ? bind(value, implementation)
487 : interface[member] = implementation[member];
491 function declareInterfaceClass(implConstructor)
495 var impl = { __proto__: implConstructor.prototype };
496 implConstructor.apply(impl, arguments);
497 populateInterfaceClass(this, impl);
501 var AuditCategory = declareInterfaceClass(AuditCategoryImpl);
502 var AuditResult = declareInterfaceClass(AuditResultImpl);
503 var EventSink = declareInterfaceClass(EventSinkImpl);
504 var ExtensionSidebarPane = declareInterfaceClass(ExtensionSidebarPaneImpl);
505 var Panel = declareInterfaceClass(PanelImpl);
506 var PanelWithSidebar = declareInterfaceClass(PanelWithSidebarImpl);
507 var Resource = declareInterfaceClass(ResourceImpl);
508 var WatchExpressionSidebarPane = declareInterfaceClass(WatchExpressionSidebarPaneImpl);
510 var extensionServer = new ExtensionServerClient();
512 webInspector = new InspectorExtensionAPI();