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() { 5 /** 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. 9 * @constructor 10 */ 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); 19 } 20 21 this.nextRequestId_ = 0; 22 this.pendingCallbacks_ = []; 23 this.logMessages_ = []; 24 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_(); 30 } 31 } 32 33 BrowserBridge.prototype = { 34 __proto__: cr.EventTarget.prototype, 35 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'); 44 }, 45 46 /** 47 * Returns true if the page is hosted inside Chrome WebUI 48 * Helps have behavior conditional to emulate_webui.py 49 */ 50 get debugMode() { 51 return this.debugMode_; 52 }, 53 54 /** 55 * Sends a message to the browser with specified args. The 56 * browser will reply asynchronously via the provided callback. 57 */ 58 callAsync: function(submessage, args, callback) { 59 var requestId = this.nextRequestId_; 60 this.nextRequestId_ += 1; 61 this.pendingCallbacks_[requestId] = callback; 62 if (!args) { 63 chrome.send('callAsync', [requestId.toString(), submessage]); 64 } else { 65 var allArgs = [requestId.toString(), submessage].concat(args); 66 chrome.send('callAsync', allArgs); 67 } 68 }, 69 70 /** 71 * Called by gpu c++ code when client info is ready. 72 */ 73 onCallAsyncReply: function(requestId, args) { 74 if (this.pendingCallbacks_[requestId] === undefined) { 75 throw new Error('requestId ' + requestId + ' is not pending'); 76 } 77 var callback = this.pendingCallbacks_[requestId]; 78 callback(args); 79 delete this.pendingCallbacks_[requestId]; 80 }, 81 82 /** 83 * Get gpuInfo data. 84 */ 85 get gpuInfo() { 86 return this.gpuInfo_; 87 }, 88 89 /** 90 * Called from gpu c++ code when GPU Info is updated. 91 */ 92 onGpuInfoUpdate: function(gpuInfo) { 93 this.gpuInfo_ = gpuInfo; 94 cr.dispatchSimpleEvent(this, 'gpuInfoUpdate'); 95 }, 96 97 /** 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. 100 */ 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); 105 } else { 106 this.clientInfo_ = data; 107 cr.dispatchSimpleEvent(this, 'clientInfoChange'); 108 } 109 }).bind(this)); 110 }, 111 112 /** 113 * Returns information about the currently running Chrome build. 114 */ 115 get clientInfo() { 116 return this.clientInfo_; 117 }, 118 119 /** 120 * This function checks for new GPU_LOG messages. 121 * If any are found, a refresh is triggered. 122 */ 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'); 129 } 130 // check again in 250 ms 131 window.setTimeout(this.beginRequestLogMessages_.bind(this), 250); 132 }).bind(this)); 133 }, 134 135 /** 136 * Returns an array of log messages issued by the GPU process, if any. 137 */ 138 get logMessages() { 139 return this.logMessages_; 140 }, 141 142 }; 143 144 return { 145 BrowserBridge: BrowserBridge 146 }; 147 }); 148