OLD | NEW |
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-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 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/JsValue.h> |
20 #include <AdblockPlus/WebRequest.h> | 20 #include <AdblockPlus/WebRequest.h> |
21 | 21 |
22 #include "JsContext.h" | 22 #include "JsContext.h" |
23 #include "Thread.h" | |
24 #include "Utils.h" | 23 #include "Utils.h" |
| 24 #include "Scheduler.h" |
25 #include "WebRequestJsObject.h" | 25 #include "WebRequestJsObject.h" |
26 | 26 |
27 namespace | 27 namespace |
28 { | 28 { |
29 class WebRequestThread : public AdblockPlus::Thread | 29 class WebRequestTask |
30 { | 30 { |
31 public: | 31 public: |
32 WebRequestThread(AdblockPlus::JsEnginePtr jsEngine, AdblockPlus::JsValueList
& arguments) | 32 WebRequestTask(AdblockPlus::JsEnginePtr jsEngine, AdblockPlus::JsValueList&
arguments) |
33 : Thread(true), jsEngine(jsEngine), url(arguments[0]->AsString()) | 33 : jsEngine(jsEngine), url(arguments[0]->AsString()) |
34 { | 34 { |
35 if (!url.length()) | 35 if (!url.length()) |
36 throw std::runtime_error("Invalid string passed as first argument to GET
"); | 36 throw std::runtime_error("Invalid string passed as first argument to GET
"); |
37 | 37 |
38 { | 38 { |
39 AdblockPlus::JsValuePtr headersObj = arguments[1]; | 39 AdblockPlus::JsValuePtr headersObj = arguments[1]; |
40 if (!headersObj->IsObject()) | 40 if (!headersObj->IsObject()) |
41 throw std::runtime_error("Second argument to GET must be an object"); | 41 throw std::runtime_error("Second argument to GET must be an object"); |
42 | 42 |
43 std::vector<std::string> properties = headersObj->GetOwnPropertyNames(); | 43 std::vector<std::string> properties = headersObj->GetOwnPropertyNames(); |
44 for (std::vector<std::string>::iterator it = properties.begin(); | 44 for (std::vector<std::string>::iterator it = properties.begin(); |
45 it != properties.end(); ++it) | 45 it != properties.end(); ++it) |
46 { | 46 { |
47 std::string header = *it; | 47 std::string header = *it; |
48 std::string headerValue = headersObj->GetProperty(header)->AsString(); | 48 std::string headerValue = headersObj->GetProperty(header)->AsString(); |
49 if (header.length() && headerValue.length()) | 49 if (header.length() && headerValue.length()) |
50 headers.push_back(std::pair<std::string, std::string>(header, header
Value)); | 50 headers.push_back(std::pair<std::string, std::string>(header, header
Value)); |
51 } | 51 } |
52 } | 52 } |
53 | 53 |
54 callback = arguments[2]; | 54 callback = arguments[2]; |
55 if (!callback->IsFunction()) | 55 if (!callback->IsFunction()) |
56 throw std::runtime_error("Third argument to GET must be a function"); | 56 throw std::runtime_error("Third argument to GET must be a function"); |
57 } | 57 } |
58 | 58 |
59 void Run() | 59 void operator()() |
60 { | 60 { |
61 AdblockPlus::ServerResponse result = jsEngine->GetWebRequest()->GET(url, h
eaders); | 61 AdblockPlus::ServerResponse result = jsEngine->GetWebRequest()->GET(url, h
eaders); |
62 | 62 |
63 AdblockPlus::JsContext context(jsEngine); | 63 AdblockPlus::JsContext context(jsEngine); |
64 | 64 |
65 AdblockPlus::JsValuePtr resultObject = jsEngine->NewObject(); | 65 AdblockPlus::JsValuePtr resultObject = jsEngine->NewObject(); |
66 resultObject->SetProperty("status", result.status); | 66 resultObject->SetProperty("status", result.status); |
67 resultObject->SetProperty("responseStatus", result.responseStatus); | 67 resultObject->SetProperty("responseStatus", result.responseStatus); |
68 resultObject->SetProperty("responseText", result.responseText); | 68 resultObject->SetProperty("responseText", result.responseText); |
69 | 69 |
(...skipping 12 matching lines...) Expand all Loading... |
82 | 82 |
83 private: | 83 private: |
84 AdblockPlus::JsEnginePtr jsEngine; | 84 AdblockPlus::JsEnginePtr jsEngine; |
85 std::string url; | 85 std::string url; |
86 AdblockPlus::HeaderList headers; | 86 AdblockPlus::HeaderList headers; |
87 AdblockPlus::JsValuePtr callback; | 87 AdblockPlus::JsValuePtr callback; |
88 }; | 88 }; |
89 | 89 |
90 v8::Handle<v8::Value> GETCallback(const v8::Arguments& arguments) | 90 v8::Handle<v8::Value> GETCallback(const v8::Arguments& arguments) |
91 { | 91 { |
92 WebRequestThread* thread; | 92 std::shared_ptr<WebRequestTask> thread; |
93 try | 93 try |
94 { | 94 { |
95 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(a
rguments); | 95 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(a
rguments); |
96 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments)
; | 96 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments)
; |
97 if (converted.size() != 3u) | 97 if (converted.size() != 3u) |
98 throw std::runtime_error("GET requires exactly 3 arguments"); | 98 throw std::runtime_error("GET requires exactly 3 arguments"); |
99 thread = new WebRequestThread(jsEngine, converted); | 99 thread = std::make_shared<WebRequestTask>(jsEngine, converted); |
100 } | 100 } |
101 catch (const std::exception& e) | 101 catch (const std::exception& e) |
102 { | 102 { |
103 using AdblockPlus::Utils::ToV8String; | 103 using AdblockPlus::Utils::ToV8String; |
104 v8::Isolate* isolate = arguments.GetIsolate(); | 104 v8::Isolate* isolate = arguments.GetIsolate(); |
105 return v8::ThrowException(ToV8String(isolate, e.what())); | 105 return v8::ThrowException(ToV8String(isolate, e.what())); |
106 } | 106 } |
107 thread->Start(); | 107 AdblockPlus::Scheduler::StartImmediatelyInSingleUseThread(AdblockPlus::MakeH
eapFunction(thread)); |
108 return v8::Undefined(); | 108 return v8::Undefined(); |
109 } | 109 } |
110 } | 110 } |
111 | 111 |
112 AdblockPlus::JsValuePtr AdblockPlus::WebRequestJsObject::Setup( | 112 AdblockPlus::JsValuePtr AdblockPlus::WebRequestJsObject::Setup( |
113 AdblockPlus::JsEnginePtr jsEngine, AdblockPlus::JsValuePtr obj) | 113 AdblockPlus::JsEnginePtr jsEngine, AdblockPlus::JsValuePtr obj) |
114 { | 114 { |
115 obj->SetProperty("GET", jsEngine->NewCallback(::GETCallback)); | 115 obj->SetProperty("GET", jsEngine->NewCallback(::GETCallback)); |
116 return obj; | 116 return obj; |
117 } | 117 } |
OLD | NEW |