1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
// Copyright (c) 2011 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.
cr.define('gpu', function() {
/**
* This class provides a 'bridge' for communicating between javascript and the
* browser. When run outside of WebUI, e.g. as a regular webpage, it provides
* synthetic data to assist in testing.
* @constructor
*/
function BrowserBridge() {
// If we are not running inside WebUI, output chrome.send messages
// to the console to help with quick-iteration debugging.
this.debugMode_ = (chrome.send === undefined && console.log);
if (this.debugMode_) {
var browserBridgeTests = document.createElement('script');
browserBridgeTests.src = './gpu_internals/browser_bridge_tests.js';
document.body.appendChild(browserBridgeTests);
}
this.nextRequestId_ = 0;
this.pendingCallbacks_ = [];
this.logMessages_ = [];
// Tell c++ code that we are ready to receive GPU Info.
if (!this.debugMode_) {
chrome.send('browserBridgeInitialized');
this.beginRequestClientInfo_();
this.beginRequestLogMessages_();
}
}
BrowserBridge.prototype = {
__proto__: cr.EventTarget.prototype,
applySimulatedData_: function applySimulatedData(data) {
// set up things according to the simulated data
this.gpuInfo_ = data.gpuInfo;
this.gpuMemoryBufferInfo_ = data.gpuMemoryBufferInfo;
this.clientInfo_ = data.clientInfo;
this.logMessages_ = data.logMessages;
cr.dispatchSimpleEvent(this, 'gpuInfoUpdate');
cr.dispatchSimpleEvent(this, 'gpuMemoryBufferInfoUpdate');
cr.dispatchSimpleEvent(this, 'clientInfoChange');
cr.dispatchSimpleEvent(this, 'logMessagesChange');
},
/**
* Returns true if the page is hosted inside Chrome WebUI
* Helps have behavior conditional to emulate_webui.py
*/
get debugMode() {
return this.debugMode_;
},
/**
* Sends a message to the browser with specified args. The
* browser will reply asynchronously via the provided callback.
*/
callAsync: function(submessage, args, callback) {
var requestId = this.nextRequestId_;
this.nextRequestId_ += 1;
this.pendingCallbacks_[requestId] = callback;
if (!args) {
chrome.send('callAsync', [requestId.toString(), submessage]);
} else {
var allArgs = [requestId.toString(), submessage].concat(args);
chrome.send('callAsync', allArgs);
}
},
/**
* Called by gpu c++ code when client info is ready.
*/
onCallAsyncReply: function(requestId, args) {
if (this.pendingCallbacks_[requestId] === undefined) {
throw new Error('requestId ' + requestId + ' is not pending');
}
var callback = this.pendingCallbacks_[requestId];
callback(args);
delete this.pendingCallbacks_[requestId];
},
/**
* Get gpuInfo data.
*/
get gpuInfo() {
return this.gpuInfo_;
},
/**
* Called from gpu c++ code when GPU Info is updated.
*/
onGpuInfoUpdate: function(gpuInfo) {
this.gpuInfo_ = gpuInfo;
cr.dispatchSimpleEvent(this, 'gpuInfoUpdate');
},
/**
* Get gpuMemoryBufferInfo data.
*/
get gpuMemoryBufferInfo() {
return this.gpuMemoryBufferInfo_;
},
/**
* Called from gpu c++ code when GpuMemoryBuffer Info is updated.
*/
onGpuMemoryBufferInfoUpdate: function(gpuMemoryBufferInfo) {
this.gpuMemoryBufferInfo_ = gpuMemoryBufferInfo;
cr.dispatchSimpleEvent(this, 'gpuMemoryBufferInfoUpdate');
},
/**
* This function begins a request for the ClientInfo. If it comes back
* as undefined, then we will issue the request again in 250ms.
*/
beginRequestClientInfo_: function() {
this.callAsync('requestClientInfo', undefined, (function(data) {
if (data === undefined) { // try again in 250 ms
window.setTimeout(this.beginRequestClientInfo_.bind(this), 250);
} else {
this.clientInfo_ = data;
cr.dispatchSimpleEvent(this, 'clientInfoChange');
}
}).bind(this));
},
/**
* Returns information about the currently running Chrome build.
*/
get clientInfo() {
return this.clientInfo_;
},
/**
* This function checks for new GPU_LOG messages.
* If any are found, a refresh is triggered.
*/
beginRequestLogMessages_: function() {
this.callAsync('requestLogMessages', undefined,
(function(messages) {
if (messages.length != this.logMessages_.length) {
this.logMessages_ = messages;
cr.dispatchSimpleEvent(this, 'logMessagesChange');
}
// check again in 250 ms
window.setTimeout(this.beginRequestLogMessages_.bind(this), 250);
}).bind(this));
},
/**
* Returns an array of log messages issued by the GPU process, if any.
*/
get logMessages() {
return this.logMessages_;
},
/**
* Returns the value of the "Sandboxed" row.
*/
isSandboxedForTesting : function() {
for (i = 0; i < this.gpuInfo_.basic_info.length; ++i) {
var info = this.gpuInfo_.basic_info[i];
if (info.description == "Sandboxed")
return info.value;
}
return false;
}
};
return {
BrowserBridge: BrowserBridge
};
});
|