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