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

Unified Diff: chrome/browser/resources/chromeos/chromevox/chromevox/background/background.js

Issue 599193002: Only inject ChromeVox classic when necessary. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@command_prev_next
Patch Set: Address comment. Created 6 years, 3 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | chrome/browser/resources/chromeos/chromevox/cvox2/background/background.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/resources/chromeos/chromevox/chromevox/background/background.js
diff --git a/chrome/browser/resources/chromeos/chromevox/chromevox/background/background.js b/chrome/browser/resources/chromeos/chromevox/chromevox/background/background.js
index c6730e45324b7defac1d0aab02cc2f9393fc98bb..f3b0d24390c680da0acdec1ee05e133f51b7ba2c 100644
--- a/chrome/browser/resources/chromeos/chromevox/chromevox/background/background.js
+++ b/chrome/browser/resources/chromeos/chromevox/chromevox/background/background.js
@@ -104,34 +104,6 @@ cvox.ChromeVoxBackground.prototype.init = function() {
this.onLoadStateChanged);
}
- var listOfFiles;
-
- // These lists of files must match the content_scripts section in
- // the manifest files.
- if (COMPILED) {
- listOfFiles = ['chromeVoxChromePageScript.js'];
- } else {
- listOfFiles = [
- 'closure/closure_preinit.js',
- 'closure/base.js',
- 'deps.js',
- 'chromevox/injected/loader.js'];
- }
-
- var self = this;
- var stageTwo = function(code) {
- // Inject the content script into all running tabs.
- chrome.windows.getAll({'populate': true}, function(windows) {
- for (var i = 0; i < windows.length; i++) {
- var tabs = windows[i].tabs;
- for (var j = 0; j < tabs.length; j++) {
- var tab = tabs[j];
- self.injectChromeVoxIntoTab(tab, listOfFiles, code);
- }
- }
- });
- };
-
this.checkVersionNumber();
// Set up a message passing system for goog.provide() calls from
@@ -149,10 +121,22 @@ cvox.ChromeVoxBackground.prototype.init = function() {
return true;
});
- // We use fetchCode instead of chrome.extensions.executeFile because
- // executeFile doesn't propagate the file name to the content script
- // which means that script is not visible in Dev Tools.
- cvox.InjectedScriptLoader.fetchCode(listOfFiles, stageTwo);
+ var self = this;
+ if (chrome.commandLinePrivate) {
+ chrome.commandLinePrivate.hasSwitch('enable-chromevox-next',
+ goog.bind(function(result) {
+ if (result) {
+ return;
+ }
+ // Inject the content script into all running tabs.
+ chrome.windows.getAll({'populate': true}, function(windows) {
+ for (var i = 0; i < windows.length; i++) {
+ var tabs = windows[i].tabs;
+ self.injectChromeVoxIntoTabs(tabs);
+ }
+ });
+ }, this));
+ }
if (localStorage['active'] == 'false') {
// Warn the user when the browser first starts if ChromeVox is inactive.
@@ -168,56 +152,79 @@ cvox.ChromeVoxBackground.prototype.init = function() {
/**
* Inject ChromeVox into a tab.
- * @param {Tab} tab The tab where ChromeVox scripts should be injected.
- * @param {Array.<string>} files The files to load.
- * @param {Object.<string, string>} code The contents of the files.
+ * @param {Array.<Tab>} tabs The tab where ChromeVox scripts should be injected.
+ * @param {boolean=} opt_forceCompiled forces compiled ChromeVox to be injected;
+ * defaults to Closure's compiled flag.
*/
-cvox.ChromeVoxBackground.prototype.injectChromeVoxIntoTab =
- function(tab, files, code) {
- window.console.log('Injecting into ' + tab.id, tab);
- var sawError = false;
+cvox.ChromeVoxBackground.prototype.injectChromeVoxIntoTabs =
+ function(tabs, opt_forceCompiled) {
+ var listOfFiles;
- /**
- * A helper function which executes code.
- * @param {string} code The code to execute.
- */
- var executeScript = goog.bind(function(code) {
- chrome.tabs.executeScript(
- tab.id,
- {'code': code,
- 'allFrames': true},
- goog.bind(function() {
- if (!chrome.extension.lastError) {
- return;
- }
- if (sawError) {
- return;
- }
- sawError = true;
- console.error('Could not inject into tab', tab);
- this.tts.speak('Error starting ChromeVox for ' +
- tab.title + ', ' + tab.url, 1);
- }, this));
- }, this);
-
- // There is a scenario where two copies of the content script can get
- // loaded into the same tab on browser startup - one automatically
- // and one because the background page injects the content script into
- // every tab on startup. To work around potential bugs resulting from this,
- // ChromeVox exports a global function called disableChromeVox() that can
- // be used here to disable any existing running instance before we inject
- // a new instance of the content script into this tab.
- //
- // It's harmless if there wasn't a copy of ChromeVox already running.
- //
- // Also, set some variables so that Closure deps work correctly and so
- // that ChromeVox knows not to announce feedback as if a page just loaded.
- executeScript('try { window.disableChromeVox(); } catch(e) { }\n' +
- 'window.INJECTED_AFTER_LOAD = true;\n' +
- 'window.CLOSURE_NO_DEPS = true\n');
-
- // Now inject the ChromeVox content script code into the tab.
- files.forEach(function(file) { executeScript(code[file]); });
+ // These lists of files must match the content_scripts section in
+ // the manifest files.
+ if (COMPILED || opt_forceCompiled) {
+ listOfFiles = ['chromeVoxChromePageScript.js'];
+ } else {
+ listOfFiles = [
+ 'closure/closure_preinit.js',
+ 'closure/base.js',
+ 'deps.js',
+ 'chromevox/injected/loader.js'];
+ }
+
+ var stageTwo = function(code) {
+ for (var i = 0, tab; tab = tabs[i]; i++) {
+ window.console.log('Injecting into ' + tab.id, tab);
+ var sawError = false;
+
+ /**
+ * A helper function which executes code.
+ * @param {string} code The code to execute.
+ */
+ var executeScript = goog.bind(function(code) {
+ chrome.tabs.executeScript(
+ tab.id,
+ {'code': code,
+ 'allFrames': true},
+ goog.bind(function() {
+ if (!chrome.extension.lastError) {
+ return;
+ }
+ if (sawError) {
+ return;
+ }
+ sawError = true;
+ console.error('Could not inject into tab', tab);
+ this.tts.speak('Error starting ChromeVox for ' +
+ tab.title + ', ' + tab.url, 1);
+ }, this));
+ }, this);
+
+ // There is a scenario where two copies of the content script can get
+ // loaded into the same tab on browser startup - one automatically and one
+ // because the background page injects the content script into every tab
+ // on startup. To work around potential bugs resulting from this,
+ // ChromeVox exports a global function called disableChromeVox() that can
+ // be used here to disable any existing running instance before we inject
+ // a new instance of the content script into this tab.
+ //
+ // It's harmless if there wasn't a copy of ChromeVox already running.
+ //
+ // Also, set some variables so that Closure deps work correctly and so
+ // that ChromeVox knows not to announce feedback as if a page just loaded.
+ executeScript('try { window.disableChromeVox(); } catch(e) { }\n' +
+ 'window.INJECTED_AFTER_LOAD = true;\n' +
+ 'window.CLOSURE_NO_DEPS = true\n');
+
+ // Now inject the ChromeVox content script code into the tab.
+ listOfFiles.forEach(function(file) { executeScript(code[file]); });
+ }
+ };
+
+ // We use fetchCode instead of chrome.extensions.executeFile because
+ // executeFile doesn't propagate the file name to the content script
+ // which means that script is not visible in Dev Tools.
+ cvox.InjectedScriptLoader.fetchCode(listOfFiles, stageTwo);
};
@@ -543,4 +550,7 @@ cvox.ChromeVoxBackground.prototype.onLoadStateChanged = function(
// Export the braille object for access by the options page.
window['braille'] = cvox.ChromeVox.braille;
+
+ // Export this background page for ChromeVox Next to access.
+ cvox.ChromeVox.background = background;
})();
« no previous file with comments | « no previous file | chrome/browser/resources/chromeos/chromevox/cvox2/background/background.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698