a68bb28cecd7e5fec6029c800ebe81df93f4a49c
[platform/framework/web/crosswalk.git] / src / chrome / tools / test / reference_build / chrome_linux / resources / inspector / devtools_extension_api.js
1 (function() {
2     /*
3  * Copyright (C) 2012 Google Inc. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *     * Neither the name of Google Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 function defineCommonExtensionSymbols(apiPrivate)
33 {
34     if (!apiPrivate.audits)
35         apiPrivate.audits = {};
36     apiPrivate.audits.Severity = {
37         Info: "info",
38         Warning: "warning",
39         Severe: "severe"
40     };
41
42     if (!apiPrivate.console)
43         apiPrivate.console = {};
44     apiPrivate.console.Severity = {
45         Debug: "debug",
46         Log: "log",
47         Warning: "warning",
48         Error: "error"
49     };
50
51     if (!apiPrivate.panels)
52         apiPrivate.panels = {};
53     apiPrivate.panels.SearchAction = {
54         CancelSearch: "cancelSearch",
55         PerformSearch: "performSearch",
56         NextSearchResult: "nextSearchResult",
57         PreviousSearchResult: "previousSearchResult"
58     };
59
60     apiPrivate.Events = {
61         AuditStarted: "audit-started-",
62         ButtonClicked: "button-clicked-",
63         ConsoleMessageAdded: "console-message-added",
64         PanelObjectSelected: "panel-objectSelected-",
65         NetworkRequestFinished: "network-request-finished",
66         OpenResource: "open-resource",
67         PanelSearch: "panel-search-",
68         ResourceAdded: "resource-added",
69         ResourceContentCommitted: "resource-content-committed",
70         TimelineEventRecorded: "timeline-event-recorded",
71         ViewShown: "view-shown-",
72         ViewHidden: "view-hidden-"
73     };
74
75     apiPrivate.Commands = {
76         AddAuditCategory: "addAuditCategory",
77         AddAuditResult: "addAuditResult",
78         AddConsoleMessage: "addConsoleMessage",
79         AddRequestHeaders: "addRequestHeaders",
80         ApplyStyleSheet: "applyStyleSheet",
81         CreatePanel: "createPanel",
82         CreateSidebarPane: "createSidebarPane",
83         CreateStatusBarButton: "createStatusBarButton",
84         EvaluateOnInspectedPage: "evaluateOnInspectedPage",
85         ForwardKeyboardEvent: "_forwardKeyboardEvent",
86         GetConsoleMessages: "getConsoleMessages",
87         GetHAR: "getHAR",
88         GetPageResources: "getPageResources",
89         GetRequestContent: "getRequestContent",
90         GetResourceContent: "getResourceContent",
91         InspectedURLChanged: "inspectedURLChanged",
92         OpenResource: "openResource",
93         Reload: "Reload",
94         Subscribe: "subscribe",
95         SetOpenResourceHandler: "setOpenResourceHandler",
96         SetResourceContent: "setResourceContent",
97         SetSidebarContent: "setSidebarContent",
98         SetSidebarHeight: "setSidebarHeight",
99         SetSidebarPage: "setSidebarPage",
100         ShowPanel: "showPanel",
101         StopAuditCategoryRun: "stopAuditCategoryRun",
102         Unsubscribe: "unsubscribe",
103         UpdateAuditProgress: "updateAuditProgress",
104         UpdateButton: "updateButton"
105     };
106 }
107
108 /**
109  * @param {number} injectedScriptId
110  * @return {!Object}
111  */
112 function injectedExtensionAPI(injectedScriptId)
113 {
114
115 var apiPrivate = {};
116
117 defineCommonExtensionSymbols(apiPrivate);
118
119 var commands = apiPrivate.Commands;
120 var events = apiPrivate.Events;
121 var userAction = false;
122
123 // Here and below, all constructors are private to API implementation.
124 // For a public type Foo, if internal fields are present, these are on
125 // a private FooImpl type, an instance of FooImpl is used in a closure
126 // by Foo consutrctor to re-bind publicly exported members to an instance
127 // of Foo.
128
129 /**
130  * @constructor
131  */
132 function EventSinkImpl(type, customDispatch)
133 {
134     this._type = type;
135     this._listeners = [];
136     this._customDispatch = customDispatch;
137 }
138
139 EventSinkImpl.prototype = {
140     addListener: function(callback)
141     {
142         if (typeof callback !== "function")
143             throw "addListener: callback is not a function";
144         if (this._listeners.length === 0)
145             extensionServer.sendRequest({ command: commands.Subscribe, type: this._type });
146         this._listeners.push(callback);
147         extensionServer.registerHandler("notify-" + this._type, this._dispatch.bind(this));
148     },
149
150     removeListener: function(callback)
151     {
152         var listeners = this._listeners;
153
154         for (var i = 0; i < listeners.length; ++i) {
155             if (listeners[i] === callback) {
156                 listeners.splice(i, 1);
157                 break;
158             }
159         }
160         if (this._listeners.length === 0)
161             extensionServer.sendRequest({ command: commands.Unsubscribe, type: this._type });
162     },
163
164     /**
165      * @param {...} vararg
166      */
167     _fire: function(vararg)
168     {
169         var listeners = this._listeners.slice();
170         for (var i = 0; i < listeners.length; ++i)
171             listeners[i].apply(null, arguments);
172     },
173
174     _dispatch: function(request)
175     {
176          if (this._customDispatch)
177              this._customDispatch.call(this, request);
178          else
179              this._fire.apply(this, request.arguments);
180     }
181 }
182
183 /**
184  * @constructor
185  */
186 function InspectorExtensionAPI()
187 {
188     this.audits = new Audits();
189     this.inspectedWindow = new InspectedWindow();
190     this.panels = new Panels();
191     this.network = new Network();
192     defineDeprecatedProperty(this, "webInspector", "resources", "network");
193     this.timeline = new Timeline();
194     this.console = new ConsoleAPI();
195 }
196
197 /**
198  * @constructor
199  */
200 function ConsoleAPI()
201 {
202     this.onMessageAdded = new EventSink(events.ConsoleMessageAdded);
203 }
204
205 ConsoleAPI.prototype = {
206     getMessages: function(callback)
207     {
208         extensionServer.sendRequest({ command: commands.GetConsoleMessages }, callback);
209     },
210
211     addMessage: function(severity, text, url, line)
212     {
213         extensionServer.sendRequest({ command: commands.AddConsoleMessage, severity: severity, text: text, url: url, line: line });
214     },
215
216     get Severity()
217     {
218         return apiPrivate.console.Severity;
219     }
220 }
221
222 /**
223  * @constructor
224  */
225 function Network()
226 {
227     /**
228      * @this {EventSinkImpl}
229      */
230     function dispatchRequestEvent(message)
231     {
232         var request = message.arguments[1];
233         request.__proto__ = new Request(message.arguments[0]);
234         this._fire(request);
235     }
236     this.onRequestFinished = new EventSink(events.NetworkRequestFinished, dispatchRequestEvent);
237     defineDeprecatedProperty(this, "network", "onFinished", "onRequestFinished");
238     this.onNavigated = new EventSink(events.InspectedURLChanged);
239 }
240
241 Network.prototype = {
242     getHAR: function(callback)
243     {
244         function callbackWrapper(result)
245         {
246             var entries = (result && result.entries) || [];
247             for (var i = 0; i < entries.length; ++i) {
248                 entries[i].__proto__ = new Request(entries[i]._requestId);
249                 delete entries[i]._requestId;
250             }
251             callback(result);
252         }
253         return extensionServer.sendRequest({ command: commands.GetHAR }, callback && callbackWrapper);
254     },
255
256     addRequestHeaders: function(headers)
257     {
258         return extensionServer.sendRequest({ command: commands.AddRequestHeaders, headers: headers, extensionId: window.location.hostname });
259     }
260 }
261
262 /**
263  * @constructor
264  */
265 function RequestImpl(id)
266 {
267     this._id = id;
268 }
269
270 RequestImpl.prototype = {
271     getContent: function(callback)
272     {
273         function callbackWrapper(response)
274         {
275             callback(response.content, response.encoding);
276         }
277         extensionServer.sendRequest({ command: commands.GetRequestContent, id: this._id }, callback && callbackWrapper);
278     }
279 }
280
281 /**
282  * @constructor
283  */
284 function Panels()
285 {
286     var panels = {
287         elements: new ElementsPanel(),
288         sources: new SourcesPanel(),
289     };
290
291     function panelGetter(name)
292     {
293         return panels[name];
294     }
295     for (var panel in panels)
296         this.__defineGetter__(panel, panelGetter.bind(null, panel));
297     this.applyStyleSheet = function(styleSheet) { extensionServer.sendRequest({ command: commands.ApplyStyleSheet, styleSheet: styleSheet }); };
298 }
299
300 Panels.prototype = {
301     create: function(title, icon, page, callback)
302     {
303         var id = "extension-panel-" + extensionServer.nextObjectId();
304         var request = {
305             command: commands.CreatePanel,
306             id: id,
307             title: title,
308             icon: icon,
309             page: page
310         };
311         extensionServer.sendRequest(request, callback && callback.bind(this, new ExtensionPanel(id)));
312     },
313
314     setOpenResourceHandler: function(callback)
315     {
316         var hadHandler = extensionServer.hasHandler(events.OpenResource);
317
318         function callbackWrapper(message)
319         {
320             // Allow the panel to show itself when handling the event.
321             userAction = true;
322             try {
323                 callback.call(null, new Resource(message.resource), message.lineNumber);
324             } finally {
325                 userAction = false;
326             }
327         }
328
329         if (!callback)
330             extensionServer.unregisterHandler(events.OpenResource);
331         else
332             extensionServer.registerHandler(events.OpenResource, callbackWrapper);
333
334         // Only send command if we either removed an existing handler or added handler and had none before.
335         if (hadHandler === !callback)
336             extensionServer.sendRequest({ command: commands.SetOpenResourceHandler, "handlerPresent": !!callback });
337     },
338
339     openResource: function(url, lineNumber, callback)
340     {
341         extensionServer.sendRequest({ command: commands.OpenResource, "url": url, "lineNumber": lineNumber }, callback);
342     },
343
344     get SearchAction()
345     {
346         return apiPrivate.panels.SearchAction;
347     }
348 }
349
350 /**
351  * @constructor
352  */
353 function ExtensionViewImpl(id)
354 {
355     this._id = id;
356
357     /**
358      * @this {EventSinkImpl}
359      */
360     function dispatchShowEvent(message)
361     {
362         var frameIndex = message.arguments[0];
363         if (typeof frameIndex === "number")
364             this._fire(window.parent.frames[frameIndex]);
365         else
366             this._fire();
367     }
368     this.onShown = new EventSink(events.ViewShown + id, dispatchShowEvent);
369     this.onHidden = new EventSink(events.ViewHidden + id);
370 }
371
372 /**
373  * @constructor
374  * @extends {ExtensionViewImpl}
375  */
376 function PanelWithSidebarImpl(hostPanelName)
377 {
378     this._hostPanelName = hostPanelName;
379     this.onSelectionChanged = new EventSink(events.PanelObjectSelected + hostPanelName);
380 }
381
382 PanelWithSidebarImpl.prototype = {
383     createSidebarPane: function(title, callback)
384     {
385         var id = "extension-sidebar-" + extensionServer.nextObjectId();
386         var request = {
387             command: commands.CreateSidebarPane,
388             panel: this._hostPanelName,
389             id: id,
390             title: title
391         };
392         function callbackWrapper()
393         {
394             callback(new ExtensionSidebarPane(id));
395         }
396         extensionServer.sendRequest(request, callback && callbackWrapper);
397     },
398
399     __proto__: ExtensionViewImpl.prototype
400 }
401
402 function declareInterfaceClass(implConstructor)
403 {
404     return function()
405     {
406         var impl = { __proto__: implConstructor.prototype };
407         implConstructor.apply(impl, arguments);
408         populateInterfaceClass(this, impl);
409     }
410 }
411
412 function defineDeprecatedProperty(object, className, oldName, newName)
413 {
414     var warningGiven = false;
415     function getter()
416     {
417         if (!warningGiven) {
418             console.warn(className + "." + oldName + " is deprecated. Use " + className + "." + newName + " instead");
419             warningGiven = true;
420         }
421         return object[newName];
422     }
423     object.__defineGetter__(oldName, getter);
424 }
425
426 function extractCallbackArgument(args)
427 {
428     var lastArgument = args[args.length - 1];
429     return typeof lastArgument === "function" ? lastArgument : undefined;
430 }
431
432 var AuditCategory = declareInterfaceClass(AuditCategoryImpl);
433 var AuditResult = declareInterfaceClass(AuditResultImpl);
434 var Button = declareInterfaceClass(ButtonImpl);
435 var EventSink = declareInterfaceClass(EventSinkImpl);
436 var ExtensionPanel = declareInterfaceClass(ExtensionPanelImpl);
437 var ExtensionSidebarPane = declareInterfaceClass(ExtensionSidebarPaneImpl);
438 var PanelWithSidebar = declareInterfaceClass(PanelWithSidebarImpl);
439 var Request = declareInterfaceClass(RequestImpl);
440 var Resource = declareInterfaceClass(ResourceImpl);
441 var Timeline = declareInterfaceClass(TimelineImpl);
442
443 /**
444  * @constructor
445  * @extends {PanelWithSidebar}
446  */
447 function ElementsPanel()
448 {
449     PanelWithSidebar.call(this, "elements");
450 }
451
452 ElementsPanel.prototype = {
453     __proto__: PanelWithSidebar.prototype
454 }
455
456 /**
457  * @constructor
458  * @extends {PanelWithSidebar}
459  */
460 function SourcesPanel()
461 {
462     PanelWithSidebar.call(this, "sources");
463 }
464
465 SourcesPanel.prototype = {
466     __proto__: PanelWithSidebar.prototype
467 }
468
469 /**
470  * @constructor
471  * @extends {ExtensionViewImpl}
472  */
473 function ExtensionPanelImpl(id)
474 {
475     ExtensionViewImpl.call(this, id);
476     this.onSearch = new EventSink(events.PanelSearch + id);
477 }
478
479 ExtensionPanelImpl.prototype = {
480     createStatusBarButton: function(iconPath, tooltipText, disabled)
481     {
482         var id = "button-" + extensionServer.nextObjectId();
483         var request = {
484             command: commands.CreateStatusBarButton,
485             panel: this._id,
486             id: id,
487             icon: iconPath,
488             tooltip: tooltipText,
489             disabled: !!disabled
490         };
491         extensionServer.sendRequest(request);
492         return new Button(id);
493     },
494
495     show: function()
496     {
497         if (!userAction)
498             return;
499
500         var request = {
501             command: commands.ShowPanel,
502             id: this._id
503         };
504         extensionServer.sendRequest(request);
505     },
506
507     __proto__: ExtensionViewImpl.prototype
508 }
509
510 /**
511  * @constructor
512  * @extends {ExtensionViewImpl}
513  */
514 function ExtensionSidebarPaneImpl(id)
515 {
516     ExtensionViewImpl.call(this, id);
517 }
518
519 ExtensionSidebarPaneImpl.prototype = {
520     setHeight: function(height)
521     {
522         extensionServer.sendRequest({ command: commands.SetSidebarHeight, id: this._id, height: height });
523     },
524
525     setExpression: function(expression, rootTitle, evaluateOptions)
526     {
527         var request = {
528             command: commands.SetSidebarContent,
529             id: this._id,
530             expression: expression,
531             rootTitle: rootTitle,
532             evaluateOnPage: true,
533         };
534         if (typeof evaluateOptions === "object")
535             request.evaluateOptions = evaluateOptions;
536         extensionServer.sendRequest(request, extractCallbackArgument(arguments));
537     },
538
539     setObject: function(jsonObject, rootTitle, callback)
540     {
541         extensionServer.sendRequest({ command: commands.SetSidebarContent, id: this._id, expression: jsonObject, rootTitle: rootTitle }, callback);
542     },
543
544     setPage: function(page)
545     {
546         extensionServer.sendRequest({ command: commands.SetSidebarPage, id: this._id, page: page });
547     },
548
549     __proto__: ExtensionViewImpl.prototype
550 }
551
552 /**
553  * @constructor
554  */
555 function ButtonImpl(id)
556 {
557     this._id = id;
558     this.onClicked = new EventSink(events.ButtonClicked + id);
559 }
560
561 ButtonImpl.prototype = {
562     update: function(iconPath, tooltipText, disabled)
563     {
564         var request = {
565             command: commands.UpdateButton,
566             id: this._id,
567             icon: iconPath,
568             tooltip: tooltipText,
569             disabled: !!disabled
570         };
571         extensionServer.sendRequest(request);
572     }
573 };
574
575 /**
576  * @constructor
577  */
578 function Audits()
579 {
580 }
581
582 Audits.prototype = {
583     addCategory: function(displayName, resultCount)
584     {
585         var id = "extension-audit-category-" + extensionServer.nextObjectId();
586         if (typeof resultCount !== "undefined")
587             console.warn("Passing resultCount to audits.addCategory() is deprecated. Use AuditResult.updateProgress() instead.");
588         extensionServer.sendRequest({ command: commands.AddAuditCategory, id: id, displayName: displayName, resultCount: resultCount });
589         return new AuditCategory(id);
590     }
591 }
592
593 /**
594  * @constructor
595  */
596 function AuditCategoryImpl(id)
597 {
598     /**
599      * @this {EventSinkImpl}
600      */
601     function dispatchAuditEvent(request)
602     {
603         var auditResult = new AuditResult(request.arguments[0]);
604         try {
605             this._fire(auditResult);
606         } catch (e) {
607             console.error("Uncaught exception in extension audit event handler: " + e);
608             auditResult.done();
609         }
610     }
611     this._id = id;
612     this.onAuditStarted = new EventSink(events.AuditStarted + id, dispatchAuditEvent);
613 }
614
615 /**
616  * @constructor
617  */
618 function AuditResultImpl(id)
619 {
620     this._id = id;
621
622     this.createURL = this._nodeFactory.bind(null, "url");
623     this.createSnippet = this._nodeFactory.bind(null, "snippet");
624     this.createText = this._nodeFactory.bind(null, "text");
625     this.createObject = this._nodeFactory.bind(null, "object");
626     this.createNode = this._nodeFactory.bind(null, "node");
627 }
628
629 AuditResultImpl.prototype = {
630     addResult: function(displayName, description, severity, details)
631     {
632         // shorthand for specifying details directly in addResult().
633         if (details && !(details instanceof AuditResultNode))
634             details = new AuditResultNode(details instanceof Array ? details : [details]);
635
636         var request = {
637             command: commands.AddAuditResult,
638             resultId: this._id,
639             displayName: displayName,
640             description: description,
641             severity: severity,
642             details: details
643         };
644         extensionServer.sendRequest(request);
645     },
646
647     createResult: function()
648     {
649         return new AuditResultNode(Array.prototype.slice.call(arguments));
650     },
651
652     updateProgress: function(worked, totalWork)
653     {
654         extensionServer.sendRequest({ command: commands.UpdateAuditProgress, resultId: this._id, progress: worked / totalWork });
655     },
656
657     done: function()
658     {
659         extensionServer.sendRequest({ command: commands.StopAuditCategoryRun, resultId: this._id });
660     },
661
662     get Severity()
663     {
664         return apiPrivate.audits.Severity;
665     },
666
667     createResourceLink: function(url, lineNumber)
668     {
669         return {
670             type: "resourceLink",
671             arguments: [url, lineNumber && lineNumber - 1]
672         };
673     },
674
675     _nodeFactory: function(type)
676     {
677         return {
678             type: type,
679             arguments: Array.prototype.slice.call(arguments, 1)
680         };
681     }
682 }
683
684 /**
685  * @constructor
686  */
687 function AuditResultNode(contents)
688 {
689     this.contents = contents;
690     this.children = [];
691     this.expanded = false;
692 }
693
694 AuditResultNode.prototype = {
695     addChild: function()
696     {
697         var node = new AuditResultNode(Array.prototype.slice.call(arguments));
698         this.children.push(node);
699         return node;
700     }
701 };
702
703 /**
704  * @constructor
705  */
706 function InspectedWindow()
707 {
708     /**
709      * @this {EventSinkImpl}
710      */
711     function dispatchResourceEvent(message)
712     {
713         this._fire(new Resource(message.arguments[0]));
714     }
715
716     /**
717      * @this {EventSinkImpl}
718      */
719     function dispatchResourceContentEvent(message)
720     {
721         this._fire(new Resource(message.arguments[0]), message.arguments[1]);
722     }
723
724     this.onResourceAdded = new EventSink(events.ResourceAdded, dispatchResourceEvent);
725     this.onResourceContentCommitted = new EventSink(events.ResourceContentCommitted, dispatchResourceContentEvent);
726 }
727
728 InspectedWindow.prototype = {
729     reload: function(optionsOrUserAgent)
730     {
731         var options = null;
732         if (typeof optionsOrUserAgent === "object")
733             options = optionsOrUserAgent;
734         else if (typeof optionsOrUserAgent === "string") {
735             options = { userAgent: optionsOrUserAgent };
736             console.warn("Passing userAgent as string parameter to inspectedWindow.reload() is deprecated. " +
737                          "Use inspectedWindow.reload({ userAgent: value}) instead.");
738         }
739         return extensionServer.sendRequest({ command: commands.Reload, options: options });
740     },
741
742     eval: function(expression, evaluateOptions)
743     {
744         var callback = extractCallbackArgument(arguments);
745         function callbackWrapper(result)
746         {
747             if (result.isError || result.isException)
748                 callback(undefined, result);
749             else
750                 callback(result.value);
751         }
752         var request = {
753             command: commands.EvaluateOnInspectedPage,
754             expression: expression
755         };
756         if (typeof evaluateOptions === "object")
757             request.evaluateOptions = evaluateOptions;
758         return extensionServer.sendRequest(request, callback && callbackWrapper);
759     },
760
761     getResources: function(callback)
762     {
763         function wrapResource(resourceData)
764         {
765             return new Resource(resourceData);
766         }
767         function callbackWrapper(resources)
768         {
769             callback(resources.map(wrapResource));
770         }
771         return extensionServer.sendRequest({ command: commands.GetPageResources }, callback && callbackWrapper);
772     }
773 }
774
775 /**
776  * @constructor
777  */
778 function ResourceImpl(resourceData)
779 {
780     this._url = resourceData.url
781     this._type = resourceData.type;
782 }
783
784 ResourceImpl.prototype = {
785     get url()
786     {
787         return this._url;
788     },
789
790     get type()
791     {
792         return this._type;
793     },
794
795     getContent: function(callback)
796     {
797         function callbackWrapper(response)
798         {
799             callback(response.content, response.encoding);
800         }
801
802         return extensionServer.sendRequest({ command: commands.GetResourceContent, url: this._url }, callback && callbackWrapper);
803     },
804
805     setContent: function(content, commit, callback)
806     {
807         return extensionServer.sendRequest({ command: commands.SetResourceContent, url: this._url, content: content, commit: commit }, callback);
808     }
809 }
810
811 /**
812  * @constructor
813  */
814 function TimelineImpl()
815 {
816     this.onEventRecorded = new EventSink(events.TimelineEventRecorded);
817 }
818
819 function forwardKeyboardEvent(event)
820 {
821     const Esc = "U+001B";
822     // We only care about global hotkeys, not about random text
823     if (!event.ctrlKey && !event.altKey && !event.metaKey && !/^F\d+$/.test(event.keyIdentifier) && event.keyIdentifier !== Esc)
824         return;
825     var request = {
826         command: commands.ForwardKeyboardEvent,
827         eventType: event.type,
828         ctrlKey: event.ctrlKey,
829         altKey: event.altKey,
830         metaKey: event.metaKey,
831         keyIdentifier: event.keyIdentifier,
832         location: event.location
833     };
834     extensionServer.sendRequest(request);
835 }
836
837 document.addEventListener("keydown", forwardKeyboardEvent, false);
838 document.addEventListener("keypress", forwardKeyboardEvent, false);
839
840 /**
841  * @constructor
842  */
843 function ExtensionServerClient()
844 {
845     this._callbacks = {};
846     this._handlers = {};
847     this._lastRequestId = 0;
848     this._lastObjectId = 0;
849
850     this.registerHandler("callback", this._onCallback.bind(this));
851
852     var channel = new MessageChannel();
853     this._port = channel.port1;
854     this._port.addEventListener("message", this._onMessage.bind(this), false);
855     this._port.start();
856
857     window.parent.postMessage("registerExtension", [ channel.port2 ], "*");
858 }
859
860 ExtensionServerClient.prototype = {
861     /**
862      * @param {function()=} callback
863      */
864     sendRequest: function(message, callback)
865     {
866         if (typeof callback === "function")
867             message.requestId = this._registerCallback(callback);
868         return this._port.postMessage(message);
869     },
870
871     hasHandler: function(command)
872     {
873         return !!this._handlers[command];
874     },
875
876     registerHandler: function(command, handler)
877     {
878         this._handlers[command] = handler;
879     },
880
881     unregisterHandler: function(command)
882     {
883         delete this._handlers[command];
884     },
885
886     nextObjectId: function()
887     {
888         return injectedScriptId + "_" + ++this._lastObjectId;
889     },
890
891     _registerCallback: function(callback)
892     {
893         var id = ++this._lastRequestId;
894         this._callbacks[id] = callback;
895         return id;
896     },
897
898     _onCallback: function(request)
899     {
900         if (request.requestId in this._callbacks) {
901             var callback = this._callbacks[request.requestId];
902             delete this._callbacks[request.requestId];
903             callback(request.result);
904         }
905     },
906
907     _onMessage: function(event)
908     {
909         var request = event.data;
910         var handler = this._handlers[request.command];
911         if (handler)
912             handler.call(this, request);
913     }
914 }
915
916 function populateInterfaceClass(interface, implementation)
917 {
918     for (var member in implementation) {
919         if (member.charAt(0) === "_")
920             continue;
921         var descriptor = null;
922         // Traverse prototype chain until we find the owner.
923         for (var owner = implementation; owner && !descriptor; owner = owner.__proto__)
924             descriptor = Object.getOwnPropertyDescriptor(owner, member);
925         if (!descriptor)
926             continue;
927         if (typeof descriptor.value === "function")
928             interface[member] = descriptor.value.bind(implementation);
929         else if (typeof descriptor.get === "function")
930             interface.__defineGetter__(member, descriptor.get.bind(implementation));
931         else
932             Object.defineProperty(interface, member, descriptor);
933     }
934 }
935
936 // extensionServer is a closure variable defined by the glue below -- make sure we fail if it's not there.
937 if (!extensionServer)
938     extensionServer = new ExtensionServerClient();
939
940 return new InspectorExtensionAPI();
941 }
942
943 /**
944  * @param {!ExtensionDescriptor} extensionInfo
945  * @return {string}
946  */
947 function buildExtensionAPIInjectedScript(extensionInfo)
948 {
949     return "(function(injectedScriptId){ " +
950         "var extensionServer;" +
951         defineCommonExtensionSymbols.toString() + ";" +
952         injectedExtensionAPI.toString() + ";" +
953         buildPlatformExtensionAPI(extensionInfo) + ";" +
954         "platformExtensionAPI(injectedExtensionAPI(injectedScriptId));" +
955         "return {};" +
956         "})";
957 }
958 /*
959  * Copyright (C) 2011 Google Inc. All rights reserved.
960  *
961  * Redistribution and use in source and binary forms, with or without
962  * modification, are permitted provided that the following conditions are
963  * met:
964  *
965  *     * Redistributions of source code must retain the above copyright
966  * notice, this list of conditions and the following disclaimer.
967  *     * Redistributions in binary form must reproduce the above
968  * copyright notice, this list of conditions and the following disclaimer
969  * in the documentation and/or other materials provided with the
970  * distribution.
971  *     * Neither the name of Google Inc. nor the names of its
972  * contributors may be used to endorse or promote products derived from
973  * this software without specific prior written permission.
974  *
975  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
976  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
977  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
978  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
979  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
980  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
981  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
982  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
983  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
984  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
985  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
986  */
987
988 function platformExtensionAPI(coreAPI)
989 {
990     function getTabId()
991     {
992         return tabId;
993     }
994     chrome = window.chrome || {};
995     // Override chrome.devtools as a workaround for a error-throwing getter being exposed
996     // in extension pages loaded into a non-extension process (only happens for remote client
997     // extensions)
998     var devtools_descriptor = Object.getOwnPropertyDescriptor(chrome, "devtools");
999     if (!devtools_descriptor || devtools_descriptor.get)
1000         Object.defineProperty(chrome, "devtools", { value: {}, enumerable: true });
1001     // Only expose tabId on chrome.devtools.inspectedWindow, not webInspector.inspectedWindow.
1002     chrome.devtools.inspectedWindow = {};
1003     chrome.devtools.inspectedWindow.__defineGetter__("tabId", getTabId);
1004     chrome.devtools.inspectedWindow.__proto__ = coreAPI.inspectedWindow;
1005     chrome.devtools.network = coreAPI.network;
1006     chrome.devtools.panels = coreAPI.panels;
1007
1008     // default to expose experimental APIs for now.
1009     if (extensionInfo.exposeExperimentalAPIs !== false) {
1010         chrome.experimental = chrome.experimental || {};
1011         chrome.experimental.devtools = chrome.experimental.devtools || {};
1012
1013         var properties = Object.getOwnPropertyNames(coreAPI);
1014         for (var i = 0; i < properties.length; ++i) {
1015             var descriptor = Object.getOwnPropertyDescriptor(coreAPI, properties[i]);
1016             Object.defineProperty(chrome.experimental.devtools, properties[i], descriptor);
1017         }
1018         chrome.experimental.devtools.inspectedWindow = chrome.devtools.inspectedWindow;
1019     }
1020     if (extensionInfo.exposeWebInspectorNamespace)
1021         window.webInspector = coreAPI;
1022 }
1023
1024         var tabId;
1025         var extensionInfo = {};
1026         var extensionServer;
1027         platformExtensionAPI(injectedExtensionAPI("remote-" + window.parent.frames.length));
1028     })();