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 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
111 return true; | 111 return true; |
112 } | 112 } |
113 | 113 |
114 return false; | 114 return false; |
115 } | 115 } |
116 | 116 |
117 var tasks = buildTaskManager(areTasksConflicting); | 117 var tasks = buildTaskManager(areTasksConflicting); |
118 | 118 |
119 // Add error processing to API calls. | 119 // Add error processing to API calls. |
120 tasks.instrumentChromeApiFunction('location.onLocationUpdate.addListener', 0); | 120 tasks.instrumentChromeApiFunction('location.onLocationUpdate.addListener', 0); |
121 tasks.instrumentChromeApiFunction('metricsPrivate.getFieldTrial', 1); | |
121 tasks.instrumentChromeApiFunction('notifications.create', 2); | 122 tasks.instrumentChromeApiFunction('notifications.create', 2); |
122 tasks.instrumentChromeApiFunction('notifications.update', 2); | 123 tasks.instrumentChromeApiFunction('notifications.update', 2); |
123 tasks.instrumentChromeApiFunction('notifications.getAll', 0); | 124 tasks.instrumentChromeApiFunction('notifications.getAll', 0); |
124 tasks.instrumentChromeApiFunction( | 125 tasks.instrumentChromeApiFunction( |
125 'notifications.onButtonClicked.addListener', 0); | 126 'notifications.onButtonClicked.addListener', 0); |
126 tasks.instrumentChromeApiFunction('notifications.onClicked.addListener', 0); | 127 tasks.instrumentChromeApiFunction('notifications.onClicked.addListener', 0); |
127 tasks.instrumentChromeApiFunction('notifications.onClosed.addListener', 0); | 128 tasks.instrumentChromeApiFunction('notifications.onClosed.addListener', 0); |
128 tasks.instrumentChromeApiFunction('omnibox.onInputEntered.addListener', 0); | 129 tasks.instrumentChromeApiFunction('omnibox.onInputEntered.addListener', 0); |
129 tasks.instrumentChromeApiFunction( | 130 tasks.instrumentChromeApiFunction( |
130 'preferencesPrivate.googleGeolocationAccessEnabled.get', | 131 'preferencesPrivate.googleGeolocationAccessEnabled.get', |
131 1); | 132 1); |
132 tasks.instrumentChromeApiFunction( | 133 tasks.instrumentChromeApiFunction( |
133 'preferencesPrivate.googleGeolocationAccessEnabled.onChange.addListener', | 134 'preferencesPrivate.googleGeolocationAccessEnabled.onChange.addListener', |
134 0); | 135 0); |
136 tasks.instrumentChromeApiFunction('permissions.contains', 1); | |
137 tasks.instrumentChromeApiFunction('permissions.remove', 1); | |
138 tasks.instrumentChromeApiFunction('permissions.request', 1); | |
135 tasks.instrumentChromeApiFunction('runtime.onInstalled.addListener', 0); | 139 tasks.instrumentChromeApiFunction('runtime.onInstalled.addListener', 0); |
136 tasks.instrumentChromeApiFunction('runtime.onStartup.addListener', 0); | 140 tasks.instrumentChromeApiFunction('runtime.onStartup.addListener', 0); |
137 tasks.instrumentChromeApiFunction('tabs.create', 1); | 141 tasks.instrumentChromeApiFunction('tabs.create', 1); |
138 tasks.instrumentChromeApiFunction('storage.local.get', 1); | 142 tasks.instrumentChromeApiFunction('storage.local.get', 1); |
139 | 143 |
140 var updateCardsAttempts = buildAttemptManager( | 144 var updateCardsAttempts = buildAttemptManager( |
141 'cards-update', | 145 'cards-update', |
142 requestLocation, | 146 requestLocation, |
143 INITIAL_POLLING_PERIOD_SECONDS, | 147 INITIAL_POLLING_PERIOD_SECONDS, |
144 MAXIMUM_POLLING_PERIOD_SECONDS); | 148 MAXIMUM_POLLING_PERIOD_SECONDS); |
(...skipping 601 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
746 hideWelcomeToast(); | 750 hideWelcomeToast(); |
747 } else { | 751 } else { |
748 console.log('Action Ignored setToastVisible=' + visibleRequest); | 752 console.log('Action Ignored setToastVisible=' + visibleRequest); |
749 } | 753 } |
750 | 754 |
751 callback(); | 755 callback(); |
752 }); | 756 }); |
753 } | 757 } |
754 | 758 |
755 /** | 759 /** |
760 * Enables or disables the Google Now background permission. | |
761 * @param {boolean} backgroundEnable true to run in the background. | |
762 * false to not run in the background. | |
763 * @param {function} callback Called on completion. | |
764 */ | |
765 function setBackgroundEnable(backgroundEnable, callback) { | |
766 instrumented.permissions.contains({permissions: ['background']}, | |
767 function(hasPermission) { | |
768 if (backgroundEnable != hasPermission) { | |
769 console.log('Action Taken setBackgroundEnable=' + backgroundEnable); | |
770 if (backgroundEnable) | |
771 instrumented.permissions.request( | |
772 {permissions: ['background']}, | |
773 function() { | |
774 callback(); | |
775 }); | |
776 else | |
777 instrumented.permissions.remove( | |
778 {permissions: ['background']}, | |
779 function() { | |
780 callback(); | |
781 }); | |
782 } else { | |
783 console.log('Action Ignored setBackgroundEnable=' + backgroundEnable); | |
784 callback(); | |
785 } | |
786 }); | |
787 } | |
788 | |
789 /** | |
756 * Does the actual work of deciding what Google Now should do | 790 * Does the actual work of deciding what Google Now should do |
757 * based off of the current state of Chrome. | 791 * based off of the current state of Chrome. |
758 * @param {boolean} signedIn true if the user is signed in. | 792 * @param {boolean} signedIn true if the user is signed in. |
759 * @param {boolean} geolocationEnabled true if | 793 * @param {boolean} geolocationEnabled true if |
760 * the geolocation option is enabled. | 794 * the geolocation option is enabled. |
761 * @param {boolean} userRespondedToToast true if | 795 * @param {boolean} userRespondedToToast true if |
762 * the user has responded to the toast. | 796 * the user has responded to the toast. |
797 * @param {boolean} enableExperiment true if | |
798 * this extension should be running. | |
799 * @param {boolean} enableBackground true if | |
800 * the background permission should be requested. | |
763 * @param {function()} callback Call this function on completion. | 801 * @param {function()} callback Call this function on completion. |
764 */ | 802 */ |
765 function updateRunningState( | 803 function updateRunningState( |
766 signedIn, | 804 signedIn, |
767 geolocationEnabled, | 805 geolocationEnabled, |
768 userRespondedToToast, | 806 userRespondedToToast, |
807 enableExperiment, | |
vadimt
2013/08/09 22:13:39
You are not using this param, so you can remove it
robliao
2013/08/09 22:16:47
This param is used to gate the starting of Google
vadimt
2013/08/09 22:21:27
Ah, correct.
Still, you don't need this.
The gatin
robliao
2013/08/09 22:24:40
If we're comfortable that once we load the extensi
vadimt
2013/08/09 22:28:26
Finch parameters don't change while Chrome is runn
robliao
2013/08/09 22:37:40
Done.
| |
808 enableBackground, | |
769 callback) { | 809 callback) { |
770 | 810 |
771 console.log( | 811 console.log( |
772 'State Update signedIn=' + signedIn + ' ' + | 812 'State Update signedIn=' + signedIn + ' ' + |
773 'geolocationEnabled=' + geolocationEnabled + ' ' + | 813 'geolocationEnabled=' + geolocationEnabled + ' ' + |
774 'userRespondedToToast=' + userRespondedToToast); | 814 'userRespondedToToast=' + userRespondedToToast + ' ' + |
815 'enableExperiment=' + enableExperiment); | |
775 | 816 |
776 var shouldSetToastVisible = false; | 817 var shouldSetToastVisible = false; |
777 var shouldPollCards = false; | 818 var shouldPollCards = false; |
819 var shouldSetBackground = false; | |
778 | 820 |
779 if (signedIn) { | 821 if (signedIn && enableExperiment) { |
780 if (geolocationEnabled) { | 822 if (geolocationEnabled) { |
781 if (!userRespondedToToast) { | 823 if (!userRespondedToToast) { |
782 // If the user enabled geolocation independently of Google Now, | 824 // If the user enabled geolocation independently of Google Now, |
783 // the user has implicitly responded to the toast. | 825 // the user has implicitly responded to the toast. |
784 // We do not want to show it again. | 826 // We do not want to show it again. |
785 chrome.storage.local.set({userRespondedToToast: true}); | 827 chrome.storage.local.set({userRespondedToToast: true}); |
786 } | 828 } |
787 | 829 |
830 if (enableBackground) | |
831 shouldSetBackground = true; | |
832 | |
788 shouldPollCards = true; | 833 shouldPollCards = true; |
789 } else { | 834 } else { |
790 if (userRespondedToToast) { | 835 if (userRespondedToToast) { |
791 recordEvent(GoogleNowEvent.USER_SUPPRESSED); | 836 recordEvent(GoogleNowEvent.USER_SUPPRESSED); |
792 } else { | 837 } else { |
793 shouldSetToastVisible = true; | 838 shouldSetToastVisible = true; |
794 } | 839 } |
795 } | 840 } |
796 } else { | 841 } else { |
797 recordEvent(GoogleNowEvent.STOPPED); | 842 recordEvent(GoogleNowEvent.STOPPED); |
798 } | 843 } |
799 | 844 |
800 console.log( | 845 console.log( |
801 'Requested Actions setToastVisible=' + shouldSetToastVisible + ' ' + | 846 'Requested Actions shouldSetBackground=' + shouldSetBackground + ' ' + |
847 'setToastVisible=' + shouldSetToastVisible + ' ' + | |
802 'setShouldPollCards=' + shouldPollCards); | 848 'setShouldPollCards=' + shouldPollCards); |
803 | 849 |
804 setToastVisible(shouldSetToastVisible, function() { | 850 setBackgroundEnable(shouldSetBackground, function() { |
805 setShouldPollCards(shouldPollCards, callback); | 851 setToastVisible(shouldSetToastVisible, function() { |
852 setShouldPollCards(shouldPollCards, callback); | |
853 }); | |
806 }); | 854 }); |
807 } | 855 } |
808 | 856 |
809 /** | 857 /** |
810 * Coordinates the behavior of Google Now for Chrome depending on | 858 * Coordinates the behavior of Google Now for Chrome depending on |
811 * Chrome and extension state. | 859 * Chrome and extension state. |
812 */ | 860 */ |
813 function onStateChange() { | 861 function onStateChange() { |
814 tasks.add(STATE_CHANGED_TASK_NAME, function(callback) { | 862 tasks.add(STATE_CHANGED_TASK_NAME, function(callback) { |
815 tasks.debugSetStepName('onStateChange-isSignedIn'); | 863 tasks.debugSetStepName('onStateChange-isSignedIn'); |
816 authenticationManager.isSignedIn(function(token) { | 864 authenticationManager.isSignedIn(function(token) { |
817 var signedIn = !!token && !!NOTIFICATION_CARDS_URL; | 865 var signedIn = !!token && !!NOTIFICATION_CARDS_URL; |
818 tasks.debugSetStepName( | 866 instrumented.metricsPrivate.getFieldTrial( |
819 'onStateChange-get-googleGeolocationAccessEnabledPref'); | 867 'GoogleNow', |
820 instrumented. | 868 function(response) { |
821 preferencesPrivate. | 869 // '' means we were enabled via the flags, but |
822 googleGeolocationAccessEnabled. | 870 // the experiment is not running. |
823 get({}, function(prefValue) { | 871 console.log('Experiment Status: ' + response); |
824 var geolocationEnabled = !!prefValue.value; | 872 var enableExperiment = (response.substring(0, 6) == 'Enable') || |
vadimt
2013/08/09 22:13:39
You don't need this. Simply write:
var enableBackg
robliao
2013/08/09 22:16:47
We want to enable the experiment even with EnableW
| |
873 (response == ''); | |
874 var enableBackground = enableExperiment && | |
875 (response != 'EnableWithoutBackground'); | |
825 tasks.debugSetStepName( | 876 tasks.debugSetStepName( |
826 'onStateChange-get-userRespondedToToast'); | 877 'onStateChange-get-googleGeolocationAccessEnabledPref'); |
827 instrumented.storage.local.get( | 878 instrumented. |
828 'userRespondedToToast', | 879 preferencesPrivate. |
829 function(items) { | 880 googleGeolocationAccessEnabled. |
830 var userRespondedToToast = !!items.userRespondedToToast; | 881 get({}, function(prefValue) { |
831 updateRunningState( | 882 var geolocationEnabled = !!prefValue.value; |
832 signedIn, | 883 tasks.debugSetStepName( |
833 geolocationEnabled, | 884 'onStateChange-get-userRespondedToToast'); |
834 userRespondedToToast, | 885 instrumented.storage.local.get( |
835 callback); | 886 'userRespondedToToast', |
887 function(items) { | |
888 var userRespondedToToast = !!items.userRespondedToToast; | |
889 updateRunningState( | |
890 signedIn, | |
891 geolocationEnabled, | |
892 userRespondedToToast, | |
893 enableExperiment, | |
894 enableBackground, | |
895 callback); | |
896 }); | |
836 }); | 897 }); |
837 }); | 898 }); |
838 }); | 899 }); |
839 }); | 900 }); |
840 } | 901 } |
841 | 902 |
842 /** | 903 /** |
843 * Displays a toast to the user asking if they want to opt in to receiving | 904 * Displays a toast to the user asking if they want to opt in to receiving |
844 * Google Now cards. | 905 * Google Now cards. |
845 */ | 906 */ |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
925 | 986 |
926 instrumented.location.onLocationUpdate.addListener(function(position) { | 987 instrumented.location.onLocationUpdate.addListener(function(position) { |
927 recordEvent(GoogleNowEvent.LOCATION_UPDATE); | 988 recordEvent(GoogleNowEvent.LOCATION_UPDATE); |
928 updateNotificationsCards(position); | 989 updateNotificationsCards(position); |
929 }); | 990 }); |
930 | 991 |
931 instrumented.omnibox.onInputEntered.addListener(function(text) { | 992 instrumented.omnibox.onInputEntered.addListener(function(text) { |
932 localStorage['server_url'] = NOTIFICATION_CARDS_URL = text; | 993 localStorage['server_url'] = NOTIFICATION_CARDS_URL = text; |
933 initialize(); | 994 initialize(); |
934 }); | 995 }); |
OLD | NEW |