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

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/sdk/RemoteObject.js

Issue 2899163002: DevTools: Promisify Runtime domain (Closed)
Patch Set: addressing comments Created 3 years, 6 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) 2009 Google Inc. All rights reserved. 2 * Copyright (C) 2009 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 363 matching lines...) Expand 10 before | Expand all | Expand 10 after
374 throw 'Not implemented'; 374 throw 'Not implemented';
375 } 375 }
376 376
377 /** 377 /**
378 * @param {function(this:Object, ...):T} functionDeclaration 378 * @param {function(this:Object, ...):T} functionDeclaration
379 * @param {!Array<!Protocol.Runtime.CallArgument>|undefined} args 379 * @param {!Array<!Protocol.Runtime.CallArgument>|undefined} args
380 * @return {!Promise<T>} 380 * @return {!Promise<T>}
381 * @template T 381 * @template T
382 */ 382 */
383 callFunctionJSONPromise(functionDeclaration, args) { 383 callFunctionJSONPromise(functionDeclaration, args) {
384 return new Promise(promiseConstructor.bind(this)); 384 return new Promise(success => this.callFunctionJSON(functionDeclaration, arg s, success));
385
386 /**
387 * @this {SDK.RemoteObject}
388 */
389 function promiseConstructor(success) {
390 this.callFunctionJSON(functionDeclaration, args, success);
391 }
392 } 385 }
393 386
394 release() { 387 release() {
395 } 388 }
396 389
397 /** 390 /**
398 * @return {!SDK.DebuggerModel} 391 * @return {!SDK.DebuggerModel}
399 */ 392 */
400 debuggerModel() { 393 debuggerModel() {
401 throw new Error('DebuggerModel-less object'); 394 throw new Error('DebuggerModel-less object');
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
568 } 561 }
569 562
570 var args = [{value: JSON.stringify(propertyPath)}]; 563 var args = [{value: JSON.stringify(propertyPath)}];
571 this.callFunction(remoteFunction, args, callback); 564 this.callFunction(remoteFunction, args, callback);
572 } 565 }
573 566
574 /** 567 /**
575 * @param {boolean} ownProperties 568 * @param {boolean} ownProperties
576 * @param {boolean} accessorPropertiesOnly 569 * @param {boolean} accessorPropertiesOnly
577 * @param {boolean} generatePreview 570 * @param {boolean} generatePreview
578 * @param {function(?Array.<!SDK.RemoteObjectProperty>, ?Array.<!SDK.RemoteObj ectProperty>)} callback 571 * @param {function(?Array<!SDK.RemoteObjectProperty>, ?Array<!SDK.RemoteObjec tProperty>)} callback
579 */ 572 */
580 doGetProperties(ownProperties, accessorPropertiesOnly, generatePreview, callba ck) { 573 doGetProperties(ownProperties, accessorPropertiesOnly, generatePreview, callba ck) {
581 if (!this._objectId) { 574 if (!this._objectId) {
582 callback(null, null); 575 callback(null, null);
583 return; 576 return;
584 } 577 }
585 578
579 this._runtimeAgent
580 .invoke_getProperties({objectId: this._objectId, ownProperties, accessor PropertiesOnly, generatePreview})
581 .then(remoteObjectBinder.bind(this));
582
586 /** 583 /**
587 * @param {?Protocol.Error} error 584 * @param {!Protocol.RuntimeAgent.GetPropertiesResponse} response
588 * @param {!Array.<!Protocol.Runtime.PropertyDescriptor>} properties
589 * @param {!Array.<!Protocol.Runtime.InternalPropertyDescriptor>=} internalP roperties
590 * @param {?Protocol.Runtime.ExceptionDetails=} exceptionDetails
591 * @this {SDK.RemoteObjectImpl} 585 * @this {SDK.RemoteObjectImpl}
592 */ 586 */
593 function remoteObjectBinder(error, properties, internalProperties, exception Details) { 587 function remoteObjectBinder(response) {
594 if (error) { 588 if (response[Protocol.Error]) {
595 callback(null, null); 589 callback(null, null);
596 return; 590 return;
597 } 591 }
598 if (exceptionDetails) { 592 if (response.exceptionDetails) {
599 this._runtimeModel.exceptionThrown(Date.now(), exceptionDetails); 593 this._runtimeModel.exceptionThrown(Date.now(), response.exceptionDetails );
600 callback(null, null); 594 callback(null, null);
601 return; 595 return;
602 } 596 }
597 var properties = response.result;
598 var internalProperties = response.internalProperties;
603 var result = []; 599 var result = [];
604 for (var i = 0; properties && i < properties.length; ++i) { 600 for (var i = 0; properties && i < properties.length; ++i) {
605 var property = properties[i]; 601 var property = properties[i];
606 var propertyValue = property.value ? this._runtimeModel.createRemoteObje ct(property.value) : null; 602 var propertyValue = property.value ? this._runtimeModel.createRemoteObje ct(property.value) : null;
607 var propertySymbol = property.symbol ? this._runtimeModel.createRemoteOb ject(property.symbol) : null; 603 var propertySymbol = property.symbol ? this._runtimeModel.createRemoteOb ject(property.symbol) : null;
608 var remoteProperty = new SDK.RemoteObjectProperty( 604 var remoteProperty = new SDK.RemoteObjectProperty(
609 property.name, propertyValue, !!property.enumerable, !!property.writ able, !!property.isOwn, 605 property.name, propertyValue, !!property.enumerable, !!property.writ able, !!property.isOwn,
610 !!property.wasThrown, propertySymbol); 606 !!property.wasThrown, propertySymbol);
611 607
612 if (typeof property.value === 'undefined') { 608 if (typeof property.value === 'undefined') {
(...skipping 11 matching lines...) Expand all
624 for (var i = 0; i < internalProperties.length; i++) { 620 for (var i = 0; i < internalProperties.length; i++) {
625 var property = internalProperties[i]; 621 var property = internalProperties[i];
626 if (!property.value) 622 if (!property.value)
627 continue; 623 continue;
628 var propertyValue = this._runtimeModel.createRemoteObject(property.val ue); 624 var propertyValue = this._runtimeModel.createRemoteObject(property.val ue);
629 internalPropertiesResult.push(new SDK.RemoteObjectProperty(property.na me, propertyValue, true, false)); 625 internalPropertiesResult.push(new SDK.RemoteObjectProperty(property.na me, propertyValue, true, false));
630 } 626 }
631 } 627 }
632 callback(result, internalPropertiesResult); 628 callback(result, internalPropertiesResult);
633 } 629 }
634 this._runtimeAgent.getProperties(
635 this._objectId, ownProperties, accessorPropertiesOnly, generatePreview, remoteObjectBinder.bind(this));
636 } 630 }
637 631
638 /** 632 /**
639 * @override 633 * @override
640 * @param {string|!Protocol.Runtime.CallArgument} name 634 * @param {string|!Protocol.Runtime.CallArgument} name
641 * @param {string} value 635 * @param {string} value
642 * @param {function(string=)} callback 636 * @param {function(string=)} callback
643 */ 637 */
644 setPropertyValue(name, value, callback) { 638 setPropertyValue(name, value, callback) {
645 if (!this._objectId) { 639 if (!this._objectId) {
646 callback('Can\'t set a property of non-object.'); 640 callback(`Can't set a property of non-object.`);
647 return; 641 return;
648 } 642 }
649 643
650 this._runtimeAgent.invoke_evaluate({expression: value, silent: true}).then(r esponse => { 644 this._runtimeAgent.invoke_evaluate({expression: value, silent: true}).then(r esponse => {
651 if (response[Protocol.Error] || response.exceptionDetails) { 645 if (response[Protocol.Error] || response.exceptionDetails) {
652 callback( 646 callback(
653 response[Protocol.Error] || 647 response[Protocol.Error] ||
654 (response.result.type !== 'string' ? response.result.description : 648 (response.result.type !== 'string' ? response.result.description :
655 /** @type {string} */ (response .result.value))); 649 /** @type {string} */ (response .result.value)));
656 return; 650 return;
657 } 651 }
658 652
659 if (typeof name === 'string') 653 if (typeof name === 'string')
660 name = SDK.RemoteObject.toCallArgument(name); 654 name = SDK.RemoteObject.toCallArgument(name);
661 655
662 this.doSetObjectPropertyValue(response.result, name, callback); 656 this.doSetObjectPropertyValue(response.result, name).then(callback);
663 657
664 if (response.result.objectId) 658 if (response.result.objectId)
665 this._runtimeAgent.releaseObject(response.result.objectId); 659 this._runtimeAgent.releaseObject(response.result.objectId);
666 }); 660 });
667 } 661 }
668 662
669 /** 663 /**
670 * @param {!Protocol.Runtime.RemoteObject} result 664 * @param {!Protocol.Runtime.RemoteObject} result
671 * @param {!Protocol.Runtime.CallArgument} name 665 * @param {!Protocol.Runtime.CallArgument} name
672 * @param {function(string=)} callback 666 * @return {!Promise<string|undefined>}
673 */ 667 */
674 doSetObjectPropertyValue(result, name, callback) { 668 async doSetObjectPropertyValue(result, name) {
675 // This assignment may be for a regular (data) property, and for an accessor property (with getter/setter). 669 // This assignment may be for a regular (data) property, and for an accessor property (with getter/setter).
676 // Note the sensitive matter about accessor property: the property may be ph ysically defined in some proto object, 670 // Note the sensitive matter about accessor property: the property may be ph ysically defined in some proto object,
677 // but logically it is bound to the object in question. JavaScript passes th is object to getters/setters, not the object 671 // but logically it is bound to the object in question. JavaScript passes th is object to getters/setters, not the object
678 // where property was defined; so do we. 672 // where property was defined; so do we.
679 var setPropertyValueFunction = 'function(a, b) { this[a] = b; }'; 673 var setPropertyValueFunction = 'function(a, b) { this[a] = b; }';
680 674
681 var argv = [name, SDK.RemoteObject.toCallArgument(result)]; 675 var argv = [name, SDK.RemoteObject.toCallArgument(result)];
682 this._runtimeAgent.callFunctionOn( 676 var response = await this._runtimeAgent.invoke_callFunctionOn(
683 this._objectId, setPropertyValueFunction, argv, true, undefined, undefin ed, undefined, undefined, 677 {objectId: this._objectId, functionDeclaration: setPropertyValueFunction , arguments: argv, silent: true});
684 propertySetCallback); 678 var error = response[Protocol.Error];
685 679 return error || response.exceptionDetails ? error || response.result.descrip tion : undefined;
686 /**
687 * @param {?Protocol.Error} error
688 * @param {!Protocol.Runtime.RemoteObject} result
689 * @param {!Protocol.Runtime.ExceptionDetails=} exceptionDetails
690 */
691 function propertySetCallback(error, result, exceptionDetails) {
692 if (error || !!exceptionDetails) {
693 callback(error || result.description);
694 return;
695 }
696 callback();
697 }
698 } 680 }
699 681
700 /** 682 /**
701 * @override 683 * @override
702 * @param {!Protocol.Runtime.CallArgument} name 684 * @param {!Protocol.Runtime.CallArgument} name
703 * @param {function(string=)} callback 685 * @param {function(string=)} callback
704 */ 686 */
705 deleteProperty(name, callback) { 687 deleteProperty(name, callback) {
706 if (!this._objectId) { 688 if (!this._objectId) {
707 callback('Can\'t delete a property of non-object.'); 689 callback(`Can't delete a property of non-object.`);
708 return; 690 return;
709 } 691 }
710 692
711 var deletePropertyFunction = 'function(a) { delete this[a]; return !(a in th is); }'; 693 var deletePropertyFunction = 'function(a) { delete this[a]; return !(a in th is); }';
712 this._runtimeAgent.callFunctionOn( 694 this._runtimeAgent
713 this._objectId, deletePropertyFunction, [name], true, undefined, undefin ed, undefined, undefined, 695 .invoke_callFunctionOn(
714 deletePropertyCallback); 696 {objectId: this._objectId, functionDeclaration: deletePropertyFuncti on, arguments: [name], silent: true})
715 697 .then(response => {
716 /** 698 if (response[Protocol.Error] || response.exceptionDetails) {
717 * @param {?Protocol.Error} error 699 callback(response[Protocol.Error] || response.result.description);
718 * @param {!Protocol.Runtime.RemoteObject} result 700 return;
719 * @param {!Protocol.Runtime.ExceptionDetails=} exceptionDetails 701 }
720 */ 702 if (!response.result.value)
721 function deletePropertyCallback(error, result, exceptionDetails) { 703 callback('Failed to delete property.');
722 if (error || !!exceptionDetails) { 704 else
723 callback(error || result.description); 705 callback();
724 return; 706 });
725 }
726 if (!result.value)
727 callback('Failed to delete property.');
728 else
729 callback();
730 }
731 } 707 }
732 708
733 /** 709 /**
734 * @override 710 * @override
735 * @param {function(this:Object, ...)} functionDeclaration 711 * @param {function(this:Object, ...)} functionDeclaration
736 * @param {!Array.<!Protocol.Runtime.CallArgument>=} args 712 * @param {!Array.<!Protocol.Runtime.CallArgument>=} args
737 * @param {function(?SDK.RemoteObject, boolean=)=} callback 713 * @param {function(?SDK.RemoteObject, boolean=)=} callback
738 */ 714 */
739 callFunction(functionDeclaration, args, callback) { 715 callFunction(functionDeclaration, args, callback) {
740 /** 716 this._runtimeAgent
741 * @param {?Protocol.Error} error 717 .invoke_callFunctionOn({
742 * @param {!Protocol.Runtime.RemoteObject} result 718 objectId: this._objectId,
743 * @param {!Protocol.Runtime.ExceptionDetails=} exceptionDetails 719 functionDeclaration: functionDeclaration.toString(),
744 * @this {SDK.RemoteObjectImpl} 720 arguments: args,
745 */ 721 silent: true
746 function mycallback(error, result, exceptionDetails) { 722 })
747 if (!callback) 723 .then(response => {
748 return; 724 if (!callback)
749 if (error) 725 return;
750 callback(null, false); 726 if (response[Protocol.Error])
751 else 727 callback(null, false);
752 callback(this._runtimeModel.createRemoteObject(result), !!exceptionDetai ls); 728 else
753 } 729 callback(this._runtimeModel.createRemoteObject(response.result), !!r esponse.exceptionDetails);
754 730 });
755 this._runtimeAgent.callFunctionOn(
756 this._objectId, functionDeclaration.toString(), args, true, undefined, u ndefined, undefined, undefined,
757 mycallback.bind(this));
758 } 731 }
759 732
760 /** 733 /**
761 * @override 734 * @override
762 * @param {function(this:Object)} functionDeclaration 735 * @param {function(this:Object)} functionDeclaration
763 * @param {!Array.<!Protocol.Runtime.CallArgument>|undefined} args 736 * @param {!Array.<!Protocol.Runtime.CallArgument>|undefined} args
764 * @param {function(*)} callback 737 * @param {function(*)} callback
765 */ 738 */
766 callFunctionJSON(functionDeclaration, args, callback) { 739 callFunctionJSON(functionDeclaration, args, callback) {
767 /** 740 this._runtimeAgent
768 * @param {?Protocol.Error} error 741 .invoke_callFunctionOn({
769 * @param {!Protocol.Runtime.RemoteObject} result 742 objectId: this._objectId,
770 * @param {!Protocol.Runtime.ExceptionDetails=} exceptionDetails 743 functionDeclaration: functionDeclaration.toString(),
771 */ 744 arguments: args,
772 function mycallback(error, result, exceptionDetails) { 745 silent: true,
773 callback((error || !!exceptionDetails) ? null : result.value); 746 returnByValue: true
774 } 747 })
775 748 .then(
776 this._runtimeAgent.callFunctionOn( 749 response => callback(response[Protocol.Error] || response.exceptionD etails ? null : response.result.value));
777 this._objectId, functionDeclaration.toString(), args, true, true, false, undefined, undefined, mycallback);
778 } 750 }
779 751
780 /** 752 /**
781 * @override 753 * @override
782 */ 754 */
783 release() { 755 release() {
784 if (!this._objectId) 756 if (!this._objectId)
785 return; 757 return;
786 this._runtimeAgent.releaseObject(this._objectId); 758 this._runtimeAgent.releaseObject(this._objectId);
787 } 759 }
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
878 // Scope objects always fetch preview. 850 // Scope objects always fetch preview.
879 generatePreview = true; 851 generatePreview = true;
880 852
881 super.doGetProperties(ownProperties, accessorPropertiesOnly, generatePreview , wrappedCallback.bind(this)); 853 super.doGetProperties(ownProperties, accessorPropertiesOnly, generatePreview , wrappedCallback.bind(this));
882 } 854 }
883 855
884 /** 856 /**
885 * @override 857 * @override
886 * @param {!Protocol.Runtime.RemoteObject} result 858 * @param {!Protocol.Runtime.RemoteObject} result
887 * @param {!Protocol.Runtime.CallArgument} argumentName 859 * @param {!Protocol.Runtime.CallArgument} argumentName
888 * @param {function(string=)} callback 860 * @return {!Promise<string|undefined>}
889 */ 861 */
890 doSetObjectPropertyValue(result, argumentName, callback) { 862 async doSetObjectPropertyValue(result, argumentName) {
891 var name = /** @type {string} */ (argumentName.value); 863 var name = /** @type {string} */ (argumentName.value);
892 this.debuggerModel().setVariableValue( 864 this.debuggerModel().setVariableValue(
893 this._scopeRef.number, name, SDK.RemoteObject.toCallArgument(result), th is._scopeRef.callFrameId, 865 this._scopeRef.number, name, SDK.RemoteObject.toCallArgument(result), th is._scopeRef.callFrameId,
894 setVariableValueCallback.bind(this)); 866 setVariableValueCallback.bind(this));
867 var callback;
868 return new Promise(resolve => callback = resolve);
895 869
896 /** 870 /**
897 * @param {string=} error 871 * @param {string=} error
898 * @this {SDK.ScopeRemoteObject} 872 * @this {SDK.ScopeRemoteObject}
899 */ 873 */
900 function setVariableValueCallback(error) { 874 function setVariableValueCallback(error) {
901 if (error) { 875 if (error) {
902 callback(error); 876 callback(error);
903 return; 877 return;
904 } 878 }
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after
1173 * @override 1147 * @override
1174 * @return {number} 1148 * @return {number}
1175 */ 1149 */
1176 arrayLength() { 1150 arrayLength() {
1177 return Array.isArray(this._value) ? this._value.length : 0; 1151 return Array.isArray(this._value) ? this._value.length : 0;
1178 } 1152 }
1179 1153
1180 /** 1154 /**
1181 * @override 1155 * @override
1182 * @param {function(this:Object, ...)} functionDeclaration 1156 * @param {function(this:Object, ...)} functionDeclaration
1183 * @param {!Array.<!Protocol.Runtime.CallArgument>=} args 1157 * @param {!Array<!Protocol.Runtime.CallArgument>=} args
1184 * @param {function(?SDK.RemoteObject, boolean=)=} callback 1158 * @param {function(?SDK.RemoteObject, boolean=)=} callback
1185 */ 1159 */
1186 callFunction(functionDeclaration, args, callback) { 1160 callFunction(functionDeclaration, args, callback) {
1187 var target = /** @type {?Object} */ (this._value); 1161 var target = /** @type {?Object} */ (this._value);
1188 var rawArgs = args ? args.map(function(arg) { 1162 var rawArgs = args ? args.map(arg => arg.value) : [];
1189 return arg.value;
1190 }) :
1191 [];
1192 1163
1193 var result; 1164 var result;
1194 var wasThrown = false; 1165 var wasThrown = false;
1195 try { 1166 try {
1196 result = functionDeclaration.apply(target, rawArgs); 1167 result = functionDeclaration.apply(target, rawArgs);
1197 } catch (e) { 1168 } catch (e) {
1198 wasThrown = true; 1169 wasThrown = true;
1199 } 1170 }
1200 1171
1201 if (!callback) 1172 if (!callback)
1202 return; 1173 return;
1203 callback(SDK.RemoteObject.fromLocalObject(result), wasThrown); 1174 callback(SDK.RemoteObject.fromLocalObject(result), wasThrown);
1204 } 1175 }
1205 1176
1206 /** 1177 /**
1207 * @override 1178 * @override
1208 * @param {function(this:Object)} functionDeclaration 1179 * @param {function(this:Object)} functionDeclaration
1209 * @param {!Array.<!Protocol.Runtime.CallArgument>|undefined} args 1180 * @param {!Array<!Protocol.Runtime.CallArgument>|undefined} args
1210 * @param {function(*)} callback 1181 * @param {function(*)} callback
1211 */ 1182 */
1212 callFunctionJSON(functionDeclaration, args, callback) { 1183 callFunctionJSON(functionDeclaration, args, callback) {
1213 var target = /** @type {?Object} */ (this._value); 1184 var target = /** @type {?Object} */ (this._value);
1214 var rawArgs = args ? args.map(function(arg) { 1185 var rawArgs = args ? args.map(arg => arg.value) : [];
1215 return arg.value;
1216 }) :
1217 [];
1218 1186
1219 var result; 1187 var result;
1220 try { 1188 try {
1221 result = functionDeclaration.apply(target, rawArgs); 1189 result = functionDeclaration.apply(target, rawArgs);
1222 } catch (e) { 1190 } catch (e) {
1223 result = null; 1191 result = null;
1224 } 1192 }
1225 1193
1226 callback(result); 1194 callback(result);
1227 } 1195 }
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after
1418 * @const 1386 * @const
1419 * @type {!RegExp} 1387 * @type {!RegExp}
1420 */ 1388 */
1421 SDK.RemoteObject._descriptionLengthParenRegex = /\(([0-9]+)\)/; 1389 SDK.RemoteObject._descriptionLengthParenRegex = /\(([0-9]+)\)/;
1422 1390
1423 /** 1391 /**
1424 * @const 1392 * @const
1425 * @type {!RegExp} 1393 * @type {!RegExp}
1426 */ 1394 */
1427 SDK.RemoteObject._descriptionLengthSquareRegex = /\[([0-9]+)\]/; 1395 SDK.RemoteObject._descriptionLengthSquareRegex = /\[([0-9]+)\]/;
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698