Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / trace-viewer / trace_viewer / about_tracing / inspector_connection.html
1 <!DOCTYPE html>
2 <!--
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.
6 -->
7 <link rel="import" href="/base.html">
8 <script>
9
10 'use strict';
11
12 /**
13  * Contains connection code that inspector's embedding framework calls on
14  * tracing, and that tracing can use to talk to inspector.
15  */
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() { };
20
21   // Interface used by inspector when it hands data to us from the backend.
22   window.InspectorFrontendAPI = {
23     setToolbarColors: function() { },
24
25     dispatchMessage: function(payload) {
26       throw new Error('Should have been patched by InspectorConnection');
27     }
28   };
29
30   /**
31    * @constructor
32    */
33   function InspectorConnection() {
34     if (InspectorConnection.instance)
35       throw new Error('Singleton');
36
37     this.nextRequestId_ = 1;
38     this.pendingRequestResolversId_ = {};
39
40     this.notificationListenersByMethodName_ = {};
41     InspectorFrontendAPI.dispatchMessage = this.dispatchMessage_.bind(this);
42   }
43
44   InspectorConnection.prototype = {
45     req: function(method, params) {
46       var id = this.nextRequestId_++;
47       var msg = JSON.stringify({
48         id: id,
49         method: method,
50         params: params
51       });
52       InspectorFrontendHost.sendMessageToBackend(msg);
53
54       return new Promise(function(resolve, reject) {
55         this.pendingRequestResolversId_[id] = {
56           resolve: resolve,
57           reject: reject
58         };
59       }.bind(this));
60     },
61
62     setNotificationListener: function(method, listener) {
63       this.notificationListenersByMethodName_[method] = listener;
64     },
65
66     dispatchMessage_: function(payload) {
67       // Special handling for Tracing.dataCollected because it is high
68       // bandwidth.
69       if (payload.indexOf('"method": "Tracing.dataCollected"') !== -1) {
70         var listener = this.notificationListenersByMethodName_[
71             'Tracing.dataCollected'];
72         if (listener) {
73           listener(payload);
74           return;
75         }
76       }
77
78       var message = JSON.parse(payload);
79       if (message.id) {
80         var resolver = this.pendingRequestResolversId_[message.id];
81         if (resolver === undefined) {
82           console.log('Unrecognized ack', message);
83           return;
84         }
85         if (message.error) {
86           resolver.reject(message.error);
87           return;
88         }
89         resolver.resolve(message.result);
90         return;
91       }
92
93       if (message['method']) {
94         var listener = this.notificationListenersByMethodName_[message.method];
95         if (listener === undefined) {
96           console.log('Unhandled ', message.method);
97           return;
98         }
99         listener(message.params);
100         return;
101       }
102       console.log('Unknown dispatchMessage: ', payload);
103     }
104   };
105
106   if (window.InspectorFrontendHost)
107     InspectorConnection.instance = new InspectorConnection();
108   else
109     InspectorConnection.instance = undefined;
110
111   return {
112     InspectorConnection: InspectorConnection
113   };
114 });
115 </script>