Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / devtools / front_end / bindings / ContentProviderBasedProjectDelegate.js
blob692e26bfe521f5c41297ecc86b545f91130dc484
1 /*
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
6 * met:
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.
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.
31 /**
32 * @constructor
33 * @extends {WebInspector.Object}
34 * @implements {WebInspector.ProjectDelegate}
35 * @param {!WebInspector.Workspace} workspace
36 * @param {string} id
37 * @param {!WebInspector.projectTypes} type
39 WebInspector.ContentProviderBasedProjectDelegate = function(workspace, id, type)
41 WebInspector.Object.call(this);
42 this._type = type;
43 /** @type {!Object.<string, !WebInspector.ContentProvider>} */
44 this._contentProviders = {};
45 this._workspace = workspace;
46 this._id = id;
47 workspace.addProject(id, this);
50 WebInspector.ContentProviderBasedProjectDelegate.prototype = {
51 /**
52 * @override
53 * @return {string}
55 type: function()
57 return this._type;
60 /**
61 * @override
62 * @return {string}
64 displayName: function()
66 // Overridden by subclasses
67 return "";
70 /**
71 * @override
72 * @return {string}
74 url: function()
76 // Overridden by subclasses
77 return "";
80 /**
81 * @override
82 * @param {string} path
83 * @param {function(?Date, ?number)} callback
85 requestMetadata: function(path, callback)
87 callback(null, null);
90 /**
91 * @override
92 * @param {string} path
93 * @param {function(?string)} callback
95 requestFileContent: function(path, callback)
97 var contentProvider = this._contentProviders[path];
98 contentProvider.requestContent(callback);
101 * @param {?string} content
102 * @param {boolean} encoded
103 * @param {string} mimeType
105 function innerCallback(content, encoded, mimeType)
107 callback(content);
112 * @override
113 * @return {boolean}
115 canSetFileContent: function()
117 return false;
121 * @override
122 * @param {string} path
123 * @param {string} newContent
124 * @param {function(?string)} callback
126 setFileContent: function(path, newContent, callback)
128 callback(null);
132 * @override
133 * @return {boolean}
135 canRename: function()
137 return false;
141 * @override
142 * @param {string} path
143 * @param {string} newName
144 * @param {function(boolean, string=, string=, string=, !WebInspector.ResourceType=)} callback
146 rename: function(path, newName, callback)
148 this.performRename(path, newName, innerCallback.bind(this));
151 * @param {boolean} success
152 * @param {string=} newName
153 * @this {WebInspector.ContentProviderBasedProjectDelegate}
155 function innerCallback(success, newName)
157 if (success)
158 this._updateName(path, /** @type {string} */ (newName));
159 callback(success, newName);
164 * @override
165 * @param {string} path
166 * @param {function()=} callback
168 refresh: function(path, callback)
170 if (callback)
171 callback();
175 * @override
176 * @param {string} path
178 excludeFolder: function(path)
183 * @override
184 * @param {string} path
185 * @param {?string} name
186 * @param {string} content
187 * @param {function(?string)} callback
189 createFile: function(path, name, content, callback)
194 * @override
195 * @param {string} path
197 deleteFile: function(path)
202 * @override
204 remove: function()
209 * @param {string} path
210 * @param {string} newName
211 * @param {function(boolean, string=)} callback
213 performRename: function(path, newName, callback)
215 callback(false);
219 * @param {string} path
220 * @param {string} newName
222 _updateName: function(path, newName)
224 var oldPath = path;
225 var copyOfPath = path.split("/");
226 copyOfPath[copyOfPath.length - 1] = newName;
227 var newPath = copyOfPath.join("/");
228 this._contentProviders[newPath] = this._contentProviders[oldPath];
229 delete this._contentProviders[oldPath];
233 * @override
234 * @param {string} path
235 * @param {string} query
236 * @param {boolean} caseSensitive
237 * @param {boolean} isRegex
238 * @param {function(!Array.<!WebInspector.ContentProvider.SearchMatch>)} callback
240 searchInFileContent: function(path, query, caseSensitive, isRegex, callback)
242 var contentProvider = this._contentProviders[path];
243 contentProvider.searchInContent(query, caseSensitive, isRegex, callback);
247 * @override
248 * @param {!WebInspector.ProjectSearchConfig} searchConfig
249 * @param {!Array.<string>} filesMathingFileQuery
250 * @param {!WebInspector.Progress} progress
251 * @param {function(!Array.<string>)} callback
253 findFilesMatchingSearchRequest: function(searchConfig, filesMathingFileQuery, progress, callback)
255 var result = [];
256 var paths = filesMathingFileQuery;
257 var totalCount = paths.length;
258 if (totalCount === 0) {
259 // searchInContent should call back later.
260 setTimeout(doneCallback, 0);
261 return;
264 var barrier = new CallbackBarrier();
265 progress.setTotalWork(paths.length);
266 for (var i = 0; i < paths.length; ++i)
267 searchInContent.call(this, paths[i], barrier.createCallback(searchInContentCallback.bind(null, paths[i])));
268 barrier.callWhenDone(doneCallback);
271 * @param {string} path
272 * @param {function(boolean)} callback
273 * @this {WebInspector.ContentProviderBasedProjectDelegate}
275 function searchInContent(path, callback)
277 var queriesToRun = searchConfig.queries().slice();
278 searchNextQuery.call(this);
281 * @this {WebInspector.ContentProviderBasedProjectDelegate}
283 function searchNextQuery()
285 if (!queriesToRun.length) {
286 callback(true);
287 return;
289 var query = queriesToRun.shift();
290 this._contentProviders[path].searchInContent(query, !searchConfig.ignoreCase(), searchConfig.isRegex(), contentCallback.bind(this));
294 * @param {!Array.<!WebInspector.ContentProvider.SearchMatch>} searchMatches
295 * @this {WebInspector.ContentProviderBasedProjectDelegate}
297 function contentCallback(searchMatches)
299 if (!searchMatches.length) {
300 callback(false);
301 return;
303 searchNextQuery.call(this);
308 * @param {string} path
309 * @param {boolean} matches
311 function searchInContentCallback(path, matches)
313 if (matches)
314 result.push(path);
315 progress.worked(1);
318 function doneCallback()
320 callback(result);
321 progress.done();
326 * @override
327 * @param {!WebInspector.Progress} progress
329 indexContent: function(progress)
331 setTimeout(progress.done.bind(progress), 0);
335 * @param {string} parentPath
336 * @param {string} name
337 * @param {string} originURL
338 * @param {string} url
339 * @param {!WebInspector.ContentProvider} contentProvider
340 * @return {string}
342 addContentProvider: function(parentPath, name, originURL, url, contentProvider)
344 var path = parentPath ? parentPath + "/" + name : name;
345 if (this._contentProviders[path])
346 return path;
347 var fileDescriptor = new WebInspector.FileDescriptor(parentPath, name, originURL, url, contentProvider.contentType());
348 this._contentProviders[path] = contentProvider;
349 this.dispatchEventToListeners(WebInspector.ProjectDelegate.Events.FileAdded, fileDescriptor);
350 return path;
354 * @param {string} path
356 removeFile: function(path)
358 delete this._contentProviders[path];
359 this.dispatchEventToListeners(WebInspector.ProjectDelegate.Events.FileRemoved, path);
363 * @return {!Object.<string, !WebInspector.ContentProvider>}
365 contentProviders: function()
367 return this._contentProviders;
370 reset: function()
372 this._contentProviders = {};
373 this._workspace.removeProject(this._id);
374 this._workspace.addProject(this._id, this);
377 __proto__: WebInspector.Object.prototype