OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 cr.define('gpu', function() { | 5 cr.define('gpu', function() { |
6 /** | 6 /** |
7 * This class provides a 'bridge' for communicating between javascript and | 7 * This class provides a 'bridge' for communicating between javascript and |
8 * the browser. | 8 * the browser. |
9 * @constructor | 9 * @constructor |
10 */ | 10 */ |
11 function BrowserBridge() { | 11 function BrowserBridge() { |
12 // If we are not running inside WebUI, output chrome.send messages | 12 // If we are not running inside WebUI, output chrome.send messages |
13 // to the console to help with quick-iteration debugging. | 13 // to the console to help with quick-iteration debugging. |
14 if (chrome.send === undefined && console.log) { | 14 if (chrome.send === undefined && console.log) { |
| 15 this.debugMode_ = true; |
15 chrome.send = function(messageHandler, args) { | 16 chrome.send = function(messageHandler, args) { |
16 console.log('chrome.send', messageHandler, args); | 17 console.log('chrome.send', messageHandler, args); |
17 }; | 18 }; |
| 19 } else { |
| 20 this.debugMode_ = false; |
18 } | 21 } |
19 | 22 |
20 this.nextRequestId_ = 0; | 23 this.nextRequestId_ = 0; |
21 this.pendingCallbacks_ = []; | 24 this.pendingCallbacks_ = []; |
22 } | 25 } |
23 | 26 |
24 BrowserBridge.prototype = { | 27 BrowserBridge.prototype = { |
25 __proto__: Object.prototype, | 28 __proto__: Object.prototype, |
26 | 29 |
27 /** | 30 /** |
| 31 * Returns true if the page is hosted inside Chrome WebUI |
| 32 * Helps have behavior conditional to emulate_webui.py |
| 33 */ |
| 34 get debugMode() { |
| 35 return this.debugMode_; |
| 36 }, |
| 37 |
| 38 /** |
28 * Sends a message to the browser with specified args. The | 39 * Sends a message to the browser with specified args. The |
29 * browser will reply asynchronously via the provided callback. | 40 * browser will reply asynchronously via the provided callback. |
30 */ | 41 */ |
31 callAsync: function(submessage, args, callback) { | 42 callAsync: function(submessage, args, callback) { |
32 var requestId = this.nextRequestId_; | 43 var requestId = this.nextRequestId_; |
33 this.nextRequestId_ += 1; | 44 this.nextRequestId_ += 1; |
34 this.pendingCallbacks_[requestId] = callback; | 45 this.pendingCallbacks_[requestId] = callback; |
35 if (!args) { | 46 if (!args) { |
36 chrome.send('callAsync', [requestId.toString(), submessage]); | 47 chrome.send('callAsync', [requestId.toString(), submessage]); |
37 } else { | 48 } else { |
(...skipping 12 matching lines...) Expand all Loading... |
50 var callback = this.pendingCallbacks_[requestId]; | 61 var callback = this.pendingCallbacks_[requestId]; |
51 callback(args); | 62 callback(args); |
52 delete this.pendingCallbacks_[requestId]; | 63 delete this.pendingCallbacks_[requestId]; |
53 } | 64 } |
54 }; | 65 }; |
55 | 66 |
56 return { | 67 return { |
57 BrowserBridge : BrowserBridge | 68 BrowserBridge : BrowserBridge |
58 }; | 69 }; |
59 }); | 70 }); |
OLD | NEW |