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 /** | |
709 * Initializes the event page on install or on browser startup. | 748 * Initializes the event page on install or on browser startup. |
710 */ | 749 */ |
711 function initialize() { | 750 function initialize() { |
712 recordEvent(DiagnosticEvent.EXTENSION_START); | 751 recordEvent(DiagnosticEvent.EXTENSION_START); |
713 storage.get('toastState', function(items) { | |
714 // The toast state might be undefined (e.g. not in storage yet) if this is | |
715 // the first time ever being prompted. | |
716 | 752 |
717 // TODO(zturner): Get the value of isGeolocationEnabled from the settings | 753 // Alarms persist across chrome restarts. This is undesirable since it |
718 // api and additionally make sure it is true. | 754 // prevents us from starting up everything (alarms are a heuristic to |
719 if (!items.toastState) { | 755 // determine if we are already running). To mitigate this, we will |
720 if (NOTIFICATION_CARDS_URL) { | 756 // shut everything down on initialize before starting everything up. |
721 chrome.identity.getAuthToken({interactive: false}, function(token) { | 757 stopPollingCards(); |
vadimt
2013/07/25 20:48:01
I'm afraid that this scenario is possible:
1. When
robliao
2013/07/25 22:35:48
It is, but that bug is beyond the scope of this de
vadimt
2013/07/26 01:00:37
Please add a TODO item and plan addressing this so
robliao
2013/07/26 01:12:17
This would be better expressed as a bug rather tha
| |
722 if (!chrome.runtime.lastError && token) | 758 onStateChange(); |
723 showWelcomeToast(); | 759 } |
724 }); | 760 |
725 } | 761 /** |
726 } else if (items.toastState == ToastOptionResponse.CHOSE_YES) { | 762 * Starts or stops the polling of cards. |
727 startPollingCards(); | 763 * @param {boolean} shouldPollCardsRequest true to start and |
764 * false to stop polling cards. | |
765 * @param {function} onSuccess Called on completion. | |
766 */ | |
767 function setShouldPollCards(shouldPollCardsRequest, onSuccess) { | |
768 tasks.debugSetStepName( | |
769 'setShouldRun-shouldRun-updateCardsAttemptsIsRunning'); | |
770 updateCardsAttempts.isRunning(function(currentValue) { | |
771 if (shouldPollCardsRequest != currentValue) { | |
772 if (shouldPollCardsRequest) | |
773 startPollingCards(); | |
774 else | |
775 stopPollingCards(); | |
728 } | 776 } |
777 onSuccess(); | |
729 }); | 778 }); |
730 } | 779 } |
731 | 780 |
781 /** | |
782 * Shows or hides the toast. | |
783 * @param {boolean} visibleRequest true to show the toast and | |
784 * false to hide the toast. | |
785 * @param {function} onSuccess Called on completion. | |
786 */ | |
787 function setToastVisible(visibleRequest, onSuccess) { | |
788 tasks.debugSetStepName( | |
789 'setToastVisible-shouldSetToastVisible-getAllNotifications'); | |
790 chrome.notifications.getAll(function(notifications) { | |
791 // TODO(vadimt): Figure out what to do when notifications are disabled for | |
792 // our extension. | |
793 notifications = notifications || {}; | |
794 | |
795 if (visibleRequest != !!notifications[WELCOME_TOAST_NOTIFICATION_ID]) { | |
796 if (visibleRequest) | |
797 showWelcomeToast(); | |
798 else | |
799 hideWelcomeToast(); | |
800 } | |
801 | |
802 onSuccess(); | |
803 }); | |
804 } | |
805 | |
806 /** | |
807 * Does the actual work of deciding what Google Now should do | |
808 * based off of the current state of Chrome. | |
809 * @param {boolean} signedIn true if the user is signed in. | |
810 * @param {boolean} geolocationEnabled true if | |
811 * the geolocation option is enabled. | |
812 * @param {boolean} userRespondedToToast true if | |
813 * the user has responded to the toast. | |
814 * @param {function()} callback Call this function on completion. | |
815 */ | |
816 function updateRunningState( | |
817 signedIn, | |
818 geolocationEnabled, | |
819 userRespondedToToast, | |
820 callback) { | |
821 | |
822 var shouldSetToastVisible = false; | |
823 var shouldPollCards = false; | |
824 | |
825 if (signedIn) { | |
826 if (geolocationEnabled) { | |
827 if (!userRespondedToToast) { | |
828 // If the user enabled geolocation independently of Google Now, | |
829 // the user has implicitly responded to the toast. | |
830 // We do not want to show it again. | |
831 storage.set({userRespondedToToast: true}); | |
832 } | |
833 | |
834 shouldPollCards = true; | |
835 } else { | |
836 if (!userRespondedToToast) | |
837 shouldSetToastVisible = true; | |
838 } | |
839 } | |
840 | |
841 setToastVisible(shouldSetToastVisible, function() { | |
842 setShouldPollCards(shouldPollCards, callback); | |
843 }); | |
844 } | |
845 | |
846 /** | |
847 * Coordinates the behavior of Google Now for Chrome depending on | |
848 * Chrome and extension state. | |
849 */ | |
850 function onStateChange() { | |
851 tasks.add(STATE_CHANGED_TASK_NAME, function(callback) { | |
852 tasks.debugSetStepName('onStateChange-getAuthToken'); | |
853 chrome.identity.getAuthToken({interactive: false}, function(token) { | |
854 var signedIn = | |
855 !chrome.runtime.lastError && | |
856 token && | |
857 !!NOTIFICATION_CARDS_URL; | |
858 tasks.debugSetStepName( | |
859 'onStateChange-get-googleGeolocationAccessEnabledPref'); | |
860 googleGeolocationAccessEnabledPref.get({}, function(prefValue) { | |
861 var geolocationEnabled = !!prefValue.value; | |
862 tasks.debugSetStepName( | |
863 'onStateChange-get-userRespondedToToast'); | |
864 storage.get('userRespondedToToast', function(items) { | |
865 var userRespondedToToast = !!items.userRespondedToToast; | |
866 updateRunningState( | |
867 signedIn, | |
868 geolocationEnabled, | |
869 userRespondedToToast, | |
870 callback); | |
871 }); | |
872 }); | |
873 }); | |
874 }); | |
875 } | |
876 | |
732 /** | 877 /** |
733 * Displays a toast to the user asking if they want to opt in to receiving | 878 * Displays a toast to the user asking if they want to opt in to receiving |
734 * Google Now cards. | 879 * Google Now cards. |
735 */ | 880 */ |
736 function showWelcomeToast() { | 881 function showWelcomeToast() { |
737 recordEvent(DiagnosticEvent.SHOW_WELCOME_TOAST); | 882 recordEvent(DiagnosticEvent.SHOW_WELCOME_TOAST); |
738 // TODO(zturner): Localize this once the component extension localization | 883 // TODO(zturner): Localize this once the component extension localization |
739 // api is complete. | 884 // api is complete. |
740 // TODO(zturner): Add icons. | 885 // TODO(zturner): Add icons. |
741 var buttons = [{title: 'Yes'}, {title: 'No'}]; | 886 var buttons = [{title: 'Yes'}, {title: 'No'}]; |
742 var options = { | 887 var options = { |
743 type: 'basic', | 888 type: 'basic', |
744 title: 'Enable Google Now Cards', | 889 title: 'Enable Google Now Cards', |
745 message: 'Would you like to be shown Google Now cards?', | 890 message: 'Would you like to be shown Google Now cards?', |
746 iconUrl: 'http://www.gstatic.com/googlenow/chrome/default.png', | 891 iconUrl: 'http://www.gstatic.com/googlenow/chrome/default.png', |
747 priority: 2, | 892 priority: 2, |
748 buttons: buttons | 893 buttons: buttons |
749 }; | 894 }; |
750 chrome.notifications.create(WELCOME_TOAST_NOTIFICATION_ID, options, | 895 chrome.notifications.create(WELCOME_TOAST_NOTIFICATION_ID, options, |
751 function(notificationId) {}); | 896 function(notificationId) {}); |
752 } | 897 } |
753 | 898 |
899 /** | |
900 * Hides the welcome toast. | |
901 */ | |
902 function hideWelcomeToast() { | |
903 chrome.notifications.clear( | |
904 WELCOME_TOAST_NOTIFICATION_ID, | |
905 function() {}); | |
906 } | |
907 | |
754 chrome.runtime.onInstalled.addListener(function(details) { | 908 chrome.runtime.onInstalled.addListener(function(details) { |
755 console.log('onInstalled ' + JSON.stringify(details)); | 909 console.log('onInstalled ' + JSON.stringify(details)); |
756 if (details.reason != 'chrome_update') { | 910 if (details.reason != 'chrome_update') { |
757 initialize(); | 911 initialize(); |
758 } | 912 } |
759 }); | 913 }); |
760 | 914 |
761 chrome.runtime.onStartup.addListener(function() { | 915 chrome.runtime.onStartup.addListener(function() { |
762 console.log('onStartup'); | 916 console.log('onStartup'); |
763 initialize(); | 917 initialize(); |
764 }); | 918 }); |
765 | 919 |
920 googleGeolocationAccessEnabledPref.onChange.addListener(function(prefValue) { | |
921 console.log('googleGeolocationAccessEnabledPref onChange ' + prefValue.value); | |
922 onStateChange(); | |
923 }); | |
924 | |
766 chrome.notifications.onClicked.addListener( | 925 chrome.notifications.onClicked.addListener( |
767 function(notificationId) { | 926 function(notificationId) { |
768 chrome.metricsPrivate.recordUserAction('GoogleNow.MessageClicked'); | 927 chrome.metricsPrivate.recordUserAction('GoogleNow.MessageClicked'); |
769 onNotificationClicked(notificationId, function(actionUrls) { | 928 onNotificationClicked(notificationId, function(actionUrls) { |
770 return actionUrls.messageUrl; | 929 return actionUrls.messageUrl; |
771 }); | 930 }); |
772 }); | 931 }); |
773 | 932 |
774 chrome.notifications.onButtonClicked.addListener( | 933 chrome.notifications.onButtonClicked.addListener( |
775 function(notificationId, buttonIndex) { | 934 function(notificationId, buttonIndex) { |
(...skipping 15 matching lines...) Expand all Loading... | |
791 | 950 |
792 chrome.location.onLocationUpdate.addListener(function(position) { | 951 chrome.location.onLocationUpdate.addListener(function(position) { |
793 recordEvent(DiagnosticEvent.LOCATION_UPDATE); | 952 recordEvent(DiagnosticEvent.LOCATION_UPDATE); |
794 updateNotificationsCards(position); | 953 updateNotificationsCards(position); |
795 }); | 954 }); |
796 | 955 |
797 chrome.omnibox.onInputEntered.addListener(function(text) { | 956 chrome.omnibox.onInputEntered.addListener(function(text) { |
798 localStorage['server_url'] = NOTIFICATION_CARDS_URL = text; | 957 localStorage['server_url'] = NOTIFICATION_CARDS_URL = text; |
799 initialize(); | 958 initialize(); |
800 }); | 959 }); |
OLD | NEW |