OLD | NEW |
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 Loading... |
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 if (!hotwordStatus.enabledSet) |
| 73 response = hotwordStatus; |
| 74 else if (hotwordStatus.enabled) |
| 75 chrome.tabs.executeScript(tab.id, {'file': 'audio_client.js'}); |
64 } | 76 } |
65 | 77 |
66 if (!hotwordStatus.enabledSet) { | 78 try { |
67 sendResponse(hotwordStatus); | 79 sendResponse(response); |
68 return; | 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; |
69 } | 89 } |
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, |
| 113 request.tab || sender.tab || {incognito: true}, |
| 114 sendResponse)); |
96 return true; | 115 return true; |
97 } | 116 } |
98 break; | 117 break; |
99 case OptInManager.CommandFromPage.CLICKED_OPTIN: | 118 case OptInManager.CommandFromPage.CLICKED_OPTIN: |
100 if (chrome.hotwordPrivate && chrome.hotwordPrivate.setEnabled && | 119 if (chrome.hotwordPrivate && chrome.hotwordPrivate.setEnabled && |
101 chrome.hotwordPrivate.getStatus) { | 120 chrome.hotwordPrivate.getStatus) { |
102 chrome.hotwordPrivate.setEnabled(true); | 121 chrome.hotwordPrivate.setEnabled(true); |
103 chrome.hotwordPrivate.getStatus( | 122 chrome.hotwordPrivate.getStatus( |
104 this.injectTab_.bind(this, sender.tab, sendResponse)); | 123 this.injectTab_.bind(this, sender.tab, sendResponse)); |
105 return true; | 124 return true; |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 // TODO(rlp): Possibly remove the next line. It's proably not used, but | 223 // 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 | 224 // leaving for now to be safe. We should remove it once all messsage |
206 // relaying is removed form the content scripts. | 225 // relaying is removed form the content scripts. |
207 chrome.runtime.onMessage.addListener(this.handleMessage_.bind(this)); | 226 chrome.runtime.onMessage.addListener(this.handleMessage_.bind(this)); |
208 chrome.runtime.onMessageExternal.addListener( | 227 chrome.runtime.onMessageExternal.addListener( |
209 this.handleMessage_.bind(this)); | 228 this.handleMessage_.bind(this)); |
210 }; | 229 }; |
211 | 230 |
212 | 231 |
213 new OptInManager().initialize(); | 232 new OptInManager().initialize(); |
OLD | NEW |