2 * Copyright (C) 2009 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
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
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.
32 function InspectorFrontendHostAPI()
36 /** @typedef {{type:string, id:(number|undefined),
37 label:(string|undefined), enabled:(boolean|undefined), checked:(boolean|undefined),
38 subItems:(!Array.<!InspectorFrontendHostAPI.ContextMenuDescriptor>|undefined)}} */
39 InspectorFrontendHostAPI.ContextMenuDescriptor;
41 InspectorFrontendHostAPI.Events = {
42 AppendedToURL: "appendedToURL",
43 CanceledSaveURL: "canceledSaveURL",
44 ContextMenuCleared: "contextMenuCleared",
45 ContextMenuItemSelected: "contextMenuItemSelected",
46 DeviceCountUpdated: "deviceCountUpdated",
47 DevicesUpdated: "devicesUpdated",
48 DispatchMessage: "dispatchMessage",
49 EnterInspectElementMode: "enterInspectElementMode",
50 FileSystemsLoaded: "fileSystemsLoaded",
51 FileSystemRemoved: "fileSystemRemoved",
52 FileSystemAdded: "fileSystemAdded",
53 IndexingTotalWorkCalculated: "indexingTotalWorkCalculated",
54 IndexingWorked: "indexingWorked",
55 IndexingDone: "indexingDone",
56 KeyEventUnhandled: "keyEventUnhandled",
57 RevealSourceLine: "revealSourceLine",
59 SearchCompleted: "searchCompleted",
60 SetToolbarColors: "setToolbarColors",
61 SetUseSoftMenu: "setUseSoftMenu",
62 ShowConsole: "showConsole"
65 InspectorFrontendHostAPI.EventDescriptors = [
66 [InspectorFrontendHostAPI.Events.AppendedToURL, ["url"]],
67 [InspectorFrontendHostAPI.Events.CanceledSaveURL, ["url"]],
68 [InspectorFrontendHostAPI.Events.ContextMenuCleared, []],
69 [InspectorFrontendHostAPI.Events.ContextMenuItemSelected, ["id"]],
70 [InspectorFrontendHostAPI.Events.DeviceCountUpdated, ["count"]],
71 [InspectorFrontendHostAPI.Events.DevicesUpdated, ["devices"]],
72 [InspectorFrontendHostAPI.Events.DispatchMessage, ["messageObject"]],
73 [InspectorFrontendHostAPI.Events.EnterInspectElementMode, [], true],
74 [InspectorFrontendHostAPI.Events.FileSystemsLoaded, ["fileSystems"]],
75 [InspectorFrontendHostAPI.Events.FileSystemRemoved, ["fileSystemPath"]],
76 [InspectorFrontendHostAPI.Events.FileSystemAdded, ["errorMessage", "fileSystem"]],
77 [InspectorFrontendHostAPI.Events.IndexingTotalWorkCalculated, ["requestId", "fileSystemPath", "totalWork"]],
78 [InspectorFrontendHostAPI.Events.IndexingWorked, ["requestId", "fileSystemPath", "worked"]],
79 [InspectorFrontendHostAPI.Events.IndexingDone, ["requestId", "fileSystemPath"]],
80 [InspectorFrontendHostAPI.Events.KeyEventUnhandled, ["event"], true],
81 [InspectorFrontendHostAPI.Events.RevealSourceLine, ["url", "lineNumber", "columnNumber"], true],
82 [InspectorFrontendHostAPI.Events.SavedURL, ["url"]],
83 [InspectorFrontendHostAPI.Events.SearchCompleted, ["requestId", "fileSystemPath", "files"]],
84 [InspectorFrontendHostAPI.Events.SetToolbarColors, ["backgroundColor", "color"]],
85 [InspectorFrontendHostAPI.Events.SetUseSoftMenu, ["useSoftMenu"]],
86 [InspectorFrontendHostAPI.Events.ShowConsole, [], true]
89 InspectorFrontendHostAPI.prototype = {
90 addFileSystem: function() { },
94 * @param {string} content
96 append: function(url, content) { },
99 * @param {number} requestId
100 * @param {string} fileSystemPath
102 indexPath: function(requestId, fileSystemPath) { },
107 getSelectionBackgroundColor: function() { },
112 getSelectionForegroundColor: function() { },
115 * Requests inspected page to be placed atop of the inspector frontend with specified bounds.
116 * @param {{x: number, y: number, width: number, height: number}} bounds
118 setInspectedPageBounds: function(bounds) { },
121 * Requests inspected page to be placed atop of the inspector frontend
122 * with passed insets from the frontend sides, respecting minimum size passed.
123 * @param {{top: number, left: number, right: number, bottom: number}} insets
124 * @param {{width: number, height: number}} minSize
126 setContentsResizingStrategy: function(insets, minSize) { },
129 * @param {string} shortcuts
131 setWhitelistedShortcuts: function(shortcuts) { },
133 inspectElementCompleted: function() { },
139 moveWindowBy: function(x, y) { },
142 * @param {string} url
144 openInNewTab: function(url) { },
147 * @param {string} fileSystemPath
149 removeFileSystem: function(fileSystemPath) { },
151 requestFileSystems: function() { },
154 * @param {string} url
155 * @param {string} content
156 * @param {boolean} forceSaveAs
158 save: function(url, content, forceSaveAs) { },
161 * @param {number} requestId
162 * @param {string} fileSystemPath
163 * @param {string} query
165 searchInPath: function(requestId, fileSystemPath, query) { },
168 * @param {number} requestId
170 stopIndexing: function(requestId) { },
172 bringToFront: function() { },
175 * @param {string} browserId
176 * @param {string} url
178 openUrlOnRemoteDeviceAndInspect: function(browserId, url) { },
180 closeWindow: function() { },
182 copyText: function(text) { },
185 * @param {string} url
187 inspectedURLChanged: function(url) { },
190 * @param {string} fileSystemId
191 * @param {string} registeredName
192 * @return {?DOMFileSystem}
194 isolatedFileSystem: function(fileSystemId, registeredName) { },
197 * @param {!FileSystem} fileSystem
199 upgradeDraggedFileSystemPermissions: function(fileSystem) { },
204 platform: function() { },
209 port: function() { },
212 * @param {number} actionCode
214 recordActionTaken: function(actionCode) { },
217 * @param {number} panelCode
219 recordPanelShown: function(panelCode) { },
222 * @param {string} message
224 sendMessageToBackend: function(message) { },
227 * @param {string} message
229 sendMessageToEmbedder: function(message) { },
232 * @param {boolean} enabled
234 setDeviceCountUpdatesEnabled: function(enabled) { },
237 * @param {boolean} enabled
239 setDevicesUpdatesEnabled: function(enabled) { },
242 * @param {string} origin
243 * @param {string} script
245 setInjectedScriptForOrigin: function(origin, script) { },
248 * @param {boolean} isDocked
249 * @param {!function()} callback
251 setIsDocked: function(isDocked, callback) { },
254 * @param {number} zoom
256 setZoomFactor: function(zoom) { },
261 zoomFactor: function() { },
263 zoomIn: function() { },
265 zoomOut: function() { },
267 resetZoom: function() { },
272 * @param {!Array.<!InspectorFrontendHostAPI.ContextMenuDescriptor>} items
274 showContextMenuAtPoint: function(x, y, items) { },
279 isUnderTest: function() { },
284 isHostedMode: function() { }
289 * @implements {InspectorFrontendHostAPI}
291 WebInspector.InspectorFrontendHostStub = function()
295 WebInspector.InspectorFrontendHostStub.prototype = {
299 getSelectionBackgroundColor: function()
307 getSelectionForegroundColor: function()
317 var match = navigator.userAgent.match(/Windows NT/);
320 match = navigator.userAgent.match(/Mac OS X/);
334 bringToFront: function()
336 this._windowVisible = true;
339 closeWindow: function()
341 this._windowVisible = false;
345 * @param {boolean} isDocked
346 * @param {!function()} callback
348 setIsDocked: function(isDocked, callback)
353 * Requests inspected page to be placed atop of the inspector frontend with specified bounds.
354 * @param {{x: number, y: number, width: number, height: number}} bounds
356 setInspectedPageBounds: function(bounds)
361 * Requests inspected page to be placed atop of the inspector frontend
362 * with passed insets from the frontend sides, respecting minimum size passed.
363 * @param {{top: number, left: number, right: number, bottom: number}} insets
364 * @param {{width: number, height: number}} minSize
366 setContentsResizingStrategy: function(insets, minSize)
370 inspectElementCompleted: function()
378 moveWindowBy: function(x, y)
383 * @param {string} origin
384 * @param {string} script
386 setInjectedScriptForOrigin: function(origin, script)
391 * @param {string} url
393 inspectedURLChanged: function(url)
395 document.title = WebInspector.UIString("Developer Tools - %s", url);
399 * @param {string} text
401 copyText: function(text)
403 WebInspector.console.error("Clipboard is not enabled in hosted mode. Please inspect using chrome://inspect");
407 * @param {string} url
409 openInNewTab: function(url)
411 window.open(url, "_blank");
415 * @param {string} url
416 * @param {string} content
417 * @param {boolean} forceSaveAs
419 save: function(url, content, forceSaveAs)
421 WebInspector.console.error("Saving files is not enabled in hosted mode. Please inspect using chrome://inspect");
422 this.events.dispatchEventToListeners(InspectorFrontendHostAPI.Events.CanceledSaveURL, url);
426 * @param {string} url
427 * @param {string} content
429 append: function(url, content)
431 WebInspector.console.error("Saving files is not enabled in hosted mode. Please inspect using chrome://inspect");
435 * @param {string} message
437 sendMessageToBackend: function(message)
442 * @param {string} message
444 sendMessageToEmbedder: function(message)
449 * @param {number} actionCode
451 recordActionTaken: function(actionCode)
456 * @param {number} panelCode
458 recordPanelShown: function(panelCode)
462 requestFileSystems: function()
466 addFileSystem: function()
471 * @param {string} fileSystemPath
473 removeFileSystem: function(fileSystemPath)
478 * @param {string} fileSystemId
479 * @param {string} registeredName
480 * @return {?DOMFileSystem}
482 isolatedFileSystem: function(fileSystemId, registeredName)
488 * @param {!FileSystem} fileSystem
490 upgradeDraggedFileSystemPermissions: function(fileSystem)
495 * @param {number} requestId
496 * @param {string} fileSystemPath
498 indexPath: function(requestId, fileSystemPath)
503 * @param {number} requestId
505 stopIndexing: function(requestId)
510 * @param {number} requestId
511 * @param {string} fileSystemPath
512 * @param {string} query
514 searchInPath: function(requestId, fileSystemPath, query)
519 * @param {number} zoom
521 setZoomFactor: function(zoom)
528 zoomFactor: function()
541 resetZoom: function()
545 setWhitelistedShortcuts: function(shortcuts)
552 isUnderTest: function()
558 * @param {string} browserId
559 * @param {string} url
561 openUrlOnRemoteDeviceAndInspect: function(browserId, url)
566 * @param {boolean} enabled
568 setDeviceCountUpdatesEnabled: function(enabled)
573 * @param {boolean} enabled
575 setDevicesUpdatesEnabled: function(enabled)
582 * @param {!Array.<!InspectorFrontendHostAPI.ContextMenuDescriptor>} items
584 showContextMenuAtPoint: function(x, y, items)
586 throw "Soft context menu should be used";
592 isHostedMode: function()
599 * @type {!InspectorFrontendHostAPI}
601 var InspectorFrontendHost = window.InspectorFrontendHost || null;
604 if (!InspectorFrontendHost) {
605 // Instantiate stub for web-hosted mode if necessary.
606 InspectorFrontendHost = new WebInspector.InspectorFrontendHostStub();
608 // Otherwise add stubs for missing methods that are declared in the interface.
609 var proto = WebInspector.InspectorFrontendHostStub.prototype;
610 for (var name in proto) {
611 var value = proto[name];
612 if (typeof value !== "function" || InspectorFrontendHost[name])
615 InspectorFrontendHost[name] = stub.bind(null, name);
620 * @param {string} name
624 console.error("Incompatible embedder: method InspectorFrontendHost." + name + " is missing. Using stub instead.");
625 var args = Array.prototype.slice.call(arguments, 1);
626 return proto[name].apply(InspectorFrontendHost, args);
629 // Attach the events object.
630 InspectorFrontendHost.events = new WebInspector.Object();
636 function InspectorFrontendAPIImpl()
638 this._isLoaded = false;
639 this._pendingCommands = [];
640 this._debugFrontend = !!Runtime.queryParam("debugFrontend");
642 var descriptors = InspectorFrontendHostAPI.EventDescriptors;
643 for (var i = 0; i < descriptors.length; ++i)
644 this[descriptors[i][0]] = this._dispatch.bind(this, descriptors[i][0], descriptors[i][1], descriptors[i][2]);
647 InspectorFrontendAPIImpl.prototype = {
648 loadCompleted: function()
650 this._isLoaded = true;
651 for (var i = 0; i < this._pendingCommands.length; ++i)
652 this._pendingCommands[i]();
653 this._pendingCommands = [];
655 window.opener.postMessage(["loadCompleted"], "*");
659 * @param {string} name
660 * @param {!Array.<string>} signature
661 * @param {boolean} runOnceLoaded
663 _dispatch: function(name, signature, runOnceLoaded)
665 var params = Array.prototype.slice.call(arguments, 3);
667 if (this._debugFrontend)
668 setImmediate(innerDispatch.bind(this));
670 innerDispatch.call(this);
673 * @this {!InspectorFrontendAPIImpl}
675 function innerDispatch()
678 this._runOnceLoaded(dispatchAfterLoad);
682 function dispatchAfterLoad()
684 // Single argument methods get dispatched with the param.
685 if (signature.length < 2) {
686 InspectorFrontendHost.events.dispatchEventToListeners(name, params[0]);
690 for (var i = 0; i < signature.length; ++i)
691 data[signature[i]] = params[i];
692 InspectorFrontendHost.events.dispatchEventToListeners(name, data);
698 * @param {function()} command
700 _runOnceLoaded: function(command)
702 if (this._isLoaded) {
706 this._pendingCommands.push(command);
711 * @param {?string} error
713 embedderMessageAck: function(id, error)
715 InspectorFrontendHost["embedderMessageAck"](id, error);
719 var InspectorFrontendAPI = new InspectorFrontendAPIImpl();