Upstream version 9.37.197.0
[platform/framework/web/crosswalk.git] / src / third_party / trace-viewer / third_party / tvcm / src / tvcm / settings.js
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.
4
5 'use strict';
6
7 /**
8  * @fileoverview Provides the Settings object.
9  */
10 tvcm.exportTo('tvcm', function() {
11   /**
12    * Settings is a simple wrapper around local storage, to make it easier
13    * to test classes that have settings.
14    *
15    * May be called as new tvcm.Settings() or simply tvcm.Settings()
16    * @constructor
17    */
18   function Settings() {
19     return Settings;
20   };
21
22   function SessionSettings() {
23     return SessionSettings;
24   }
25
26   function AddStaticStorageFunctionsToClass_(input_class, storage) {
27     input_class.storage_ = storage;
28
29     /**
30      * Get the setting with the given name.
31      *
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.
37      */
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)
42         return opt_default;
43
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.
47       try {
48         return JSON.parse(rawVal).value;
49       } catch (e) {
50         input_class.storage_.removeItem(
51             input_class.namespace_(key, opt_namespace));
52         return opt_default;
53       }
54     };
55
56     /**
57      * Set the setting with the given name to the given value.
58      *
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.
64      */
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);
71     };
72
73     /**
74      * Return a list of all the keys, or all the keys in the given namespace
75      * if one is provided.
76      *
77      * @param {string=} opt_namespace If set, only return settings which
78      * begin with this prefix.
79      */
80     input_class.keys = function(opt_namespace) {
81       var result = [];
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));
87       }
88       return result;
89     };
90
91     input_class.isnamespaced_ = function(key, opt_namespace) {
92       return key.indexOf(input_class.normalize_(opt_namespace)) == 0;
93     };
94
95     input_class.namespace_ = function(key, opt_namespace) {
96       return input_class.normalize_(opt_namespace) + key;
97     };
98
99     input_class.unnamespace_ = function(key, opt_namespace) {
100       return key.replace(input_class.normalize_(opt_namespace), '');
101     };
102
103     /**
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.
108      */
109     input_class.normalize_ = function(opt_namespace) {
110       return input_class.NAMESPACE + (opt_namespace ? opt_namespace + '.' : '');
111     };
112
113     input_class.setAlternativeStorageInstance = function(instance) {
114       input_class.storage_ = instance;
115     };
116
117     input_class.getAlternativeStorageInstance = function() {
118       if (input_class.storage_ === localStorage)
119         return undefined;
120       return input_class.storage_;
121     };
122
123     input_class.NAMESPACE = 'trace-viewer';
124   };
125
126   AddStaticStorageFunctionsToClass_(Settings, localStorage);
127   AddStaticStorageFunctionsToClass_(SessionSettings, sessionStorage);
128
129   return {
130     Settings: Settings,
131     SessionSettings: SessionSettings
132   };
133 });