Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / devtools / front_end / sdk / ContentProviderBasedProjectDelegate.js
1 /*
2  * Copyright (C) 2013 Google 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 are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31  /**
32  * @constructor
33  * @implements {WebInspector.ProjectDelegate}
34  * @param {!WebInspector.Workspace} workspace
35  * @param {string} id
36  * @param {!WebInspector.projectTypes} type
37  */
38 WebInspector.ContentProviderBasedProjectDelegate = function(workspace, id, type)
39 {
40     this._type = type;
41     /** @type {!Object.<string, !WebInspector.ContentProvider>} */
42     this._contentProviders = {};
43     this._projectStore = workspace.addProject(id, this);
44 }
45
46 WebInspector.ContentProviderBasedProjectDelegate.prototype = {
47     /**
48      * @return {string}
49      */
50     type: function()
51     {
52         return this._type;
53     },
54
55     /**
56      * @return {string}
57      */
58     displayName: function()
59     {
60         // Overriddden by subclasses
61         return "";
62     },
63
64     /**
65      * @param {string} path
66      * @param {function(?Date, ?number)} callback
67      */
68     requestMetadata: function(path, callback)
69     {
70         callback(null, null);
71     },
72
73     /**
74      * @param {string} path
75      * @param {function(?string)} callback
76      */
77     requestFileContent: function(path, callback)
78     {
79         var contentProvider = this._contentProviders[path];
80         contentProvider.requestContent(callback);
81
82         /**
83          * @param {?string} content
84          * @param {boolean} encoded
85          * @param {string} mimeType
86          */
87         function innerCallback(content, encoded, mimeType)
88         {
89             callback(content);
90         }
91     },
92
93     /**
94      * @return {boolean}
95      */
96     canSetFileContent: function()
97     {
98         return false;
99     },
100
101     /**
102      * @param {string} path
103      * @param {string} newContent
104      * @param {function(?string)} callback
105      */
106     setFileContent: function(path, newContent, callback)
107     {
108         callback(null);
109     },
110
111     /**
112      * @return {boolean}
113      */
114     canRename: function()
115     {
116         return false;
117     },
118
119     /**
120      * @param {string} path
121      * @param {string} newName
122      * @param {function(boolean, string=, string=, string=, !WebInspector.ResourceType=)} callback
123      */
124     rename: function(path, newName, callback)
125     {
126         this.performRename(path, newName, innerCallback.bind(this));
127
128         /**
129          * @param {boolean} success
130          * @param {string=} newName
131          * @this {WebInspector.ContentProviderBasedProjectDelegate}
132          */
133         function innerCallback(success, newName)
134         {
135             if (success)
136                 this._updateName(path, /** @type {string} */ (newName));
137             callback(success, newName);
138         }
139     },
140
141     /**
142      * @param {string} path
143      */
144     refresh: function(path)
145     {
146     },
147
148     /**
149      * @param {string} path
150      */
151     excludeFolder: function(path)
152     {
153     },
154
155     /**
156      * @param {string} path
157      * @param {?string} name
158      * @param {string} content
159      * @param {function(?string)} callback
160      */
161     createFile: function(path, name, content, callback)
162     {
163     },
164
165     /**
166      * @param {string} path
167      */
168     deleteFile: function(path)
169     {
170     },
171
172     remove: function()
173     {
174     },
175
176     /**
177      * @param {string} path
178      * @param {string} newName
179      * @param {function(boolean, string=)} callback
180      */
181     performRename: function(path, newName, callback)
182     {
183         callback(false);
184     },
185
186     /**
187      * @param {string} path
188      * @param {string} newName
189      */
190     _updateName: function(path, newName)
191     {
192         var oldPath = path;
193         var copyOfPath = path.split("/");
194         copyOfPath[copyOfPath.length - 1] = newName;
195         var newPath = copyOfPath.join("/");
196         this._contentProviders[newPath] = this._contentProviders[oldPath];
197         delete this._contentProviders[oldPath];
198     },
199
200     /**
201      * @param {string} path
202      * @param {string} query
203      * @param {boolean} caseSensitive
204      * @param {boolean} isRegex
205      * @param {function(!Array.<!WebInspector.ContentProvider.SearchMatch>)} callback
206      */
207     searchInFileContent: function(path, query, caseSensitive, isRegex, callback)
208     {
209         var contentProvider = this._contentProviders[path];
210         contentProvider.searchInContent(query, caseSensitive, isRegex, callback);
211     },
212
213     /**
214      * @param {!WebInspector.ProjectSearchConfig} searchConfig
215      * @param {!WebInspector.Progress} progress
216      * @param {function(!Array.<string>)} callback
217      */
218     findFilesMatchingSearchRequest: function(searchConfig, progress, callback)
219     {
220         var result = [];
221         var paths = Object.keys(this._contentProviders);
222         var totalCount = paths.length;
223         if (totalCount === 0) {
224             // searchInContent should call back later.
225             setTimeout(doneCallback, 0);
226             return;
227         }
228
229         paths = paths.filter(searchConfig.filePathMatchesFileQuery.bind(searchConfig));
230         var barrier = new CallbackBarrier();
231         progress.setTotalWork(paths.length);
232         for (var i = 0; i < paths.length; ++i)
233             searchInContent.call(this, paths[i], barrier.createCallback(searchInContentCallback.bind(null, paths[i])));
234         barrier.callWhenDone(doneCallback);
235
236         /**
237          * @param {string} path
238          * @param {function(boolean)} callback
239          * @this {WebInspector.ContentProviderBasedProjectDelegate}
240          */
241         function searchInContent(path, callback)
242         {
243             var queriesToRun = searchConfig.queries().slice();
244             searchNextQuery.call(this);
245
246             /**
247              * @this {WebInspector.ContentProviderBasedProjectDelegate}
248              */
249             function searchNextQuery()
250             {
251                 if (!queriesToRun.length) {
252                     callback(true);
253                     return;
254                 }
255                 var query = queriesToRun.shift();
256                 this._contentProviders[path].searchInContent(query, !searchConfig.ignoreCase(), searchConfig.isRegex(), contentCallback.bind(this));
257             }
258
259             /**
260              * @param {!Array.<!WebInspector.ContentProvider.SearchMatch>} searchMatches
261              * @this {WebInspector.ContentProviderBasedProjectDelegate}
262              */
263             function contentCallback(searchMatches)
264             {
265                 if (!searchMatches.length) {
266                     callback(false);
267                     return;
268                 }
269                 searchNextQuery.call(this);
270             }
271         }
272
273         /**
274          * @param {string} path
275          * @param {boolean} matches
276          */
277         function searchInContentCallback(path, matches)
278         {
279             if (matches)
280                 result.push(path);
281             progress.worked(1);
282         }
283
284         function doneCallback()
285         {
286             callback(result);
287             progress.done();
288         }
289     },
290
291     /**
292      * @param {!WebInspector.Progress} progress
293      */
294     indexContent: function(progress)
295     {
296         setTimeout(progress.done.bind(progress), 0);
297     },
298
299     /**
300      * @param {string} parentPath
301      * @param {string} name
302      * @param {string} url
303      * @param {!WebInspector.ContentProvider} contentProvider
304      * @return {string}
305      */
306     addContentProvider: function(parentPath, name, url, contentProvider)
307     {
308         var path = parentPath ? parentPath + "/" + name : name;
309         if (this._contentProviders[path])
310             return path;
311         var fileDescriptor = new WebInspector.FileDescriptor(parentPath, name, url, url, contentProvider.contentType());
312         this._contentProviders[path] = contentProvider;
313         this._projectStore.addFile(fileDescriptor);
314         return path;
315     },
316
317     /**
318      * @param {string} path
319      */
320     removeFile: function(path)
321     {
322         delete this._contentProviders[path];
323         this._projectStore.removeFile(path);
324     },
325
326     /**
327      * @return {!Object.<string, !WebInspector.ContentProvider>}
328      */
329     contentProviders: function()
330     {
331         return this._contentProviders;
332     },
333
334     reset: function()
335     {
336         this._contentProviders = {};
337         this._projectStore.reset();
338     }
339 }