Chromium Code Reviews| Index: chrome/browser/resources/hotword/page_audio_manager.js |
| diff --git a/chrome/browser/resources/hotword/page_audio_manager.js b/chrome/browser/resources/hotword/page_audio_manager.js |
| index 43b763b8fd226e9ea03a97ff0d53d452b6442051..23be1fbb1518a69dcf119de6f8bbb58bb32b1fb6 100644 |
| --- a/chrome/browser/resources/hotword/page_audio_manager.js |
| +++ b/chrome/browser/resources/hotword/page_audio_manager.js |
| @@ -36,6 +36,7 @@ cr.define('hotword', function() { |
| this.tabUpdatedListener_ = this.handleUpdatedTab_.bind(this); |
| this.tabActivatedListener_ = this.handleActivatedTab_.bind(this); |
| this.windowFocusChangedListener_ = this.handleChangedWindow_.bind(this); |
| + this.messageListener_ = this.handleMessageFromPage_.bind(this); |
| // Need to setup listeners on startup, otherwise events that caused the |
| // event page to start up, will be lost. |
| @@ -43,6 +44,7 @@ cr.define('hotword', function() { |
| this.stateManager_.onStatusChanged.addListener(function() { |
| this.updateListeners_(); |
| + this.updateTabState_(); |
| }.bind(this)); |
| }; |
| @@ -108,6 +110,8 @@ cr.define('hotword', function() { |
| // Check for the new tab page first. |
| if (this.checkUrlPathIsEligible_(url, 'chrome://newtab')) |
| return true; |
| + if (this.checkUrlPathIsEligible_(url, 'http://kcarattini-z620.syd.corp.google.com:8888/')) |
|
Anand Mistry (off Chromium)
2015/03/10 02:47:29
Remove.
kcarattini
2015/03/10 04:34:05
Done.
|
| + return true; |
| // Check URLs with each type of local-based TLD. |
| for (var i = 0; i < baseGoogleUrls.length; i++) { |
| @@ -227,7 +231,7 @@ cr.define('hotword', function() { |
| }, |
| /** |
| - * Handles a tab that was just became active. |
| + * Handles a tab that has just become active. |
| * @param {{tabId: number}} info Information about the activated tab. |
| * @private |
| */ |
| @@ -348,7 +352,7 @@ cr.define('hotword', function() { |
| /** |
| * Starts hotwording if the currently active tab is eligible for hotwording |
| - * (i.e. google.com). |
| + * (e.g. google.com). |
| * @private |
| */ |
| startHotwordingIfEligible_: function() { |
| @@ -408,6 +412,80 @@ cr.define('hotword', function() { |
| } |
| }, |
| + |
| + /** |
| + * Handles a message directly from the NTP/HP/SERP. |
| + * @param {!Object} request Message from the sender. |
| + * @param {!MessageSender} sender Information about the sender. |
| + * @param {!function(HotwordStatus)} sendResponse Callback to respond |
| + * to sender. |
| + * @return {boolean} Whether to maintain the port open to call sendResponse. |
| + * @private |
| + */ |
| + handleMessageFromPage_: function(request, sender, sendResponse) { |
| + switch (request.type) { |
| + case CommandFromPage.PAGE_WAKEUP: |
| + if ((sender.tab && this.isEligibleUrl_(sender.tab.url)) && |
| + chrome.hotwordPrivate && chrome.hotwordPrivate.getStatus) { |
|
Anand Mistry (off Chromium)
2015/03/10 02:47:29
Remove these checks. We already assume that chrome
kcarattini
2015/03/10 04:34:05
Where do we make that assumption? I don't see it u
Anand Mistry (off Chromium)
2015/03/10 04:40:54
Look at other files in this directory, such as sta
kcarattini
2015/03/10 04:50:58
Done.
|
| + chrome.hotwordPrivate.getStatus( |
| + this.statusDone_.bind( |
| + this, |
| + request.tab || sender.tab || {incognito: true}, |
| + sendResponse)); |
| + return true; |
| + } |
| + break; |
| + case CommandFromPage.CLICKED_OPTIN: |
| + if (chrome.hotwordPrivate && chrome.hotwordPrivate.setEnabled && |
| + chrome.hotwordPrivate.getStatus) { |
| + chrome.hotwordPrivate.setEnabled(true); |
| + // chrome.hotwordPrivate.getStatus( |
|
Anand Mistry (off Chromium)
2015/03/10 02:47:29
Remove.
kcarattini
2015/03/10 04:34:05
Done.
|
| + // this.injectTab_.bind(this, sender.tab, sendResponse)); |
| + // return true; |
| + } |
| + break; |
| + // User has explicitly clicked 'no thanks'. |
| + case CommandFromPage.CLICKED_NO_OPTIN: |
| + if (chrome.hotwordPrivate && chrome.hotwordPrivate.setEnabled) { |
| + chrome.hotwordPrivate.setEnabled(false); |
| + } |
| + break; |
| + } |
| + return false; |
| + }, |
| + |
| + /** |
| + * Sends the response to the tab. |
| + * @param {Tab} tab The tab that the request was sent from. |
| + * @param {function(HotwordStatus)} sendResponse Callback function to |
| + * respond to sender. |
| + * @param {HotwordStatus} hotwordStatus Status of the hotword extension. |
| + * @private |
| + */ |
| + statusDone_: function(tab, sendResponse, hotwordStatus) { |
| + var response = {'doNotShowOptinMessage': true}; |
| + |
| + if (!tab.incognito && hotwordStatus.available && |
| + !hotwordStatus.enabledSet) { |
| + response = hotwordStatus; |
| + } |
| + |
| + try { |
| + sendResponse(response); |
| + } catch (err) { |
| + // Suppress the exception thrown by sendResponse() when the page doesn't |
| + // specify a response callback in the call to |
| + // chrome.runtime.sendMessage(). |
| + // Unfortunately, there doesn't appear to be a way to detect one-way |
| + // messages without explicitly saying in the message itself. This |
| + // message is defined as a constant in |
| + // extensions/renderer/messaging_bindings.cc |
| + if (err.message == 'Attempting to use a disconnected port object') |
| + return; |
| + throw err; |
| + } |
| + }, |
| + |
| /** |
| * Set up event listeners. |
| * @private |
| @@ -422,6 +500,14 @@ cr.define('hotword', function() { |
| chrome.tabs.onActivated.addListener(this.tabActivatedListener_); |
| chrome.windows.onFocusChanged.addListener( |
| this.windowFocusChangedListener_); |
| + // TODO(kcarattini): Possibly remove the next line. It's probably not |
| + // used, but leaving for now to be safe. We should remove it once all |
| + // messsage relaying is removed from the content scripts. |
| + if (chrome.runtime.onMessage.hasListener(this.messageListener_)) |
| + return; |
| + chrome.runtime.onMessage.addListener(this.messageListener_); |
|
Anand Mistry (off Chromium)
2015/03/10 02:47:29
Isn't this one unnecessary? If so, it should be re
kcarattini
2015/03/10 04:34:05
Done.
|
| + chrome.runtime.onMessageExternal.addListener( |
| + this.messageListener_); |
| }, |
| /** |
| @@ -435,6 +521,8 @@ cr.define('hotword', function() { |
| chrome.tabs.onActivated.removeListener(this.tabActivatedListener_); |
| chrome.windows.onFocusChanged.removeListener( |
| this.windowFocusChangedListener_); |
| + // Don't remove the Message listeners, as we want them listening all |
| + // the time, |
| }, |
| /** |