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.
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.
13 * <cr-settings-pref-tracker pref="{{prefs.settings.foo.bar}}">
14 * </cr-settings-pref-tracker>
16 * @element cr-settings-pref-tracker
21 * An array of all the tracker instances.
22 * @type {!Array<!CrSettingsPrefTrackerElement>}
27 * Validates all tracker instances.
30 var validateAll_ = function() {
31 instances
.forEach(function(tracker
) {
36 document
.addEventListener(CrSettingsPrefs
.INITIALIZED
, validateAll_
);
39 is
: 'cr-settings-pref-tracker',
43 * The Preference object being tracked.
48 observer
: 'validate_',
60 * Throws an error if prefs are initialized and the tracked pref is not
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'));