| 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 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 chrome.hotwordPrivate.setAudioLoggingEnabled) { | 117 chrome.hotwordPrivate.setAudioLoggingEnabled) { |
| 118 chrome.hotwordPrivate.setAudioLoggingEnabled(false); | 118 chrome.hotwordPrivate.setAudioLoggingEnabled(false); |
| 119 } | 119 } |
| 120 break; | 120 break; |
| 121 default: | 121 default: |
| 122 break; | 122 break; |
| 123 } | 123 } |
| 124 return false; | 124 return false; |
| 125 }; | 125 }; |
| 126 | 126 |
| 127 /** |
| 128 * Helper function to test URLs as being valid for running the |
| 129 * hotwording extension. It's used by isEligibleUrl to make that |
| 130 * function clearer. |
| 131 * @param {string} url URL to check. |
| 132 * @param {string} base Base URL to compare against.. |
| 133 * @return {boolean} True if url is an eligible hotword URL. |
| 134 */ |
| 135 OptInManager.prototype.checkEligibleUrl = function(url, base) { |
| 136 if (!url) |
| 137 return false; |
| 138 |
| 139 if (url === base || |
| 140 url === base + '/' || |
| 141 url.indexOf(base + '/_/chrome/newtab?') === 0 || // Appcache NTP. |
| 142 url.indexOf(base + '/?') === 0 || |
| 143 url.indexOf(base + '/#') === 0 || |
| 144 url.indexOf(base + '/webhp') === 0 || |
| 145 url.indexOf(base + '/search') === 0) { |
| 146 return true; |
| 147 } |
| 148 return false; |
| 149 |
| 150 }; |
| 127 | 151 |
| 128 /** | 152 /** |
| 129 * Determines if a URL is eligible for hotwording. For now, the | 153 * Determines if a URL is eligible for hotwording. For now, the |
| 130 * valid pages are the Google HP and SERP (this will include the NTP). | 154 * valid pages are the Google HP and SERP (this will include the NTP). |
| 131 * @param {string} url Url to check. | 155 * @param {string} url URL to check. |
| 132 * @return {boolean} True if url is eligible hotword url. | 156 * @return {boolean} True if url is an eligible hotword URL. |
| 133 */ | 157 */ |
| 134 OptInManager.prototype.isEligibleUrl = function(url) { | 158 OptInManager.prototype.isEligibleUrl = function(url) { |
| 135 if (!url) | 159 if (!url) |
| 136 return false; | 160 return false; |
| 137 | 161 |
| 162 // More URLs will be added in the future so leaving this as an array. |
| 138 var baseUrls = [ | 163 var baseUrls = [ |
| 139 'https://www.google.com', | 164 'chrome://newtab' |
| 140 'chrome://newtab', | 165 ]; |
| 141 'https://encrypted.google.com' | 166 var baseGoogleUrls = [ |
| 167 'https://www.google.', |
| 168 'https://encrypted.google.' |
| 169 ]; |
| 170 var tlds = [ |
| 171 'com', |
| 172 'co.uk', |
| 173 'de', |
| 174 'fr', |
| 175 'ru' |
| 142 ]; | 176 ]; |
| 143 | 177 |
| 144 for (var i = 0; i < baseUrls.length; i++) { | 178 // Check URLs which do not have locale-based TLDs first. |
| 145 var base = baseUrls[i]; | 179 if (this.checkEligibleUrl(url, baseUrls[0])) |
| 146 if (url === base + '/' || | 180 return true; |
| 147 url.indexOf(base + '/_/chrome/newtab?') === 0 || // Appcache NTP. | 181 |
| 148 url.indexOf(base + '/?') === 0 || | 182 // Check URLs with each type of local-based TLD. |
| 149 url.indexOf(base + '/#') === 0 || | 183 for (var i = 0; i < baseGoogleUrls.length; i++) { |
| 150 url.indexOf(base + '/webhp') === 0 || | 184 for (var j = 0; j < tlds.length; j++) { |
| 151 url.indexOf(base + '/search') === 0) { | 185 var base = baseGoogleUrls[i] + tlds[j]; |
| 152 return true; | 186 if (this.checkEligibleUrl(url, base)) |
| 187 return true; |
| 153 } | 188 } |
| 154 } | 189 } |
| 155 return false; | 190 return false; |
| 156 }; | 191 }; |
| 157 | 192 |
| 158 | 193 |
| 159 /** | 194 /** |
| 160 * Initializes the extension. | 195 * Initializes the extension. |
| 161 */ | 196 */ |
| 162 OptInManager.prototype.initialize = function() { | 197 OptInManager.prototype.initialize = function() { |
| 163 // TODO(rlp): Possibly remove the next line. It's proably not used, but | 198 // TODO(rlp): Possibly remove the next line. It's proably not used, but |
| 164 // leaving for now to be safe. We should remove it once all messsage | 199 // leaving for now to be safe. We should remove it once all messsage |
| 165 // relaying is removed form the content scripts. | 200 // relaying is removed form the content scripts. |
| 166 chrome.runtime.onMessage.addListener(this.handleMessage_.bind(this)); | 201 chrome.runtime.onMessage.addListener(this.handleMessage_.bind(this)); |
| 167 chrome.runtime.onMessageExternal.addListener( | 202 chrome.runtime.onMessageExternal.addListener( |
| 168 this.handleMessage_.bind(this)); | 203 this.handleMessage_.bind(this)); |
| 169 }; | 204 }; |
| 170 | 205 |
| 171 | 206 |
| 172 new OptInManager().initialize(); | 207 new OptInManager().initialize(); |
| OLD | NEW |