Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / hotword_helper / manager.js
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.
4
5 'use strict';
6
7 /**
8  * @fileoverview This extension manages communications between Chrome,
9  * Google.com pages and the Chrome Hotword extension.
10  *
11  * This helper extension is required due to the depoyment plan for Chrome M34:
12  *
13  *  - The hotword extension will be distributed as an externally loaded
14  *      component extension.
15  *  - Settings for enabling and disabling the hotword extension has moved to
16  *      Chrome settings.
17  *  - Newtab page is served via chrome://newtab/
18  *
19  */
20
21
22
23 /** @constructor */
24 var OptInManager = function() {};
25
26
27 /**
28  * @const {string}
29  * @private
30  */
31 OptInManager.HOTWORD_EXTENSION_ID_ = 'bepbmhgboaologfdajaanbcjmnhjmhfn';
32
33
34 /**
35  * Commands sent from this helper extension to the hotword extension.
36  * @enum {string}
37  */
38 OptInManager.CommandFromHelper = {
39   DISABLE: 'ds',
40   ENABLE: 'en'
41 };
42
43
44 /**
45  * Commands sent from the page to this content script.
46  * @enum {string}
47  */
48 OptInManager.CommandFromPage = {
49   // User has explicitly clicked 'no'.
50   CLICKED_NO_OPTIN: 'hcno',
51   // User has opted in.
52   CLICKED_OPTIN: 'hco'
53 };
54
55
56 /**
57  * Handles a tab being activated / focused on.
58  * @param {{tabId: number}} info Information about the activated tab.
59  * @private
60  */
61 OptInManager.prototype.handleActivatedTab_ = function(info) {
62   chrome.tabs.get(info.tabId, this.preInjectTab_.bind(this));
63 };
64
65
66 /**
67  * Handles an updated tab.
68  * @param {number} tabId Id of the updated tab.
69  * @param {{status: string}} info Change info of the tab.
70  * @param {!Tab} tab Updated tab.
71  * @private
72  */
73 OptInManager.prototype.handleUpdatedTab_ = function(tabId, info, tab) {
74   //  Chrome fires multiple update events: undefined, loading and completed.
75   // We perform content injection on loading state.
76   if ('loading' !== info['status'])
77     return;
78   this.preInjectTab_(tab);
79 };
80
81
82 /**
83  * @param {Tab} tab Tab to consider.
84  * @private
85  */
86 OptInManager.prototype.preInjectTab_ = function(tab) {
87   if (!tab || !this.isEligibleUrl(tab.url))
88     return;
89   if (chrome.hotwordPrivate && chrome.hotwordPrivate.getStatus)
90     chrome.hotwordPrivate.getStatus(this.injectTab_.bind(this, tab));
91 };
92
93
94 /**
95  * @param {Tab} tab Tab to inject.
96  * @param {HotwordStatus} hotwordStatus Status of the hotword extension.
97  * @private
98  */
99 OptInManager.prototype.injectTab_ = function(tab, hotwordStatus) {
100   if (hotwordStatus.available) {
101     if (hotwordStatus.enabled)
102       chrome.tabs.executeScript(tab.id, {'file': 'audio_client.js'});
103     else
104       chrome.tabs.executeScript(tab.id, {'file': 'optin_client.js'});
105   }
106 };
107
108
109 /**
110  * Handles changes in the enabled state of the hotword feature in the
111  * Chrome settings page.
112  * @private
113  */
114 OptInManager.prototype.handleEnabledChange_ = function() {
115   if (chrome.hotwordPrivate && chrome.hotwordPrivate.getStatus)
116     chrome.hotwordPrivate.getStatus(this.updateEnabled_.bind(this));
117 };
118
119
120 /**
121  * Sends a message to the hotword extension to update it about Chrome settings.
122  * @param {HotwordStatus} hotwordStatus Status of the hotword extension.
123  * @private
124  */
125 OptInManager.prototype.updateEnabled_ = function(hotwordStatus) {
126   if (hotwordStatus.enabled) {
127     chrome.runtime.sendMessage(
128         OptInManager.HOTWORD_EXTENSION_ID_,
129         {'cmd': OptInManager.CommandFromHelper.ENABLE});
130   } else {
131     chrome.runtime.sendMessage(
132         OptInManager.HOTWORD_EXTENSION_ID_,
133         {'cmd': OptInManager.CommandFromHelper.DISABLE});
134   }
135 };
136
137
138 /**
139  * Handles messages from the helper content script.
140  * @param {*} request Message from the sender.
141  * @param {MessageSender} sender Information about the sender.
142  * @param {function(*)} sendResponse Callback function to respond to sender.
143  * @private
144  */
145 OptInManager.prototype.handleMessage_ = function(
146     request, sender, sendResponse) {
147   if (request.type) {
148     if (request.type === OptInManager.CommandFromPage.CLICKED_OPTIN) {
149       if (chrome.hotwordPrivate && chrome.hotwordPrivate.setEnabled) {
150         chrome.hotwordPrivate.setEnabled(true);
151         this.preInjectTab_(sender.tab);
152       }
153     }
154     // User has explicitly clicked 'no thanks'.
155     if (request.type === OptInManager.CommandFromPage.CLICKED_NO_OPTIN) {
156       if (chrome.hotwordPrivate && chrome.hotwordPrivate.setEnabled) {
157         chrome.hotwordPrivate.setEnabled(false);
158       }
159     }
160   }
161 };
162
163
164 /**
165  * Determines if a URL is eligible for hotwording. For now, the
166  * valid pages are the Google HP and SERP (this will include the NTP).
167  * @param {string} url Url to check.
168  * @return {boolean} True if url is eligible hotword url.
169  */
170 OptInManager.prototype.isEligibleUrl = function(url) {
171   if (!url) {
172     return false;
173   }
174
175   var baseUrls = [
176     'https://www.google.com',
177     'chrome://newtab',
178     'https://encrypted.google.com'
179   ];
180
181   for (var i = 0; i < baseUrls.length; i++) {
182     var base = baseUrls[i];
183     if (url === base + '/' ||
184         url.indexOf(base + '/_/chrome/newtab?') === 0 ||  // Appcache NTP.
185         url.indexOf(base + '/?') === 0 ||
186         url.indexOf(base + '/#') === 0 ||
187         url.indexOf(base + '/webhp') === 0 ||
188         url.indexOf(base + '/search') === 0) {
189       return true;
190     }
191   }
192   return false;
193 };
194
195
196 /**
197  * Initializes the extension.
198  */
199 OptInManager.prototype.initialize = function() {
200   chrome.tabs.onActivated.addListener(this.handleActivatedTab_.bind(this));
201   chrome.tabs.onUpdated.addListener(this.handleUpdatedTab_.bind(this));
202   chrome.runtime.onMessage.addListener(this.handleMessage_.bind(this));
203
204   if (chrome.hotwordPrivate && chrome.hotwordPrivate.onEnabledChanged) {
205     chrome.hotwordPrivate.onEnabledChanged.addListener(
206         this.handleEnabledChange_.bind(this));
207   }
208 };
209
210
211 new OptInManager().initialize();