Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: lib/io.js

Issue 29350042: Issue 4023 - Move storage of subscription lists to localStorage (Closed)
Left Patch Set: Cleanup Created Aug. 25, 2016, 1:57 a.m.
Right Patch Set: Make sure entry["compressed"] is truthful Created Sept. 8, 2016, 11:40 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « chrome/ext/background.js ('k') | lib/lz-string.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
55 { 55 {
56 return new FakeFile(path); 56 return new FakeFile(path);
57 }, 57 },
58 58
59 readFromFile: function(file, listener, callback) 59 readFromFile: function(file, listener, callback)
60 { 60 {
61 function onLoaded(entry) 61 function onLoaded(entry)
62 { 62 {
63 if ("content" in entry) 63 if ("content" in entry)
64 { 64 {
65 if ("compressed" in entry) 65 if (entry["compressed"])
66 entry.content = JSON.parse(LZString.decompressFromUTF16(entry.content) ); 66 entry.content = JSON.parse(LZString.decompressFromUTF16(entry.content) );
67 for (let line of entry.content) 67 for (let line of entry.content)
68 listener.process(line); 68 listener.process(line);
69 } 69 }
70 callback(null); 70 callback(null);
71 } 71 }
72 72
73 loadFile(file).then(onLoaded, callback); 73 loadFile(file).then(onLoaded, callback);
74 }, 74 },
75 75
76 writeToFile: function(file, data, callback) 76 writeToFile: function(file, data, callback)
77 { 77 {
78 let entry = {}; 78 let entry = {};
79 let key = fileToKey(file); 79 let key = fileToKey(file);
80 80
81 // Edge cannot write files larger than 1Mb (500k string in UTF-16).
kzar 2016/08/30 14:13:02 I think this comment should be inside the else blo
Oleksandr 2016/08/31 21:54:37 Done.
82 // However it does not always raise an exception when trying to do so.
83 if (typeof browser == "undefined") 81 if (typeof browser == "undefined")
84 { 82 {
85 entry[key] = {lastModified: Date.now(), 83 entry[key] = {lastModified: Date.now(), content: data};
kzar 2016/08/30 14:13:03 Nit: No need to wrap this into two lines if it wil
Oleksandr 2016/08/31 21:54:37 Done.
86 content: data};
87 ext.storage.set(entry, callback); 84 ext.storage.set(entry, callback);
88 } 85 }
89 else 86 else
90 { 87 {
91 // Compress and fallback to localStorage 88 // Edge cannot write files larger than 1Mb (500k string in UTF-16) via
89 // storage API. However it does not always raise an exception when trying
90 // to do so. The solution is to compress and fallback to localStorage.
92 let processedData = LZString.compressToUTF16(JSON.stringify(data)); 91 let processedData = LZString.compressToUTF16(JSON.stringify(data));
93 ext.storage.remove(key); 92 ext.storage.remove(key);
94 entry[key] = {lastModified: Date.now(), 93 entry[key] = {lastModified: Date.now(), content: processedData,
kzar 2016/08/30 14:13:02 Nit: This could fit on two lines instead of three.
Oleksandr 2016/08/31 21:54:37 Done. However it looked better as it was, IMHO.
kzar 2016/09/01 12:57:37 I can't see the change?
95 content: processedData,
96 compressed: true}; 94 compressed: true};
97 window.localStorage.setItem(key, JSON.stringify(entry[key])); 95 window.localStorage.setItem(key, JSON.stringify(entry[key]));
98 callback(); 96 callback();
99 } 97 }
100 }, 98 },
101 99
102 statFile: function(file, callback) 100 statFile: function(file, callback)
103 { 101 {
104 function onLoaded(entry) 102 function onLoaded(entry)
105 { 103 {
106 callback(null, { 104 callback(null, {
107 exists: true, 105 exists: true,
108 lastModified: entry.lastModified 106 lastModified: entry.lastModified
109 }); 107 });
110 } 108 }
111 109
112 loadFile(file).then(onLoaded, callback); 110 loadFile(file).then(onLoaded, callback);
113 } 111 }
114 }; 112 };
LEFTRIGHT

Powered by Google App Engine
This is Rietveld