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

Side by Side Diff: lib/io.js

Issue 6287762160353280: Fixed: patterns.ini contents are being processed multiple times (Closed)
Patch Set: Fixed typo Created March 16, 2014, 9:40 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 | « no previous file | no next file » | 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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 /** 18 /**
19 * @fileOverview Module containing file I/O helpers. 19 * @fileOverview Module containing file I/O helpers.
20 */ 20 */
21 21
22 Cu.import("resource://gre/modules/XPCOMUtils.jsm"); 22 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
23 Cu.import("resource://gre/modules/Services.jsm"); 23 Cu.import("resource://gre/modules/Services.jsm");
24 Cu.import("resource://gre/modules/FileUtils.jsm"); 24 Cu.import("resource://gre/modules/FileUtils.jsm");
25 Cu.import("resource://gre/modules/NetUtil.jsm"); 25 Cu.import("resource://gre/modules/NetUtil.jsm");
26 26
27 let {TimeLine} = require("timeline"); 27 let {TimeLine} = require("timeline");
28 let {Utils} = require("utils");
28 29
29 let IO = exports.IO = 30 let IO = exports.IO =
30 { 31 {
31 /** 32 /**
32 * Retrieves the platform-dependent line break string. 33 * Retrieves the platform-dependent line break string.
33 */ 34 */
34 get lineBreak() 35 get lineBreak()
35 { 36 {
36 let lineBreak = (Services.appinfo.OS == "WINNT" ? "\r\n" : "\n"); 37 let lineBreak = (Services.appinfo.OS == "WINNT" ? "\r\n" : "\n");
37 delete IO.lineBreak; 38 delete IO.lineBreak;
(...skipping 27 matching lines...) Expand all
65 * Reads strings from a file asynchronously, calls listener.process() with 66 * 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. 67 * 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. 68 * The callback will be called when the operation is done.
68 */ 69 */
69 readFromFile: function(/**nsIFile|nsIURI*/ file, /**Boolean*/ decode, /**Objec t*/ listener, /**Function*/ callback, /**String*/ timeLineID) 70 readFromFile: function(/**nsIFile|nsIURI*/ file, /**Boolean*/ decode, /**Objec t*/ listener, /**Function*/ callback, /**String*/ timeLineID)
70 { 71 {
71 try 72 try
72 { 73 {
73 let processing = false; 74 let processing = false;
74 let buffer = ""; 75 let buffer = "";
75 let loadEvent = null; 76 let loaded = false;
76 let errorEvent = null; 77 let error = Cr.NS_OK;
77 let uri = file instanceof Ci.nsIFile ? Services.io.newFileURI(file) : file ; 78 let uri = file instanceof Ci.nsIFile ? Services.io.newFileURI(file) : file ;
78 let request = new XMLHttpRequest(); 79 let request = new XMLHttpRequest();
79 request.mozBackgroundRequest = true; 80 request.mozBackgroundRequest = true;
80 request.open("GET", uri.spec); 81 request.open("GET", uri.spec);
81 request.responseType = "moz-chunked-text"; 82 request.responseType = "moz-chunked-text";
82 request.overrideMimeType("text/plain" + (decode ? "? charset=utf-8" : "")) ; 83 request.overrideMimeType("text/plain" + (decode ? "? charset=utf-8" : "")) ;
83 84
84 let onProgress = function(event) 85 let onProgress = function(data)
85 { 86 {
86 if (timeLineID) 87 if (timeLineID)
87 { 88 {
88 TimeLine.asyncStart(timeLineID); 89 TimeLine.asyncStart(timeLineID);
89 } 90 }
90 91
91 let data = event.target.response;
92 let index = (processing ? -1 : Math.max(data.lastIndexOf("\n"), data.las tIndexOf("\r"))); 92 let index = (processing ? -1 : Math.max(data.lastIndexOf("\n"), data.las tIndexOf("\r")));
93 if (index >= 0) 93 if (index >= 0)
94 { 94 {
95 // Protect against reentrance in case the listener processes events. 95 // Protect against reentrance in case the listener processes events.
96 processing = true; 96 processing = true;
97 try 97 try
98 { 98 {
99 let oldBuffer = buffer; 99 let oldBuffer = buffer;
100 buffer = data.substr(index + 1); 100 buffer = data.substr(index + 1);
101 data = data.substr(0, index + 1); 101 data = data.substr(0, index + 1);
102 let lines = data.split(/[\r\n]+/); 102 let lines = data.split(/[\r\n]+/);
103 lines.pop(); 103 lines.pop();
104 lines[0] = oldBuffer + lines[0]; 104 lines[0] = oldBuffer + lines[0];
105 for (let i = 0; i < lines.length; i++) 105 for (let i = 0; i < lines.length; i++)
106 listener.process(lines[i]); 106 listener.process(lines[i]);
107 } 107 }
108 finally 108 finally
109 { 109 {
110 processing = false; 110 processing = false;
111 let e = { 111 data = buffer;
112 target: {response: buffer}
113 };
114 buffer = ""; 112 buffer = "";
115 onProgress(e); 113 onProgress(data);
116 114
117 if (loadEvent) 115 if (loaded)
118 { 116 {
119 let event = loadEvent; 117 loaded = false;
120 loadEvent = null; 118 onLoad();
121 onLoad(event);
122 } 119 }
123 120
124 if (errorEvent) 121 if (error != Cr.NS_OK)
125 { 122 {
126 let event = errorEvent; 123 let param = error;
127 errorEvent = null; 124 error = Cr.NS_OK;
128 onError(event); 125 onError(param);
129 } 126 }
130 } 127 }
131 } 128 }
132 else 129 else
133 buffer += data; 130 buffer += data;
134 131
135 if (timeLineID) 132 if (timeLineID)
136 { 133 {
137 TimeLine.asyncEnd(timeLineID); 134 TimeLine.asyncEnd(timeLineID);
138 } 135 }
139 }; 136 };
140 137
141 let onLoad = function(event) 138 let onLoad = function()
142 { 139 {
143 if (processing) 140 if (processing)
144 { 141 {
145 // Still processing data, delay processing this event. 142 // Still processing data, delay processing this event.
146 loadEvent = event; 143 loaded = true;
147 return; 144 return;
148 } 145 }
149 146
150 if (timeLineID) 147 if (timeLineID)
151 { 148 {
152 TimeLine.asyncStart(timeLineID); 149 TimeLine.asyncStart(timeLineID);
153 } 150 }
154 151
155 if (buffer !== "") 152 if (buffer !== "")
156 listener.process(buffer); 153 listener.process(buffer);
157 listener.process(null); 154 listener.process(null);
158 155
159 if (timeLineID) 156 if (timeLineID)
160 { 157 {
161 TimeLine.asyncEnd(timeLineID); 158 TimeLine.asyncEnd(timeLineID);
162 TimeLine.asyncDone(timeLineID); 159 TimeLine.asyncDone(timeLineID);
163 } 160 }
164 161
165 callback(null); 162 callback(null);
166 }; 163 };
167 164
168 let onError = function(event) 165 let onError = function(result)
169 { 166 {
170 if (processing) 167 if (processing)
171 { 168 {
172 // Still processing data, delay processing this event. 169 // Still processing data, delay processing this event.
173 errorEvent = event; 170 error = result;
174 return; 171 return;
175 } 172 }
176 173
177 let e = Cc["@mozilla.org/js/xpc/Exception;1"].createInstance(Ci.nsIXPCEx ception); 174 let e = Cc["@mozilla.org/js/xpc/Exception;1"].createInstance(Ci.nsIXPCEx ception);
178 e.initialize("File read operation failed", result, null, Components.stac k, file, null); 175 e.initialize("File read operation failed", result, null, Components.stac k, file, null);
179 callback(e); 176 callback(e);
180 177
181 if (timeLineID) 178 if (timeLineID)
182 { 179 {
183 TimeLine.asyncDone(timeLineID); 180 TimeLine.asyncDone(timeLineID);
184 } 181 }
185 }; 182 };
186 183
187 request.addEventListener("progress", onProgress, false); 184 request.addEventListener("progress", function(event)
188 request.addEventListener("load", onLoad, false); 185 {
189 request.addEventListener("error", onError, false); 186 Utils.runAsync(onProgress.bind(this, event.target.response));
187 }, false);
188 request.addEventListener("load", function(event)
189 {
190 Utils.runAsync(onLoad.bind(this));
191 }, false);
192 request.addEventListener("error", function(event)
193 {
194 Utils.runAsync(onError.bind(this, event.result));
195 }, false);
190 196
191 request.send(null); 197 request.send(null);
192 } 198 }
193 catch (e) 199 catch (e)
194 { 200 {
195 callback(e); 201 callback(e);
196 } 202 }
197 }, 203 },
198 /** 204 /**
199 * Writes string data to a file asynchronously, optionally encodes it into 205 * Writes string data to a file asynchronously, optionally encodes it into
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
379 isFile: exists && file.isFile(), 385 isFile: exists && file.isFile(),
380 lastModified: exists ? file.lastModifiedTime : 0 386 lastModified: exists ? file.lastModifiedTime : 0
381 }); 387 });
382 } 388 }
383 catch(e) 389 catch(e)
384 { 390 {
385 callback(e); 391 callback(e);
386 } 392 }
387 } 393 }
388 } 394 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld