Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: src/WebRequestJsObject.cpp

Issue 10259001: XMLHttpRequest API (Closed)
Patch Set: Created April 10, 2013, 3:19 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 #include <map>
2 #include <AdblockPlus.h>
3 #include "WebRequestJsObject.h"
4 #include "Thread.h"
5
6 namespace
7 {
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
23 {
24 public:
25 WebRequestThread(const v8::Arguments& arguments)
26 : isolate(v8::Isolate::GetCurrent()),
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 {
31 const v8::Locker locker(isolate);
32 const v8::HandleScope handleScope;
33
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 ");
36
37 {
38 const v8::Local<v8::Value> value = arguments[1];
39 if (!value->IsObject())
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);
42 const v8::Local<v8::Array> properties = object->GetOwnPropertyNames();
43 for (unsigned i = 0; i < properties->Length(); i++)
44 {
45 const v8::Local<v8::Value> property = properties->Get(i);
46 std::string header = fromV8String(property);
47 std::string headerValue = fromV8String(object->Get(property));
48 if (header.length() && headerValue.length())
49 headers.push_back(std::pair<std::string, std::string>(header, header Value));
50 }
51 }
52
53 const v8::Local<v8::Value> callbackValue = arguments[2];
54 if (!callbackValue->IsFunction())
55 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 }
70
71 void Run()
72 {
73 AdblockPlus::ServerResponse result = webRequest->GET(url, headers);
74
75 const v8::Locker locker(isolate);
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("responseStatus"), v8::Integer::New(resu lt.responseStatus));
80 resultObject->Set(v8::String::New("responseText"), toV8String(result.respo nseText));
81
82 v8::Local<v8::Object> headersObject = v8::Object::New();
83 for (AdblockPlus::HeadersList::iterator it = result.responseHeaders.begin( );
84 it != result.responseHeaders.end(); ++it)
85 {
86 headersObject->Set(toV8String(it->first), toV8String(it->second));
87 }
88 resultObject->Set(v8::String::New("responseHeaders"), headersObject);
89
90 v8::Local<v8::Value> resultValue = resultObject;
91 callback->Call(thisPtr, 1, &resultValue);
92 delete this;
93 }
94
95 private:
96 v8::Isolate* const isolate;
97 v8::Persistent<v8::Context> context;
98 v8::Persistent<v8::Object> thisPtr;
99 std::string url;
100 AdblockPlus::HeadersList headers;
101 v8::Persistent<v8::Function> callback;
102 AdblockPlus::WebRequest* webRequest;
103 };
104
105 v8::Handle<v8::Value> GETCallback(const v8::Arguments& arguments)
106 {
107 WebRequestThread* thread;
108 try
109 {
110 if (arguments.Length() != 3u)
111 throw std::runtime_error("GET requires exactly 3 arguments");
112 thread = new WebRequestThread(arguments);
113 }
114 catch (const std::exception& e)
115 {
116 return v8::ThrowException(v8::String::New(e.what()));
117 }
118 thread->Start();
119 return v8::Undefined();
120 }
121 }
122
123 v8::Handle<v8::ObjectTemplate> AdblockPlus::WebRequestJsObject::Create(
124 AdblockPlus::WebRequest& webRequest)
125 {
126 v8::HandleScope handleScope;
127 const v8::Handle<v8::ObjectTemplate> request = v8::ObjectTemplate::New();
128 const v8::Handle<v8::FunctionTemplate> getFunction =
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));
131 request->Set(v8::String::New("GET"), getFunction);
132 return handleScope.Close(request);
133 }
OLDNEW

Powered by Google App Engine
This is Rietveld