| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 cr.define('hotword', function() { | 5 cr.define('hotword', function() { |
| 6 'use strict'; | 6 'use strict'; |
| 7 | 7 |
| 8 /** | 8 /** |
| 9 * Class used to manage the interaction between hotwording and the | 9 * Class used to manage the interaction between hotwording and the |
| 10 * NTP/google.com. Injects a content script to interact with NTP/google.com | 10 * NTP/google.com. Injects a content script to interact with NTP/google.com |
| 11 * and updates the global hotwording state based on interaction with those | 11 * and updates the global hotwording state based on interaction with those |
| 12 * pages. | 12 * pages. |
| 13 * @param {!hotword.StateManager} stateManager | 13 * @param {!hotword.StateManager} stateManager |
| 14 * @constructor | 14 * @constructor |
| 15 * @struct | 15 * @class |
| 16 */ | 16 */ |
| 17 function PageAudioManager(stateManager) { | 17 function PageAudioManager(stateManager) { |
| 18 /** | 18 /** |
| 19 * Manager of global hotwording state. | 19 * Manager of global hotwording state. |
| 20 * @private {!hotword.StateManager} | 20 * @private {!hotword.StateManager} |
| 21 */ | 21 */ |
| 22 this.stateManager_ = stateManager; | 22 this.stateManager_ = stateManager; |
| 23 | 23 |
| 24 /** | 24 /** |
| 25 * Mapping between tab ID and port that is connected from the injected | 25 * Mapping between tab ID and port that is connected from the injected |
| 26 * content script. | 26 * content script. |
| 27 * @private {!Object.<number, chrome.runtime.Port>} | 27 * @private {!Object.<number, Port>} |
| 28 */ | 28 */ |
| 29 this.portMap_ = {}; | 29 this.portMap_ = {}; |
| 30 | 30 |
| 31 /** | 31 /** |
| 32 * Chrome event listeners. Saved so that they can be de-registered when | 32 * Chrome event listeners. Saved so that they can be de-registered when |
| 33 * hotwording is disabled. | 33 * hotwording is disabled. |
| 34 */ | 34 */ |
| 35 this.connectListener_ = this.handleConnect_.bind(this); | 35 this.connectListener_ = this.handleConnect_.bind(this); |
| 36 this.tabCreatedListener_ = this.handleCreatedTab_.bind(this); | 36 this.tabCreatedListener_ = this.handleCreatedTab_.bind(this); |
| 37 this.tabUpdatedListener_ = this.handleUpdatedTab_.bind(this); | 37 this.tabUpdatedListener_ = this.handleUpdatedTab_.bind(this); |
| (...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 301 * active tab. | 301 * active tab. |
| 302 * @private | 302 * @private |
| 303 */ | 303 */ |
| 304 hotwordTriggered_: function() { | 304 hotwordTriggered_: function() { |
| 305 this.findCurrentTab_(function(tab) { | 305 this.findCurrentTab_(function(tab) { |
| 306 if (tab) | 306 if (tab) |
| 307 this.sendClient_(CommandToPage.HOTWORD_VOICE_TRIGGER, tab.id); | 307 this.sendClient_(CommandToPage.HOTWORD_VOICE_TRIGGER, tab.id); |
| 308 }); | 308 }); |
| 309 }, | 309 }, |
| 310 | 310 |
| 311 /* | 311 /** |
| 312 * Starts hotwording. | 312 * Starts hotwording. |
| 313 * @private | 313 * @private |
| 314 */ | 314 */ |
| 315 startHotwording_: function() { | 315 startHotwording_: function() { |
| 316 this.stateManager_.startSession( | 316 this.stateManager_.startSession( |
| 317 hotword.constants.SessionSource.NTP, | 317 hotword.constants.SessionSource.NTP, |
| 318 function() { | 318 function() { |
| 319 this.sendAllClients_(CommandToPage.HOTWORD_STARTED); | 319 this.sendAllClients_(CommandToPage.HOTWORD_STARTED); |
| 320 }.bind(this), | 320 }.bind(this), |
| 321 this.hotwordTriggered_.bind(this)); | 321 this.hotwordTriggered_.bind(this)); |
| 322 }, | 322 }, |
| 323 | 323 |
| 324 /* | 324 /** |
| 325 * Starts hotwording if the currently active tab is eligible for hotwording | 325 * Starts hotwording if the currently active tab is eligible for hotwording |
| 326 * (i.e. google.com). | 326 * (i.e. google.com). |
| 327 * @private | 327 * @private |
| 328 */ | 328 */ |
| 329 startHotwordingIfEligible_: function() { | 329 startHotwordingIfEligible_: function() { |
| 330 this.findCurrentTab_(function(tab) { | 330 this.findCurrentTab_(function(tab) { |
| 331 if (!tab) { | 331 if (!tab) { |
| 332 this.stopHotwording_(); | 332 this.stopHotwording_(); |
| 333 return; | 333 return; |
| 334 } | 334 } |
| 335 if (this.isEligibleUrl_(tab.url)) | 335 if (this.isEligibleUrl_(tab.url)) |
| 336 this.startHotwording_(); | 336 this.startHotwording_(); |
| 337 }); | 337 }); |
| 338 }, | 338 }, |
| 339 | 339 |
| 340 /* | 340 /** |
| 341 * Stops hotwording. | 341 * Stops hotwording. |
| 342 * @private | 342 * @private |
| 343 */ | 343 */ |
| 344 stopHotwording_: function() { | 344 stopHotwording_: function() { |
| 345 this.stateManager_.stopSession(hotword.constants.SessionSource.NTP); | 345 this.stateManager_.stopSession(hotword.constants.SessionSource.NTP); |
| 346 this.sendAllClients_(CommandToPage.HOTWORD_ENDED); | 346 this.sendAllClients_(CommandToPage.HOTWORD_ENDED); |
| 347 }, | 347 }, |
| 348 | 348 |
| 349 /* | 349 /** |
| 350 * Stops hotwording if the currently active tab is not eligible for | 350 * Stops hotwording if the currently active tab is not eligible for |
| 351 * hotwording (i.e. google.com). | 351 * hotwording (i.e. google.com). |
| 352 * @private | 352 * @private |
| 353 */ | 353 */ |
| 354 stopHotwordingIfIneligibleTab_: function() { | 354 stopHotwordingIfIneligibleTab_: function() { |
| 355 this.findCurrentTab_(function(tab) { | 355 this.findCurrentTab_(function(tab) { |
| 356 if (!tab) { | 356 if (!tab) { |
| 357 this.stopHotwording_(); | 357 this.stopHotwording_(); |
| 358 return; | 358 return; |
| 359 } | 359 } |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 426 this.stopHotwording_(); | 426 this.stopHotwording_(); |
| 427 this.disconnectAllClients_(); | 427 this.disconnectAllClients_(); |
| 428 } | 428 } |
| 429 } | 429 } |
| 430 }; | 430 }; |
| 431 | 431 |
| 432 return { | 432 return { |
| 433 PageAudioManager: PageAudioManager | 433 PageAudioManager: PageAudioManager |
| 434 }; | 434 }; |
| 435 }); | 435 }); |
| OLD | NEW |