OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 |
| 5 cr.define('gpu', function() { |
| 6 /** |
| 7 * This class provides a 'bridge' for communicating between javascript and |
| 8 * the browser. |
| 9 * @constructor |
| 10 */ |
| 11 function BrowserBridge() { |
| 12 // If we are not running inside DOMUI, output chrome.send messages |
| 13 // to the console to help with quick-iteration debugging. |
| 14 if (chrome.send === undefined && console.log) { |
| 15 chrome.send = function(messageHandler, args) { |
| 16 console.log('chrome.send', messageHandler, args); |
| 17 }; |
| 18 } |
| 19 |
| 20 this.nextRequestId_ = 0; |
| 21 this.pendingCallbacks_ = []; |
| 22 } |
| 23 |
| 24 BrowserBridge.prototype = { |
| 25 __proto__: Object.prototype, |
| 26 |
| 27 /** |
| 28 * Sends a message to the browser with specified args. The |
| 29 * browser will reply asynchronously via the provided callback. |
| 30 */ |
| 31 callAsync: function(submessage, args, callback) { |
| 32 var requestId = this.nextRequestId_; |
| 33 this.nextRequestId_ += 1; |
| 34 this.pendingCallbacks_[requestId] = callback; |
| 35 if (!args) { |
| 36 chrome.send('callAsync', [requestId.toString(), submessage]); |
| 37 } else { |
| 38 var allArgs = [requestId.toString(), submessage].concat(args); |
| 39 chrome.send('callAsync', allArgs); |
| 40 } |
| 41 }, |
| 42 |
| 43 /** |
| 44 * Called by gpu c++ code when client info is ready. |
| 45 */ |
| 46 onCallAsyncReply: function(requestId, args) { |
| 47 if (this.pendingCallbacks_[requestId] === undefined) { |
| 48 throw new Error('requestId ' + requestId + ' is not pending'); |
| 49 } |
| 50 var callback = this.pendingCallbacks_[requestId]; |
| 51 callback(args); |
| 52 delete this.pendingCallbacks_[requestId]; |
| 53 } |
| 54 }; |
| 55 |
| 56 return { |
| 57 BrowserBridge : BrowserBridge |
| 58 }; |
| 59 }); |
OLD | NEW |