| <!DOCTYPE html> |
| <!-- |
| Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| Use of this source code is governed by a BSD-style license that can be |
| found in the LICENSE file. |
| --> |
| <link rel="import" href="/base/base.html"> |
| <script> |
| |
| 'use strict'; |
| |
| /** |
| * Contains connection code that inspector's embedding framework calls on |
| * tracing, and that tracing can use to talk to inspector. |
| */ |
| tr.exportTo('tr.ui.e.about_tracing', function() { |
| // Interface used by inspector when it hands data to us from the backend. |
| window.DevToolsAPI = { |
| setToolbarColors: function() { }, |
| addExtensions: function() { }, |
| setInspectedPageId: function() { }, |
| |
| dispatchMessage: function(payload) { |
| throw new Error('Should have been patched by InspectorConnection'); |
| } |
| }; |
| // Temporary until inspector backend switches to DevToolsAPI. |
| window.InspectorFrontendAPI = window.DevToolsAPI; |
| |
| /** |
| * @constructor |
| */ |
| function InspectorConnection() { |
| if (InspectorConnection.instance) |
| throw new Error('Singleton'); |
| |
| this.nextRequestId_ = 1; |
| this.pendingRequestResolversId_ = {}; |
| |
| this.notificationListenersByMethodName_ = {}; |
| DevToolsAPI.dispatchMessage = this.dispatchMessage_.bind(this); |
| } |
| |
| InspectorConnection.prototype = { |
| req: function(method, params) { |
| var id = this.nextRequestId_++; |
| var msg = JSON.stringify({ |
| id: id, |
| method: method, |
| params: params |
| }); |
| DevToolsHost.sendMessageToBackend(msg); |
| |
| return new Promise(function(resolve, reject) { |
| this.pendingRequestResolversId_[id] = { |
| resolve: resolve, |
| reject: reject |
| }; |
| }.bind(this)); |
| }, |
| |
| setNotificationListener: function(method, listener) { |
| this.notificationListenersByMethodName_[method] = listener; |
| }, |
| |
| dispatchMessage_: function(payload) { |
| var isStringPayload = typeof payload === 'string'; |
| // Special handling for Tracing.dataCollected because it is high |
| // bandwidth. |
| var isDataCollectedMessage = isStringPayload ? |
| payload.indexOf('"method": "Tracing.dataCollected"') !== -1 : |
| payload.method === 'Tracing.dataCollected'; |
| if (isDataCollectedMessage) { |
| var listener = this.notificationListenersByMethodName_[ |
| 'Tracing.dataCollected']; |
| if (listener) { |
| // FIXME(loislo): trace viewer should be able to process |
| // raw message object because string based version a few times |
| // slower on the browser side. |
| // see https://codereview.chromium.org/784513002. |
| listener(isStringPayload ? payload : JSON.stringify(payload)); |
| return; |
| } |
| } |
| |
| var message = isStringPayload ? JSON.parse(payload) : payload; |
| if (message.id) { |
| var resolver = this.pendingRequestResolversId_[message.id]; |
| if (resolver === undefined) { |
| console.log('Unrecognized ack', message); |
| return; |
| } |
| if (message.error) { |
| resolver.reject(message.error); |
| return; |
| } |
| resolver.resolve(message.result); |
| return; |
| } |
| |
| if (message['method']) { |
| var listener = this.notificationListenersByMethodName_[message.method]; |
| if (listener === undefined) { |
| console.log('Unhandled ', message.method); |
| return; |
| } |
| listener(message.params); |
| return; |
| } |
| console.log('Unknown dispatchMessage: ', payload); |
| } |
| }; |
| |
| if (window.DevToolsHost) |
| InspectorConnection.instance = new InspectorConnection(); |
| else |
| InspectorConnection.instance = undefined; |
| |
| return { |
| InspectorConnection: InspectorConnection |
| }; |
| }); |
| </script> |