OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
3 * Copyright (C) 2006-2014 Eyeo GmbH | 3 * Copyright (C) 2006-2014 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" | 23 #include "Thread.h" |
24 #include "WebRequestJsObject.h" | 24 #include "WebRequestJsObject.h" |
| 25 #include "Utils.h" |
25 | 26 |
26 namespace | 27 namespace |
27 { | 28 { |
28 class WebRequestThread : public AdblockPlus::Thread | 29 class WebRequestThread : public AdblockPlus::Thread |
29 { | 30 { |
30 public: | 31 public: |
31 WebRequestThread(AdblockPlus::JsEnginePtr jsEngine, AdblockPlus::JsValueList
& arguments) | 32 WebRequestThread(AdblockPlus::JsEnginePtr jsEngine, AdblockPlus::JsValueList
& arguments) |
32 : jsEngine(jsEngine), url(arguments[0]->AsString()) | 33 : jsEngine(jsEngine), url(arguments[0]->AsString()) |
33 { | 34 { |
34 if (!url.length()) | 35 if (!url.length()) |
(...skipping 17 matching lines...) Expand all Loading... |
52 | 53 |
53 callback = arguments[2]; | 54 callback = arguments[2]; |
54 if (!callback->IsFunction()) | 55 if (!callback->IsFunction()) |
55 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"); |
56 } | 57 } |
57 | 58 |
58 void Run() | 59 void Run() |
59 { | 60 { |
60 AdblockPlus::ServerResponse result = jsEngine->GetWebRequest()->GET(url, h
eaders); | 61 AdblockPlus::ServerResponse result = jsEngine->GetWebRequest()->GET(url, h
eaders); |
61 | 62 |
62 AdblockPlus::JsContext context(jsEngine); | 63 AdblockPlus::JsContext context{*jsEngine}; |
63 | 64 |
64 AdblockPlus::JsValuePtr resultObject = jsEngine->NewObject(); | 65 AdblockPlus::JsValuePtr resultObject = jsEngine->NewObject(); |
65 resultObject->SetProperty("status", result.status); | 66 resultObject->SetProperty("status", result.status); |
66 resultObject->SetProperty("responseStatus", result.responseStatus); | 67 resultObject->SetProperty("responseStatus", result.responseStatus); |
67 resultObject->SetProperty("responseText", result.responseText); | 68 resultObject->SetProperty("responseText", result.responseText); |
68 | 69 |
69 AdblockPlus::JsValuePtr headersObject = jsEngine->NewObject(); | 70 AdblockPlus::JsValuePtr headersObject = jsEngine->NewObject(); |
70 for (AdblockPlus::HeaderList::iterator it = result.responseHeaders.begin()
; | 71 for (AdblockPlus::HeaderList::iterator it = result.responseHeaders.begin()
; |
71 it != result.responseHeaders.end(); ++it) | 72 it != result.responseHeaders.end(); ++it) |
72 { | 73 { |
73 headersObject->SetProperty(it->first, it->second); | 74 headersObject->SetProperty(it->first, it->second); |
74 } | 75 } |
75 resultObject->SetProperty("responseHeaders", headersObject); | 76 resultObject->SetProperty("responseHeaders", headersObject); |
76 | 77 |
77 AdblockPlus::JsValueList params; | 78 AdblockPlus::JsValueList params; |
78 params.push_back(resultObject); | 79 params.push_back(resultObject); |
79 callback->Call(params); | 80 callback->Call(params); |
80 delete this; | 81 delete this; |
81 } | 82 } |
82 | 83 |
83 private: | 84 private: |
84 AdblockPlus::JsEnginePtr jsEngine; | 85 AdblockPlus::JsEnginePtr jsEngine; |
85 std::string url; | 86 std::string url; |
86 AdblockPlus::HeaderList headers; | 87 AdblockPlus::HeaderList headers; |
87 AdblockPlus::JsValuePtr callback; | 88 AdblockPlus::JsValuePtr callback; |
88 }; | 89 }; |
89 | 90 |
90 v8::Handle<v8::Value> GETCallback(const v8::Arguments& arguments) | 91 void GETCallback(const v8::FunctionCallbackInfo<v8::Value>& info) |
91 { | 92 { |
92 WebRequestThread* thread; | 93 auto isolate = info.GetIsolate(); |
93 try | 94 try |
94 { | 95 { |
95 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(a
rguments); | 96 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(i
nfo); |
96 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments)
; | 97 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(info); |
97 if (converted.size() != 3u) | 98 if (converted.size() != 3u) |
98 throw std::runtime_error("GET requires exactly 3 arguments"); | 99 throw std::runtime_error("GET requires exactly 3 arguments"); |
99 thread = new WebRequestThread(jsEngine, converted); | 100 WebRequestThread* thread = new WebRequestThread(jsEngine, converted); |
| 101 thread->Start(); |
100 } | 102 } |
101 catch (const std::exception& e) | 103 catch (const std::exception& e) |
102 { | 104 { |
103 return v8::ThrowException(v8::String::New(e.what())); | 105 isolate->ThrowException(AdblockPlus::Utils::ToV8String(isolate, e.what()))
; |
104 } | 106 } |
105 thread->Start(); | |
106 return v8::Undefined(); | |
107 } | 107 } |
108 } | 108 } |
109 | 109 |
110 AdblockPlus::JsValuePtr AdblockPlus::WebRequestJsObject::Setup( | 110 AdblockPlus::JsValuePtr AdblockPlus::WebRequestJsObject::Setup( |
111 AdblockPlus::JsEnginePtr jsEngine, AdblockPlus::JsValuePtr obj) | 111 AdblockPlus::JsEnginePtr jsEngine, AdblockPlus::JsValuePtr obj) |
112 { | 112 { |
113 obj->SetProperty("GET", jsEngine->NewCallback(::GETCallback)); | 113 obj->SetProperty("GET", jsEngine->NewCallback(::GETCallback)); |
114 return obj; | 114 return obj; |
115 } | 115 } |
OLD | NEW |