e12cd7688851d17ce9d379364c3e40a77a6f0fff
[WebKit-https.git] / Source / WebInspectorUI / UserInterface / Models / Canvas.js
1 /*
2  * Copyright (C) 2017 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 WebInspector.Canvas = class Canvas extends WebInspector.Object
27 {
28     constructor(identifier, contextType, frame, {domNode, cssCanvasName} = {})
29     {
30         super();
31
32         console.assert(identifier);
33         console.assert(contextType);
34         console.assert(frame instanceof WebInspector.Frame);
35
36         this._identifier = identifier;
37         this._contextType = contextType;
38         this._frame = frame;
39         this._domNode = domNode || null;
40         this._cssCanvasName = cssCanvasName || "";
41     }
42
43     // Static
44
45     static fromPayload(payload)
46     {
47         let contextType = null;
48         switch (payload.contextType) {
49         case CanvasAgent.ContextType.Canvas2D:
50             contextType = WebInspector.Canvas.ContextType.Canvas2D;
51             break;
52         case CanvasAgent.ContextType.WebGL:
53             contextType = WebInspector.Canvas.ContextType.WebGL;
54             break;
55         default:
56             console.error("Invalid canvas context type", payload.contextType);
57         }
58
59         let frame = WebInspector.frameResourceManager.frameForIdentifier(payload.frameId);
60         return new WebInspector.Canvas(payload.canvasId, contextType, frame, {
61             domNode: payload.nodeId ? WebInspector.domTreeManager.nodeForId(payload.nodeId) : null,
62             cssCanvasName: payload.cssCanvasName,
63         });
64     }
65
66     static displayNameForContextType(contextType)
67     {
68         switch (contextType) {
69         case WebInspector.Canvas.ContextType.Canvas2D:
70             return WebInspector.UIString("2D");
71         case WebInspector.Canvas.ContextType.WebGL:
72             return WebInspector.unlocalizedString("WebGL");
73         default:
74             console.error("Invalid canvas context type", contextType);
75         }
76     }
77
78     static resetUniqueDisplayNameNumbers()
79     {
80         WebInspector.Canvas._nextUniqueDisplayNameNumber = 1;
81     }
82
83     // Public
84
85     get identifier() { return this._identifier; }
86     get contextType() { return this._contextType; }
87     get frame() { return this._frame; }
88     get cssCanvasName() { return this._cssCanvasName; }
89
90     get displayName()
91     {
92         if (this._cssCanvasName)
93             return WebInspector.UIString("CSS canvas ā€œ%sā€").format(this._cssCanvasName);
94
95         if (this._domNode) {
96             let idSelector = this._domNode.escapedIdSelector;
97             if (idSelector)
98                 return WebInspector.UIString("Canvas %s").format(idSelector);
99         }
100
101         if (!this._uniqueDisplayNameNumber)
102             this._uniqueDisplayNameNumber = this.constructor._nextUniqueDisplayNameNumber++;
103         return WebInspector.UIString("Canvas %d").format(this._uniqueDisplayNameNumber);
104     }
105
106     requestNode(callback)
107     {
108         if (this._domNode) {
109             callback(this._domNode);
110             return;
111         }
112
113         WebInspector.domTreeManager.requestDocument((document) => {
114             CanvasAgent.requestNode(this._identifier, (error, nodeId) => {
115                 if (error) {
116                     callback(null);
117                     return;
118                 }
119
120                 this._domNode = WebInspector.domTreeManager.nodeForId(nodeId);
121                 callback(this._domNode);
122             });
123         });
124     }
125
126     requestContent(callback)
127     {
128         CanvasAgent.requestContent(this._identifier, (error, content) => {
129             if (error) {
130                 callback(null);
131                 return;
132             }
133
134             callback(content);
135         });
136     }
137
138     saveIdentityToCookie(cookie)
139     {
140         cookie[WebInspector.Canvas.FrameURLCookieKey] = this._frame.url.hash;
141
142         if (this._cssCanvasName)
143             cookie[WebInspector.Canvas.CSSCanvasNameCookieKey] = this._cssCanvasName;
144         else if (this._domNode)
145             cookie[WebInspector.Canvas.NodePathCookieKey] = this._domNode.path;
146
147     }
148 };
149
150 WebInspector.Canvas._nextUniqueDisplayNameNumber = 1;
151
152 WebInspector.Canvas.FrameURLCookieKey = "canvas-frame-url";
153 WebInspector.Canvas.CSSCanvasNameCookieKey = "canvas-css-canvas-name";
154
155 WebInspector.Canvas.ContextType = {
156     Canvas2D: "canvas-2d",
157     WebGL: "webgl",
158 };
159
160 WebInspector.Canvas.ResourceSidebarType = "resource-type-canvas";