Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: ui/accessibility/extensions/colorenhancer/src/storage.js

Issue 984833004: Add color enhancer as a chromium accessibility extensions. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: More docs. Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 // TODO(wnwen): Move to chrome.storage.local, wrap calls, add JsDocs.
6
7 var DEFAULT_DELTA = 1.0;
8 var LOCAL_STORAGE_TAG_DELTA = 'cvd_delta';
9 var LOCAL_STORAGE_TAG_SITE_DELTA = 'cvd_site_deltas';
10
11
12 function validDelta(delta) {
13 return delta >= 0 && delta <= 1;
14 }
15
16
17 function getDefaultDelta() {
18 var delta = localStorage[LOCAL_STORAGE_TAG_DELTA];
19 if (validDelta(delta)) {
20 return delta;
21 }
22 delta = DEFAULT_DELTA;
23 localStorage[LOCAL_STORAGE_TAG_DELTA] = delta;
24 return delta;
25 }
26
27
28 function setDefaultDelta(delta) {
29 if (!validDelta(delta)) {
30 delta = DEFAULT_DELTA;
31 }
32 localStorage[LOCAL_STORAGE_TAG_DELTA] = delta;
33 }
34
35
36 function getSiteDelta(site) {
37 var delta = getDefaultDelta();
38 try {
39 var siteDeltas = JSON.parse(localStorage[LOCAL_STORAGE_TAG_SITE_DELTA]);
40 delta = siteDeltas[site];
41 if (!validDelta(delta)) {
42 delta = getDefaultDelta();
43 }
44 } catch (e) {
45 delta = getDefaultDelta();
46 }
47 return delta;
48 }
49
50
51 function setSiteDelta(site, delta) {
52 if (!validDelta(delta)) {
53 delta = getDefaultDelta();
54 }
55 var siteDeltas = {};
56 try {
57 siteDeltas = JSON.parse(localStorage[LOCAL_STORAGE_TAG_SITE_DELTA]);
58 } catch (e) {
59 siteDeltas = {};
60 }
61 siteDeltas[site] = delta;
62 localStorage[LOCAL_STORAGE_TAG_SITE_DELTA] = JSON.stringify(siteDeltas);
63 }
64
65
66 function resetSiteDeltas() {
67 var siteDeltas = {};
68 localStorage[LOCAL_STORAGE_TAG_SITE_DELTA] = JSON.stringify(siteDeltas);
69 }
70
71
72 // ======= Severity setting =======
73
74 var DEFAULT_SEVERITY = 1.0;
75 var LOCAL_STORAGE_TAG_SEVERITY = 'cvd_severity';
76 var LOCAL_STORAGE_TAG_SITE_SEVERITY = 'cvd_severities';
77
78
79 function validSeverity(severity) {
80 return severity >= 0 && severity <= 1;
81 }
82
83
84 function getDefaultSeverity() {
85 var severity = localStorage[LOCAL_STORAGE_TAG_SEVERITY];
86 if (validSeverity(severity)) {
87 return severity;
88 }
89 severity = DEFAULT_SEVERITY;
90 localStorage[LOCAL_STORAGE_TAG_SEVERITY] = severity;
91 return severity;
92 }
93
94
95 function setDefaultSeverity(severity) {
96 if (!validSeverity(severity)) {
97 severity = DEFAULT_SEVERITY;
98 }
99 localStorage[LOCAL_STORAGE_TAG_SEVERITY] = severity;
100 }
101
102
103 // TODO(mustaq): Remove site-specific severity setting.
104 function getSiteSeverity(site) {
105 var severity = getDefaultSeverity();
106 try {
107 var siteSeverities =
108 JSON.parse(localStorage[LOCAL_STORAGE_TAG_SITE_SEVERITY]);
109 severity = siteSeverities[site];
110 if (!validSeverity(severity)) {
111 severity = getDefaultSeverity();
112 }
113 } catch (e) {
114 severity = getDefaultSeverity();
115 }
116 return severity;
117 }
118
119
120 function setSiteSeverity(site, severity) {
121 if (!validSeverity(severity)) {
122 severity = getDefaultSeverity();
123 }
124 var siteSeverities = {};
125 try {
126 siteSeverities = JSON.parse(localStorage[LOCAL_STORAGE_TAG_SITE_SEVERITY]);
127 } catch (e) {
128 siteSeverities = {};
129 }
130 siteSeverities[site] = severity;
131 localStorage[LOCAL_STORAGE_TAG_SITE_SEVERITY] =
132 JSON.stringify(siteSeverities);
133 }
134
135
136 function resetSiteSeverities() {
137 var siteSeverities = {};
138 localStorage[LOCAL_STORAGE_TAG_SITE_SEVERITY] =
139 JSON.stringify(siteSeverities);
140 }
141
142
143 // ======= Type setting =======
144
145 // TODO(wnwen): Use enums rather than strings.
146 var DEFAULT_TYPE = 'PROTANOMALY';
147 var LOCAL_STORAGE_TAG_TYPE = 'cvd_type';
148
149
150 function validType(type) {
151 return type === 'PROTANOMALY' ||
152 type === 'DEUTERANOMALY' ||
153 type === 'TRITANOMALY';
154 }
155
156
157 function getDefaultType() {
158 var type = localStorage[LOCAL_STORAGE_TAG_TYPE];
159 if (validType(type)) {
160 return type;
161 }
162 type = DEFAULT_TYPE;
163 localStorage[LOCAL_STORAGE_TAG_TYPE] = type;
164 return type;
165 }
166
167
168 function setDefaultType(type) {
169 if (!validType(type)) {
170 type = DEFAULT_TYPE;
171 }
172 localStorage[LOCAL_STORAGE_TAG_TYPE] = type;
173 }
174
175
176 // ======= Simulate setting =======
177
178 var DEFAULT_SIMULATE = false;
179 var LOCAL_STORAGE_TAG_SIMULATE = 'cvd_simulate';
180
181
182 function validSimulate(simulate) {
183 return simulate == true || simulate == false;
184 }
185
186
187 function getDefaultSimulate() {
188 var simulate = localStorage[LOCAL_STORAGE_TAG_SIMULATE];
189
190 //Make it a boolean if possible
191 if (simulate === 'true') {
192 simulate = true;
193 } else if (simulate === 'false') {
194 simulate = false;
195 } else {
196 simulate = 'undef';
197 }
198
199 if (validSimulate(simulate)) {
200 return simulate;
201 }
202 simulate = DEFAULT_SIMULATE;
203 localStorage[LOCAL_STORAGE_TAG_SIMULATE] = simulate;
204 return simulate;
205 }
206
207
208 function setDefaultSimulate(simulate) {
209 if (!validSimulate(simulate)) {
210 simulate = DEFAULT_SIMULATE;
211 }
212 localStorage[LOCAL_STORAGE_TAG_SIMULATE] = simulate;
213 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698