2 * Copyright (C) 2011 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.ExtensionServer = function()
33 this._clientObjects = {};
35 this._subscribers = {};
36 this._subscriptionStartHandlers = {};
37 this._subscriptionStopHandlers = {};
38 this._extraHeaders = {};
40 this._lastRequestId = 0;
41 this._allowedOrigins = {};
42 this._status = new WebInspector.ExtensionStatus();
44 this._registerHandler("addAuditCategory", this._onAddAuditCategory.bind(this));
45 this._registerHandler("addAuditResult", this._onAddAuditResult.bind(this));
46 this._registerHandler("addConsoleMessage", this._onAddConsoleMessage.bind(this));
47 this._registerHandler("addRequestHeaders", this._onAddRequestHeaders.bind(this));
48 this._registerHandler("createPanel", this._onCreatePanel.bind(this));
49 this._registerHandler("createSidebarPane", this._onCreateSidebarPane.bind(this));
50 this._registerHandler("evaluateOnInspectedPage", this._onEvaluateOnInspectedPage.bind(this));
51 this._registerHandler("getHAR", this._onGetHAR.bind(this));
52 this._registerHandler("getConsoleMessages", this._onGetConsoleMessages.bind(this));
53 this._registerHandler("getPageResources", this._onGetPageResources.bind(this));
54 this._registerHandler("getRequestContent", this._onGetRequestContent.bind(this));
55 this._registerHandler("getResourceContent", this._onGetResourceContent.bind(this));
56 this._registerHandler("log", this._onLog.bind(this));
57 this._registerHandler("reload", this._onReload.bind(this));
58 this._registerHandler("setResourceContent", this._onSetResourceContent.bind(this));
59 this._registerHandler("setSidebarHeight", this._onSetSidebarHeight.bind(this));
60 this._registerHandler("setSidebarContent", this._onSetSidebarContent.bind(this));
61 this._registerHandler("setSidebarPage", this._onSetSidebarPage.bind(this));
62 this._registerHandler("stopAuditCategoryRun", this._onStopAuditCategoryRun.bind(this));
63 this._registerHandler("subscribe", this._onSubscribe.bind(this));
64 this._registerHandler("unsubscribe", this._onUnsubscribe.bind(this));
66 window.addEventListener("message", this._onWindowMessage.bind(this), false);
69 WebInspector.ExtensionServer.prototype = {
70 notifyObjectSelected: function(panelId, objectId)
72 this._postNotification("panel-objectSelected-" + panelId, objectId);
75 notifySearchAction: function(panelId, action, searchString)
77 this._postNotification("panel-search-" + panelId, action, searchString);
80 notifyPanelShown: function(panelId)
82 this._postNotification("panel-shown-" + panelId);
85 notifyPanelHidden: function(panelId)
87 this._postNotification("panel-hidden-" + panelId);
90 _inspectedURLChanged: function(event)
94 this._postNotification("inspectedURLChanged", url);
97 notifyInspectorReset: function()
99 this._postNotification("reset");
102 notifyExtensionSidebarUpdated: function(id)
104 this._postNotification("sidebar-updated-" + id);
107 startAuditRun: function(category, auditRun)
109 this._clientObjects[auditRun.id] = auditRun;
110 this._postNotification("audit-started-" + category.id, auditRun.id);
113 stopAuditRun: function(auditRun)
115 delete this._clientObjects[auditRun.id];
118 notifyResourceContentCommitted: function(resource, content)
120 this._postNotification("resource-content-committed", this._makeResource(resource), content);
123 _postNotification: function(type, details)
125 var subscribers = this._subscribers[type];
129 command: "notify-" + type,
130 arguments: Array.prototype.slice.call(arguments, 1)
132 for (var i = 0; i < subscribers.length; ++i)
133 subscribers[i].postMessage(message);
136 _onSubscribe: function(message, port)
138 var subscribers = this._subscribers[message.type];
140 subscribers.push(port);
142 this._subscribers[message.type] = [ port ];
143 if (this._subscriptionStartHandlers[message.type])
144 this._subscriptionStartHandlers[message.type]();
148 _onUnsubscribe: function(message, port)
150 var subscribers = this._subscribers[message.type];
153 subscribers.remove(port);
154 if (!subscribers.length) {
155 delete this._subscribers[message.type];
156 if (this._subscriptionStopHandlers[message.type])
157 this._subscriptionStopHandlers[message.type]();
161 _onAddRequestHeaders: function(message)
163 var id = message.extensionId;
164 if (typeof id !== "string")
165 return this._status.E_BADARGTYPE("extensionId", typeof id, "string");
166 var extensionHeaders = this._extraHeaders[id];
167 if (!extensionHeaders) {
168 extensionHeaders = {};
169 this._extraHeaders[id] = extensionHeaders;
171 for (name in message.headers)
172 extensionHeaders[name] = message.headers[name];
174 for (extension in this._extraHeaders) {
175 var headers = this._extraHeaders[extension];
176 for (name in headers) {
177 if (typeof headers[name] === "string")
178 allHeaders[name] = headers[name];
181 NetworkAgent.setExtraHeaders(allHeaders);
184 _onCreatePanel: function(message, port)
187 // The ids are generated on the client API side and must be unique, so the check below
188 // shouldn't be hit unless someone is bypassing the API.
189 if (id in this._clientObjects || id in WebInspector.panels)
190 return this._status.E_EXISTS(id);
192 var panel = new WebInspector.ExtensionPanel(id, message.title, this._expandResourcePath(port._extensionOrigin, message.icon));
193 this._clientObjects[id] = panel;
194 WebInspector.panels[id] = panel;
195 WebInspector.addPanel(panel);
197 var iframe = this.createClientIframe(panel.element, this._expandResourcePath(port._extensionOrigin, message.page));
198 iframe.addStyleClass("panel");
199 return this._status.OK();
202 _onCreateSidebarPane: function(message, constructor)
204 var panel = WebInspector.panels[message.panel];
206 return this._status.E_NOTFOUND(message.panel);
207 if (!panel.sidebarElement || !panel.sidebarPanes)
208 return this._status.E_NOTSUPPORTED();
210 var sidebar = new WebInspector.ExtensionSidebarPane(message.title, message.id);
211 this._clientObjects[id] = sidebar;
212 panel.sidebarPanes[id] = sidebar;
213 panel.sidebarElement.appendChild(sidebar.element);
215 return this._status.OK();
218 createClientIframe: function(parent, url)
220 var iframe = document.createElement("iframe");
222 iframe.addStyleClass("extension");
223 parent.appendChild(iframe);
227 _onSetSidebarHeight: function(message)
229 var sidebar = this._clientObjects[message.id];
231 return this._status.E_NOTFOUND(message.id);
232 sidebar.bodyElement.firstChild.style.height = message.height;
235 _onSetSidebarContent: function(message)
237 var sidebar = this._clientObjects[message.id];
239 return this._status.E_NOTFOUND(message.id);
240 if (message.evaluateOnPage)
241 sidebar.setExpression(message.expression, message.rootTitle);
243 sidebar.setObject(message.expression, message.rootTitle);
246 _onSetSidebarPage: function(message, port)
248 var sidebar = this._clientObjects[message.id];
250 return this._status.E_NOTFOUND(message.id);
251 sidebar.setPage(this._expandResourcePath(port._extensionOrigin, message.page));
254 _onLog: function(message)
256 WebInspector.log(message.message);
259 _onReload: function(message)
261 if (typeof message.userAgent === "string")
262 NetworkAgent.setUserAgentOverride(message.userAgent);
264 PageAgent.reload(false);
265 return this._status.OK();
268 _onEvaluateOnInspectedPage: function(message, port)
270 function callback(error, resultPayload, wasThrown)
274 var resultObject = WebInspector.RemoteObject.fromPayload(resultPayload);
277 result.isException = true;
278 result.value = resultObject.description;
279 this._dispatchCallback(message.requestId, port, result);
281 var evalExpression = "JSON.stringify(eval(unescape('" + escape(message.expression) + "')));";
282 RuntimeAgent.evaluate(evalExpression, "", true, callback.bind(this));
285 _onGetConsoleMessages: function()
287 return WebInspector.console.messages.map(this._makeConsoleMessage);
290 _onAddConsoleMessage: function(message)
292 function convertSeverity(level)
295 case WebInspector.extensionAPI.console.Severity.Tip:
296 return WebInspector.ConsoleMessage.MessageLevel.Tip;
297 case WebInspector.extensionAPI.console.Severity.Log:
298 return WebInspector.ConsoleMessage.MessageLevel.Log;
299 case WebInspector.extensionAPI.console.Severity.Warning:
300 return WebInspector.ConsoleMessage.MessageLevel.Warning;
301 case WebInspector.extensionAPI.console.Severity.Error:
302 return WebInspector.ConsoleMessage.MessageLevel.Error;
303 case WebInspector.extensionAPI.console.Severity.Debug:
304 return WebInspector.ConsoleMessage.MessageLevel.Debug;
307 var level = convertSeverity(message.severity);
309 return this._status.E_BADARG("message.severity", message.severity);
311 var consoleMessage = new WebInspector.ConsoleMessage(
312 WebInspector.ConsoleMessage.MessageSource.JS,
313 WebInspector.ConsoleMessage.MessageType.Log,
321 null); // networkRequestId
322 WebInspector.console.addMessage(consoleMessage);
325 _makeConsoleMessage: function(message)
327 function convertLevel(level)
332 case WebInspector.ConsoleMessage.MessageLevel.Tip:
333 return WebInspector.extensionAPI.console.Severity.Tip;
334 case WebInspector.ConsoleMessage.MessageLevel.Log:
335 return WebInspector.extensionAPI.console.Severity.Log;
336 case WebInspector.ConsoleMessage.MessageLevel.Warning:
337 return WebInspector.extensionAPI.console.Severity.Warning;
338 case WebInspector.ConsoleMessage.MessageLevel.Error:
339 return WebInspector.extensionAPI.console.Severity.Error;
340 case WebInspector.ConsoleMessage.MessageLevel.Debug:
341 return WebInspector.extensionAPI.console.Severity.Debug;
343 return WebInspector.extensionAPI.console.Severity.Log;
347 severity: convertLevel(message.level),
351 result.url = message.url;
353 result.line = message.line;
357 _onGetHAR: function()
359 var requests = WebInspector.networkLog.resources;
360 var harLog = (new WebInspector.HARLog(requests)).build();
361 for (var i = 0; i < harLog.entries.length; ++i)
362 harLog.entries[i]._requestId = this._requestId(requests[i]);
366 _makeResource: function(resource)
370 type: WebInspector.Resource.Type.toString(resource.type)
374 _onGetPageResources: function()
377 function pushResourceData(resource)
379 resources.push(this._makeResource(resource));
381 WebInspector.resourceTreeModel.forAllResources(pushResourceData.bind(this));
385 _getResourceContent: function(resource, message, port)
387 function onContentAvailable(content, encoded)
390 encoding: encoded ? "base64" : "",
393 this._dispatchCallback(message.requestId, port, response);
395 resource.requestContent(onContentAvailable.bind(this));
398 _onGetRequestContent: function(message, port)
400 var request = this._requestById(message.id);
402 return this._status.E_NOTFOUND(message.id);
403 this._getResourceContent(request, message, port);
406 _onGetResourceContent: function(message, port)
408 var resource = WebInspector.resourceTreeModel.resourceForURL(message.url);
410 return this._status.E_NOTFOUND(message.url);
411 this._getResourceContent(resource, message, port);
414 _onSetResourceContent: function(message, port)
416 function callbackWrapper(error)
418 var response = error ? this._status.E_FAILED(error) : this._status.OK();
419 this._dispatchCallback(message.requestId, port, response);
421 var resource = WebInspector.resourceTreeModel.resourceForURL(message.url);
423 return this._status.E_NOTFOUND(message.url);
424 resource.setContent(message.content, message.commit, callbackWrapper.bind(this));
427 _requestId: function(request)
429 if (!request._extensionRequestId) {
430 request._extensionRequestId = ++this._lastRequestId;
431 this._requests[request._extensionRequestId] = request;
433 return request._extensionRequestId;
436 _requestById: function(id)
438 return this._requests[id];
441 _onAddAuditCategory: function(message)
443 var category = new WebInspector.ExtensionAuditCategory(message.id, message.displayName, message.resultCount);
444 if (WebInspector.panels.audits.getCategory(category.id))
445 return this._status.E_EXISTS(category.id);
446 this._clientObjects[message.id] = category;
447 WebInspector.panels.audits.addCategory(category);
450 _onAddAuditResult: function(message)
452 var auditResult = this._clientObjects[message.resultId];
454 return this._status.E_NOTFOUND(message.resultId);
456 auditResult.addResult(message.displayName, message.description, message.severity, message.details);
460 return this._status.OK();
463 _onStopAuditCategoryRun: function(message)
465 var auditRun = this._clientObjects[message.resultId];
467 return this._status.E_NOTFOUND(message.resultId);
471 _dispatchCallback: function(requestId, port, result)
473 port.postMessage({ command: "callback", requestId: requestId, result: result });
476 initExtensions: function()
478 this._registerAutosubscriptionHandler("console-message-added",
479 WebInspector.console, WebInspector.ConsoleModel.Events.MessageAdded, this._notifyConsoleMessageAdded);
480 this._registerAutosubscriptionHandler("network-request-finished",
481 WebInspector.networkManager, WebInspector.NetworkManager.EventTypes.ResourceFinished, this._notifyRequestFinished);
482 this._registerAutosubscriptionHandler("resource-added",
483 WebInspector.resourceTreeModel, WebInspector.ResourceTreeModel.EventTypes.ResourceAdded, this._notifyResourceAdded);
485 function onTimelineSubscriptionStarted()
487 WebInspector.timelineManager.addEventListener(WebInspector.TimelineManager.EventTypes.TimelineEventRecorded,
488 this._notifyTimelineEventRecorded, this);
489 WebInspector.timelineManager.start();
491 function onTimelineSubscriptionStopped()
493 WebInspector.timelineManager.stop();
494 WebInspector.timelineManager.removeEventListener(WebInspector.TimelineManager.EventTypes.TimelineEventRecorded,
495 this._notifyTimelineEventRecorded, this);
497 this._registerSubscriptionHandler("timeline-event-recorded", onTimelineSubscriptionStarted.bind(this), onTimelineSubscriptionStopped.bind(this));
499 WebInspector.resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.InspectedURLChanged,
500 this._inspectedURLChanged, this);
501 InspectorExtensionRegistry.getExtensionsAsync();
504 _notifyConsoleMessageAdded: function(event)
506 this._postNotification("console-message-added", this._makeConsoleMessage(event.data));
509 _notifyResourceAdded: function(event)
511 var resource = event.data;
512 this._postNotification("resource-added", this._makeResource(resource));
515 _notifyRequestFinished: function(event)
517 var request = event.data;
518 this._postNotification("network-request-finished", this._requestId(request), (new WebInspector.HAREntry(request)).build());
521 _notifyTimelineEventRecorded: function(event)
523 this._postNotification("timeline-event-recorded", event.data);
526 _addExtensions: function(extensions)
528 const urlOriginRegExp = new RegExp("([^:]+:\/\/[^/]*)\/"); // Can't use regexp literal here, MinJS chokes on it.
530 // See ExtensionAPI.js and ExtensionCommon.js for details.
531 InspectorFrontendHost.setExtensionAPI(this._buildExtensionAPIInjectedScript());
532 for (var i = 0; i < extensions.length; ++i) {
533 var extension = extensions[i];
535 if (!extension.startPage)
537 var originMatch = urlOriginRegExp.exec(extension.startPage);
539 console.error("Skipping extension with invalid URL: " + extension.startPage);
542 this._allowedOrigins[originMatch[1]] = true;
543 var iframe = document.createElement("iframe");
544 iframe.src = extension.startPage;
545 iframe.style.display = "none";
546 document.body.appendChild(iframe);
548 console.error("Failed to initialize extension " + extension.startPage + ":" + e);
553 _buildExtensionAPIInjectedScript: function()
555 var resourceTypes = {};
556 var resourceTypeProperties = Object.getOwnPropertyNames(WebInspector.Resource.Type);
557 for (var i = 0; i < resourceTypeProperties.length; ++i) {
558 var propName = resourceTypeProperties[i];
559 var propValue = WebInspector.Resource.Type[propName];
560 if (typeof propValue === "number")
561 resourceTypes[propName] = WebInspector.Resource.Type.toString(propValue);
563 var platformAPI = WebInspector.buildPlatformExtensionAPI ? WebInspector.buildPlatformExtensionAPI() : "";
564 return "(function(){ " +
565 "var apiPrivate = {};" +
566 "(" + WebInspector.commonExtensionSymbols.toString() + ")(apiPrivate);" +
567 "(" + WebInspector.injectedExtensionAPI.toString() + ").apply(this, arguments);" +
572 _onWindowMessage: function(event)
574 if (event.data !== "registerExtension")
576 if (!this._allowedOrigins.hasOwnProperty(event.origin)) {
577 if (event.origin !== location.origin) // Just ignore inspector frames.
578 console.error("Ignoring unauthorized client request from " + event.origin);
581 var port = event.ports[0];
582 port._extensionOrigin = event.origin;
583 port.addEventListener("message", this._onmessage.bind(this), false);
587 _onmessage: function(event)
589 var message = event.data;
592 if (message.command in this._handlers)
593 result = this._handlers[message.command](message, event.target);
595 result = this._status.E_NOTSUPPORTED(message.command);
597 if (result && message.requestId)
598 this._dispatchCallback(message.requestId, event.target, result);
601 _registerHandler: function(command, callback)
603 this._handlers[command] = callback;
606 _registerSubscriptionHandler: function(eventTopic, onSubscribeFirst, onUnsubscribeLast)
608 this._subscriptionStartHandlers[eventTopic] = onSubscribeFirst;
609 this._subscriptionStopHandlers[eventTopic] = onUnsubscribeLast;
612 _registerAutosubscriptionHandler: function(eventTopic, eventTarget, frontendEventType, handler)
614 this._registerSubscriptionHandler(eventTopic,
615 WebInspector.Object.prototype.addEventListener.bind(eventTarget, frontendEventType, handler, this),
616 WebInspector.Object.prototype.removeEventListener.bind(eventTarget, frontendEventType, handler, this));
619 _expandResourcePath: function(extensionPath, resourcePath)
623 return extensionPath + escape(this._normalizePath(resourcePath));
626 _normalizePath: function(path)
628 var source = path.split("/");
631 for (var i = 0; i < source.length; ++i) {
632 if (source[i] === ".")
634 // Ignore empty path components resulting from //, as well as a leading and traling slashes.
635 if (source[i] === "")
637 if (source[i] === "..")
640 result.push(source[i]);
642 return "/" + result.join("/");
646 WebInspector.ExtensionServer._statuses =
649 E_EXISTS: "Object already exists: %s",
650 E_BADARG: "Invalid argument %s: %s",
651 E_BADARGTYPE: "Invalid type for argument %s: got %s, expected %s",
652 E_NOTFOUND: "Object not found: %s",
653 E_NOTSUPPORTED: "Object does not support requested operation: %s",
654 E_FAILED: "Operation failed: %s"
657 WebInspector.ExtensionStatus = function()
659 function makeStatus(code)
661 var description = WebInspector.ExtensionServer._statuses[code] || code;
662 var details = Array.prototype.slice.call(arguments, 1);
663 var status = { code: code, description: description, details: details };
665 status.isError = true;
666 console.log("Extension server error: " + String.vsprintf(description, details));
670 for (status in WebInspector.ExtensionServer._statuses)
671 this[status] = makeStatus.bind(null, status);
674 WebInspector.addExtensions = function(extensions)
676 WebInspector.extensionServer._addExtensions(extensions);
679 WebInspector.extensionServer = new WebInspector.ExtensionServer();