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._projectStore = workspace.addProject(id, this);
46 WebInspector.ContentProviderBasedProjectDelegate.prototype = {
58 displayName: function()
60 // Overriddden by subclasses
65 * @param {string} path
66 * @param {function(?Date, ?number)} callback
68 requestMetadata: function(path, callback)
74 * @param {string} path
75 * @param {function(?string)} callback
77 requestFileContent: function(path, callback)
79 var contentProvider = this._contentProviders[path];
80 contentProvider.requestContent(callback);
83 * @param {?string} content
84 * @param {boolean} encoded
85 * @param {string} mimeType
87 function innerCallback(content, encoded, mimeType)
96 canSetFileContent: function()
102 * @param {string} path
103 * @param {string} newContent
104 * @param {function(?string)} callback
106 setFileContent: function(path, newContent, callback)
114 canRename: function()
120 * @param {string} path
121 * @param {string} newName
122 * @param {function(boolean, string=, string=, string=, !WebInspector.ResourceType=)} callback
124 rename: function(path, newName, callback)
126 this.performRename(path, newName, innerCallback.bind(this));
129 * @param {boolean} success
130 * @param {string=} newName
131 * @this {WebInspector.ContentProviderBasedProjectDelegate}
133 function innerCallback(success, newName)
136 this._updateName(path, /** @type {string} */ (newName));
137 callback(success, newName);
142 * @param {string} path
144 refresh: function(path)
149 * @param {string} path
151 excludeFolder: function(path)
156 * @param {string} path
157 * @param {?string} name
158 * @param {string} content
159 * @param {function(?string)} callback
161 createFile: function(path, name, content, callback)
166 * @param {string} path
168 deleteFile: function(path)
177 * @param {string} path
178 * @param {string} newName
179 * @param {function(boolean, string=)} callback
181 performRename: function(path, newName, callback)
187 * @param {string} path
188 * @param {string} newName
190 _updateName: function(path, newName)
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];
201 * @param {string} path
202 * @param {string} query
203 * @param {boolean} caseSensitive
204 * @param {boolean} isRegex
205 * @param {function(!Array.<!WebInspector.ContentProvider.SearchMatch>)} callback
207 searchInFileContent: function(path, query, caseSensitive, isRegex, callback)
209 var contentProvider = this._contentProviders[path];
210 contentProvider.searchInContent(query, caseSensitive, isRegex, callback);
214 * @param {!WebInspector.ProjectSearchConfig} searchConfig
215 * @param {!WebInspector.Progress} progress
216 * @param {function(!Array.<string>)} callback
218 findFilesMatchingSearchRequest: function(searchConfig, progress, callback)
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);
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);
237 * @param {string} path
238 * @param {function(boolean)} callback
239 * @this {WebInspector.ContentProviderBasedProjectDelegate}
241 function searchInContent(path, callback)
243 var queriesToRun = searchConfig.queries().slice();
244 searchNextQuery.call(this);
247 * @this {WebInspector.ContentProviderBasedProjectDelegate}
249 function searchNextQuery()
251 if (!queriesToRun.length) {
255 var query = queriesToRun.shift();
256 this._contentProviders[path].searchInContent(query, !searchConfig.ignoreCase(), searchConfig.isRegex(), contentCallback.bind(this));
260 * @param {!Array.<!WebInspector.ContentProvider.SearchMatch>} searchMatches
261 * @this {WebInspector.ContentProviderBasedProjectDelegate}
263 function contentCallback(searchMatches)
265 if (!searchMatches.length) {
269 searchNextQuery.call(this);
274 * @param {string} path
275 * @param {boolean} matches
277 function searchInContentCallback(path, matches)
284 function doneCallback()
292 * @param {!WebInspector.Progress} progress
294 indexContent: function(progress)
296 setTimeout(progress.done.bind(progress), 0);
300 * @param {string} parentPath
301 * @param {string} name
302 * @param {string} url
303 * @param {!WebInspector.ContentProvider} contentProvider
306 addContentProvider: function(parentPath, name, url, contentProvider)
308 var path = parentPath ? parentPath + "/" + name : name;
309 if (this._contentProviders[path])
311 var fileDescriptor = new WebInspector.FileDescriptor(parentPath, name, url, url, contentProvider.contentType());
312 this._contentProviders[path] = contentProvider;
313 this._projectStore.addFile(fileDescriptor);
318 * @param {string} path
320 removeFile: function(path)
322 delete this._contentProviders[path];
323 this._projectStore.removeFile(path);
327 * @return {!Object.<string, !WebInspector.ContentProvider>}
329 contentProviders: function()
331 return this._contentProviders;
336 this._contentProviders = {};
337 this._projectStore.reset();