OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved. | 2 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * | 7 * |
8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
11 * notice, this list of conditions and the following disclaimer in the | 11 * notice, this list of conditions and the following disclaimer in the |
12 * documentation and/or other materials provided with the distribution. | 12 * documentation and/or other materials provided with the distribution. |
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of | 13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of |
14 * its contributors may be used to endorse or promote products derived | 14 * its contributors may be used to endorse or promote products derived |
15 * from this software without specific prior written permission. | 15 * from this software without specific prior written permission. |
16 * | 16 * |
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY | 17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | 18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY | 20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | 21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | 22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | 23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 */ | 27 */ |
28 | |
29 /** | 28 /** |
30 * @constructor | 29 * @unrestricted |
31 * @param {!WebInspector.DatabaseModel} model | |
32 * @param {string} id | |
33 * @param {string} domain | |
34 * @param {string} name | |
35 * @param {string} version | |
36 */ | 30 */ |
37 WebInspector.Database = function(model, id, domain, name, version) | 31 WebInspector.Database = class { |
38 { | 32 /** |
| 33 * @param {!WebInspector.DatabaseModel} model |
| 34 * @param {string} id |
| 35 * @param {string} domain |
| 36 * @param {string} name |
| 37 * @param {string} version |
| 38 */ |
| 39 constructor(model, id, domain, name, version) { |
39 this._model = model; | 40 this._model = model; |
40 this._id = id; | 41 this._id = id; |
41 this._domain = domain; | 42 this._domain = domain; |
42 this._name = name; | 43 this._name = name; |
43 this._version = version; | 44 this._version = version; |
44 }; | 45 } |
45 | 46 |
46 WebInspector.Database.prototype = { | 47 /** @return {string} */ |
47 /** @return {string} */ | 48 get id() { |
48 get id() | 49 return this._id; |
49 { | 50 } |
50 return this._id; | |
51 }, | |
52 | 51 |
53 /** @return {string} */ | 52 /** @return {string} */ |
54 get name() | 53 get name() { |
55 { | 54 return this._name; |
56 return this._name; | 55 } |
57 }, | |
58 | 56 |
59 set name(x) | 57 /** @param {string} x */ |
60 { | 58 set name(x) { |
61 this._name = x; | 59 this._name = x; |
62 }, | 60 } |
63 | 61 |
64 /** @return {string} */ | 62 /** @return {string} */ |
65 get version() | 63 get version() { |
66 { | 64 return this._version; |
67 return this._version; | 65 } |
68 }, | |
69 | 66 |
70 set version(x) | 67 /** @param {string} x */ |
71 { | 68 set version(x) { |
72 this._version = x; | 69 this._version = x; |
73 }, | 70 } |
74 | 71 |
75 /** @return {string} */ | 72 /** @return {string} */ |
76 get domain() | 73 get domain() { |
77 { | 74 return this._domain; |
78 return this._domain; | 75 } |
79 }, | |
80 | 76 |
81 set domain(x) | 77 /** @param {string} x */ |
82 { | 78 set domain(x) { |
83 this._domain = x; | 79 this._domain = x; |
84 }, | 80 } |
85 | 81 |
| 82 /** |
| 83 * @param {function(!Array.<string>)} callback |
| 84 */ |
| 85 getTableNames(callback) { |
| 86 function sortingCallback(error, names) { |
| 87 if (!error) |
| 88 callback(names.sort()); |
| 89 } |
| 90 this._model._agent.getDatabaseTableNames(this._id, sortingCallback); |
| 91 } |
| 92 |
| 93 /** |
| 94 * @param {string} query |
| 95 * @param {function(!Array.<string>=, !Array.<*>=)} onSuccess |
| 96 * @param {function(string)} onError |
| 97 */ |
| 98 executeSql(query, onSuccess, onError) { |
86 /** | 99 /** |
87 * @param {function(!Array.<string>)} callback | 100 * @param {?Protocol.Error} error |
| 101 * @param {!Array.<string>=} columnNames |
| 102 * @param {!Array.<*>=} values |
| 103 * @param {!DatabaseAgent.Error=} errorObj |
88 */ | 104 */ |
89 getTableNames: function(callback) | 105 function callback(error, columnNames, values, errorObj) { |
90 { | 106 if (error) { |
91 function sortingCallback(error, names) | 107 onError(error); |
92 { | 108 return; |
93 if (!error) | 109 } |
94 callback(names.sort()); | 110 if (errorObj) { |
95 } | 111 var message; |
96 this._model._agent.getDatabaseTableNames(this._id, sortingCallback); | 112 if (errorObj.message) |
97 }, | 113 message = errorObj.message; |
98 | 114 else if (errorObj.code === 2) |
99 /** | 115 message = WebInspector.UIString('Database no longer has expected versi
on.'); |
100 * @param {string} query | 116 else |
101 * @param {function(!Array.<string>=, !Array.<*>=)} onSuccess | 117 message = WebInspector.UIString('An unexpected error %s occurred.', er
rorObj.code); |
102 * @param {function(string)} onError | 118 onError(message); |
103 */ | 119 return; |
104 executeSql: function(query, onSuccess, onError) | 120 } |
105 { | 121 onSuccess(columnNames, values); |
106 /** | |
107 * @param {?Protocol.Error} error | |
108 * @param {!Array.<string>=} columnNames | |
109 * @param {!Array.<*>=} values | |
110 * @param {!DatabaseAgent.Error=} errorObj | |
111 */ | |
112 function callback(error, columnNames, values, errorObj) | |
113 { | |
114 if (error) { | |
115 onError(error); | |
116 return; | |
117 } | |
118 if (errorObj) { | |
119 var message; | |
120 if (errorObj.message) | |
121 message = errorObj.message; | |
122 else if (errorObj.code === 2) | |
123 message = WebInspector.UIString("Database no longer has expe
cted version."); | |
124 else | |
125 message = WebInspector.UIString("An unexpected error %s occu
rred.", errorObj.code); | |
126 onError(message); | |
127 return; | |
128 } | |
129 onSuccess(columnNames, values); | |
130 } | |
131 this._model._agent.executeSQL(this._id, query, callback); | |
132 } | 122 } |
| 123 this._model._agent.executeSQL(this._id, query, callback); |
| 124 } |
133 }; | 125 }; |
134 | 126 |
135 /** | 127 /** |
136 * @constructor | 128 * @unrestricted |
137 * @extends {WebInspector.SDKModel} | |
138 * @param {!WebInspector.Target} target | |
139 */ | 129 */ |
140 WebInspector.DatabaseModel = function(target) | 130 WebInspector.DatabaseModel = class extends WebInspector.SDKModel { |
141 { | 131 /** |
142 WebInspector.SDKModel.call(this, WebInspector.DatabaseModel, target); | 132 * @param {!WebInspector.Target} target |
| 133 */ |
| 134 constructor(target) { |
| 135 super(WebInspector.DatabaseModel, target); |
143 | 136 |
144 this._databases = []; | 137 this._databases = []; |
145 this._agent = target.databaseAgent(); | 138 this._agent = target.databaseAgent(); |
146 this.target().registerDatabaseDispatcher(new WebInspector.DatabaseDispatcher
(this)); | 139 this.target().registerDatabaseDispatcher(new WebInspector.DatabaseDispatcher
(this)); |
| 140 } |
| 141 |
| 142 /** |
| 143 * @param {!WebInspector.Target} target |
| 144 * @return {!WebInspector.DatabaseModel} |
| 145 */ |
| 146 static fromTarget(target) { |
| 147 if (!target[WebInspector.DatabaseModel._symbol]) |
| 148 target[WebInspector.DatabaseModel._symbol] = new WebInspector.DatabaseMode
l(target); |
| 149 |
| 150 return target[WebInspector.DatabaseModel._symbol]; |
| 151 } |
| 152 |
| 153 enable() { |
| 154 if (this._enabled) |
| 155 return; |
| 156 this._agent.enable(); |
| 157 this._enabled = true; |
| 158 } |
| 159 |
| 160 disable() { |
| 161 if (!this._enabled) |
| 162 return; |
| 163 this._enabled = false; |
| 164 this._databases = []; |
| 165 this._agent.disable(); |
| 166 this.dispatchEventToListeners(WebInspector.DatabaseModel.Events.DatabasesRem
oved); |
| 167 } |
| 168 |
| 169 /** |
| 170 * @return {!Array.<!WebInspector.Database>} |
| 171 */ |
| 172 databases() { |
| 173 var result = []; |
| 174 for (var database of this._databases) |
| 175 result.push(database); |
| 176 return result; |
| 177 } |
| 178 |
| 179 /** |
| 180 * @param {!WebInspector.Database} database |
| 181 */ |
| 182 _addDatabase(database) { |
| 183 this._databases.push(database); |
| 184 this.dispatchEventToListeners(WebInspector.DatabaseModel.Events.DatabaseAdde
d, database); |
| 185 } |
147 }; | 186 }; |
148 | 187 |
149 /** @enum {symbol} */ | 188 /** @enum {symbol} */ |
150 WebInspector.DatabaseModel.Events = { | 189 WebInspector.DatabaseModel.Events = { |
151 DatabaseAdded: Symbol("DatabaseAdded"), | 190 DatabaseAdded: Symbol('DatabaseAdded'), |
152 DatabasesRemoved: Symbol("DatabasesRemoved") | 191 DatabasesRemoved: Symbol('DatabasesRemoved') |
153 }; | |
154 | |
155 WebInspector.DatabaseModel.prototype = { | |
156 enable: function() | |
157 { | |
158 if (this._enabled) | |
159 return; | |
160 this._agent.enable(); | |
161 this._enabled = true; | |
162 }, | |
163 | |
164 disable: function() | |
165 { | |
166 if (!this._enabled) | |
167 return; | |
168 this._enabled = false; | |
169 this._databases = []; | |
170 this._agent.disable(); | |
171 this.dispatchEventToListeners(WebInspector.DatabaseModel.Events.Database
sRemoved); | |
172 }, | |
173 | |
174 /** | |
175 * @return {!Array.<!WebInspector.Database>} | |
176 */ | |
177 databases: function() | |
178 { | |
179 var result = []; | |
180 for (var database of this._databases) | |
181 result.push(database); | |
182 return result; | |
183 }, | |
184 | |
185 /** | |
186 * @param {!WebInspector.Database} database | |
187 */ | |
188 _addDatabase: function(database) | |
189 { | |
190 this._databases.push(database); | |
191 this.dispatchEventToListeners(WebInspector.DatabaseModel.Events.Database
Added, database); | |
192 }, | |
193 | |
194 __proto__: WebInspector.SDKModel.prototype | |
195 }; | 192 }; |
196 | 193 |
197 /** | 194 /** |
198 * @constructor | |
199 * @implements {DatabaseAgent.Dispatcher} | 195 * @implements {DatabaseAgent.Dispatcher} |
200 * @param {!WebInspector.DatabaseModel} model | 196 * @unrestricted |
201 */ | 197 */ |
202 WebInspector.DatabaseDispatcher = function(model) | 198 WebInspector.DatabaseDispatcher = class { |
203 { | 199 /** |
| 200 * @param {!WebInspector.DatabaseModel} model |
| 201 */ |
| 202 constructor(model) { |
204 this._model = model; | 203 this._model = model; |
| 204 } |
| 205 |
| 206 /** |
| 207 * @override |
| 208 * @param {!DatabaseAgent.Database} payload |
| 209 */ |
| 210 addDatabase(payload) { |
| 211 this._model._addDatabase( |
| 212 new WebInspector.Database(this._model, payload.id, payload.domain, paylo
ad.name, payload.version)); |
| 213 } |
205 }; | 214 }; |
206 | 215 |
207 WebInspector.DatabaseDispatcher.prototype = { | 216 WebInspector.DatabaseModel._symbol = Symbol('DatabaseModel'); |
208 /** | |
209 * @override | |
210 * @param {!DatabaseAgent.Database} payload | |
211 */ | |
212 addDatabase: function(payload) | |
213 { | |
214 this._model._addDatabase(new WebInspector.Database( | |
215 this._model, | |
216 payload.id, | |
217 payload.domain, | |
218 payload.name, | |
219 payload.version)); | |
220 } | |
221 }; | |
222 | 217 |
223 WebInspector.DatabaseModel._symbol = Symbol("DatabaseModel"); | |
224 /** | |
225 * @param {!WebInspector.Target} target | |
226 * @return {!WebInspector.DatabaseModel} | |
227 */ | |
228 WebInspector.DatabaseModel.fromTarget = function(target) | |
229 { | |
230 if (!target[WebInspector.DatabaseModel._symbol]) | |
231 target[WebInspector.DatabaseModel._symbol] = new WebInspector.DatabaseMo
del(target); | |
232 | 218 |
233 return target[WebInspector.DatabaseModel._symbol]; | |
234 }; | |
OLD | NEW |