| Left: | ||
| Right: | 
| LEFT | RIGHT | 
|---|---|
| 1 #include <map> | 1 #include <map> | 
| 2 #include <AdblockPlus.h> | 2 #include <AdblockPlus.h> | 
| 3 #include "WebRequestJsObject.h" | 3 #include "WebRequestJsObject.h" | 
| 4 #include "Thread.h" | 4 #include "Thread.h" | 
| 5 | 5 | 
| 6 namespace | 6 namespace | 
| 7 { | 7 { | 
| 8 std::string fromV8String(v8::Handle<v8::Value> value) | 8 std::string fromV8String(v8::Handle<v8::Value> value) | 
| 9 { | 9 { | 
| 10 v8::String::Utf8Value stringValue(value); | 10 v8::String::Utf8Value stringValue(value); | 
| (...skipping 13 matching lines...) Expand all Loading... | |
| 24 public: | 24 public: | 
| 25 WebRequestThread(const v8::Arguments& arguments) | 25 WebRequestThread(const v8::Arguments& arguments) | 
| 26 : isolate(v8::Isolate::GetCurrent()), | 26 : isolate(v8::Isolate::GetCurrent()), | 
| 27 context(v8::Persistent<v8::Context>::New(isolate, v8::Context::GetCurr ent())), | 27 context(v8::Persistent<v8::Context>::New(isolate, v8::Context::GetCurr ent())), | 
| 28 thisPtr(v8::Persistent<v8::Object>::New(isolate, arguments.Holder())), | 28 thisPtr(v8::Persistent<v8::Object>::New(isolate, arguments.Holder())), | 
| 29 url(fromV8String(arguments[0])) | 29 url(fromV8String(arguments[0])) | 
| 30 { | 30 { | 
| 31 const v8::Locker locker(isolate); | 31 const v8::Locker locker(isolate); | 
| 32 const v8::HandleScope handleScope; | 32 const v8::HandleScope handleScope; | 
| 33 | 33 | 
| 34 if (!url.length()) | 34 if (!url.length()) | 
| 
 
Felix Dahlke
2013/04/11 09:33:42
Another refactoring I'm planning to do is to not p
 
Wladimir Palant
2013/04/11 16:32:33
I'll leave it like that for now.
 
 | |
| 35 throw std::runtime_error("Invalid string passed as first argument to GET "); | 35 throw std::runtime_error("Invalid string passed as first argument to GET "); | 
| 36 | 36 | 
| 37 { | 37 { | 
| 38 const v8::Local<v8::Value> value = arguments[1]; | 38 const v8::Local<v8::Value> value = arguments[1]; | 
| 39 if (!value->IsObject()) | 39 if (!value->IsObject()) | 
| 40 throw std::runtime_error("Second argument to GET must be an object"); | 40 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); | 41 const v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(value); | 
| 42 const v8::Local<v8::Array> properties = object->GetOwnPropertyNames(); | 42 const v8::Local<v8::Array> properties = object->GetOwnPropertyNames(); | 
| 43 for (unsigned i = 0; i < properties->Length(); i++) | 43 for (unsigned i = 0; i < properties->Length(); i++) | 
| 44 { | 44 { | 
| (...skipping 28 matching lines...) Expand all Loading... | |
| 73 AdblockPlus::ServerResponse result = webRequest->GET(url, headers); | 73 AdblockPlus::ServerResponse result = webRequest->GET(url, headers); | 
| 74 | 74 | 
| 75 const v8::Locker locker(isolate); | 75 const v8::Locker locker(isolate); | 
| 76 const v8::HandleScope handleScope; | 76 const v8::HandleScope handleScope; | 
| 77 const v8::Context::Scope contextScope(context); | 77 const v8::Context::Scope contextScope(context); | 
| 78 v8::Local<v8::Object> resultObject = v8::Object::New(); | 78 v8::Local<v8::Object> resultObject = v8::Object::New(); | 
| 79 resultObject->Set(v8::String::New("responseStatus"), v8::Integer::New(resu lt.responseStatus)); | 79 resultObject->Set(v8::String::New("responseStatus"), v8::Integer::New(resu lt.responseStatus)); | 
| 80 resultObject->Set(v8::String::New("responseText"), toV8String(result.respo nseText)); | 80 resultObject->Set(v8::String::New("responseText"), toV8String(result.respo nseText)); | 
| 81 | 81 | 
| 82 v8::Local<v8::Object> headersObject = v8::Object::New(); | 82 v8::Local<v8::Object> headersObject = v8::Object::New(); | 
| 83 for (AdblockPlus::HeadersList::iterator it = result.responseHeaders.begin( ); | 83 for (AdblockPlus::HeaderList::iterator it = result.responseHeaders.begin() ; | 
| 84 it != result.responseHeaders.end(); ++it) | 84 it != result.responseHeaders.end(); ++it) | 
| 85 { | 85 { | 
| 86 headersObject->Set(toV8String(it->first), toV8String(it->second)); | 86 headersObject->Set(toV8String(it->first), toV8String(it->second)); | 
| 87 } | 87 } | 
| 88 resultObject->Set(v8::String::New("responseHeaders"), headersObject); | 88 resultObject->Set(v8::String::New("responseHeaders"), headersObject); | 
| 89 | 89 | 
| 90 v8::Local<v8::Value> resultValue = resultObject; | 90 v8::Local<v8::Value> resultValue = resultObject; | 
| 91 callback->Call(thisPtr, 1, &resultValue); | 91 callback->Call(thisPtr, 1, &resultValue); | 
| 92 delete this; | 92 delete this; | 
| 93 } | 93 } | 
| 94 | 94 | 
| 95 private: | 95 private: | 
| 96 v8::Isolate* const isolate; | 96 v8::Isolate* const isolate; | 
| 97 v8::Persistent<v8::Context> context; | 97 v8::Persistent<v8::Context> context; | 
| 98 v8::Persistent<v8::Object> thisPtr; | 98 v8::Persistent<v8::Object> thisPtr; | 
| 99 std::string url; | 99 std::string url; | 
| 100 AdblockPlus::HeadersList headers; | 100 AdblockPlus::HeaderList headers; | 
| 101 v8::Persistent<v8::Function> callback; | 101 v8::Persistent<v8::Function> callback; | 
| 102 AdblockPlus::WebRequest* webRequest; | 102 AdblockPlus::WebRequest* webRequest; | 
| 103 }; | 103 }; | 
| 104 | 104 | 
| 105 v8::Handle<v8::Value> GETCallback(const v8::Arguments& arguments) | 105 v8::Handle<v8::Value> GETCallback(const v8::Arguments& arguments) | 
| 106 { | 106 { | 
| 107 WebRequestThread* thread; | 107 WebRequestThread* thread; | 
| 108 try | 108 try | 
| 109 { | 109 { | 
| 110 if (arguments.Length() != 3u) | 110 if (arguments.Length() != 3u) | 
| 111 throw std::runtime_error("GET requires exactly 3 arguments"); | 111 throw std::runtime_error("GET requires exactly 3 arguments"); | 
| 112 thread = new WebRequestThread(arguments); | 112 thread = new WebRequestThread(arguments); | 
| 113 } | 113 } | 
| 114 catch (const std::exception& e) | 114 catch (const std::exception& e) | 
| 115 { | 115 { | 
| 116 return v8::ThrowException(v8::String::New(e.what())); | 116 return v8::ThrowException(v8::String::New(e.what())); | 
| 117 } | 117 } | 
| 118 thread->Start(); | 118 thread->Start(); | 
| 119 return v8::Undefined(); | 119 return v8::Undefined(); | 
| 120 } | 120 } | 
| 121 } | 121 } | 
| 122 | 122 | 
| 123 v8::Handle<v8::ObjectTemplate> AdblockPlus::WebRequestJsObject::Create( | 123 v8::Handle<v8::ObjectTemplate> AdblockPlus::WebRequestJsObject::Create( | 
| 124 AdblockPlus::WebRequest& webRequest) | 124 AdblockPlus::WebRequest& webRequest) | 
| 125 { | 125 { | 
| 126 v8::HandleScope handleScope; | 126 v8::HandleScope handleScope; | 
| 127 const v8::Handle<v8::ObjectTemplate> request = v8::ObjectTemplate::New(); | 127 const v8::Handle<v8::ObjectTemplate> request = v8::ObjectTemplate::New(); | 
| 128 const v8::Handle<v8::FunctionTemplate> getFunction = | 128 const v8::Handle<v8::FunctionTemplate> getFunction = | 
| 129 v8::FunctionTemplate::New(::GETCallback, | 129 v8::FunctionTemplate::New(::GETCallback, | 
| 
 
Felix Dahlke
2013/04/11 09:33:42
The :: is not necessary here, was just necessary i
 
Wladimir Palant
2013/04/11 16:32:33
:: is actually considered good style to indicate r
 
 | |
| 130 v8::External::New(&webRequest)); | 130 v8::External::New(&webRequest)); | 
| 131 request->Set(v8::String::New("GET"), getFunction); | 131 request->Set(v8::String::New("GET"), getFunction); | 
| 132 return handleScope.Close(request); | 132 return handleScope.Close(request); | 
| 133 } | 133 } | 
| LEFT | RIGHT |