Remove getCached* methods from remoting.Identity.
[chromium-blink-merge.git] / remoting / webapp / crd / js / butter_bar.js
blob3334c7f975066e28a78b3e1e543cc6a75bef20e7
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.
5 /**
6  * @fileoverview
7  * ButterBar class that is used to show the butter bar with various
8  * notifications.
9  */
11 'use strict';
13 /** @suppress {duplicate} */
14 var remoting = remoting || {};
16 /**
17  * @constructor
18  */
19 remoting.ButterBar = function() {
20   this.storageKey_ = '';
22   /** @type{remoting.ButterBar} */
23   var that = this;
25   /** @param {Object} syncValues */
26   var onSyncDataLoaded = function(syncValues) {
27     chrome.storage.local.get(
28         remoting.kIT2MeVisitedStorageKey,
29         that.onStateLoaded_.bind(that, syncValues));
30   };
32   chrome.storage.sync.get(
33       [remoting.ButterBar.kSurveyStorageKey_,
34        remoting.ButterBar.kHangoutsStorageKey_],
35       onSyncDataLoaded);
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  */
47 remoting.ButterBar.prototype.show_ =
48     function(messageId, substitutions, storageKey) {
49   this.storageKey_ = storageKey;
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);
58   document.getElementById(remoting.ButterBar.kDismissId_).addEventListener(
59       'click', this.dismiss.bind(this, false), false);
61   document.getElementById(remoting.ButterBar.kId_).hidden = false;
64 /**
65   * @param {Object} syncValues
66   * @param {Object} localValues
67   * @private
68   */
69 remoting.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];
79   var showSurvey = !surveyDismissed;
80   var showHangouts = it2meExpanded && !hangoutsDismissed;
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   }
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                 'target="_blank">',
100                 '</a>'],
101                remoting.ButterBar.kHangoutsStorageKey_);
102   }
105 /** @const @private */
106 remoting.ButterBar.kId_ = 'butter-bar';
108 /** @const @private */
109 remoting.ButterBar.kMessageId_ = 'butter-bar-message';
110 /** @const @private */
111 remoting.ButterBar.kDismissId_ = 'butter-bar-dismiss';
113 /** @const @private */
114 remoting.ButterBar.kSurveyStorageKey_ = 'feedback-survey-dismissed';
115 /** @const @private */
116 remoting.ButterBar.kHangoutsStorageKey_ = 'hangouts-notice-dismissed';
119  * Hide the butter bar request and record some basic information about the
120  * current state of the world in synced storage. This may be useful in the
121  * future if we want to show the request again. At the moment, the data itself
122  * is ignored; only its presence or absence is important.
124  * @param {boolean} accepted True if the user clicked the "accept" link;
125  *     false if they clicked the close icon.
126  */
127 remoting.ButterBar.prototype.dismiss = function(accepted) {
128   var value = {};
129   value[this.storageKey_] = {
130     optIn: accepted,
131     date: new Date(),
132     version: chrome.runtime.getManifest().version
133   };
134   chrome.storage.sync.set(value);
136   document.getElementById(remoting.ButterBar.kId_).hidden = true;