Upstream version 7.35.144.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 {string} url
97      */
98     _openInNewTab: function(url)
99     {
100         InspectorFrontendHost.openInNewTab(url);
101     },
102
103     /** 
104      * @param {!WebInspector.ContextMenu} contextMenu
105      * @param {!Object} target
106      */
107     _appendContentProviderItems: function(contextMenu, target)
108     {
109         if (!(target instanceof WebInspector.UISourceCode || target instanceof WebInspector.Resource || target instanceof WebInspector.NetworkRequest))
110             return;
111         var contentProvider = /** @type {!WebInspector.ContentProvider} */ (target);
112         if (!contentProvider.contentURL())
113             return;
114
115         contextMenu.appendItem(WebInspector.openLinkExternallyLabel(), this._openInNewTab.bind(this, contentProvider.contentURL()));
116         // Skip 0th handler, as it's 'Use default panel' one.
117         for (var i = 1; i < this.handlerNames.length; ++i) {
118             var handler = this.handlerNames[i];
119             contextMenu.appendItem(WebInspector.UIString(WebInspector.useLowerCaseMenuTitles() ? "Open using %s" : "Open Using %s", handler),
120                 this.dispatchToHandler.bind(this, handler, { url: contentProvider.contentURL() }));
121         }
122         contextMenu.appendItem(WebInspector.copyLinkAddressLabel(), InspectorFrontendHost.copyText.bind(InspectorFrontendHost, contentProvider.contentURL()));
123
124         if (!contentProvider.contentURL())
125             return;
126
127         var contentType = contentProvider.contentType();
128         if (contentType !== WebInspector.resourceTypes.Document &&
129             contentType !== WebInspector.resourceTypes.Stylesheet &&
130             contentType !== WebInspector.resourceTypes.Script)
131             return;
132
133         /**
134          * @param {boolean} forceSaveAs
135          * @param {?string} content
136          */
137         function doSave(forceSaveAs, content)
138         {
139             var url = contentProvider.contentURL();
140             WebInspector.fileManager.save(url, /** @type {string} */ (content), forceSaveAs);
141             WebInspector.fileManager.close(url);
142         }
143
144         /**
145          * @param {boolean} forceSaveAs
146          */
147         function save(forceSaveAs)
148         {
149             if (contentProvider instanceof WebInspector.UISourceCode) {
150                 var uiSourceCode = /** @type {!WebInspector.UISourceCode} */ (contentProvider);
151                 uiSourceCode.saveToFileSystem(forceSaveAs);
152                 return;
153             }
154             contentProvider.requestContent(doSave.bind(null, forceSaveAs));
155         }
156
157         contextMenu.appendSeparator();
158         contextMenu.appendItem(WebInspector.UIString("Save"), save.bind(null, false));
159         contextMenu.appendItem(WebInspector.UIString(WebInspector.useLowerCaseMenuTitles() ? "Save as..." : "Save As..."), save.bind(null, true));
160     },
161
162     /** 
163      * @param {!WebInspector.ContextMenu} contextMenu
164      * @param {!Object} target
165      */
166     _appendHrefItems: function(contextMenu, target)
167     {
168         if (!(target instanceof Node))
169             return;
170         var targetNode = /** @type {!Node} */ (target);
171
172         var anchorElement = targetNode.enclosingNodeOrSelfWithClass("webkit-html-resource-link") || targetNode.enclosingNodeOrSelfWithClass("webkit-html-external-link");
173         if (!anchorElement)
174             return;
175
176         var resourceURL = anchorElement.href;
177         if (!resourceURL)
178             return;
179
180         // Add resource-related actions.
181         contextMenu.appendItem(WebInspector.openLinkExternallyLabel(), this._openInNewTab.bind(this, resourceURL));
182
183         function openInResourcesPanel(resourceURL)
184         {
185             var resource = WebInspector.resourceForURL(resourceURL);
186             if (resource)
187                 WebInspector.Revealer.reveal(resource);
188             else
189                 InspectorFrontendHost.openInNewTab(resourceURL);
190         }
191         if (WebInspector.resourceForURL(resourceURL))
192             contextMenu.appendItem(WebInspector.UIString(WebInspector.useLowerCaseMenuTitles() ? "Open link in Resources panel" : "Open Link in Resources Panel"), openInResourcesPanel.bind(null, resourceURL));
193         contextMenu.appendItem(WebInspector.copyLinkAddressLabel(), InspectorFrontendHost.copyText.bind(InspectorFrontendHost, resourceURL));
194     },
195
196     __proto__: WebInspector.Object.prototype
197 }
198
199
200 WebInspector.HandlerRegistry.EventTypes = {
201     HandlersUpdated: "HandlersUpdated"
202 }
203
204 /**
205  * @constructor
206  */
207 WebInspector.HandlerSelector = function(handlerRegistry)
208 {
209     this._handlerRegistry = handlerRegistry;
210     this.element = document.createElement("select");
211     this.element.addEventListener("change", this._onChange.bind(this), false);
212     this._update();
213     this._handlerRegistry.addEventListener(WebInspector.HandlerRegistry.EventTypes.HandlersUpdated, this._update.bind(this));
214 }
215
216 WebInspector.HandlerSelector.prototype =
217 {
218     _update: function()
219     {
220         this.element.removeChildren();
221         var names = this._handlerRegistry.handlerNames;
222         var activeHandler = this._handlerRegistry.activeHandler;
223
224         for (var i = 0; i < names.length; ++i) {
225             var option = document.createElement("option");
226             option.textContent = names[i];
227             option.selected = activeHandler === names[i];
228             this.element.appendChild(option);
229         }
230         this.element.disabled = names.length <= 1;
231     },
232
233     _onChange: function(event)
234     {
235         var value = event.target.value;
236         this._handlerRegistry.activeHandler = value;
237     }
238 }
239
240 /**
241  * @constructor
242  * @implements {WebInspector.ContextMenu.Provider}
243  */
244 WebInspector.HandlerRegistry.ContextMenuProvider = function()
245 {
246 }
247
248 WebInspector.HandlerRegistry.ContextMenuProvider.prototype = {
249     /**
250      * @param {!WebInspector.ContextMenu} contextMenu
251      * @param {!Object} target
252      */
253     appendApplicableItems: function(event, contextMenu, target)
254     {
255         WebInspector.openAnchorLocationRegistry._appendContentProviderItems(contextMenu, target);
256         WebInspector.openAnchorLocationRegistry._appendHrefItems(contextMenu, target);
257     }
258 }
259
260 /**
261  * @constructor
262  * @implements {WebInspector.Linkifier.LinkHandler}
263  */
264 WebInspector.HandlerRegistry.LinkHandler = function()
265 {
266 }
267
268 WebInspector.HandlerRegistry.LinkHandler.prototype = {
269     /**
270      * @param {string} url
271      * @param {number=} lineNumber
272      * @return {boolean}
273      */
274     handleLink: function(url, lineNumber)
275     {
276         return WebInspector.openAnchorLocationRegistry.dispatch({ url: url, lineNumber: lineNumber});
277     }
278 }
279
280 /**
281  * @type {!WebInspector.HandlerRegistry}
282  */
283 WebInspector.openAnchorLocationRegistry;