Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1945)

Side by Side Diff: chrome/browser/resources/hotword_helper/manager.js

Issue 587153002: Suppress the exception thrown when responding to message with no response callback. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add test. Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. 1 // Copyright (c) 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 'use strict'; 5 'use strict';
6 6
7 /** 7 /**
8 * @fileoverview This extension manages communications between Chrome, 8 * @fileoverview This extension manages communications between Chrome,
9 * Google.com pages and the Chrome Hotword extension. 9 * Google.com pages and the Chrome Hotword extension.
10 * 10 *
(...skipping 14 matching lines...) Expand all
25 25
26 26
27 /** 27 /**
28 * @const {string} 28 * @const {string}
29 * @private 29 * @private
30 */ 30 */
31 OptInManager.HOTWORD_EXTENSION_ID_ = 'bepbmhgboaologfdajaanbcjmnhjmhfn'; 31 OptInManager.HOTWORD_EXTENSION_ID_ = 'bepbmhgboaologfdajaanbcjmnhjmhfn';
32 32
33 33
34 /** 34 /**
35 * Test extension ID.
36 * @const {string}
37 * @private
38 */
39 OptInManager.TEST_EXTENSION_ID_ = 'cpfhkdbjfdgdebcjlifoldbijinjfifp';
40
41
42 /**
35 * Commands sent from the page to this content script. 43 * Commands sent from the page to this content script.
36 * @enum {string} 44 * @enum {string}
37 */ 45 */
38 OptInManager.CommandFromPage = { 46 OptInManager.CommandFromPage = {
39 // User has explicitly clicked 'no'. 47 // User has explicitly clicked 'no'.
40 CLICKED_NO_OPTIN: 'hcno', 48 CLICKED_NO_OPTIN: 'hcno',
41 // User has opted in. 49 // User has opted in.
42 CLICKED_OPTIN: 'hco', 50 CLICKED_OPTIN: 'hco',
43 // Audio logging is opted in. 51 // Audio logging is opted in.
44 AUDIO_LOGGING_ON: 'alon', 52 AUDIO_LOGGING_ON: 'alon',
45 // Audio logging is opted out. 53 // Audio logging is opted out.
46 AUDIO_LOGGING_OFF: 'aloff', 54 AUDIO_LOGGING_OFF: 'aloff',
47 // User visited an eligible page. 55 // User visited an eligible page.
48 PAGE_WAKEUP: 'wu' 56 PAGE_WAKEUP: 'wu'
49 }; 57 };
50 58
51 59
52 /** 60 /**
53 * @param {Tab} tab Tab to inject. 61 * @param {Tab} tab Tab to inject.
54 * @param {function(HotwordStatus)} sendResponse Callback function to respond 62 * @param {function(HotwordStatus)} sendResponse Callback function to respond
55 * to sender. 63 * to sender.
56 * @param {HotwordStatus} hotwordStatus Status of the hotword extension. 64 * @param {HotwordStatus} hotwordStatus Status of the hotword extension.
57 * @private 65 * @private
58 */ 66 */
59 OptInManager.prototype.injectTab_ = function( 67 OptInManager.prototype.injectTab_ = function(
60 tab, sendResponse, hotwordStatus) { 68 tab, sendResponse, hotwordStatus) {
61 if (tab.incognito || !hotwordStatus.available) { 69 var response = {'doNotShowOptinMessage': true};
62 sendResponse({'doNotShowOptinMessage': true}); 70
63 return; 71 if (tab.incognito || !hotwordStatus.available)
72 response = {'doNotShowOptinMessage': true};
Dan Beam 2014/09/24 01:37:35 how's this different from what |response| already
Anand Mistry (off Chromium) 2014/09/24 03:44:12 Oops. Just translated that part blindly.
73 else if (!hotwordStatus.enabledSet)
74 response = hotwordStatus;
75 else if (hotwordStatus.enabled)
76 chrome.tabs.executeScript(tab.id, {'file': 'audio_client.js'});
77
78 try {
79 sendResponse(response);
80 } catch (err) {
81 // Suppress the exception thrown by sendResponse() when the page doesn't
82 // specify a response callback in the call to chrome.runtime.sendMessage().
83 // Unfortunately, there doesn't appear to be a way to detect one-way
84 // messages without explicitly saying in the message itself. This message
85 // is defined as a constant in extensions/renderer/messaging_bindings.cc
86 if (err.message == 'Attempting to use a disconnected port object')
87 return;
88 throw err;
64 } 89 }
65
66 if (!hotwordStatus.enabledSet) {
67 sendResponse(hotwordStatus);
68 return;
69 }
70
71 if (hotwordStatus.enabled)
72 chrome.tabs.executeScript(tab.id, {'file': 'audio_client.js'});
73 sendResponse({'doNotShowOptinMessage': true});
74 }; 90 };
75 91
76 92
77 /** 93 /**
78 * Handles messages from the helper content script. 94 * Handles messages from the helper content script.
79 * @param {*} request Message from the sender. 95 * @param {*} request Message from the sender.
80 * @param {MessageSender} sender Information about the sender. 96 * @param {MessageSender} sender Information about the sender.
81 * @param {function(HotwordStatus)} sendResponse Callback function to respond 97 * @param {function(HotwordStatus)} sendResponse Callback function to respond
82 * to sender. 98 * to sender.
83 * @return {boolean} Whether to maintain the port open to call sendResponse. 99 * @return {boolean} Whether to maintain the port open to call sendResponse.
84 * @private 100 * @private
85 */ 101 */
86 OptInManager.prototype.handleMessage_ = function( 102 OptInManager.prototype.handleMessage_ = function(
87 request, sender, sendResponse) { 103 request, sender, sendResponse) {
88 switch (request.type) { 104 switch (request.type) {
89 case OptInManager.CommandFromPage.PAGE_WAKEUP: 105 case OptInManager.CommandFromPage.PAGE_WAKEUP:
90 if (((sender.tab && this.isEligibleUrl(sender.tab.url)) || 106 if (((sender.tab && this.isEligibleUrl(sender.tab.url)) ||
91 sender.id == OptInManager.HOTWORD_EXTENSION_ID_) && 107 sender.id == OptInManager.HOTWORD_EXTENSION_ID_ ||
108 sender.id == OptInManager.TEST_EXTENSION_ID_) &&
92 chrome.hotwordPrivate && chrome.hotwordPrivate.getStatus) { 109 chrome.hotwordPrivate && chrome.hotwordPrivate.getStatus) {
93 chrome.hotwordPrivate.getStatus( 110 chrome.hotwordPrivate.getStatus(
94 this.injectTab_.bind(this, request.tab || sender.tab, 111 this.injectTab_.bind(
95 sendResponse)); 112 this, request.tab || sender.tab || {incognito: true},
113 sendResponse));
Dan Beam 2014/09/24 01:37:35 nit: chrome.hotwordPrivate.getStatus(this.injec
Anand Mistry (off Chromium) 2014/09/24 03:44:12 Done.
96 return true; 114 return true;
97 } 115 }
98 break; 116 break;
99 case OptInManager.CommandFromPage.CLICKED_OPTIN: 117 case OptInManager.CommandFromPage.CLICKED_OPTIN:
100 if (chrome.hotwordPrivate && chrome.hotwordPrivate.setEnabled && 118 if (chrome.hotwordPrivate && chrome.hotwordPrivate.setEnabled &&
101 chrome.hotwordPrivate.getStatus) { 119 chrome.hotwordPrivate.getStatus) {
102 chrome.hotwordPrivate.setEnabled(true); 120 chrome.hotwordPrivate.setEnabled(true);
103 chrome.hotwordPrivate.getStatus( 121 chrome.hotwordPrivate.getStatus(
104 this.injectTab_.bind(this, sender.tab, sendResponse)); 122 this.injectTab_.bind(this, sender.tab, sendResponse));
105 return true; 123 return true;
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
204 // TODO(rlp): Possibly remove the next line. It's proably not used, but 222 // TODO(rlp): Possibly remove the next line. It's proably not used, but
205 // leaving for now to be safe. We should remove it once all messsage 223 // leaving for now to be safe. We should remove it once all messsage
206 // relaying is removed form the content scripts. 224 // relaying is removed form the content scripts.
207 chrome.runtime.onMessage.addListener(this.handleMessage_.bind(this)); 225 chrome.runtime.onMessage.addListener(this.handleMessage_.bind(this));
208 chrome.runtime.onMessageExternal.addListener( 226 chrome.runtime.onMessageExternal.addListener(
209 this.handleMessage_.bind(this)); 227 this.handleMessage_.bind(this));
210 }; 228 };
211 229
212 230
213 new OptInManager().initialize(); 231 new OptInManager().initialize();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698