OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2015 Eyeo GmbH | 3 * Copyright (C) 2006-2015 Eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 27 matching lines...) Expand all Loading... |
38 defaults.show_statsinicon = true; | 38 defaults.show_statsinicon = true; |
39 defaults.show_statsinpopup = true; | 39 defaults.show_statsinpopup = true; |
40 defaults.shouldShowBlockElementMenu = true; | 40 defaults.shouldShowBlockElementMenu = true; |
41 defaults.hidePlaceholders = true; | 41 defaults.hidePlaceholders = true; |
42 | 42 |
43 let listeners = []; | 43 let listeners = []; |
44 | 44 |
45 function defineProperty(key) | 45 function defineProperty(key) |
46 { | 46 { |
47 let value = null; | 47 let value = null; |
48 Prefs.__defineGetter__(key, function() | 48 Object.defineProperty(Prefs, key, { |
49 { | 49 get: function() |
50 if (value === null) | |
51 { | 50 { |
52 if (key in ext.storage) | 51 if (value === null) |
53 { | 52 { |
54 try | 53 if (key in ext.storage) |
55 { | 54 { |
56 value = JSON.parse(ext.storage[key]); | 55 try |
| 56 { |
| 57 value = JSON.parse(ext.storage[key]); |
| 58 } |
| 59 catch(e) |
| 60 { |
| 61 Cu.reportError(e); |
| 62 } |
57 } | 63 } |
58 catch(e) | 64 |
59 { | 65 if (value === null) |
60 Cu.reportError(e); | 66 value = JSON.parse(JSON.stringify(defaults[key])); |
61 } | |
62 } | 67 } |
| 68 return value; |
| 69 }, |
| 70 set: function(newValue) |
| 71 { |
| 72 if (typeof newValue != typeof defaults[key]) |
| 73 throw new Error("Attempt to change preference type"); |
63 | 74 |
64 if (value === null) | 75 let stringified = JSON.stringify(newValue); |
65 value = JSON.parse(JSON.stringify(defaults[key])); | 76 if (stringified != JSON.stringify(defaults[key])) |
66 } | 77 ext.storage[key] = stringified; |
67 return value; | 78 else |
68 }); | 79 delete ext.storage[key]; |
69 Prefs.__defineSetter__(key, function(newValue) | |
70 { | |
71 if (typeof newValue != typeof defaults[key]) | |
72 throw new Error("Attempt to change preference type"); | |
73 | 80 |
74 let stringified = JSON.stringify(newValue); | 81 value = newValue; |
75 if (stringified != JSON.stringify(defaults[key])) | |
76 ext.storage[key] = stringified; | |
77 else | |
78 delete ext.storage[key]; | |
79 | 82 |
80 value = newValue; | 83 for (let listener of listeners) |
| 84 listener(key); |
81 | 85 |
82 for (let listener of listeners) | 86 return value; |
83 listener(key); | 87 }, |
84 | 88 enumerable: true |
85 return value; | |
86 }); | 89 }); |
87 } | 90 } |
88 | 91 |
89 | 92 |
90 let Prefs = exports.Prefs = { | 93 let Prefs = exports.Prefs = { |
91 addListener: function(listener) | 94 addListener: function(listener) |
92 { | 95 { |
93 if (listeners.indexOf(listener) < 0) | 96 if (listeners.indexOf(listener) < 0) |
94 listeners.push(listener); | 97 listeners.push(listener); |
95 }, | 98 }, |
96 | 99 |
97 removeListener: function(listener) | 100 removeListener: function(listener) |
98 { | 101 { |
99 let index = listeners.indexOf(listener); | 102 let index = listeners.indexOf(listener); |
100 if (index >= 0) | 103 if (index >= 0) |
101 listeners.splice(index, 1); | 104 listeners.splice(index, 1); |
102 }, | 105 }, |
103 }; | 106 }; |
104 | 107 |
105 for (let key in defaults) | 108 for (let key in defaults) |
106 defineProperty(key); | 109 defineProperty(key); |
OLD | NEW |