Home | History | Annotate | Download | only in hotword_helper
      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 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
     11  *   extension.
     12  */
     13 
     14 
     15 
     16 /** @constructor */
     17 var OptInClient = function() {};
     18 
     19 
     20 /**
     21  * Commands sent from this injected content scripts to the page.
     22  * @enum {string}
     23  */
     24 OptInClient.CommandToPage = {
     25   // Allow hotword opt-in message bubble to be displayed.
     26   ALLOW_OPTIN_MESSAGE: 'chwom'
     27 };
     28 
     29 
     30 /**
     31  * Commands sent from the page to this content script.
     32  * @enum {string}
     33  */
     34 OptInClient.CommandFromPage = {
     35   // User has explicitly clicked 'no'.
     36   CLICKED_NO_OPTIN: 'hcno',
     37   // User has opted in.
     38   CLICKED_OPTIN: 'hco',
     39   // Audio logging is opted in.
     40   AUDIO_LOGGING_ON: 'alon',
     41   // Audio logging is opted out.
     42   AUDIO_LOGGING_OFF: 'aloff',
     43 };
     44 
     45 
     46 /**
     47  * Used to determine if this content script has already been injected.
     48  * @const {string}
     49  * @private
     50  */
     51 OptInClient.EXISTS_ = 'chwoihe';
     52 
     53 
     54 /**
     55  * Handles the messages posted to the window, mainly listening for
     56  * the optin and no optin clicks. Also listening for preference on
     57  * audio logging.
     58  * @param {!MessageEvent} messageEvent Message event from the window.
     59  * @private
     60  */
     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});
     65   }
     66 };
     67 
     68 
     69 /**
     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.
     72  * @private
     73  */
     74 OptInClient.prototype.notifyPageAllowOptIn_ = function() {
     75   if (document.readyState === 'complete') {
     76     window.postMessage(
     77         {'type': OptInClient.CommandToPage.ALLOW_OPTIN_MESSAGE}, '*');
     78   }
     79 };
     80 
     81 
     82 /**
     83  * Initializes the content script.
     84  */
     85 OptInClient.prototype.initialize = function() {
     86   if (OptInClient.EXISTS_ in window)
     87     return;
     88   window[OptInClient.EXISTS_] = true;
     89   window.addEventListener(
     90       'message', this.handleCommandFromPage_.bind(this), false);
     91 
     92   if (document.readyState === 'complete')
     93     this.notifyPageAllowOptIn_();
     94   else
     95     document.onreadystatechange = this.notifyPageAllowOptIn_;
     96 };
     97 
     98 
     99 new OptInClient().initialize();
    100