Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(373)

Unified Diff: chrome/browser/resources/google_now/background.js

Issue 21985002: Add Finch Checks to the State Machine (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@SMLog
Patch Set: CR Feedback Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/resources/google_now/background.js
diff --git a/chrome/browser/resources/google_now/background.js b/chrome/browser/resources/google_now/background.js
index aa68162313ed48f53b2c64666c6a21d938af991c..53532216e68a3d2f627fca58c3aca16f134244f2 100644
--- a/chrome/browser/resources/google_now/background.js
+++ b/chrome/browser/resources/google_now/background.js
@@ -128,13 +128,16 @@ tasks.instrumentApiFunction(
chrome.notifications.onButtonClicked, 'addListener', 0);
tasks.instrumentApiFunction(chrome.notifications.onClicked, 'addListener', 0);
tasks.instrumentApiFunction(chrome.notifications.onClosed, 'addListener', 0);
+tasks.instrumentApiFunction(chrome.permissions, 'contains', 1);
+tasks.instrumentApiFunction(chrome.permissions, 'remove', 1);
+tasks.instrumentApiFunction(chrome.permissions, 'request', 1);
+tasks.instrumentApiFunction(chrome.runtime.onInstalled, 'addListener', 0);
+tasks.instrumentApiFunction(chrome.runtime.onStartup, 'addListener', 0);
+tasks.instrumentApiFunction(chrome.tabs, 'create', 1);
vadimt 2013/08/03 02:07:53 Please sync, build and run Chrome. Run you functio
tasks.instrumentApiFunction(
googleGeolocationAccessEnabledPref.onChange,
'addListener',
0);
-tasks.instrumentApiFunction(chrome.runtime.onInstalled, 'addListener', 0);
-tasks.instrumentApiFunction(chrome.runtime.onStartup, 'addListener', 0);
-tasks.instrumentApiFunction(chrome.tabs, 'create', 1);
tasks.instrumentApiFunction(storage, 'get', 1);
var updateCardsAttempts = buildAttemptManager(
@@ -709,6 +712,9 @@ function setShouldPollCards(shouldPollCardsRequest, onSuccess) {
startPollingCards();
else
stopPollingCards();
+ } else {
+ console.log(
+ 'Action Ignored setShouldPollCards=' + shouldPollCardsRequest);
}
onSuccess();
});
@@ -734,6 +740,8 @@ function setToastVisible(visibleRequest, onSuccess) {
showWelcomeToast();
else
hideWelcomeToast();
+ } else {
+ console.log('Action Ignored setToastVisible=' + visibleRequest);
}
onSuccess();
@@ -741,6 +749,36 @@ function setToastVisible(visibleRequest, onSuccess) {
}
/**
+ * Enables or disables the Google Now background permission.
+ * @param {boolean} backgroundEnable true to run in the background.
+ * false to not run in the background.
+ * @param {function} onSuccess Called on completion.
+ */
+function setBackgroundEnable(backgroundEnable, onSuccess) {
+ chrome.permissions.contains({permissions: ['background']},
+ function(hasPermission) {
+ if (backgroundEnable != hasPermission) {
+ console.log('Action Taken setBackgroundEnable=' + backgroundEnable);
+ if (backgroundEnable)
+ chrome.permissions.request(
+ {permissions: ['background']},
+ function() {
+ onSuccess();
+ });
+ else
+ chrome.permissions.remove(
+ {permissions: ['background']},
+ function() {
+ onSuccess();
+ });
+ } else {
+ console.log('Action Ignored setBackgroundEnable=' + backgroundEnable);
+ onSuccess();
+ }
+ });
+}
+
+/**
* Does the actual work of deciding what Google Now should do
* based off of the current state of Chrome.
* @param {boolean} signedIn true if the user is signed in.
@@ -748,23 +786,28 @@ function setToastVisible(visibleRequest, onSuccess) {
* the geolocation option is enabled.
* @param {boolean} userRespondedToToast true if
* the user has responded to the toast.
+ * @param {boolean} enableExperiment true if
+ * this extension should be running.
* @param {function()} callback Call this function on completion.
*/
function updateRunningState(
signedIn,
geolocationEnabled,
userRespondedToToast,
+ enableExperiment,
callback) {
console.log(
'State Update signedIn=' + signedIn + ' ' +
'geolocationEnabled=' + geolocationEnabled + ' ' +
- 'userRespondedToToast=' + userRespondedToToast);
+ 'userRespondedToToast=' + userRespondedToToast + ' ' +
+ 'enableExperiment=' + enableExperiment);
var shouldSetToastVisible = false;
var shouldPollCards = false;
+ var shouldSetBackground = false;
- if (signedIn) {
+ if (signedIn && enableExperiment) {
if (geolocationEnabled) {
if (!userRespondedToToast) {
// If the user enabled geolocation independently of Google Now,
@@ -774,6 +817,7 @@ function updateRunningState(
}
shouldPollCards = true;
+ shouldSetBackground = true;
} else {
if (userRespondedToToast) {
recordEvent(GoogleNowEvent.USER_SUPPRESSED);
@@ -786,11 +830,14 @@ function updateRunningState(
}
console.log(
- 'Requested Actions setToastVisible=' + shouldSetToastVisible + ' ' +
+ 'Requested Actions shouldSetBackground=' + shouldSetBackground + ' ' +
+ 'setToastVisible=' + shouldSetToastVisible + ' ' +
'setShouldPollCards=' + shouldPollCards);
- setToastVisible(shouldSetToastVisible, function() {
- setShouldPollCards(shouldPollCards, callback);
+ setBackgroundEnable(shouldSetBackground, function() {
+ setToastVisible(shouldSetToastVisible, function() {
+ setShouldPollCards(shouldPollCards, callback);
+ });
});
}
@@ -803,19 +850,27 @@ function onStateChange() {
tasks.debugSetStepName('onStateChange-isSignedIn');
authenticationManager.isSignedIn(function(token) {
var signedIn = !!token && !!NOTIFICATION_CARDS_URL;
- tasks.debugSetStepName(
- 'onStateChange-get-googleGeolocationAccessEnabledPref');
- googleGeolocationAccessEnabledPref.get({}, function(prefValue) {
- var geolocationEnabled = !!prefValue.value;
+ chrome.metricsPrivate.getFieldTrial('GoogleNow', function(response) {
+ // '' means we were enabled via the flags, but
+ // the experiment is not running.
+ var enableExperiment = (response == 'Enable' ||
vadimt 2013/08/03 02:07:53 You need to check for "EnableWithBackground", "Ena
+ (response == 'EnabledViaFlag') ||
+ (response == ''));
tasks.debugSetStepName(
- 'onStateChange-get-userRespondedToToast');
- storage.get('userRespondedToToast', function(items) {
- var userRespondedToToast = !!items.userRespondedToToast;
- updateRunningState(
- signedIn,
- geolocationEnabled,
- userRespondedToToast,
- callback);
+ 'onStateChange-get-googleGeolocationAccessEnabledPref');
+ googleGeolocationAccessEnabledPref.get({}, function(prefValue) {
+ var geolocationEnabled = !!prefValue.value;
+ tasks.debugSetStepName(
+ 'onStateChange-get-userRespondedToToast');
+ storage.get('userRespondedToToast', function(items) {
+ var userRespondedToToast = !!items.userRespondedToToast;
+ updateRunningState(
+ signedIn,
+ geolocationEnabled,
+ userRespondedToToast,
+ enableExperiment,
+ callback);
+ });
});
});
});

Powered by Google App Engine
This is Rietveld 408576698