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 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
68 * Time we keep dismissals after successful server dismiss requests. | 68 * Time we keep dismissals after successful server dismiss requests. |
69 */ | 69 */ |
70 var DISMISS_RETENTION_TIME_MS = 20 * 60 * 1000; // 20 minutes | 70 var DISMISS_RETENTION_TIME_MS = 20 * 60 * 1000; // 20 minutes |
71 | 71 |
72 /** | 72 /** |
73 * Names for tasks that can be created by the extension. | 73 * Names for tasks that can be created by the extension. |
74 */ | 74 */ |
75 var UPDATE_CARDS_TASK_NAME = 'update-cards'; | 75 var UPDATE_CARDS_TASK_NAME = 'update-cards'; |
76 var DISMISS_CARD_TASK_NAME = 'dismiss-card'; | 76 var DISMISS_CARD_TASK_NAME = 'dismiss-card'; |
77 var RETRY_DISMISS_TASK_NAME = 'retry-dismiss'; | 77 var RETRY_DISMISS_TASK_NAME = 'retry-dismiss'; |
78 var STATE_CHANGED_TASK_NAME = 'state-changed'; | |
78 | 79 |
79 var LOCATION_WATCH_NAME = 'location-watch'; | 80 var LOCATION_WATCH_NAME = 'location-watch'; |
80 | 81 |
81 var WELCOME_TOAST_NOTIFICATION_ID = 'enable-now-toast'; | 82 var WELCOME_TOAST_NOTIFICATION_ID = 'enable-now-toast'; |
82 | 83 |
83 /** | 84 /** |
84 * The indices of the buttons that are displayed on the welcome toast. | 85 * The indices of the buttons that are displayed on the welcome toast. |
85 * @enum {number} | 86 * @enum {number} |
86 */ | 87 */ |
87 var ToastButtonIndex = {YES: 0, NO: 1}; | 88 var ToastButtonIndex = {YES: 0, NO: 1}; |
88 | 89 |
89 /** | 90 /** |
90 * The action that the user performed on the welcome toast. | |
91 * @enum {number} | |
92 */ | |
93 var ToastOptionResponse = {CHOSE_YES: 1, CHOSE_NO: 2}; | |
94 | |
95 /** | |
96 * Checks if a new task can't be scheduled when another task is already | 91 * Checks if a new task can't be scheduled when another task is already |
97 * scheduled. | 92 * scheduled. |
98 * @param {string} newTaskName Name of the new task. | 93 * @param {string} newTaskName Name of the new task. |
99 * @param {string} scheduledTaskName Name of the scheduled task. | 94 * @param {string} scheduledTaskName Name of the scheduled task. |
100 * @return {boolean} Whether the new task conflicts with the existing task. | 95 * @return {boolean} Whether the new task conflicts with the existing task. |
101 */ | 96 */ |
102 function areTasksConflicting(newTaskName, scheduledTaskName) { | 97 function areTasksConflicting(newTaskName, scheduledTaskName) { |
103 if (newTaskName == UPDATE_CARDS_TASK_NAME && | 98 if (newTaskName == UPDATE_CARDS_TASK_NAME && |
104 scheduledTaskName == UPDATE_CARDS_TASK_NAME) { | 99 scheduledTaskName == UPDATE_CARDS_TASK_NAME) { |
105 // If a card update is requested while an old update is still scheduled, we | 100 // If a card update is requested while an old update is still scheduled, we |
106 // don't need the new update. | 101 // don't need the new update. |
107 return true; | 102 return true; |
108 } | 103 } |
109 | 104 |
110 if (newTaskName == RETRY_DISMISS_TASK_NAME && | 105 if (newTaskName == RETRY_DISMISS_TASK_NAME && |
111 (scheduledTaskName == UPDATE_CARDS_TASK_NAME || | 106 (scheduledTaskName == UPDATE_CARDS_TASK_NAME || |
112 scheduledTaskName == DISMISS_CARD_TASK_NAME || | 107 scheduledTaskName == DISMISS_CARD_TASK_NAME || |
113 scheduledTaskName == RETRY_DISMISS_TASK_NAME)) { | 108 scheduledTaskName == RETRY_DISMISS_TASK_NAME)) { |
114 // No need to schedule retry-dismiss action if another action that tries to | 109 // No need to schedule retry-dismiss action if another action that tries to |
115 // send dismissals is scheduled. | 110 // send dismissals is scheduled. |
116 return true; | 111 return true; |
117 } | 112 } |
118 | 113 |
119 return false; | 114 return false; |
120 } | 115 } |
121 | 116 |
117 var googleGeolocationAccessEnabledPref = | |
118 chrome.preferencesPrivate.googleGeolocationAccessEnabled; | |
119 | |
122 var tasks = buildTaskManager(areTasksConflicting); | 120 var tasks = buildTaskManager(areTasksConflicting); |
123 | 121 |
124 // Add error processing to API calls. | 122 // Add error processing to API calls. |
125 tasks.instrumentApiFunction(chrome.identity, 'getAuthToken', 1); | 123 tasks.instrumentApiFunction(chrome.identity, 'getAuthToken', 1); |
126 tasks.instrumentApiFunction(chrome.identity, 'removeCachedAuthToken', 1); | 124 tasks.instrumentApiFunction(chrome.identity, 'removeCachedAuthToken', 1); |
127 tasks.instrumentApiFunction(chrome.location.onLocationUpdate, 'addListener', 0); | 125 tasks.instrumentApiFunction(chrome.location.onLocationUpdate, 'addListener', 0); |
128 tasks.instrumentApiFunction(chrome.notifications, 'create', 2); | 126 tasks.instrumentApiFunction(chrome.notifications, 'create', 2); |
129 tasks.instrumentApiFunction(chrome.notifications, 'update', 2); | 127 tasks.instrumentApiFunction(chrome.notifications, 'update', 2); |
130 tasks.instrumentApiFunction(chrome.notifications, 'getAll', 0); | 128 tasks.instrumentApiFunction(chrome.notifications, 'getAll', 0); |
131 tasks.instrumentApiFunction( | 129 tasks.instrumentApiFunction( |
132 chrome.notifications.onButtonClicked, 'addListener', 0); | 130 chrome.notifications.onButtonClicked, 'addListener', 0); |
133 tasks.instrumentApiFunction(chrome.notifications.onClicked, 'addListener', 0); | 131 tasks.instrumentApiFunction(chrome.notifications.onClicked, 'addListener', 0); |
134 tasks.instrumentApiFunction(chrome.notifications.onClosed, 'addListener', 0); | 132 tasks.instrumentApiFunction(chrome.notifications.onClosed, 'addListener', 0); |
133 tasks.instrumentApiFunction( | |
134 googleGeolocationAccessEnabledPref.onChange, | |
135 'addListener', | |
136 0); | |
135 tasks.instrumentApiFunction(chrome.runtime.onInstalled, 'addListener', 0); | 137 tasks.instrumentApiFunction(chrome.runtime.onInstalled, 'addListener', 0); |
136 tasks.instrumentApiFunction(chrome.runtime.onStartup, 'addListener', 0); | 138 tasks.instrumentApiFunction(chrome.runtime.onStartup, 'addListener', 0); |
137 tasks.instrumentApiFunction(chrome.tabs, 'create', 1); | 139 tasks.instrumentApiFunction(chrome.tabs, 'create', 1); |
138 tasks.instrumentApiFunction(storage, 'get', 1); | 140 tasks.instrumentApiFunction(storage, 'get', 1); |
139 | 141 |
140 var updateCardsAttempts = buildAttemptManager( | 142 var updateCardsAttempts = buildAttemptManager( |
141 'cards-update', | 143 'cards-update', |
142 requestLocation, | 144 requestLocation, |
143 INITIAL_POLLING_PERIOD_SECONDS, | 145 INITIAL_POLLING_PERIOD_SECONDS, |
144 MAXIMUM_POLLING_PERIOD_SECONDS); | 146 MAXIMUM_POLLING_PERIOD_SECONDS); |
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
385 storage.set({ | 387 storage.set({ |
386 notificationsData: newNotificationsData, | 388 notificationsData: newNotificationsData, |
387 recentDismissals: updatedRecentDismissals | 389 recentDismissals: updatedRecentDismissals |
388 }); | 390 }); |
389 callback(); | 391 callback(); |
390 }); | 392 }); |
391 }); | 393 }); |
392 } | 394 } |
393 | 395 |
394 /** | 396 /** |
397 * Removes all cards and card state on Google Now close down. | |
398 * For example, this occurs when the geolocation preference is unchecked in the | |
399 * content settings. | |
400 */ | |
401 function removeAllCards() { | |
402 console.log('removeAllCards'); | |
403 | |
404 // TODO(robliao): Once Google Now clears its own checkbox in the | |
405 // notifications center and bug 260376 is fixed, the below clearing | |
406 // code is no longer necessary. | |
407 chrome.notifications.getAll(function(notifications) { | |
408 for (var notificationId in notifications) { | |
409 chrome.notifications.clear(notificationId, function() {}); | |
410 } | |
411 storage.set({notificationsData: {}}); | |
412 }); | |
413 } | |
414 | |
415 /** | |
395 * Requests notification cards from the server. | 416 * Requests notification cards from the server. |
396 * @param {Location} position Location of this computer. | 417 * @param {Location} position Location of this computer. |
397 * @param {function()} callback Completion callback. | 418 * @param {function()} callback Completion callback. |
398 */ | 419 */ |
399 function requestNotificationCards(position, callback) { | 420 function requestNotificationCards(position, callback) { |
400 console.log('requestNotificationCards ' + JSON.stringify(position) + | 421 console.log('requestNotificationCards ' + JSON.stringify(position) + |
401 ' from ' + NOTIFICATION_CARDS_URL); | 422 ' from ' + NOTIFICATION_CARDS_URL); |
402 | 423 |
403 if (!NOTIFICATION_CARDS_URL) { | 424 if (!NOTIFICATION_CARDS_URL) { |
404 callback(); | 425 callback(); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
439 /** | 460 /** |
440 * Starts getting location for a cards update. | 461 * Starts getting location for a cards update. |
441 */ | 462 */ |
442 function requestLocation() { | 463 function requestLocation() { |
443 console.log('requestLocation'); | 464 console.log('requestLocation'); |
444 recordEvent(DiagnosticEvent.LOCATION_REQUEST); | 465 recordEvent(DiagnosticEvent.LOCATION_REQUEST); |
445 // TODO(vadimt): Figure out location request options. | 466 // TODO(vadimt): Figure out location request options. |
446 chrome.location.watchLocation(LOCATION_WATCH_NAME, {}); | 467 chrome.location.watchLocation(LOCATION_WATCH_NAME, {}); |
447 } | 468 } |
448 | 469 |
470 /** | |
471 * Stops getting the location. | |
472 */ | |
473 function stopRequestLocation() { | |
474 console.log('stopRequestLocation'); | |
475 chrome.location.clearWatch(LOCATION_WATCH_NAME); | |
476 } | |
449 | 477 |
450 /** | 478 /** |
451 * Obtains new location; requests and shows notification cards based on this | 479 * Obtains new location; requests and shows notification cards based on this |
452 * location. | 480 * location. |
453 * @param {Location} position Location of this computer. | 481 * @param {Location} position Location of this computer. |
454 */ | 482 */ |
455 function updateNotificationsCards(position) { | 483 function updateNotificationsCards(position) { |
456 console.log('updateNotificationsCards ' + JSON.stringify(position) + | 484 console.log('updateNotificationsCards ' + JSON.stringify(position) + |
457 ' @' + new Date()); | 485 ' @' + new Date()); |
458 tasks.add(UPDATE_CARDS_TASK_NAME, function(callback) { | 486 tasks.add(UPDATE_CARDS_TASK_NAME, function(callback) { |
459 console.log('updateNotificationsCards-task-begin'); | 487 console.log('updateNotificationsCards-task-begin'); |
460 updateCardsAttempts.planForNext(function() { | 488 updateCardsAttempts.isRunning(function(running) { |
461 processPendingDismissals(function(success) { | 489 if (running) { |
462 if (success) { | 490 updateCardsAttempts.planForNext(function() { |
463 // The cards are requested only if there are no unsent dismissals. | 491 processPendingDismissals(function(success) { |
464 requestNotificationCards(position, callback); | 492 if (success) { |
465 } else { | 493 // The cards are requested only if there are no unsent dismissals. |
466 callback(); | 494 requestNotificationCards(position, callback); |
467 } | 495 } else { |
468 }); | 496 callback(); |
497 } | |
498 }); | |
499 }); | |
500 } | |
469 }); | 501 }); |
470 }); | 502 }); |
471 } | 503 } |
472 | 504 |
473 /** | 505 /** |
474 * Sends a server request to dismiss a card. | 506 * Sends a server request to dismiss a card. |
475 * @param {string} notificationId Unique identifier of the card. | 507 * @param {string} notificationId Unique identifier of the card. |
476 * @param {number} dismissalTimeMs Time of the user's dismissal of the card in | 508 * @param {number} dismissalTimeMs Time of the user's dismissal of the card in |
477 * milliseconds since epoch. | 509 * milliseconds since epoch. |
478 * @param {Object} dismissalParameters Dismissal parameters. | 510 * @param {Object} dismissalParameters Dismissal parameters. |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
621 chrome.windows.create({url: url}); | 653 chrome.windows.create({url: url}); |
622 }); | 654 }); |
623 }); | 655 }); |
624 } | 656 } |
625 | 657 |
626 /** | 658 /** |
627 * Responds to a click of one of the buttons on the welcome toast. | 659 * Responds to a click of one of the buttons on the welcome toast. |
628 * @param {number} buttonIndex The index of the button which was clicked. | 660 * @param {number} buttonIndex The index of the button which was clicked. |
629 */ | 661 */ |
630 function onToastNotificationClicked(buttonIndex) { | 662 function onToastNotificationClicked(buttonIndex) { |
663 storage.set({userRespondedToToast: true}); | |
664 | |
631 if (buttonIndex == ToastButtonIndex.YES) { | 665 if (buttonIndex == ToastButtonIndex.YES) { |
632 chrome.metricsPrivate.recordUserAction('GoogleNow.WelcomeToastClickedYes'); | 666 chrome.metricsPrivate.recordUserAction('GoogleNow.WelcomeToastClickedYes'); |
633 storage.set({toastState: ToastOptionResponse.CHOSE_YES}); | 667 googleGeolocationAccessEnabledPref.set({value: true}); |
634 | 668 // The googlegeolocationaccessenabled preference change callback |
635 // TODO(zturner): Update chrome geolocation setting once the settings | 669 // will take care of starting the poll for cards. |
636 // API is in place. | |
637 startPollingCards(); | |
638 } else { | 670 } else { |
639 chrome.metricsPrivate.recordUserAction('GoogleNow.WelcomeToastClickedNo'); | 671 chrome.metricsPrivate.recordUserAction('GoogleNow.WelcomeToastClickedNo'); |
640 storage.set({toastState: ToastOptionResponse.CHOSE_NO}); | 672 onStateChange(); |
641 } | 673 } |
642 | |
643 chrome.notifications.clear( | |
644 WELCOME_TOAST_NOTIFICATION_ID, | |
645 function(wasCleared) {}); | |
646 } | 674 } |
647 | 675 |
648 /** | 676 /** |
649 * Callback for chrome.notifications.onClosed event. | 677 * Callback for chrome.notifications.onClosed event. |
650 * @param {string} notificationId Unique identifier of the notification. | 678 * @param {string} notificationId Unique identifier of the notification. |
651 * @param {boolean} byUser Whether the notification was closed by the user. | 679 * @param {boolean} byUser Whether the notification was closed by the user. |
652 */ | 680 */ |
653 function onNotificationClosed(notificationId, byUser) { | 681 function onNotificationClosed(notificationId, byUser) { |
654 if (!byUser) | 682 if (!byUser) |
655 return; | 683 return; |
656 | 684 |
657 if (notificationId == WELCOME_TOAST_NOTIFICATION_ID) { | 685 if (notificationId == WELCOME_TOAST_NOTIFICATION_ID) { |
658 // Even though they only closed the notification without clicking no, treat | 686 // Even though they only closed the notification without clicking no, treat |
659 // it as though they clicked No anwyay, and don't show the toast again. | 687 // it as though they clicked No anwyay, and don't show the toast again. |
660 chrome.metricsPrivate.recordUserAction('GoogleNow.WelcomeToastDismissed'); | 688 chrome.metricsPrivate.recordUserAction('GoogleNow.WelcomeToastDismissed'); |
661 storage.set({toastState: ToastOptionResponse.CHOSE_NO}); | 689 storage.set({userRespondedToToast: true}); |
662 return; | 690 return; |
663 } | 691 } |
664 | 692 |
665 // At this point we are guaranteed that the notification is a now card. | 693 // At this point we are guaranteed that the notification is a now card. |
666 chrome.metricsPrivate.recordUserAction('GoogleNow.Dismissed'); | 694 chrome.metricsPrivate.recordUserAction('GoogleNow.Dismissed'); |
667 | 695 |
668 tasks.add(DISMISS_CARD_TASK_NAME, function(callback) { | 696 tasks.add(DISMISS_CARD_TASK_NAME, function(callback) { |
669 dismissalAttempts.start(); | 697 dismissalAttempts.start(); |
670 | 698 |
671 // Deleting the notification in case it was re-added while this task was | 699 // Deleting the notification in case it was re-added while this task was |
(...skipping 27 matching lines...) Expand all Loading... | |
699 */ | 727 */ |
700 function startPollingCards() { | 728 function startPollingCards() { |
701 // Create an update timer for a case when for some reason location request | 729 // Create an update timer for a case when for some reason location request |
702 // gets stuck. | 730 // gets stuck. |
703 updateCardsAttempts.start(MAXIMUM_POLLING_PERIOD_SECONDS); | 731 updateCardsAttempts.start(MAXIMUM_POLLING_PERIOD_SECONDS); |
704 | 732 |
705 requestLocation(); | 733 requestLocation(); |
706 } | 734 } |
707 | 735 |
708 /** | 736 /** |
737 * Stops all machinery in the polling system. | |
738 */ | |
739 function stopPollingCards() { | |
740 stopRequestLocation(); | |
741 | |
742 updateCardsAttempts.stop(); | |
743 | |
744 removeAllCards(); | |
745 } | |
746 | |
747 /** | |
748 * Stops everything in Google Now, from polling to any displayed UI. | |
749 */ | |
750 function stopAndCloseDown() { | |
751 stopPollingCards(); | |
752 | |
753 hideWelcomeToast(); | |
vadimt
2013/07/24 00:59:38
hideWelcomeToast() should not be part of this.
It
robliao
2013/07/24 20:07:37
Done.
On 2013/07/24 00:59:38, vadimt wrote:
| |
754 } | |
755 | |
756 /** | |
709 * Initializes the event page on install or on browser startup. | 757 * Initializes the event page on install or on browser startup. |
710 */ | 758 */ |
711 function initialize() { | 759 function initialize() { |
712 recordEvent(DiagnosticEvent.EXTENSION_START); | 760 recordEvent(DiagnosticEvent.EXTENSION_START); |
713 storage.get('toastState', function(items) { | 761 onStateChange(); |
vadimt
2013/07/24 00:59:38
Suppose when we closed Chrome last time, we were i
robliao
2013/07/24 20:07:37
Stopping updateCardsAttempts is best here. It keep
| |
714 // The toast state might be undefined (e.g. not in storage yet) if this is | 762 } |
715 // the first time ever being prompted. | |
716 | 763 |
717 // TODO(zturner): Get the value of isGeolocationEnabled from the settings | 764 /** |
718 // api and additionally make sure it is true. | 765 * Starts or stops the extension machinery. |
719 if (!items.toastState) { | 766 * @param {boolean} shouldRunRequest true to start and |
720 if (NOTIFICATION_CARDS_URL) { | 767 * false to stop this extension. |
721 chrome.identity.getAuthToken({interactive: false}, function(token) { | 768 * @param {function} onSuccess Called on completion. |
722 if (!chrome.runtime.lastError && token) | 769 */ |
723 showWelcomeToast(); | 770 function setShouldRun(shouldRunRequest, onSuccess) { |
724 }); | 771 tasks.debugSetStepName( |
772 'setShouldRun-shouldRun-updateCardsAttemptsIsRunning'); | |
773 updateCardsAttempts.isRunning(function(currentValue) { | |
vadimt
2013/07/24 00:59:38
Please unindent.
robliao
2013/07/24 20:07:37
Done.
| |
774 if (shouldRunRequest != currentValue) { | |
775 if (shouldRunRequest) | |
776 startPollingCards(); | |
777 else | |
778 stopAndCloseDown(); | |
725 } | 779 } |
726 } else if (items.toastState == ToastOptionResponse.CHOSE_YES) { | 780 onSuccess(); |
727 startPollingCards(); | 781 }); |
782 } | |
783 | |
784 /** | |
785 * Shows or hides the toast. | |
786 * @param {boolean} visibleRequest true to show the toast and | |
787 * false to hide the toast. | |
788 * @param {function} onSuccess Called on completion. | |
789 */ | |
790 function setToastVisible(visibleRequest, onSuccess) { | |
791 tasks.debugSetStepName( | |
792 'setToastVisible-shouldSetToastVisible-getAllNotifications'); | |
793 chrome.notifications.getAll(function(notifications) { | |
794 // TODO(vadimt): Figure out what to do when notifications are disabled for | |
795 // our extension. | |
796 notifications = notifications || {}; | |
797 | |
798 var notificationFound = false; | |
799 for (var notificationId in notifications) { | |
800 if (notificationId == WELCOME_TOAST_NOTIFICATION_ID) { | |
801 notificationFound = true; | |
802 break; | |
803 } | |
728 } | 804 } |
805 | |
806 if ((notificationFound && !visibleRequest) || | |
807 (!notificationFound && visibleRequest)) | |
vadimt
2013/07/24 00:59:38
if (visibleRequest != !!notifications[WELCOME_TOAS
robliao
2013/07/24 20:07:37
Done.
| |
808 { | |
809 if (visibleRequest) | |
810 showWelcomeToast(); | |
811 else | |
812 hideWelcomeToast(); | |
813 } | |
814 | |
815 onSuccess(); | |
729 }); | 816 }); |
730 } | 817 } |
731 | 818 |
819 /** | |
820 * Does the actual work of deciding what Google Now should do | |
821 * based off of the current state of Chrome. | |
822 * @param {boolean} signedIn true if the user is signed in. | |
823 * @param {boolean} geolocationEnabled true if | |
824 * the geolocation option is enabled. | |
825 * @param {boolean} userRespondedToToast true if | |
826 * the user has responded to the toast. | |
827 * @param {function()} callback Call this function on completion. | |
828 */ | |
829 function updateRunningState( | |
830 signedIn, | |
831 geolocationEnabled, | |
832 userRespondedToToast, | |
833 callback) { | |
834 | |
835 var toastVisible = false; | |
vadimt
2013/07/24 00:59:38
toastShouldBeVisible?
Otherwise, it may seem that
robliao
2013/07/24 20:07:37
Used shouldSetToastVisible.
On 2013/07/24 00:59:38
| |
836 var shouldRun = false; | |
837 | |
838 if (signedIn) { | |
839 if (geolocationEnabled) { | |
840 if (!userRespondedToToast) { | |
841 // If the user enabled geolocation independently of Google Now, | |
842 // the user has implicitly responded to the toast. | |
843 // We do not want to show it again. | |
844 storage.set({userRespondedToToast: true}); | |
845 } | |
846 | |
847 shouldRun = true; | |
848 } else { | |
849 if (userRespondedToToast) { | |
850 shouldRun = false; | |
vadimt
2013/07/24 00:59:38
Already false.
robliao
2013/07/24 20:07:37
Kept here for state machine clarity.
On 2013/07/24
vadimt
2013/07/24 20:49:43
Then you should always set both variables in all b
robliao
2013/07/24 21:07:06
State machine implementations should generally be
| |
851 } else { | |
852 toastVisible = true; | |
853 } | |
854 } | |
855 } else { | |
856 shouldRun = false; | |
vadimt
2013/07/24 00:59:38
Already false.
robliao
2013/07/24 20:07:37
Kept here for state machine clarity.
Ideally, the
vadimt
2013/07/24 20:49:43
For me, assigning to false is stating "nothing is
robliao
2013/07/24 21:07:06
Done.
| |
857 } | |
858 | |
859 setToastVisible(toastVisible, function() { | |
vadimt
2013/07/24 00:59:38
With new simplified tasks, you don't have to invok
robliao
2013/07/24 20:07:37
Is the new simplified tasks checked in?
On 2013/07
vadimt
2013/07/24 20:49:43
No, but will be by the moment you'll be checking i
robliao
2013/07/24 21:07:06
Do you have a delta I can pull?
On 2013/07/24 20:4
| |
860 setShouldRun(shouldRun, callback); | |
861 }); | |
862 } | |
863 | |
864 /** | |
865 * Coordinates the behavior of Google Now for Chrome depending on | |
866 * Chrome and extension state. | |
867 */ | |
868 function onStateChange() { | |
869 tasks.add(STATE_CHANGED_TASK_NAME, function(callback) { | |
870 tasks.debugSetStepName('onStateChange-getAuthToken'); | |
871 chrome.identity.getAuthToken({interactive: false}, function(token) { | |
872 var signedIn = | |
873 !chrome.runtime.lastError && | |
874 token && | |
875 !!NOTIFICATION_CARDS_URL; | |
876 tasks.debugSetStepName( | |
877 'onStateChange-get-googleGeolocationAccessEnabledPref'); | |
878 googleGeolocationAccessEnabledPref.get({}, function(prefValue) { | |
879 var geolocationEnabled = !!prefValue.value; | |
880 tasks.debugSetStepName( | |
881 'onStateChange-get-userRespondedToToast'); | |
882 storage.get('userRespondedToToast', function(items) { | |
883 var userRespondedToToast = !!items.userRespondedToToast; | |
884 updateRunningState( | |
885 signedIn, | |
886 geolocationEnabled, | |
887 userRespondedToToast, | |
888 callback); | |
889 }); | |
890 }); | |
891 }); | |
892 }); | |
893 } | |
894 | |
732 /** | 895 /** |
733 * Displays a toast to the user asking if they want to opt in to receiving | 896 * Displays a toast to the user asking if they want to opt in to receiving |
734 * Google Now cards. | 897 * Google Now cards. |
735 */ | 898 */ |
736 function showWelcomeToast() { | 899 function showWelcomeToast() { |
737 recordEvent(DiagnosticEvent.SHOW_WELCOME_TOAST); | 900 recordEvent(DiagnosticEvent.SHOW_WELCOME_TOAST); |
738 // TODO(zturner): Localize this once the component extension localization | 901 // TODO(zturner): Localize this once the component extension localization |
739 // api is complete. | 902 // api is complete. |
740 // TODO(zturner): Add icons. | 903 // TODO(zturner): Add icons. |
741 var buttons = [{title: 'Yes'}, {title: 'No'}]; | 904 var buttons = [{title: 'Yes'}, {title: 'No'}]; |
742 var options = { | 905 var options = { |
743 type: 'basic', | 906 type: 'basic', |
744 title: 'Enable Google Now Cards', | 907 title: 'Enable Google Now Cards', |
745 message: 'Would you like to be shown Google Now cards?', | 908 message: 'Would you like to be shown Google Now cards?', |
746 iconUrl: 'http://www.gstatic.com/googlenow/chrome/default.png', | 909 iconUrl: 'http://www.gstatic.com/googlenow/chrome/default.png', |
747 priority: 2, | 910 priority: 2, |
748 buttons: buttons | 911 buttons: buttons |
749 }; | 912 }; |
750 chrome.notifications.create(WELCOME_TOAST_NOTIFICATION_ID, options, | 913 chrome.notifications.create(WELCOME_TOAST_NOTIFICATION_ID, options, |
751 function(notificationId) {}); | 914 function(notificationId) {}); |
752 } | 915 } |
753 | 916 |
917 /** | |
918 * Hides the welcome toast. | |
919 */ | |
920 function hideWelcomeToast() { | |
921 chrome.notifications.clear( | |
922 WELCOME_TOAST_NOTIFICATION_ID, | |
923 function() {}); | |
924 } | |
925 | |
754 chrome.runtime.onInstalled.addListener(function(details) { | 926 chrome.runtime.onInstalled.addListener(function(details) { |
755 console.log('onInstalled ' + JSON.stringify(details)); | 927 console.log('onInstalled ' + JSON.stringify(details)); |
756 if (details.reason != 'chrome_update') { | 928 if (details.reason != 'chrome_update') { |
757 initialize(); | 929 initialize(); |
758 } | 930 } |
759 }); | 931 }); |
760 | 932 |
761 chrome.runtime.onStartup.addListener(function() { | 933 chrome.runtime.onStartup.addListener(function() { |
762 console.log('onStartup'); | 934 console.log('onStartup'); |
763 initialize(); | 935 initialize(); |
764 }); | 936 }); |
765 | 937 |
938 googleGeolocationAccessEnabledPref.onChange.addListener(function(prefValue) { | |
939 console.log('googleGeolocationAccessEnabledPref onChange ' + prefValue.value); | |
940 onStateChange(); | |
941 }); | |
942 | |
766 chrome.notifications.onClicked.addListener( | 943 chrome.notifications.onClicked.addListener( |
767 function(notificationId) { | 944 function(notificationId) { |
768 chrome.metricsPrivate.recordUserAction('GoogleNow.MessageClicked'); | 945 chrome.metricsPrivate.recordUserAction('GoogleNow.MessageClicked'); |
769 onNotificationClicked(notificationId, function(actionUrls) { | 946 onNotificationClicked(notificationId, function(actionUrls) { |
770 return actionUrls.messageUrl; | 947 return actionUrls.messageUrl; |
771 }); | 948 }); |
772 }); | 949 }); |
773 | 950 |
774 chrome.notifications.onButtonClicked.addListener( | 951 chrome.notifications.onButtonClicked.addListener( |
775 function(notificationId, buttonIndex) { | 952 function(notificationId, buttonIndex) { |
(...skipping 15 matching lines...) Expand all Loading... | |
791 | 968 |
792 chrome.location.onLocationUpdate.addListener(function(position) { | 969 chrome.location.onLocationUpdate.addListener(function(position) { |
793 recordEvent(DiagnosticEvent.LOCATION_UPDATE); | 970 recordEvent(DiagnosticEvent.LOCATION_UPDATE); |
794 updateNotificationsCards(position); | 971 updateNotificationsCards(position); |
795 }); | 972 }); |
796 | 973 |
797 chrome.omnibox.onInputEntered.addListener(function(text) { | 974 chrome.omnibox.onInputEntered.addListener(function(text) { |
798 localStorage['server_url'] = NOTIFICATION_CARDS_URL = text; | 975 localStorage['server_url'] = NOTIFICATION_CARDS_URL = text; |
799 initialize(); | 976 initialize(); |
800 }); | 977 }); |
OLD | NEW |