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

Side by Side 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.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/JsEngine.cpp ('k') | test/BaseJsTest.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <map> 18 #include <map>
19 #include <AdblockPlus/JsValue.h> 19 #include <AdblockPlus/IWebRequest.h>
20 #include <AdblockPlus/WebRequest.h>
21 20
22 #include "JsContext.h" 21 #include "JsContext.h"
23 #include "Thread.h"
24 #include "Utils.h" 22 #include "Utils.h"
25 #include "WebRequestJsObject.h" 23 #include "WebRequestJsObject.h"
24 #include <thread> // TODO: remove with removing of JsEngine::webRequestLegacy
25
26 using namespace AdblockPlus;
27
28 void JsEngine::ScheduleWebRequest(const v8::Arguments& arguments)
29 {
30 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(argum ents);
31 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments);
32 if (converted.size() != 3u)
33 throw std::runtime_error("GET requires exactly 3 arguments");
34
35 auto url = converted[0].AsString();
36 if (!url.length())
37 throw std::runtime_error("Invalid string passed as first argument to GET");
38
39 AdblockPlus::HeaderList headers;
40 {
41 const AdblockPlus::JsValue& headersObj = converted[1];
42 if (!headersObj.IsObject())
43 throw std::runtime_error("Second argument to GET must be an object");
44
45 std::vector<std::string> properties = headersObj.GetOwnPropertyNames();
46 for (const auto& header : properties)
47 {
48 std::string headerValue = headersObj.GetProperty(header).AsString();
49 if (header.length() && headerValue.length())
50 headers.push_back(std::pair<std::string, std::string>(header, headerValu e));
51 }
52 }
53
54 if (!converted[2].IsFunction())
55 throw std::runtime_error("Third argument to GET must be a function");
56
57 auto paramsID = jsEngine->StoreJsValues(converted);
58 std::weak_ptr<JsEngine> weakJsEngine = jsEngine;
59 auto getCallback = [weakJsEngine, paramsID](const ServerResponse& response)
60 {
61 auto jsEngine = weakJsEngine.lock();
62 if (!jsEngine)
63 return;
64 auto webRequestParams = jsEngine->TakeJsValues(paramsID);
65
66 AdblockPlus::JsContext context(*jsEngine);
67
68 auto resultObject = jsEngine->NewObject();
69 resultObject.SetProperty("status", response.status);
70 resultObject.SetProperty("responseStatus", response.responseStatus);
71 resultObject.SetProperty("responseText", response.responseText);
72
73 auto headersObject = jsEngine->NewObject();
74 for (const auto& header : response.responseHeaders)
75 {
76 headersObject.SetProperty(header.first, header.second);
77 }
78 resultObject.SetProperty("responseHeaders", headersObject);
79
80 webRequestParams[2].Call(resultObject);
81 };
82
83
84 if (jsEngine->webRequestLegacy)
85 {
86 std::thread([jsEngine, url, headers, getCallback]
87 {
88 getCallback(jsEngine->webRequestLegacy->GET(url, headers));
89 }).detach();
90 return;
91 }
92
93 jsEngine->webRequest->GET(url, headers, getCallback);
94 }
26 95
27 namespace 96 namespace
28 { 97 {
29 class WebRequestThread : public AdblockPlus::Thread
30 {
31 public:
32 WebRequestThread(const AdblockPlus::JsEnginePtr& jsEngine, const AdblockPlus ::JsValueList& arguments)
33 : Thread(true), jsEngine(jsEngine), url(arguments[0].AsString()),
34 callback(arguments[2])
35 {
36 if (!url.length())
37 throw std::runtime_error("Invalid string passed as first argument to GET ");
38
39 {
40 const AdblockPlus::JsValue& headersObj = arguments[1];
41 if (!headersObj.IsObject())
42 throw std::runtime_error("Second argument to GET must be an object");
43
44 std::vector<std::string> properties = headersObj.GetOwnPropertyNames();
45 for (const auto& header : properties)
46 {
47 std::string headerValue = headersObj.GetProperty(header).AsString();
48 if (header.length() && headerValue.length())
49 headers.push_back(std::pair<std::string, std::string>(header, header Value));
50 }
51 }
52
53 if (!callback.IsFunction())
54 throw std::runtime_error("Third argument to GET must be a function");
55 }
56
57 void Run()
58 {
59 AdblockPlus::ServerResponse result = jsEngine->GetWebRequest()->GET(url, h eaders);
60
61 AdblockPlus::JsContext context(*jsEngine);
62
63 auto resultObject = jsEngine->NewObject();
64 resultObject.SetProperty("status", result.status);
65 resultObject.SetProperty("responseStatus", result.responseStatus);
66 resultObject.SetProperty("responseText", result.responseText);
67
68 auto headersObject = jsEngine->NewObject();
69 for (const auto& header : result.responseHeaders)
70 {
71 headersObject.SetProperty(header.first, header.second);
72 }
73 resultObject.SetProperty("responseHeaders", headersObject);
74
75 AdblockPlus::JsValueList params;
76 params.push_back(resultObject);
77 callback.Call(params);
78 }
79
80 private:
81 AdblockPlus::JsEnginePtr jsEngine;
82 std::string url;
83 AdblockPlus::HeaderList headers;
84 AdblockPlus::JsValue callback;
85 };
86
87 v8::Handle<v8::Value> GETCallback(const v8::Arguments& arguments) 98 v8::Handle<v8::Value> GETCallback(const v8::Arguments& arguments)
88 { 99 {
89 WebRequestThread* thread;
90 try 100 try
91 { 101 {
92 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(a rguments); 102 AdblockPlus::JsEngine::ScheduleWebRequest(arguments);
93 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments) ; 103 } catch (const std::exception& e)
94 if (converted.size() != 3u)
95 throw std::runtime_error("GET requires exactly 3 arguments");
96 thread = new WebRequestThread(jsEngine, converted);
97 }
98 catch (const std::exception& e)
99 { 104 {
100 using AdblockPlus::Utils::ToV8String; 105 using AdblockPlus::Utils::ToV8String;
101 v8::Isolate* isolate = arguments.GetIsolate(); 106 v8::Isolate* isolate = arguments.GetIsolate();
102 return v8::ThrowException(ToV8String(isolate, e.what())); 107 return v8::ThrowException(ToV8String(isolate, e.what()));
103 } 108 }
104 thread->Start(); 109
105 return v8::Undefined(); 110 return v8::Undefined();
106 } 111 }
107 } 112 }
108 113
109 AdblockPlus::JsValue& AdblockPlus::WebRequestJsObject::Setup( 114 AdblockPlus::JsValue& AdblockPlus::WebRequestJsObject::Setup(
110 AdblockPlus::JsEngine& jsEngine, AdblockPlus::JsValue& obj) 115 AdblockPlus::JsEngine& jsEngine, AdblockPlus::JsValue& obj)
111 { 116 {
112 obj.SetProperty("GET", jsEngine.NewCallback(::GETCallback)); 117 obj.SetProperty("GET", jsEngine.NewCallback(::GETCallback));
113 return obj; 118 return obj;
114 } 119 }
OLDNEW
« 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