summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/resources/options/reset_profile_settings_banner.js
blob: 4517e9151c7fed0d4ba41775df3337f2c710d506 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2013 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.

// Note: the native-side handler for this is ResetProfileSettingsHandler.

cr.define('options', function() {
  /** @const */ var OptionsPage = options.OptionsPage;

  /**
   * ResetProfileSettingsBanner class
   * Provides encapsulated handling of the Reset Profile Settings banner.
   * @constructor
   */
  function ResetProfileSettingsBanner() {}

  cr.addSingletonGetter(ResetProfileSettingsBanner);

  ResetProfileSettingsBanner.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/resetProfileSettings.
     *
     * In such a case, ResetProfileSettingsOverlay'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,

    /**
     * Initializes the banner's event handlers.
     */
    initialize: function() {
      $('reset-profile-settings-banner-close').onclick = function(event) {
        chrome.send('metricsHandler:recordAction',
            ['AutomaticReset_WebUIBanner_ManuallyClosed']);
        ResetProfileSettingsBanner.dismiss();
      };
      $('reset-profile-settings-banner-activate').onclick = function(event) {
        chrome.send('metricsHandler:recordAction',
            ['AutomaticReset_WebUIBanner_ResetClicked']);
        OptionsPage.navigateToPage('resetProfileSettings');
      };
    },

    /**
     * Called by the native code to show the banner if needed.
     * @private
     */
    show_: function() {
      if (!this.hadBeenDismissed_) {
        chrome.send('metricsHandler:recordAction',
            ['AutomaticReset_WebUIBanner_BannerShown']);
        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.
     * @private
     */
    dismiss_: function() {
      chrome.send('onDismissedResetProfileSettingsBanner');
      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) {
      $('reset-profile-settings-banner').hidden = !show;
    }
  };

  // Forward public APIs to private implementations.
  [
    'show',
    'dismiss',
  ].forEach(function(name) {
    ResetProfileSettingsBanner[name] = function() {
      var instance = ResetProfileSettingsBanner.getInstance();
      return instance[name + '_'].apply(instance, arguments);
    };
  });

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