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

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

Issue 2851913002: [DevTools] Do not expose agents on Target
Patch Set: storage and tests.js Created 3 years, 7 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) 2011 Google Inc. All rights reserved. 2 * Copyright (C) 2011 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 16 matching lines...) Expand all
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30
31 /** 31 /**
32 * @unrestricted 32 * @unrestricted
33 */ 33 */
34 SDK.NetworkManager = class extends SDK.SDKModel { 34 SDK.NetworkManager = class extends SDK.SDKModel {
35 /** 35 /**
36 * @param {!SDK.Target} target 36 * @param {!SDK.Target} target
37 * @param {!Protocol.Dispatcher} dispatcher
37 */ 38 */
38 constructor(target) { 39 constructor(target, dispatcher) {
39 super(target); 40 super(target, dispatcher);
40 this._dispatcher = new SDK.NetworkDispatcher(this); 41 this._dispatcher = new SDK.NetworkDispatcher(this);
41 this._networkAgent = target.networkAgent(); 42 this._networkAgent = dispatcher.networkAgent();
42 target.registerNetworkDispatcher(this._dispatcher); 43 dispatcher.registerNetworkDispatcher(this._dispatcher);
43 if (Common.moduleSetting('cacheDisabled').get()) 44 if (Common.moduleSetting('cacheDisabled').get())
44 this._networkAgent.setCacheDisabled(true); 45 this._networkAgent.setCacheDisabled(true);
45 46
46 // Limit buffer when talking to a remote device. 47 // Limit buffer when talking to a remote device.
47 if (Runtime.queryParam('remoteFrontend') || Runtime.queryParam('ws')) 48 if (Runtime.queryParam('remoteFrontend') || Runtime.queryParam('ws'))
48 this._networkAgent.enable(10000000, 5000000); 49 this._networkAgent.enable(10000000, 5000000);
49 else 50 else
50 this._networkAgent.enable(); 51 this._networkAgent.enable();
51 52
52 this._bypassServiceWorkerSetting = Common.settings.createSetting('bypassServ iceWorker', false); 53 this._bypassServiceWorkerSetting = Common.settings.createSetting('bypassServ iceWorker', false);
(...skipping 397 matching lines...) Expand 10 before | Expand all | Expand 10 after
450 this._finishNetworkRequest(networkRequest, time, -1); 451 this._finishNetworkRequest(networkRequest, time, -1);
451 } 452 }
452 453
453 /** 454 /**
454 * @override 455 * @override
455 * @param {!Protocol.Network.RequestId} requestId 456 * @param {!Protocol.Network.RequestId} requestId
456 * @param {string} requestURL 457 * @param {string} requestURL
457 * @param {!Protocol.Network.Initiator=} initiator 458 * @param {!Protocol.Network.Initiator=} initiator
458 */ 459 */
459 webSocketCreated(requestId, requestURL, initiator) { 460 webSocketCreated(requestId, requestURL, initiator) {
460 var networkRequest = new SDK.NetworkRequest(this._manager, requestId, reques tURL, '', '', '', initiator || null); 461 var networkRequest = new SDK.NetworkRequest(
462 this._manager, this._manager._networkAgent, requestId, requestURL, '', ' ', '', initiator || null);
461 networkRequest.setResourceType(Common.resourceTypes.WebSocket); 463 networkRequest.setResourceType(Common.resourceTypes.WebSocket);
462 this._startNetworkRequest(networkRequest); 464 this._startNetworkRequest(networkRequest);
463 } 465 }
464 466
465 /** 467 /**
466 * @override 468 * @override
467 * @param {!Protocol.Network.RequestId} requestId 469 * @param {!Protocol.Network.RequestId} requestId
468 * @param {!Protocol.Network.Timestamp} time 470 * @param {!Protocol.Network.Timestamp} time
469 * @param {!Protocol.Network.Timestamp} wallTime 471 * @param {!Protocol.Network.Timestamp} wallTime
470 * @param {!Protocol.Network.WebSocketRequest} request 472 * @param {!Protocol.Network.WebSocketRequest} request
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
649 651
650 /** 652 /**
651 * @param {!Protocol.Network.RequestId} requestId 653 * @param {!Protocol.Network.RequestId} requestId
652 * @param {string} frameId 654 * @param {string} frameId
653 * @param {!Protocol.Network.LoaderId} loaderId 655 * @param {!Protocol.Network.LoaderId} loaderId
654 * @param {string} url 656 * @param {string} url
655 * @param {string} documentURL 657 * @param {string} documentURL
656 * @param {?Protocol.Network.Initiator} initiator 658 * @param {?Protocol.Network.Initiator} initiator
657 */ 659 */
658 _createNetworkRequest(requestId, frameId, loaderId, url, documentURL, initiato r) { 660 _createNetworkRequest(requestId, frameId, loaderId, url, documentURL, initiato r) {
659 return new SDK.NetworkRequest(this._manager, requestId, url, documentURL, fr ameId, loaderId, initiator); 661 return new SDK.NetworkRequest(
662 this._manager, this._manager._networkAgent, requestId, url, documentURL, frameId, loaderId, initiator);
660 } 663 }
661 }; 664 };
662 665
663 /** 666 /**
664 * @implements {SDK.TargetManager.Observer} 667 * @implements {SDK.SDKModelObserver<!SDK.NetworkManager>}
665 * @unrestricted 668 * @unrestricted
666 */ 669 */
667 SDK.MultitargetNetworkManager = class extends Common.Object { 670 SDK.MultitargetNetworkManager = class extends Common.Object {
668 constructor() { 671 constructor() {
669 super(); 672 super();
670 this._userAgentOverride = ''; 673 this._userAgentOverride = '';
671 /** @type {!Set<!Protocol.NetworkAgent>} */
672 this._agents = new Set();
673 /** @type {!SDK.NetworkManager.Conditions} */ 674 /** @type {!SDK.NetworkManager.Conditions} */
674 this._networkConditions = SDK.NetworkManager.NoThrottlingConditions; 675 this._networkConditions = SDK.NetworkManager.NoThrottlingConditions;
675 676
676 this._blockingEnabledSetting = Common.moduleSetting('requestBlockingEnabled' ); 677 this._blockingEnabledSetting = Common.moduleSetting('requestBlockingEnabled' );
677 this._blockedPatternsSetting = Common.settings.createSetting('networkBlocked Patterns', []); 678 this._blockedPatternsSetting = Common.settings.createSetting('networkBlocked Patterns', []);
678 this._effectiveBlockedURLs = []; 679 this._effectiveBlockedURLs = [];
679 this._updateBlockedPatterns(); 680 this._updateBlockedPatterns();
680 681
681 SDK.targetManager.observeTargets(this, SDK.Target.Capability.Network); 682 SDK.targetManager.observeModels(SDK.NetworkManager, this);
682 } 683 }
683 684
684 /** 685 /**
685 * @param {string} uaString 686 * @param {string} uaString
686 * @return {string} 687 * @return {string}
687 */ 688 */
688 static patchUserAgentWithChromeVersion(uaString) { 689 static patchUserAgentWithChromeVersion(uaString) {
689 // Patches Chrome/CriOS version from user agent ("1.2.3.4" when user agent i s: "Chrome/1.2.3.4"). 690 // Patches Chrome/CriOS version from user agent ("1.2.3.4" when user agent i s: "Chrome/1.2.3.4").
690 var chromeRegex = new RegExp('(?:^|\\W)Chrome/(\\S+)'); 691 var chromeRegex = new RegExp('(?:^|\\W)Chrome/(\\S+)');
691 var chromeMatch = navigator.userAgent.match(chromeRegex); 692 var chromeMatch = navigator.userAgent.match(chromeRegex);
692 if (chromeMatch && chromeMatch.length > 1) 693 if (chromeMatch && chromeMatch.length > 1)
693 return String.sprintf(uaString, chromeMatch[1]); 694 return String.sprintf(uaString, chromeMatch[1]);
694 return uaString; 695 return uaString;
695 } 696 }
696 697
697 /** 698 /**
698 * @override 699 * @override
699 * @param {!SDK.Target} target 700 * @param {!SDK.NetworkManager} networkManager
700 */ 701 */
701 targetAdded(target) { 702 modelAdded(networkManager) {
702 var networkAgent = target.networkAgent(); 703 var networkAgent = networkManager._networkAgent;
703 if (this._extraHeaders) 704 if (this._extraHeaders)
704 networkAgent.setExtraHTTPHeaders(this._extraHeaders); 705 networkAgent.setExtraHTTPHeaders(this._extraHeaders);
705 if (this._currentUserAgent()) 706 if (this._currentUserAgent())
706 networkAgent.setUserAgentOverride(this._currentUserAgent()); 707 networkAgent.setUserAgentOverride(this._currentUserAgent());
707 if (this._effectiveBlockedURLs.length) 708 if (this._effectiveBlockedURLs.length)
708 networkAgent.setBlockedURLs(this._effectiveBlockedURLs); 709 networkAgent.setBlockedURLs(this._effectiveBlockedURLs);
709 this._agents.add(networkAgent);
710 if (this.isThrottling()) 710 if (this.isThrottling())
711 this._updateNetworkConditions(networkAgent); 711 this._updateNetworkConditions(networkManager);
712 } 712 }
713 713
714 /** 714 /**
715 * @override 715 * @override
716 * @param {!SDK.Target} target 716 * @param {!SDK.NetworkManager} networkManager
717 */ 717 */
718 targetRemoved(target) { 718 modelRemoved(networkManager) {
719 this._agents.delete(target.networkAgent());
720 } 719 }
721 720
722 /** 721 /**
723 * @return {boolean} 722 * @return {boolean}
724 */ 723 */
725 isThrottling() { 724 isThrottling() {
726 return this._networkConditions.download >= 0 || this._networkConditions.uplo ad >= 0 || 725 return this._networkConditions.download >= 0 || this._networkConditions.uplo ad >= 0 ||
727 this._networkConditions.latency > 0; 726 this._networkConditions.latency > 0;
728 } 727 }
729 728
730 /** 729 /**
731 * @return {boolean} 730 * @return {boolean}
732 */ 731 */
733 isOffline() { 732 isOffline() {
734 return !this._networkConditions.download && !this._networkConditions.upload; 733 return !this._networkConditions.download && !this._networkConditions.upload;
735 } 734 }
736 735
737 /** 736 /**
738 * @param {!SDK.NetworkManager.Conditions} conditions 737 * @param {!SDK.NetworkManager.Conditions} conditions
739 */ 738 */
740 setNetworkConditions(conditions) { 739 setNetworkConditions(conditions) {
741 this._networkConditions = conditions; 740 this._networkConditions = conditions;
742 for (var agent of this._agents) 741 for (var networkManager of SDK.targetManager.models(SDK.NetworkManager))
743 this._updateNetworkConditions(agent); 742 this._updateNetworkConditions(networkManager);
744 this.dispatchEventToListeners(SDK.MultitargetNetworkManager.Events.Condition sChanged); 743 this.dispatchEventToListeners(SDK.MultitargetNetworkManager.Events.Condition sChanged);
745 } 744 }
746 745
747 /** 746 /**
748 * @return {!SDK.NetworkManager.Conditions} 747 * @return {!SDK.NetworkManager.Conditions}
749 */ 748 */
750 networkConditions() { 749 networkConditions() {
751 return this._networkConditions; 750 return this._networkConditions;
752 } 751 }
753 752
754 /** 753 /**
755 * @param {!Protocol.NetworkAgent} networkAgent 754 * @param {!SDK.NetworkManager} networkManager
756 */ 755 */
757 _updateNetworkConditions(networkAgent) { 756 _updateNetworkConditions(networkManager) {
758 var conditions = this._networkConditions; 757 var conditions = this._networkConditions;
759 if (!this.isThrottling()) { 758 if (!this.isThrottling()) {
760 networkAgent.emulateNetworkConditions(false, 0, 0, 0); 759 networkManager._networkAgent.emulateNetworkConditions(false, 0, 0, 0);
761 } else { 760 } else {
762 networkAgent.emulateNetworkConditions( 761 networkManager._networkAgent.emulateNetworkConditions(
763 this.isOffline(), conditions.latency, conditions.download < 0 ? 0 : co nditions.download, 762 this.isOffline(), conditions.latency, conditions.download < 0 ? 0 : co nditions.download,
764 conditions.upload < 0 ? 0 : conditions.upload, SDK.NetworkManager._con nectionType(conditions)); 763 conditions.upload < 0 ? 0 : conditions.upload, SDK.NetworkManager._con nectionType(conditions));
765 } 764 }
766 } 765 }
767 766
768 /** 767 /**
769 * @param {!Protocol.Network.Headers} headers 768 * @param {!Protocol.Network.Headers} headers
770 */ 769 */
771 setExtraHTTPHeaders(headers) { 770 setExtraHTTPHeaders(headers) {
772 this._extraHeaders = headers; 771 this._extraHeaders = headers;
773 for (var agent of this._agents) 772 for (var networkManager of SDK.targetManager.models(SDK.NetworkManager))
774 agent.setExtraHTTPHeaders(this._extraHeaders); 773 networkManager._networkAgent.setExtraHTTPHeaders(this._extraHeaders);
775 } 774 }
776 775
777 /** 776 /**
778 * @return {string} 777 * @return {string}
779 */ 778 */
780 _currentUserAgent() { 779 _currentUserAgent() {
781 return this._customUserAgent ? this._customUserAgent : this._userAgentOverri de; 780 return this._customUserAgent ? this._customUserAgent : this._userAgentOverri de;
782 } 781 }
783 782
784 _updateUserAgentOverride() { 783 _updateUserAgentOverride() {
785 var userAgent = this._currentUserAgent(); 784 var userAgent = this._currentUserAgent();
786 Host.ResourceLoader.targetUserAgent = userAgent; 785 Host.ResourceLoader.targetUserAgent = userAgent;
787 for (var agent of this._agents) 786 for (var networkManager of SDK.targetManager.models(SDK.NetworkManager))
788 agent.setUserAgentOverride(userAgent); 787 networkManager._networkAgent.setUserAgentOverride(userAgent);
789 } 788 }
790 789
791 /** 790 /**
792 * @param {string} userAgent 791 * @param {string} userAgent
793 */ 792 */
794 setUserAgentOverride(userAgent) { 793 setUserAgentOverride(userAgent) {
795 if (this._userAgentOverride === userAgent) 794 if (this._userAgentOverride === userAgent)
796 return; 795 return;
797 this._userAgentOverride = userAgent; 796 this._userAgentOverride = userAgent;
798 if (!this._customUserAgent) 797 if (!this._customUserAgent)
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
861 if (this._blockingEnabledSetting.get()) { 860 if (this._blockingEnabledSetting.get()) {
862 for (var pattern of this._blockedPatternsSetting.get()) { 861 for (var pattern of this._blockedPatternsSetting.get()) {
863 if (pattern.enabled) 862 if (pattern.enabled)
864 urls.push(pattern.url); 863 urls.push(pattern.url);
865 } 864 }
866 } 865 }
867 866
868 if (!urls.length && !this._effectiveBlockedURLs.length) 867 if (!urls.length && !this._effectiveBlockedURLs.length)
869 return; 868 return;
870 this._effectiveBlockedURLs = urls; 869 this._effectiveBlockedURLs = urls;
871 for (var agent of this._agents) 870 for (var networkManager of SDK.targetManager.models(SDK.NetworkManager))
872 agent.setBlockedURLs(this._effectiveBlockedURLs); 871 networkManager._networkAgent.setBlockedURLs(this._effectiveBlockedURLs);
873 } 872 }
874 873
875 clearBrowserCache() { 874 clearBrowserCache() {
876 for (var agent of this._agents) 875 for (var networkManager of SDK.targetManager.models(SDK.NetworkManager))
877 agent.clearBrowserCache(); 876 networkManager._networkAgent.clearBrowserCache();
878 } 877 }
879 878
880 clearBrowserCookies() { 879 clearBrowserCookies() {
881 for (var agent of this._agents) 880 for (var networkManager of SDK.targetManager.models(SDK.NetworkManager))
882 agent.clearBrowserCookies(); 881 networkManager._networkAgent.clearBrowserCookies();
883 } 882 }
884 883
885 /** 884 /**
886 * @param {string} origin 885 * @param {string} origin
887 * @param {function(!Array<string>)} callback 886 * @param {function(!Array<string>)} callback
888 */ 887 */
889 getCertificate(origin, callback) { 888 getCertificate(origin, callback) {
890 var target = SDK.targetManager.mainTarget(); 889 var networkManager = SDK.targetManager.models(SDK.NetworkManager)[0];
891 target.networkAgent().getCertificate(origin, mycallback); 890 if (networkManager)
892 891 networkManager._networkAgent.getCertificate(origin, (error, certificate) = > callback(error ? [] : certificate));
893 /** 892 else
894 * @param {?Protocol.Error} error 893 callback([]);
895 * @param {!Array<string>} certificate
896 */
897 function mycallback(error, certificate) {
898 callback(error ? [] : certificate);
899 }
900 } 894 }
901 895
902 /** 896 /**
903 * @param {string} url 897 * @param {string} url
904 * @param {function(number, !Object.<string, string>, string)} callback 898 * @param {function(number, !Object.<string, string>, string)} callback
905 */ 899 */
906 loadResource(url, callback) { 900 loadResource(url, callback) {
907 var headers = {}; 901 var headers = {};
908 902
909 var currentUserAgent = this._currentUserAgent(); 903 var currentUserAgent = this._currentUserAgent();
(...skipping 11 matching lines...) Expand all
921 SDK.MultitargetNetworkManager.Events = { 915 SDK.MultitargetNetworkManager.Events = {
922 BlockedPatternsChanged: Symbol('BlockedPatternsChanged'), 916 BlockedPatternsChanged: Symbol('BlockedPatternsChanged'),
923 ConditionsChanged: Symbol('ConditionsChanged'), 917 ConditionsChanged: Symbol('ConditionsChanged'),
924 UserAgentChanged: Symbol('UserAgentChanged') 918 UserAgentChanged: Symbol('UserAgentChanged')
925 }; 919 };
926 920
927 /** 921 /**
928 * @type {!SDK.MultitargetNetworkManager} 922 * @type {!SDK.MultitargetNetworkManager}
929 */ 923 */
930 SDK.multitargetNetworkManager; 924 SDK.multitargetNetworkManager;
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698