1// Copyright 2013 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/**
6 * @fileoverview
7 * ButterBar class that is used to show the butter bar with various
8 * notifications.
9 */
10
11'use strict';
12
13/** @suppress {duplicate} */
14var remoting = remoting || {};
15
16/**
17 * @constructor
18 */
19remoting.ButterBar = function() {
20  this.storageKey_ = '';
21
22  /** @type{remoting.ButterBar} */
23  var that = this;
24
25  /** @param {Object} syncValues */
26  var onSyncDataLoaded = function(syncValues) {
27    chrome.storage.local.get(
28        remoting.kIT2MeVisitedStorageKey,
29        that.onStateLoaded_.bind(that, syncValues));
30  };
31
32  chrome.storage.sync.get(
33      [remoting.ButterBar.kSurveyStorageKey_,
34       remoting.ButterBar.kHangoutsStorageKey_],
35      onSyncDataLoaded);
36}
37
38/**
39 * Shows butter bar with the specified |message| and updates |storageKey| after
40 * the bar is dismissed.
41 *
42 * @param {string} messageId
43 * @param {string|Array} substitutions
44 * @param {string} storageKey
45 * @private
46 */
47remoting.ButterBar.prototype.show_ =
48    function(messageId, substitutions, storageKey) {
49  this.storageKey_ = storageKey;
50
51  var messageElement = document.getElementById(remoting.ButterBar.kMessageId_);
52  l10n.localizeElementFromTag(messageElement, messageId, substitutions, true);
53  var acceptLink =
54      /** @type{Element} */ messageElement.getElementsByTagName('a')[0];
55  acceptLink.addEventListener(
56      'click', this.dismiss.bind(this, true), false);
57
58  document.getElementById(remoting.ButterBar.kDismissId_).addEventListener(
59      'click', this.dismiss.bind(this, false), false);
60
61  document.getElementById(remoting.ButterBar.kId_).hidden = false;
62}
63
64/**
65  * @param {Object} syncValues
66  * @param {Object} localValues
67  * @private
68  */
69remoting.ButterBar.prototype.onStateLoaded_ =
70    function(syncValues, localValues) {
71  /** @type {boolean} */
72  var surveyDismissed = !!syncValues[remoting.ButterBar.kSurveyStorageKey_];
73  /** @type {boolean} */
74  var hangoutsDismissed =
75      !!syncValues[remoting.ButterBar.kHangoutsStorageKey_];
76  /** @type {boolean} */
77  var it2meExpanded = !!localValues[remoting.kIT2MeVisitedStorageKey];
78
79  var showSurvey = !surveyDismissed;
80  var showHangouts = it2meExpanded && !hangoutsDismissed;
81
82  // If both messages can be shown choose only one randomly.
83  if (showSurvey && showHangouts) {
84    if (Math.random() > 0.5) {
85      showSurvey = false;
86    } else {
87      showHangouts = false;
88    }
89  }
90
91  if (showSurvey) {
92    this.show_(/*i18n-content*/'SURVEY_INVITATION',
93               ['<a href="http://goo.gl/njH2q" target="_blank">', '</a>'],
94               remoting.ButterBar.kSurveyStorageKey_);
95  } else if (showHangouts) {
96    this.show_(/*i18n-content*/'HANGOUTS_INVITATION',
97               ['<a id="hangouts-accept" ' +
98                'href="https://plus.google.com/hangouts/_?gid=818572447316">',
99                '</a>'],
100               remoting.ButterBar.kHangoutsStorageKey_);
101  }
102};
103
104/** @const @private */
105remoting.ButterBar.kId_ = 'butter-bar';
106
107/** @const @private */
108remoting.ButterBar.kMessageId_ = 'butter-bar-message';
109/** @const @private */
110remoting.ButterBar.kDismissId_ = 'butter-bar-dismiss';
111
112/** @const @private */
113remoting.ButterBar.kSurveyStorageKey_ = 'feedback-survey-dismissed';
114/** @const @private */
115remoting.ButterBar.kHangoutsStorageKey_ = 'hangouts-notice-dismissed';
116
117/**
118 * Hide the butter bar request and record some basic information about the
119 * current state of the world in synced storage. This may be useful in the
120 * future if we want to show the request again. At the moment, the data itself
121 * is ignored; only its presence or absence is important.
122 *
123 * @param {boolean} accepted True if the user clicked the "accept" link;
124 *     false if they clicked the close icon.
125 */
126remoting.ButterBar.prototype.dismiss = function(accepted) {
127  var value = {};
128  value[this.storageKey_] = {
129    optIn: accepted,
130    date: new Date(),
131    version: chrome.runtime.getManifest().version
132  };
133  chrome.storage.sync.set(value);
134
135  document.getElementById(remoting.ButterBar.kId_).hidden = true;
136};
137