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

Unified Diff: src/FileSystemJsObject.cpp

Issue 10173031: Don`t use references to JsEngine to avoid use-after-free errors,switch to shared_ptr instead (Closed)
Patch Set: Created April 18, 2013, 4:15 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
@@ -1,43 +1,42 @@
#include <AdblockPlus/FileSystem.h>
#include <stdexcept>
#include <sstream>
#include <vector>
-#include <AdblockPlus/JsEngine.h>
#include <AdblockPlus/JsValue.h>
#include "FileSystemJsObject.h"
#include "Utils.h"
#include "Thread.h"
#include "Utils.h"
using namespace AdblockPlus;
namespace
{
class IoThread : public Thread
{
public:
- IoThread(JsEngine& jsEngine, JsValuePtr callback)
- : jsEngine(jsEngine), fileSystem(jsEngine.GetFileSystem()),
+ IoThread(JsEnginePtr jsEngine, JsValuePtr callback)
+ : jsEngine(jsEngine), fileSystem(jsEngine->GetFileSystem()),
callback(callback)
{
}
protected:
- JsEngine& jsEngine;
+ JsEnginePtr jsEngine;
FileSystemPtr fileSystem;
JsValuePtr callback;
};
class ReadThread : public IoThread
{
public:
- ReadThread(JsEngine& jsEngine, JsValuePtr callback,
+ ReadThread(JsEnginePtr jsEngine, JsValuePtr callback,
const std::string& path)
: IoThread(jsEngine, callback), path(path)
{
}
void Run()
{
std::string content;
@@ -52,33 +51,33 @@ namespace
error = e.what();
}
catch (...)
{
error = "Unknown error while reading from " + path;
}
const JsEngine::Context context(jsEngine);
- JsValuePtr result = jsEngine.NewObject();
+ JsValuePtr result = jsEngine->NewObject();
result->SetProperty("content", content);
result->SetProperty("error", error);
JsValueList params;
params.push_back(result);
callback->Call(params);
delete this;
}
private:
std::string path;
};
class WriteThread : public IoThread
{
public:
- WriteThread(JsEngine& jsEngine, JsValuePtr callback,
+ WriteThread(JsEnginePtr jsEngine, JsValuePtr callback,
const std::string& path, const std::string& content)
: IoThread(jsEngine, callback), path(path), content(content)
{
}
void Run()
{
std::string error;
@@ -93,32 +92,32 @@ namespace
error = e.what();
}
catch (...)
{
error = "Unknown error while writing to " + path;
}
const JsEngine::Context context(jsEngine);
- JsValuePtr errorValue = jsEngine.NewValue(error);
+ JsValuePtr errorValue = jsEngine->NewValue(error);
JsValueList params;
params.push_back(errorValue);
callback->Call(params);
delete this;
}
private:
std::string path;
std::string content;
};
class MoveThread : public IoThread
{
public:
- MoveThread(JsEngine& jsEngine, JsValuePtr callback,
+ MoveThread(JsEnginePtr jsEngine, JsValuePtr callback,
const std::string& fromPath, const std::string& toPath)
: IoThread(jsEngine, callback), fromPath(fromPath), toPath(toPath)
{
}
void Run()
{
std::string error;
@@ -131,32 +130,32 @@ namespace
error = e.what();
}
catch (...)
{
error = "Unknown error while moving " + fromPath + " to " + toPath;
}
const JsEngine::Context context(jsEngine);
- JsValuePtr errorValue = jsEngine.NewValue(error);
+ JsValuePtr errorValue = jsEngine->NewValue(error);
JsValueList params;
params.push_back(errorValue);
callback->Call(params);
delete this;
}
private:
std::string fromPath;
std::string toPath;
};
class RemoveThread : public IoThread
{
public:
- RemoveThread(JsEngine& jsEngine, JsValuePtr callback,
+ RemoveThread(JsEnginePtr jsEngine, JsValuePtr callback,
const std::string& path)
: IoThread(jsEngine, callback), path(path)
{
}
void Run()
{
std::string error;
@@ -169,31 +168,31 @@ namespace
error = e.what();
}
catch (...)
{
error = "Unknown error while removing " + path;
}
const JsEngine::Context context(jsEngine);
- JsValuePtr errorValue = jsEngine.NewValue(error);
+ JsValuePtr errorValue = jsEngine->NewValue(error);
JsValueList params;
params.push_back(errorValue);
callback->Call(params);
delete this;
}
private:
std::string path;
};
class StatThread : public IoThread
{
public:
- StatThread(JsEngine& jsEngine, JsValuePtr callback,
+ StatThread(JsEnginePtr jsEngine, JsValuePtr callback,
const std::string& path)
: IoThread(jsEngine, callback), path(path)
{
}
void Run()
{
std::string error;
@@ -207,17 +206,17 @@ namespace
error = e.what();
}
catch (...)
{
error = "Unknown error while calling stat on " + path;
}
const JsEngine::Context context(jsEngine);
- JsValuePtr result = jsEngine.NewObject();
+ JsValuePtr result = jsEngine->NewObject();
result->SetProperty("exists", statResult.exists);
result->SetProperty("isFile", statResult.isFile);
result->SetProperty("isDirectory", statResult.isDirectory);
result->SetProperty("lastModified", statResult.lastModified);
result->SetProperty("error", error);
JsValueList params;
params.push_back(result);
@@ -226,101 +225,101 @@ namespace
}
private:
std::string path;
};
v8::Handle<v8::Value> ReadCallback(const v8::Arguments& arguments)
{
- AdblockPlus::JsEngine& jsEngine = AdblockPlus::JsEngine::FromArguments(arguments);
- AdblockPlus::JsValueList converted = jsEngine.ConvertArguments(arguments);
+ AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arguments);
+ AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments);
if (converted.size() != 2)
return v8::ThrowException(v8::String::New(
"_fileSystem.read requires 2 parameters"));
if (!converted[1]->IsFunction())
return v8::ThrowException(v8::String::New(
"Second argument to _fileSystem.read must be a function"));
ReadThread* const readThread = new ReadThread(jsEngine, converted[1],
converted[0]->AsString());
readThread->Start();
return v8::Undefined();
}
v8::Handle<v8::Value> WriteCallback(const v8::Arguments& arguments)
{
- AdblockPlus::JsEngine& jsEngine = AdblockPlus::JsEngine::FromArguments(arguments);
- AdblockPlus::JsValueList converted = jsEngine.ConvertArguments(arguments);
+ AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arguments);
+ AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments);
if (converted.size() != 3)
return v8::ThrowException(v8::String::New(
"_fileSystem.write requires 3 parameters"));
if (!converted[2]->IsFunction())
return v8::ThrowException(v8::String::New(
"Third argument to _fileSystem.write must be a function"));
WriteThread* const writeThread = new WriteThread(jsEngine, converted[2],
converted[0]->AsString(), converted[1]->AsString());
writeThread->Start();
return v8::Undefined();
}
v8::Handle<v8::Value> MoveCallback(const v8::Arguments& arguments)
{
- AdblockPlus::JsEngine& jsEngine = AdblockPlus::JsEngine::FromArguments(arguments);
- AdblockPlus::JsValueList converted = jsEngine.ConvertArguments(arguments);
+ AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arguments);
+ AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments);
if (converted.size() != 3)
return v8::ThrowException(v8::String::New(
"_fileSystem.move requires 3 parameters"));
if (!converted[2]->IsFunction())
return v8::ThrowException(v8::String::New(
"Third argument to _fileSystem.move must be a function"));
MoveThread* const moveThread = new MoveThread(jsEngine, converted[2],
converted[0]->AsString(), converted[1]->AsString());
moveThread->Start();
return v8::Undefined();
}
v8::Handle<v8::Value> RemoveCallback(const v8::Arguments& arguments)
{
- AdblockPlus::JsEngine& jsEngine = AdblockPlus::JsEngine::FromArguments(arguments);
- AdblockPlus::JsValueList converted = jsEngine.ConvertArguments(arguments);
+ AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arguments);
+ AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments);
if (converted.size() != 2)
return v8::ThrowException(v8::String::New(
"_fileSystem.remove requires 2 parameters"));
if (!converted[1]->IsFunction())
return v8::ThrowException(v8::String::New(
"Second argument to _fileSystem.remove must be a function"));
RemoveThread* const removeThread = new RemoveThread(jsEngine, converted[1],
converted[0]->AsString());
removeThread->Start();
return v8::Undefined();
}
v8::Handle<v8::Value> StatCallback(const v8::Arguments& arguments)
{
- AdblockPlus::JsEngine& jsEngine = AdblockPlus::JsEngine::FromArguments(arguments);
- AdblockPlus::JsValueList converted = jsEngine.ConvertArguments(arguments);
+ AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arguments);
+ AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments);
if (converted.size() != 2)
return v8::ThrowException(v8::String::New(
"_fileSystem.stat requires 2 parameters"));
if (!converted[1]->IsFunction())
return v8::ThrowException(v8::String::New(
"Second argument to _fileSystem.stat must be a function"));
StatThread* const statThread = new StatThread(jsEngine, converted[1],
converted[0]->AsString());
statThread->Start();
return v8::Undefined();
}
}
-JsValuePtr FileSystemJsObject::Setup(JsEngine& jsEngine, JsValuePtr obj)
+JsValuePtr FileSystemJsObject::Setup(JsEnginePtr jsEngine, JsValuePtr obj)
{
- obj->SetProperty("read", jsEngine.NewCallback(::ReadCallback));
- obj->SetProperty("write", jsEngine.NewCallback(::WriteCallback));
- obj->SetProperty("move", jsEngine.NewCallback(::MoveCallback));
- obj->SetProperty("remove", jsEngine.NewCallback(::RemoveCallback));
- obj->SetProperty("stat", jsEngine.NewCallback(::StatCallback));
+ obj->SetProperty("read", jsEngine->NewCallback(::ReadCallback));
+ obj->SetProperty("write", jsEngine->NewCallback(::WriteCallback));
+ obj->SetProperty("move", jsEngine->NewCallback(::MoveCallback));
+ obj->SetProperty("remove", jsEngine->NewCallback(::RemoveCallback));
+ obj->SetProperty("stat", jsEngine->NewCallback(::StatCallback));
return obj;
}
« no previous file with comments | « src/FileSystemJsObject.h ('k') | src/FilterEngine.cpp » ('j') | src/JsEngine.cpp » ('J')

Powered by Google App Engine
This is Rietveld