Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / devtools / front_end / HandlerRegistry.js
1 /*
2  * Copyright (C) 2011 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.Object}
34  */
35 WebInspector.HandlerRegistry = function(setting)
36 {
37     WebInspector.Object.call(this);
38     this._handlers = {};
39     this._setting = setting;
40     this._activeHandler = this._setting.get();
41
42     WebInspector.moduleManager.registerModule("handler-registry");
43 }
44
45 WebInspector.HandlerRegistry.prototype = {
46     get handlerNames()
47     {
48         return Object.getOwnPropertyNames(this._handlers);
49     },
50
51     get activeHandler()
52     {
53         return this._activeHandler;
54     },
55
56     set activeHandler(value)
57     {
58         this._activeHandler = value;
59         this._setting.set(value);
60     },
61
62     /**
63      * @param {!Object} data
64      * @return {boolean}
65      */
66     dispatch: function(data)
67     {
68         return this.dispatchToHandler(this._activeHandler, data);
69     },
70
71     /**
72      * @param {string} name
73      * @param {!Object} data
74      * @return {boolean}
75      */
76     dispatchToHandler: function(name, data)
77     {
78         var handler = this._handlers[name];
79         var result = handler && handler(data);
80         return !!result;
81     },
82
83     registerHandler: function(name, handler)
84     {
85         this._handlers[name] = handler;
86         this.dispatchEventToListeners(WebInspector.HandlerRegistry.EventTypes.HandlersUpdated);
87     },
88
89     unregisterHandler: function(name)
90     {
91         delete this._handlers[name];
92         this.dispatchEventToListeners(WebInspector.HandlerRegistry.EventTypes.HandlersUpdated);
93     },
94
95     /** 
96      * @param {!WebInspector.ContextMenu} contextMenu
97      * @param {!Object} target
98      */
99     _appendContentProviderItems: function(contextMenu, target)
100     {
101         if (!(target instanceof WebInspector.UISourceCode || target instanceof WebInspector.Resource || target instanceof WebInspector.NetworkRequest))
102             return;
103         var contentProvider = /** @type {!WebInspector.ContentProvider} */ (target);
104         if (!contentProvider.contentURL())
105             return;
106
107         contextMenu.appendItem(WebInspector.openLinkExternallyLabel(), WebInspector.openResource.bind(WebInspector, contentProvider.contentURL(), false));
108         // Skip 0th handler, as it's 'Use default panel' one.
109         for (var i = 1; i < this.handlerNames.length; ++i) {
110             var handler = this.handlerNames[i];
111             contextMenu.appendItem(WebInspector.UIString(WebInspector.useLowerCaseMenuTitles() ? "Open using %s" : "Open Using %s", handler),
112                 this.dispatchToHandler.bind(this, handler, { url: contentProvider.contentURL() }));
113         }
114         contextMenu.appendItem(WebInspector.copyLinkAddressLabel(), InspectorFrontendHost.copyText.bind(InspectorFrontendHost, contentProvider.contentURL()));
115
116         if (!contentProvider.contentURL())
117             return;
118
119         var contentType = contentProvider.contentType();
120         if (contentType !== WebInspector.resourceTypes.Document &&
121             contentType !== WebInspector.resourceTypes.Stylesheet &&
122             contentType !== WebInspector.resourceTypes.Script)
123             return;
124
125         /**
126          * @param {boolean} forceSaveAs
127          * @param {?string} content
128          */
129         function doSave(forceSaveAs, content)
130         {
131             var url = contentProvider.contentURL();
132             WebInspector.fileManager.save(url, /** @type {string} */ (content), forceSaveAs);
133             WebInspector.fileManager.close(url);
134         }
135
136         /**
137          * @param {boolean} forceSaveAs
138          * @this {WebInspector.HandlerRegistry}
139          */
140         function save(forceSaveAs)
141         {
142             if (contentProvider instanceof WebInspector.UISourceCode) {
143                 var uiSourceCode = /** @type {!WebInspector.UISourceCode} */ (contentProvider);
144                 uiSourceCode.saveToFileSystem(forceSaveAs);
145                 return;
146             }
147             contentProvider.requestContent(doSave.bind(this, forceSaveAs));
148         }
149
150         contextMenu.appendSeparator();
151         contextMenu.appendItem(WebInspector.UIString("Save"), save.bind(this, false));
152         contextMenu.appendItem(WebInspector.UIString(WebInspector.useLowerCaseMenuTitles() ? "Save as..." : "Save As..."), save.bind(this, true));
153     },
154
155     /** 
156      * @param {!WebInspector.ContextMenu} contextMenu
157      * @param {!Object} target
158      */
159     _appendHrefItems: function(contextMenu, target)
160     {
161         if (!(target instanceof Node))
162             return;
163         var targetNode = /** @type {!Node} */ (target);
164
165         var anchorElement = targetNode.enclosingNodeOrSelfWithClass("webkit-html-resource-link") || targetNode.enclosingNodeOrSelfWithClass("webkit-html-external-link");
166         if (!anchorElement)
167             return;
168
169         var resourceURL = anchorElement.href;
170         if (!resourceURL)
171             return;
172
173         // Add resource-related actions.
174         contextMenu.appendItem(WebInspector.openLinkExternallyLabel(), WebInspector.openResource.bind(WebInspector, resourceURL, false));
175         if (WebInspector.resourceForURL(resourceURL))
176             contextMenu.appendItem(WebInspector.UIString(WebInspector.useLowerCaseMenuTitles() ? "Open link in Resources panel" : "Open Link in Resources Panel"), WebInspector.openResource.bind(null, resourceURL, true));
177         contextMenu.appendItem(WebInspector.copyLinkAddressLabel(), InspectorFrontendHost.copyText.bind(InspectorFrontendHost, resourceURL));
178     },
179
180     __proto__: WebInspector.Object.prototype
181 }
182
183
184 WebInspector.HandlerRegistry.EventTypes = {
185     HandlersUpdated: "HandlersUpdated"
186 }
187
188 /**
189  * @constructor
190  */
191 WebInspector.HandlerSelector = function(handlerRegistry)
192 {
193     this._handlerRegistry = handlerRegistry;
194     this.element = document.createElement("select");
195     this.element.addEventListener("change", this._onChange.bind(this), false);
196     this._update();
197     this._handlerRegistry.addEventListener(WebInspector.HandlerRegistry.EventTypes.HandlersUpdated, this._update.bind(this));
198 }
199
200 WebInspector.HandlerSelector.prototype =
201 {
202     _update: function()
203     {
204         this.element.removeChildren();
205         var names = this._handlerRegistry.handlerNames;
206         var activeHandler = this._handlerRegistry.activeHandler;
207
208         for (var i = 0; i < names.length; ++i) {
209             var option = document.createElement("option");
210             option.textContent = names[i];
211             option.selected = activeHandler === names[i];
212             this.element.appendChild(option);
213         }
214         this.element.disabled = names.length <= 1;
215     },
216
217     _onChange: function(event)
218     {
219         var value = event.target.value;
220         this._handlerRegistry.activeHandler = value;
221     }
222 }
223
224 /**
225  * @constructor
226  * @implements {WebInspector.ContextMenu.Provider}
227  */
228 WebInspector.HandlerRegistry.ContextMenuProvider = function()
229 {
230 }
231
232 WebInspector.HandlerRegistry.ContextMenuProvider.prototype = {
233     /**
234      * @param {!WebInspector.ContextMenu} contextMenu
235      * @param {!Object} target
236      */
237     appendApplicableItems: function(event, contextMenu, target)
238     {
239         WebInspector.openAnchorLocationRegistry._appendContentProviderItems(contextMenu, target);
240         WebInspector.openAnchorLocationRegistry._appendHrefItems(contextMenu, target);
241     }
242 }
243
244 /**
245  * @constructor
246  * @implements {WebInspector.Linkifier.LinkHandler}
247  */
248 WebInspector.HandlerRegistry.LinkHandler = function()
249 {
250 }
251
252 WebInspector.HandlerRegistry.LinkHandler.prototype = {
253     /**
254      * @param {string} url
255      * @param {number=} lineNumber
256      * @return {boolean}
257      */
258     handleLink: function(url, lineNumber)
259     {
260         return WebInspector.openAnchorLocationRegistry.dispatch({ url: url, lineNumber: lineNumber});
261     }
262 }
263
264 /**
265  * @type {!WebInspector.HandlerRegistry}
266  */
267 WebInspector.openAnchorLocationRegistry;