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 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} enableBackground true if | |
| 798 * the background permission should be requested. | |
| 763 * @param {function()} callback Call this function on completion. | 799 * @param {function()} callback Call this function on completion. |
| 764 */ | 800 */ |
| 765 function updateRunningState( | 801 function updateRunningState( |
| 766 signedIn, | 802 signedIn, |
| 767 geolocationEnabled, | 803 geolocationEnabled, |
| 768 userRespondedToToast, | 804 userRespondedToToast, |
| 805 enableBackground, | |
| 769 callback) { | 806 callback) { |
| 770 | 807 |
| 771 console.log( | 808 console.log( |
| 772 'State Update signedIn=' + signedIn + ' ' + | 809 'State Update signedIn=' + signedIn + ' ' + |
| 773 'geolocationEnabled=' + geolocationEnabled + ' ' + | 810 'geolocationEnabled=' + geolocationEnabled + ' ' + |
| 774 'userRespondedToToast=' + userRespondedToToast); | 811 'userRespondedToToast=' + userRespondedToToast); |
| 775 | 812 |
| 776 var shouldSetToastVisible = false; | 813 var shouldSetToastVisible = false; |
| 777 var shouldPollCards = false; | 814 var shouldPollCards = false; |
| 815 var shouldSetBackground = false; | |
| 778 | 816 |
| 779 if (signedIn) { | 817 if (signedIn) { |
| 780 if (geolocationEnabled) { | 818 if (geolocationEnabled) { |
| 781 if (!userRespondedToToast) { | 819 if (!userRespondedToToast) { |
| 782 // If the user enabled geolocation independently of Google Now, | 820 // If the user enabled geolocation independently of Google Now, |
| 783 // the user has implicitly responded to the toast. | 821 // the user has implicitly responded to the toast. |
| 784 // We do not want to show it again. | 822 // We do not want to show it again. |
| 785 chrome.storage.local.set({userRespondedToToast: true}); | 823 chrome.storage.local.set({userRespondedToToast: true}); |
| 786 } | 824 } |
| 787 | 825 |
| 826 if (enableBackground) | |
| 827 shouldSetBackground = true; | |
| 828 | |
| 788 shouldPollCards = true; | 829 shouldPollCards = true; |
| 789 } else { | 830 } else { |
| 790 if (userRespondedToToast) { | 831 if (userRespondedToToast) { |
| 791 recordEvent(GoogleNowEvent.USER_SUPPRESSED); | 832 recordEvent(GoogleNowEvent.USER_SUPPRESSED); |
| 792 } else { | 833 } else { |
| 793 shouldSetToastVisible = true; | 834 shouldSetToastVisible = true; |
| 794 } | 835 } |
| 795 } | 836 } |
| 796 } else { | 837 } else { |
| 797 recordEvent(GoogleNowEvent.STOPPED); | 838 recordEvent(GoogleNowEvent.STOPPED); |
| 798 } | 839 } |
| 799 | 840 |
| 800 console.log( | 841 console.log( |
| 801 'Requested Actions setToastVisible=' + shouldSetToastVisible + ' ' + | 842 'Requested Actions shouldSetBackground=' + shouldSetBackground + ' ' + |
| 843 'setToastVisible=' + shouldSetToastVisible + ' ' + | |
| 802 'setShouldPollCards=' + shouldPollCards); | 844 'setShouldPollCards=' + shouldPollCards); |
| 803 | 845 |
| 804 setToastVisible(shouldSetToastVisible, function() { | 846 setBackgroundEnable(shouldSetBackground, function() { |
| 805 setShouldPollCards(shouldPollCards, callback); | 847 setToastVisible(shouldSetToastVisible, function() { |
| 848 setShouldPollCards(shouldPollCards, callback); | |
| 849 }); | |
| 806 }); | 850 }); |
| 807 } | 851 } |
| 808 | 852 |
| 809 /** | 853 /** |
| 810 * Coordinates the behavior of Google Now for Chrome depending on | 854 * Coordinates the behavior of Google Now for Chrome depending on |
| 811 * Chrome and extension state. | 855 * Chrome and extension state. |
| 812 */ | 856 */ |
| 813 function onStateChange() { | 857 function onStateChange() { |
| 814 tasks.add(STATE_CHANGED_TASK_NAME, function(callback) { | 858 tasks.add(STATE_CHANGED_TASK_NAME, function(callback) { |
| 815 tasks.debugSetStepName('onStateChange-isSignedIn'); | 859 tasks.debugSetStepName('onStateChange-isSignedIn'); |
| 816 authenticationManager.isSignedIn(function(token) { | 860 authenticationManager.isSignedIn(function(token) { |
| 817 var signedIn = !!token && !!NOTIFICATION_CARDS_URL; | 861 var signedIn = !!token && !!NOTIFICATION_CARDS_URL; |
| 818 tasks.debugSetStepName( | 862 instrumented.metricsPrivate.getFieldTrial( |
| 819 'onStateChange-get-googleGeolocationAccessEnabledPref'); | 863 'GoogleNow', |
| 820 instrumented. | 864 function(response) { |
| 821 preferencesPrivate. | 865 console.log('Experiment Status: ' + response); |
|
robliao
2013/08/12 17:18:22
Intentional Addition for Finch Diagnostics
| |
| 822 googleGeolocationAccessEnabled. | 866 var enableBackground = (response != 'EnableWithoutBackground'); |
| 823 get({}, function(prefValue) { | |
| 824 var geolocationEnabled = !!prefValue.value; | |
| 825 tasks.debugSetStepName( | 867 tasks.debugSetStepName( |
| 826 'onStateChange-get-userRespondedToToast'); | 868 'onStateChange-get-googleGeolocationAccessEnabledPref'); |
| 827 instrumented.storage.local.get( | 869 instrumented. |
| 828 'userRespondedToToast', | 870 preferencesPrivate. |
| 829 function(items) { | 871 googleGeolocationAccessEnabled. |
| 830 var userRespondedToToast = !!items.userRespondedToToast; | 872 get({}, function(prefValue) { |
| 831 updateRunningState( | 873 var geolocationEnabled = !!prefValue.value; |
| 832 signedIn, | 874 tasks.debugSetStepName( |
| 833 geolocationEnabled, | 875 'onStateChange-get-userRespondedToToast'); |
| 834 userRespondedToToast, | 876 instrumented.storage.local.get( |
| 835 callback); | 877 'userRespondedToToast', |
| 878 function(items) { | |
| 879 var userRespondedToToast = !!items.userRespondedToToast; | |
| 880 updateRunningState( | |
| 881 signedIn, | |
| 882 geolocationEnabled, | |
| 883 userRespondedToToast, | |
| 884 enableBackground, | |
| 885 callback); | |
| 886 }); | |
| 836 }); | 887 }); |
| 837 }); | 888 }); |
| 838 }); | 889 }); |
| 839 }); | 890 }); |
| 840 } | 891 } |
| 841 | 892 |
| 842 /** | 893 /** |
| 843 * Displays a toast to the user asking if they want to opt in to receiving | 894 * Displays a toast to the user asking if they want to opt in to receiving |
| 844 * Google Now cards. | 895 * Google Now cards. |
| 845 */ | 896 */ |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 925 | 976 |
| 926 instrumented.location.onLocationUpdate.addListener(function(position) { | 977 instrumented.location.onLocationUpdate.addListener(function(position) { |
| 927 recordEvent(GoogleNowEvent.LOCATION_UPDATE); | 978 recordEvent(GoogleNowEvent.LOCATION_UPDATE); |
| 928 updateNotificationsCards(position); | 979 updateNotificationsCards(position); |
| 929 }); | 980 }); |
| 930 | 981 |
| 931 instrumented.omnibox.onInputEntered.addListener(function(text) { | 982 instrumented.omnibox.onInputEntered.addListener(function(text) { |
| 932 localStorage['server_url'] = NOTIFICATION_CARDS_URL = text; | 983 localStorage['server_url'] = NOTIFICATION_CARDS_URL = text; |
| 933 initialize(); | 984 initialize(); |
| 934 }); | 985 }); |
| OLD | NEW |