Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / devtools / front_end / bindings / BlackboxSupport.js
blob0d0c0cf1ae341e15e69ba290c671dd8bce239617
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 WebInspector.BlackboxSupport = {}
7 /**
8  * @param {string} url
9  * @return {string}
10  */
11 WebInspector.BlackboxSupport._urlToRegExpString = function(url)
13     var parsedURL = new WebInspector.ParsedURL(url);
14     if (parsedURL.isAboutBlank() || parsedURL.isDataURL())
15         return "";
16     if (!parsedURL.isValid)
17         return "^" + url.escapeForRegExp() + "$";
18     var name = parsedURL.lastPathComponent;
19     if (name)
20         name = "/" + name;
21     else if (parsedURL.folderPathComponents)
22         name = parsedURL.folderPathComponents + "/";
23     if (!name)
24         name = parsedURL.host;
25     if (!name)
26         return "";
27     var scheme = parsedURL.scheme;
28     var prefix = "";
29     if (scheme && scheme !== "http" && scheme !== "https") {
30         prefix = "^" + scheme + "://";
31         if (scheme === "chrome-extension")
32             prefix += parsedURL.host + "\\b";
33         prefix += ".*";
34     }
35     return prefix + name.escapeForRegExp() + (url.endsWith(name) ? "$" : "\\b");
38 /**
39  * @param {string} url
40  * @return {boolean}
41  */
42 WebInspector.BlackboxSupport.canBlackboxURL = function(url)
44     return !!WebInspector.BlackboxSupport._urlToRegExpString(url);
47 /**
48  * @param {string} url
49  */
50 WebInspector.BlackboxSupport.blackboxURL = function(url)
52     var regexPatterns = WebInspector.moduleSetting("skipStackFramesPattern").getAsArray();
53     var regexValue = WebInspector.BlackboxSupport._urlToRegExpString(url);
54     if (!regexValue)
55         return;
56     var found = false;
57     for (var i = 0; i < regexPatterns.length; ++i) {
58         var item = regexPatterns[i];
59         if (item.pattern === regexValue) {
60             item.disabled = false;
61             found = true;
62             break;
63         }
64     }
65     if (!found)
66         regexPatterns.push({ pattern: regexValue });
67     WebInspector.moduleSetting("skipStackFramesPattern").setAsArray(regexPatterns);
70 /**
71  * @param {string} url
72  * @param {boolean} isContentScript
73  */
74 WebInspector.BlackboxSupport.unblackbox = function(url, isContentScript)
76     if (isContentScript)
77         WebInspector.moduleSetting("skipContentScripts").set(false);
79     var regexPatterns = WebInspector.moduleSetting("skipStackFramesPattern").getAsArray();
80     var regexValue = WebInspector.BlackboxSupport._urlToRegExpString(url);
81     if (!regexValue)
82         return;
83     regexPatterns = regexPatterns.filter(function(item) {
84         return item.pattern !== regexValue;
85     });
86     for (var i = 0; i < regexPatterns.length; ++i) {
87         var item = regexPatterns[i];
88         if (item.disabled)
89             continue;
90         try {
91             var regex = new RegExp(item.pattern);
92             if (regex.test(url))
93                 item.disabled = true;
94         } catch (e) {
95         }
96     }
97     WebInspector.moduleSetting("skipStackFramesPattern").setAsArray(regexPatterns);
101  * @param {string} url
102  * @return {boolean}
103  */
104 WebInspector.BlackboxSupport.isBlackboxedURL = function(url)
106     var regex = WebInspector.moduleSetting("skipStackFramesPattern").asRegExp();
107     return regex && regex.test(url);
111  * @param {string} url
112  * @param {boolean} isContentScript
113  * @return {boolean}
114  */
115 WebInspector.BlackboxSupport.isBlackboxed = function(url, isContentScript)
117     if (isContentScript && WebInspector.moduleSetting("skipContentScripts").get())
118         return true;
119     return WebInspector.BlackboxSupport.isBlackboxedURL(url);
123  * @param {function(!WebInspector.Event)} listener
124  * @param {!Object=} thisObject
125  */
126 WebInspector.BlackboxSupport.addChangeListener = function(listener, thisObject)
128     WebInspector.moduleSetting("skipStackFramesPattern").addChangeListener(listener, thisObject);
132  * @param {function(!WebInspector.Event)} listener
133  * @param {!Object=} thisObject
134  */
135 WebInspector.BlackboxSupport.removeChangeListener = function(listener, thisObject)
137     WebInspector.moduleSetting("skipStackFramesPattern").removeChangeListener(listener, thisObject);