| OLD | NEW |
| 1 #include <map> | 1 #include <map> |
| 2 #include <AdblockPlus/JsEngine.h> | |
| 3 #include <AdblockPlus/JsValue.h> | 2 #include <AdblockPlus/JsValue.h> |
| 4 #include <AdblockPlus/WebRequest.h> | 3 #include <AdblockPlus/WebRequest.h> |
| 5 #include "WebRequestJsObject.h" | 4 #include "WebRequestJsObject.h" |
| 6 #include "Thread.h" | 5 #include "Thread.h" |
| 7 | 6 |
| 8 namespace | 7 namespace |
| 9 { | 8 { |
| 10 class WebRequestThread : public AdblockPlus::Thread | 9 class WebRequestThread : public AdblockPlus::Thread |
| 11 { | 10 { |
| 12 public: | 11 public: |
| 13 WebRequestThread(AdblockPlus::JsEngine& jsEngine, AdblockPlus::JsValueList&
arguments) | 12 WebRequestThread(AdblockPlus::JsEnginePtr jsEngine, AdblockPlus::JsValueList
& arguments) |
| 14 : jsEngine(jsEngine), url(arguments[0]->AsString()) | 13 : jsEngine(jsEngine), url(arguments[0]->AsString()) |
| 15 { | 14 { |
| 16 if (!url.length()) | 15 if (!url.length()) |
| 17 throw std::runtime_error("Invalid string passed as first argument to GET
"); | 16 throw std::runtime_error("Invalid string passed as first argument to GET
"); |
| 18 | 17 |
| 19 { | 18 { |
| 20 AdblockPlus::JsValuePtr headersObj = arguments[1]; | 19 AdblockPlus::JsValuePtr headersObj = arguments[1]; |
| 21 if (!headersObj->IsObject()) | 20 if (!headersObj->IsObject()) |
| 22 throw std::runtime_error("Second argument to GET must be an object"); | 21 throw std::runtime_error("Second argument to GET must be an object"); |
| 23 | 22 |
| 24 std::vector<std::string> properties = headersObj->GetOwnPropertyNames(); | 23 std::vector<std::string> properties = headersObj->GetOwnPropertyNames(); |
| 25 for (std::vector<std::string>::iterator it = properties.begin(); | 24 for (std::vector<std::string>::iterator it = properties.begin(); |
| 26 it != properties.end(); ++it) | 25 it != properties.end(); ++it) |
| 27 { | 26 { |
| 28 std::string header = *it; | 27 std::string header = *it; |
| 29 std::string headerValue = headersObj->GetProperty(header)->AsString(); | 28 std::string headerValue = headersObj->GetProperty(header)->AsString(); |
| 30 if (header.length() && headerValue.length()) | 29 if (header.length() && headerValue.length()) |
| 31 headers.push_back(std::pair<std::string, std::string>(header, header
Value)); | 30 headers.push_back(std::pair<std::string, std::string>(header, header
Value)); |
| 32 } | 31 } |
| 33 } | 32 } |
| 34 | 33 |
| 35 callback = arguments[2]; | 34 callback = arguments[2]; |
| 36 if (!callback->IsFunction()) | 35 if (!callback->IsFunction()) |
| 37 throw std::runtime_error("Third argument to GET must be a function"); | 36 throw std::runtime_error("Third argument to GET must be a function"); |
| 38 } | 37 } |
| 39 | 38 |
| 40 void Run() | 39 void Run() |
| 41 { | 40 { |
| 42 AdblockPlus::ServerResponse result = jsEngine.GetWebRequest()->GET(url, he
aders); | 41 AdblockPlus::ServerResponse result = jsEngine->GetWebRequest()->GET(url, h
eaders); |
| 43 | 42 |
| 44 AdblockPlus::JsEngine::Context context(jsEngine); | 43 AdblockPlus::JsEngine::Context context(jsEngine); |
| 45 | 44 |
| 46 AdblockPlus::JsValuePtr resultObject = jsEngine.NewObject(); | 45 AdblockPlus::JsValuePtr resultObject = jsEngine->NewObject(); |
| 47 resultObject->SetProperty("status", result.status); | 46 resultObject->SetProperty("status", result.status); |
| 48 resultObject->SetProperty("responseStatus", result.responseStatus); | 47 resultObject->SetProperty("responseStatus", result.responseStatus); |
| 49 resultObject->SetProperty("responseText", result.responseText); | 48 resultObject->SetProperty("responseText", result.responseText); |
| 50 | 49 |
| 51 AdblockPlus::JsValuePtr headersObject = jsEngine.NewObject(); | 50 AdblockPlus::JsValuePtr headersObject = jsEngine->NewObject(); |
| 52 for (AdblockPlus::HeaderList::iterator it = result.responseHeaders.begin()
; | 51 for (AdblockPlus::HeaderList::iterator it = result.responseHeaders.begin()
; |
| 53 it != result.responseHeaders.end(); ++it) | 52 it != result.responseHeaders.end(); ++it) |
| 54 { | 53 { |
| 55 headersObject->SetProperty(it->first, it->second); | 54 headersObject->SetProperty(it->first, it->second); |
| 56 } | 55 } |
| 57 resultObject->SetProperty("responseHeaders", headersObject); | 56 resultObject->SetProperty("responseHeaders", headersObject); |
| 58 | 57 |
| 59 AdblockPlus::JsValueList params; | 58 AdblockPlus::JsValueList params; |
| 60 params.push_back(resultObject); | 59 params.push_back(resultObject); |
| 61 callback->Call(params); | 60 callback->Call(params); |
| 62 delete this; | 61 delete this; |
| 63 } | 62 } |
| 64 | 63 |
| 65 private: | 64 private: |
| 66 AdblockPlus::JsEngine& jsEngine; | 65 AdblockPlus::JsEnginePtr jsEngine; |
| 67 std::string url; | 66 std::string url; |
| 68 AdblockPlus::HeaderList headers; | 67 AdblockPlus::HeaderList headers; |
| 69 AdblockPlus::JsValuePtr callback; | 68 AdblockPlus::JsValuePtr callback; |
| 70 }; | 69 }; |
| 71 | 70 |
| 72 v8::Handle<v8::Value> GETCallback(const v8::Arguments& arguments) | 71 v8::Handle<v8::Value> GETCallback(const v8::Arguments& arguments) |
| 73 { | 72 { |
| 74 WebRequestThread* thread; | 73 WebRequestThread* thread; |
| 75 try | 74 try |
| 76 { | 75 { |
| 77 AdblockPlus::JsEngine& jsEngine = AdblockPlus::JsEngine::FromArguments(arg
uments); | 76 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(a
rguments); |
| 78 AdblockPlus::JsValueList converted = jsEngine.ConvertArguments(arguments); | 77 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments)
; |
| 79 if (converted.size() != 3u) | 78 if (converted.size() != 3u) |
| 80 throw std::runtime_error("GET requires exactly 3 arguments"); | 79 throw std::runtime_error("GET requires exactly 3 arguments"); |
| 81 thread = new WebRequestThread(jsEngine, converted); | 80 thread = new WebRequestThread(jsEngine, converted); |
| 82 } | 81 } |
| 83 catch (const std::exception& e) | 82 catch (const std::exception& e) |
| 84 { | 83 { |
| 85 return v8::ThrowException(v8::String::New(e.what())); | 84 return v8::ThrowException(v8::String::New(e.what())); |
| 86 } | 85 } |
| 87 thread->Start(); | 86 thread->Start(); |
| 88 return v8::Undefined(); | 87 return v8::Undefined(); |
| 89 } | 88 } |
| 90 } | 89 } |
| 91 | 90 |
| 92 AdblockPlus::JsValuePtr AdblockPlus::WebRequestJsObject::Setup( | 91 AdblockPlus::JsValuePtr AdblockPlus::WebRequestJsObject::Setup( |
| 93 AdblockPlus::JsEngine& jsEngine, AdblockPlus::JsValuePtr obj) | 92 AdblockPlus::JsEnginePtr jsEngine, AdblockPlus::JsValuePtr obj) |
| 94 { | 93 { |
| 95 obj->SetProperty("GET", jsEngine.NewCallback(::GETCallback)); | 94 obj->SetProperty("GET", jsEngine->NewCallback(::GETCallback)); |
| 96 return obj; | 95 return obj; |
| 97 } | 96 } |
| OLD | NEW |