2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
33 * @implements {WebInspector.ProjectDelegate}
34 * @param {!WebInspector.Workspace} workspace
36 * @param {!WebInspector.projectTypes} type
38 WebInspector.ContentProviderBasedProjectDelegate = function(workspace, id, type)
41 /** @type {!Object.<string, !WebInspector.ContentProvider>} */
42 this._contentProviders = {};
43 this._workspace = workspace;
45 this._projectStore = workspace.addProject(id, this);
48 WebInspector.ContentProviderBasedProjectDelegate.prototype = {
60 displayName: function()
62 // Overriddden by subclasses
67 * @param {string} path
68 * @param {function(?Date, ?number)} callback
70 requestMetadata: function(path, callback)
76 * @param {string} path
77 * @param {function(?string)} callback
79 requestFileContent: function(path, callback)
81 var contentProvider = this._contentProviders[path];
82 contentProvider.requestContent(callback);
85 * @param {?string} content
86 * @param {boolean} encoded
87 * @param {string} mimeType
89 function innerCallback(content, encoded, mimeType)
98 canSetFileContent: function()
104 * @param {string} path
105 * @param {string} newContent
106 * @param {function(?string)} callback
108 setFileContent: function(path, newContent, callback)
116 canRename: function()
122 * @param {string} path
123 * @param {string} newName
124 * @param {function(boolean, string=, string=, string=, !WebInspector.ResourceType=)} callback
126 rename: function(path, newName, callback)
128 this.performRename(path, newName, innerCallback.bind(this));
131 * @param {boolean} success
132 * @param {string=} newName
133 * @this {WebInspector.ContentProviderBasedProjectDelegate}
135 function innerCallback(success, newName)
138 this._updateName(path, /** @type {string} */ (newName));
139 callback(success, newName);
144 * @param {string} path
146 refresh: function(path)
151 * @param {string} path
153 excludeFolder: function(path)
158 * @param {string} path
159 * @param {?string} name
160 * @param {string} content
161 * @param {function(?string)} callback
163 createFile: function(path, name, content, callback)
168 * @param {string} path
170 deleteFile: function(path)
179 * @param {string} path
180 * @param {string} newName
181 * @param {function(boolean, string=)} callback
183 performRename: function(path, newName, callback)
189 * @param {string} path
190 * @param {string} newName
192 _updateName: function(path, newName)
195 var copyOfPath = path.split("/");
196 copyOfPath[copyOfPath.length - 1] = newName;
197 var newPath = copyOfPath.join("/");
198 this._contentProviders[newPath] = this._contentProviders[oldPath];
199 delete this._contentProviders[oldPath];
203 * @param {string} path
204 * @param {string} query
205 * @param {boolean} caseSensitive
206 * @param {boolean} isRegex
207 * @param {function(!Array.<!WebInspector.ContentProvider.SearchMatch>)} callback
209 searchInFileContent: function(path, query, caseSensitive, isRegex, callback)
211 var contentProvider = this._contentProviders[path];
212 contentProvider.searchInContent(query, caseSensitive, isRegex, callback);
216 * @param {!WebInspector.ProjectSearchConfig} searchConfig
217 * @param {!WebInspector.Progress} progress
218 * @param {function(!Array.<string>)} callback
220 findFilesMatchingSearchRequest: function(searchConfig, progress, callback)
223 var paths = Object.keys(this._contentProviders);
224 var totalCount = paths.length;
225 if (totalCount === 0) {
226 // searchInContent should call back later.
227 setTimeout(doneCallback, 0);
231 paths = paths.filter(searchConfig.filePathMatchesFileQuery.bind(searchConfig));
232 var barrier = new CallbackBarrier();
233 progress.setTotalWork(paths.length);
234 for (var i = 0; i < paths.length; ++i)
235 searchInContent.call(this, paths[i], barrier.createCallback(searchInContentCallback.bind(null, paths[i])));
236 barrier.callWhenDone(doneCallback);
239 * @param {string} path
240 * @param {function(boolean)} callback
241 * @this {WebInspector.ContentProviderBasedProjectDelegate}
243 function searchInContent(path, callback)
245 var queriesToRun = searchConfig.queries().slice();
246 searchNextQuery.call(this);
249 * @this {WebInspector.ContentProviderBasedProjectDelegate}
251 function searchNextQuery()
253 if (!queriesToRun.length) {
257 var query = queriesToRun.shift();
258 this._contentProviders[path].searchInContent(query, !searchConfig.ignoreCase(), searchConfig.isRegex(), contentCallback.bind(this));
262 * @param {!Array.<!WebInspector.ContentProvider.SearchMatch>} searchMatches
263 * @this {WebInspector.ContentProviderBasedProjectDelegate}
265 function contentCallback(searchMatches)
267 if (!searchMatches.length) {
271 searchNextQuery.call(this);
276 * @param {string} path
277 * @param {boolean} matches
279 function searchInContentCallback(path, matches)
286 function doneCallback()
294 * @param {!WebInspector.Progress} progress
296 indexContent: function(progress)
298 setTimeout(progress.done.bind(progress), 0);
302 * @param {string} parentPath
303 * @param {string} name
304 * @param {string} url
305 * @param {!WebInspector.ContentProvider} contentProvider
308 addContentProvider: function(parentPath, name, url, contentProvider)
310 var path = parentPath ? parentPath + "/" + name : name;
311 if (this._contentProviders[path])
313 var fileDescriptor = new WebInspector.FileDescriptor(parentPath, name, url, url, contentProvider.contentType());
314 this._contentProviders[path] = contentProvider;
315 this._projectStore.addFile(fileDescriptor);
320 * @param {string} path
322 removeFile: function(path)
324 delete this._contentProviders[path];
325 this._projectStore.removeFile(path);
329 * @return {!Object.<string, !WebInspector.ContentProvider>}
331 contentProviders: function()
333 return this._contentProviders;
338 this._contentProviders = {};
339 this._workspace.removeProject(this._id);
340 this._projectStore = this._workspace.addProject(this._id, this);