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 if (args) { |
| 17 console.log('chrome.send', messageHandler, args); |
| 18 } else { |
| 19 console.log('chrome.send', messageHandler); |
| 20 } |
| 21 }; |
| 22 } |
| 23 |
| 24 this.nextRequestId_ = 0; |
| 25 this.pendingCallbacks_ = []; |
| 26 } |
| 27 |
| 28 BrowserBridge.prototype = { |
| 29 __proto__: Object.prototype, |
| 30 |
| 31 /** |
| 32 * Sends a message to the browser with specified args. The |
| 33 * browser will reply asynchronously via the provided callback. |
| 34 */ |
| 35 callAsync: function(submessage, args, callback) { |
| 36 var requestId = this.nextRequestId_; |
| 37 this.nextRequestId_ += 1; |
| 38 this.pendingCallbacks_[requestId] = callback; |
| 39 if (!args) { |
| 40 chrome.send('callAsync', [requestId.toString(), submessage]); |
| 41 } else { |
| 42 var allArgs = [requestId.toString(), submessage].concat(args); |
| 43 chrome.send('callAsync', allArgs); |
| 44 } |
| 45 }, |
| 46 |
| 47 /** |
| 48 * Called by gpu c++ code when client info is ready. |
| 49 */ |
| 50 onCallAsyncReply: function(requestId, args) { |
| 51 if (this.pendingCallbacks_[requestId] === undefined) { |
| 52 throw new Error('requestId ' + requestId + ' is not pending'); |
| 53 } |
| 54 var callback = this.pendingCallbacks_[requestId]; |
| 55 callback(args); |
| 56 delete this.pendingCallbacks_[requestId]; |
| 57 } |
| 58 }; |
| 59 |
| 60 return { |
| 61 BrowserBridge : BrowserBridge |
| 62 }; |
| 63 }); |
OLD | NEW |