Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / devtools / front_end / SimpleWorkspaceProvider.js
1 /*
2  * Copyright (C) 2012 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  * @extends {WebInspector.ContentProviderBasedProjectDelegate}
34  * @param {string} name
35  * @param {string} type
36  */
37 WebInspector.SimpleProjectDelegate = function(name, type)
38 {
39     WebInspector.ContentProviderBasedProjectDelegate.call(this, type);
40     this._name = name;
41     this._lastUniqueSuffix = 0;
42 }
43
44 WebInspector.SimpleProjectDelegate.projectId = function(name, type)
45 {
46     var typePrefix = type !== WebInspector.projectTypes.Network ? (type + ":") : "";
47     return typePrefix + name;
48 }
49
50 WebInspector.SimpleProjectDelegate.prototype = {
51     /**
52      * @return {string}
53      */
54     id: function()
55     {
56         return WebInspector.SimpleProjectDelegate.projectId(this._name, this.type());
57     },
58
59     /**
60      * @return {string}
61      */
62     displayName: function()
63     {
64         if (typeof this._displayName !== "undefined")
65             return this._displayName;
66         if (!this._name) {
67             this._displayName = this.type() !== WebInspector.projectTypes.Snippets ? WebInspector.UIString("(no domain)") : "";
68             return this._displayName;
69         }
70         var parsedURL = new WebInspector.ParsedURL(this._name);
71         if (parsedURL.isValid) {
72             this._displayName = parsedURL.host + (parsedURL.port ? (":" + parsedURL.port) : "");
73             if (!this._displayName)
74                 this._displayName = this._name;
75         }
76         else
77             this._displayName = this._name;
78         return this._displayName;
79     },
80
81     /**
82      * @param {string} parentPath
83      * @param {string} name
84      * @param {string} url
85      * @param {!WebInspector.ContentProvider} contentProvider
86      * @param {boolean} isEditable
87      * @param {boolean=} isContentScript
88      * @return {string}
89      */
90     addFile: function(parentPath, name, forceUniquePath, url, contentProvider, isEditable, isContentScript)
91     {
92         if (forceUniquePath)
93             name = this._ensureUniqueName(parentPath, name);
94         return this.addContentProvider(parentPath, name, url, contentProvider, isEditable, isContentScript);
95     },
96
97     /**
98      * @param {string} parentPath
99      * @param {string} name
100      * @return {string}
101      */
102     _ensureUniqueName: function(parentPath, name)
103      {
104         var path = parentPath ? parentPath + "/" + name : name;
105         var uniquePath = path;
106         var suffix = "";
107         var contentProviders = this.contentProviders();
108         while (contentProviders[uniquePath]) {
109             suffix = " (" + (++this._lastUniqueSuffix) + ")";
110             uniquePath = path + suffix;
111         }
112         return name + suffix;
113     },
114
115     __proto__: WebInspector.ContentProviderBasedProjectDelegate.prototype
116 }
117
118 /**
119  * @constructor
120  * @extends {WebInspector.Object}
121  * @param {!WebInspector.Workspace} workspace
122  * @param {string} type
123  */
124 WebInspector.SimpleWorkspaceProvider = function(workspace, type)
125 {
126     this._workspace = workspace;
127     this._type = type;
128     this._simpleProjectDelegates = {};
129 }
130
131 WebInspector.SimpleWorkspaceProvider.prototype = {
132     /**
133      * @param {string} projectName
134      * @return {!WebInspector.SimpleProjectDelegate}
135      */
136     _projectDelegate: function(projectName)
137     {
138         if (this._simpleProjectDelegates[projectName])
139             return this._simpleProjectDelegates[projectName];
140         var simpleProjectDelegate = new WebInspector.SimpleProjectDelegate(projectName, this._type);
141         this._simpleProjectDelegates[projectName] = simpleProjectDelegate;
142         this._workspace.addProject(simpleProjectDelegate);
143         return simpleProjectDelegate;
144     },
145  
146     /**
147      * @param {string} url
148      * @param {!WebInspector.ContentProvider} contentProvider
149      * @param {boolean} isEditable
150      * @param {boolean=} isContentScript
151      * @return {!WebInspector.UISourceCode}
152      */
153     addFileForURL: function(url, contentProvider, isEditable, isContentScript)
154     {
155         return this._innerAddFileForURL(url, contentProvider, isEditable, false, isContentScript);
156     },
157
158     /**
159      * @param {string} url
160      * @param {!WebInspector.ContentProvider} contentProvider
161      * @param {boolean} isEditable
162      * @param {boolean=} isContentScript
163      * @return {!WebInspector.UISourceCode}
164      */
165     addUniqueFileForURL: function(url, contentProvider, isEditable, isContentScript)
166     {
167         return this._innerAddFileForURL(url, contentProvider, isEditable, true, isContentScript);
168     },
169
170     /**
171      * @param {string} url
172      * @param {!WebInspector.ContentProvider} contentProvider
173      * @param {boolean} isEditable
174      * @param {boolean} forceUnique
175      * @param {boolean=} isContentScript
176      * @return {!WebInspector.UISourceCode}
177      */
178     _innerAddFileForURL: function(url, contentProvider, isEditable, forceUnique, isContentScript)
179     {
180         var splitURL = WebInspector.ParsedURL.splitURL(url);
181         var projectName = splitURL[0];
182         var parentPath = splitURL.slice(1, splitURL.length - 1).join("/");
183         var name = splitURL[splitURL.length - 1];
184         var projectDelegate = this._projectDelegate(projectName);
185         var path = projectDelegate.addFile(parentPath, name, forceUnique, url, contentProvider, isEditable, isContentScript);
186         var uiSourceCode = /** @type {!WebInspector.UISourceCode} */ (this._workspace.uiSourceCode(projectDelegate.id(), path));
187         console.assert(uiSourceCode);
188         return uiSourceCode;
189     },
190
191     reset: function()
192     {
193         for (var projectName in this._simpleProjectDelegates)
194             this._simpleProjectDelegates[projectName].reset();
195         this._simpleProjectDelegates = {};
196     },
197     
198     __proto__: WebInspector.Object.prototype
199 }