| 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 | 
| 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 #include <AdblockPlus/FileSystem.h> | 18 #include <AdblockPlus/FileSystem.h> | 
| 19 #include <stdexcept> | 19 #include <stdexcept> | 
| 20 #include <sstream> | 20 #include <sstream> | 
| 21 #include <vector> | 21 #include <vector> | 
| 22 | 22 | 
| 23 #include <AdblockPlus/JsValue.h> | 23 #include <AdblockPlus/JsValue.h> | 
| 24 #include "FileSystemJsObject.h" | 24 #include "FileSystemJsObject.h" | 
| 25 #include "JsContext.h" | 25 #include "JsContext.h" | 
| 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 using AdblockPlus::Utils::ThrowExceptionInJS; | 
| 30 | 31 | 
| 31 namespace | 32 namespace | 
| 32 { | 33 { | 
| 33   class IoThread : public Thread | 34   class IoThread : public Thread | 
| 34   { | 35   { | 
| 35   public: | 36   public: | 
| 36     IoThread(const JsEnginePtr& jsEngine, const JsValue& callback) | 37     IoThread(const JsEnginePtr& jsEngine, const JsValue& callback) | 
| 37       : Thread(true), jsEngine(jsEngine), fileSystem(jsEngine->GetFileSystem()), | 38       : Thread(true), jsEngine(jsEngine), fileSystem(jsEngine->GetFileSystem()), | 
| 38         callback(callback) | 39         callback(callback) | 
| 39     { | 40     { | 
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 234 | 235 | 
| 235       JsValueList params; | 236       JsValueList params; | 
| 236       params.push_back(result); | 237       params.push_back(result); | 
| 237       callback.Call(params); | 238       callback.Call(params); | 
| 238     } | 239     } | 
| 239 | 240 | 
| 240   private: | 241   private: | 
| 241     std::string path; | 242     std::string path; | 
| 242   }; | 243   }; | 
| 243 | 244 | 
| 244   v8::Handle<v8::Value> ReadCallback(const v8::Arguments& arguments) | 245   void ReadCallback(const v8::FunctionCallbackInfo<v8::Value>& arguments) | 
| 245   { | 246   { | 
| 246     AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg
     uments); | 247     AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg
     uments); | 
| 247     AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments); | 248     AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments); | 
| 248 | 249 | 
| 249     v8::Isolate* isolate = arguments.GetIsolate(); | 250     v8::Isolate* isolate = arguments.GetIsolate(); | 
| 250     if (converted.size() != 2) | 251     if (converted.size() != 2) | 
| 251       return v8::ThrowException(Utils::ToV8String(isolate, | 252       return ThrowExceptionInJS(isolate, "_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 ThrowExceptionInJS(isolate, "Second argument to _fileSystem.read mu
     st be a function"); | 
| 255         "Second argument to _fileSystem.read must be a function")); |  | 
| 256     ReadThread* const readThread = new ReadThread(jsEngine, converted[1], | 255     ReadThread* const readThread = new ReadThread(jsEngine, converted[1], | 
| 257         converted[0].AsString()); | 256         converted[0].AsString()); | 
| 258     readThread->Start(); | 257     readThread->Start(); | 
| 259     return v8::Undefined(); |  | 
| 260   } | 258   } | 
| 261 | 259 | 
| 262   v8::Handle<v8::Value> WriteCallback(const v8::Arguments& arguments) | 260   void WriteCallback(const v8::FunctionCallbackInfo<v8::Value>& arguments) | 
| 263   { | 261   { | 
| 264     AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg
     uments); | 262     AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg
     uments); | 
