Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / devtools / front_end / Database.js
1 /*
2  * Copyright (C) 2007, 2008 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  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
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
15  *     from this software without specific prior written permission.
16  *
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
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
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
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
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
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 /**
30  * @constructor
31  * @param {!WebInspector.DatabaseModel} model
32  */
33 WebInspector.Database = function(model, id, domain, name, version)
34 {
35     this._model = model;
36     this._id = id;
37     this._domain = domain;
38     this._name = name;
39     this._version = version;
40 }
41
42 WebInspector.Database.prototype = {
43     /** @return {string} */
44     get id()
45     {
46         return this._id;
47     },
48
49     /** @return {string} */
50     get name()
51     {
52         return this._name;
53     },
54
55     set name(x)
56     {
57         this._name = x;
58     },
59
60     /** @return {string} */
61     get version()
62     {
63         return this._version;
64     },
65
66     set version(x)
67     {
68         this._version = x;
69     },
70
71     /** @return {string} */
72     get domain()
73     {
74         return this._domain;
75     },
76
77     set domain(x)
78     {
79         this._domain = x;
80     },
81
82     /**
83      * @param {function(!Array.<string>)} callback
84      */
85     getTableNames: function(callback)
86     {
87         function sortingCallback(error, names)
88         {
89             if (!error)
90                 callback(names.sort());
91         }
92         DatabaseAgent.getDatabaseTableNames(this._id, sortingCallback);
93     },
94
95     /**
96      * @param {string} query
97      * @param {function(!Array.<string>=, !Array.<*>=)} onSuccess
98      * @param {function(string)} onError
99      */
100     executeSql: function(query, onSuccess, onError)
101     {
102         /**
103          * @param {?Protocol.Error} error
104          * @param {!Array.<string>=} columnNames
105          * @param {!Array.<*>=} values
106          * @param {!DatabaseAgent.Error=} errorObj
107          */
108         function callback(error, columnNames, values, errorObj)
109         {
110             if (error) {
111                 onError(error);
112                 return;
113             }
114             if (errorObj) {
115                 var message;
116                 if (errorObj.message)
117                     message = errorObj.message;
118                 else if (errorObj.code == 2)
119                     message = WebInspector.UIString("Database no longer has expected version.");
120                 else
121                     message = WebInspector.UIString("An unexpected error %s occurred.", errorObj.code);
122                 onError(message);
123                 return;
124             }
125             onSuccess(columnNames, values);
126         }
127         DatabaseAgent.executeSQL(this._id, query, callback);
128     }
129 }
130
131 /**
132  * @constructor
133  * @extends {WebInspector.Object}
134  */
135 WebInspector.DatabaseModel = function()
136 {
137     this._databases = [];
138     InspectorBackend.registerDatabaseDispatcher(new WebInspector.DatabaseDispatcher(this));
139     DatabaseAgent.enable();
140 }
141
142 WebInspector.DatabaseModel.Events = {
143     DatabaseAdded: "DatabaseAdded"
144 }
145
146 WebInspector.DatabaseModel.prototype = {
147     /**
148      * @return {!Array.<!WebInspector.Database>}
149      */
150     databases: function()
151     {
152         var result = [];
153         for (var databaseId in this._databases)
154             result.push(this._databases[databaseId]);
155         return result;
156     },
157
158     /**
159      * @param {!DatabaseAgent.DatabaseId} databaseId
160      * @return {!WebInspector.Database}
161      */
162     databaseForId: function(databaseId)
163     {
164         return this._databases[databaseId];
165     },
166
167     /**
168      * @param {!WebInspector.Database} database
169      */
170     _addDatabase: function(database)
171     {
172         this._databases.push(database);
173         this.dispatchEventToListeners(WebInspector.DatabaseModel.Events.DatabaseAdded, database);
174     },
175
176     __proto__: WebInspector.Object.prototype
177 }
178
179 /**
180  * @constructor
181  * @implements {DatabaseAgent.Dispatcher}
182  * @param {!WebInspector.DatabaseModel} model
183  */
184 WebInspector.DatabaseDispatcher = function(model)
185 {
186     this._model = model;
187 }
188
189 WebInspector.DatabaseDispatcher.prototype = {
190     /**
191      * @param {!DatabaseAgent.Database} payload
192      */
193     addDatabase: function(payload)
194     {
195         this._model._addDatabase(new WebInspector.Database(
196             this._model,
197             payload.id,
198             payload.domain,
199             payload.name,
200             payload.version));
201     }
202 }
203
204 /**
205  * @type {!WebInspector.DatabaseModel}
206  */
207 WebInspector.databaseModel;