webconsole.js (3062B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 const { 8 FrontClassWithSpec, 9 registerFront, 10 } = require("resource://devtools/shared/protocol.js"); 11 const { 12 webconsoleSpec, 13 } = require("resource://devtools/shared/specs/webconsole.js"); 14 const { 15 getAdHocFrontOrPrimitiveGrip, 16 } = require("resource://devtools/client/fronts/object.js"); 17 18 /** 19 * A WebConsoleFront is used as a front end for the WebConsoleActor that is 20 * created on the server, hiding implementation details. 21 * 22 * @param object client 23 * The DevToolsClient instance we live for. 24 */ 25 class WebConsoleFront extends FrontClassWithSpec(webconsoleSpec) { 26 constructor(client, targetFront, parentFront) { 27 super(client, targetFront, parentFront); 28 this._client = client; 29 this.events = []; 30 31 // Attribute name from which to retrieve the actorID out of the target actor's form 32 this.formAttributeName = "consoleActor"; 33 34 this.before("consoleAPICall", this.beforeConsoleAPICall); 35 this.before("pageError", this.beforePageError); 36 } 37 38 get actor() { 39 return this.actorID; 40 } 41 42 beforeConsoleAPICall(packet) { 43 if (packet.message && Array.isArray(packet.message.arguments)) { 44 // We might need to create fronts for each of the message arguments. 45 packet.message.arguments = packet.message.arguments.map(arg => 46 getAdHocFrontOrPrimitiveGrip(arg, this) 47 ); 48 } 49 return packet; 50 } 51 52 beforePageError(packet) { 53 if (packet?.pageError?.errorMessage) { 54 packet.pageError.errorMessage = getAdHocFrontOrPrimitiveGrip( 55 packet.pageError.errorMessage, 56 this 57 ); 58 } 59 60 if (packet?.pageError?.exception) { 61 packet.pageError.exception = getAdHocFrontOrPrimitiveGrip( 62 packet.pageError.exception, 63 this 64 ); 65 } 66 return packet; 67 } 68 69 async getCachedMessages(messageTypes) { 70 const response = await super.getCachedMessages(messageTypes); 71 if (Array.isArray(response.messages)) { 72 response.messages = response.messages.map(packet => { 73 if (Array.isArray(packet?.message?.arguments)) { 74 // We might need to create fronts for each of the message arguments. 75 packet.message.arguments = packet.message.arguments.map(arg => 76 getAdHocFrontOrPrimitiveGrip(arg, this) 77 ); 78 } 79 80 if (packet.pageError?.exception) { 81 packet.pageError.exception = getAdHocFrontOrPrimitiveGrip( 82 packet.pageError.exception, 83 this 84 ); 85 } 86 87 return packet; 88 }); 89 } 90 return response; 91 } 92 93 /** 94 * Close the WebConsoleFront. 95 * 96 */ 97 destroy() { 98 if (!this._client) { 99 return null; 100 } 101 102 // This will make future calls to this function harmless because of the early return 103 // at the top of the function. 104 this._client = null; 105 106 return super.destroy(); 107 } 108 } 109 110 registerFront(WebConsoleFront);