Skip direct map from apk check for Samsung Mega.
[chromium-blink-merge.git] / ui / accessibility / extensions / caretbrowsing / background.js
blob18c722a2fc64fb00ae4b146fc05ca43096f123cf
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
3 // found in the LICENSE file.
5 /**
6 * @fileoverview Script that runs on the background page.
7 */
9 CONTENT_SCRIPTS = [
10 'accessibility_utils.js',
11 'traverse_util.js',
12 'caret_browsing.js'
15 /**
16 * The class handling the Caret Browsing background page, which keeps
17 * track of the current state, handles the browser action button, and
18 * initializes the content script in all running tabs when the extension
19 * is first loaded.
20 * @constructor
22 var CaretBkgnd = function() {};
24 /**
25 * Flag indicating whether caret browsing is enabled. Global, applies to
26 * all tabs simultaneously.
27 * @type {boolean}
29 CaretBkgnd.isEnabled;
31 /**
32 * Change the browser action icon and tooltip based on the enabled state.
34 CaretBkgnd.setIcon = function() {
35 chrome.browserAction.setIcon(
36 {'path': CaretBkgnd.isEnabled ?
37 '../caret_19_on.png' :
38 '../caret_19.png'});
39 chrome.browserAction.setTitle(
40 {'title': CaretBkgnd.isEnabled ?
41 'Turn Off Caret Browsing (F7)' :
42 'Turn On Caret Browsing (F7)' });
45 /**
46 * This is called when the extension is first loaded, so that it can be
47 * immediately used in all already-open tabs. It's not needed for any
48 * new tabs that open after that, the content script will be automatically
49 * injected into any new tab.
51 CaretBkgnd.injectContentScripts = function() {
52 chrome.windows.getAll({'populate': true}, function(windows) {
53 for (var i = 0; i < windows.length; i++) {
54 var tabs = windows[i].tabs;
55 for (var j = 0; j < tabs.length; j++) {
56 for (var k = 0; k < CONTENT_SCRIPTS.length; k++) {
57 chrome.tabs.executeScript(
58 tabs[j].id,
59 {file: CONTENT_SCRIPTS[k], allFrames: true},
60 function(result) {
61 // Ignore.
62 chrome.runtime.lastError;
63 });
67 });
70 /**
71 * Toggle caret browsing on or off, and update the browser action icon and
72 * all open tabs.
74 CaretBkgnd.toggle = function() {
75 CaretBkgnd.isEnabled = !CaretBkgnd.isEnabled;
76 var obj = {};
77 obj['enabled'] = CaretBkgnd.isEnabled;
78 chrome.storage.sync.set(obj);
79 CaretBkgnd.setIcon();
82 /**
83 * Initialize the background script. Set the initial value of the flag
84 * based on the saved preference in localStorage, update the browser action,
85 * inject into running tabs, and then set up communication with content
86 * scripts in tabs. Also check for prefs updates (from the options page)
87 * and send them to content scripts.
89 CaretBkgnd.init = function() {
90 chrome.storage.sync.get('enabled', function(result) {
91 CaretBkgnd.isEnabled = result['enabled'];
92 CaretBkgnd.setIcon();
93 CaretBkgnd.injectContentScripts();
95 chrome.browserAction.onClicked.addListener(function(tab) {
96 CaretBkgnd.toggle();
97 });
98 });
100 chrome.storage.onChanged.addListener(function() {
101 chrome.storage.sync.get('enabled', function(result) {
102 CaretBkgnd.isEnabled = result['enabled'];
103 CaretBkgnd.setIcon();
108 CaretBkgnd.init();