Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / resources / settings / pref_tracker / pref_tracker.js
blob1d692abdadef906c608475d0f2446b1e3829fceb
1 // Copyright 2015 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 * `cr-settings-pref-tracker` is a utility element used to track the
8 * initialization of a specified preference and throw an error if the pref
9 * is not defined after prefs have all been fetched.
11 * Example:
13 * <cr-settings-pref-tracker pref="{{prefs.settings.foo.bar}}">
14 * </cr-settings-pref-tracker>
16 * @element cr-settings-pref-tracker
18 (function() {
20 /**
21 * An array of all the tracker instances.
22 * @type {!Array<!CrSettingsPrefTrackerElement>}
24 var instances = [];
26 /**
27 * Validates all tracker instances.
28 * @private
30 var validateAll_ = function() {
31 instances.forEach(function(tracker) {
32 tracker.validate_();
33 });
36 document.addEventListener(CrSettingsPrefs.INITIALIZED, validateAll_);
38 Polymer({
39 is: 'cr-settings-pref-tracker',
41 properties: {
42 /**
43 * The Preference object being tracked.
44 * @type {?PrefObject}
46 pref: {
47 type: Object,
48 observer: 'validate_',
52 /** @override */
53 ready: function() {
54 this.validate_();
56 instances.push(this);
59 /**
60 * Throws an error if prefs are initialized and the tracked pref is not
61 * found.
62 * @private
64 validate_: function() {
65 this.async(function() {
66 // Note that null == undefined.
67 if (CrSettingsPrefs.isInitialized && this.pref == null) {
68 // HACK ALERT: This is the best clue we have as to the pref key for
69 // this tracker. This value should not be relied upon anywhere or
70 // actually used besides for this error message.
71 var parentControlHTML = this.parentNode && this.parentNode.host &&
72 this.parentNode.host.outerHTML;
74 throw new Error('Pref not found. Parent control:' +
75 (parentControlHTML || 'Unknown'));
77 });
79 });
80 })();