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

Unified Diff: src/WebRequestJsObject.cpp

Issue 29428650: Issue 5180 - introduce asynchronous web request (Closed) Base URL: https://github.com/adblockplus/libadblockplus.git
Patch Set: Created May 3, 2017, 2:21 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
« no previous file with comments | « src/JsEngine.cpp ('k') | test/BaseJsTest.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/WebRequestJsObject.cpp
diff --git a/src/WebRequestJsObject.cpp b/src/WebRequestJsObject.cpp
index 70375b2fddae53198f0965c8fc62d0a71b975dd6..b5002ed009ad778e90b80cd1fb295d3cdf41fa90 100644
--- a/src/WebRequestJsObject.cpp
+++ b/src/WebRequestJsObject.cpp
@@ -16,92 +16,97 @@
*/
#include <map>
-#include <AdblockPlus/JsValue.h>
-#include <AdblockPlus/WebRequest.h>
+#include <AdblockPlus/IWebRequest.h>
#include "JsContext.h"
-#include "Thread.h"
#include "Utils.h"
#include "WebRequestJsObject.h"
+#include <thread> // TODO: remove with removing of JsEngine::webRequestLegacy
-namespace
+using namespace AdblockPlus;
+
+void JsEngine::ScheduleWebRequest(const v8::Arguments& arguments)
{
- class WebRequestThread : public AdblockPlus::Thread
+ 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");
+
+ auto url = converted[0].AsString();
+ if (!url.length())
+ throw std::runtime_error("Invalid string passed as first argument to GET");
+
+ AdblockPlus::HeaderList headers;
{
- public:
- WebRequestThread(const AdblockPlus::JsEnginePtr& jsEngine, const AdblockPlus::JsValueList& arguments)
- : Thread(true), jsEngine(jsEngine), url(arguments[0].AsString()),
- callback(arguments[2])
+ const AdblockPlus::JsValue& headersObj = converted[1];
+ if (!headersObj.IsObject())
+ throw std::runtime_error("Second argument to GET must be an object");
+
+ std::vector<std::string> properties = headersObj.GetOwnPropertyNames();
+ for (const auto& header : properties)
{
- if (!url.length())
- throw std::runtime_error("Invalid string passed as first argument to GET");
-
- {
- const AdblockPlus::JsValue& headersObj = arguments[1];
- if (!headersObj.IsObject())
- throw std::runtime_error("Second argument to GET must be an object");
-
- std::vector<std::string> properties = headersObj.GetOwnPropertyNames();
- for (const auto& header : properties)
- {
- std::string headerValue = headersObj.GetProperty(header).AsString();
- if (header.length() && headerValue.length())
- headers.push_back(std::pair<std::string, std::string>(header, headerValue));
- }
- }
-
- if (!callback.IsFunction())
- throw std::runtime_error("Third argument to GET must be a function");
+ std::string headerValue = headersObj.GetProperty(header).AsString();
+ if (header.length() && headerValue.length())
+ headers.push_back(std::pair<std::string, std::string>(header, headerValue));
}
+ }
- void Run()
- {
- AdblockPlus::ServerResponse result = jsEngine->GetWebRequest()->GET(url, headers);
+ if (!converted[2].IsFunction())
+ throw std::runtime_error("Third argument to GET must be a function");
- AdblockPlus::JsContext context(*jsEngine);
+ auto paramsID = jsEngine->StoreJsValues(converted);
+ std::weak_ptr<JsEngine> weakJsEngine = jsEngine;
+ auto getCallback = [weakJsEngine, paramsID](const ServerResponse& response)
+ {
+ auto jsEngine = weakJsEngine.lock();
+ if (!jsEngine)
+ return;
+ auto webRequestParams = jsEngine->TakeJsValues(paramsID);
- auto resultObject = jsEngine->NewObject();
- resultObject.SetProperty("status", result.status);
- resultObject.SetProperty("responseStatus", result.responseStatus);
- resultObject.SetProperty("responseText", result.responseText);
+ AdblockPlus::JsContext context(*jsEngine);
- auto headersObject = jsEngine->NewObject();
- for (const auto& header : result.responseHeaders)
- {
- headersObject.SetProperty(header.first, header.second);
- }
- resultObject.SetProperty("responseHeaders", headersObject);
+ auto resultObject = jsEngine->NewObject();
+ resultObject.SetProperty("status", response.status);
+ resultObject.SetProperty("responseStatus", response.responseStatus);
+ resultObject.SetProperty("responseText", response.responseText);
- AdblockPlus::JsValueList params;
- params.push_back(resultObject);
- callback.Call(params);
+ auto headersObject = jsEngine->NewObject();
+ for (const auto& header : response.responseHeaders)
+ {
+ headersObject.SetProperty(header.first, header.second);
}
+ resultObject.SetProperty("responseHeaders", headersObject);
- private:
- AdblockPlus::JsEnginePtr jsEngine;
- std::string url;
- AdblockPlus::HeaderList headers;
- AdblockPlus::JsValue callback;
+ webRequestParams[2].Call(resultObject);
};
+
+ if (jsEngine->webRequestLegacy)
+ {
+ std::thread([jsEngine, url, headers, getCallback]
+ {
+ getCallback(jsEngine->webRequestLegacy->GET(url, headers));
+ }).detach();
+ return;
+ }
+
+ jsEngine->webRequest->GET(url, headers, getCallback);
+}
+
+namespace
+{
v8::Handle<v8::Value> GETCallback(const v8::Arguments& arguments)
{
- WebRequestThread* thread;
try
{
- 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)
+ AdblockPlus::JsEngine::ScheduleWebRequest(arguments);
+ } catch (const std::exception& e)
{
using AdblockPlus::Utils::ToV8String;
v8::Isolate* isolate = arguments.GetIsolate();
return v8::ThrowException(ToV8String(isolate, e.what()));
}
- thread->Start();
+
return v8::Undefined();
}
}
« no previous file with comments | « src/JsEngine.cpp ('k') | test/BaseJsTest.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld