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

Unified Diff: src/WebRequestJsObject.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/WebRequestJsObject.cpp
===================================================================
--- a/src/WebRequestJsObject.cpp
+++ b/src/WebRequestJsObject.cpp
@@ -1,21 +1,20 @@
#include <map>
-#include <AdblockPlus/JsEngine.h>
#include <AdblockPlus/JsValue.h>
#include <AdblockPlus/WebRequest.h>
#include "WebRequestJsObject.h"
#include "Thread.h"
namespace
{
class WebRequestThread : public AdblockPlus::Thread
{
public:
- WebRequestThread(AdblockPlus::JsEngine& jsEngine, AdblockPlus::JsValueList& arguments)
+ WebRequestThread(AdblockPlus::JsEnginePtr jsEngine, AdblockPlus::JsValueList& arguments)
: jsEngine(jsEngine), url(arguments[0]->AsString())
{
if (!url.length())
throw std::runtime_error("Invalid string passed as first argument to GET");
{
AdblockPlus::JsValuePtr headersObj = arguments[1];
if (!headersObj->IsObject())
@@ -34,64 +33,64 @@ namespace
callback = arguments[2];
if (!callback->IsFunction())
throw std::runtime_error("Third argument to GET must be a function");
}
void Run()
{
- AdblockPlus::ServerResponse result = jsEngine.GetWebRequest()->GET(url, headers);
+ AdblockPlus::ServerResponse result = jsEngine->GetWebRequest()->GET(url, headers);
AdblockPlus::JsEngine::Context context(jsEngine);
- AdblockPlus::JsValuePtr resultObject = jsEngine.NewObject();
+ AdblockPlus::JsValuePtr resultObject = jsEngine->NewObject();
resultObject->SetProperty("status", result.status);
resultObject->SetProperty("responseStatus", result.responseStatus);
resultObject->SetProperty("responseText", result.responseText);
- AdblockPlus::JsValuePtr headersObject = jsEngine.NewObject();
+ AdblockPlus::JsValuePtr headersObject = jsEngine->NewObject();
for (AdblockPlus::HeaderList::iterator it = result.responseHeaders.begin();
it != result.responseHeaders.end(); ++it)
{
headersObject->SetProperty(it->first, it->second);
}
resultObject->SetProperty("responseHeaders", headersObject);
AdblockPlus::JsValueList params;
params.push_back(resultObject);
callback->Call(params);
delete this;
}
private:
- AdblockPlus::JsEngine& jsEngine;
+ AdblockPlus::JsEnginePtr jsEngine;
std::string url;
AdblockPlus::HeaderList headers;
AdblockPlus::JsValuePtr callback;
};
v8::Handle<v8::Value> GETCallback(const v8::Arguments& arguments)
{
WebRequestThread* thread;
try
{
- 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() != 3u)
throw std::runtime_error("GET requires exactly 3 arguments");
thread = new WebRequestThread(jsEngine, converted);
}
catch (const std::exception& e)
{
return v8::ThrowException(v8::String::New(e.what()));
}
thread->Start();
return v8::Undefined();
}
}
AdblockPlus::JsValuePtr AdblockPlus::WebRequestJsObject::Setup(
- AdblockPlus::JsEngine& jsEngine, AdblockPlus::JsValuePtr obj)
+ AdblockPlus::JsEnginePtr jsEngine, AdblockPlus::JsValuePtr obj)
{
- obj->SetProperty("GET", jsEngine.NewCallback(::GETCallback));
+ obj->SetProperty("GET", jsEngine->NewCallback(::GETCallback));
return obj;
}
« src/JsEngine.cpp ('K') | « src/WebRequestJsObject.h ('k') | test/AppInfoJsObject.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld