1// Copyright 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
5cr.define('options', function() {
6
7  /**
8   * Base class for banners that appear at the top of the settings page.
9   */
10  function SettingsBannerBase() {}
11
12  cr.addSingletonGetter(SettingsBannerBase);
13
14  SettingsBannerBase.prototype = {
15    /**
16     * Whether or not the banner has already been dismissed.
17     *
18     * This is needed because of the surprising ordering of asynchronous
19     * JS<->native calls when the settings page is opened with specifying a
20     * given sub-page, e.g. chrome://settings/AutomaticSettingsReset.
21     *
22     * In such a case, AutomaticSettingsResetOverlay's didShowPage(), which
23     * calls our dismiss() method, would be called before the native Handlers'
24     * InitalizePage() methods have an effect in the JS, which includes calling
25     * our show() method. This would mean that the banner would be first
26     * dismissed, then shown. We want to prevent this.
27     *
28     * @type {boolean}
29     * @private
30     */
31    hadBeenDismissed_: false,
32
33    /**
34     * Metric name to send when a show event occurs.
35     */
36    showMetricName_: '',
37
38    /**
39     * Name of the native callback invoked when the banner is dismised.
40     */
41    dismissNativeCallbackName_: '',
42
43    /**
44     * DOM element whose visibility is set when setVisibility_ is called.
45     */
46    setVisibilibyDomElement_: null,
47
48    /**
49     * Called by the native code to show the banner if needed.
50     * @private
51     */
52    show_: function() {
53      if (!this.hadBeenDismissed_) {
54        chrome.send('metricsHandler:recordAction', [this.showMetricName_]);
55        this.setVisibility_(true);
56      }
57    },
58
59    /**
60     * Called when the banner should be closed as a result of something taking
61     * place on the WebUI page, i.e. when its close button is pressed, or when
62     * the confirmation dialog for the profile settings reset feature is opened.
63     * @private
64     */
65    dismiss_: function() {
66      chrome.send(this.dismissNativeCallbackName_);
67      this.hadBeenDismissed_ = true;
68      this.setVisibility_(false);
69    },
70
71    /**
72     * Sets whether or not the reset profile settings banner shall be visible.
73     * @param {boolean} show Whether or not to show the banner.
74     * @private
75     */
76    setVisibility_: function(show) {
77      this.setVisibilibyDomElement_.hidden = !show;
78    },
79
80  };
81
82  // Export
83  return {
84    SettingsBannerBase: SettingsBannerBase
85  };
86});
87