3 Copyright (c) 2013 The Chromium Authors. All rights reserved.
4 Use of this source code is governed by a BSD-style license that can be
5 found in the LICENSE file.
7 <link rel="import" href="/base.html">
13 * Contains connection code that inspector's embedding framework calls on
14 * tracing, and that tracing can use to talk to inspector.
16 tv.exportTo('about_tracing', function() {
17 // Shim needed to keep the bootup code of chrome://inspect happy.
18 window.WebInspector = { };
19 window.WebInspector.addExtensions = function() { };
21 // Interface used by inspector when it hands data to us from the backend.
22 window.InspectorFrontendAPI = {
23 setToolbarColors: function() { },
25 dispatchMessage: function(payload) {
26 throw new Error('Should have been patched by InspectorConnection');
33 function InspectorConnection() {
34 if (InspectorConnection.instance)
35 throw new Error('Singleton');
37 this.nextRequestId_ = 1;
38 this.pendingRequestResolversId_ = {};
40 this.notificationListenersByMethodName_ = {};
41 InspectorFrontendAPI.dispatchMessage = this.dispatchMessage_.bind(this);
44 InspectorConnection.prototype = {
45 req: function(method, params) {
46 var id = this.nextRequestId_++;
47 var msg = JSON.stringify({
52 InspectorFrontendHost.sendMessageToBackend(msg);
54 return new Promise(function(resolve, reject) {
55 this.pendingRequestResolversId_[id] = {
62 setNotificationListener: function(method, listener) {
63 this.notificationListenersByMethodName_[method] = listener;
66 dispatchMessage_: function(payload) {
67 // Special handling for Tracing.dataCollected because it is high
69 if (payload.indexOf('"method": "Tracing.dataCollected"') !== -1) {
70 var listener = this.notificationListenersByMethodName_[
71 'Tracing.dataCollected'];
78 var message = JSON.parse(payload);
80 var resolver = this.pendingRequestResolversId_[message.id];
81 if (resolver === undefined) {
82 console.log('Unrecognized ack', message);
86 resolver.reject(message.error);
89 resolver.resolve(message.result);
93 if (message['method']) {
94 var listener = this.notificationListenersByMethodName_[message.method];
95 if (listener === undefined) {
96 console.log('Unhandled ', message.method);
99 listener(message.params);
102 console.log('Unknown dispatchMessage: ', payload);
106 if (window.InspectorFrontendHost)
107 InspectorConnection.instance = new InspectorConnection();
109 InspectorConnection.instance = undefined;
112 InspectorConnection: InspectorConnection