Changeset 192614 in webkit


Ignore:
Timestamp:
Nov 18, 2015, 10:27:21 PM (10 years ago)
Author:
BJ Burg
Message:

Web Inspector: move cookie url matching out of CookieStorageContentView and clean up some code
https://bugs.webkit.org/show_bug.cgi?id=151424

Reviewed by Timothy Hatcher.

Move the code that decides whether a cookie matches a resource URL to a model class.
This will make it possible to test this code easily without pulling Views into tests.

  • UserInterface/Models/CookieStorageObject.js:

(WebInspector.CookieStorageObject.cookieMatchesResourceURL):
(WebInspector.CookieStorageObject.cookieDomainMatchesResourceDomain):
(WebInspector.CookieStorageObject.prototype.saveIdentityToCookie):
(WebInspector.CookieStorageObject):

  • UserInterface/Views/CookieStorageContentView.js:

(WebInspector.CookieStorageContentView.prototype.update): use Promises.
(WebInspector.CookieStorageContentView.prototype._rebuildTable):
(WebInspector.CookieStorageContentView.prototype._filterCookies):

Use Array.filter() and Array.some() to express this logic more directly.

(WebInspector.CookieStorageContentView.cookieMatchesResourceURL): Deleted.
(WebInspector.CookieStorageContentView.cookieDomainMatchesResourceDomain): Deleted.
(WebInspector.CookieStorageContentView.prototype.update.callback): Deleted.

