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 666 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
677 } | 677 } |
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 | |
688 /** @type {!Set<string>} */ | |
689 this._blockedURLs = new Set(); | |
690 this._blockedSetting = Common.moduleSetting('networkBlockedURLs'); | |
691 this._blockedSetting.addChangeListener(this._updateBlockedURLs, this); | |
692 this._updateBlockedURLs(); | |
693 | |
694 this._userAgentOverride = ''; | 687 this._userAgentOverride = ''; |
695 /** @type {!Set<!Protocol.NetworkAgent>} */ | 688 /** @type {!Set<!Protocol.NetworkAgent>} */ |
696 this._agents = new Set(); | 689 this._agents = new Set(); |
697 /** @type {!SDK.NetworkManager.Conditions} */ | 690 /** @type {!SDK.NetworkManager.Conditions} */ |
698 this._networkConditions = SDK.NetworkManager.NoThrottlingConditions; | 691 this._networkConditions = SDK.NetworkManager.NoThrottlingConditions; |
699 | 692 |
| 693 this._agentsHaveBlockedURLs = false; |
| 694 this._blockedEnabledSetting = Common.moduleSetting('requestBlockingEnabled')
; |
| 695 this._blockedEnabledSetting.addChangeListener(this._updateBlockedURLs, this)
; |
| 696 this._blockedURLsSetting = Common.moduleSetting('networkBlockedURLs'); |
| 697 this._blockedURLsSetting.addChangeListener(this._updateBlockedURLs, this); |
| 698 this._updateBlockedURLs(); |
| 699 |
700 SDK.targetManager.observeTargets(this, SDK.Target.Capability.Network); | 700 SDK.targetManager.observeTargets(this, SDK.Target.Capability.Network); |
701 } | 701 } |
702 | 702 |
703 /** | 703 /** |
704 * @param {string} uaString | 704 * @param {string} uaString |
705 * @return {string} | 705 * @return {string} |
706 */ | 706 */ |
707 static patchUserAgentWithChromeVersion(uaString) { | 707 static patchUserAgentWithChromeVersion(uaString) { |
708 // 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"). |
709 var chromeRegex = new RegExp('(?:^|\\W)Chrome/(\\S+)'); | 709 var chromeRegex = new RegExp('(?:^|\\W)Chrome/(\\S+)'); |
710 var chromeMatch = navigator.userAgent.match(chromeRegex); | 710 var chromeMatch = navigator.userAgent.match(chromeRegex); |
711 if (chromeMatch && chromeMatch.length > 1) | 711 if (chromeMatch && chromeMatch.length > 1) |
712 return String.sprintf(uaString, chromeMatch[1]); | 712 return String.sprintf(uaString, chromeMatch[1]); |
713 return uaString; | 713 return uaString; |
714 } | 714 } |
715 | 715 |
716 /** | 716 /** |
717 * @override | 717 * @override |
718 * @param {!SDK.Target} target | 718 * @param {!SDK.Target} target |
719 */ | 719 */ |
720 targetAdded(target) { | 720 targetAdded(target) { |
721 var networkAgent = target.networkAgent(); | 721 var networkAgent = target.networkAgent(); |
722 if (this._extraHeaders) | 722 if (this._extraHeaders) |
723 networkAgent.setExtraHTTPHeaders(this._extraHeaders); | 723 networkAgent.setExtraHTTPHeaders(this._extraHeaders); |
724 if (this._currentUserAgent()) | 724 if (this._currentUserAgent()) |
725 networkAgent.setUserAgentOverride(this._currentUserAgent()); | 725 networkAgent.setUserAgentOverride(this._currentUserAgent()); |
726 for (var url of this._blockedURLs) | 726 if (this._blockedEnabledSetting.get()) |
727 networkAgent.addBlockedURL(url); | 727 networkAgent.setBlockedURLs(this._blockedURLsSetting.get()); |
728 this._agents.add(networkAgent); | 728 this._agents.add(networkAgent); |
729 if (this.isThrottling()) | 729 if (this.isThrottling()) |
730 this._updateNetworkConditions(networkAgent); | 730 this._updateNetworkConditions(networkAgent); |
731 } | 731 } |
732 | 732 |
733 /** | 733 /** |
734 * @override | 734 * @override |
735 * @param {!SDK.Target} target | 735 * @param {!SDK.Target} target |
736 */ | 736 */ |
737 targetRemoved(target) { | 737 targetRemoved(target) { |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
828 | 828 |
829 /** | 829 /** |
830 * @param {string} userAgent | 830 * @param {string} userAgent |
831 */ | 831 */ |
832 setCustomUserAgentOverride(userAgent) { | 832 setCustomUserAgentOverride(userAgent) { |
833 this._customUserAgent = userAgent; | 833 this._customUserAgent = userAgent; |
834 this._updateUserAgentOverride(); | 834 this._updateUserAgentOverride(); |
835 } | 835 } |
836 | 836 |
837 _updateBlockedURLs() { | 837 _updateBlockedURLs() { |
838 var blocked = this._blockedSetting.get(); | 838 var urls = /** @type {!Array<string>} */ ([]); |
839 for (var url of blocked) { | 839 if (this._blockedEnabledSetting.get()) |
840 if (!this._blockedURLs.has(url)) | 840 urls = this._blockedURLsSetting.get(); |
841 this._addBlockedURL(url); | 841 if (!urls.length && !this._agentsHaveBlockedURLs) |
842 } | 842 return; |
843 for (var url of this._blockedURLs) { | 843 this._agentsHaveBlockedURLs = !!urls.length; |
844 if (blocked.indexOf(url) === -1) | |
845 this._removeBlockedURL(url); | |
846 } | |
847 } | |
848 | |
849 /** | |
850 * @param {string} url | |
851 */ | |
852 _addBlockedURL(url) { | |
853 this._blockedURLs.add(url); | |
854 for (var agent of this._agents) | 844 for (var agent of this._agents) |
855 agent.addBlockedURL(url); | 845 agent.setBlockedURLs(urls); |
856 } | |
857 | |
858 /** | |
859 * @param {string} url | |
860 */ | |
861 _removeBlockedURL(url) { | |
862 this._blockedURLs.delete(url); | |
863 for (var agent of this._agents) | |
864 agent.removeBlockedURL(url); | |
865 } | 846 } |
866 | 847 |
867 clearBrowserCache() { | 848 clearBrowserCache() { |
868 for (var agent of this._agents) | 849 for (var agent of this._agents) |
869 agent.clearBrowserCache(); | 850 agent.clearBrowserCache(); |
870 } | 851 } |
871 | 852 |
872 clearBrowserCookies() { | 853 clearBrowserCookies() { |
873 for (var agent of this._agents) | 854 for (var agent of this._agents) |
874 agent.clearBrowserCookies(); | 855 agent.clearBrowserCookies(); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
908 Host.ResourceLoader.load(url, headers, callback); | 889 Host.ResourceLoader.load(url, headers, callback); |
909 } | 890 } |
910 }; | 891 }; |
911 | 892 |
912 /** @enum {symbol} */ | 893 /** @enum {symbol} */ |
913 SDK.MultitargetNetworkManager.Events = { | 894 SDK.MultitargetNetworkManager.Events = { |
914 ConditionsChanged: Symbol('ConditionsChanged'), | 895 ConditionsChanged: Symbol('ConditionsChanged'), |
915 UserAgentChanged: Symbol('UserAgentChanged') | 896 UserAgentChanged: Symbol('UserAgentChanged') |
916 }; | 897 }; |
917 | 898 |
918 | |
919 /** | 899 /** |
920 * @type {!SDK.MultitargetNetworkManager} | 900 * @type {!SDK.MultitargetNetworkManager} |
921 */ | 901 */ |
922 SDK.multitargetNetworkManager; | 902 SDK.multitargetNetworkManager; |
OLD | NEW |