| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 Provides an implementation of approved origins that relies | 6 * @fileoverview Provides an implementation of approved origins that relies |
| 7 * on the chrome.cryptotokenPrivate.requestPermission API. | 7 * on the chrome.cryptotokenPrivate.requestPermission API. |
| 8 * (and only) allows google.com to use security keys. | 8 * (and only) allows google.com to use security keys. |
| 9 * | 9 * |
| 10 */ | 10 */ |
| 11 'use strict'; | 11 'use strict'; |
| 12 | 12 |
| 13 /** | 13 /** |
| 14 * Allows the caller to check whether the user has approved the use of | 14 * Allows the caller to check whether the user has approved the use of |
| 15 * security keys from an origin. | 15 * security keys from an origin. |
| 16 * @constructor | 16 * @constructor |
| 17 * @implements {ApprovedOrigins} | 17 * @implements {ApprovedOrigins} |
| 18 */ | 18 */ |
| 19 function CryptoTokenApprovedOrigin() {} | 19 function CryptoTokenApprovedOrigin() {} |
| 20 | 20 |
| 21 /** | 21 /** |
| 22 * Checks whether the origin is approved to use security keys. (If not, an | 22 * Checks whether the origin is approved to use security keys. (If not, an |
| 23 * approval prompt may be shown.) | 23 * approval prompt may be shown.) |
| 24 * @param {string} origin The origin to approve. | 24 * @param {string} origin The origin to approve. |
| 25 * @param {number=} opt_tabId A tab id to display approval prompt in. | 25 * @param {number=} opt_tabId A tab id to display approval prompt in. |
| 26 * For this implementation, the tabId is always necessary, even though | 26 * For this implementation, the tabId is always necessary, even though |
| 27 * the type allows undefined. | 27 * the type allows undefined. |
| 28 * @return {Promise<boolean>} A promise for the result of the check. | 28 * @return {Promise<boolean>} A promise for the result of the check. |
| 29 */ | 29 */ |
| 30 CryptoTokenApprovedOrigin.prototype.isApprovedOrigin = | 30 CryptoTokenApprovedOrigin.prototype.isApprovedOrigin = function( |
| 31 function(origin, opt_tabId) { | 31 origin, opt_tabId) { |
| 32 return new Promise(function(resolve, reject) { | 32 return new Promise(function(resolve, reject) { |
| 33 if (opt_tabId === undefined) { | 33 if (opt_tabId === undefined) { |
| 34 resolve(false); |
| 35 return; |
| 36 } |
| 37 var tabId = /** @type {number} */ (opt_tabId); |
| 38 tabInForeground(tabId).then(function(result) { |
| 39 if (!result) { |
| 34 resolve(false); | 40 resolve(false); |
| 35 return; | 41 return; |
| 36 } | 42 } |
| 37 var tabId = /** @type {number} */ (opt_tabId); | 43 if (!chrome.tabs || !chrome.tabs.get) { |
| 38 tabInForeground(tabId).then(function(result) { | 44 reject(); |
| 39 if (!result) { | 45 return; |
| 46 } |
| 47 chrome.tabs.get(tabId, function(tab) { |
| 48 if (chrome.runtime.lastError) { |
| 40 resolve(false); | 49 resolve(false); |
| 41 return; | 50 return; |
| 42 } | 51 } |
| 43 if (!chrome.tabs || !chrome.tabs.get) { | 52 var tabOrigin = getOriginFromUrl(tab.url); |
| 44 reject(); | 53 resolve(tabOrigin == origin); |
| 45 return; | |
| 46 } | |
| 47 chrome.tabs.get(tabId, function(tab) { | |
| 48 if (chrome.runtime.lastError) { | |
| 49 resolve(false); | |
| 50 return; | |
| 51 } | |
| 52 var tabOrigin = getOriginFromUrl(tab.url); | |
| 53 resolve(tabOrigin == origin); | |
| 54 }); | |
| 55 }); | 54 }); |
| 55 }); |
| 56 }); | 56 }); |
| 57 }; | 57 }; |
| OLD | NEW |