| 265     AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments); | 263     AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments); | 
| 266 | 264 | 
| 267     v8::Isolate* isolate = arguments.GetIsolate(); | 265     v8::Isolate* isolate = arguments.GetIsolate(); | 
| 268     if (converted.size() != 3) | 266     if (converted.size() != 3) | 
| 269       return v8::ThrowException(Utils::ToV8String(isolate, | 267       return ThrowExceptionInJS(isolate, "_fileSystem.write requires 3 parameter
     s"); | 
| 270         "_fileSystem.write requires 3 parameters")); |  | 
| 271     if (!converted[2].IsFunction()) | 268     if (!converted[2].IsFunction()) | 
| 272       return v8::ThrowException(Utils::ToV8String(isolate, | 269       return ThrowExceptionInJS(isolate, "Third argument to _fileSystem.write mu
     st be a function"); | 
| 273         "Third argument to _fileSystem.write must be a function")); |  | 
| 274     WriteThread* const writeThread = new WriteThread(jsEngine, converted[2], | 270     WriteThread* const writeThread = new WriteThread(jsEngine, converted[2], | 
| 275         converted[0].AsString(), converted[1].AsString()); | 271         converted[0].AsString(), converted[1].AsString()); | 
| 276     writeThread->Start(); | 272     writeThread->Start(); | 
| 277     return v8::Undefined(); |  | 
| 278   } | 273   } | 
| 279 | 274 | 
| 280   v8::Handle<v8::Value> MoveCallback(const v8::Arguments& arguments) | 275   void MoveCallback(const v8::FunctionCallbackInfo<v8::Value>& arguments) | 
| 281   { | 276   { | 
| 282     AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg
     uments); | 277     AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg
     uments); | 
| 283     AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments); | 278     AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments); | 
| 284 | 279 | 
| 285     v8::Isolate* isolate = arguments.GetIsolate(); | 280     v8::Isolate* isolate = arguments.GetIsolate(); | 
| 286     if (converted.size() != 3) | 281     if (converted.size() != 3) | 
| 287       return v8::ThrowException(Utils::ToV8String(isolate, | 282       return ThrowExceptionInJS(isolate, "_fileSystem.move requires 3 parameters
     "); | 
| 288         "_fileSystem.move requires 3 parameters")); |  | 
| 289     if (!converted[2].IsFunction()) | 283     if (!converted[2].IsFunction()) | 
| 290       return v8::ThrowException(Utils::ToV8String(isolate, | 284       return ThrowExceptionInJS(isolate, "Third argument to _fileSystem.move mus
     t be a function"); | 
| 291         "Third argument to _fileSystem.move must be a function")); |  | 
| 292     MoveThread* const moveThread = new MoveThread(jsEngine, converted[2], | 285     MoveThread* const moveThread = new MoveThread(jsEngine, converted[2], | 
| 293         converted[0].AsString(), converted[1].AsString()); | 286         converted[0].AsString(), converted[1].AsString()); | 
| 294     moveThread->Start(); | 287     moveThread->Start(); | 
| 295     return v8::Undefined(); |  | 
| 296   } | 288   } | 
| 297 | 289 | 
| 298   v8::Handle<v8::Value> RemoveCallback(const v8::Arguments& arguments) | 290   void RemoveCallback(const v8::FunctionCallbackInfo<v8::Value>& arguments) | 
| 299   { | 291   { | 
| 300     AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg
     uments); | 292     AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg
     uments); | 
| 301     AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments); | 293     AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments); | 
| 302 | 294 | 
| 303     v8::Isolate* isolate = arguments.GetIsolate(); | 295     v8::Isolate* isolate = arguments.GetIsolate(); | 
| 304     if (converted.size() != 2) | 296     if (converted.size() != 2) | 
| 305       return v8::ThrowException(Utils::ToV8String(isolate, | 297       return ThrowExceptionInJS(isolate, "_fileSystem.remove requires 2 paramete
     rs"); | 
| 306         "_fileSystem.remove requires 2 parameters")); |  | 
| 307     if (!converted[1].IsFunction()) | 298     if (!converted[1].IsFunction()) | 
| 308       return v8::ThrowException(Utils::ToV8String(isolate, | 299       return ThrowExceptionInJS(isolate, "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], | 300     RemoveThread* const removeThread = new RemoveThread(jsEngine, converted[1], | 
| 311         converted[0].AsString()); | 301         converted[0].AsString()); | 
| 312     removeThread->Start(); | 302     removeThread->Start(); | 
| 313     return v8::Undefined(); |  | 
| 314   } | 303   } | 
| 315 | 304 | 
| 316   v8::Handle<v8::Value> StatCallback(const v8::Arguments& arguments) | 305   void StatCallback(const v8::FunctionCallbackInfo<v8::Value>& arguments) | 
| 317   { | 306   { | 
| 318     AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg
     uments); | 307     AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg
     uments); | 
