Give names to all utility processes.
[chromium-blink-merge.git] / chrome / browser / resources / settings / pref_tracker / pref_tracker.js
blob104b18626e8f9cabb4ca0f390208ae50bcea8a5c
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('cr-settings-pref-tracker', {
39 publish: {
40 /**
41 * The Preference object being tracked.
43 * @attribute pref
44 * @type {Object}
45 * @default null
47 pref: null,
50 observe: {
51 pref: 'validate_',
54 /** @override */
55 ready: function() {
56 this.validate_();
58 instances.push(this);
61 /**
62 * Throws an error if prefs are initialized and the tracked pref is not
63 * found.
64 * @private
66 validate_: function() {
67 this.async(function() {
68 // Note that null == undefined.
69 if (CrSettingsPrefs.isInitialized && this.pref == null) {
70 // HACK ALERT: This is the best clue we have as to the pref key for
71 // this tracker. This value should not be relied upon anywhere or
72 // actually used besides for this error message.
73 var keyHint = '';
74 var parentPrefString = this.parentNode && this.parentNode.host &&
75 this.parentNode.host.getAttribute('pref');
76 if (parentPrefString) {
77 keyHint = parentPrefString.match(/{{([a-z._]+)}}/)[1];
80 throw new Error('Pref not found. Key Hint: ' + keyHint);
82 });
84 });
85 })();