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.
8 * @fileoverview This is the content script injected by the opt in helper.
9 * It is injected into the newtab page and also google.com and notifies the
10 * page to show an opt-in message for the user to enable the built-in hotword
17 var OptInClient = function() {};
21 * Commands sent from this injected content scripts to the page.
24 OptInClient.CommandToPage = {
25 // Allow hotword opt-in message bubble to be displayed.
26 ALLOW_OPTIN_MESSAGE: 'chwom'
31 * Commands sent from the page to this content script.
34 OptInClient.CommandFromPage = {
35 // User has explicitly clicked 'no'.
36 CLICKED_NO_OPTIN: 'hcno',
39 // Audio logging is opted in.
40 AUDIO_LOGGING_ON: 'alon',
41 // Audio logging is opted out.
42 AUDIO_LOGGING_OFF: 'aloff',
47 * Used to determine if this content script has already been injected.
51 OptInClient.EXISTS_ = 'chwoihe';
55 * Handles the messages posted to the window, mainly listening for
56 * the optin and no optin clicks. Also listening for preference on
58 * @param {!MessageEvent} messageEvent Message event from the window.
61 OptInClient.prototype.handleCommandFromPage_ = function(messageEvent) {
62 if (messageEvent.source === window && messageEvent.data.type) {
63 var command = messageEvent.data.type;
64 chrome.runtime.sendMessage({'type': command});
70 * Sends a command to the page allowing it to display the hotword opt-in
71 * message. It is up to the page whether it is actually displayed.
74 OptInClient.prototype.notifyPageAllowOptIn_ = function() {
75 if (document.readyState === 'complete') {
77 {'type': OptInClient.CommandToPage.ALLOW_OPTIN_MESSAGE}, '*');
83 * Initializes the content script.
85 OptInClient.prototype.initialize = function() {
86 if (OptInClient.EXISTS_ in window)
88 window[OptInClient.EXISTS_] = true;
89 window.addEventListener(
90 'message', this.handleCommandFromPage_.bind(this), false);
92 if (document.readyState === 'complete')
93 this.notifyPageAllowOptIn_();
95 document.onreadystatechange = this.notifyPageAllowOptIn_;
99 new OptInClient().initialize();