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

Side by Side Diff: lib/io.js

Issue 29339112: Issue 3716 - Split up files stored in storage.local (Closed)
Patch Set: Remove setMultiple. Use suggested code. Created March 31, 2016, 9:51 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
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 16 matching lines...) Expand all
27 let key = fileToKey(file); 27 let key = fileToKey(file);
28 28
29 ext.storage.get([key], function(items) 29 ext.storage.get([key], function(items)
30 { 30 {
31 let entry = items[key]; 31 let entry = items[key];
32 32
33 if (entry) 33 if (entry)
34 successCallback(entry); 34 successCallback(entry);
35 else 35 else
36 errorCallback(new Error("File doesn't exist")); 36 errorCallback(new Error("File doesn't exist"));
37 }); 37 });
Sebastian Noack 2016/03/31 12:36:54 While changing most of the code in here, this is a
38 } 38 }
39 39
40 // Save the file, splitting it into chunks, if required.
40 function saveFile(file, data, callback) 41 function saveFile(file, data, callback)
Sebastian Noack 2016/03/31 12:36:53 Since this function is only used by writeToFile()
41 { 42 {
42 ext.storage.set( 43 let key = fileToKey(file);
43 fileToKey(file), 44 let items = {};
45 items[key] = {lastModified: Date.now()};
46
47 if (typeof browser == "object")
Sebastian Noack 2016/03/31 12:36:54 A comment that this accounts for Edge and how the
Sebastian Noack 2016/03/31 14:08:53 I just noticed that we potentially leak chunks whe
48 {
49 let quota = 1024 * 1024 / 2 - 1000;
50 let chunks = [];
51 let chunk = [];
52 let chunkSize = 0;
53
54 for (let line of data)
44 { 55 {
45 content: data, 56 if (line.length + chunkSize > quota)
46 lastModified: Date.now() 57 {
47 }, 58 chunks.push(chunk);
48 callback 59 chunk = [];
49 ); 60 chunkSize = 0;
61 }
62
63 chunk.push(line);
64 chunkSize += line.length;
65 }
66 chunks.push(chunk);
67
68 if (chunks.length > 1)
69 {
70 for (let i = 0; i < chunks.length; i++)
71 items[key + ":" + i] = chunks[i];
72 items[key].chunks = chunks.length;
73 ext.storage.set(items, callback);
74 return;
75 }
76 }
77
78 items[key].content = data;
79 ext.storage.set(items, callback);
50 } 80 }
51 81
52 exports.IO = 82 exports.IO =
53 { 83 {
54 resolveFilePath: function(path) 84 resolveFilePath: function(path)
55 { 85 {
56 return new FakeFile(path); 86 return new FakeFile(path);
57 }, 87 },
58 88
59 readFromFile: function(file, listener, callback) 89 readFromFile: function(file, listener, callback)
60 { 90 {
61 function onLoaded(entry) 91 function onLoaded(entry)
62 { 92 {
63 for (let line of entry.content) 93 if ("content" in entry)
64 listener.process(line); 94 {
95 for (let line of entry.content)
96 listener.process(line);
65 97
66 listener.process(null); 98 listener.process(null);
67 callback(null); 99 callback(null);
100 }
101 else
102 {
103 let keys = [];
104 for (let i = 0; i < entry.chunks; i++)
105 keys.push(fileToKey(file) + ":" + i);
Sebastian Noack 2016/03/31 12:36:54 How about moving the logic to create those keys in
106
107 ext.storage.get(keys, items =>
108 {
109 for (let key of keys)
110 for (let line of items[key])
111 listener.process(line);
112
113 listener.process(null);
114 callback(null);
115 });
116 }
68 } 117 }
69 118
70 loadFile(file, onLoaded, callback); 119 loadFile(file, onLoaded, callback);
71 }, 120 },
72 121
73 writeToFile: function(file, data, callback) 122 writeToFile: function(file, data, callback)
74 { 123 {
75 saveFile(file, data, callback); 124 saveFile(file, data, callback);
76 }, 125 },
77 126
78 copyFile: function(fromFile, toFile, callback)
79 {
80 function onLoaded(entry)
81 {
82 saveFile(toFile, entry.content, callback);
83 }
84
85 loadFile(fromFile, onLoaded, callback);
86 },
87
88 renameFile: function(fromFile, newName, callback)
89 {
90 function onLoaded()
91 {
92 ext.storage.remove(fileToKey(fromFile), function()
93 {
94 ext.storage.set(keyPrefix + newName, entry, callback);
95 });
96 }
97
98 loadFile(fromFile, onLoaded, callback);
99 },
100
101 removeFile: function(file, callback)
102 {
103 ext.storage.remove(fileToKey(file), callback);
104 },
105
106 statFile: function(file, callback) 127 statFile: function(file, callback)
107 { 128 {
108 function onLoaded(entry) 129 function onLoaded(entry)
109 { 130 {
110 callback(null, { 131 callback(null, {
111 exists: true, 132 exists: true,
112 lastModified: entry.lastModified 133 lastModified: entry.lastModified
113 }); 134 });
114 } 135 }
115 136
116 loadFile(file, onLoaded, callback); 137 loadFile(file, onLoaded, callback);
117 } 138 }
118 }; 139 };
OLDNEW

Powered by Google App Engine
This is Rietveld