ExtensionServer.js 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  1. /*
  2. * Copyright (C) 2011 Google Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. *
  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
  13. * distribution.
  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.
  17. *
  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.
  29. */
  30. /**
  31. * @constructor
  32. */
  33. WebInspector.ExtensionServer = function()
  34. {
  35. this._clientObjects = {};
  36. this._handlers = {};
  37. this._subscribers = {};
  38. this._subscriptionStartHandlers = {};
  39. this._subscriptionStopHandlers = {};
  40. this._extraHeaders = {};
  41. this._requests = {};
  42. this._lastRequestId = 0;
  43. this._registeredExtensions = {};
  44. this._status = new WebInspector.ExtensionStatus();
  45. var commands = WebInspector.extensionAPI.Commands;
  46. this._registerHandler(commands.AddAuditCategory, this._onAddAuditCategory.bind(this));
  47. this._registerHandler(commands.AddAuditResult, this._onAddAuditResult.bind(this));
  48. this._registerHandler(commands.AddConsoleMessage, this._onAddConsoleMessage.bind(this));
  49. this._registerHandler(commands.AddRequestHeaders, this._onAddRequestHeaders.bind(this));
  50. this._registerHandler(commands.CreatePanel, this._onCreatePanel.bind(this));
  51. this._registerHandler(commands.CreateSidebarPane, this._onCreateSidebarPane.bind(this));
  52. this._registerHandler(commands.CreateStatusBarButton, this._onCreateStatusBarButton.bind(this));
  53. this._registerHandler(commands.EvaluateOnInspectedPage, this._onEvaluateOnInspectedPage.bind(this));
  54. this._registerHandler(commands.ForwardKeyboardEvent, this._onForwardKeyboardEvent.bind(this));
  55. this._registerHandler(commands.GetHAR, this._onGetHAR.bind(this));
  56. this._registerHandler(commands.GetConsoleMessages, this._onGetConsoleMessages.bind(this));
  57. this._registerHandler(commands.GetPageResources, this._onGetPageResources.bind(this));
  58. this._registerHandler(commands.GetRequestContent, this._onGetRequestContent.bind(this));
  59. this._registerHandler(commands.GetResourceContent, this._onGetResourceContent.bind(this));
  60. this._registerHandler(commands.Reload, this._onReload.bind(this));
  61. this._registerHandler(commands.SetOpenResourceHandler, this._onSetOpenResourceHandler.bind(this));
  62. this._registerHandler(commands.SetResourceContent, this._onSetResourceContent.bind(this));
  63. this._registerHandler(commands.SetSidebarHeight, this._onSetSidebarHeight.bind(this));
  64. this._registerHandler(commands.SetSidebarContent, this._onSetSidebarContent.bind(this));
  65. this._registerHandler(commands.SetSidebarPage, this._onSetSidebarPage.bind(this));
  66. this._registerHandler(commands.ShowPanel, this._onShowPanel.bind(this));
  67. this._registerHandler(commands.StopAuditCategoryRun, this._onStopAuditCategoryRun.bind(this));
  68. this._registerHandler(commands.Subscribe, this._onSubscribe.bind(this));
  69. this._registerHandler(commands.OpenResource, this._onOpenResource.bind(this));
  70. this._registerHandler(commands.Unsubscribe, this._onUnsubscribe.bind(this));
  71. this._registerHandler(commands.UpdateButton, this._onUpdateButton.bind(this));
  72. this._registerHandler(commands.UpdateAuditProgress, this._onUpdateAuditProgress.bind(this));
  73. window.addEventListener("message", this._onWindowMessage.bind(this), false);
  74. }
  75. WebInspector.ExtensionServer.prototype = {
  76. hasExtensions: function()
  77. {
  78. return !!Object.keys(this._registeredExtensions).length;
  79. },
  80. notifySearchAction: function(panelId, action, searchString)
  81. {
  82. this._postNotification(WebInspector.extensionAPI.Events.PanelSearch + panelId, action, searchString);
  83. },
  84. notifyViewShown: function(identifier, frameIndex)
  85. {
  86. this._postNotification(WebInspector.extensionAPI.Events.ViewShown + identifier, frameIndex);
  87. },
  88. notifyViewHidden: function(identifier)
  89. {
  90. this._postNotification(WebInspector.extensionAPI.Events.ViewHidden + identifier);
  91. },
  92. notifyButtonClicked: function(identifier)
  93. {
  94. this._postNotification(WebInspector.extensionAPI.Events.ButtonClicked + identifier);
  95. },
  96. _inspectedURLChanged: function(event)
  97. {
  98. this._requests = {};
  99. var url = event.data;
  100. this._postNotification(WebInspector.extensionAPI.Events.InspectedURLChanged, url);
  101. },
  102. startAuditRun: function(category, auditRun)
  103. {
  104. this._clientObjects[auditRun.id] = auditRun;
  105. this._postNotification("audit-started-" + category.id, auditRun.id);
  106. },
  107. stopAuditRun: function(auditRun)
  108. {
  109. delete this._clientObjects[auditRun.id];
  110. },
  111. /**
  112. * @param {string} type
  113. * @return {boolean}
  114. */
  115. hasSubscribers: function(type)
  116. {
  117. return !!this._subscribers[type];
  118. },
  119. /**
  120. * @param {string} type
  121. * @param {...*} vararg
  122. */
  123. _postNotification: function(type, vararg)
  124. {
  125. var subscribers = this._subscribers[type];
  126. if (!subscribers)
  127. return;
  128. var message = {
  129. command: "notify-" + type,
  130. arguments: Array.prototype.slice.call(arguments, 1)
  131. };
  132. for (var i = 0; i < subscribers.length; ++i)
  133. subscribers[i].postMessage(message);
  134. },
  135. _onSubscribe: function(message, port)
  136. {
  137. var subscribers = this._subscribers[message.type];
  138. if (subscribers)
  139. subscribers.push(port);
  140. else {
  141. this._subscribers[message.type] = [ port ];
  142. if (this._subscriptionStartHandlers[message.type])
  143. this._subscriptionStartHandlers[message.type]();
  144. }
  145. },
  146. _onUnsubscribe: function(message, port)
  147. {
  148. var subscribers = this._subscribers[message.type];
  149. if (!subscribers)
  150. return;
  151. subscribers.remove(port);
  152. if (!subscribers.length) {
  153. delete this._subscribers[message.type];
  154. if (this._subscriptionStopHandlers[message.type])
  155. this._subscriptionStopHandlers[message.type]();
  156. }
  157. },
  158. _onAddRequestHeaders: function(message)
  159. {
  160. var id = message.extensionId;
  161. if (typeof id !== "string")
  162. return this._status.E_BADARGTYPE("extensionId", typeof id, "string");
  163. var extensionHeaders = this._extraHeaders[id];
  164. if (!extensionHeaders) {
  165. extensionHeaders = {};
  166. this._extraHeaders[id] = extensionHeaders;
  167. }
  168. for (var name in message.headers)
  169. extensionHeaders[name] = message.headers[name];
  170. var allHeaders = /** @type NetworkAgent.Headers */ ({});
  171. for (var extension in this._extraHeaders) {
  172. var headers = this._extraHeaders[extension];
  173. for (name in headers) {
  174. if (typeof headers[name] === "string")
  175. allHeaders[name] = headers[name];
  176. }
  177. }
  178. NetworkAgent.setExtraHTTPHeaders(allHeaders);
  179. },
  180. _onCreatePanel: function(message, port)
  181. {
  182. var id = message.id;
  183. // The ids are generated on the client API side and must be unique, so the check below
  184. // shouldn't be hit unless someone is bypassing the API.
  185. if (id in this._clientObjects || id in WebInspector.panels)
  186. return this._status.E_EXISTS(id);
  187. var page = this._expandResourcePath(port._extensionOrigin, message.page);
  188. var panelDescriptor = new WebInspector.PanelDescriptor(id, message.title, undefined, undefined, new WebInspector.ExtensionPanel(id, page));
  189. this._clientObjects[id] = panelDescriptor.panel();
  190. WebInspector.inspectorView.addPanel(panelDescriptor);
  191. return this._status.OK();
  192. },
  193. _onShowPanel: function(message)
  194. {
  195. // Note: WebInspector.showPanel already sanitizes input.
  196. WebInspector.showPanel(message.id);
  197. },
  198. _onCreateStatusBarButton: function(message, port)
  199. {
  200. var panel = this._clientObjects[message.panel];
  201. if (!panel || !(panel instanceof WebInspector.ExtensionPanel))
  202. return this._status.E_NOTFOUND(message.panel);
  203. var button = new WebInspector.ExtensionButton(message.id, this._expandResourcePath(port._extensionOrigin, message.icon), message.tooltip, message.disabled);
  204. this._clientObjects[message.id] = button;
  205. panel.addStatusBarItem(button.element);
  206. return this._status.OK();
  207. },
  208. _onUpdateButton: function(message, port)
  209. {
  210. var button = this._clientObjects[message.id];
  211. if (!button || !(button instanceof WebInspector.ExtensionButton))
  212. return this._status.E_NOTFOUND(message.id);
  213. button.update(this._expandResourcePath(port._extensionOrigin, message.icon), message.tooltip, message.disabled);
  214. return this._status.OK();
  215. },
  216. _onCreateSidebarPane: function(message)
  217. {
  218. var panel = WebInspector.panel(message.panel);
  219. if (!panel)
  220. return this._status.E_NOTFOUND(message.panel);
  221. if (!panel.addExtensionSidebarPane)
  222. return this._status.E_NOTSUPPORTED();
  223. var id = message.id;
  224. var sidebar = new WebInspector.ExtensionSidebarPane(message.title, message.id);
  225. this._clientObjects[id] = sidebar;
  226. panel.addExtensionSidebarPane(id, sidebar);
  227. return this._status.OK();
  228. },
  229. _onSetSidebarHeight: function(message)
  230. {
  231. var sidebar = this._clientObjects[message.id];
  232. if (!sidebar)
  233. return this._status.E_NOTFOUND(message.id);
  234. sidebar.setHeight(message.height);
  235. return this._status.OK();
  236. },
  237. _onSetSidebarContent: function(message, port)
  238. {
  239. var sidebar = this._clientObjects[message.id];
  240. if (!sidebar)
  241. return this._status.E_NOTFOUND(message.id);
  242. function callback(error)
  243. {
  244. var result = error ? this._status.E_FAILED(error) : this._status.OK();
  245. this._dispatchCallback(message.requestId, port, result);
  246. }
  247. if (message.evaluateOnPage)
  248. return sidebar.setExpression(message.expression, message.rootTitle, message.evaluateOptions, port._extensionOrigin, callback.bind(this));
  249. sidebar.setObject(message.expression, message.rootTitle, callback.bind(this));
  250. },
  251. _onSetSidebarPage: function(message, port)
  252. {
  253. var sidebar = this._clientObjects[message.id];
  254. if (!sidebar)
  255. return this._status.E_NOTFOUND(message.id);
  256. sidebar.setPage(this._expandResourcePath(port._extensionOrigin, message.page));
  257. },
  258. _onOpenResource: function(message)
  259. {
  260. var a = document.createElement("a");
  261. a.href = message.url;
  262. a.lineNumber = message.lineNumber;
  263. return WebInspector.showAnchorLocation(a) ? this._status.OK() : this._status.E_NOTFOUND(message.url);
  264. },
  265. _onSetOpenResourceHandler: function(message, port)
  266. {
  267. var name = this._registeredExtensions[port._extensionOrigin].name || ("Extension " + port._extensionOrigin);
  268. if (message.handlerPresent)
  269. WebInspector.openAnchorLocationRegistry.registerHandler(name, this._handleOpenURL.bind(this, port));
  270. else
  271. WebInspector.openAnchorLocationRegistry.unregisterHandler(name);
  272. },
  273. _handleOpenURL: function(port, details)
  274. {
  275. var url = /** @type {string} */ (details.url);
  276. var contentProvider = WebInspector.workspace.uiSourceCodeForOriginURL(url) || WebInspector.resourceForURL(url);
  277. if (!contentProvider)
  278. return false;
  279. var lineNumber = details.lineNumber;
  280. if (typeof lineNumber === "number")
  281. lineNumber += 1;
  282. port.postMessage({
  283. command: "open-resource",
  284. resource: this._makeResource(contentProvider),
  285. lineNumber: lineNumber
  286. });
  287. return true;
  288. },
  289. _onReload: function(message)
  290. {
  291. var options = /** @type ExtensionReloadOptions */ (message.options || {});
  292. NetworkAgent.setUserAgentOverride(typeof options.userAgent === "string" ? options.userAgent : "");
  293. var injectedScript;
  294. if (options.injectedScript)
  295. injectedScript = "(function(){" + options.injectedScript + "})()";
  296. var preprocessingScript = options.preprocessingScript;
  297. PageAgent.reload(!!options.ignoreCache, injectedScript, preprocessingScript);
  298. return this._status.OK();
  299. },
  300. _onEvaluateOnInspectedPage: function(message, port)
  301. {
  302. /**
  303. * @param {?Protocol.Error} error
  304. * @param {RuntimeAgent.RemoteObject} resultPayload
  305. * @param {boolean=} wasThrown
  306. */
  307. function callback(error, resultPayload, wasThrown)
  308. {
  309. var result;
  310. if (error)
  311. result = this._status.E_PROTOCOLERROR(error.toString());
  312. else if (wasThrown)
  313. result = { isException: true, value: resultPayload.description };
  314. else
  315. result = { value: resultPayload.value };
  316. this._dispatchCallback(message.requestId, port, result);
  317. }
  318. return this.evaluate(message.expression, true, true, message.evaluateOptions, port._extensionOrigin, callback.bind(this));
  319. },
  320. _onGetConsoleMessages: function()
  321. {
  322. return WebInspector.console.messages.map(this._makeConsoleMessage);
  323. },
  324. _onAddConsoleMessage: function(message)
  325. {
  326. function convertSeverity(level)
  327. {
  328. switch (level) {
  329. case WebInspector.extensionAPI.console.Severity.Log:
  330. return WebInspector.ConsoleMessage.MessageLevel.Log;
  331. case WebInspector.extensionAPI.console.Severity.Warning:
  332. return WebInspector.ConsoleMessage.MessageLevel.Warning;
  333. case WebInspector.extensionAPI.console.Severity.Error:
  334. return WebInspector.ConsoleMessage.MessageLevel.Error;
  335. case WebInspector.extensionAPI.console.Severity.Debug:
  336. return WebInspector.ConsoleMessage.MessageLevel.Debug;
  337. }
  338. }
  339. var level = convertSeverity(message.severity);
  340. if (!level)
  341. return this._status.E_BADARG("message.severity", message.severity);
  342. var consoleMessage = WebInspector.ConsoleMessage.create(
  343. WebInspector.ConsoleMessage.MessageSource.JS,
  344. level,
  345. message.text,
  346. WebInspector.ConsoleMessage.MessageType.Log,
  347. message.url,
  348. message.line);
  349. WebInspector.console.addMessage(consoleMessage);
  350. },
  351. _makeConsoleMessage: function(message)
  352. {
  353. function convertLevel(level)
  354. {
  355. if (!level)
  356. return;
  357. switch (level) {
  358. case WebInspector.ConsoleMessage.MessageLevel.Log:
  359. return WebInspector.extensionAPI.console.Severity.Log;
  360. case WebInspector.ConsoleMessage.MessageLevel.Warning:
  361. return WebInspector.extensionAPI.console.Severity.Warning;
  362. case WebInspector.ConsoleMessage.MessageLevel.Error:
  363. return WebInspector.extensionAPI.console.Severity.Error;
  364. case WebInspector.ConsoleMessage.MessageLevel.Debug:
  365. return WebInspector.extensionAPI.console.Severity.Debug;
  366. default:
  367. return WebInspector.extensionAPI.console.Severity.Log;
  368. }
  369. }
  370. var result = {
  371. severity: convertLevel(message.level),
  372. text: message.text,
  373. };
  374. if (message.url)
  375. result.url = message.url;
  376. if (message.line)
  377. result.line = message.line;
  378. return result;
  379. },
  380. _onGetHAR: function()
  381. {
  382. var requests = WebInspector.networkLog.requests;
  383. var harLog = (new WebInspector.HARLog(requests)).build();
  384. for (var i = 0; i < harLog.entries.length; ++i)
  385. harLog.entries[i]._requestId = this._requestId(requests[i]);
  386. return harLog;
  387. },
  388. /**
  389. * @param {WebInspector.ContentProvider} contentProvider
  390. */
  391. _makeResource: function(contentProvider)
  392. {
  393. return {
  394. url: contentProvider.contentURL(),
  395. type: contentProvider.contentType().name()
  396. };
  397. },
  398. /**
  399. * @return {!Array.<WebInspector.ContentProvider>}
  400. */
  401. _onGetPageResources: function()
  402. {
  403. var resources = {};
  404. function pushResourceData(contentProvider)
  405. {
  406. if (!resources[contentProvider.contentURL()])
  407. resources[contentProvider.contentURL()] = this._makeResource(contentProvider);
  408. }
  409. var uiSourceCodes = WebInspector.workspace.uiSourceCodesForProjectType(WebInspector.projectTypes.Network);
  410. uiSourceCodes.forEach(pushResourceData.bind(this));
  411. WebInspector.resourceTreeModel.forAllResources(pushResourceData.bind(this));
  412. return Object.values(resources);
  413. },
  414. /**
  415. * @param {WebInspector.ContentProvider} contentProvider
  416. */
  417. _getResourceContent: function(contentProvider, message, port)
  418. {
  419. /**
  420. * @param {?string} content
  421. * @param {boolean} contentEncoded
  422. * @param {string} mimeType
  423. */
  424. function onContentAvailable(content, contentEncoded, mimeType)
  425. {
  426. var response = {
  427. encoding: contentEncoded ? "base64" : "",
  428. content: content
  429. };
  430. this._dispatchCallback(message.requestId, port, response);
  431. }
  432. contentProvider.requestContent(onContentAvailable.bind(this));
  433. },
  434. _onGetRequestContent: function(message, port)
  435. {
  436. var request = this._requestById(message.id);
  437. if (!request)
  438. return this._status.E_NOTFOUND(message.id);
  439. this._getResourceContent(request, message, port);
  440. },
  441. _onGetResourceContent: function(message, port)
  442. {
  443. var url = /** @type {string} */ (message.url);
  444. var contentProvider = WebInspector.workspace.uiSourceCodeForOriginURL(url) || WebInspector.resourceForURL(url);
  445. if (!contentProvider)
  446. return this._status.E_NOTFOUND(url);
  447. this._getResourceContent(contentProvider, message, port);
  448. },
  449. _onSetResourceContent: function(message, port)
  450. {
  451. /**
  452. * @param {?Protocol.Error} error
  453. */
  454. function callbackWrapper(error)
  455. {
  456. var response = error ? this._status.E_FAILED(error) : this._status.OK();
  457. this._dispatchCallback(message.requestId, port, response);
  458. }
  459. var url = /** @type {string} */ (message.url);
  460. var uiSourceCode = WebInspector.workspace.uiSourceCodeForOriginURL(url);
  461. if (!uiSourceCode) {
  462. var resource = WebInspector.resourceTreeModel.resourceForURL(url);
  463. if (!resource)
  464. return this._status.E_NOTFOUND(url);
  465. return this._status.E_NOTSUPPORTED("Resource is not editable")
  466. }
  467. uiSourceCode.setWorkingCopy(message.content);
  468. if (message.commit)
  469. uiSourceCode.commitWorkingCopy(callbackWrapper.bind(this));
  470. else
  471. callbackWrapper.call(this, null);
  472. },
  473. _requestId: function(request)
  474. {
  475. if (!request._extensionRequestId) {
  476. request._extensionRequestId = ++this._lastRequestId;
  477. this._requests[request._extensionRequestId] = request;
  478. }
  479. return request._extensionRequestId;
  480. },
  481. _requestById: function(id)
  482. {
  483. return this._requests[id];
  484. },
  485. _onAddAuditCategory: function(message, port)
  486. {
  487. var category = new WebInspector.ExtensionAuditCategory(port._extensionOrigin, message.id, message.displayName, message.resultCount);
  488. if (WebInspector.panel("audits").getCategory(category.id))
  489. return this._status.E_EXISTS(category.id);
  490. this._clientObjects[message.id] = category;
  491. WebInspector.panel("audits").addCategory(category);
  492. },
  493. _onAddAuditResult: function(message)
  494. {
  495. var auditResult = this._clientObjects[message.resultId];
  496. if (!auditResult)
  497. return this._status.E_NOTFOUND(message.resultId);
  498. try {
  499. auditResult.addResult(message.displayName, message.description, message.severity, message.details);
  500. } catch (e) {
  501. return e;
  502. }
  503. return this._status.OK();
  504. },
  505. _onUpdateAuditProgress: function(message)
  506. {
  507. var auditResult = this._clientObjects[message.resultId];
  508. if (!auditResult)
  509. return this._status.E_NOTFOUND(message.resultId);
  510. auditResult.updateProgress(Math.min(Math.max(0, message.progress), 1));
  511. },
  512. _onStopAuditCategoryRun: function(message)
  513. {
  514. var auditRun = this._clientObjects[message.resultId];
  515. if (!auditRun)
  516. return this._status.E_NOTFOUND(message.resultId);
  517. auditRun.done();
  518. },
  519. _onForwardKeyboardEvent: function(message)
  520. {
  521. const Esc = "U+001B";
  522. if (!message.ctrlKey && !message.altKey && !message.metaKey && !/^F\d+$/.test(message.keyIdentifier) && message.keyIdentifier !== Esc)
  523. return;
  524. // Fool around closure compiler -- it has its own notion of both KeyboardEvent constructor
  525. // and initKeyboardEvent methods and overriding these in externs.js does not have effect.
  526. var event = new window.KeyboardEvent(message.eventType, {
  527. keyIdentifier: message.keyIdentifier,
  528. location: message.location,
  529. ctrlKey: message.ctrlKey,
  530. altKey: message.altKey,
  531. shiftKey: message.shiftKey,
  532. metaKey: message.metaKey
  533. });
  534. document.dispatchEvent(event);
  535. },
  536. _dispatchCallback: function(requestId, port, result)
  537. {
  538. if (requestId)
  539. port.postMessage({ command: "callback", requestId: requestId, result: result });
  540. },
  541. initExtensions: function()
  542. {
  543. this._registerAutosubscriptionHandler(WebInspector.extensionAPI.Events.ConsoleMessageAdded,
  544. WebInspector.console, WebInspector.ConsoleModel.Events.MessageAdded, this._notifyConsoleMessageAdded);
  545. this._registerAutosubscriptionHandler(WebInspector.extensionAPI.Events.NetworkRequestFinished,
  546. WebInspector.networkManager, WebInspector.NetworkManager.EventTypes.RequestFinished, this._notifyRequestFinished);
  547. this._registerAutosubscriptionHandler(WebInspector.extensionAPI.Events.ResourceAdded,
  548. WebInspector.workspace,
  549. WebInspector.Workspace.Events.UISourceCodeAdded,
  550. this._notifyResourceAdded);
  551. this._registerAutosubscriptionHandler(WebInspector.extensionAPI.Events.ElementsPanelObjectSelected,
  552. WebInspector.notifications,
  553. WebInspector.ElementsTreeOutline.Events.SelectedNodeChanged,
  554. this._notifyElementsSelectionChanged);
  555. this._registerAutosubscriptionHandler(WebInspector.extensionAPI.Events.ResourceContentCommitted,
  556. WebInspector.workspace,
  557. WebInspector.Workspace.Events.UISourceCodeContentCommitted,
  558. this._notifyUISourceCodeContentCommitted);
  559. function onTimelineSubscriptionStarted()
  560. {
  561. WebInspector.timelineManager.addEventListener(WebInspector.TimelineManager.EventTypes.TimelineEventRecorded,
  562. this._notifyTimelineEventRecorded, this);
  563. WebInspector.timelineManager.start();
  564. }
  565. function onTimelineSubscriptionStopped()
  566. {
  567. WebInspector.timelineManager.stop();
  568. WebInspector.timelineManager.removeEventListener(WebInspector.TimelineManager.EventTypes.TimelineEventRecorded,
  569. this._notifyTimelineEventRecorded, this);
  570. }
  571. this._registerSubscriptionHandler(WebInspector.extensionAPI.Events.TimelineEventRecorded,
  572. onTimelineSubscriptionStarted.bind(this), onTimelineSubscriptionStopped.bind(this));
  573. WebInspector.resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.InspectedURLChanged,
  574. this._inspectedURLChanged, this);
  575. this._initDone = true;
  576. if (this._pendingExtensions) {
  577. this._pendingExtensions.forEach(this._innerAddExtension, this);
  578. delete this._pendingExtensions;
  579. }
  580. InspectorExtensionRegistry.getExtensionsAsync();
  581. },
  582. _notifyConsoleMessageAdded: function(event)
  583. {
  584. this._postNotification(WebInspector.extensionAPI.Events.ConsoleMessageAdded, this._makeConsoleMessage(event.data));
  585. },
  586. _notifyResourceAdded: function(event)
  587. {
  588. var uiSourceCode = /** @type {WebInspector.UISourceCode} */ (event.data);
  589. this._postNotification(WebInspector.extensionAPI.Events.ResourceAdded, this._makeResource(uiSourceCode));
  590. },
  591. _notifyUISourceCodeContentCommitted: function(event)
  592. {
  593. var uiSourceCode = /** @type {WebInspector.UISourceCode} */ (event.data.uiSourceCode);
  594. var content = /** @type {string} */ (event.data.content);
  595. this._postNotification(WebInspector.extensionAPI.Events.ResourceContentCommitted, this._makeResource(uiSourceCode), content);
  596. },
  597. _notifyRequestFinished: function(event)
  598. {
  599. var request = /** @type {WebInspector.NetworkRequest} */ (event.data);
  600. this._postNotification(WebInspector.extensionAPI.Events.NetworkRequestFinished, this._requestId(request), (new WebInspector.HAREntry(request)).build());
  601. },
  602. _notifyElementsSelectionChanged: function()
  603. {
  604. this._postNotification(WebInspector.extensionAPI.Events.ElementsPanelObjectSelected);
  605. },
  606. _notifyTimelineEventRecorded: function(event)
  607. {
  608. this._postNotification(WebInspector.extensionAPI.Events.TimelineEventRecorded, event.data);
  609. },
  610. /**
  611. * @param {Array.<ExtensionDescriptor>} extensions
  612. */
  613. _addExtensions: function(extensions)
  614. {
  615. extensions.forEach(this._addExtension, this);
  616. },
  617. /**
  618. * @param {ExtensionDescriptor} extensionInfo
  619. */
  620. _addExtension: function(extensionInfo)
  621. {
  622. if (this._initDone) {
  623. this._innerAddExtension(extensionInfo);
  624. return;
  625. }
  626. if (this._pendingExtensions)
  627. this._pendingExtensions.push(extensionInfo);
  628. else
  629. this._pendingExtensions = [extensionInfo];
  630. },
  631. /**
  632. * @param {ExtensionDescriptor} extensionInfo
  633. */
  634. _innerAddExtension: function(extensionInfo)
  635. {
  636. const urlOriginRegExp = new RegExp("([^:]+:\/\/[^/]*)\/"); // Can't use regexp literal here, MinJS chokes on it.
  637. var startPage = extensionInfo.startPage;
  638. var name = extensionInfo.name;
  639. try {
  640. var originMatch = urlOriginRegExp.exec(startPage);
  641. if (!originMatch) {
  642. console.error("Skipping extension with invalid URL: " + startPage);
  643. return false;
  644. }
  645. var extensionOrigin = originMatch[1];
  646. if (!this._registeredExtensions[extensionOrigin]) {
  647. // See ExtensionAPI.js and ExtensionCommon.js for details.
  648. InspectorFrontendHost.setInjectedScriptForOrigin(extensionOrigin, buildExtensionAPIInjectedScript(extensionInfo));
  649. this._registeredExtensions[extensionOrigin] = { name: name };
  650. }
  651. var iframe = document.createElement("iframe");
  652. iframe.src = startPage;
  653. iframe.style.display = "none";
  654. document.body.appendChild(iframe);
  655. } catch (e) {
  656. console.error("Failed to initialize extension " + startPage + ":" + e);
  657. return false;
  658. }
  659. return true;
  660. },
  661. _onWindowMessage: function(event)
  662. {
  663. if (event.data === "registerExtension")
  664. this._registerExtension(event.origin, event.ports[0]);
  665. },
  666. _registerExtension: function(origin, port)
  667. {
  668. if (!this._registeredExtensions.hasOwnProperty(origin)) {
  669. if (origin !== window.location.origin) // Just ignore inspector frames.
  670. console.error("Ignoring unauthorized client request from " + origin);
  671. return;
  672. }
  673. port._extensionOrigin = origin;
  674. port.addEventListener("message", this._onmessage.bind(this), false);
  675. port.start();
  676. },
  677. _onmessage: function(event)
  678. {
  679. var message = event.data;
  680. var result;
  681. if (message.command in this._handlers)
  682. result = this._handlers[message.command](message, event.target);
  683. else
  684. result = this._status.E_NOTSUPPORTED(message.command);
  685. if (result && message.requestId)
  686. this._dispatchCallback(message.requestId, event.target, result);
  687. },
  688. _registerHandler: function(command, callback)
  689. {
  690. console.assert(command);
  691. this._handlers[command] = callback;
  692. },
  693. _registerSubscriptionHandler: function(eventTopic, onSubscribeFirst, onUnsubscribeLast)
  694. {
  695. this._subscriptionStartHandlers[eventTopic] = onSubscribeFirst;
  696. this._subscriptionStopHandlers[eventTopic] = onUnsubscribeLast;
  697. },
  698. _registerAutosubscriptionHandler: function(eventTopic, eventTarget, frontendEventType, handler)
  699. {
  700. this._registerSubscriptionHandler(eventTopic,
  701. eventTarget.addEventListener.bind(eventTarget, frontendEventType, handler, this),
  702. eventTarget.removeEventListener.bind(eventTarget, frontendEventType, handler, this));
  703. },
  704. _expandResourcePath: function(extensionPath, resourcePath)
  705. {
  706. if (!resourcePath)
  707. return;
  708. return extensionPath + this._normalizePath(resourcePath);
  709. },
  710. _normalizePath: function(path)
  711. {
  712. var source = path.split("/");
  713. var result = [];
  714. for (var i = 0; i < source.length; ++i) {
  715. if (source[i] === ".")
  716. continue;
  717. // Ignore empty path components resulting from //, as well as a leading and traling slashes.
  718. if (source[i] === "")
  719. continue;
  720. if (source[i] === "..")
  721. result.pop();
  722. else
  723. result.push(source[i]);
  724. }
  725. return "/" + result.join("/");
  726. },
  727. /**
  728. * @param {string} expression
  729. * @param {boolean} exposeCommandLineAPI
  730. * @param {boolean} returnByValue
  731. * @param {Object} options
  732. * @param {string} securityOrigin
  733. * @param {function(?string, ?RuntimeAgent.RemoteObject, boolean=)} callback
  734. */
  735. evaluate: function(expression, exposeCommandLineAPI, returnByValue, options, securityOrigin, callback)
  736. {
  737. var contextId;
  738. if (typeof options === "object") {
  739. function resolveURLToFrame(url)
  740. {
  741. var found;
  742. function hasMatchingURL(frame)
  743. {
  744. found = (frame.url === url) ? frame : null;
  745. return found;
  746. }
  747. WebInspector.resourceTreeModel.frames().some(hasMatchingURL);
  748. return found;
  749. }
  750. var frame = options.frameURL ? resolveURLToFrame(options.frameURL) : WebInspector.resourceTreeModel.mainFrame;
  751. if (!frame) {
  752. if (options.frameURL)
  753. console.warn("evaluate: there is no frame with URL " + options.frameURL);
  754. else
  755. console.warn("evaluate: the main frame is not yet available");
  756. return this._status.E_NOTFOUND(options.frameURL || "<top>");
  757. }
  758. var contextSecurityOrigin;
  759. if (options.useContentScriptContext)
  760. contextSecurityOrigin = securityOrigin;
  761. else if (options.scriptExecutionContext)
  762. contextSecurityOrigin = options.scriptExecutionContext;
  763. var frameContextList = WebInspector.runtimeModel.contextListByFrame(frame);
  764. var context;
  765. if (contextSecurityOrigin) {
  766. context = frameContextList.contextBySecurityOrigin(contextSecurityOrigin);
  767. if (!context) {
  768. console.warn("The JS context " + contextSecurityOrigin + " was not found in the frame " + frame.url)
  769. return this._status.E_NOTFOUND(contextSecurityOrigin)
  770. }
  771. } else {
  772. context = frameContextList.mainWorldContext();
  773. if (!context)
  774. return this._status.E_FAILED(frame.url + " has no execution context");
  775. }
  776. contextId = context.id;
  777. }
  778. RuntimeAgent.evaluate(expression, "extension", exposeCommandLineAPI, true, contextId, returnByValue, false, callback);
  779. }
  780. }
  781. /**
  782. * @constructor
  783. */
  784. WebInspector.ExtensionStatus = function()
  785. {
  786. function makeStatus(code, description)
  787. {
  788. var details = Array.prototype.slice.call(arguments, 2);
  789. var status = { code: code, description: description, details: details };
  790. if (code !== "OK") {
  791. status.isError = true;
  792. console.log("Extension server error: " + String.vsprintf(description, details));
  793. }
  794. return status;
  795. }
  796. this.OK = makeStatus.bind(null, "OK", "OK");
  797. this.E_EXISTS = makeStatus.bind(null, "E_EXISTS", "Object already exists: %s");
  798. this.E_BADARG = makeStatus.bind(null, "E_BADARG", "Invalid argument %s: %s");
  799. this.E_BADARGTYPE = makeStatus.bind(null, "E_BADARGTYPE", "Invalid type for argument %s: got %s, expected %s");
  800. this.E_NOTFOUND = makeStatus.bind(null, "E_NOTFOUND", "Object not found: %s");
  801. this.E_NOTSUPPORTED = makeStatus.bind(null, "E_NOTSUPPORTED", "Object does not support requested operation: %s");
  802. this.E_PROTOCOLERROR = makeStatus.bind(null, "E_PROTOCOLERROR", "Inspector protocol error: %s");
  803. this.E_FAILED = makeStatus.bind(null, "E_FAILED", "Operation failed: %s");
  804. }
  805. WebInspector.addExtensions = function(extensions)
  806. {
  807. WebInspector.extensionServer._addExtensions(extensions);
  808. }
  809. WebInspector.extensionAPI = {};
  810. defineCommonExtensionSymbols(WebInspector.extensionAPI);
  811. WebInspector.extensionServer = new WebInspector.ExtensionServer();
  812. window.addExtension = function(page, name)
  813. {
  814. WebInspector.extensionServer._addExtension({
  815. startPage: page,
  816. name: name,
  817. });
  818. }