summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/resources/options/settings_banner.js
blob: 666aaf2aa500c29ce3b68ee685923fc23bf104ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

cr.define('options', function() {

  /**
   * Base class for banners that appear at the top of the settings page.
   * @constructor
   */
  function SettingsBannerBase() {}

  cr.addSingletonGetter(SettingsBannerBase);

  SettingsBannerBase.prototype = {
    /**
     * Whether or not the banner has already been dismissed.
     *
     * This is needed because of the surprising ordering of asynchronous
     * JS<->native calls when the settings page is opened with specifying a
     * given sub-page, e.g. chrome://settings/AutomaticSettingsReset.
     *
     * In such a case, AutomaticSettingsResetOverlay's didShowPage(), which
     * calls our dismiss() method, would be called before the native Handlers'
     * InitalizePage() methods have an effect in the JS, which includes calling
     * our show() method. This would mean that the banner would be first
     * dismissed, then shown. We want to prevent this.
     *
     * @type {boolean}
     * @private
     */
    hadBeenDismissed_: false,

    /**
     * Metric name to send when a show event occurs.
     * @protected
     */
    showMetricName: '',

    /**
     * Name of the native callback invoked when the banner is dismised.
     * @protected
     */
    dismissNativeCallbackName: '',

    /**
     * DOM element whose visibility is set when setVisibility_ is called.
     * @protected
     */
    visibilityDomElement: null,

    /**
     * Called by the native code to show the banner if needed.
     * @protected
     */
    show: function() {
      if (!this.hadBeenDismissed_) {
        chrome.send('metricsHandler:recordAction', [this.showMetricName]);
        this.setVisibility_(true);
      }
    },

    /**
     * Called when the banner should be closed as a result of something taking
     * place on the WebUI page, i.e. when its close button is pressed, or when
     * the confirmation dialog for the profile settings reset feature is opened.
     * @protected
     */
    dismiss: function() {
      chrome.send(this.dismissNativeCallbackName);
      this.hadBeenDismissed_ = true;
      this.setVisibility_(false);
    },

    /**
     * Sets whether or not the reset profile settings banner shall be visible.
     * @param {boolean} show Whether or not to show the banner.
     * @private
     */
    setVisibility_: function(show) {
      this.visibilityDomElement.hidden = !show;
    },
  };

  // Export
  return {
    SettingsBannerBase: SettingsBannerBase
  };
});