Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(855)

Side by Side Diff: third_party/WebKit/Source/platform/v8_inspector/InjectedScriptSource.js

Issue 1917733002: [DevTools] Move part of CommandLineAPI to native (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2007 Apple Inc. All rights reserved. 2 * Copyright (C) 2007 Apple Inc. All rights reserved.
3 * Copyright (C) 2013 Google Inc. All rights reserved. 3 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
7 * are met: 7 * are met:
8 * 8 *
9 * 1. Redistributions of source code must retain the above copyright 9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer. 10 * notice, this list of conditions and the following disclaimer.
(...skipping 589 matching lines...) Expand 10 before | Expand all | Expand 10 after
600 } 600 }
601 601
602 for (var i = 0; i < jsonMLObject.length; ++i) 602 for (var i = 0; i < jsonMLObject.length; ++i)
603 this._substituteObjectTagsInCustomPreview(objectGroupName, jsonM LObject[i]); 603 this._substituteObjectTagsInCustomPreview(objectGroupName, jsonM LObject[i]);
604 } finally { 604 } finally {
605 this._customPreviewRecursionDepth--; 605 this._customPreviewRecursionDepth--;
606 } 606 }
607 }, 607 },
608 608
609 /** 609 /**
610 * @return {!CommandLineAPI} 610 * @param {!Object} nativeCommandLineAPI
611 * @return {!Object}
611 */ 612 */
612 commandLineAPI: function() 613 installCommandLineAPI: function(nativeCommandLineAPI)
613 { 614 {
614 return new CommandLineAPI(this._commandLineAPIImpl); 615 // NOTE: This list contains only not native Command Line API methods. Fo r full list: V8Console.
616 // NOTE: Argument names of these methods will be printed in the console, so use pretty names!
617 var members = [ "$", "$$", "$x", "monitorEvents", "unmonitorEvents", "in spect", "copy", "getEventListeners" ];
618 var commandLineAPIImpl = this._commandLineAPIImpl;
619 for (var member of members)
620 nativeCommandLineAPI[member] = bind(commandLineAPIImpl[member], comm andLineAPIImpl);
621 for (var i = 0; i < 5; ++i) {
622 var member = "$" + i;
623 nativeCommandLineAPI[member] = bind(commandLineAPIImpl._inspectedObj ect, commandLineAPIImpl, i);
624 }
625 var functionToStringMap = new Map([
626 ["$", "function $(selector, [startNode]) { [Command Line AP I] }"],
627 ["$$", "function $$(selector, [startNode]) { [Command Line A PI] }"],
628 ["$x", "function $x(xpath, [startNode]) { [Command Line API] }"],
629 ["dir", "function dir(value) { [Command Line API] }"],
630 ["dirxml", "function dirxml(value) { [Command Line API] }"],
631 ["keys", "function keys(object) { [Command Line API] }"],
632 ["values", "function values(object) { [Command Line API] }"],
633 ["inspect", "function inspect(object) { [Command Line API] }"],
634 ["copy", "function copy(value) { [Command Line API] }"],
635 ["clear", "function clear() { [Command Line API] }"],
636 ["debug", "function debug(function) { [Command Line API] }"],
637 ["undebug", "function undebug(function) { [Command Line API] }"],
638 ["monitor", "function monitor(function) { [Command Line API] }"],
639 ["unmonitor", "function unmonitor(function) { [Command Line API] }" ],
640 ["table", "function table(data, [columns]) { [Command Line API] }"],
641 ["profile", "function profile(title) { [Command Line API] }"],
642 ["profileEnd", "function profileEnd(title) { [Command Line API] }"],
643 ["monitorEvents", "function monitorEvents(object, [types]) { [Comm and Line API] }"],
644 ["unmonitorEvents", "function unmonitorEvents(object, [types]) { [Co mmand Line API] }"],
645 ["getEventListeners", "function getEventListeners(node) { [Command L ine API] }"]
646 ]);
647 for (let entry of functionToStringMap)
648 nativeCommandLineAPI[entry[0]].toString = (() => entry[1]);
649 return nativeCommandLineAPI;
615 }, 650 },
616 651
617 /** 652 /**
618 * @param {string} objectGroup 653 * @param {string} objectGroup
619 * @return {!Object} 654 * @return {!Object}
620 */ 655 */
621 remoteObjectAPI: function(objectGroup) 656 remoteObjectAPI: function(objectGroup)
622 { 657 {
623 /** 658 /**
624 * @suppressReceiverCheck 659 * @suppressReceiverCheck
(...skipping 504 matching lines...) Expand 10 before | Expand all | Expand 10 after
1129 return string.substr(0, leftHalf) + "\u2026" + string.substr(string. length - rightHalf, rightHalf); 1164 return string.substr(0, leftHalf) + "\u2026" + string.substr(string. length - rightHalf, rightHalf);
1130 } 1165 }
1131 return string.substr(0, maxLength) + "\u2026"; 1166 return string.substr(0, maxLength) + "\u2026";
1132 }, 1167 },
1133 1168
1134 __proto__: null 1169 __proto__: null
1135 } 1170 }
1136 1171
1137 /** 1172 /**
1138 * @constructor 1173 * @constructor
1139 * @param {!CommandLineAPIImpl} commandLineAPIImpl
1140 */
1141 function CommandLineAPI(commandLineAPIImpl)
1142 {
1143 /**
1144 * @param {string} name The name of the method for which a toString method s hould be generated.
1145 * @return {function():string}
1146 */
1147 function customToStringMethod(name)
1148 {
1149 return function()
1150 {
1151 var funcArgsSyntax = "";
1152 try {
1153 var funcSyntax = "" + commandLineAPIImpl[name];
1154 funcSyntax = funcSyntax.replace(/\n/g, " ");
1155 funcSyntax = funcSyntax.replace(/^function[^\(]*\(([^\)]*)\).*$/ , "$1");
1156 funcSyntax = funcSyntax.replace(/\s*,\s*/g, ", ");
1157 funcSyntax = funcSyntax.replace(/\bopt_(\w+)\b/g, "[$1]");
1158 funcArgsSyntax = funcSyntax.trim();
1159 } catch (e) {
1160 }
1161 return "function " + name + "(" + funcArgsSyntax + ") { [Command Lin e API] }";
1162 };
1163 }
1164
1165 for (var i = 0; i < CommandLineAPI.members_.length; ++i) {
1166 var member = CommandLineAPI.members_[i];
1167 this[member] = bind(commandLineAPIImpl[member], commandLineAPIImpl);
1168 this[member].toString = customToStringMethod(member);
1169 }
1170
1171 for (var i = 0; i < 5; ++i) {
1172 var member = "$" + i;
1173 this[member] = bind(commandLineAPIImpl._inspectedObject, commandLineAPII mpl, i);
1174 }
1175 this.__proto__ = null;
1176 }
1177
1178 // NOTE: Please keep the list of API methods below synchronized to that in WebIn spector.RuntimeModel
1179 // and V8InjectedScriptHost!
1180 // NOTE: Argument names of these methods will be printed in the console, so use pretty names!
1181 /**
1182 * @type {!Array.<string>}
1183 * @const
1184 */
1185 CommandLineAPI.members_ = [
1186 "$", "$$", "$x", "dir", "dirxml", "keys", "values", "profile", "profileEnd",
1187 "monitorEvents", "unmonitorEvents", "inspect", "copy", "clear", "getEventLis teners",
1188 "debug", "undebug", "monitor", "unmonitor", "table"
1189 ];
1190
1191 /**
1192 * @constructor
1193 */ 1174 */
1194 function CommandLineAPIImpl() 1175 function CommandLineAPIImpl()
1195 { 1176 {
1196 } 1177 }
1197 1178
1198 CommandLineAPIImpl.prototype = { 1179 CommandLineAPIImpl.prototype = {
1199 /** 1180 /**
1200 * @param {string} selector 1181 * @param {string} selector
1201 * @param {!Node=} opt_startNode 1182 * @param {!Node=} opt_startNode
1202 * @return {*} 1183 * @return {*}
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
1249 default: 1230 default:
1250 var nodes = []; 1231 var nodes = [];
1251 var node; 1232 var node;
1252 while (node = result.iterateNext()) 1233 while (node = result.iterateNext())
1253 push(nodes, node); 1234 push(nodes, node);
1254 return nodes; 1235 return nodes;
1255 } 1236 }
1256 }, 1237 },
1257 1238
1258 /** 1239 /**
1259 * @return {*}
1260 */
1261 dir: function(var_args)
1262 {
1263 return InjectedScriptHost.callFunction(inspectedGlobalObject.console.dir , inspectedGlobalObject.console, slice(arguments));
1264 },
1265
1266 /**
1267 * @return {*}
1268 */
1269 dirxml: function(var_args)
1270 {
1271 return InjectedScriptHost.callFunction(inspectedGlobalObject.console.dir xml, inspectedGlobalObject.console, slice(arguments));
1272 },
1273
1274 /**
1275 * @return {!Array.<string>}
1276 */
1277 keys: function(object)
1278 {
1279 return Object.keys(object);
1280 },
1281
1282 /**
1283 * @return {!Array.<*>}
1284 */
1285 values: function(object)
1286 {
1287 var result = [];
1288 for (var key in object)
1289 push(result, object[key]);
1290 return result;
1291 },
1292
1293 /**
1294 * @return {*}
1295 */
1296 profile: function(opt_title)
1297 {
1298 return InjectedScriptHost.callFunction(inspectedGlobalObject.console.pro file, inspectedGlobalObject.console, slice(arguments));
1299 },
1300
1301 /**
1302 * @return {*}
1303 */
1304 profileEnd: function(opt_title)
1305 {
1306 return InjectedScriptHost.callFunction(inspectedGlobalObject.console.pro fileEnd, inspectedGlobalObject.console, slice(arguments));
1307 },
1308
1309 /**
1310 * @param {!Object} object 1240 * @param {!Object} object
1311 * @param {!Array.<string>|string=} opt_types 1241 * @param {!Array.<string>|string=} opt_types
1312 */ 1242 */
1313 monitorEvents: function(object, opt_types) 1243 monitorEvents: function(object, opt_types)
1314 { 1244 {
1315 if (!object || !object.addEventListener || !object.removeEventListener) 1245 if (!object || !object.addEventListener || !object.removeEventListener)
1316 return; 1246 return;
1317 var types = this._normalizeEventTypes(opt_types); 1247 var types = this._normalizeEventTypes(opt_types);
1318 for (var i = 0; i < types.length; ++i) { 1248 for (var i = 0; i < types.length; ++i) {
1319 object.removeEventListener(types[i], this._logEvent, false); 1249 object.removeEventListener(types[i], this._logEvent, false);
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
1356 } catch (e) { 1286 } catch (e) {
1357 string = toString(object); 1287 string = toString(object);
1358 } 1288 }
1359 } 1289 }
1360 1290
1361 var hints = { copyToClipboard: true, __proto__: null }; 1291 var hints = { copyToClipboard: true, __proto__: null };
1362 var remoteObject = injectedScript._wrapObject(string, "") 1292 var remoteObject = injectedScript._wrapObject(string, "")
1363 InjectedScriptHost.inspect(remoteObject, hints); 1293 InjectedScriptHost.inspect(remoteObject, hints);
1364 }, 1294 },
1365 1295
1366 clear: function()
1367 {
1368 InjectedScriptHost.clearConsoleMessages();
1369 },
1370
1371 /** 1296 /**
1372 * @param {!Node} node 1297 * @param {!Node} node
1373 * @return {!Object|undefined} 1298 * @return {!Object|undefined}
1374 */ 1299 */
1375 getEventListeners: function(node) 1300 getEventListeners: function(node)
1376 { 1301 {
1377 var result = nullifyObjectProto(InjectedScriptHost.getEventListeners(nod e)); 1302 var result = nullifyObjectProto(InjectedScriptHost.getEventListeners(nod e));
1378 if (!result) 1303 if (!result)
1379 return; 1304 return;
1380 1305
(...skipping 28 matching lines...) Expand all
1409 for (var type in result) { 1334 for (var type in result) {
1410 var listeners = result[type]; 1335 var listeners = result[type];
1411 for (var i = 0, listener; listener = listeners[i]; ++i) { 1336 for (var i = 0, listener; listener = listeners[i]; ++i) {
1412 listener["type"] = type; 1337 listener["type"] = type;
1413 listener["remove"] = removeFunc; 1338 listener["remove"] = removeFunc;
1414 } 1339 }
1415 } 1340 }
1416 return result; 1341 return result;
1417 }, 1342 },
1418 1343
1419 debug: function(fn)
1420 {
1421 InjectedScriptHost.debugFunction(fn);
1422 },
1423
1424 undebug: function(fn)
1425 {
1426 InjectedScriptHost.undebugFunction(fn);
1427 },
1428
1429 monitor: function(fn)
1430 {
1431 InjectedScriptHost.monitorFunction(fn);
1432 },
1433
1434 unmonitor: function(fn)
1435 {
1436 InjectedScriptHost.unmonitorFunction(fn);
1437 },
1438
1439 table: function(data, opt_columns)
1440 {
1441 InjectedScriptHost.callFunction(inspectedGlobalObject.console.table, ins pectedGlobalObject.console, slice(arguments));
1442 },
1443
1444 /** 1344 /**
1445 * @param {number} num 1345 * @param {number} num
1446 */ 1346 */
1447 _inspectedObject: function(num) 1347 _inspectedObject: function(num)
1448 { 1348 {
1449 return InjectedScriptHost.inspectedObject(num); 1349 return InjectedScriptHost.inspectedObject(num);
1450 }, 1350 },
1451 1351
1452 /** 1352 /**
1453 * @param {!Array.<string>|string=} types 1353 * @param {!Array.<string>|string=} types
(...skipping 29 matching lines...) Expand all
1483 */ 1383 */
1484 _logEvent: function(event) 1384 _logEvent: function(event)
1485 { 1385 {
1486 inspectedGlobalObject.console.log(event.type, event); 1386 inspectedGlobalObject.console.log(event.type, event);
1487 } 1387 }
1488 } 1388 }
1489 1389
1490 injectedScript._commandLineAPIImpl = new CommandLineAPIImpl(); 1390 injectedScript._commandLineAPIImpl = new CommandLineAPIImpl();
1491 return injectedScript; 1391 return injectedScript;
1492 }) 1392 })
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698