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

Side by Side Diff: lib/io.js

Issue 5731880649359360: Don't delay Firefox startup (Closed)
Patch Set: Fixed wrong callback parameters Created March 14, 2014, 6:08 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/filterStorage.js ('k') | lib/utils.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>, 2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2014 Eyeo GmbH 3 * Copyright (C) 2006-2014 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 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 63
64 /** 64 /**
65 * Reads strings from a file asynchronously, calls listener.process() with 65 * Reads strings from a file asynchronously, calls listener.process() with
66 * each line read and with a null parameter once the read operation is done. 66 * each line read and with a null parameter once the read operation is done.
67 * The callback will be called when the operation is done. 67 * The callback will be called when the operation is done.
68 */ 68 */
69 readFromFile: function(/**nsIFile|nsIURI*/ file, /**Boolean*/ decode, /**Objec t*/ listener, /**Function*/ callback, /**String*/ timeLineID) 69 readFromFile: function(/**nsIFile|nsIURI*/ file, /**Boolean*/ decode, /**Objec t*/ listener, /**Function*/ callback, /**String*/ timeLineID)
70 { 70 {
71 try 71 try
72 { 72 {
73 let processing = false;
73 let buffer = ""; 74 let buffer = "";
75 let loadEvent = null;
76 let errorEvent = null;
74 let uri = file instanceof Ci.nsIFile ? Services.io.newFileURI(file) : file ; 77 let uri = file instanceof Ci.nsIFile ? Services.io.newFileURI(file) : file ;
75 let request = new XMLHttpRequest(); 78 let request = new XMLHttpRequest();
76 request.mozBackgroundRequest = true; 79 request.mozBackgroundRequest = true;
77 request.open("GET", uri.spec); 80 request.open("GET", uri.spec);
78 request.responseType = "moz-chunked-text"; 81 request.responseType = "moz-chunked-text";
79 request.overrideMimeType("text/plain" + (decode ? "? charset=utf-8" : "")) ; 82 request.overrideMimeType("text/plain" + (decode ? "? charset=utf-8" : "")) ;
80 83
81 request.addEventListener("progress", function(event) 84 let onProgress = function(event)
82 { 85 {
83 if (timeLineID) 86 if (timeLineID)
84 { 87 {
85 TimeLine.asyncStart(timeLineID); 88 TimeLine.asyncStart(timeLineID);
86 } 89 }
87 90
88 let data = event.target.response; 91 let data = event.target.response;
89 let index = Math.max(data.lastIndexOf("\n"), data.lastIndexOf("\r")); 92 let index = (processing ? -1 : Math.max(data.lastIndexOf("\n"), data.las tIndexOf("\r")));
90 if (index >= 0) 93 if (index >= 0)
91 { 94 {
92 let oldBuffer = buffer; 95 // Protect against reentrance in case the listener processes events.
93 buffer = data.substr(index + 1); 96 processing = true;
94 data = data.substr(0, index + 1); 97 try
95 let lines = data.split(/[\r\n]+/); 98 {
96 lines.pop(); 99 let oldBuffer = buffer;
97 lines[0] = oldBuffer + lines[0]; 100 buffer = data.substr(index + 1);
98 for (let i = 0; i < lines.length; i++) 101 data = data.substr(0, index + 1);
99 listener.process(lines[i]); 102 let lines = data.split(/[\r\n]+/);
103 lines.pop();
104 lines[0] = oldBuffer + lines[0];
105 for (let i = 0; i < lines.length; i++)
106 listener.process(lines[i]);
107 }
108 finally
109 {
110 processing = false;
111 let e = {
112 target: {response: buffer}
113 };
114 buffer = "";
115 onProgress(e);
116
117 if (loadEvent)
118 {
119 let event = loadEvent;
120 loadEvent = null;
121 onLoad(event);
122 }
123
124 if (errorEvent)
125 {
126 let event = errorEvent;
127 errorEvent = null;
128 onError(event);
129 }
130 }
100 } 131 }
101 else 132 else
102 buffer += data; 133 buffer += data;
103 134
104 if (timeLineID) 135 if (timeLineID)
105 { 136 {
106 TimeLine.asyncEnd(timeLineID); 137 TimeLine.asyncEnd(timeLineID);
107 } 138 }
108 }, false); 139 };
109 140
110 request.addEventListener("load", function(event) 141 let onLoad = function(event)
111 { 142 {
143 if (processing)
144 {
145 // Still processing data, delay processing this event.
146 loadEvent = event;
147 return;
148 }
149
112 if (timeLineID) 150 if (timeLineID)
113 { 151 {
114 TimeLine.asyncStart(timeLineID); 152 TimeLine.asyncStart(timeLineID);
115 } 153 }
116 154
117 if (buffer !== "") 155 if (buffer !== "")
118 listener.process(buffer); 156 listener.process(buffer);
119 listener.process(null); 157 listener.process(null);
120 158
121 if (timeLineID) 159 if (timeLineID)
122 { 160 {
123 TimeLine.asyncEnd(timeLineID); 161 TimeLine.asyncEnd(timeLineID);
124 TimeLine.asyncDone(timeLineID); 162 TimeLine.asyncDone(timeLineID);
125 } 163 }
126 164
127 callback(null); 165 callback(null);
128 }, false); 166 };
129 167
130 request.addEventListener("error", function() 168 let onError = function(event)
131 { 169 {
170 if (processing)
171 {
172 // Still processing data, delay processing this event.
173 errorEvent = event;
174 return;
175 }
176
132 let e = Cc["@mozilla.org/js/xpc/Exception;1"].createInstance(Ci.nsIXPCEx ception); 177 let e = Cc["@mozilla.org/js/xpc/Exception;1"].createInstance(Ci.nsIXPCEx ception);
133 e.initialize("File read operation failed", result, null, Components.stac k, file, null); 178 e.initialize("File read operation failed", result, null, Components.stac k, file, null);
134 callback(e); 179 callback(e);
135 180
136 if (timeLineID) 181 if (timeLineID)
137 { 182 {
138 TimeLine.asyncDone(timeLineID); 183 TimeLine.asyncDone(timeLineID);
139 } 184 }
140 }, false); 185 };
186
187 request.addEventListener("progress", onProgress, false);
188 request.addEventListener("load", onLoad, false);
189 request.addEventListener("error", onError, false);
141 190
142 request.send(null); 191 request.send(null);
143 } 192 }
144 catch (e) 193 catch (e)
145 { 194 {
146 callback(e); 195 callback(e);
147 } 196 }
148 }, 197 },
149 /** 198 /**
150 * Writes string data to a file asynchronously, optionally encodes it into 199 * Writes string data to a file asynchronously, optionally encodes it into
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
330 isFile: exists && file.isFile(), 379 isFile: exists && file.isFile(),
331 lastModified: exists ? file.lastModifiedTime : 0 380 lastModified: exists ? file.lastModifiedTime : 0
332 }); 381 });
333 } 382 }
334 catch(e) 383 catch(e)
335 { 384 {
336 callback(e); 385 callback(e);
337 } 386 }
338 } 387 }
339 } 388 }
OLDNEW
« no previous file with comments | « lib/filterStorage.js ('k') | lib/utils.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld