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