Upstream version 7.35.139.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 the page to this content script.
36  * @enum {string}
37  */
38 OptInManager.CommandFromPage = {
39   // User has explicitly clicked 'no'.
40   CLICKED_NO_OPTIN: 'hcno',
41   // User has opted in.
42   CLICKED_OPTIN: 'hco',
43   // Audio logging is opted in.
44   AUDIO_LOGGING_ON: 'alon',
45   // Audio logging is opted out.
46   AUDIO_LOGGING_OFF: 'aloff',
47   // User visited an eligible page.
48   PAGE_WAKEUP: 'wu'
49 };
50
51
52 /**
53  * @param {Tab} tab Tab to inject.
54  * @param {function(HotwordStatus)} sendResponse Callback function to respond
55  *     to sender.
56  * @param {HotwordStatus} hotwordStatus Status of the hotword extension.
57  * @private
58  */
59 OptInManager.prototype.injectTab_ = function(
60     tab, sendResponse, hotwordStatus) {
61   if (!hotwordStatus.available)
62     return;
63   if (hotwordStatus.enabled)
64     chrome.tabs.executeScript(tab.id, {'file': 'audio_client.js'});
65   else if (!hotwordStatus.enabledSet)
66     chrome.tabs.executeScript(tab.id, {'file': 'optin_client.js'});
67   sendResponse(hotwordStatus);
68 };
69
70
71 /**
72  * Handles messages from the helper content script.
73  * @param {*} request Message from the sender.
74  * @param {MessageSender} sender Information about the sender.
75  * @param {function(HotwordStatus)} sendResponse Callback function to respond
76  *     to sender.
77  * @return {boolean} Whether to maintain the port open to call sendResponse.
78  * @private
79  */
80 OptInManager.prototype.handleMessage_ = function(
81     request, sender, sendResponse) {
82   switch (request.type) {
83     case OptInManager.CommandFromPage.PAGE_WAKEUP:
84       if (((sender.tab && this.isEligibleUrl(sender.tab.url)) ||
85           sender.id == OptInManager.HOTWORD_EXTENSION_ID_) &&
86           chrome.hotwordPrivate && chrome.hotwordPrivate.getStatus) {
87         chrome.hotwordPrivate.getStatus(
88             this.injectTab_.bind(this, request.tab || sender.tab,
89                                  sendResponse));
90         return true;
91       }
92       break;
93     case OptInManager.CommandFromPage.CLICKED_OPTIN:
94       if (chrome.hotwordPrivate && chrome.hotwordPrivate.setEnabled &&
95           chrome.hotwordPrivate.getStatus) {
96         chrome.hotwordPrivate.setEnabled(true);
97         chrome.hotwordPrivate.getStatus(
98             this.injectTab_.bind(this, sender.tab, sendResponse));
99         return true;
100       }
101       break;
102     // User has explicitly clicked 'no thanks'.
103     case OptInManager.CommandFromPage.CLICKED_NO_OPTIN:
104       if (chrome.hotwordPrivate && chrome.hotwordPrivate.setEnabled) {
105         chrome.hotwordPrivate.setEnabled(false);
106       }
107       break;
108     // Information regarding the audio logging preference was sent.
109     case OptInManager.CommandFromPage.AUDIO_LOGGING_ON:
110       if (chrome.hotwordPrivate &&
111           chrome.hotwordPrivate.setAudioLoggingEnabled) {
112         chrome.hotwordPrivate.setAudioLoggingEnabled(true);
113       }
114       break;
115     case OptInManager.CommandFromPage.AUDIO_LOGGING_OFF:
116       if (chrome.hotwordPrivate &&
117           chrome.hotwordPrivate.setAudioLoggingEnabled) {
118         chrome.hotwordPrivate.setAudioLoggingEnabled(false);
119       }
120       break;
121     default:
122       break;
123   }
124   return false;
125 };
126
127
128 /**
129  * 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).
131  * @param {string} url Url to check.
132  * @return {boolean} True if url is eligible hotword url.
133  */
134 OptInManager.prototype.isEligibleUrl = function(url) {
135   if (!url)
136     return false;
137
138   var baseUrls = [
139     'https://www.google.com',
140     'chrome://newtab',
141     'https://encrypted.google.com'
142   ];
143
144   for (var i = 0; i < baseUrls.length; i++) {
145     var base = baseUrls[i];
146     if (url === base + '/' ||
147         url.indexOf(base + '/_/chrome/newtab?') === 0 ||  // Appcache NTP.
148         url.indexOf(base + '/?') === 0 ||
149         url.indexOf(base + '/#') === 0 ||
150         url.indexOf(base + '/webhp') === 0 ||
151         url.indexOf(base + '/search') === 0) {
152       return true;
153     }
154   }
155   return false;
156 };
157
158
159 /**
160  * Initializes the extension.
161  */
162 OptInManager.prototype.initialize = function() {
163   // 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
165   // relaying is removed form the content scripts.
166   chrome.runtime.onMessage.addListener(this.handleMessage_.bind(this));
167   chrome.runtime.onMessageExternal.addListener(
168       this.handleMessage_.bind(this));
169 };
170
171
172 new OptInManager().initialize();