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

Side by Side Diff: src/FileSystemJsObject.cpp

Issue 29543810: Issue 5118 - Lock the platform interfaces before use (Closed) Base URL: https://hg.adblockplus.org/libadblockplus/
Patch Set: Last details Created Sept. 13, 2017, 8:17 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 | « src/ConsoleJsObject.cpp ('k') | src/JsEngine.cpp » ('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 <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present 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 29 matching lines...) Expand all
40 v8::Isolate* isolate = arguments.GetIsolate(); 40 v8::Isolate* isolate = arguments.GetIsolate();
41 if (converted.size() != 2) 41 if (converted.size() != 2)
42 return ThrowExceptionInJS(isolate, "_fileSystem.read requires 2 parameters "); 42 return ThrowExceptionInJS(isolate, "_fileSystem.read requires 2 parameters ");
43 if (!converted[1].IsFunction()) 43 if (!converted[1].IsFunction())
44 return ThrowExceptionInJS(isolate, "Second argument to _fileSystem.read mu st be a function"); 44 return ThrowExceptionInJS(isolate, "Second argument to _fileSystem.read mu st be a function");
45 45
46 JsValueList values; 46 JsValueList values;
47 values.push_back(converted[1]); 47 values.push_back(converted[1]);
48 auto weakCallback = jsEngine->StoreJsValues(values); 48 auto weakCallback = jsEngine->StoreJsValues(values);
49 std::weak_ptr<JsEngine> weakJsEngine = jsEngine; 49 std::weak_ptr<JsEngine> weakJsEngine = jsEngine;
50 jsEngine->GetPlatform().GetFileSystem().Read(converted[0].AsString(), 50 auto fileName = converted[0].AsString();
51 [weakJsEngine, weakCallback] 51 jsEngine->GetPlatform().WithFileSystem(
52 (IFileSystem::IOBuffer&& content, const std::string& error) 52 [weakJsEngine, weakCallback, fileName](IFileSystem& fileSystem)
53 { 53 {
54 auto jsEngine = weakJsEngine.lock(); 54 fileSystem.Read(fileName,
55 if (!jsEngine) 55 [weakJsEngine, weakCallback]
56 return; 56 (IFileSystem::IOBuffer&& content, const std::string& error)
57 {
58 auto jsEngine = weakJsEngine.lock();
59 if (!jsEngine)
60 return;
57 61
58 const JsContext context(*jsEngine); 62 const JsContext context(*jsEngine);
59 auto result = jsEngine->NewObject(); 63 auto result = jsEngine->NewObject();
60 result.SetStringBufferProperty("content", std::move(content)); 64 result.SetStringBufferProperty("content", std::move(content));
61 if (!error.empty()) 65 if (!error.empty())
62 result.SetProperty("error", error); 66 result.SetProperty("error", error);
63 jsEngine->TakeJsValues(weakCallback)[0].Call(result); 67 jsEngine->TakeJsValues(weakCallback)[0].Call(result);
68 });
64 }); 69 });
65 } 70 }
66 71
67 inline bool IsEndOfLine(char c) 72 inline bool IsEndOfLine(char c)
68 { 73 {
69 return c == 10 || c == 13; 74 return c == 10 || c == 13;
70 } 75 }
71 76
72 inline StringBuffer::const_iterator SkipEndOfLine(StringBuffer::const_iterator ii, StringBuffer::const_iterator end) 77 inline StringBuffer::const_iterator SkipEndOfLine(StringBuffer::const_iterator ii, StringBuffer::const_iterator end)
73 { 78 {
(...skipping 20 matching lines...) Expand all
94 if (!converted[1].IsFunction()) 99 if (!converted[1].IsFunction())
95 return ThrowExceptionInJS(isolate, "Second argument to _fileSystem.readFro mFile must be a function (listener callback)"); 100 return ThrowExceptionInJS(isolate, "Second argument to _fileSystem.readFro mFile must be a function (listener callback)");
96 if (!converted[2].IsFunction()) 101 if (!converted[2].IsFunction())
97 return ThrowExceptionInJS(isolate, "Third argument to _fileSystem.readFrom File must be a function (done callback)"); 102 return ThrowExceptionInJS(isolate, "Third argument to _fileSystem.readFrom File must be a function (done callback)");
98 103
99 JsValueList values; 104 JsValueList values;
100 values.push_back(converted[1]); 105 values.push_back(converted[1]);
101 values.push_back(converted[2]); 106 values.push_back(converted[2]);
102 auto weakCallback = jsEngine->StoreJsValues(values); 107 auto weakCallback = jsEngine->StoreJsValues(values);
103 std::weak_ptr<JsEngine> weakJsEngine = jsEngine; 108 std::weak_ptr<JsEngine> weakJsEngine = jsEngine;
104 jsEngine->GetPlatform().GetFileSystem().Read(converted[0].AsString(), 109 auto fileName = converted[0].AsString();
105 [weakJsEngine, weakCallback] 110 jsEngine->GetPlatform().WithFileSystem(
106 (IFileSystem::IOBuffer&& content, const std::string& error) 111 [weakJsEngine, weakCallback, fileName](IFileSystem& fileSystem)
107 { 112 {
108 auto jsEngine = weakJsEngine.lock(); 113 fileSystem.Read(fileName,
109 if (!jsEngine) 114 [weakJsEngine, weakCallback]
110 return; 115 (IFileSystem::IOBuffer&& content, const std::string& error)
116 {
117 auto jsEngine = weakJsEngine.lock();
118 if (!jsEngine)
119 return;
111 120
112 const JsContext context(*jsEngine); 121 const JsContext context(*jsEngine);
113 122
114 auto jsValues = jsEngine->TakeJsValues(weakCallback); 123 auto jsValues = jsEngine->TakeJsValues(weakCallback);
115 if (!error.empty()) 124 if (!error.empty())
116 { 125 {
117 jsValues[1].Call(jsEngine->NewValue(error)); 126 jsValues[1].Call(jsEngine->NewValue(error));
118 return; 127 return;
119 } 128 }
120 129
121 auto processFunc = jsValues[0].UnwrapValue().As<v8::Function>(); 130 auto processFunc = jsValues[0].UnwrapValue().As<v8::Function>();
122 131
123 auto globalContext = context.GetV8Context()->Global(); 132 auto globalContext = context.GetV8Context()->Global();
124 if (!globalContext->IsObject()) 133 if (!globalContext->IsObject())
125 throw std::runtime_error("`this` pointer has to be an object"); 134 throw std::runtime_error("`this` pointer has to be an object");
126 135
127 const v8::TryCatch tryCatch; 136 const v8::TryCatch tryCatch;
128 137
129 const auto contentEnd = content.cend(); 138 const auto contentEnd = content.cend();
130 auto stringBegin = SkipEndOfLine(content.begin(), contentEnd); 139 auto stringBegin = SkipEndOfLine(content.begin(), contentEnd);
131 do 140 do
132 { 141 {
133 auto stringEnd = AdvanceToEndOfLine(stringBegin, contentEnd); 142 auto stringEnd = AdvanceToEndOfLine(stringBegin, contentEnd);
134 auto jsLine = Utils::StringBufferToV8String(jsEngine->GetIsolate(), Stri ngBuffer(stringBegin, stringEnd)).As<v8::Value>(); 143 auto jsLine = Utils::StringBufferToV8String(jsEngine->GetIsolate() , StringBuffer(stringBegin, stringEnd)).As<v8::Value>();
135 processFunc->Call(globalContext, 1, &jsLine); 144 processFunc->Call(globalContext, 1, &jsLine);
136 if (tryCatch.HasCaught()) 145 if (tryCatch.HasCaught())
137 { 146 {
138 jsValues[1].Call(jsEngine->NewValue(JsError::ExceptionToString(tryCatc h.Exception(), tryCatch.Message()))); 147 jsValues[1].Call(jsEngine->NewValue(JsError::ExceptionToString(t ryCatch.Exception(), tryCatch.Message())));
139 return; 148 return;
140 } 149 }
141 stringBegin = SkipEndOfLine(stringEnd, contentEnd); 150 stringBegin = SkipEndOfLine(stringEnd, contentEnd);
142 } while (stringBegin != contentEnd); 151 } while (stringBegin != contentEnd);
143 jsValues[1].Call(); 152 jsValues[1].Call();
144 }); 153 });
154 });
145 } 155 }
146 156
147 void WriteCallback(const v8::FunctionCallbackInfo<v8::Value>& arguments) 157 void WriteCallback(const v8::FunctionCallbackInfo<v8::Value>& arguments)
148 { 158 {
149 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments); 159 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments);
150 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments); 160 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments);
151 161
152 v8::Isolate* isolate = arguments.GetIsolate(); 162 v8::Isolate* isolate = arguments.GetIsolate();
153 if (converted.size() != 3) 163 if (converted.size() != 3)
154 return ThrowExceptionInJS(isolate, "_fileSystem.write requires 3 parameter s"); 164 return ThrowExceptionInJS(isolate, "_fileSystem.write requires 3 parameter s");
155 if (!converted[2].IsFunction()) 165 if (!converted[2].IsFunction())
156 return ThrowExceptionInJS(isolate, "Third argument to _fileSystem.write mu st be a function"); 166 return ThrowExceptionInJS(isolate, "Third argument to _fileSystem.write mu st be a function");
157 167
158 JsValueList values; 168 JsValueList values;
159 values.push_back(converted[2]); 169 values.push_back(converted[2]);
160 auto weakCallback = jsEngine->StoreJsValues(values); 170 auto weakCallback = jsEngine->StoreJsValues(values);
161 std::weak_ptr<JsEngine> weakJsEngine = jsEngine; 171 std::weak_ptr<JsEngine> weakJsEngine = jsEngine;
162 auto content = converted[1].AsStringBuffer(); 172 auto content = converted[1].AsStringBuffer();
163 jsEngine->GetPlatform().GetFileSystem().Write(converted[0].AsString(), 173 auto fileName = converted[0].AsString();
164 content, 174 jsEngine->GetPlatform().WithFileSystem(
165 [weakJsEngine, weakCallback](const std::string& error) 175 [weakJsEngine, weakCallback, fileName, content](IFileSystem& fileSystem)
166 { 176 {
167 auto jsEngine = weakJsEngine.lock(); 177 fileSystem.Write(fileName, content,
168 if (!jsEngine) 178 [weakJsEngine, weakCallback](const std::string& error)
169 return; 179 {
180 auto jsEngine = weakJsEngine.lock();
181 if (!jsEngine)
182 return;
170 183
171 const JsContext context(*jsEngine); 184 const JsContext context(*jsEngine);
172 JsValueList params; 185 JsValueList params;
173 if (!error.empty()) 186 if (!error.empty())
174 params.push_back(jsEngine->NewValue(error)); 187 params.push_back(jsEngine->NewValue(error));
175 jsEngine->TakeJsValues(weakCallback)[0].Call(params); 188 jsEngine->TakeJsValues(weakCallback)[0].Call(params);
189 });
176 }); 190 });
177 } 191 }
178 192
179 void MoveCallback(const v8::FunctionCallbackInfo<v8::Value>& arguments) 193 void MoveCallback(const v8::FunctionCallbackInfo<v8::Value>& arguments)
180 { 194 {
181 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments); 195 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments);
182 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments); 196 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments);
183 197
184 v8::Isolate* isolate = arguments.GetIsolate(); 198 v8::Isolate* isolate = arguments.GetIsolate();
185 if (converted.size() != 3) 199 if (converted.size() != 3)
186 return ThrowExceptionInJS(isolate, "_fileSystem.move requires 3 parameters "); 200 return ThrowExceptionInJS(isolate, "_fileSystem.move requires 3 parameters ");
187 if (!converted[2].IsFunction()) 201 if (!converted[2].IsFunction())
188 return ThrowExceptionInJS(isolate, "Third argument to _fileSystem.move mus t be a function"); 202 return ThrowExceptionInJS(isolate, "Third argument to _fileSystem.move mus t be a function");
189 203
190 JsValueList values; 204 JsValueList values;
191 values.push_back(converted[2]); 205 values.push_back(converted[2]);
192 auto weakCallback = jsEngine->StoreJsValues(values); 206 auto weakCallback = jsEngine->StoreJsValues(values);
193 std::weak_ptr<JsEngine> weakJsEngine = jsEngine; 207 std::weak_ptr<JsEngine> weakJsEngine = jsEngine;
194 jsEngine->GetPlatform().GetFileSystem().Move(converted[0].AsString(), 208 auto from = converted[0].AsString();
195 converted[1].AsString(), 209 auto to = converted[1].AsString();
196 [weakJsEngine, weakCallback](const std::string& error) 210 jsEngine->GetPlatform().WithFileSystem(
211 [weakJsEngine, weakCallback, from, to](IFileSystem& fileSystem)
197 { 212 {
198 auto jsEngine = weakJsEngine.lock(); 213 fileSystem.Move(from, to,
199 if (!jsEngine) 214 [weakJsEngine, weakCallback](const std::string& error)
200 return; 215 {
216 auto jsEngine = weakJsEngine.lock();
217 if (!jsEngine)
218 return;
201 219
202 const JsContext context(*jsEngine); 220 const JsContext context(*jsEngine);
203 JsValueList params; 221 JsValueList params;
204 if (!error.empty()) 222 if (!error.empty())
205 params.push_back(jsEngine->NewValue(error)); 223 params.push_back(jsEngine->NewValue(error));
206 jsEngine->TakeJsValues(weakCallback)[0].Call(params); 224 jsEngine->TakeJsValues(weakCallback)[0].Call(params);
225 });
207 }); 226 });
208 } 227 }
209 228
210 void RemoveCallback(const v8::FunctionCallbackInfo<v8::Value>& arguments) 229 void RemoveCallback(const v8::FunctionCallbackInfo<v8::Value>& arguments)
211 { 230 {
212 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments); 231 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments);
213 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments); 232 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments);
214 233
215 v8::Isolate* isolate = arguments.GetIsolate(); 234 v8::Isolate* isolate = arguments.GetIsolate();
216 if (converted.size() != 2) 235 if (converted.size() != 2)
217 return ThrowExceptionInJS(isolate, "_fileSystem.remove requires 2 paramete rs"); 236 return ThrowExceptionInJS(isolate, "_fileSystem.remove requires 2 paramete rs");
218 if (!converted[1].IsFunction()) 237 if (!converted[1].IsFunction())
219 return ThrowExceptionInJS(isolate, "Second argument to _fileSystem.remove must be a function"); 238 return ThrowExceptionInJS(isolate, "Second argument to _fileSystem.remove must be a function");
220 239
221 JsValueList values; 240 JsValueList values;
222 values.push_back(converted[1]); 241 values.push_back(converted[1]);
223 auto weakCallback = jsEngine->StoreJsValues(values); 242 auto weakCallback = jsEngine->StoreJsValues(values);
224 std::weak_ptr<JsEngine> weakJsEngine = jsEngine; 243 std::weak_ptr<JsEngine> weakJsEngine = jsEngine;
225 jsEngine->GetPlatform().GetFileSystem().Remove(converted[0].AsString(), 244 auto fileName = converted[0].AsString();
226 [weakJsEngine, weakCallback](const std::string& error) 245 jsEngine->GetPlatform().WithFileSystem(
246 [weakJsEngine, weakCallback, fileName](IFileSystem& fileSystem)
227 { 247 {
228 auto jsEngine = weakJsEngine.lock(); 248 fileSystem.Remove(fileName,
229 if (!jsEngine) 249 [weakJsEngine, weakCallback](const std::string& error)
230 return; 250 {
251 auto jsEngine = weakJsEngine.lock();
252 if (!jsEngine)
253 return;
231 254
232 const JsContext context(*jsEngine); 255 const JsContext context(*jsEngine);
233 JsValueList params; 256 JsValueList params;
234 if (!error.empty()) 257 if (!error.empty())
235 params.push_back(jsEngine->NewValue(error)); 258 params.push_back(jsEngine->NewValue(error));
236 jsEngine->TakeJsValues(weakCallback)[0].Call(params); 259 jsEngine->TakeJsValues(weakCallback)[0].Call(params);
260 });
237 }); 261 });
238 } 262 }
239 263
240 void StatCallback(const v8::FunctionCallbackInfo<v8::Value>& arguments) 264 void StatCallback(const v8::FunctionCallbackInfo<v8::Value>& arguments)
241 { 265 {
242 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments); 266 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments);
243 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments); 267 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments);
244 268
245 v8::Isolate* isolate = arguments.GetIsolate(); 269 v8::Isolate* isolate = arguments.GetIsolate();
246 if (converted.size() != 2) 270 if (converted.size() != 2)
247 return ThrowExceptionInJS(isolate, "_fileSystem.stat requires 2 parameters "); 271 return ThrowExceptionInJS(isolate, "_fileSystem.stat requires 2 parameters ");
248 if (!converted[1].IsFunction()) 272 if (!converted[1].IsFunction())
249 return ThrowExceptionInJS(isolate, "Second argument to _fileSystem.stat mu st be a function"); 273 return ThrowExceptionInJS(isolate, "Second argument to _fileSystem.stat mu st be a function");
250 274
251 JsValueList values; 275 JsValueList values;
252 values.push_back(converted[1]); 276 values.push_back(converted[1]);
253 auto weakCallback = jsEngine->StoreJsValues(values); 277 auto weakCallback = jsEngine->StoreJsValues(values);
254 std::weak_ptr<JsEngine> weakJsEngine = jsEngine; 278 std::weak_ptr<JsEngine> weakJsEngine = jsEngine;
255 jsEngine->GetPlatform().GetFileSystem().Stat(converted[0].AsString(), 279 auto fileName = converted[0].AsString();
256 [weakJsEngine, weakCallback] 280 jsEngine->GetPlatform().WithFileSystem(
257 (const IFileSystem::StatResult& statResult, const std::string& error) 281 [weakJsEngine, weakCallback, fileName](IFileSystem& fileSystem)
258 { 282 {
259 auto jsEngine = weakJsEngine.lock(); 283 fileSystem.Stat(fileName,
260 if (!jsEngine) 284 [weakJsEngine, weakCallback]
261 return; 285 (const IFileSystem::StatResult& statResult, const std::string& error)
286 {
287 auto jsEngine = weakJsEngine.lock();
288 if (!jsEngine)
289 return;
262 290
263 const JsContext context(*jsEngine); 291 const JsContext context(*jsEngine);
264 auto result = jsEngine->NewObject(); 292 auto result = jsEngine->NewObject();
265 293
266 result.SetProperty("exists", statResult.exists); 294 result.SetProperty("exists", statResult.exists);
267 result.SetProperty("lastModified", statResult.lastModified); 295 result.SetProperty("lastModified", statResult.lastModified);
268 if (!error.empty()) 296 if (!error.empty())
269 result.SetProperty("error", error); 297 result.SetProperty("error", error);
270 298
271 JsValueList params; 299 JsValueList params;
272 params.push_back(result); 300 params.push_back(result);
273 jsEngine->TakeJsValues(weakCallback)[0].Call(params); 301 jsEngine->TakeJsValues(weakCallback)[0].Call(params);
302 });
274 }); 303 });
275 } 304 }
276 } 305 }
277 306
278 307
279 JsValue& FileSystemJsObject::Setup(JsEngine& jsEngine, JsValue& obj) 308 JsValue& FileSystemJsObject::Setup(JsEngine& jsEngine, JsValue& obj)
280 { 309 {
281 obj.SetProperty("read", jsEngine.NewCallback(::ReadCallback)); 310 obj.SetProperty("read", jsEngine.NewCallback(::ReadCallback));
282 obj.SetProperty("readFromFile", jsEngine.NewCallback(::ReadFromFileCallback)); 311 obj.SetProperty("readFromFile", jsEngine.NewCallback(::ReadFromFileCallback));
283 obj.SetProperty("write", jsEngine.NewCallback(::WriteCallback)); 312 obj.SetProperty("write", jsEngine.NewCallback(::WriteCallback));
284 obj.SetProperty("move", jsEngine.NewCallback(::MoveCallback)); 313 obj.SetProperty("move", jsEngine.NewCallback(::MoveCallback));
285 obj.SetProperty("remove", jsEngine.NewCallback(::RemoveCallback)); 314 obj.SetProperty("remove", jsEngine.NewCallback(::RemoveCallback));
286 obj.SetProperty("stat", jsEngine.NewCallback(::StatCallback)); 315 obj.SetProperty("stat", jsEngine.NewCallback(::StatCallback));
287 return obj; 316 return obj;
288 } 317 }
OLDNEW
« no previous file with comments | « src/ConsoleJsObject.cpp ('k') | src/JsEngine.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld