04dfdbc44586d711f576255e311a9c9eab6d7155
[WebKit-https.git] / Source / WebInspectorUI / UserInterface / Models / AuditTestBase.js
1 /*
2  * Copyright (C) 2018 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.AuditTestBase = class AuditTestBase extends WI.Object
27 {
28     constructor(name, {description} = {})
29     {
30         console.assert(typeof name === "string");
31         console.assert(!description || typeof description === "string");
32
33         super();
34
35         this._name = name;
36         this._description = description || null;
37
38         this._runningState = WI.AuditManager.RunningState.Inactive;
39         this._result = null;
40     }
41
42     // Public
43
44     get name() { return this._name; }
45     get description() { return this._description; }
46     get runningState() { return this._runningState; }
47     get result() { return this._result; }
48
49     async start()
50     {
51         // Called from WI.AuditManager.
52
53         console.assert(WI.auditManager.runningState === WI.AuditManager.RunningState.Active);
54
55         console.assert(this._runningState === WI.AuditManager.RunningState.Inactive);
56         if (this._runningState !== WI.AuditManager.RunningState.Inactive)
57             return;
58
59         this._runningState = WI.AuditManager.RunningState.Active;
60         this.dispatchEventToListeners(WI.AuditTestBase.Event.Scheduled);
61
62         await this.run();
63
64         this._runningState = WI.AuditManager.RunningState.Inactive;
65         this.dispatchEventToListeners(WI.AuditTestBase.Event.Completed);
66     }
67
68     stop()
69     {
70         // Called from WI.AuditManager.
71
72         console.assert(this._runningState !== WI.AuditManager.RunningState.Inactive);
73
74         if (this._runningState !== WI.AuditManager.RunningState.Active)
75             return;
76
77         this._runningState = WI.AuditManager.RunningState.Stopping;
78         this.dispatchEventToListeners(WI.AuditTestBase.Event.Stopping);
79     }
80
81     clearResult(options = {})
82     {
83         if (!this._result)
84             return false;
85
86         this._result = null;
87
88         if (!options.suppressResultClearedEvent)
89             this.dispatchEventToListeners(WI.AuditTestBase.Event.ResultCleared);
90
91         return true;
92     }
93
94     saveIdentityToCookie(cookie)
95     {
96         cookie["audit-" + this.constructor.TypeIdentifier + "-name"] = this._name;
97     }
98
99     toJSON()
100     {
101         let json = {
102             type: this.constructor.TypeIdentifier,
103             name: this._name,
104         };
105         if (this._description)
106             json.description = this._description;
107         return json;
108     }
109
110     // Protected
111
112     async run()
113     {
114         throw WI.NotImplementedError.subclassMustOverride();
115     }
116 };
117
118 WI.AuditTestBase.Event = {
119     Completed: "audit-test-base-completed",
120     Progress: "audit-test-base-progress",
121     ResultCleared: "audit-test-base-result-cleared",
122     Scheduled: "audit-test-base-scheduled",
123     Stopping: "audit-test-base-stopping",
124 };