1 // Copyright (c) 2012 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.
8 * @fileoverview Provides the Settings object.
10 tvcm.exportTo('tvcm', function() {
12 * Settings is a simple wrapper around local storage, to make it easier
13 * to test classes that have settings.
15 * May be called as new tvcm.Settings() or simply tvcm.Settings()
22 function SessionSettings() {
23 return SessionSettings;
26 function AddStaticStorageFunctionsToClass_(input_class, storage) {
27 input_class.storage_ = storage;
30 * Get the setting with the given name.
32 * @param {string} key The name of the setting.
33 * @param {string=} opt_default The default value to return if not set.
34 * @param {string=} opt_namespace If set, the setting name will be prefixed
35 * with this namespace, e.g. "categories.settingName". This is useful for
36 * a set of related settings.
38 input_class.get = function(key, opt_default, opt_namespace) {
39 key = input_class.namespace_(key, opt_namespace);
40 var rawVal = input_class.storage_.getItem(key);
41 if (rawVal === null || rawVal === undefined)
44 // Old settings versions used to stringify objects instead of putting them
45 // into JSON. If those are encountered, parse will fail. In that case,
46 // "upgrade" the setting to the default value.
48 return JSON.parse(rawVal).value;
50 input_class.storage_.removeItem(
51 input_class.namespace_(key, opt_namespace));
57 * Set the setting with the given name to the given value.
59 * @param {string} key The name of the setting.
60 * @param {string} value The value of the setting.
61 * @param {string=} opt_namespace If set, the setting name will be prefixed
62 * with this namespace, e.g. "categories.settingName". This is useful for
63 * a set of related settings.
65 input_class.set = function(key, value, opt_namespace) {
66 if (value === undefined)
67 throw new Error('Settings.set: value must not be undefined');
68 var v = JSON.stringify({value: value});
69 input_class.storage_.setItem(
70 input_class.namespace_(key, opt_namespace), v);
74 * Return a list of all the keys, or all the keys in the given namespace
77 * @param {string=} opt_namespace If set, only return settings which
78 * begin with this prefix.
80 input_class.keys = function(opt_namespace) {
82 opt_namespace = opt_namespace || '';
83 for (var i = 0; i < input_class.storage_.length; i++) {
84 var key = input_class.storage_.key(i);
85 if (input_class.isnamespaced_(key, opt_namespace))
86 result.push(input_class.unnamespace_(key, opt_namespace));
91 input_class.isnamespaced_ = function(key, opt_namespace) {
92 return key.indexOf(input_class.normalize_(opt_namespace)) == 0;
95 input_class.namespace_ = function(key, opt_namespace) {
96 return input_class.normalize_(opt_namespace) + key;
99 input_class.unnamespace_ = function(key, opt_namespace) {
100 return key.replace(input_class.normalize_(opt_namespace), '');
104 * All settings are prefixed with a global namespace to avoid collisions.
105 * input_class may also be namespaced with an additional prefix passed into
106 * the get, set, and keys methods in order to group related settings.
107 * This method makes sure the two namespaces are always set properly.
109 input_class.normalize_ = function(opt_namespace) {
110 return input_class.NAMESPACE + (opt_namespace ? opt_namespace + '.' : '');
113 input_class.setAlternativeStorageInstance = function(instance) {
114 input_class.storage_ = instance;
117 input_class.getAlternativeStorageInstance = function() {
118 if (input_class.storage_ === localStorage)
120 return input_class.storage_;
123 input_class.NAMESPACE = 'trace-viewer';
126 AddStaticStorageFunctionsToClass_(Settings, localStorage);
127 AddStaticStorageFunctionsToClass_(SessionSettings, sessionStorage);
131 SessionSettings: SessionSettings