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

Unified Diff: src/FileSystemJsObject.cpp

Issue 29367003: Issue #4711 - Rewrite legacy thread facilities
Patch Set: Created Dec. 7, 2016, 4:44 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/FileSystemJsObject.cpp
===================================================================
--- a/src/FileSystemJsObject.cpp
+++ b/src/FileSystemJsObject.cpp
@@ -23,38 +23,41 @@
#include <AdblockPlus/JsValue.h>
#include "FileSystemJsObject.h"
#include "JsContext.h"
-#include "Thread.h"
+#include "Scheduler.h"
#include "Utils.h"
using namespace AdblockPlus;
namespace
{
- class IoThread : public Thread
+ class IoTask
{
+ protected:
+ virtual void operator()() = 0;
+ JsEnginePtr jsEngine;
+ FileSystemPtr fileSystem;
+ JsValuePtr callback;
+
public:
- IoThread(JsEnginePtr jsEngine, JsValuePtr callback)
- : Thread(true), jsEngine(jsEngine), fileSystem(jsEngine->GetFileSystem()),
+ IoTask(JsEnginePtr jsEngine, JsValuePtr callback)
+ : jsEngine(jsEngine), fileSystem(jsEngine->GetFileSystem()),
callback(callback)
{
}
- protected:
- JsEnginePtr jsEngine;
- FileSystemPtr fileSystem;
- JsValuePtr callback;
+ virtual ~IoTask() {}
};
- class ReadThread : public IoThread
+ class ReadTask : public IoTask
{
public:
- ReadThread(JsEnginePtr jsEngine, JsValuePtr callback,
+ ReadTask(JsEnginePtr jsEngine, JsValuePtr callback,
const std::string& path)
- : IoThread(jsEngine, callback), path(path)
+ : IoTask(jsEngine, callback), path(path)
{
}
- void Run()
+ void operator()()
{
std::string content;
std::string error;
@@ -85,16 +88,16 @@
std::string path;
};
- class WriteThread : public IoThread
+ class WriteTask : public IoTask
{
public:
- WriteThread(JsEnginePtr jsEngine, JsValuePtr callback,
+ WriteTask(JsEnginePtr jsEngine, JsValuePtr callback,
const std::string& path, const std::string& content)
- : IoThread(jsEngine, callback), path(path), content(content)
+ : IoTask(jsEngine, callback), path(path), content(content)
{
}
- void Run()
+ void operator()()
{
std::string error;
try
@@ -124,16 +127,16 @@
std::string content;
};
- class MoveThread : public IoThread
+ class MoveTask : public IoTask
{
public:
- MoveThread(JsEnginePtr jsEngine, JsValuePtr callback,
+ MoveTask(JsEnginePtr jsEngine, JsValuePtr callback,
const std::string& fromPath, const std::string& toPath)
- : IoThread(jsEngine, callback), fromPath(fromPath), toPath(toPath)
+ : IoTask(jsEngine, callback), fromPath(fromPath), toPath(toPath)
{
}
- void Run()
+ void operator()()
{
std::string error;
try
@@ -161,16 +164,16 @@
std::string toPath;
};
- class RemoveThread : public IoThread
+ class RemoveTask : public IoTask
{
public:
- RemoveThread(JsEnginePtr jsEngine, JsValuePtr callback,
+ RemoveTask(JsEnginePtr jsEngine, JsValuePtr callback,
const std::string& path)
- : IoThread(jsEngine, callback), path(path)
+ : IoTask(jsEngine, callback), path(path)
{
}
- void Run()
+ void operator()()
{
std::string error;
try
@@ -198,16 +201,16 @@
};
- class StatThread : public IoThread
+ class StatTask : public IoTask
{
public:
- StatThread(JsEnginePtr jsEngine, JsValuePtr callback,
+ StatTask(JsEnginePtr jsEngine, JsValuePtr callback,
const std::string& path)
- : IoThread(jsEngine, callback), path(path)
+ : IoTask(jsEngine, callback), path(path)
{
}
- void Run()
+ void operator()()
{
std::string error;
FileSystem::StatResult statResult;
@@ -253,9 +256,9 @@
if (!converted[1]->IsFunction())
return v8::ThrowException(Utils::ToV8String(isolate,
"Second argument to _fileSystem.read must be a function"));
- ReadThread* const readThread = new ReadThread(jsEngine, converted[1],
+ const auto readTask = std::make_shared<ReadTask>(jsEngine, converted[1],
converted[0]->AsString());
- readThread->Start();
+ AdblockPlus::Scheduler::StartImmediatelyInSingleUseThread(AdblockPlus::MakeHeapFunction(readTask));
return v8::Undefined();
}
@@ -271,9 +274,9 @@
if (!converted[2]->IsFunction())
return v8::ThrowException(Utils::ToV8String(isolate,
"Third argument to _fileSystem.write must be a function"));
- WriteThread* const writeThread = new WriteThread(jsEngine, converted[2],
+ const auto writeTask = std::make_shared<WriteTask>(jsEngine, converted[2],
converted[0]->AsString(), converted[1]->AsString());
- writeThread->Start();
+ AdblockPlus::Scheduler::StartImmediatelyInSingleUseThread(AdblockPlus::MakeHeapFunction(writeTask));
return v8::Undefined();
}
@@ -289,9 +292,9 @@
if (!converted[2]->IsFunction())
return v8::ThrowException(Utils::ToV8String(isolate,
"Third argument to _fileSystem.move must be a function"));
- MoveThread* const moveThread = new MoveThread(jsEngine, converted[2],
+ const auto moveTask = std::make_shared<MoveTask>(jsEngine, converted[2],
converted[0]->AsString(), converted[1]->AsString());
- moveThread->Start();
+ AdblockPlus::Scheduler::StartImmediatelyInSingleUseThread(AdblockPlus::MakeHeapFunction(moveTask));
return v8::Undefined();
}
@@ -307,9 +310,9 @@
if (!converted[1]->IsFunction())
return v8::ThrowException(Utils::ToV8String(isolate,
"Second argument to _fileSystem.remove must be a function"));
- RemoveThread* const removeThread = new RemoveThread(jsEngine, converted[1],
+ const auto removeTask = std::make_shared<RemoveTask>(jsEngine, converted[1],
converted[0]->AsString());
- removeThread->Start();
+ AdblockPlus::Scheduler::StartImmediatelyInSingleUseThread(AdblockPlus::MakeHeapFunction(removeTask));
return v8::Undefined();
}
@@ -325,9 +328,9 @@
if (!converted[1]->IsFunction())
return v8::ThrowException(Utils::ToV8String(isolate,
"Second argument to _fileSystem.stat must be a function"));
- StatThread* const statThread = new StatThread(jsEngine, converted[1],
+ const auto statTask = std::make_shared<StatTask>(jsEngine, converted[1],
converted[0]->AsString());
- statThread->Start();
+ AdblockPlus::Scheduler::StartImmediatelyInSingleUseThread(AdblockPlus::MakeHeapFunction(statTask));
return v8::Undefined();
}
« no previous file with comments | « libadblockplus.gyp ('k') | src/FilterEngine.cpp » ('j') | src/GlobalJsObject.cpp » ('J')

Powered by Google App Engine
This is Rietveld