OLD | NEW |
---|---|
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 667 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
678 }; | 678 }; |
679 | 679 |
680 /** | 680 /** |
681 * @implements {SDK.TargetManager.Observer} | 681 * @implements {SDK.TargetManager.Observer} |
682 * @unrestricted | 682 * @unrestricted |
683 */ | 683 */ |
684 SDK.MultitargetNetworkManager = class extends Common.Object { | 684 SDK.MultitargetNetworkManager = class extends Common.Object { |
685 constructor() { | 685 constructor() { |
686 super(); | 686 super(); |
687 | 687 |
688 /** @type {!Set<string>} */ | |
689 this._blockedURLs = new Set(); | |
690 this._blockedSetting = Common.moduleSetting('networkBlockedURLs'); | |
691 this._blockedSetting.addChangeListener(this._updateBlockedURLs, this); | |
692 this._blockedSetting.set([]); | |
693 this._updateBlockedURLs(); | |
694 | |
695 this._userAgentOverride = ''; | 688 this._userAgentOverride = ''; |
696 this._isRequestBlockingEnabled = false; | 689 this._isRequestBlockingEnabled = false; |
697 /** @type {!Set<!Protocol.NetworkAgent>} */ | 690 /** @type {!Set<!Protocol.NetworkAgent>} */ |
698 this._agents = new Set(); | 691 this._agents = new Set(); |
699 /** @type {!SDK.NetworkManager.Conditions} */ | 692 /** @type {!SDK.NetworkManager.Conditions} */ |
700 this._networkConditions = SDK.NetworkManager.NoThrottlingConditions; | 693 this._networkConditions = SDK.NetworkManager.NoThrottlingConditions; |
701 | 694 |
695 this._blockedSetting = Common.moduleSetting('networkBlockedURLs'); | |
696 this._blockedSetting.addChangeListener(this._updateBlockedURLs, this); | |
697 this._blockedSetting.set([]); | |
pfeldman
2017/03/06 18:23:10
Don't add it here?
| |
698 this._updateBlockedURLs(); | |
699 | |
702 SDK.targetManager.observeTargets(this, SDK.Target.Capability.Network); | 700 SDK.targetManager.observeTargets(this, SDK.Target.Capability.Network); |
703 } | 701 } |
704 | 702 |
705 /** | 703 /** |
706 * @param {string} uaString | 704 * @param {string} uaString |
707 * @return {string} | 705 * @return {string} |
708 */ | 706 */ |
709 static patchUserAgentWithChromeVersion(uaString) { | 707 static patchUserAgentWithChromeVersion(uaString) { |
710 // Patches Chrome/CriOS version from user agent ("1.2.3.4" when user agent i s: "Chrome/1.2.3.4"). | 708 // Patches Chrome/CriOS version from user agent ("1.2.3.4" when user agent i s: "Chrome/1.2.3.4"). |
711 var chromeRegex = new RegExp('(?:^|\\W)Chrome/(\\S+)'); | 709 var chromeRegex = new RegExp('(?:^|\\W)Chrome/(\\S+)'); |
712 var chromeMatch = navigator.userAgent.match(chromeRegex); | 710 var chromeMatch = navigator.userAgent.match(chromeRegex); |
713 if (chromeMatch && chromeMatch.length > 1) | 711 if (chromeMatch && chromeMatch.length > 1) |
714 return String.sprintf(uaString, chromeMatch[1]); | 712 return String.sprintf(uaString, chromeMatch[1]); |
715 return uaString; | 713 return uaString; |
716 } | 714 } |
717 | 715 |
718 /** | 716 /** |
719 * @override | 717 * @override |
720 * @param {!SDK.Target} target | 718 * @param {!SDK.Target} target |
721 */ | 719 */ |
722 targetAdded(target) { | 720 targetAdded(target) { |
723 var networkAgent = target.networkAgent(); | 721 var networkAgent = target.networkAgent(); |
724 if (this._extraHeaders) | 722 if (this._extraHeaders) |
725 networkAgent.setExtraHTTPHeaders(this._extraHeaders); | 723 networkAgent.setExtraHTTPHeaders(this._extraHeaders); |
726 if (this._currentUserAgent()) | 724 if (this._currentUserAgent()) |
727 networkAgent.setUserAgentOverride(this._currentUserAgent()); | 725 networkAgent.setUserAgentOverride(this._currentUserAgent()); |
728 for (var url of this._blockedURLs) | 726 if (this._isRequestBlockingEnabled) |
729 networkAgent.addBlockedURL(url); | 727 networkAgent.setBlockedURLs(this._blockedSetting.get()); |
730 this._agents.add(networkAgent); | 728 this._agents.add(networkAgent); |
731 if (this.isThrottling()) | 729 if (this.isThrottling()) |
732 this._updateNetworkConditions(networkAgent); | 730 this._updateNetworkConditions(networkAgent); |
733 } | 731 } |
734 | 732 |
735 /** | 733 /** |
736 * @override | 734 * @override |
737 * @param {!SDK.Target} target | 735 * @param {!SDK.Target} target |
738 */ | 736 */ |
739 targetRemoved(target) { | 737 targetRemoved(target) { |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
830 | 828 |
831 /** | 829 /** |
832 * @param {string} userAgent | 830 * @param {string} userAgent |
833 */ | 831 */ |
834 setCustomUserAgentOverride(userAgent) { | 832 setCustomUserAgentOverride(userAgent) { |
835 this._customUserAgent = userAgent; | 833 this._customUserAgent = userAgent; |
836 this._updateUserAgentOverride(); | 834 this._updateUserAgentOverride(); |
837 } | 835 } |
838 | 836 |
839 _updateBlockedURLs() { | 837 _updateBlockedURLs() { |
840 var blocked = this._blockedSetting.get(); | 838 var urls = /** @type {!Array<string>} */ ([]); |
841 for (var url of blocked) { | 839 if (this._isRequestBlockingEnabled) |
842 if (!this._blockedURLs.has(url)) | 840 urls = this._blockedSetting.get(); |
843 this._addBlockedURL(url); | 841 for (var agent of this._agents) |
844 } | 842 agent.setBlockedURLs(urls); |
pfeldman
2017/03/06 18:23:10
It'be nice to not issue these calls unless necessa
| |
845 for (var url of this._blockedURLs) { | |
846 if (blocked.indexOf(url) === -1) | |
847 this._removeBlockedURL(url); | |
848 } | |
849 } | 843 } |
850 | 844 |
851 /** | 845 /** |
852 * @param {string} url | |
853 */ | |
854 _addBlockedURL(url) { | |
855 this._blockedURLs.add(url); | |
856 if (!this._isRequestBlockingEnabled) | |
857 return; | |
858 for (var agent of this._agents) | |
859 agent.addBlockedURL(url); | |
860 } | |
861 | |
862 /** | |
863 * @param {string} url | |
864 */ | |
865 _removeBlockedURL(url) { | |
866 this._blockedURLs.delete(url); | |
867 if (!this._isRequestBlockingEnabled) | |
868 return; | |
869 for (var agent of this._agents) | |
870 agent.removeBlockedURL(url); | |
871 } | |
872 | |
873 /** | |
874 * @param {boolean} enabled | 846 * @param {boolean} enabled |
875 */ | 847 */ |
876 setRequestBlockingEnabled(enabled) { | 848 setRequestBlockingEnabled(enabled) { |
877 if (this._isRequestBlockingEnabled === enabled) | 849 if (this._isRequestBlockingEnabled === enabled) |
878 return; | 850 return; |
879 this._isRequestBlockingEnabled = enabled; | 851 this._isRequestBlockingEnabled = enabled; |
880 if (enabled) { | 852 this._updateBlockedURLs(); |
pfeldman
2017/03/06 18:23:10
It can be easily achieved via clearing the blocked
| |
881 for (var agent of this._agents) | |
882 this._blockedURLs.forEach(url => agent.addBlockedURL(url)); | |
883 } else { | |
884 for (var agent of this._agents) | |
885 this._blockedURLs.forEach(url => agent.removeBlockedURL(url)); | |
886 } | |
887 this.emit(new SDK.MultitargetNetworkManager.RequestBlockingEnabledChangedEve nt()); | 853 this.emit(new SDK.MultitargetNetworkManager.RequestBlockingEnabledChangedEve nt()); |
888 } | 854 } |
889 | 855 |
890 isRequestBlockingEnabled() { | 856 isRequestBlockingEnabled() { |
891 return this._isRequestBlockingEnabled; | 857 return this._isRequestBlockingEnabled; |
892 } | 858 } |
893 | 859 |
894 clearBrowserCache() { | 860 clearBrowserCache() { |
895 for (var agent of this._agents) | 861 for (var agent of this._agents) |
896 agent.clearBrowserCache(); | 862 agent.clearBrowserCache(); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
942 UserAgentChanged: Symbol('UserAgentChanged') | 908 UserAgentChanged: Symbol('UserAgentChanged') |
943 }; | 909 }; |
944 | 910 |
945 /** @implements {Common.Emittable} */ | 911 /** @implements {Common.Emittable} */ |
946 SDK.MultitargetNetworkManager.RequestBlockingEnabledChangedEvent = class {}; | 912 SDK.MultitargetNetworkManager.RequestBlockingEnabledChangedEvent = class {}; |
947 | 913 |
948 /** | 914 /** |
949 * @type {!SDK.MultitargetNetworkManager} | 915 * @type {!SDK.MultitargetNetworkManager} |
950 */ | 916 */ |
951 SDK.multitargetNetworkManager; | 917 SDK.multitargetNetworkManager; |
OLD | NEW |