Home | History | Annotate | Download | only in sdk
      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.
      4 
      5 WebInspector.BlackboxSupport = {}
      6 
      7 /**
      8  * @param {string} url
      9  * @return {string}
     10  */
     11 WebInspector.BlackboxSupport._urlToRegExpString = function(url)
     12 {
     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");
     36 }
     37 
     38 /**
     39  * @param {string} url
     40  * @return {boolean}
     41  */
     42 WebInspector.BlackboxSupport.canBlackboxURL = function(url)
     43 {
     44     return !!WebInspector.BlackboxSupport._urlToRegExpString(url);
     45 }
     46 
     47 /**
     48  * @param {string} url
     49  */
     50 WebInspector.BlackboxSupport.blackboxURL = function(url)
     51 {
     52     var regexPatterns = WebInspector.settings.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.settings.skipStackFramesPattern.setAsArray(regexPatterns);
     68 }
     69 
     70 /**
     71  * @param {string} url
     72  * @param {boolean} isContentScript
     73  */
     74 WebInspector.BlackboxSupport.unblackbox = function(url, isContentScript)
     75 {
     76     if (isContentScript)
     77         WebInspector.settings.skipContentScripts.set(false);
     78 
     79     var regexPatterns = WebInspector.settings.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.settings.skipStackFramesPattern.setAsArray(regexPatterns);
     98 }
     99 
    100 /**
    101  * @param {string} url
    102  * @return {boolean}
    103  */
    104 WebInspector.BlackboxSupport.isBlackboxedURL = function(url)
    105 {
    106     var regex = WebInspector.settings.skipStackFramesPattern.asRegExp();
    107     return (url && regex) ? regex.test(url) : false;
    108 }
    109 
    110 /**
    111  * @param {string} url
    112  * @param {boolean} isContentScript
    113  * @return {boolean}
    114  */
    115 WebInspector.BlackboxSupport.isBlackboxed = function(url, isContentScript)
    116 {
    117     if (isContentScript && WebInspector.settings.skipContentScripts.get())
    118         return true;
    119     return WebInspector.BlackboxSupport.isBlackboxedURL(url);
    120 }
    121