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 691 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 702 function setShouldPollCards(shouldPollCardsRequest, onSuccess) { | 702 function setShouldPollCards(shouldPollCardsRequest, onSuccess) { |
| 703 tasks.debugSetStepName( | 703 tasks.debugSetStepName( |
| 704 'setShouldRun-shouldRun-updateCardsAttemptsIsRunning'); | 704 'setShouldRun-shouldRun-updateCardsAttemptsIsRunning'); |
| 705 updateCardsAttempts.isRunning(function(currentValue) { | 705 updateCardsAttempts.isRunning(function(currentValue) { |
| 706 if (shouldPollCardsRequest != currentValue) { | 706 if (shouldPollCardsRequest != currentValue) { |
| 707 console.log('Action Taken setShouldPollCards=' + shouldPollCardsRequest); | 707 console.log('Action Taken setShouldPollCards=' + shouldPollCardsRequest); |
| 708 if (shouldPollCardsRequest) | 708 if (shouldPollCardsRequest) |
| 709 startPollingCards(); | 709 startPollingCards(); |
| 710 else | 710 else |
| 711 stopPollingCards(); | 711 stopPollingCards(); |
| 712 } else { | |
| 713 console.log( | |
| 714 'Action Ignored setShouldPollCards=' + shouldPollCardsRequest); | |
| 712 } | 715 } |
| 713 onSuccess(); | 716 onSuccess(); |
| 714 }); | 717 }); |
| 715 } | 718 } |
| 716 | 719 |
| 717 /** | 720 /** |
| 718 * Shows or hides the toast. | 721 * Shows or hides the toast. |
| 719 * @param {boolean} visibleRequest true to show the toast and | 722 * @param {boolean} visibleRequest true to show the toast and |
| 720 * false to hide the toast. | 723 * false to hide the toast. |
| 721 * @param {function} onSuccess Called on completion. | 724 * @param {function} onSuccess Called on completion. |
| 722 */ | 725 */ |
| 723 function setToastVisible(visibleRequest, onSuccess) { | 726 function setToastVisible(visibleRequest, onSuccess) { |
| 724 tasks.debugSetStepName( | 727 tasks.debugSetStepName( |
| 725 'setToastVisible-shouldSetToastVisible-getAllNotifications'); | 728 'setToastVisible-shouldSetToastVisible-getAllNotifications'); |
| 726 chrome.notifications.getAll(function(notifications) { | 729 chrome.notifications.getAll(function(notifications) { |
| 727 // TODO(vadimt): Figure out what to do when notifications are disabled for | 730 // TODO(vadimt): Figure out what to do when notifications are disabled for |
| 728 // our extension. | 731 // our extension. |
| 729 notifications = notifications || {}; | 732 notifications = notifications || {}; |
| 730 | 733 |
| 731 if (visibleRequest != !!notifications[WELCOME_TOAST_NOTIFICATION_ID]) { | 734 if (visibleRequest != !!notifications[WELCOME_TOAST_NOTIFICATION_ID]) { |
| 732 console.log('Action Taken setToastVisible=' + visibleRequest); | 735 console.log('Action Taken setToastVisible=' + visibleRequest); |
| 733 if (visibleRequest) | 736 if (visibleRequest) |
| 734 showWelcomeToast(); | 737 showWelcomeToast(); |
| 735 else | 738 else |
| 736 hideWelcomeToast(); | 739 hideWelcomeToast(); |
| 740 } else { | |
| 741 console.log('Action Ignored setToastVisible=' + visibleRequest); | |
| 737 } | 742 } |
| 738 | 743 |
| 739 onSuccess(); | 744 onSuccess(); |
| 740 }); | 745 }); |
| 741 } | 746 } |
| 742 | 747 |
| 743 /** | 748 /** |
| 749 * Enables or disables the Google Now background permission. | |
| 750 * @param {boolean} backgroundEnable true to run in the background. | |
| 751 * false to not run in the background. | |
| 752 * @param {function} onSuccess Called on completion. | |
| 753 */ | |
| 754 function setBackgroundEnable(backgroundEnable, onSuccess) { | |
|
vadimt
2013/08/02 21:54:43
onSuccess -> callback
robliao
2013/08/03 01:21:18
Kept this way to remain consistent with the above.
| |
| 755 chrome.permissions.contains({permissions: ['background']}, | |
|
vadimt
2013/08/02 21:54:43
Please instrument new APIs.
robliao
2013/08/03 01:21:18
Done.
| |
| 756 function(hasPermission) { | |
| 757 if (backgroundEnable != hasPermission) { | |
| 758 console.log('Action Taken setBackgroundEnable=' + backgroundEnable); | |
| 759 if (backgroundEnable) | |
| 760 chrome.permissions.request( | |
| 761 {permissions: ['background']}, | |
| 762 function() { | |
| 763 onSuccess(); | |
| 764 }); | |
| 765 else | |
| 766 chrome.permissions.remove( | |
| 767 {permissions: ['background']}, | |
| 768 function() { | |
| 769 onSuccess(); | |
| 770 }); | |
| 771 } else { | |
| 772 console.log('Action Ignored setBackgroundEnable=' + backgroundEnable); | |
| 773 onSuccess(); | |
| 774 } | |
| 775 }); | |
| 776 } | |
| 777 | |
| 778 /** | |
| 744 * Does the actual work of deciding what Google Now should do | 779 * Does the actual work of deciding what Google Now should do |
| 745 * based off of the current state of Chrome. | 780 * based off of the current state of Chrome. |
| 746 * @param {boolean} signedIn true if the user is signed in. | 781 * @param {boolean} signedIn true if the user is signed in. |
| 747 * @param {boolean} geolocationEnabled true if | 782 * @param {boolean} geolocationEnabled true if |
| 748 * the geolocation option is enabled. | 783 * the geolocation option is enabled. |
| 749 * @param {boolean} userRespondedToToast true if | 784 * @param {boolean} userRespondedToToast true if |
| 750 * the user has responded to the toast. | 785 * the user has responded to the toast. |
| 786 * @param {boolean} enableExperiment true if | |
| 787 * this extension should be running. | |
|
vadimt
2013/08/02 21:54:43
Actually, we need a flag that conditionally turns
robliao
2013/08/03 01:21:18
We will want to update the doc:
S & L => poll + ba
vadimt
2013/08/03 02:07:53
The ultimate goal is to implement as described in
robliao
2013/08/09 21:46:44
Clarified with
Modes without Background:
EnableWit
| |
| 751 * @param {function()} callback Call this function on completion. | 788 * @param {function()} callback Call this function on completion. |
| 752 */ | 789 */ |
| 753 function updateRunningState( | 790 function updateRunningState( |
| 754 signedIn, | 791 signedIn, |
| 755 geolocationEnabled, | 792 geolocationEnabled, |
| 756 userRespondedToToast, | 793 userRespondedToToast, |
| 794 enableExperiment, | |
| 757 callback) { | 795 callback) { |
|
vadimt
2013/08/02 21:54:43
Are you going to get rid of callback and debugSetS
robliao
2013/08/03 01:21:18
That's the plan. I didn't know if you were doing t
vadimt
2013/08/03 02:07:53
Please do this only for your task. I'll take care
robliao
2013/08/09 21:46:44
Done. There was no debugSetStepName added in.
On 2
vadimt
2013/08/09 22:13:39
OK :), but please plan a separate CL where you'll
| |
| 758 | 796 |
| 759 console.log( | 797 console.log( |
| 760 'State Update signedIn=' + signedIn + ' ' + | 798 'State Update signedIn=' + signedIn + ' ' + |
| 761 'geolocationEnabled=' + geolocationEnabled + ' ' + | 799 'geolocationEnabled=' + geolocationEnabled + ' ' + |
| 762 'userRespondedToToast=' + userRespondedToToast); | 800 'userRespondedToToast=' + userRespondedToToast + ' ' + |
| 801 'enableExperiment=' + enableExperiment); | |
| 763 | 802 |
| 764 var shouldSetToastVisible = false; | 803 var shouldSetToastVisible = false; |
| 765 var shouldPollCards = false; | 804 var shouldPollCards = false; |
| 805 var shouldSetBackground = false; | |
| 766 | 806 |
| 767 if (signedIn) { | 807 if (signedIn && enableExperiment) { |
| 768 if (geolocationEnabled) { | 808 if (geolocationEnabled) { |
| 769 if (!userRespondedToToast) { | 809 if (!userRespondedToToast) { |
| 770 // If the user enabled geolocation independently of Google Now, | 810 // If the user enabled geolocation independently of Google Now, |
| 771 // the user has implicitly responded to the toast. | 811 // the user has implicitly responded to the toast. |
| 772 // We do not want to show it again. | 812 // We do not want to show it again. |
| 773 storage.set({userRespondedToToast: true}); | 813 storage.set({userRespondedToToast: true}); |
| 774 } | 814 } |
| 775 | 815 |
| 776 shouldPollCards = true; | 816 shouldPollCards = true; |
| 817 shouldSetBackground = true; | |
| 777 } else { | 818 } else { |
| 778 if (userRespondedToToast) { | 819 if (userRespondedToToast) { |
| 779 recordEvent(GoogleNowEvent.USER_SUPPRESSED); | 820 recordEvent(GoogleNowEvent.USER_SUPPRESSED); |
| 780 } else { | 821 } else { |
| 781 shouldSetToastVisible = true; | 822 shouldSetToastVisible = true; |
| 782 } | 823 } |
| 783 } | 824 } |
| 784 } else { | 825 } else { |
| 785 recordEvent(GoogleNowEvent.STOPPED); | 826 recordEvent(GoogleNowEvent.STOPPED); |
| 786 } | 827 } |
| 787 | 828 |
| 788 console.log( | 829 console.log( |
| 789 'Requested Actions setToastVisible=' + shouldSetToastVisible + ' ' + | 830 'Requested Actions shouldSetBackground=' + shouldSetBackground + ' ' + |
| 831 'setToastVisible=' + shouldSetToastVisible + ' ' + | |
| 790 'setShouldPollCards=' + shouldPollCards); | 832 'setShouldPollCards=' + shouldPollCards); |
| 791 | 833 |
| 792 setToastVisible(shouldSetToastVisible, function() { | 834 setBackgroundEnable(shouldSetBackground, function() { |
| 793 setShouldPollCards(shouldPollCards, callback); | 835 setToastVisible(shouldSetToastVisible, function() { |
| 836 setShouldPollCards(shouldPollCards, callback); | |
| 837 }); | |
| 794 }); | 838 }); |
| 795 } | 839 } |
| 796 | 840 |
| 797 /** | 841 /** |
| 798 * Coordinates the behavior of Google Now for Chrome depending on | 842 * Coordinates the behavior of Google Now for Chrome depending on |
| 799 * Chrome and extension state. | 843 * Chrome and extension state. |
| 800 */ | 844 */ |
| 801 function onStateChange() { | 845 function onStateChange() { |
| 802 tasks.add(STATE_CHANGED_TASK_NAME, function(callback) { | 846 tasks.add(STATE_CHANGED_TASK_NAME, function(callback) { |
| 803 tasks.debugSetStepName('onStateChange-isSignedIn'); | 847 tasks.debugSetStepName('onStateChange-isSignedIn'); |
| 804 authenticationManager.isSignedIn(function(token) { | 848 authenticationManager.isSignedIn(function(token) { |
| 805 var signedIn = !!token && !!NOTIFICATION_CARDS_URL; | 849 var signedIn = !!token && !!NOTIFICATION_CARDS_URL; |
| 806 tasks.debugSetStepName( | 850 chrome.metricsPrivate.getFieldTrial('GoogleNow', function(response) { |
|
vadimt
2013/08/02 21:54:43
How did you test this?
robliao
2013/08/03 01:21:18
Tested with my personal finch server.
On 2013/08/0
| |
| 807 'onStateChange-get-googleGeolocationAccessEnabledPref'); | 851 // '' means we were enabled via the flags but not the experiment. |
|
vadimt
2013/08/02 21:54:43
More precisely:
// '' means we were enabled via th
robliao
2013/08/03 01:21:18
Done.
| |
| 808 googleGeolocationAccessEnabledPref.get({}, function(prefValue) { | 852 var enableExperiment = (response == 'EnableWithBackground' || |
| 809 var geolocationEnabled = !!prefValue.value; | 853 (response == 'EnableWithFlag') || |
| 854 (response == '')); | |
| 810 tasks.debugSetStepName( | 855 tasks.debugSetStepName( |
| 811 'onStateChange-get-userRespondedToToast'); | 856 'onStateChange-get-googleGeolocationAccessEnabledPref'); |
| 812 storage.get('userRespondedToToast', function(items) { | 857 googleGeolocationAccessEnabledPref.get({}, function(prefValue) { |
| 813 var userRespondedToToast = !!items.userRespondedToToast; | 858 var geolocationEnabled = !!prefValue.value; |
| 814 updateRunningState( | 859 tasks.debugSetStepName( |
| 815 signedIn, | 860 'onStateChange-get-userRespondedToToast'); |
| 816 geolocationEnabled, | 861 storage.get('userRespondedToToast', function(items) { |
| 817 userRespondedToToast, | 862 var userRespondedToToast = !!items.userRespondedToToast; |
| 818 callback); | 863 updateRunningState( |
| 864 signedIn, | |
| 865 geolocationEnabled, | |
| 866 userRespondedToToast, | |
| 867 enableExperiment, | |
| 868 callback); | |
| 869 }); | |
| 819 }); | 870 }); |
| 820 }); | 871 }); |
| 821 }); | 872 }); |
| 822 }); | 873 }); |
| 823 } | 874 } |
| 824 | 875 |
| 825 /** | 876 /** |
| 826 * Displays a toast to the user asking if they want to opt in to receiving | 877 * Displays a toast to the user asking if they want to opt in to receiving |
| 827 * Google Now cards. | 878 * Google Now cards. |
| 828 */ | 879 */ |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 903 | 954 |
| 904 chrome.location.onLocationUpdate.addListener(function(position) { | 955 chrome.location.onLocationUpdate.addListener(function(position) { |
| 905 recordEvent(GoogleNowEvent.LOCATION_UPDATE); | 956 recordEvent(GoogleNowEvent.LOCATION_UPDATE); |
| 906 updateNotificationsCards(position); | 957 updateNotificationsCards(position); |
| 907 }); | 958 }); |
| 908 | 959 |
| 909 chrome.omnibox.onInputEntered.addListener(function(text) { | 960 chrome.omnibox.onInputEntered.addListener(function(text) { |
| 910 localStorage['server_url'] = NOTIFICATION_CARDS_URL = text; | 961 localStorage['server_url'] = NOTIFICATION_CARDS_URL = text; |
| 911 initialize(); | 962 initialize(); |
| 912 }); | 963 }); |
| OLD | NEW |