Location:
trunk/Source/WebInspectorUI
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebInspectorUI/ChangeLog

    r192613 r192614  
     12015-11-18  Brian Burg  <bburg@apple.com>
     2
     3        Web Inspector: move cookie url matching out of CookieStorageContentView and clean up some code
     4        https://bugs.webkit.org/show_bug.cgi?id=151424
     5
     6        Reviewed by Timothy Hatcher.
     7
     8        Move the code that decides whether a cookie matches a resource URL to a model class.
     9        This will make it possible to test this code easily without pulling Views into tests.
     10
     11        * UserInterface/Models/CookieStorageObject.js:
     12        (WebInspector.CookieStorageObject.cookieMatchesResourceURL):
     13        (WebInspector.CookieStorageObject.cookieDomainMatchesResourceDomain):
     14        (WebInspector.CookieStorageObject.prototype.saveIdentityToCookie):
     15        (WebInspector.CookieStorageObject):
     16        * UserInterface/Views/CookieStorageContentView.js:
     17        (WebInspector.CookieStorageContentView.prototype.update): use Promises.
     18        (WebInspector.CookieStorageContentView.prototype._rebuildTable):
     19        (WebInspector.CookieStorageContentView.prototype._filterCookies):
     20
     21            Use Array.filter() and Array.some() to express this logic more directly.
     22
     23        (WebInspector.CookieStorageContentView.cookieMatchesResourceURL): Deleted.
     24        (WebInspector.CookieStorageContentView.cookieDomainMatchesResourceDomain): Deleted.
     25        (WebInspector.CookieStorageContentView.prototype.update.callback): Deleted.
     26
    1272015-11-18  Matt Baker  <mattbaker@apple.com>
    228
  • trunk/Source/WebInspectorUI/UserInterface/Models/CookieStorageObject.js

    r181769 r192614  
    11/*
    2  * Copyright (C) 2013 Apple Inc. All rights reserved.
     2 * Copyright (C) 2013, 2015 Apple Inc. All rights reserved.
    33 *
    44 * Redistribution and use in source and binary forms, with or without
     
    3131    }
    3232
     33    // Static
     34
     35    static cookieMatchesResourceURL(cookie, resourceURL)
     36    {
     37        var parsedURL = parseURL(resourceURL);
     38        if (!parsedURL || !WebInspector.CookieStorageObject.cookieDomainMatchesResourceDomain(cookie.domain, parsedURL.host))
     39            return false;
     40
     41        return (parsedURL.path.startsWith(cookie.path)
     42            && (!cookie.port || parsedURL.port === cookie.port)
     43            && (!cookie.secure || parsedURL.scheme === "https"));
     44    }
     45
     46    static cookieDomainMatchesResourceDomain(cookieDomain, resourceDomain)
     47    {
     48        if (cookieDomain.charAt(0) !== ".")
     49            return resourceDomain === cookieDomain;
     50        return !!resourceDomain.match(new RegExp("^([^\\.]+\\.)?" + cookieDomain.substring(1).escapeForRegExp() + "$"), "i");
     51    }
     52
    3353    // Public
    3454
     
    4060    saveIdentityToCookie(cookie)
    4161    {
    42         // FIXME: This class will need to look up cookies that are set for this host.
     62        // FIXME <https://webkit.org/b/151413>: This class should actually store cookie data for this host.
    4363        cookie[WebInspector.CookieStorageObject.CookieHostCookieKey] = this.host;
    4464    }   
  • trunk/Source/WebInspectorUI/UserInterface/Views/CookieStorageContentView.js

    r192086 r192614  
    3838    }
    3939
    40     // Static
    41 
    42     static cookieMatchesResourceURL(cookie, resourceURL)
    43     {
    44         var parsedURL = parseURL(resourceURL);
    45         if (!parsedURL || !WebInspector.CookieStorageContentView.cookieDomainMatchesResourceDomain(cookie.domain, parsedURL.host))
    46             return false;
    47 
    48         return (parsedURL.path.startsWith(cookie.path)
    49             && (!cookie.port || parsedURL.port === cookie.port)
    50             && (!cookie.secure || parsedURL.scheme === "https"));
    51     }
    52 
    53     static cookieDomainMatchesResourceDomain(cookieDomain, resourceDomain)
    54     {
    55         if (cookieDomain.charAt(0) !== ".")
    56             return resourceDomain === cookieDomain;
    57         return !!resourceDomain.match(new RegExp("^([^\\.]+\\.)?" + cookieDomain.substring(1).escapeForRegExp() + "$"), "i");
    58     }
    59 
    6040    // Public
    6141
     
    6747    update()
    6848    {
    69         function callback(error, cookies)
    70         {
    71             if (error)
    72                 return;
    73 
    74             this._cookies = this._filterCookies(cookies);
     49        PageAgent.getCookies().then((payload) => {
     50            this._cookies = this._filterCookies(payload.cookies);
    7551            this._rebuildTable();
    76         }
    77 
    78         PageAgent.getCookies(callback.bind(this));
     52        }).catch((error) => {
     53            console.error("Could not fetch cookies: ", error);
     54        });
    7955    }
    8056
     
    9672    _rebuildTable()
    9773    {
    98         // FIXME: If there are no cookies, do we want to show an empty datagrid, or do something like the old
    99         // inspector and show some text saying there are no cookies?
     74        // FIXME <https://webkit.org/b/151400>: If there are no cookies, add placeholder explanatory text.
    10075        if (!this._dataGrid) {
    10176            var columns = {name: {}, value: {}, domain: {}, path: {}, expires: {}, size: {}, http: {}, secure: {}};
     
    173148    _filterCookies(cookies)
    174149    {
    175         var filteredCookies = [];
    176         var resourcesForDomain = [];
    177 
    178         var frames = WebInspector.frameResourceManager.frames;
    179         for (var i = 0; i < frames.length; ++i) {
    180             var resources = frames[i].resources;
    181             for (var j = 0; j < resources.length; ++j) {
    182                 var urlComponents = resources[j].urlComponents;
    183                 if (urlComponents && urlComponents.host && urlComponents.host === this.representedObject.host)
    184                     resourcesForDomain.push(resources[j].url);
    185             }
    186 
    187             // The main resource isn't always in the list of resources, make sure to add it to the list of resources
    188             // we get the URLs from.
    189             var mainResourceURLComponents = frames[i].mainResource.urlComponents;
    190             if (mainResourceURLComponents && mainResourceURLComponents.host && mainResourceURLComponents.host === this.representedObject.host)
    191                 resourcesForDomain.push(frames[i].mainResource.url);
    192         }
    193 
    194         for (var i = 0; i < cookies.length; ++i) {
    195             for (var j = 0; j < resourcesForDomain.length; ++j) {
    196                 if (WebInspector.CookieStorageContentView.cookieMatchesResourceURL(cookies[i], resourcesForDomain[j])) {
    197                     filteredCookies.push(cookies[i]);
    198                     break;
    199                 }
    200             }
    201         }
    202 
    203         return filteredCookies;
     150        let resourceMatchesStorageDomain = (resource) => {
     151            let urlComponents = resource.urlComponents;
     152            return urlComponents && urlComponents.host && urlComponents.host === this.representedObject.host;
     153        }
     154
     155        let allResources = [];
     156        for (let frame of WebInspector.frameResourceManager.frames) {
     157            // The main resource isn't in the list of resources, so add it as a candidate.
     158            allResources.push(frame.mainResource);
     159            allResources = allResources.concat(frame.resources);
     160        }
     161
     162        let resourcesForDomain = allResources.filter(resourceMatchesStorageDomain);
     163
     164        let cookiesForDomain = cookies.filter((cookie) => {
     165            return resourcesForDomain.some((resource) => {
     166                return WebInspector.CookieStorageObject.cookieMatchesResourceURL(cookie, resource.url);
     167            });
     168        });
     169        return cookiesForDomain;
    204170    }
    205171
Note: See TracChangeset for help on using the changeset viewer.