| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 'use strict'; | 5 'use strict'; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * @fileoverview Utility objects and functions for Google Now extension. | 8 * @fileoverview Utility objects and functions for Google Now extension. |
| 9 * Most important entities here: | 9 * Most important entities here: |
| 10 * (1) 'wrapper' is a module used to add error handling and other services to | 10 * (1) 'wrapper' is a module used to add error handling and other services to |
| (...skipping 350 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 361 delete pendingCallbacks[idToRemove]; | 361 delete pendingCallbacks[idToRemove]; |
| 362 }); | 362 }); |
| 363 } | 363 } |
| 364 } | 364 } |
| 365 } | 365 } |
| 366 | 366 |
| 367 if (wrapperPluginInstance) | 367 if (wrapperPluginInstance) |
| 368 wrapperPluginInstance.prologue(); | 368 wrapperPluginInstance.prologue(); |
| 369 | 369 |
| 370 // Call the original callback. | 370 // Call the original callback. |
| 371 callback.apply(null, arguments); | 371 var returnValue = callback.apply(null, arguments); |
| 372 | 372 |
| 373 if (wrapperPluginInstance) | 373 if (wrapperPluginInstance) |
| 374 wrapperPluginInstance.epilogue(); | 374 wrapperPluginInstance.epilogue(); |
| 375 | 375 |
| 376 verify(isInWrappedCallback, | 376 verify(isInWrappedCallback, |
| 377 'Instrumented callback is not instrumented upon exit'); | 377 'Instrumented callback is not instrumented upon exit'); |
| 378 isInWrappedCallback = false; | 378 isInWrappedCallback = false; |
| 379 |
| 380 return returnValue; |
| 379 } catch (error) { | 381 } catch (error) { |
| 380 reportError(error); | 382 reportError(error); |
| 381 } | 383 } |
| 382 }; | 384 }; |
| 383 } | 385 } |
| 384 | 386 |
| 385 /** | 387 /** |
| 386 * Returns an instrumented function. | 388 * Returns an instrumented function. |
| 387 * @param {!Array.<string>} functionIdentifierParts Path to the chrome.* | 389 * @param {!Array.<string>} functionIdentifierParts Path to the chrome.* |
| 388 * function. | 390 * function. |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 472 checkInWrappedCallback: checkInWrappedCallback, | 474 checkInWrappedCallback: checkInWrappedCallback, |
| 473 debugGetStateString: debugGetStateString | 475 debugGetStateString: debugGetStateString |
| 474 }; | 476 }; |
| 475 })(); | 477 })(); |
| 476 | 478 |
| 477 wrapper.instrumentChromeApiFunction('alarms.get', 1); | 479 wrapper.instrumentChromeApiFunction('alarms.get', 1); |
| 478 wrapper.instrumentChromeApiFunction('alarms.onAlarm.addListener', 0); | 480 wrapper.instrumentChromeApiFunction('alarms.onAlarm.addListener', 0); |
| 479 wrapper.instrumentChromeApiFunction('identity.getAuthToken', 1); | 481 wrapper.instrumentChromeApiFunction('identity.getAuthToken', 1); |
| 480 wrapper.instrumentChromeApiFunction('identity.onSignInChanged.addListener', 0); | 482 wrapper.instrumentChromeApiFunction('identity.onSignInChanged.addListener', 0); |
| 481 wrapper.instrumentChromeApiFunction('identity.removeCachedAuthToken', 1); | 483 wrapper.instrumentChromeApiFunction('identity.removeCachedAuthToken', 1); |
| 484 wrapper.instrumentChromeApiFunction('storage.local.get', 1); |
| 482 wrapper.instrumentChromeApiFunction('webstorePrivate.getBrowserLogin', 0); | 485 wrapper.instrumentChromeApiFunction('webstorePrivate.getBrowserLogin', 0); |
| 483 | 486 |
| 484 /** | 487 /** |
| 485 * Unique ID of the next promise. | 488 * Unique ID of the next promise. |
| 486 * @type {number} | 489 * @type {number} |
| 487 */ | 490 */ |
| 488 var nextPromiseId = 0; | 491 var nextPromiseId = 0; |
| 489 | 492 |
| 490 /** | 493 /** |
| 491 * Add task tracking support to Promise.then. | 494 * Add task tracking support to Promise.then. |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 527 this.__promiseId = promiseId; | 530 this.__promiseId = promiseId; |
| 528 var promise = originalCatch.call( | 531 var promise = originalCatch.call( |
| 529 this, wrapper.wrapCallback(callback, false, | 532 this, wrapper.wrapCallback(callback, false, |
| 530 {id: this.__promiseId, isCatch: true})); | 533 {id: this.__promiseId, isCatch: true})); |
| 531 promise.__promiseId = promiseId; | 534 promise.__promiseId = promiseId; |
| 532 return promise; | 535 return promise; |
| 533 } | 536 } |
| 534 }(); | 537 }(); |
| 535 | 538 |
| 536 /** | 539 /** |
| 540 * Control promise rejection. |
| 541 * @enum {boolean} |
| 542 */ |
| 543 var PromiseRejection = { |
| 544 /** Disallow promise rejection */ |
| 545 DISALLOW: false, |
| 546 /** Allow promise rejection */ |
| 547 ALLOW: true |
| 548 }; |
| 549 |
| 550 /** |
| 551 * Provides the promise equivalent of instrumented.storage.local.get. |
| 552 * @param {Object} defaultStorageObject Default storage object to fill. |
| 553 * @param {boolean} opt_allowPromiseRejection If PromiseRejection.ALLOW, |
| 554 * allow promise rejection on errors, otherwise the default storage |
| 555 * object is resolved. |
| 556 * @return {Promise} A promise that fills the default storage object. On |
| 557 * failure, if promise rejection is allowed, the promise is rejected, |
| 558 * otherwise it is resolved to the default storage object. |
| 559 */ |
| 560 function fillFromChromeLocalStorage( |
| 561 defaultStorageObject, |
| 562 opt_allowPromiseRejection) { |
| 563 return new Promise(function(resolve, reject) { |
| 564 instrumented.storage.local.get(defaultStorageObject, function(items) { |
| 565 if (items) { |
| 566 resolve(items); |
| 567 } else if (opt_allowPromiseRejection === PromiseRejection.ALLOW) { |
| 568 reject(); |
| 569 } else { |
| 570 resolve(defaultStorageObject); |
| 571 } |
| 572 }); |
| 573 }); |
| 574 } |
| 575 |
| 576 /** |
| 537 * Promise Pending Callback Data. Counts outstanding "then" and "catch" | 577 * Promise Pending Callback Data. Counts outstanding "then" and "catch" |
| 538 * callbacks; | 578 * callbacks; |
| 539 * | 579 * |
| 540 * @typedef {{ | 580 * @typedef {{ |
| 541 * thenCount: number, | 581 * thenCount: number, |
| 542 * catchCount: number | 582 * catchCount: number |
| 543 * }} | 583 * }} |
| 544 */ | 584 */ |
| 545 var PromisePendingCallbackData; | 585 var PromisePendingCallbackData; |
| 546 | 586 |
| (...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 817 chrome.alarms.clear(alarmName); | 857 chrome.alarms.clear(alarmName); |
| 818 chrome.storage.local.remove(currentDelayStorageKey); | 858 chrome.storage.local.remove(currentDelayStorageKey); |
| 819 } | 859 } |
| 820 | 860 |
| 821 /** | 861 /** |
| 822 * Plans for the next attempt. | 862 * Plans for the next attempt. |
| 823 * @param {function()} callback Completion callback. It will be invoked after | 863 * @param {function()} callback Completion callback. It will be invoked after |
| 824 * the planning is done. | 864 * the planning is done. |
| 825 */ | 865 */ |
| 826 function planForNext(callback) { | 866 function planForNext(callback) { |
| 827 instrumented.storage.local.get(currentDelayStorageKey, function(items) { | 867 var request = {}; |
| 828 if (!items) { | 868 request[currentDelayStorageKey] = maximumDelaySeconds; |
| 829 items = {}; | 869 fillFromChromeLocalStorage(request).then(function(items) { |
| 830 items[currentDelayStorageKey] = maximumDelaySeconds; | |
| 831 } | |
| 832 console.log('planForNext-get-storage ' + JSON.stringify(items)); | 870 console.log('planForNext-get-storage ' + JSON.stringify(items)); |
| 833 scheduleNextAttempt(items[currentDelayStorageKey]); | 871 scheduleNextAttempt(items[currentDelayStorageKey]); |
| 834 callback(); | 872 callback(); |
| 835 }); | 873 }); |
| 836 } | 874 } |
| 837 | 875 |
| 838 instrumented.alarms.onAlarm.addListener(function(alarm) { | 876 instrumented.alarms.onAlarm.addListener(function(alarm) { |
| 839 if (alarm.name == alarmName) | 877 if (alarm.name == alarmName) |
| 840 isRunning(function(running) { | 878 isRunning(function(running) { |
| 841 if (running) | 879 if (running) |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 915 function addListener(callback) { | 953 function addListener(callback) { |
| 916 listeners.push(callback); | 954 listeners.push(callback); |
| 917 } | 955 } |
| 918 | 956 |
| 919 /** | 957 /** |
| 920 * Checks if the last signed in state matches the current one. | 958 * Checks if the last signed in state matches the current one. |
| 921 * If it doesn't, it notifies the listeners of the change. | 959 * If it doesn't, it notifies the listeners of the change. |
| 922 */ | 960 */ |
| 923 function checkAndNotifyListeners() { | 961 function checkAndNotifyListeners() { |
| 924 isSignedIn().then(function(signedIn) { | 962 isSignedIn().then(function(signedIn) { |
| 925 instrumented.storage.local.get('lastSignedInState', function(items) { | 963 fillFromChromeLocalStorage({lastSignedInState: undefined}) |
| 926 items = items || {}; | 964 .then(function(items) { |
| 927 if (items.lastSignedInState != signedIn) { | 965 if (items.lastSignedInState != signedIn) { |
| 928 chrome.storage.local.set( | 966 chrome.storage.local.set( |
| 929 {lastSignedInState: signedIn}); | 967 {lastSignedInState: signedIn}); |
| 930 listeners.forEach(function(callback) { | 968 listeners.forEach(function(callback) { |
| 931 callback(); | 969 callback(); |
| 932 }); | 970 }); |
| 933 } | 971 } |
| 972 }); |
| 934 }); | 973 }); |
| 935 }); | |
| 936 } | 974 } |
| 937 | 975 |
| 938 instrumented.identity.onSignInChanged.addListener(function() { | 976 instrumented.identity.onSignInChanged.addListener(function() { |
| 939 checkAndNotifyListeners(); | 977 checkAndNotifyListeners(); |
| 940 }); | 978 }); |
| 941 | 979 |
| 942 instrumented.alarms.onAlarm.addListener(function(alarm) { | 980 instrumented.alarms.onAlarm.addListener(function(alarm) { |
| 943 if (alarm.name == alarmName) | 981 if (alarm.name == alarmName) |
| 944 checkAndNotifyListeners(); | 982 checkAndNotifyListeners(); |
| 945 }); | 983 }); |
| 946 | 984 |
| 947 // Poll for the sign in state every hour. | 985 // Poll for the sign in state every hour. |
| 948 // One hour is just an arbitrary amount of time chosen. | 986 // One hour is just an arbitrary amount of time chosen. |
| 949 chrome.alarms.create(alarmName, {periodInMinutes: 60}); | 987 chrome.alarms.create(alarmName, {periodInMinutes: 60}); |
| 950 | 988 |
| 951 return { | 989 return { |
| 952 addListener: addListener, | 990 addListener: addListener, |
| 953 getAuthToken: getAuthToken, | 991 getAuthToken: getAuthToken, |
| 954 isSignedIn: isSignedIn, | 992 isSignedIn: isSignedIn, |
| 955 removeToken: removeToken | 993 removeToken: removeToken |
| 956 }; | 994 }; |
| 957 } | 995 } |
| OLD | NEW |