| 319     AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments); | 308     AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments); | 
| 320 | 309 | 
| 321     v8::Isolate* isolate = arguments.GetIsolate(); | 310     v8::Isolate* isolate = arguments.GetIsolate(); | 
| 322     if (converted.size() != 2) | 311     if (converted.size() != 2) | 
| 323       return v8::ThrowException(Utils::ToV8String(isolate, | 312       return ThrowExceptionInJS(isolate, "_fileSystem.stat requires 2 parameters
     "); | 
| 324         "_fileSystem.stat requires 2 parameters")); |  | 
| 325     if (!converted[1].IsFunction()) | 313     if (!converted[1].IsFunction()) | 
| 326       return v8::ThrowException(Utils::ToV8String(isolate, | 314       return ThrowExceptionInJS(isolate, "Second argument to _fileSystem.stat mu
     st be a function"); | 
| 327         "Second argument to _fileSystem.stat must be a function")); |  | 
| 328     StatThread* const statThread = new StatThread(jsEngine, converted[1], | 315     StatThread* const statThread = new StatThread(jsEngine, converted[1], | 
| 329         converted[0].AsString()); | 316         converted[0].AsString()); | 
| 330     statThread->Start(); | 317     statThread->Start(); | 
| 331     return v8::Undefined(); |  | 
| 332   } | 318   } | 
| 333 | 319 | 
| 334   v8::Handle<v8::Value> ResolveCallback(const v8::Arguments& arguments) | 320   void ResolveCallback(const v8::FunctionCallbackInfo<v8::Value>& arguments) | 
| 335   { | 321   { | 
| 336     AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg
     uments); | 322     AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg
     uments); | 
| 337     AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments); | 323     AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments); | 
| 338 | 324 | 
| 339     v8::Isolate* isolate = arguments.GetIsolate(); | 325     v8::Isolate* isolate = arguments.GetIsolate(); | 
| 340     if (converted.size() != 1) | 326     if (converted.size() != 1) | 
| 341       return v8::ThrowException(Utils::ToV8String(isolate, | 327       return ThrowExceptionInJS(isolate, "_fileSystem.resolve requires 1 paramet
     er"); | 
| 342         "_fileSystem.resolve requires 1 parameter")); |  | 
| 343 | 328 | 
| 344     std::string resolved = jsEngine->GetFileSystem()->Resolve(converted[0].AsStr
     ing()); | 329     std::string resolved = jsEngine->GetFileSystem()->Resolve(converted[0].AsStr
     ing()); | 
| 345 | 330     arguments.GetReturnValue().Set(Utils::ToV8String(isolate, resolved)); | 
| 346     return Utils::ToV8String(isolate, resolved); |  | 
| 347   } | 331   } | 
| 348 |  | 
| 349 } | 332 } | 
| 350 | 333 | 
| 351 | 334 | 
| 352 JsValue& FileSystemJsObject::Setup(JsEngine& jsEngine, JsValue& obj) | 335 JsValue& FileSystemJsObject::Setup(JsEngine& jsEngine, JsValue& obj) | 
| 353 { | 336 { | 
| 354   obj.SetProperty("read", jsEngine.NewCallback(::ReadCallback)); | 337   obj.SetProperty("read", jsEngine.NewCallback(::ReadCallback)); | 
| 355   obj.SetProperty("write", jsEngine.NewCallback(::WriteCallback)); | 338   obj.SetProperty("write", jsEngine.NewCallback(::WriteCallback)); | 
| 356   obj.SetProperty("move", jsEngine.NewCallback(::MoveCallback)); | 339   obj.SetProperty("move", jsEngine.NewCallback(::MoveCallback)); | 
| 357   obj.SetProperty("remove", jsEngine.NewCallback(::RemoveCallback)); | 340   obj.SetProperty("remove", jsEngine.NewCallback(::RemoveCallback)); | 
| 358   obj.SetProperty("stat", jsEngine.NewCallback(::StatCallback)); | 341   obj.SetProperty("stat", jsEngine.NewCallback(::StatCallback)); | 
| 359   obj.SetProperty("resolve", jsEngine.NewCallback(::ResolveCallback)); | 342   obj.SetProperty("resolve", jsEngine.NewCallback(::ResolveCallback)); | 
| 360   return obj; | 343   return obj; | 
| 361 } | 344 } | 
| OLD | NEW | 
|---|