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 |
(...skipping 17 matching lines...) Expand all Loading... |
28 this.portMap_ = {}; | 28 this.portMap_ = {}; |
29 | 29 |
30 /** | 30 /** |
31 * 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 |
32 * hotwording is disabled. | 32 * hotwording is disabled. |
33 */ | 33 */ |
34 this.connectListener_ = this.handleConnect_.bind(this); | 34 this.connectListener_ = this.handleConnect_.bind(this); |
35 this.tabCreatedListener_ = this.handleCreatedTab_.bind(this); | 35 this.tabCreatedListener_ = this.handleCreatedTab_.bind(this); |
36 this.tabUpdatedListener_ = this.handleUpdatedTab_.bind(this); | 36 this.tabUpdatedListener_ = this.handleUpdatedTab_.bind(this); |
37 this.tabActivatedListener_ = this.handleActivatedTab_.bind(this); | 37 this.tabActivatedListener_ = this.handleActivatedTab_.bind(this); |
| 38 this.microphoneStateChangedListener_ = |
| 39 this.handleMicrophoneStateChanged_.bind(this); |
38 this.windowFocusChangedListener_ = this.handleChangedWindow_.bind(this); | 40 this.windowFocusChangedListener_ = this.handleChangedWindow_.bind(this); |
39 this.messageListener_ = this.handleMessageFromPage_.bind(this); | 41 this.messageListener_ = this.handleMessageFromPage_.bind(this); |
40 | 42 |
41 // Need to setup listeners on startup, otherwise events that caused the | 43 // Need to setup listeners on startup, otherwise events that caused the |
42 // event page to start up, will be lost. | 44 // event page to start up, will be lost. |
43 this.setupListeners_(); | 45 this.setupListeners_(); |
44 | 46 |
45 this.stateManager_.onStatusChanged.addListener(function() { | 47 this.stateManager_.onStatusChanged.addListener(function() { |
46 this.updateListeners_(); | 48 this.updateListeners_(); |
47 this.updateTabState_(); | 49 this.updateTabState_(); |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 | 232 |
231 /** | 233 /** |
232 * Handles a tab that has just become active. | 234 * Handles a tab that has just become active. |
233 * @param {{tabId: number}} info Information about the activated tab. | 235 * @param {{tabId: number}} info Information about the activated tab. |
234 * @private | 236 * @private |
235 */ | 237 */ |
236 handleActivatedTab_: function(info) { | 238 handleActivatedTab_: function(info) { |
237 this.updateTabState_(); | 239 this.updateTabState_(); |
238 }, | 240 }, |
239 | 241 |
| 242 /** |
| 243 * Handles the microphone state changing. |
| 244 * @param {boolean} enabled Whether the microphone is now enabled. |
| 245 * @private |
| 246 */ |
| 247 handleMicrophoneStateChanged_: function(enabled) { |
| 248 if (enabled) { |
| 249 this.updateTabState_(); |
| 250 return; |
| 251 } |
| 252 |
| 253 this.stopHotwording_(); |
| 254 }, |
240 | 255 |
241 /** | 256 /** |
242 * Handles a change in Chrome windows. | 257 * Handles a change in Chrome windows. |
243 * Note: this does not always trigger in Linux. | 258 * Note: this does not always trigger in Linux. |
244 * @param {number} windowId Id of newly focused window. | 259 * @param {number} windowId Id of newly focused window. |
245 * @private | 260 * @private |
246 */ | 261 */ |
247 handleChangedWindow_: function(windowId) { | 262 handleChangedWindow_: function(windowId) { |
248 this.updateTabState_(); | 263 this.updateTabState_(); |
249 }, | 264 }, |
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
485 setupListeners_: function() { | 500 setupListeners_: function() { |
486 if (chrome.runtime.onConnect.hasListener(this.connectListener_)) | 501 if (chrome.runtime.onConnect.hasListener(this.connectListener_)) |
487 return; | 502 return; |
488 | 503 |
489 chrome.runtime.onConnect.addListener(this.connectListener_); | 504 chrome.runtime.onConnect.addListener(this.connectListener_); |
490 chrome.tabs.onCreated.addListener(this.tabCreatedListener_); | 505 chrome.tabs.onCreated.addListener(this.tabCreatedListener_); |
491 chrome.tabs.onUpdated.addListener(this.tabUpdatedListener_); | 506 chrome.tabs.onUpdated.addListener(this.tabUpdatedListener_); |
492 chrome.tabs.onActivated.addListener(this.tabActivatedListener_); | 507 chrome.tabs.onActivated.addListener(this.tabActivatedListener_); |
493 chrome.windows.onFocusChanged.addListener( | 508 chrome.windows.onFocusChanged.addListener( |
494 this.windowFocusChangedListener_); | 509 this.windowFocusChangedListener_); |
| 510 chrome.hotwordPrivate.onMicrophoneStateChanged.addListener( |
| 511 this.microphoneStateChangedListener_); |
495 if (chrome.runtime.onMessage.hasListener(this.messageListener_)) | 512 if (chrome.runtime.onMessage.hasListener(this.messageListener_)) |
496 return; | 513 return; |
497 chrome.runtime.onMessageExternal.addListener( | 514 chrome.runtime.onMessageExternal.addListener( |
498 this.messageListener_); | 515 this.messageListener_); |
499 }, | 516 }, |
500 | 517 |
501 /** | 518 /** |
502 * Remove event listeners. | 519 * Remove event listeners. |
503 * @private | 520 * @private |
504 */ | 521 */ |
505 removeListeners_: function() { | 522 removeListeners_: function() { |
506 chrome.runtime.onConnect.removeListener(this.connectListener_); | 523 chrome.runtime.onConnect.removeListener(this.connectListener_); |
507 chrome.tabs.onCreated.removeListener(this.tabCreatedListener_); | 524 chrome.tabs.onCreated.removeListener(this.tabCreatedListener_); |
508 chrome.tabs.onUpdated.removeListener(this.tabUpdatedListener_); | 525 chrome.tabs.onUpdated.removeListener(this.tabUpdatedListener_); |
509 chrome.tabs.onActivated.removeListener(this.tabActivatedListener_); | 526 chrome.tabs.onActivated.removeListener(this.tabActivatedListener_); |
510 chrome.windows.onFocusChanged.removeListener( | 527 chrome.windows.onFocusChanged.removeListener( |
511 this.windowFocusChangedListener_); | 528 this.windowFocusChangedListener_); |
| 529 chrome.hotwordPrivate.onMicrophoneStateChanged.removeListener( |
| 530 this.microphoneStateChangedListener_); |
512 // Don't remove the Message listener, as we want them listening all | 531 // Don't remove the Message listener, as we want them listening all |
513 // the time, | 532 // the time, |
514 }, | 533 }, |
515 | 534 |
516 /** | 535 /** |
517 * Update event listeners based on the current hotwording state. | 536 * Update event listeners based on the current hotwording state. |
518 * @private | 537 * @private |
519 */ | 538 */ |
520 updateListeners_: function() { | 539 updateListeners_: function() { |
521 if (this.stateManager_.isSometimesOnEnabled()) { | 540 if (this.stateManager_.isSometimesOnEnabled()) { |
522 this.setupListeners_(); | 541 this.setupListeners_(); |
523 } else { | 542 } else { |
524 this.removeListeners_(); | 543 this.removeListeners_(); |
525 this.stopHotwording_(); | 544 this.stopHotwording_(); |
526 this.disconnectAllClients_(); | 545 this.disconnectAllClients_(); |
527 } | 546 } |
528 } | 547 } |
529 }; | 548 }; |
530 | 549 |
531 return { | 550 return { |
532 PageAudioManager: PageAudioManager | 551 PageAudioManager: PageAudioManager |
533 }; | 552 }; |
534 }); | 553 }); |
OLD | NEW |