OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 /** | 5 /** |
6 * @fileoverview A collection of utility methods for UberPage and its contained | 6 * @fileoverview A collection of utility methods for UberPage and its contained |
7 * pages. | 7 * pages. |
8 */ | 8 */ |
9 | 9 |
10 cr.define('uber', function() { | 10 cr.define('uber', function() { |
11 /** | 11 /** |
12 * Invokes a method on the parent window (UberPage). This is a convenience | 12 * Invokes a method on the parent window (UberPage). This is a convenience |
13 * method for API calls into the uber page. | 13 * method for API calls into the uber page. |
14 * @param {String} method The name of the method to invoke. | 14 * @param {String} method The name of the method to invoke. |
15 * @param {Object} params Optional property bag of parameters to pass to the | 15 * @param {Object=} opt_params Optional property bag of parameters to pass to |
16 * invoked method. | 16 * the invoked method. |
17 * @private | 17 * @private |
18 */ | 18 */ |
19 function invokeMethodOnParent(method, params) { | 19 function invokeMethodOnParent(method, opt_params) { |
20 if (!window.parent) | 20 if (window.location == window.parent.location) |
21 return; | 21 return; |
22 | 22 |
23 var data = {method: method, params: params}; | 23 invokeMethodOnWindow(window.parent, method, opt_params, 'chrome://chrome'); |
24 window.parent.postMessage(data, 'chrome://chrome'); | 24 } |
25 }; | 25 |
| 26 /** |
| 27 * Invokes a method on the target window. |
| 28 * @param {String} method The name of the method to invoke. |
| 29 * @param {Object=} opt_params Optional property bag of parameters to pass to |
| 30 * the invoked method. |
| 31 * @param {String=} opt_url The origin of the target window. |
| 32 * @private |
| 33 */ |
| 34 function invokeMethodOnWindow(targetWindow, method, opt_params, opt_url) { |
| 35 var data = {method: method, params: opt_params}; |
| 36 targetWindow.postMessage(data, opt_url ? opt_url : '*'); |
| 37 } |
26 | 38 |
27 return { | 39 return { |
28 invokeMethodOnParent: invokeMethodOnParent | 40 invokeMethodOnParent: invokeMethodOnParent, |
29 } | 41 invokeMethodOnWindow: invokeMethodOnWindow, |
| 42 }; |
30 }); | 43 }); |
OLD | NEW |