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 /** @type {!Set<string>} */ | |
696 this._blockedURLs = new Set(); | |
pfeldman
2017/02/21 23:59:01
Why duping the data, just keep the setting array.
| |
697 this._blockedSetting = Common.moduleSetting('networkBlockedURLs'); | |
698 this._blockedSetting.addChangeListener(this._updateBlockedURLs, this); | |
699 this._blockedSetting.set([]); | |
700 this._updateBlockedURLs(); | |
701 | |
702 SDK.targetManager.observeTargets(this, SDK.Target.Capability.Network); | 702 SDK.targetManager.observeTargets(this, SDK.Target.Capability.Network); |
703 } | 703 } |
704 | 704 |
705 /** | 705 /** |
706 * @param {string} uaString | 706 * @param {string} uaString |
707 * @return {string} | 707 * @return {string} |
708 */ | 708 */ |
709 static patchUserAgentWithChromeVersion(uaString) { | 709 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"). | 710 // 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+)'); | 711 var chromeRegex = new RegExp('(?:^|\\W)Chrome/(\\S+)'); |
712 var chromeMatch = navigator.userAgent.match(chromeRegex); | 712 var chromeMatch = navigator.userAgent.match(chromeRegex); |
713 if (chromeMatch && chromeMatch.length > 1) | 713 if (chromeMatch && chromeMatch.length > 1) |
714 return String.sprintf(uaString, chromeMatch[1]); | 714 return String.sprintf(uaString, chromeMatch[1]); |
715 return uaString; | 715 return uaString; |
716 } | 716 } |
717 | 717 |
718 /** | 718 /** |
719 * @override | 719 * @override |
720 * @param {!SDK.Target} target | 720 * @param {!SDK.Target} target |
721 */ | 721 */ |
722 targetAdded(target) { | 722 targetAdded(target) { |
723 var networkAgent = target.networkAgent(); | 723 var networkAgent = target.networkAgent(); |
724 if (this._extraHeaders) | 724 if (this._extraHeaders) |
725 networkAgent.setExtraHTTPHeaders(this._extraHeaders); | 725 networkAgent.setExtraHTTPHeaders(this._extraHeaders); |
726 if (this._currentUserAgent()) | 726 if (this._currentUserAgent()) |
727 networkAgent.setUserAgentOverride(this._currentUserAgent()); | 727 networkAgent.setUserAgentOverride(this._currentUserAgent()); |
728 for (var url of this._blockedURLs) | 728 if (this._isRequestBlockingEnabled) |
729 networkAgent.addBlockedURL(url); | 729 networkAgent.setBlockedURLs(this._blockedURLs.valuesArray()); |
730 this._agents.add(networkAgent); | 730 this._agents.add(networkAgent); |
731 if (this.isThrottling()) | 731 if (this.isThrottling()) |
732 this._updateNetworkConditions(networkAgent); | 732 this._updateNetworkConditions(networkAgent); |
733 } | 733 } |
734 | 734 |
735 /** | 735 /** |
736 * @override | 736 * @override |
737 * @param {!SDK.Target} target | 737 * @param {!SDK.Target} target |
738 */ | 738 */ |
739 targetRemoved(target) { | 739 targetRemoved(target) { |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
830 | 830 |
831 /** | 831 /** |
832 * @param {string} userAgent | 832 * @param {string} userAgent |
833 */ | 833 */ |
834 setCustomUserAgentOverride(userAgent) { | 834 setCustomUserAgentOverride(userAgent) { |
835 this._customUserAgent = userAgent; | 835 this._customUserAgent = userAgent; |
836 this._updateUserAgentOverride(); | 836 this._updateUserAgentOverride(); |
837 } | 837 } |
838 | 838 |
839 _updateBlockedURLs() { | 839 _updateBlockedURLs() { |
840 var blocked = this._blockedSetting.get(); | 840 this._blockedURLs = new Set(this._blockedSetting.get()); |
pfeldman
2017/02/21 23:59:01
this would become a local variable.
| |
841 for (var url of blocked) { | 841 var urls = /** @type {!Array<string>} */ ([]); |
842 if (!this._blockedURLs.has(url)) | 842 if (this._isRequestBlockingEnabled) |
843 this._addBlockedURL(url); | 843 urls = this._blockedURLs.valuesArray(); |
844 } | 844 for (var agent of this._agents) |
845 for (var url of this._blockedURLs) { | 845 agent.setBlockedURLs(urls); |
846 if (blocked.indexOf(url) === -1) | |
847 this._removeBlockedURL(url); | |
848 } | |
849 } | 846 } |
850 | 847 |
851 /** | 848 /** |
852 * @param {string} url | 849 * @param {string} url |
853 */ | 850 */ |
854 _addBlockedURL(url) { | 851 _addBlockedURL(url) { |
855 this._blockedURLs.add(url); | 852 this._blockedURLs.add(url); |
856 if (!this._isRequestBlockingEnabled) | 853 this._blockedSetting.set(this._blockedURLs.valuesArray()); |
857 return; | |
858 for (var agent of this._agents) | |
859 agent.addBlockedURL(url); | |
860 } | 854 } |
861 | 855 |
862 /** | 856 /** |
863 * @param {string} url | 857 * @param {string} url |
864 */ | 858 */ |
865 _removeBlockedURL(url) { | 859 _removeBlockedURL(url) { |
866 this._blockedURLs.delete(url); | 860 this._blockedURLs.delete(url); |
867 if (!this._isRequestBlockingEnabled) | 861 this._blockedSetting.set(this._blockedURLs.valuesArray()); |
868 return; | |
869 for (var agent of this._agents) | |
870 agent.removeBlockedURL(url); | |
871 } | 862 } |
872 | 863 |
873 /** | 864 /** |
874 * @param {boolean} enabled | 865 * @param {boolean} enabled |
875 */ | 866 */ |
876 setRequestBlockingEnabled(enabled) { | 867 setRequestBlockingEnabled(enabled) { |
877 if (this._isRequestBlockingEnabled === enabled) | 868 if (this._isRequestBlockingEnabled === enabled) |
878 return; | 869 return; |
879 this._isRequestBlockingEnabled = enabled; | 870 this._isRequestBlockingEnabled = enabled; |
880 if (enabled) { | 871 this._updateBlockedURLs(); |
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()); | 872 this.emit(new SDK.MultitargetNetworkManager.RequestBlockingEnabledChangedEve nt()); |
888 } | 873 } |
889 | 874 |
890 isRequestBlockingEnabled() { | 875 isRequestBlockingEnabled() { |
891 return this._isRequestBlockingEnabled; | 876 return this._isRequestBlockingEnabled; |
892 } | 877 } |
893 | 878 |
894 clearBrowserCache() { | 879 clearBrowserCache() { |
895 for (var agent of this._agents) | 880 for (var agent of this._agents) |
896 agent.clearBrowserCache(); | 881 agent.clearBrowserCache(); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
942 UserAgentChanged: Symbol('UserAgentChanged') | 927 UserAgentChanged: Symbol('UserAgentChanged') |
943 }; | 928 }; |
944 | 929 |
945 /** @implements {Common.Emittable} */ | 930 /** @implements {Common.Emittable} */ |
946 SDK.MultitargetNetworkManager.RequestBlockingEnabledChangedEvent = class {}; | 931 SDK.MultitargetNetworkManager.RequestBlockingEnabledChangedEvent = class {}; |
947 | 932 |
948 /** | 933 /** |
949 * @type {!SDK.MultitargetNetworkManager} | 934 * @type {!SDK.MultitargetNetworkManager} |
950 */ | 935 */ |
951 SDK.multitargetNetworkManager; | 936 SDK.multitargetNetworkManager; |
OLD | NEW |