Chromium Code Reviews| 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 The event page for Google Now for Chrome implementation. | 8 * @fileoverview The event page for Google Now for Chrome implementation. |
| 9 * The Google Now event page gets Google Now cards from the server and shows | 9 * The Google Now event page gets Google Now cards from the server and shows |
| 10 * them as Chrome notifications. | 10 * them as Chrome notifications. |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 121 | 121 |
| 122 // Add error processing to API calls. | 122 // Add error processing to API calls. |
| 123 tasks.instrumentApiFunction(chrome.location.onLocationUpdate, 'addListener', 0); | 123 tasks.instrumentApiFunction(chrome.location.onLocationUpdate, 'addListener', 0); |
| 124 tasks.instrumentApiFunction(chrome.notifications, 'create', 2); | 124 tasks.instrumentApiFunction(chrome.notifications, 'create', 2); |
| 125 tasks.instrumentApiFunction(chrome.notifications, 'update', 2); | 125 tasks.instrumentApiFunction(chrome.notifications, 'update', 2); |
| 126 tasks.instrumentApiFunction(chrome.notifications, 'getAll', 0); | 126 tasks.instrumentApiFunction(chrome.notifications, 'getAll', 0); |
| 127 tasks.instrumentApiFunction( | 127 tasks.instrumentApiFunction( |
| 128 chrome.notifications.onButtonClicked, 'addListener', 0); | 128 chrome.notifications.onButtonClicked, 'addListener', 0); |
| 129 tasks.instrumentApiFunction(chrome.notifications.onClicked, 'addListener', 0); | 129 tasks.instrumentApiFunction(chrome.notifications.onClicked, 'addListener', 0); |
| 130 tasks.instrumentApiFunction(chrome.notifications.onClosed, 'addListener', 0); | 130 tasks.instrumentApiFunction(chrome.notifications.onClosed, 'addListener', 0); |
| 131 tasks.instrumentApiFunction(chrome.permissions, 'contains', 1); | |
| 132 tasks.instrumentApiFunction(chrome.permissions, 'remove', 1); | |
| 133 tasks.instrumentApiFunction(chrome.permissions, 'request', 1); | |
| 134 tasks.instrumentApiFunction(chrome.runtime.onInstalled, 'addListener', 0); | |
| 135 tasks.instrumentApiFunction(chrome.runtime.onStartup, 'addListener', 0); | |
| 136 tasks.instrumentApiFunction(chrome.tabs, 'create', 1); | |
|
vadimt
2013/08/03 02:07:53
Please sync, build and run Chrome. Run you functio
| |
| 131 tasks.instrumentApiFunction( | 137 tasks.instrumentApiFunction( |
| 132 googleGeolocationAccessEnabledPref.onChange, | 138 googleGeolocationAccessEnabledPref.onChange, |
| 133 'addListener', | 139 'addListener', |
| 134 0); | 140 0); |
| 135 tasks.instrumentApiFunction(chrome.runtime.onInstalled, 'addListener', 0); | |
| 136 tasks.instrumentApiFunction(chrome.runtime.onStartup, 'addListener', 0); | |
| 137 tasks.instrumentApiFunction(chrome.tabs, 'create', 1); | |
| 138 tasks.instrumentApiFunction(storage, 'get', 1); | 141 tasks.instrumentApiFunction(storage, 'get', 1); |
| 139 | 142 |
| 140 var updateCardsAttempts = buildAttemptManager( | 143 var updateCardsAttempts = buildAttemptManager( |
| 141 'cards-update', | 144 'cards-update', |
| 142 requestLocation, | 145 requestLocation, |
| 143 INITIAL_POLLING_PERIOD_SECONDS, | 146 INITIAL_POLLING_PERIOD_SECONDS, |
| 144 MAXIMUM_POLLING_PERIOD_SECONDS); | 147 MAXIMUM_POLLING_PERIOD_SECONDS); |
| 145 var dismissalAttempts = buildAttemptManager( | 148 var dismissalAttempts = buildAttemptManager( |
| 146 'dismiss', | 149 'dismiss', |
| 147 retryPendingDismissals, | 150 retryPendingDismissals, |
| (...skipping 554 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 702 function setShouldPollCards(shouldPollCardsRequest, onSuccess) { | 705 function setShouldPollCards(shouldPollCardsRequest, onSuccess) { |
| 703 tasks.debugSetStepName( | 706 tasks.debugSetStepName( |
| 704 'setShouldRun-shouldRun-updateCardsAttemptsIsRunning'); | 707 'setShouldRun-shouldRun-updateCardsAttemptsIsRunning'); |
| 705 updateCardsAttempts.isRunning(function(currentValue) { | 708 updateCardsAttempts.isRunning(function(currentValue) { |
| 706 if (shouldPollCardsRequest != currentValue) { | 709 if (shouldPollCardsRequest != currentValue) { |
| 707 console.log('Action Taken setShouldPollCards=' + shouldPollCardsRequest); | 710 console.log('Action Taken setShouldPollCards=' + shouldPollCardsRequest); |
| 708 if (shouldPollCardsRequest) | 711 if (shouldPollCardsRequest) |
| 709 startPollingCards(); | 712 startPollingCards(); |
| 710 else | 713 else |
| 711 stopPollingCards(); | 714 stopPollingCards(); |
| 715 } else { | |
| 716 console.log( | |
| 717 'Action Ignored setShouldPollCards=' + shouldPollCardsRequest); | |
| 712 } | 718 } |
| 713 onSuccess(); | 719 onSuccess(); |
| 714 }); | 720 }); |
| 715 } | 721 } |
| 716 | 722 |
| 717 /** | 723 /** |
| 718 * Shows or hides the toast. | 724 * Shows or hides the toast. |
| 719 * @param {boolean} visibleRequest true to show the toast and | 725 * @param {boolean} visibleRequest true to show the toast and |
| 720 * false to hide the toast. | 726 * false to hide the toast. |
| 721 * @param {function} onSuccess Called on completion. | 727 * @param {function} onSuccess Called on completion. |
| 722 */ | 728 */ |
| 723 function setToastVisible(visibleRequest, onSuccess) { | 729 function setToastVisible(visibleRequest, onSuccess) { |
| 724 tasks.debugSetStepName( | 730 tasks.debugSetStepName( |
| 725 'setToastVisible-shouldSetToastVisible-getAllNotifications'); | 731 'setToastVisible-shouldSetToastVisible-getAllNotifications'); |
| 726 chrome.notifications.getAll(function(notifications) { | 732 chrome.notifications.getAll(function(notifications) { |
| 727 // TODO(vadimt): Figure out what to do when notifications are disabled for | 733 // TODO(vadimt): Figure out what to do when notifications are disabled for |
| 728 // our extension. | 734 // our extension. |
| 729 notifications = notifications || {}; | 735 notifications = notifications || {}; |
| 730 | 736 |
| 731 if (visibleRequest != !!notifications[WELCOME_TOAST_NOTIFICATION_ID]) { | 737 if (visibleRequest != !!notifications[WELCOME_TOAST_NOTIFICATION_ID]) { |
| 732 console.log('Action Taken setToastVisible=' + visibleRequest); | 738 console.log('Action Taken setToastVisible=' + visibleRequest); |
| 733 if (visibleRequest) | 739 if (visibleRequest) |
| 734 showWelcomeToast(); | 740 showWelcomeToast(); |
| 735 else | 741 else |
| 736 hideWelcomeToast(); | 742 hideWelcomeToast(); |
| 743 } else { | |
| 744 console.log('Action Ignored setToastVisible=' + visibleRequest); | |
| 737 } | 745 } |
| 738 | 746 |
| 739 onSuccess(); | 747 onSuccess(); |
| 740 }); | 748 }); |
| 741 } | 749 } |
| 742 | 750 |
| 743 /** | 751 /** |
| 752 * Enables or disables the Google Now background permission. | |
| 753 * @param {boolean} backgroundEnable true to run in the background. | |
| 754 * false to not run in the background. | |
| 755 * @param {function} onSuccess Called on completion. | |
| 756 */ | |
| 757 function setBackgroundEnable(backgroundEnable, onSuccess) { | |
| 758 chrome.permissions.contains({permissions: ['background']}, | |
| 759 function(hasPermission) { | |
| 760 if (backgroundEnable != hasPermission) { | |
| 761 console.log('Action Taken setBackgroundEnable=' + backgroundEnable); | |
| 762 if (backgroundEnable) | |
| 763 chrome.permissions.request( | |
| 764 {permissions: ['background']}, | |
| 765 function() { | |
| 766 onSuccess(); | |
| 767 }); | |
| 768 else | |
| 769 chrome.permissions.remove( | |
| 770 {permissions: ['background']}, | |
| 771 function() { | |
| 772 onSuccess(); | |
| 773 }); | |
| 774 } else { | |
| 775 console.log('Action Ignored setBackgroundEnable=' + backgroundEnable); | |
| 776 onSuccess(); | |
| 777 } | |
| 778 }); | |
| 779 } | |
| 780 | |
| 781 /** | |
| 744 * Does the actual work of deciding what Google Now should do | 782 * Does the actual work of deciding what Google Now should do |
| 745 * based off of the current state of Chrome. | 783 * based off of the current state of Chrome. |
| 746 * @param {boolean} signedIn true if the user is signed in. | 784 * @param {boolean} signedIn true if the user is signed in. |
| 747 * @param {boolean} geolocationEnabled true if | 785 * @param {boolean} geolocationEnabled true if |
| 748 * the geolocation option is enabled. | 786 * the geolocation option is enabled. |
| 749 * @param {boolean} userRespondedToToast true if | 787 * @param {boolean} userRespondedToToast true if |
| 750 * the user has responded to the toast. | 788 * the user has responded to the toast. |
| 789 * @param {boolean} enableExperiment true if | |
| 790 * this extension should be running. | |
| 751 * @param {function()} callback Call this function on completion. | 791 * @param {function()} callback Call this function on completion. |
| 752 */ | 792 */ |
| 753 function updateRunningState( | 793 function updateRunningState( |
| 754 signedIn, | 794 signedIn, |
| 755 geolocationEnabled, | 795 geolocationEnabled, |
| 756 userRespondedToToast, | 796 userRespondedToToast, |
| 797 enableExperiment, | |
| 757 callback) { | 798 callback) { |
| 758 | 799 |
| 759 console.log( | 800 console.log( |
| 760 'State Update signedIn=' + signedIn + ' ' + | 801 'State Update signedIn=' + signedIn + ' ' + |
| 761 'geolocationEnabled=' + geolocationEnabled + ' ' + | 802 'geolocationEnabled=' + geolocationEnabled + ' ' + |
| 762 'userRespondedToToast=' + userRespondedToToast); | 803 'userRespondedToToast=' + userRespondedToToast + ' ' + |
| 804 'enableExperiment=' + enableExperiment); | |
| 763 | 805 |
| 764 var shouldSetToastVisible = false; | 806 var shouldSetToastVisible = false; |
| 765 var shouldPollCards = false; | 807 var shouldPollCards = false; |
| 808 var shouldSetBackground = false; | |
| 766 | 809 |
| 767 if (signedIn) { | 810 if (signedIn && enableExperiment) { |
| 768 if (geolocationEnabled) { | 811 if (geolocationEnabled) { |
| 769 if (!userRespondedToToast) { | 812 if (!userRespondedToToast) { |
| 770 // If the user enabled geolocation independently of Google Now, | 813 // If the user enabled geolocation independently of Google Now, |
| 771 // the user has implicitly responded to the toast. | 814 // the user has implicitly responded to the toast. |
| 772 // We do not want to show it again. | 815 // We do not want to show it again. |
| 773 storage.set({userRespondedToToast: true}); | 816 storage.set({userRespondedToToast: true}); |
| 774 } | 817 } |
| 775 | 818 |
| 776 shouldPollCards = true; | 819 shouldPollCards = true; |
| 820 shouldSetBackground = true; | |
| 777 } else { | 821 } else { |
| 778 if (userRespondedToToast) { | 822 if (userRespondedToToast) { |
| 779 recordEvent(GoogleNowEvent.USER_SUPPRESSED); | 823 recordEvent(GoogleNowEvent.USER_SUPPRESSED); |
| 780 } else { | 824 } else { |
| 781 shouldSetToastVisible = true; | 825 shouldSetToastVisible = true; |
| 782 } | 826 } |
| 783 } | 827 } |
| 784 } else { | 828 } else { |
| 785 recordEvent(GoogleNowEvent.STOPPED); | 829 recordEvent(GoogleNowEvent.STOPPED); |
| 786 } | 830 } |
| 787 | 831 |
| 788 console.log( | 832 console.log( |
| 789 'Requested Actions setToastVisible=' + shouldSetToastVisible + ' ' + | 833 'Requested Actions shouldSetBackground=' + shouldSetBackground + ' ' + |
| 834 'setToastVisible=' + shouldSetToastVisible + ' ' + | |
| 790 'setShouldPollCards=' + shouldPollCards); | 835 'setShouldPollCards=' + shouldPollCards); |
| 791 | 836 |
| 792 setToastVisible(shouldSetToastVisible, function() { | 837 setBackgroundEnable(shouldSetBackground, function() { |
| 793 setShouldPollCards(shouldPollCards, callback); | 838 setToastVisible(shouldSetToastVisible, function() { |
| 839 setShouldPollCards(shouldPollCards, callback); | |
| 840 }); | |
| 794 }); | 841 }); |
| 795 } | 842 } |
| 796 | 843 |
| 797 /** | 844 /** |
| 798 * Coordinates the behavior of Google Now for Chrome depending on | 845 * Coordinates the behavior of Google Now for Chrome depending on |
| 799 * Chrome and extension state. | 846 * Chrome and extension state. |
| 800 */ | 847 */ |
| 801 function onStateChange() { | 848 function onStateChange() { |
| 802 tasks.add(STATE_CHANGED_TASK_NAME, function(callback) { | 849 tasks.add(STATE_CHANGED_TASK_NAME, function(callback) { |
| 803 tasks.debugSetStepName('onStateChange-isSignedIn'); | 850 tasks.debugSetStepName('onStateChange-isSignedIn'); |
| 804 authenticationManager.isSignedIn(function(token) { | 851 authenticationManager.isSignedIn(function(token) { |
| 805 var signedIn = !!token && !!NOTIFICATION_CARDS_URL; | 852 var signedIn = !!token && !!NOTIFICATION_CARDS_URL; |
| 806 tasks.debugSetStepName( | 853 chrome.metricsPrivate.getFieldTrial('GoogleNow', function(response) { |
| 807 'onStateChange-get-googleGeolocationAccessEnabledPref'); | 854 // '' means we were enabled via the flags, but |
| 808 googleGeolocationAccessEnabledPref.get({}, function(prefValue) { | 855 // the experiment is not running. |
| 809 var geolocationEnabled = !!prefValue.value; | 856 var enableExperiment = (response == 'Enable' || |
|
vadimt
2013/08/03 02:07:53
You need to check for "EnableWithBackground", "Ena
| |
| 857 (response == 'EnabledViaFlag') || | |
| 858 (response == '')); | |
| 810 tasks.debugSetStepName( | 859 tasks.debugSetStepName( |
| 811 'onStateChange-get-userRespondedToToast'); | 860 'onStateChange-get-googleGeolocationAccessEnabledPref'); |
| 812 storage.get('userRespondedToToast', function(items) { | 861 googleGeolocationAccessEnabledPref.get({}, function(prefValue) { |
| 813 var userRespondedToToast = !!items.userRespondedToToast; | 862 var geolocationEnabled = !!prefValue.value; |
| 814 updateRunningState( | 863 tasks.debugSetStepName( |
| 815 signedIn, | 864 'onStateChange-get-userRespondedToToast'); |
| 816 geolocationEnabled, | 865 storage.get('userRespondedToToast', function(items) { |
| 817 userRespondedToToast, | 866 var userRespondedToToast = !!items.userRespondedToToast; |
| 818 callback); | 867 updateRunningState( |
| 868 signedIn, | |
| 869 geolocationEnabled, | |
| 870 userRespondedToToast, | |
| 871 enableExperiment, | |
| 872 callback); | |
| 873 }); | |
| 819 }); | 874 }); |
| 820 }); | 875 }); |
| 821 }); | 876 }); |
| 822 }); | 877 }); |
| 823 } | 878 } |
| 824 | 879 |
| 825 /** | 880 /** |
| 826 * Displays a toast to the user asking if they want to opt in to receiving | 881 * Displays a toast to the user asking if they want to opt in to receiving |
| 827 * Google Now cards. | 882 * Google Now cards. |
| 828 */ | 883 */ |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 903 | 958 |
| 904 chrome.location.onLocationUpdate.addListener(function(position) { | 959 chrome.location.onLocationUpdate.addListener(function(position) { |
| 905 recordEvent(GoogleNowEvent.LOCATION_UPDATE); | 960 recordEvent(GoogleNowEvent.LOCATION_UPDATE); |
| 906 updateNotificationsCards(position); | 961 updateNotificationsCards(position); |
| 907 }); | 962 }); |
| 908 | 963 |
| 909 chrome.omnibox.onInputEntered.addListener(function(text) { | 964 chrome.omnibox.onInputEntered.addListener(function(text) { |
| 910 localStorage['server_url'] = NOTIFICATION_CARDS_URL = text; | 965 localStorage['server_url'] = NOTIFICATION_CARDS_URL = text; |
| 911 initialize(); | 966 initialize(); |
| 912 }); | 967 }); |
| OLD | NEW |