1// Copyright 2014 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5(function() { 6 7 // Correspond to steps in the hotword opt-in flow. 8 /** @const */ var HOTWORD_AUDIO_HISTORY = 'hotword-audio-history-container'; 9 /** @const */ var HOTWORD_ONLY_START = 'hotword-only-container'; 10 /** @const */ var AUDIO_HISTORY_START = 'audio-history-container'; 11 /** @const */ var SPEECH_TRAINING = 'speech-training-container'; 12 /** @const */ var FINISHED = 'finished-container'; 13 14 /** 15 * These flows correspond to the three LaunchModes as defined in 16 * chrome/browser/search/hotword_service.h and should be kept in sync 17 * with them. 18 * @const 19 */ 20 var FLOWS = [ 21 // TODO(kcarattini): Remove the first flow, since we will not be 22 // managing the Audio History Setting in Chrome anymore. 23 [AUDIO_HISTORY_START], 24 [HOTWORD_ONLY_START, SPEECH_TRAINING, FINISHED], 25 [HOTWORD_AUDIO_HISTORY, SPEECH_TRAINING, FINISHED], 26 [SPEECH_TRAINING, FINISHED] 27 ]; 28 29 /** 30 * Class to control the page flow of the always-on hotword and 31 * Audio History opt-in process. 32 * @constructor 33 */ 34 function Flow() { 35 this.currentStepIndex_ = -1; 36 this.currentFlow_ = []; 37 } 38 39 /** 40 * Advances the current step. 41 */ 42 Flow.prototype.advanceStep = function() { 43 this.currentStepIndex_++; 44 if (this.currentStepIndex_ < this.currentFlow_.length) 45 this.showStep_.apply(this); 46 }; 47 48 /** 49 * Gets the appropriate flow and displays its first page. 50 */ 51 Flow.prototype.startFlow = function() { 52 if (chrome.hotwordPrivate && chrome.hotwordPrivate.getLaunchState) 53 chrome.hotwordPrivate.getLaunchState(this.startFlowForMode_.bind(this)); 54 }; 55 56 // ---- private methods: 57 58 /** 59 * Gets and starts the appropriate flow for the launch mode. 60 * @private 61 */ 62 Flow.prototype.startFlowForMode_ = function(state) { 63 assert(state.launchMode >= 0 && state.launchMode < FLOWS.length, 64 'Invalid Launch Mode.'); 65 this.currentFlow_ = FLOWS[state.launchMode]; 66 this.advanceStep(); 67 }; 68 69 /** 70 * Displays the current step. If the current step is not the first step, 71 * also hides the previous step. 72 * @private 73 */ 74 Flow.prototype.showStep_ = function() { 75 var currentStep = this.currentFlow_[this.currentStepIndex_]; 76 var previousStep = null; 77 if (this.currentStepIndex_ > 0) 78 previousStep = this.currentFlow_[this.currentStepIndex_ - 1]; 79 80 if (previousStep) 81 document.getElementById(previousStep).hidden = true; 82 83 document.getElementById(currentStep).hidden = false; 84 }; 85 86 window.Flow = Flow; 87})(); 88