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: Created Aug. 22, 2016, 9:24 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 21 matching lines...) Expand all
32 ext.storage.get([key], items => 32 ext.storage.get([key], items =>
33 { 33 {
34 let entry = items[key]; 34 let entry = items[key];
35 if (!entry) 35 if (!entry)
36 { 36 {
37 // Check in localStorage 37 // Check in localStorage
38 try 38 try
39 { 39 {
40 entry = JSON.parse(window.localStorage.getItem(key)); 40 entry = JSON.parse(window.localStorage.getItem(key));
41 } 41 }
42 catch(err) 42 catch(err) {}
43 {
44 entry = null;
kzar 2016/08/22 15:38:32 Nit: `entry = null` seems redundant since we alrea
Oleksandr 2016/08/31 21:54:35 Done.
45 }
46 } 43 }
47 if (entry) 44 if (entry)
48 resolve(entry); 45 resolve(entry);
49 else 46 else
50 {
kzar 2016/08/22 15:38:32 Nit: Please remove the braces for the else clause.
Oleksandr 2016/08/31 21:54:35 Done.
51 reject(new Error("File doesn't exist")); 47 reject(new Error("File doesn't exist"));
52 }
53 }); 48 });
54 }); 49 });
55 } 50 }
56 51
57 exports.IO = 52 exports.IO =
58 { 53 {
59 resolveFilePath: function(path) 54 resolveFilePath: function(path)
60 { 55 {
61 return new FakeFile(path); 56 return new FakeFile(path);
62 }, 57 },
63 58
64 readFromFile: function(file, listener, callback) 59 readFromFile: function(file, listener, callback)
65 { 60 {
66 function onLoaded(entry) 61 function onLoaded(entry)
67 { 62 {
68 if ("content" in entry) 63 if ("content" in entry)
69 { 64 {
70 if ("compressed" in entry) 65 if (entry["compressed"])
71 entry.content = JSON.parse(LZString.decompressFromUTF16(entry.content) ); 66 entry.content = JSON.parse(LZString.decompressFromUTF16(entry.content) );
72 for (let line of entry.content) 67 for (let line of entry.content)
73 listener.process(line); 68 listener.process(line);
74 } 69 }
75 callback(null); 70 callback(null);
76 } 71 }
77 72
78 loadFile(file).then(onLoaded, callback); 73 loadFile(file).then(onLoaded, callback);
79 }, 74 },
80 75
81 writeToFile: function(file, data, callback) 76 writeToFile: function(file, data, callback)
82 { 77 {
83 let entry = {}; 78 let entry = {};
84 var key = fileToKey(file); 79 let key = fileToKey(file);
kzar 2016/08/22 15:38:32 You seem to use let and var inconsistently through
Oleksandr 2016/08/31 21:54:35 Done.
85 80
86 // Edge cannot write files larger than 1Mb (500k string in UTF-16).
87 // However it does not always raise an exception when trying to do so.
88 if (typeof browser == "undefined") 81 if (typeof browser == "undefined")
kzar 2016/08/22 15:38:32 Since this code is for the adblockplusedge reposit
Sebastian Noack 2016/08/22 16:49:19 As discussed on IRC, this should go into a bookmar
Oleksandr 2016/08/31 21:54:36 Acknowledged.
89 { 82 {
90 entry[key] = {lastModified: Date.now(), 83 entry[key] = {lastModified: Date.now(), content: data};
91 content: data};
92 ext.storage.set(entry, callback); 84 ext.storage.set(entry, callback);
93 } 85 }
94 else 86 else
95 { 87 {
96 // Compress and fallback to localStorage 88 // Edge cannot write files larger than 1Mb (500k string in UTF-16) via
97 var dataString = JSON.stringify(data); 89 // storage API. However it does not always raise an exception when trying
kzar 2016/08/22 15:38:32 Perhaps do the JSON serialization and compression
Oleksandr 2016/08/31 21:54:35 Done.
98 var processedData = LZString.compressToUTF16(dataString); 90 // to do so. The solution is to compress and fallback to localStorage.
99 chrome.storage.local.remove(key); 91 let processedData = LZString.compressToUTF16(JSON.stringify(data));
kzar 2016/08/22 15:38:32 Did you mean to use chrome.storage instead of ext.
Oleksandr 2016/08/31 21:54:36 Done.
100 entry[key] = {lastModified: Date.now(), 92 ext.storage.remove(key);
101 content: processedData, 93 entry[key] = {lastModified: Date.now(), content: processedData,
102 compressed: true}; 94 compressed: true};
103 window.localStorage.setItem(key, JSON.stringify(entry[key])); 95 window.localStorage.setItem(key, JSON.stringify(entry[key]));
104 callback(); 96 callback();
105 } 97 }
106 }, 98 },
107 99
108 statFile: function(file, callback) 100 statFile: function(file, callback)
109 { 101 {
110 function onLoaded(entry) 102 function onLoaded(entry)
111 { 103 {
112 callback(null, { 104 callback(null, {
113 exists: true, 105 exists: true,
114 lastModified: entry.lastModified 106 lastModified: entry.lastModified
115 }); 107 });
116 } 108 }
117 109
118 loadFile(file).then(onLoaded, callback); 110 loadFile(file).then(onLoaded, callback);
119 } 111 }
120 }; 112 };
LEFTRIGHT

Powered by Google App Engine
This is Rietveld