| 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('metricsPrivate.getVariationParams', 1); | 122 tasks.instrumentChromeApiFunction('metricsPrivate.getVariationParams', 1); |
| 122 tasks.instrumentChromeApiFunction('notifications.create', 2); | 123 tasks.instrumentChromeApiFunction('notifications.create', 2); |
| 123 tasks.instrumentChromeApiFunction('notifications.update', 2); | 124 tasks.instrumentChromeApiFunction('notifications.update', 2); |
| 124 tasks.instrumentChromeApiFunction('notifications.getAll', 0); | 125 tasks.instrumentChromeApiFunction('notifications.getAll', 0); |
| 125 tasks.instrumentChromeApiFunction( | 126 tasks.instrumentChromeApiFunction( |
| 126 'notifications.onButtonClicked.addListener', 0); | 127 'notifications.onButtonClicked.addListener', 0); |
| 127 tasks.instrumentChromeApiFunction('notifications.onClicked.addListener', 0); | 128 tasks.instrumentChromeApiFunction('notifications.onClicked.addListener', 0); |
| 128 tasks.instrumentChromeApiFunction('notifications.onClosed.addListener', 0); | 129 tasks.instrumentChromeApiFunction('notifications.onClosed.addListener', 0); |
| 129 tasks.instrumentChromeApiFunction('omnibox.onInputEntered.addListener', 0); | 130 tasks.instrumentChromeApiFunction('omnibox.onInputEntered.addListener', 0); |
| 130 tasks.instrumentChromeApiFunction( | 131 tasks.instrumentChromeApiFunction( |
| 131 'preferencesPrivate.googleGeolocationAccessEnabled.get', | 132 'preferencesPrivate.googleGeolocationAccessEnabled.get', |
| 132 1); | 133 1); |
| 133 tasks.instrumentChromeApiFunction( | 134 tasks.instrumentChromeApiFunction( |
| 134 'preferencesPrivate.googleGeolocationAccessEnabled.onChange.addListener', | 135 'preferencesPrivate.googleGeolocationAccessEnabled.onChange.addListener', |
| 135 0); | 136 0); |
| 137 tasks.instrumentChromeApiFunction('permissions.contains', 1); |
| 138 tasks.instrumentChromeApiFunction('permissions.remove', 1); |
| 139 tasks.instrumentChromeApiFunction('permissions.request', 1); |
| 136 tasks.instrumentChromeApiFunction('runtime.onInstalled.addListener', 0); | 140 tasks.instrumentChromeApiFunction('runtime.onInstalled.addListener', 0); |
| 137 tasks.instrumentChromeApiFunction('runtime.onStartup.addListener', 0); | 141 tasks.instrumentChromeApiFunction('runtime.onStartup.addListener', 0); |
| 138 tasks.instrumentChromeApiFunction('tabs.create', 1); | 142 tasks.instrumentChromeApiFunction('tabs.create', 1); |
| 139 tasks.instrumentChromeApiFunction('storage.local.get', 1); | 143 tasks.instrumentChromeApiFunction('storage.local.get', 1); |
| 140 | 144 |
| 141 var updateCardsAttempts = buildAttemptManager( | 145 var updateCardsAttempts = buildAttemptManager( |
| 142 'cards-update', | 146 'cards-update', |
| 143 requestLocation, | 147 requestLocation, |
| 144 INITIAL_POLLING_PERIOD_SECONDS, | 148 INITIAL_POLLING_PERIOD_SECONDS, |
| 145 MAXIMUM_POLLING_PERIOD_SECONDS); | 149 MAXIMUM_POLLING_PERIOD_SECONDS); |
| (...skipping 609 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 755 hideWelcomeToast(); | 759 hideWelcomeToast(); |
| 756 } else { | 760 } else { |
| 757 console.log('Action Ignored setToastVisible=' + visibleRequest); | 761 console.log('Action Ignored setToastVisible=' + visibleRequest); |
| 758 } | 762 } |
| 759 | 763 |
| 760 callback(); | 764 callback(); |
| 761 }); | 765 }); |
| 762 } | 766 } |
| 763 | 767 |
| 764 /** | 768 /** |
| 769 * Enables or disables the Google Now background permission. |
| 770 * @param {boolean} backgroundEnable true to run in the background. |
| 771 * false to not run in the background. |
| 772 * @param {function} callback Called on completion. |
| 773 */ |
| 774 function setBackgroundEnable(backgroundEnable, callback) { |
| 775 instrumented.permissions.contains({permissions: ['background']}, |
| 776 function(hasPermission) { |
| 777 if (backgroundEnable != hasPermission) { |
| 778 console.log('Action Taken setBackgroundEnable=' + backgroundEnable); |
| 779 if (backgroundEnable) |
| 780 instrumented.permissions.request( |
| 781 {permissions: ['background']}, |
| 782 function() { |
| 783 callback(); |
| 784 }); |
| 785 else |
| 786 instrumented.permissions.remove( |
| 787 {permissions: ['background']}, |
| 788 function() { |
| 789 callback(); |
| 790 }); |
| 791 } else { |
| 792 console.log('Action Ignored setBackgroundEnable=' + backgroundEnable); |
| 793 callback(); |
| 794 } |
| 795 }); |
| 796 } |
| 797 |
| 798 /** |
| 765 * Does the actual work of deciding what Google Now should do | 799 * Does the actual work of deciding what Google Now should do |
| 766 * based off of the current state of Chrome. | 800 * based off of the current state of Chrome. |
| 767 * @param {boolean} signedIn true if the user is signed in. | 801 * @param {boolean} signedIn true if the user is signed in. |
| 768 * @param {boolean} geolocationEnabled true if | 802 * @param {boolean} geolocationEnabled true if |
| 769 * the geolocation option is enabled. | 803 * the geolocation option is enabled. |
| 770 * @param {boolean} userRespondedToToast true if | 804 * @param {boolean} userRespondedToToast true if |
| 771 * the user has responded to the toast. | 805 * the user has responded to the toast. |
| 806 * @param {boolean} enableBackground true if |
| 807 * the background permission should be requested. |
| 772 * @param {function()} callback Call this function on completion. | 808 * @param {function()} callback Call this function on completion. |
| 773 */ | 809 */ |
| 774 function updateRunningState( | 810 function updateRunningState( |
| 775 signedIn, | 811 signedIn, |
| 776 geolocationEnabled, | 812 geolocationEnabled, |
| 777 userRespondedToToast, | 813 userRespondedToToast, |
| 814 enableBackground, |
| 778 callback) { | 815 callback) { |
| 779 | 816 |
| 780 console.log( | 817 console.log( |
| 781 'State Update signedIn=' + signedIn + ' ' + | 818 'State Update signedIn=' + signedIn + ' ' + |
| 782 'geolocationEnabled=' + geolocationEnabled + ' ' + | 819 'geolocationEnabled=' + geolocationEnabled + ' ' + |
| 783 'userRespondedToToast=' + userRespondedToToast); | 820 'userRespondedToToast=' + userRespondedToToast); |
| 784 | 821 |
| 785 var shouldSetToastVisible = false; | 822 var shouldSetToastVisible = false; |
| 786 var shouldPollCards = false; | 823 var shouldPollCards = false; |
| 824 var shouldSetBackground = false; |
| 787 | 825 |
| 788 if (signedIn) { | 826 if (signedIn) { |
| 789 if (geolocationEnabled) { | 827 if (geolocationEnabled) { |
| 790 if (!userRespondedToToast) { | 828 if (!userRespondedToToast) { |
| 791 // If the user enabled geolocation independently of Google Now, | 829 // If the user enabled geolocation independently of Google Now, |
| 792 // the user has implicitly responded to the toast. | 830 // the user has implicitly responded to the toast. |
| 793 // We do not want to show it again. | 831 // We do not want to show it again. |
| 794 chrome.storage.local.set({userRespondedToToast: true}); | 832 chrome.storage.local.set({userRespondedToToast: true}); |
| 795 } | 833 } |
| 796 | 834 |
| 835 if (enableBackground) |
| 836 shouldSetBackground = true; |
| 837 |
| 797 shouldPollCards = true; | 838 shouldPollCards = true; |
| 798 } else { | 839 } else { |
| 799 if (userRespondedToToast) { | 840 if (userRespondedToToast) { |
| 800 recordEvent(GoogleNowEvent.USER_SUPPRESSED); | 841 recordEvent(GoogleNowEvent.USER_SUPPRESSED); |
| 801 } else { | 842 } else { |
| 802 shouldSetToastVisible = true; | 843 shouldSetToastVisible = true; |
| 803 } | 844 } |
| 804 } | 845 } |
| 805 } else { | 846 } else { |
| 806 recordEvent(GoogleNowEvent.STOPPED); | 847 recordEvent(GoogleNowEvent.STOPPED); |
| 807 } | 848 } |
| 808 | 849 |
| 809 console.log( | 850 console.log( |
| 810 'Requested Actions setToastVisible=' + shouldSetToastVisible + ' ' + | 851 'Requested Actions shouldSetBackground=' + shouldSetBackground + ' ' + |
| 852 'setToastVisible=' + shouldSetToastVisible + ' ' + |
| 811 'setShouldPollCards=' + shouldPollCards); | 853 'setShouldPollCards=' + shouldPollCards); |
| 812 | 854 |
| 813 setToastVisible(shouldSetToastVisible, function() { | 855 setBackgroundEnable(shouldSetBackground, function() { |
| 814 setShouldPollCards(shouldPollCards, callback); | 856 setToastVisible(shouldSetToastVisible, function() { |
| 857 setShouldPollCards(shouldPollCards, callback); |
| 858 }); |
| 815 }); | 859 }); |
| 816 } | 860 } |
| 817 | 861 |
| 818 /** | 862 /** |
| 819 * Coordinates the behavior of Google Now for Chrome depending on | 863 * Coordinates the behavior of Google Now for Chrome depending on |
| 820 * Chrome and extension state. | 864 * Chrome and extension state. |
| 821 */ | 865 */ |
| 822 function onStateChange() { | 866 function onStateChange() { |
| 823 tasks.add(STATE_CHANGED_TASK_NAME, function(callback) { | 867 tasks.add(STATE_CHANGED_TASK_NAME, function(callback) { |
| 824 tasks.debugSetStepName('onStateChange-isSignedIn'); | 868 tasks.debugSetStepName('onStateChange-isSignedIn'); |
| 825 authenticationManager.isSignedIn(function(token) { | 869 authenticationManager.isSignedIn(function(token) { |
| 826 var signedIn = !!token && !!NOTIFICATION_CARDS_URL; | 870 var signedIn = !!token && !!NOTIFICATION_CARDS_URL; |
| 827 tasks.debugSetStepName( | 871 instrumented.metricsPrivate.getFieldTrial( |
| 828 'onStateChange-get-googleGeolocationAccessEnabledPref'); | 872 'GoogleNow', |
| 829 instrumented. | 873 function(response) { |
| 830 preferencesPrivate. | 874 console.log('Experiment Status: ' + response); |
| 831 googleGeolocationAccessEnabled. | 875 var enableBackground = (response != 'EnableWithoutBackground'); |
| 832 get({}, function(prefValue) { | |
| 833 var geolocationEnabled = !!prefValue.value; | |
| 834 tasks.debugSetStepName( | 876 tasks.debugSetStepName( |
| 835 'onStateChange-get-userRespondedToToast'); | 877 'onStateChange-get-googleGeolocationAccessEnabledPref'); |
| 836 instrumented.storage.local.get( | 878 instrumented. |
| 837 'userRespondedToToast', | 879 preferencesPrivate. |
| 838 function(items) { | 880 googleGeolocationAccessEnabled. |
| 839 var userRespondedToToast = !!items.userRespondedToToast; | 881 get({}, function(prefValue) { |
| 840 updateRunningState( | 882 var geolocationEnabled = !!prefValue.value; |
| 841 signedIn, | 883 tasks.debugSetStepName( |
| 842 geolocationEnabled, | 884 'onStateChange-get-userRespondedToToast'); |
| 843 userRespondedToToast, | 885 instrumented.storage.local.get( |
| 844 callback); | 886 'userRespondedToToast', |
| 887 function(items) { |
| 888 var userRespondedToToast = !!items.userRespondedToToast; |
| 889 updateRunningState( |
| 890 signedIn, |
| 891 geolocationEnabled, |
| 892 userRespondedToToast, |
| 893 enableBackground, |
| 894 callback); |
| 895 }); |
| 845 }); | 896 }); |
| 846 }); | 897 }); |
| 847 }); | 898 }); |
| 848 }); | 899 }); |
| 849 } | 900 } |
| 850 | 901 |
| 851 /** | 902 /** |
| 852 * Displays a toast to the user asking if they want to opt in to receiving | 903 * Displays a toast to the user asking if they want to opt in to receiving |
| 853 * Google Now cards. | 904 * Google Now cards. |
| 854 */ | 905 */ |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 934 | 985 |
| 935 instrumented.location.onLocationUpdate.addListener(function(position) { | 986 instrumented.location.onLocationUpdate.addListener(function(position) { |
| 936 recordEvent(GoogleNowEvent.LOCATION_UPDATE); | 987 recordEvent(GoogleNowEvent.LOCATION_UPDATE); |
| 937 updateNotificationsCards(position); | 988 updateNotificationsCards(position); |
| 938 }); | 989 }); |
| 939 | 990 |
| 940 instrumented.omnibox.onInputEntered.addListener(function(text) { | 991 instrumented.omnibox.onInputEntered.addListener(function(text) { |
| 941 localStorage['server_url'] = NOTIFICATION_CARDS_URL = text; | 992 localStorage['server_url'] = NOTIFICATION_CARDS_URL = text; |
| 942 initialize(); | 993 initialize(); |
| 943 }); | 994 }); |
| OLD | NEW |