OLD | NEW |
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-2017 eyeo GmbH | 3 * Copyright (C) 2006-2017 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 15 matching lines...) Expand all Loading... |
26 #include "Thread.h" | 26 #include "Thread.h" |
27 #include "Utils.h" | 27 #include "Utils.h" |
28 | 28 |
29 using namespace AdblockPlus; | 29 using namespace AdblockPlus; |
30 | 30 |
31 namespace | 31 namespace |
32 { | 32 { |
33 class IoThread : public Thread | 33 class IoThread : public Thread |
34 { | 34 { |
35 public: | 35 public: |
36 IoThread(JsEnginePtr jsEngine, JsValuePtr callback) | 36 IoThread(const JsEnginePtr& jsEngine, const JsConstValuePtr& callback) |
37 : Thread(true), jsEngine(jsEngine), fileSystem(jsEngine->GetFileSystem()), | 37 : Thread(true), jsEngine(jsEngine), fileSystem(jsEngine->GetFileSystem()), |
38 callback(callback) | 38 callback(callback) |
39 { | 39 { |
40 } | 40 } |
41 | 41 |
42 protected: | 42 protected: |
43 JsEnginePtr jsEngine; | 43 JsEnginePtr jsEngine; |
44 FileSystemPtr fileSystem; | 44 FileSystemPtr fileSystem; |
45 JsValuePtr callback; | 45 JsConstValuePtr callback; |
46 }; | 46 }; |
47 | 47 |
48 class ReadThread : public IoThread | 48 class ReadThread : public IoThread |
49 { | 49 { |
50 public: | 50 public: |
51 ReadThread(JsEnginePtr jsEngine, JsValuePtr callback, | 51 ReadThread(const JsEnginePtr& jsEngine, const JsConstValuePtr& callback, |
52 const std::string& path) | 52 const std::string& path) |
53 : IoThread(jsEngine, callback), path(path) | 53 : IoThread(jsEngine, callback), path(path) |
54 { | 54 { |
55 } | 55 } |
56 | 56 |
57 void Run() | 57 void Run() |
58 { | 58 { |
59 std::string content; | 59 std::string content; |
60 std::string error; | 60 std::string error; |
61 try | 61 try |
(...skipping 19 matching lines...) Expand all Loading... |
81 callback->Call(params); | 81 callback->Call(params); |
82 } | 82 } |
83 | 83 |
84 private: | 84 private: |
85 std::string path; | 85 std::string path; |
86 }; | 86 }; |
87 | 87 |
88 class WriteThread : public IoThread | 88 class WriteThread : public IoThread |
89 { | 89 { |
90 public: | 90 public: |
91 WriteThread(JsEnginePtr jsEngine, JsValuePtr callback, | 91 WriteThread(const JsEnginePtr& jsEngine, const JsConstValuePtr& callback, |
92 const std::string& path, const std::string& content) | 92 const std::string& path, const std::string& content) |
93 : IoThread(jsEngine, callback), path(path), content(content) | 93 : IoThread(jsEngine, callback), path(path), content(content) |
94 { | 94 { |
95 } | 95 } |
96 | 96 |
97 void Run() | 97 void Run() |
98 { | 98 { |
99 std::string error; | 99 std::string error; |
100 try | 100 try |
101 { | 101 { |
(...skipping 18 matching lines...) Expand all Loading... |
120 } | 120 } |
121 | 121 |
122 private: | 122 private: |
123 std::string path; | 123 std::string path; |
124 std::string content; | 124 std::string content; |
125 }; | 125 }; |
126 | 126 |
127 class MoveThread : public IoThread | 127 class MoveThread : public IoThread |
128 { | 128 { |
129 public: | 129 public: |
130 MoveThread(JsEnginePtr jsEngine, JsValuePtr callback, | 130 MoveThread(const JsEnginePtr& jsEngine, const JsConstValuePtr& callback, |
131 const std::string& fromPath, const std::string& toPath) | 131 const std::string& fromPath, const std::string& toPath) |
132 : IoThread(jsEngine, callback), fromPath(fromPath), toPath(toPath) | 132 : IoThread(jsEngine, callback), fromPath(fromPath), toPath(toPath) |
133 { | 133 { |
134 } | 134 } |
135 | 135 |
136 void Run() | 136 void Run() |
137 { | 137 { |
138 std::string error; | 138 std::string error; |
139 try | 139 try |
140 { | 140 { |
(...skipping 16 matching lines...) Expand all Loading... |
157 } | 157 } |
158 | 158 |
159 private: | 159 private: |
160 std::string fromPath; | 160 std::string fromPath; |
161 std::string toPath; | 161 std::string toPath; |
162 }; | 162 }; |
163 | 163 |
164 class RemoveThread : public IoThread | 164 class RemoveThread : public IoThread |
165 { | 165 { |
166 public: | 166 public: |
167 RemoveThread(JsEnginePtr jsEngine, JsValuePtr callback, | 167 RemoveThread(const JsEnginePtr& jsEngine, const JsConstValuePtr& callback, |
168 const std::string& path) | 168 const std::string& path) |
169 : IoThread(jsEngine, callback), path(path) | 169 : IoThread(jsEngine, callback), path(path) |
170 { | 170 { |
171 } | 171 } |
172 | 172 |
173 void Run() | 173 void Run() |
174 { | 174 { |
175 std::string error; | 175 std::string error; |
176 try | 176 try |
177 { | 177 { |
(...skipping 16 matching lines...) Expand all Loading... |
194 } | 194 } |
195 | 195 |
196 private: | 196 private: |
197 std::string path; | 197 std::string path; |
198 }; | 198 }; |
199 | 199 |
200 | 200 |
201 class StatThread : public IoThread | 201 class StatThread : public IoThread |
202 { | 202 { |
203 public: | 203 public: |
204 StatThread(JsEnginePtr jsEngine, JsValuePtr callback, | 204 StatThread(const JsEnginePtr& jsEngine, const JsConstValuePtr& callback, |
205 const std::string& path) | 205 const std::string& path) |
206 : IoThread(jsEngine, callback), path(path) | 206 : IoThread(jsEngine, callback), path(path) |
207 { | 207 { |
208 } | 208 } |
209 | 209 |
210 void Run() | 210 void Run() |
211 { | 211 { |
212 std::string error; | 212 std::string error; |
213 FileSystem::StatResult statResult; | 213 FileSystem::StatResult statResult; |
214 try | 214 try |
(...skipping 22 matching lines...) Expand all Loading... |
237 callback->Call(params); | 237 callback->Call(params); |
238 } | 238 } |
239 | 239 |
240 private: | 240 private: |
241 std::string path; | 241 std::string path; |
242 }; | 242 }; |
243 | 243 |
244 v8::Handle<v8::Value> ReadCallback(const v8::Arguments& arguments) | 244 v8::Handle<v8::Value> ReadCallback(const v8::Arguments& arguments) |
245 { | 245 { |
246 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg
uments); | 246 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg
uments); |
247 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments); | 247 AdblockPlus::JsConstValueList converted = jsEngine->ConvertArguments(argumen
ts); |
248 | 248 |
249 v8::Isolate* isolate = arguments.GetIsolate(); | 249 v8::Isolate* isolate = arguments.GetIsolate(); |
250 if (converted.size() != 2) | 250 if (converted.size() != 2) |
251 return v8::ThrowException(Utils::ToV8String(isolate, | 251 return v8::ThrowException(Utils::ToV8String(isolate, |
252 "_fileSystem.read requires 2 parameters")); | 252 "_fileSystem.read requires 2 parameters")); |
253 if (!converted[1]->IsFunction()) | 253 if (!converted[1]->IsFunction()) |
254 return v8::ThrowException(Utils::ToV8String(isolate, | 254 return v8::ThrowException(Utils::ToV8String(isolate, |
255 "Second argument to _fileSystem.read must be a function")); | 255 "Second argument to _fileSystem.read must be a function")); |
256 ReadThread* const readThread = new ReadThread(jsEngine, converted[1], | 256 ReadThread* const readThread = new ReadThread(jsEngine, converted[1], |
257 converted[0]->AsString()); | 257 converted[0]->AsString()); |
258 readThread->Start(); | 258 readThread->Start(); |
259 return v8::Undefined(); | 259 return v8::Undefined(); |
260 } | 260 } |
261 | 261 |
262 v8::Handle<v8::Value> WriteCallback(const v8::Arguments& arguments) | 262 v8::Handle<v8::Value> WriteCallback(const v8::Arguments& arguments) |
263 { | 263 { |
264 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg
uments); | 264 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg
uments); |
265 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments); | 265 AdblockPlus::JsConstValueList converted = jsEngine->ConvertArguments(argumen
ts); |
266 | 266 |
267 v8::Isolate* isolate = arguments.GetIsolate(); | 267 v8::Isolate* isolate = arguments.GetIsolate(); |
268 if (converted.size() != 3) | 268 if (converted.size() != 3) |
269 return v8::ThrowException(Utils::ToV8String(isolate, | 269 return v8::ThrowException(Utils::ToV8String(isolate, |
270 "_fileSystem.write requires 3 parameters")); | 270 "_fileSystem.write requires 3 parameters")); |
271 if (!converted[2]->IsFunction()) | 271 if (!converted[2]->IsFunction()) |
272 return v8::ThrowException(Utils::ToV8String(isolate, | 272 return v8::ThrowException(Utils::ToV8String(isolate, |
273 "Third argument to _fileSystem.write must be a function")); | 273 "Third argument to _fileSystem.write must be a function")); |
274 WriteThread* const writeThread = new WriteThread(jsEngine, converted[2], | 274 WriteThread* const writeThread = new WriteThread(jsEngine, converted[2], |
275 converted[0]->AsString(), converted[1]->AsString()); | 275 converted[0]->AsString(), converted[1]->AsString()); |
276 writeThread->Start(); | 276 writeThread->Start(); |
277 return v8::Undefined(); | 277 return v8::Undefined(); |
278 } | 278 } |
279 | 279 |
280 v8::Handle<v8::Value> MoveCallback(const v8::Arguments& arguments) | 280 v8::Handle<v8::Value> MoveCallback(const v8::Arguments& arguments) |
281 { | 281 { |
282 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg
uments); | 282 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg
uments); |
283 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments); | 283 AdblockPlus::JsConstValueList converted = jsEngine->ConvertArguments(argumen
ts); |
284 | 284 |
285 v8::Isolate* isolate = arguments.GetIsolate(); | 285 v8::Isolate* isolate = arguments.GetIsolate(); |
286 if (converted.size() != 3) | 286 if (converted.size() != 3) |
287 return v8::ThrowException(Utils::ToV8String(isolate, | 287 return v8::ThrowException(Utils::ToV8String(isolate, |
288 "_fileSystem.move requires 3 parameters")); | 288 "_fileSystem.move requires 3 parameters")); |
289 if (!converted[2]->IsFunction()) | 289 if (!converted[2]->IsFunction()) |
290 return v8::ThrowException(Utils::ToV8String(isolate, | 290 return v8::ThrowException(Utils::ToV8String(isolate, |
291 "Third argument to _fileSystem.move must be a function")); | 291 "Third argument to _fileSystem.move must be a function")); |
292 MoveThread* const moveThread = new MoveThread(jsEngine, converted[2], | 292 MoveThread* const moveThread = new MoveThread(jsEngine, converted[2], |
293 converted[0]->AsString(), converted[1]->AsString()); | 293 converted[0]->AsString(), converted[1]->AsString()); |
294 moveThread->Start(); | 294 moveThread->Start(); |
295 return v8::Undefined(); | 295 return v8::Undefined(); |
296 } | 296 } |
297 | 297 |
298 v8::Handle<v8::Value> RemoveCallback(const v8::Arguments& arguments) | 298 v8::Handle<v8::Value> RemoveCallback(const v8::Arguments& arguments) |
299 { | 299 { |
300 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg
uments); | 300 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg
uments); |
301 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments); | 301 AdblockPlus::JsConstValueList converted = jsEngine->ConvertArguments(argumen
ts); |
302 | 302 |
303 v8::Isolate* isolate = arguments.GetIsolate(); | 303 v8::Isolate* isolate = arguments.GetIsolate(); |
304 if (converted.size() != 2) | 304 if (converted.size() != 2) |
305 return v8::ThrowException(Utils::ToV8String(isolate, | 305 return v8::ThrowException(Utils::ToV8String(isolate, |
306 "_fileSystem.remove requires 2 parameters")); | 306 "_fileSystem.remove requires 2 parameters")); |
307 if (!converted[1]->IsFunction()) | 307 if (!converted[1]->IsFunction()) |
308 return v8::ThrowException(Utils::ToV8String(isolate, | 308 return v8::ThrowException(Utils::ToV8String(isolate, |
309 "Second argument to _fileSystem.remove must be a function")); | 309 "Second argument to _fileSystem.remove must be a function")); |
310 RemoveThread* const removeThread = new RemoveThread(jsEngine, converted[1], | 310 RemoveThread* const removeThread = new RemoveThread(jsEngine, converted[1], |
311 converted[0]->AsString()); | 311 converted[0]->AsString()); |
312 removeThread->Start(); | 312 removeThread->Start(); |
313 return v8::Undefined(); | 313 return v8::Undefined(); |
314 } | 314 } |
315 | 315 |
316 v8::Handle<v8::Value> StatCallback(const v8::Arguments& arguments) | 316 v8::Handle<v8::Value> StatCallback(const v8::Arguments& arguments) |
317 { | 317 { |
318 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg
uments); | 318 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg
uments); |
319 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments); | 319 AdblockPlus::JsConstValueList converted = jsEngine->ConvertArguments(argumen
ts); |
320 | 320 |
321 v8::Isolate* isolate = arguments.GetIsolate(); | 321 v8::Isolate* isolate = arguments.GetIsolate(); |
322 if (converted.size() != 2) | 322 if (converted.size() != 2) |
323 return v8::ThrowException(Utils::ToV8String(isolate, | 323 return v8::ThrowException(Utils::ToV8String(isolate, |
324 "_fileSystem.stat requires 2 parameters")); | 324 "_fileSystem.stat requires 2 parameters")); |
325 if (!converted[1]->IsFunction()) | 325 if (!converted[1]->IsFunction()) |
326 return v8::ThrowException(Utils::ToV8String(isolate, | 326 return v8::ThrowException(Utils::ToV8String(isolate, |
327 "Second argument to _fileSystem.stat must be a function")); | 327 "Second argument to _fileSystem.stat must be a function")); |
328 StatThread* const statThread = new StatThread(jsEngine, converted[1], | 328 StatThread* const statThread = new StatThread(jsEngine, converted[1], |
329 converted[0]->AsString()); | 329 converted[0]->AsString()); |
330 statThread->Start(); | 330 statThread->Start(); |
331 return v8::Undefined(); | 331 return v8::Undefined(); |
332 } | 332 } |
333 | 333 |
334 v8::Handle<v8::Value> ResolveCallback(const v8::Arguments& arguments) | 334 v8::Handle<v8::Value> ResolveCallback(const v8::Arguments& arguments) |
335 { | 335 { |
336 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg
uments); | 336 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg
uments); |
337 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments); | 337 AdblockPlus::JsConstValueList converted = jsEngine->ConvertArguments(argumen
ts); |
338 | 338 |
339 v8::Isolate* isolate = arguments.GetIsolate(); | 339 v8::Isolate* isolate = arguments.GetIsolate(); |
340 if (converted.size() != 1) | 340 if (converted.size() != 1) |
341 return v8::ThrowException(Utils::ToV8String(isolate, | 341 return v8::ThrowException(Utils::ToV8String(isolate, |
342 "_fileSystem.resolve requires 1 parameter")); | 342 "_fileSystem.resolve requires 1 parameter")); |
343 | 343 |
344 std::string resolved = jsEngine->GetFileSystem()->Resolve(converted[0]->AsSt
ring()); | 344 std::string resolved = jsEngine->GetFileSystem()->Resolve(converted[0]->AsSt
ring()); |
345 | 345 |
346 return Utils::ToV8String(isolate, resolved); | 346 return Utils::ToV8String(isolate, resolved); |
347 } | 347 } |
348 | 348 |
349 } | 349 } |
350 | 350 |
351 | 351 |
352 JsValuePtr FileSystemJsObject::Setup(JsEngine& jsEngine, const JsValuePtr& obj) | 352 JsValue& FileSystemJsObject::Setup(JsEngine& jsEngine, JsValue& obj) |
353 { | 353 { |
354 obj->SetProperty("read", jsEngine.NewCallback(::ReadCallback)); | 354 obj.SetProperty("read", *jsEngine.NewCallback(::ReadCallback)); |
355 obj->SetProperty("write", jsEngine.NewCallback(::WriteCallback)); | 355 obj.SetProperty("write", *jsEngine.NewCallback(::WriteCallback)); |
356 obj->SetProperty("move", jsEngine.NewCallback(::MoveCallback)); | 356 obj.SetProperty("move", *jsEngine.NewCallback(::MoveCallback)); |
357 obj->SetProperty("remove", jsEngine.NewCallback(::RemoveCallback)); | 357 obj.SetProperty("remove", *jsEngine.NewCallback(::RemoveCallback)); |
358 obj->SetProperty("stat", jsEngine.NewCallback(::StatCallback)); | 358 obj.SetProperty("stat", *jsEngine.NewCallback(::StatCallback)); |
359 obj->SetProperty("resolve", jsEngine.NewCallback(::ResolveCallback)); | 359 obj.SetProperty("resolve", *jsEngine.NewCallback(::ResolveCallback)); |
360 return obj; | 360 return obj; |
361 } | 361 } |
OLD | NEW |