Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / devtools / front_end / host / InspectorFrontendHost.js
1 /*
2  * Copyright (C) 2009 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 /** @interface */
32 function InspectorFrontendHostAPI()
33 {
34     /**
35      * @type {!WebInspector.EventTarget}
36      */
37     this.events;
38 }
39
40 /** @typedef {{type:string, id:(number|undefined),
41                label:(string|undefined), enabled:(boolean|undefined), checked:(boolean|undefined),
42                subItems:(!Array.<!InspectorFrontendHostAPI.ContextMenuDescriptor>|undefined)}} */
43 InspectorFrontendHostAPI.ContextMenuDescriptor;
44
45 InspectorFrontendHostAPI.Events = {
46     AppendedToURL: "appendedToURL",
47     CanceledSaveURL: "canceledSaveURL",
48     ContextMenuCleared: "contextMenuCleared",
49     ContextMenuItemSelected: "contextMenuItemSelected",
50     DeviceCountUpdated: "deviceCountUpdated",
51     DevicesUpdated: "devicesUpdated",
52     DispatchMessage: "dispatchMessage",
53     DispatchMessageChunk: "dispatchMessageChunk",
54     EnterInspectElementMode: "enterInspectElementMode",
55     FileSystemsLoaded: "fileSystemsLoaded",
56     FileSystemRemoved: "fileSystemRemoved",
57     FileSystemAdded: "fileSystemAdded",
58     IndexingTotalWorkCalculated: "indexingTotalWorkCalculated",
59     IndexingWorked: "indexingWorked",
60     IndexingDone: "indexingDone",
61     KeyEventUnhandled: "keyEventUnhandled",
62     RevealSourceLine: "revealSourceLine",
63     SavedURL: "savedURL",
64     SearchCompleted: "searchCompleted",
65     SetToolbarColors: "setToolbarColors",
66     SetUseSoftMenu: "setUseSoftMenu",
67     ShowConsole: "showConsole"
68 }
69
70 InspectorFrontendHostAPI.EventDescriptors = [
71     [InspectorFrontendHostAPI.Events.AppendedToURL, ["url"]],
72     [InspectorFrontendHostAPI.Events.CanceledSaveURL, ["url"]],
73     [InspectorFrontendHostAPI.Events.ContextMenuCleared, []],
74     [InspectorFrontendHostAPI.Events.ContextMenuItemSelected, ["id"]],
75     [InspectorFrontendHostAPI.Events.DeviceCountUpdated, ["count"]],
76     [InspectorFrontendHostAPI.Events.DevicesUpdated, ["devices"]],
77     [InspectorFrontendHostAPI.Events.DispatchMessage, ["messageObject"]],
78     [InspectorFrontendHostAPI.Events.DispatchMessageChunk, ["messageChunk", "messageSize"]],
79     [InspectorFrontendHostAPI.Events.EnterInspectElementMode, []],
80     [InspectorFrontendHostAPI.Events.FileSystemsLoaded, ["fileSystems"]],
81     [InspectorFrontendHostAPI.Events.FileSystemRemoved, ["fileSystemPath"]],
82     [InspectorFrontendHostAPI.Events.FileSystemAdded, ["errorMessage", "fileSystem"]],
83     [InspectorFrontendHostAPI.Events.IndexingTotalWorkCalculated, ["requestId", "fileSystemPath", "totalWork"]],
84     [InspectorFrontendHostAPI.Events.IndexingWorked, ["requestId", "fileSystemPath", "worked"]],
85     [InspectorFrontendHostAPI.Events.IndexingDone, ["requestId", "fileSystemPath"]],
86     [InspectorFrontendHostAPI.Events.KeyEventUnhandled, ["event"]],
87     [InspectorFrontendHostAPI.Events.RevealSourceLine, ["url", "lineNumber", "columnNumber"]],
88     [InspectorFrontendHostAPI.Events.SavedURL, ["url"]],
89     [InspectorFrontendHostAPI.Events.SearchCompleted, ["requestId", "fileSystemPath", "files"]],
90     [InspectorFrontendHostAPI.Events.SetToolbarColors, ["backgroundColor", "color"]],
91     [InspectorFrontendHostAPI.Events.SetUseSoftMenu, ["useSoftMenu"]],
92     [InspectorFrontendHostAPI.Events.ShowConsole, []]
93 ];
94
95 InspectorFrontendHostAPI.prototype = {
96     addFileSystem: function() { },
97
98     /**
99      * @param {string} url
100      * @param {string} content
101      */
102     append: function(url, content) { },
103
104     loadCompleted: function() { },
105
106     /**
107      * @param {number} requestId
108      * @param {string} fileSystemPath
109      */
110     indexPath: function(requestId, fileSystemPath) { },
111
112     /**
113      * @return {string}
114      */
115     getSelectionBackgroundColor: function() { },
116
117     /**
118      * @return {string}
119      */
120     getSelectionForegroundColor: function() { },
121
122     /**
123      * Requests inspected page to be placed atop of the inspector frontend with specified bounds.
124      * @param {{x: number, y: number, width: number, height: number}} bounds
125      */
126     setInspectedPageBounds: function(bounds) { },
127
128     /**
129      * Requests inspected page to be placed atop of the inspector frontend
130      * with passed insets from the frontend sides, respecting minimum size passed.
131      * @param {{top: number, left: number, right: number, bottom: number}} insets
132      * @param {{width: number, height: number}} minSize
133      */
134     setContentsResizingStrategy: function(insets, minSize) { },
135
136     /**
137      * @param {string} shortcuts
138      */
139     setWhitelistedShortcuts: function(shortcuts) { },
140
141     inspectElementCompleted: function() { },
142
143     /**
144      * @param {number} x
145      * @param {number} y
146      */
147     moveWindowBy: function(x, y) { },
148
149     /**
150      * @param {string} url
151      */
152     openInNewTab: function(url) { },
153
154     /**
155      * @param {string} fileSystemPath
156      */
157     removeFileSystem: function(fileSystemPath) { },
158
159     requestFileSystems: function() { },
160
161     /**
162      * @param {string} url
163      * @param {string} content
164      * @param {boolean} forceSaveAs
165      */
166     save: function(url, content, forceSaveAs) { },
167
168     /**
169      * @param {number} requestId
170      * @param {string} fileSystemPath
171      * @param {string} query
172      */
173     searchInPath: function(requestId, fileSystemPath, query) { },
174
175     /**
176      * @param {number} requestId
177      */
178     stopIndexing: function(requestId) { },
179
180     bringToFront: function() { },
181
182     /**
183      * @param {string} browserId
184      * @param {string} url
185      */
186     openUrlOnRemoteDeviceAndInspect: function(browserId, url) { },
187
188     closeWindow: function() { },
189
190     copyText: function(text) { },
191
192     /**
193      * @param {string} url
194      */
195     inspectedURLChanged: function(url) { },
196
197     /**
198      * @param {string} fileSystemId
199      * @param {string} registeredName
200      * @return {?DOMFileSystem}
201      */
202     isolatedFileSystem: function(fileSystemId, registeredName) { },
203
204     /**
205      * @param {!FileSystem} fileSystem
206      */
207     upgradeDraggedFileSystemPermissions: function(fileSystem) { },
208
209     /**
210      * @return {string}
211      */
212     platform: function() { },
213
214     /**
215      * @return {string}
216      */
217     port: function() { },
218
219     /**
220      * @param {number} actionCode
221      */
222     recordActionTaken: function(actionCode) { },
223
224     /**
225      * @param {number} panelCode
226      */
227     recordPanelShown: function(panelCode) { },
228
229     /**
230      * @param {string} message
231      */
232     sendMessageToBackend: function(message) { },
233
234     /**
235      * @param {string} message
236      */
237     sendMessageToEmbedder: function(message) { },
238
239     /**
240      * @param {boolean} enabled
241      */
242     setDeviceCountUpdatesEnabled: function(enabled) { },
243
244     /**
245      * @param {boolean} enabled
246      */
247     setDevicesUpdatesEnabled: function(enabled) { },
248
249     /**
250      * @param {string} origin
251      * @param {string} script
252      */
253     setInjectedScriptForOrigin: function(origin, script) { },
254
255     /**
256      * @param {boolean} isDocked
257      * @param {function()} callback
258      */
259     setIsDocked: function(isDocked, callback) { },
260
261     /**
262      * @param {number} zoom
263      */
264     setZoomFactor: function(zoom) { },
265
266     /**
267      * @return {number}
268      */
269     zoomFactor: function() { },
270
271     zoomIn: function() { },
272
273     zoomOut: function() { },
274
275     resetZoom: function() { },
276
277     /**
278      * @param {number} x
279      * @param {number} y
280      * @param {!Array.<!InspectorFrontendHostAPI.ContextMenuDescriptor>} items
281      * @param {!Document} document
282      */
283     showContextMenuAtPoint: function(x, y, items, document) { },
284
285     /**
286      * @return {boolean}
287      */
288     isUnderTest: function() { },
289
290     /**
291      * @return {boolean}
292      */
293     isHostedMode: function() { }
294 }
295
296 /**
297  * @constructor
298  * @implements {InspectorFrontendHostAPI}
299  * @suppressGlobalPropertiesCheck
300  */
301 WebInspector.InspectorFrontendHostStub = function()
302 {
303     /**
304      * @param {!Event} event
305      */
306     function stopEventPropagation(event)
307     {
308         // Let browser handle Ctrl+/Ctrl- shortcuts in hosted mode.
309         var zoomModifier = WebInspector.isMac() ? event.metaKey : event.ctrlKey;
310         if (zoomModifier && (event.keyCode === 187 || event.keyCode === 189))
311             event.stopPropagation();
312     }
313     document.addEventListener("keydown", stopEventPropagation, true);
314 }
315
316 WebInspector.InspectorFrontendHostStub.prototype = {
317     /**
318      * @return {string}
319      */
320     getSelectionBackgroundColor: function()
321     {
322         return "#6e86ff";
323     },
324
325     /**
326      * @return {string}
327      */
328     getSelectionForegroundColor: function()
329     {
330         return "#ffffff";
331     },
332
333     /**
334      * @return {string}
335      */
336     platform: function()
337     {
338         var match = navigator.userAgent.match(/Windows NT/);
339         if (match)
340             return "windows";
341         match = navigator.userAgent.match(/Mac OS X/);
342         if (match)
343             return "mac";
344         return "linux";
345     },
346
347     /**
348      * @return {string}
349      */
350     port: function()
351     {
352         return "unknown";
353     },
354
355     loadCompleted: function()
356     {
357     },
358
359     bringToFront: function()
360     {
361         this._windowVisible = true;
362     },
363
364     closeWindow: function()
365     {
366         this._windowVisible = false;
367     },
368
369     /**
370      * @param {boolean} isDocked
371      * @param {function()} callback
372      */
373     setIsDocked: function(isDocked, callback)
374     {
375     },
376
377     /**
378      * Requests inspected page to be placed atop of the inspector frontend with specified bounds.
379      * @param {{x: number, y: number, width: number, height: number}} bounds
380      */
381     setInspectedPageBounds: function(bounds)
382     {
383     },
384
385     /**
386      * Requests inspected page to be placed atop of the inspector frontend
387      * with passed insets from the frontend sides, respecting minimum size passed.
388      * @param {{top: number, left: number, right: number, bottom: number}} insets
389      * @param {{width: number, height: number}} minSize
390      */
391     setContentsResizingStrategy: function(insets, minSize)
392     {
393     },
394
395     inspectElementCompleted: function()
396     {
397     },
398
399     /**
400      * @param {number} x
401      * @param {number} y
402      */
403     moveWindowBy: function(x, y)
404     {
405     },
406
407     /**
408      * @param {string} origin
409      * @param {string} script
410      */
411     setInjectedScriptForOrigin: function(origin, script)
412     {
413     },
414
415     /**
416      * @param {string} url
417      * @suppressGlobalPropertiesCheck
418      */
419     inspectedURLChanged: function(url)
420     {
421         document.title = WebInspector.UIString("Developer Tools - %s", url);
422     },
423
424     /**
425      * @param {string} text
426      */
427     copyText: function(text)
428     {
429         WebInspector.console.error("Clipboard is not enabled in hosted mode. Please inspect using chrome://inspect");
430     },
431
432     /**
433      * @param {string} url
434      */
435     openInNewTab: function(url)
436     {
437         window.open(url, "_blank");
438     },
439
440     /**
441      * @param {string} url
442      * @param {string} content
443      * @param {boolean} forceSaveAs
444      */
445     save: function(url, content, forceSaveAs)
446     {
447         WebInspector.console.error("Saving files is not enabled in hosted mode. Please inspect using chrome://inspect");
448         this.events.dispatchEventToListeners(InspectorFrontendHostAPI.Events.CanceledSaveURL, url);
449     },
450
451     /**
452      * @param {string} url
453      * @param {string} content
454      */
455     append: function(url, content)
456     {
457         WebInspector.console.error("Saving files is not enabled in hosted mode. Please inspect using chrome://inspect");
458     },
459
460     /**
461      * @param {string} message
462      */
463     sendMessageToBackend: function(message)
464     {
465     },
466
467     /**
468      * @param {string} message
469      */
470     sendMessageToEmbedder: function(message)
471     {
472     },
473
474     /**
475      * @param {number} actionCode
476      */
477     recordActionTaken: function(actionCode)
478     {
479     },
480
481     /**
482      * @param {number} panelCode
483      */
484     recordPanelShown: function(panelCode)
485     {
486     },
487
488     requestFileSystems: function()
489     {
490     },
491
492     addFileSystem: function()
493     {
494     },
495
496     /**
497      * @param {string} fileSystemPath
498      */
499     removeFileSystem: function(fileSystemPath)
500     {
501     },
502
503     /**
504      * @param {string} fileSystemId
505      * @param {string} registeredName
506      * @return {?DOMFileSystem}
507      */
508     isolatedFileSystem: function(fileSystemId, registeredName)
509     {
510         return null;
511     },
512
513     /**
514      * @param {!FileSystem} fileSystem
515      */
516     upgradeDraggedFileSystemPermissions: function(fileSystem)
517     {
518     },
519
520     /**
521      * @param {number} requestId
522      * @param {string} fileSystemPath
523      */
524     indexPath: function(requestId, fileSystemPath)
525     {
526     },
527
528     /**
529      * @param {number} requestId
530      */
531     stopIndexing: function(requestId)
532     {
533     },
534
535     /**
536      * @param {number} requestId
537      * @param {string} fileSystemPath
538      * @param {string} query
539      */
540     searchInPath: function(requestId, fileSystemPath, query)
541     {
542     },
543
544     /**
545      * @param {number} zoom
546      */
547     setZoomFactor: function(zoom)
548     {
549     },
550
551     /**
552      * @return {number}
553      */
554     zoomFactor: function()
555     {
556         return 1;
557     },
558
559     zoomIn: function()
560     {
561     },
562
563     zoomOut: function()
564     {
565     },
566
567     resetZoom: function()
568     {
569     },
570
571     setWhitelistedShortcuts: function(shortcuts)
572     {
573     },
574
575     /**
576      * @return {boolean}
577      */
578     isUnderTest: function()
579     {
580         return false;
581     },
582
583     /**
584      * @param {string} browserId
585      * @param {string} url
586      */
587     openUrlOnRemoteDeviceAndInspect: function(browserId, url)
588     {
589     },
590
591     /**
592      * @param {boolean} enabled
593      */
594     setDeviceCountUpdatesEnabled: function(enabled)
595     {
596     },
597
598     /**
599      * @param {boolean} enabled
600      */
601     setDevicesUpdatesEnabled: function(enabled)
602     {
603     },
604
605     /**
606      * @param {number} x
607      * @param {number} y
608      * @param {!Array.<!InspectorFrontendHostAPI.ContextMenuDescriptor>} items
609      * @param {!Document} document
610      */
611     showContextMenuAtPoint: function(x, y, items, document)
612     {
613         throw "Soft context menu should be used";
614     },
615
616     /**
617      * @return {boolean}
618      */
619     isHostedMode: function()
620     {
621         return true;
622     }
623 };
624
625 /**
626  * @type {!InspectorFrontendHostAPI}
627  */
628 var InspectorFrontendHost = window.InspectorFrontendHost || null;
629
630 (function() {
631     if (!InspectorFrontendHost) {
632         // Instantiate stub for web-hosted mode if necessary.
633         InspectorFrontendHost = new WebInspector.InspectorFrontendHostStub();
634     } else {
635         // Otherwise add stubs for missing methods that are declared in the interface.
636         var proto = WebInspector.InspectorFrontendHostStub.prototype;
637         for (var name in proto) {
638             var value = proto[name];
639             if (typeof value !== "function" || InspectorFrontendHost[name])
640                 continue;
641
642             InspectorFrontendHost[name] = stub.bind(null, name);
643         }
644     }
645
646     /**
647      * @param {string} name
648      */
649     function stub(name)
650     {
651         console.error("Incompatible embedder: method InspectorFrontendHost." + name + " is missing. Using stub instead.");
652         var args = Array.prototype.slice.call(arguments, 1);
653         return proto[name].apply(InspectorFrontendHost, args);
654     }
655
656     // Attach the events object.
657     InspectorFrontendHost.events = new WebInspector.Object();
658 })();
659
660 /**
661  * @constructor
662  */
663 function InspectorFrontendAPIImpl()
664 {
665     this._debugFrontend = !!Runtime.queryParam("debugFrontend");
666
667     var descriptors = InspectorFrontendHostAPI.EventDescriptors;
668     for (var i = 0; i < descriptors.length; ++i)
669         this[descriptors[i][0]] = this._dispatch.bind(this, descriptors[i][0], descriptors[i][1], descriptors[i][2]);
670 }
671
672 InspectorFrontendAPIImpl.prototype = {
673     /**
674      * @param {string} name
675      * @param {!Array.<string>} signature
676      * @param {boolean} runOnceLoaded
677      */
678     _dispatch: function(name, signature, runOnceLoaded)
679     {
680         var params = Array.prototype.slice.call(arguments, 3);
681
682         if (this._debugFrontend)
683             setImmediate(innerDispatch);
684         else
685             innerDispatch();
686
687         function innerDispatch()
688         {
689             // Single argument methods get dispatched with the param.
690             if (signature.length < 2) {
691                 InspectorFrontendHost.events.dispatchEventToListeners(name, params[0]);
692                 return;
693             }
694             var data = {};
695             for (var i = 0; i < signature.length; ++i)
696                 data[signature[i]] = params[i];
697             InspectorFrontendHost.events.dispatchEventToListeners(name, data);
698         }
699     },
700
701     /**
702      * @param {number} id
703      * @param {?string} error
704      */
705     embedderMessageAck: function(id, error)
706     {
707         InspectorFrontendHost["embedderMessageAck"](id, error);
708     }
709 }
710
711 var InspectorFrontendAPI = new InspectorFrontendAPIImpl();