~mmach/netext73/webkit2gtk

« back to all changes in this revision

Viewing changes to Source/WebInspectorUI/UserInterface/Models/LocalResourceOverride.js

  • Committer: mmach
  • Date: 2023-06-16 17:21:37 UTC
  • Revision ID: netbit73@gmail.com-20230616172137-2rqx6yr96ga9g3kp
1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2019 Apple Inc. All rights reserved.
 
3
 *
 
4
 * Redistribution and use in source and binary forms, with or without
 
5
 * modification, are permitted provided that the following conditions
 
6
 * are met:
 
7
 * 1. Redistributions of source code must retain the above copyright
 
8
 *    notice, this list of conditions and the following disclaimer.
 
9
 * 2. Redistributions in binary form must reproduce the above copyright
 
10
 *    notice, this list of conditions and the following disclaimer in the
 
11
 *    documentation and/or other materials provided with the distribution.
 
12
 *
 
13
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
 
14
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 
15
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 
16
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
 
17
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 
18
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 
19
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 
20
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 
21
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 
22
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 
23
 * THE POSSIBILITY OF SUCH DAMAGE.
 
24
 */
 
25
 
 
26
WI.LocalResourceOverride = class LocalResourceOverride extends WI.Object
 
27
{
 
28
    constructor(localResource, {isCaseSensitive, isRegex, disabled} = {})
 
29
    {
 
30
        console.assert(localResource instanceof WI.LocalResource);
 
31
        console.assert(localResource.isLocalResourceOverride);
 
32
        console.assert(localResource.url);
 
33
        console.assert(isCaseSensitive === undefined || typeof isCaseSensitive === "boolean");
 
34
        console.assert(isRegex === undefined || typeof isRegex === "boolean");
 
35
        console.assert(disabled === undefined || typeof disabled === "boolean");
 
36
 
 
37
        super();
 
38
 
 
39
        this._localResource = localResource;
 
40
        this._isCaseSensitive = isCaseSensitive !== undefined ? isCaseSensitive : true;
 
41
        this._isRegex = isRegex !== undefined ? isRegex : false;
 
42
        this._disabled = disabled !== undefined ? disabled : false;
 
43
    }
 
44
 
 
45
    // Static
 
46
 
 
47
    static create({url, mimeType, content, base64Encoded, statusCode, statusText, headers, isCaseSensitive, isRegex, disabled})
 
48
    {
 
49
        let localResource = new WI.LocalResource({
 
50
            request: {
 
51
                url: isRegex ? url : WI.urlWithoutFragment(url),
 
52
            },
 
53
            response: {
 
54
                headers,
 
55
                mimeType,
 
56
                statusCode,
 
57
                statusText,
 
58
                content,
 
59
                base64Encoded,
 
60
            },
 
61
            isLocalResourceOverride: true,
 
62
        });
 
63
 
 
64
        return new WI.LocalResourceOverride(localResource, {isCaseSensitive, isRegex, disabled});
 
65
    }
 
66
 
 
67
    // Import / Export
 
68
 
 
69
    static fromJSON(json)
 
70
    {
 
71
        let {localResource, isCaseSensitive, isRegex, disabled} = json;
 
72
        return new WI.LocalResourceOverride(WI.LocalResource.fromJSON(localResource), {isCaseSensitive, isRegex, disabled});
 
73
    }
 
74
 
 
75
    toJSON(key)
 
76
    {
 
77
        let json = {
 
78
            localResource: this._localResource.toJSON(key),
 
79
            isCaseSensitive: this._isCaseSensitive,
 
80
            isRegex: this._isRegex,
 
81
            disabled: this._disabled,
 
82
        };
 
83
 
 
84
        if (key === WI.ObjectStore.toJSONSymbol)
 
85
            json[WI.objectStores.localResourceOverrides.keyPath] = this._localResource.url;
 
86
 
 
87
        return json;
 
88
    }
 
89
 
 
90
    // Public
 
91
 
 
92
    get url() { return this._localResource.url; }
 
93
    get localResource() { return this._localResource; }
 
94
    get isCaseSensitive() { return this._isCaseSensitive; }
 
95
    get isRegex() { return this._isRegex; }
 
96
 
 
97
    get disabled()
 
98
    {
 
99
        return this._disabled;
 
100
    }
 
101
 
 
102
    set disabled(disabled)
 
103
    {
 
104
        if (this._disabled === disabled)
 
105
            return;
 
106
 
 
107
        this._disabled = !!disabled;
 
108
 
 
109
        this.dispatchEventToListeners(WI.LocalResourceOverride.Event.DisabledChanged);
 
110
    }
 
111
 
 
112
    matches(url)
 
113
    {
 
114
        if (this._isRegex) {
 
115
            let regex = new RegExp(this.url, !this._isCaseSensitive ? "i" : "");
 
116
            return regex.test(url);
 
117
        }
 
118
 
 
119
        if (!this._isCaseSensitive)
 
120
            return String(url).toLowerCase() === this.url.toLowerCase();
 
121
 
 
122
        return url === this.url;
 
123
    }
 
124
 
 
125
    // Protected
 
126
 
 
127
    saveIdentityToCookie(cookie)
 
128
    {
 
129
        cookie["local-resource-override-url"] = this._localResource.url;
 
130
        cookie["local-resource-override-is-case-sensitive"] = this._isCaseSensitive;
 
131
        cookie["local-resource-override-is-regex"] = this._isRegex;
 
132
        cookie["local-resource-override-disabled"] = this._disabled;
 
133
    }
 
134
};
 
135
 
 
136
WI.LocalResourceOverride.TypeIdentifier = "local-resource-override";
 
137
 
 
138
WI.LocalResourceOverride.Event = {
 
139
    DisabledChanged: "local-resource-override-disabled-state-did-change",
 
140
};