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

Side by Side Diff: src/WebRequestJsObject.cpp

Issue 10296001: Implement File API (Closed)
Patch Set: Address issues Created April 15, 2013, 1:42 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
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 #include "Utils.h"
5 6
6 namespace 7 namespace
7 { 8 {
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 }
Wladimir Palant 2013/04/16 06:52:44 You might want to revert that change. My refactori
21
22 class WebRequestThread : public AdblockPlus::Thread 9 class WebRequestThread : public AdblockPlus::Thread
23 { 10 {
24 public: 11 public:
25 WebRequestThread(const v8::Arguments& arguments) 12 WebRequestThread(const v8::Arguments& arguments)
26 : isolate(v8::Isolate::GetCurrent()), 13 : isolate(v8::Isolate::GetCurrent()),
27 context(v8::Persistent<v8::Context>::New(isolate, v8::Context::GetCurr ent())), 14 context(v8::Persistent<v8::Context>::New(isolate, v8::Context::GetCurr ent())),
28 thisPtr(v8::Persistent<v8::Object>::New(isolate, arguments.Holder())), 15 thisPtr(v8::Persistent<v8::Object>::New(isolate, arguments.Holder())),
29 url(fromV8String(arguments[0])) 16 url(AdblockPlus::Utils::FromV8String(arguments[0]))
30 { 17 {
31 const v8::Locker locker(isolate); 18 const v8::Locker locker(isolate);
32 const v8::HandleScope handleScope; 19 const v8::HandleScope handleScope;
33 20
34 if (!url.length()) 21 if (!url.length())
35 throw std::runtime_error("Invalid string passed as first argument to GET "); 22 throw std::runtime_error("Invalid string passed as first argument to GET ");
36 23
37 { 24 {
38 const v8::Local<v8::Value> value = arguments[1]; 25 const v8::Local<v8::Value> value = arguments[1];
39 if (!value->IsObject()) 26 if (!value->IsObject())
40 throw std::runtime_error("Second argument to GET must be an object"); 27 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); 28 const v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(value);
42 const v8::Local<v8::Array> properties = object->GetOwnPropertyNames(); 29 const v8::Local<v8::Array> properties = object->GetOwnPropertyNames();
43 for (unsigned i = 0; i < properties->Length(); i++) 30 for (unsigned i = 0; i < properties->Length(); i++)
44 { 31 {
45 const v8::Local<v8::Value> property = properties->Get(i); 32 const v8::Local<v8::Value> property = properties->Get(i);
46 std::string header = fromV8String(property); 33 std::string header = AdblockPlus::Utils::FromV8String(property);
47 std::string headerValue = fromV8String(object->Get(property)); 34 std::string headerValue = AdblockPlus::Utils::FromV8String(object->Get (property));
48 if (header.length() && headerValue.length()) 35 if (header.length() && headerValue.length())
49 headers.push_back(std::pair<std::string, std::string>(header, header Value)); 36 headers.push_back(std::pair<std::string, std::string>(header, header Value));
50 } 37 }
51 } 38 }
52 39
53 const v8::Local<v8::Value> callbackValue = arguments[2]; 40 const v8::Local<v8::Value> callbackValue = arguments[2];
54 if (!callbackValue->IsFunction()) 41 if (!callbackValue->IsFunction())
55 throw std::runtime_error("Third argument to GET must be a function"); 42 throw std::runtime_error("Third argument to GET must be a function");
56 callback = v8::Persistent<v8::Function>::New(isolate, 43 callback = v8::Persistent<v8::Function>::New(isolate,
57 v8::Local<v8::Function>::Cast(callbackValue)); 44 v8::Local<v8::Function>::Cast(callbackValue));
(...skipping 13 matching lines...) Expand all
71 void Run() 58 void Run()
72 { 59 {
73 AdblockPlus::ServerResponse result = webRequest->GET(url, headers); 60 AdblockPlus::ServerResponse result = webRequest->GET(url, headers);
74 61
75 const v8::Locker locker(isolate); 62 const v8::Locker locker(isolate);
76 const v8::HandleScope handleScope; 63 const v8::HandleScope handleScope;
77 const v8::Context::Scope contextScope(context); 64 const v8::Context::Scope contextScope(context);
78 v8::Local<v8::Object> resultObject = v8::Object::New(); 65 v8::Local<v8::Object> resultObject = v8::Object::New();
79 resultObject->Set(v8::String::New("status"), v8::Number::New(result.status )); 66 resultObject->Set(v8::String::New("status"), v8::Number::New(result.status ));
80 resultObject->Set(v8::String::New("responseStatus"), v8::Integer::New(resu lt.responseStatus)); 67 resultObject->Set(v8::String::New("responseStatus"), v8::Integer::New(resu lt.responseStatus));
81 resultObject->Set(v8::String::New("responseText"), toV8String(result.respo nseText)); 68 resultObject->Set(v8::String::New("responseText"), AdblockPlus::Utils::ToV 8String(result.responseText));
82 69
83 v8::Local<v8::Object> headersObject = v8::Object::New(); 70 v8::Local<v8::Object> headersObject = v8::Object::New();
84 for (AdblockPlus::HeaderList::iterator it = result.responseHeaders.begin() ; 71 for (AdblockPlus::HeaderList::iterator it = result.responseHeaders.begin() ;
85 it != result.responseHeaders.end(); ++it) 72 it != result.responseHeaders.end(); ++it)
86 { 73 {
87 headersObject->Set(toV8String(it->first), toV8String(it->second)); 74 headersObject->Set(AdblockPlus::Utils::ToV8String(it->first), AdblockPlu s::Utils::ToV8String(it->second));
88 } 75 }
89 resultObject->Set(v8::String::New("responseHeaders"), headersObject); 76 resultObject->Set(v8::String::New("responseHeaders"), headersObject);
90 77
91 v8::Local<v8::Value> resultValue = resultObject; 78 v8::Local<v8::Value> resultValue = resultObject;
92 callback->Call(thisPtr, 1, &resultValue); 79 callback->Call(thisPtr, 1, &resultValue);
93 delete this; 80 delete this;
94 } 81 }
95 82
96 private: 83 private:
97 v8::Isolate* const isolate; 84 v8::Isolate* const isolate;
(...skipping 27 matching lines...) Expand all
125 AdblockPlus::WebRequest& webRequest) 112 AdblockPlus::WebRequest& webRequest)
126 { 113 {
127 v8::HandleScope handleScope; 114 v8::HandleScope handleScope;
128 const v8::Handle<v8::ObjectTemplate> request = v8::ObjectTemplate::New(); 115 const v8::Handle<v8::ObjectTemplate> request = v8::ObjectTemplate::New();
129 const v8::Handle<v8::FunctionTemplate> getFunction = 116 const v8::Handle<v8::FunctionTemplate> getFunction =
130 v8::FunctionTemplate::New(::GETCallback, 117 v8::FunctionTemplate::New(::GETCallback,
131 v8::External::New(&webRequest)); 118 v8::External::New(&webRequest));
132 request->Set(v8::String::New("GET"), getFunction); 119 request->Set(v8::String::New("GET"), getFunction);
133 return handleScope.Close(request); 120 return handleScope.Close(request);
134 } 121 }
OLDNEW
« src/FileSystemJsObject.cpp ('K') | « src/Utils.cpp ('k') | test/FileSystemJsObject.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld