| OLD | NEW |
| 1 #include <map> | 1 #include <map> |
| 2 #include <AdblockPlus.h> | 2 #include <AdblockPlus/JsEngine.h> |
| 3 #include <AdblockPlus/JsValue.h> |
| 4 #include <AdblockPlus/WebRequest.h> |
| 3 #include "WebRequestJsObject.h" | 5 #include "WebRequestJsObject.h" |
| 4 #include "Thread.h" | 6 #include "Thread.h" |
| 5 | 7 |
| 6 namespace | 8 namespace |
| 7 { | 9 { |
| 8 std::string fromV8String(v8::Handle<v8::Value> value) | |
| 9 { | |
| 10 v8::String::Utf8Value stringValue(value); | |
| 11 if (stringValue.length()) | |
| 12 return std::string(*stringValue, stringValue.length()); | |
| 13 else | |
| 14 return std::string(); | |
| 15 } | |
| 16 | |
| 17 v8::Local<v8::String> toV8String(const std::string& str) | |
| 18 { | |
| 19 return v8::String::New(str.c_str(), str.length()); | |
| 20 } | |
| 21 | |
| 22 class WebRequestThread : public AdblockPlus::Thread | 10 class WebRequestThread : public AdblockPlus::Thread |
| 23 { | 11 { |
| 24 public: | 12 public: |
| 25 WebRequestThread(const v8::Arguments& arguments) | 13 WebRequestThread(AdblockPlus::JsEngine& jsEngine, AdblockPlus::JsValueList&
arguments) |
| 26 : isolate(v8::Isolate::GetCurrent()), | 14 : jsEngine(jsEngine), url(arguments[0]->AsString()) |
| 27 context(v8::Persistent<v8::Context>::New(isolate, v8::Context::GetCurr
ent())), | |
| 28 thisPtr(v8::Persistent<v8::Object>::New(isolate, arguments.Holder())), | |
| 29 url(fromV8String(arguments[0])) | |
| 30 { | 15 { |
| 31 const v8::Locker locker(isolate); | |
| 32 const v8::HandleScope handleScope; | |
| 33 | |
| 34 if (!url.length()) | 16 if (!url.length()) |
| 35 throw std::runtime_error("Invalid string passed as first argument to GET
"); | 17 throw std::runtime_error("Invalid string passed as first argument to GET
"); |
| 36 | 18 |
| 37 { | 19 { |
| 38 const v8::Local<v8::Value> value = arguments[1]; | 20 AdblockPlus::JsValuePtr headersObj = arguments[1]; |
| 39 if (!value->IsObject()) | 21 if (!headersObj->IsObject()) |
| 40 throw std::runtime_error("Second argument to GET must be an object"); | 22 throw std::runtime_error("Second argument to GET must be an object"); |
| 41 const v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(value); | 23 |
| 42 const v8::Local<v8::Array> properties = object->GetOwnPropertyNames(); | 24 std::vector<std::string> properties = headersObj->GetOwnPropertyNames(); |
| 43 for (unsigned i = 0; i < properties->Length(); i++) | 25 for (std::vector<std::string>::iterator it = properties.begin(); |
| 26 it != properties.end(); ++it) |
| 44 { | 27 { |
| 45 const v8::Local<v8::Value> property = properties->Get(i); | 28 std::string header = *it; |
| 46 std::string header = fromV8String(property); | 29 std::string headerValue = headersObj->GetProperty(header)->AsString(); |
| 47 std::string headerValue = fromV8String(object->Get(property)); | |
| 48 if (header.length() && headerValue.length()) | 30 if (header.length() && headerValue.length()) |
| 49 headers.push_back(std::pair<std::string, std::string>(header, header
Value)); | 31 headers.push_back(std::pair<std::string, std::string>(header, header
Value)); |
| 50 } | 32 } |
| 51 } | 33 } |
| 52 | 34 |
| 53 const v8::Local<v8::Value> callbackValue = arguments[2]; | 35 callback = arguments[2]; |
| 54 if (!callbackValue->IsFunction()) | 36 if (!callback->IsFunction()) |
| 55 throw std::runtime_error("Third argument to GET must be a function"); | 37 throw std::runtime_error("Third argument to GET must be a function"); |
| 56 callback = v8::Persistent<v8::Function>::New(isolate, | |
| 57 v8::Local<v8::Function>::Cast(callbackValue)); | |
| 58 | |
| 59 const v8::Local<const v8::External> external = | |
| 60 v8::Local<const v8::External>::Cast(arguments.Data()); | |
| 61 webRequest = static_cast<AdblockPlus::WebRequest* const>(external->Value()
); | |
| 62 } | |
| 63 | |
| 64 ~WebRequestThread() | |
| 65 { | |
| 66 context.Dispose(isolate); | |
| 67 thisPtr.Dispose(isolate); | |
| 68 callback.Dispose(isolate); | |
| 69 } | 38 } |
| 70 | 39 |
| 71 void Run() | 40 void Run() |
| 72 { | 41 { |
| 73 AdblockPlus::ServerResponse result = webRequest->GET(url, headers); | 42 AdblockPlus::ServerResponse result = jsEngine.GetWebRequest().GET(url, hea
ders); |
| 74 | 43 |
| 75 const v8::Locker locker(isolate); | 44 AdblockPlus::JsEngine::Context context(jsEngine); |
| 76 const v8::HandleScope handleScope; | |
| 77 const v8::Context::Scope contextScope(context); | |
| 78 v8::Local<v8::Object> resultObject = v8::Object::New(); | |
| 79 resultObject->Set(v8::String::New("status"), v8::Number::New(result.status
)); | |
| 80 resultObject->Set(v8::String::New("responseStatus"), v8::Integer::New(resu
lt.responseStatus)); | |
| 81 resultObject->Set(v8::String::New("responseText"), toV8String(result.respo
nseText)); | |
| 82 | 45 |
| 83 v8::Local<v8::Object> headersObject = v8::Object::New(); | 46 AdblockPlus::JsValuePtr resultObject = jsEngine.NewObject(); |
| 47 resultObject->SetProperty("status", result.status); |
| 48 resultObject->SetProperty("responseStatus", result.responseStatus); |
| 49 resultObject->SetProperty("responseText", result.responseText); |
| 50 |
| 51 AdblockPlus::JsValuePtr headersObject = jsEngine.NewObject(); |
| 84 for (AdblockPlus::HeaderList::iterator it = result.responseHeaders.begin()
; | 52 for (AdblockPlus::HeaderList::iterator it = result.responseHeaders.begin()
; |
| 85 it != result.responseHeaders.end(); ++it) | 53 it != result.responseHeaders.end(); ++it) |
| 86 { | 54 { |
| 87 headersObject->Set(toV8String(it->first), toV8String(it->second)); | 55 headersObject->SetProperty(it->first, it->second); |
| 88 } | 56 } |
| 89 resultObject->Set(v8::String::New("responseHeaders"), headersObject); | 57 resultObject->SetProperty("responseHeaders", headersObject); |
| 90 | 58 |
| 91 v8::Local<v8::Value> resultValue = resultObject; | 59 AdblockPlus::JsValueList params; |
| 92 callback->Call(thisPtr, 1, &resultValue); | 60 params.push_back(resultObject); |
| 61 callback->Call(params); |
| 93 delete this; | 62 delete this; |
| 94 } | 63 } |
| 95 | 64 |
| 96 private: | 65 private: |
| 97 v8::Isolate* const isolate; | 66 AdblockPlus::JsEngine& jsEngine; |
| 98 v8::Persistent<v8::Context> context; | |
| 99 v8::Persistent<v8::Object> thisPtr; | |
| 100 std::string url; | 67 std::string url; |
| 101 AdblockPlus::HeaderList headers; | 68 AdblockPlus::HeaderList headers; |
| 102 v8::Persistent<v8::Function> callback; | 69 AdblockPlus::JsValuePtr callback; |
| 103 AdblockPlus::WebRequest* webRequest; | |
| 104 }; | 70 }; |
| 105 | 71 |
| 106 v8::Handle<v8::Value> GETCallback(const v8::Arguments& arguments) | 72 v8::Handle<v8::Value> GETCallback(const v8::Arguments& arguments) |
| 107 { | 73 { |
| 108 WebRequestThread* thread; | 74 WebRequestThread* thread; |
| 109 try | 75 try |
| 110 { | 76 { |
| 111 if (arguments.Length() != 3u) | 77 AdblockPlus::JsEngine& jsEngine = AdblockPlus::JsEngine::FromArguments(arg
uments); |
| 78 AdblockPlus::JsValueList converted = jsEngine.ConvertArguments(arguments); |
| 79 if (converted.size() != 3u) |
| 112 throw std::runtime_error("GET requires exactly 3 arguments"); | 80 throw std::runtime_error("GET requires exactly 3 arguments"); |
| 113 thread = new WebRequestThread(arguments); | 81 thread = new WebRequestThread(jsEngine, converted); |
| 114 } | 82 } |
| 115 catch (const std::exception& e) | 83 catch (const std::exception& e) |
| 116 { | 84 { |
| 117 return v8::ThrowException(v8::String::New(e.what())); | 85 return v8::ThrowException(v8::String::New(e.what())); |
| 118 } | 86 } |
| 119 thread->Start(); | 87 thread->Start(); |
| 120 return v8::Undefined(); | 88 return v8::Undefined(); |
| 121 } | 89 } |
| 122 } | 90 } |
| 123 | 91 |
| 124 v8::Handle<v8::ObjectTemplate> AdblockPlus::WebRequestJsObject::Create( | 92 v8::Handle<v8::ObjectTemplate> AdblockPlus::WebRequestJsObject::Create( |
| 125 AdblockPlus::WebRequest& webRequest) | 93 AdblockPlus::JsEngine& jsEngine) |
| 126 { | 94 { |
| 127 v8::HandleScope handleScope; | 95 v8::HandleScope handleScope; |
| 128 const v8::Handle<v8::ObjectTemplate> request = v8::ObjectTemplate::New(); | 96 const v8::Handle<v8::ObjectTemplate> request = v8::ObjectTemplate::New(); |
| 129 const v8::Handle<v8::FunctionTemplate> getFunction = | 97 const v8::Handle<v8::FunctionTemplate> getFunction = |
| 130 v8::FunctionTemplate::New(::GETCallback, | 98 v8::FunctionTemplate::New(::GETCallback, |
| 131 v8::External::New(&webRequest)); | 99 v8::External::New(&jsEngine)); |
| 132 request->Set(v8::String::New("GET"), getFunction); | 100 request->Set(v8::String::New("GET"), getFunction); |
| 133 return handleScope.Close(request); | 101 return handleScope.Close(request); |
| 134 } | 102 } |
| OLD | NEW |