1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 cr.define('gpu', function() {
6 * This class provides a 'bridge' for communicating between javascript and the
7 * browser. When run outside of WebUI, e.g. as a regular webpage, it provides
8 * synthetic data to assist in testing.
11 function BrowserBridge() {
12 // If we are not running inside WebUI, output chrome.send messages
13 // to the console to help with quick-iteration debugging.
14 this.debugMode_ = (chrome.send === undefined && console.log);
15 if (this.debugMode_) {
16 var browserBridgeTests = document.createElement('script');
17 browserBridgeTests.src = './gpu_internals/browser_bridge_tests.js';
18 document.body.appendChild(browserBridgeTests);
21 this.nextRequestId_ = 0;
22 this.pendingCallbacks_ = [];
23 this.logMessages_ = [];
25 // Tell c++ code that we are ready to receive GPU Info.
26 if (!this.debugMode_) {
27 chrome.send('browserBridgeInitialized');
28 this.beginRequestClientInfo_();
29 this.beginRequestLogMessages_();
33 BrowserBridge.prototype = {
34 __proto__: cr.EventTarget.prototype,
36 applySimulatedData_: function applySimulatedData(data) {
37 // set up things according to the simulated data
38 this.gpuInfo_ = data.gpuInfo;
39 this.clientInfo_ = data.clientInfo;
40 this.logMessages_ = data.logMessages;
41 cr.dispatchSimpleEvent(this, 'gpuInfoUpdate');
42 cr.dispatchSimpleEvent(this, 'clientInfoChange');
43 cr.dispatchSimpleEvent(this, 'logMessagesChange');
47 * Returns true if the page is hosted inside Chrome WebUI
48 * Helps have behavior conditional to emulate_webui.py
51 return this.debugMode_;
55 * Sends a message to the browser with specified args. The
56 * browser will reply asynchronously via the provided callback.
58 callAsync: function(submessage, args, callback) {
59 var requestId = this.nextRequestId_;
60 this.nextRequestId_ += 1;
61 this.pendingCallbacks_[requestId] = callback;
63 chrome.send('callAsync', [requestId.toString(), submessage]);
65 var allArgs = [requestId.toString(), submessage].concat(args);
66 chrome.send('callAsync', allArgs);
71 * Called by gpu c++ code when client info is ready.
73 onCallAsyncReply: function(requestId, args) {
74 if (this.pendingCallbacks_[requestId] === undefined) {
75 throw new Error('requestId ' + requestId + ' is not pending');
77 var callback = this.pendingCallbacks_[requestId];
79 delete this.pendingCallbacks_[requestId];
90 * Called from gpu c++ code when GPU Info is updated.
92 onGpuInfoUpdate: function(gpuInfo) {
93 this.gpuInfo_ = gpuInfo;
94 cr.dispatchSimpleEvent(this, 'gpuInfoUpdate');
98 * This function begins a request for the ClientInfo. If it comes back
99 * as undefined, then we will issue the request again in 250ms.
101 beginRequestClientInfo_: function() {
102 this.callAsync('requestClientInfo', undefined, (function(data) {
103 if (data === undefined) { // try again in 250 ms
104 window.setTimeout(this.beginRequestClientInfo_.bind(this), 250);
106 this.clientInfo_ = data;
107 cr.dispatchSimpleEvent(this, 'clientInfoChange');
113 * Returns information about the currently running Chrome build.
116 return this.clientInfo_;
120 * This function checks for new GPU_LOG messages.
121 * If any are found, a refresh is triggered.
123 beginRequestLogMessages_: function() {
124 this.callAsync('requestLogMessages', undefined,
125 (function(messages) {
126 if (messages.length != this.logMessages_.length) {
127 this.logMessages_ = messages;
128 cr.dispatchSimpleEvent(this, 'logMessagesChange');
130 // check again in 250 ms
131 window.setTimeout(this.beginRequestLogMessages_.bind(this), 250);
136 * Returns an array of log messages issued by the GPU process, if any.
139 return this.logMessages_;
145 BrowserBridge: BrowserBridge