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

Side by Side Diff: src/FileSystemJsObject.cpp

Issue 10184021: Some refactoring of global JavaScript objects (Closed)
Patch Set: Unbitrotted patch Created April 16, 2013, 3:32 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
« no previous file with comments | « src/FileSystemJsObject.h ('k') | src/GlobalJsObject.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #include <AdblockPlus/FileSystem.h> 1 #include <AdblockPlus/FileSystem.h>
2 #include <stdexcept> 2 #include <stdexcept>
3 #include <sstream> 3 #include <sstream>
4 #include <vector> 4 #include <vector>
5 5
6 #include <AdblockPlus/JsEngine.h>
7 #include <AdblockPlus/JsValue.h>
6 #include "FileSystemJsObject.h" 8 #include "FileSystemJsObject.h"
7 #include "Utils.h" 9 #include "Utils.h"
8 #include "Thread.h" 10 #include "Thread.h"
9 #include "Utils.h" 11 #include "Utils.h"
10 12
11 using namespace AdblockPlus; 13 using namespace AdblockPlus;
12 14
13 namespace 15 namespace
14 { 16 {
15 class IoThread : public Thread 17 class IoThread : public Thread
16 { 18 {
17 public: 19 public:
18 IoThread(FileSystem& fileSystem, v8::Handle<v8::Function> callback) 20 IoThread(JsEngine& jsEngine, JsValuePtr callback)
19 : isolate(v8::Isolate::GetCurrent()), 21 : jsEngine(jsEngine), fileSystem(jsEngine.GetFileSystem()),
20 context(v8::Persistent<v8::Context>::New(isolate, 22 callback(callback)
21 v8::Context::GetCurrent())),
22 fileSystem(fileSystem),
23 callback(v8::Persistent<v8::Function>::New(isolate, callback))
24 { 23 {
25 } 24 }
26 25
27 virtual ~IoThread()
28 {
29 callback.Dispose(isolate);
30 context.Dispose(isolate);
31 }
32
33 protected: 26 protected:
34 v8::Isolate* const isolate; 27 JsEngine& jsEngine;
35 v8::Persistent<v8::Context> context;
36 FileSystem& fileSystem; 28 FileSystem& fileSystem;
37 v8::Persistent<v8::Function> callback; 29 JsValuePtr callback;
38 }; 30 };
39 31
40 class ReadThread : public IoThread 32 class ReadThread : public IoThread
41 { 33 {
42 public: 34 public:
43 ReadThread(FileSystem& fileSystem, v8::Handle<v8::Function> callback, 35 ReadThread(JsEngine& jsEngine, JsValuePtr callback,
44 const std::string& path) 36 const std::string& path)
45 : IoThread(fileSystem, callback), path(path) 37 : IoThread(jsEngine, callback), path(path)
46 { 38 {
47 } 39 }
48 40
49 void Run() 41 void Run()
50 { 42 {
51 std::string content; 43 std::string content;
52 std::string error; 44 std::string error;
53 try 45 try
54 { 46 {
55 std::tr1::shared_ptr<std::istream> stream = fileSystem.Read(path); 47 std::tr1::shared_ptr<std::istream> stream = fileSystem.Read(path);
56 content = Utils::Slurp(*stream); 48 content = Utils::Slurp(*stream);
57 } 49 }
58 catch (std::exception& e) 50 catch (std::exception& e)
59 { 51 {
60 error = e.what(); 52 error = e.what();
61 } 53 }
62 catch (...) 54 catch (...)
63 { 55 {
64 error = "Unknown error while reading from " + path; 56 error = "Unknown error while reading from " + path;
65 } 57 }
66 58
67 const v8::Locker locker(isolate); 59 const JsEngine::Context context(jsEngine);
68 const v8::HandleScope handleScope; 60 JsValuePtr result = jsEngine.NewObject();
69 const v8::Context::Scope contextScope(context); 61 result->SetProperty("content", content);
70 v8::Handle<v8::Object> result = v8::Object::New(); 62 result->SetProperty("error", error);
71 result->Set(v8::String::New("content"), Utils::ToV8String(content)); 63 JsValueList params;
72 result->Set(v8::String::New("error"), Utils::ToV8String(error)); 64 params.push_back(result);
73 callback->Call(callback, 1, 65 callback->Call(params);
74 reinterpret_cast<v8::Handle<v8::Value>*>(&result));
75 delete this; 66 delete this;
76 } 67 }
77 68
78 private: 69 private:
79 std::string path; 70 std::string path;
80 }; 71 };
81 72
82 class WriteThread : public IoThread 73 class WriteThread : public IoThread
83 { 74 {
84 public: 75 public:
85 WriteThread(FileSystem& fileSystem, v8::Handle<v8::Function> callback, 76 WriteThread(JsEngine& jsEngine, JsValuePtr callback,
86 const std::string& path, const std::string& content) 77 const std::string& path, const std::string& content)
87 : IoThread(fileSystem, callback), path(path), content(content) 78 : IoThread(jsEngine, callback), path(path), content(content)
88 { 79 {
89 } 80 }
90 81
91 void Run() 82 void Run()
92 { 83 {
93 std::string error; 84 std::string error;
94 try 85 try
95 { 86 {
96 std::tr1::shared_ptr<std::ostream> stream(new std::stringstream); 87 std::tr1::shared_ptr<std::ostream> stream(new std::stringstream);
97 *stream << content; 88 *stream << content;
98 fileSystem.Write(path, stream); 89 fileSystem.Write(path, stream);
99 } 90 }
100 catch (std::exception& e) 91 catch (std::exception& e)
101 { 92 {
102 error = e.what(); 93 error = e.what();
103 } 94 }
104 catch (...) 95 catch (...)
105 { 96 {
106 error = "Unknown error while writing to " + path; 97 error = "Unknown error while writing to " + path;
107 } 98 }
108 99
109 const v8::Locker locker(isolate); 100 const JsEngine::Context context(jsEngine);
110 const v8::HandleScope handleScope; 101 JsValuePtr errorValue = jsEngine.NewValue(error);
111 const v8::Context::Scope contextScope(context); 102 JsValueList params;
112 v8::Handle<v8::Value> errorValue = Utils::ToV8String(error); 103 params.push_back(errorValue);
113 callback->Call(callback, 1, &errorValue); 104 callback->Call(params);
114 delete this; 105 delete this;
115 } 106 }
116 107
117 private: 108 private:
118 std::string path; 109 std::string path;
119 std::string content; 110 std::string content;
120 }; 111 };
121 112
122 class MoveThread : public IoThread 113 class MoveThread : public IoThread
123 { 114 {
124 public: 115 public:
125 MoveThread(FileSystem& fileSystem, v8::Handle<v8::Function> callback, 116 MoveThread(JsEngine& jsEngine, JsValuePtr callback,
126 const std::string& fromPath, const std::string& toPath) 117 const std::string& fromPath, const std::string& toPath)
127 : IoThread(fileSystem, callback), fromPath(fromPath), toPath(toPath) 118 : IoThread(jsEngine, callback), fromPath(fromPath), toPath(toPath)
128 { 119 {
129 } 120 }
130 121
131 void Run() 122 void Run()
132 { 123 {
133 std::string error; 124 std::string error;
134 try 125 try
135 { 126 {
136 fileSystem.Move(fromPath, toPath); 127 fileSystem.Move(fromPath, toPath);
137 } 128 }
138 catch (std::exception& e) 129 catch (std::exception& e)
139 { 130 {
140 error = e.what(); 131 error = e.what();
141 } 132 }
142 catch (...) 133 catch (...)
143 { 134 {
144 error = "Unknown error while moving " + fromPath + " to " + toPath; 135 error = "Unknown error while moving " + fromPath + " to " + toPath;
145 } 136 }
146 137
147 const v8::Locker locker(isolate); 138 const JsEngine::Context context(jsEngine);
148 const v8::HandleScope handleScope; 139 JsValuePtr errorValue = jsEngine.NewValue(error);
149 const v8::Context::Scope contextScope(context); 140 JsValueList params;
150 v8::Handle<v8::Value> errorValue = Utils::ToV8String(error); 141 params.push_back(errorValue);
151 callback->Call(callback, 1, &errorValue); 142 callback->Call(params);
152 delete this; 143 delete this;
153 } 144 }
154 145
155 private: 146 private:
156 std::string fromPath; 147 std::string fromPath;
157 std::string toPath; 148 std::string toPath;
158 }; 149 };
159 150
160 class RemoveThread : public IoThread 151 class RemoveThread : public IoThread
161 { 152 {
162 public: 153 public:
163 RemoveThread(FileSystem& fileSystem, v8::Handle<v8::Function> callback, 154 RemoveThread(JsEngine& jsEngine, JsValuePtr callback,
164 const std::string& path) 155 const std::string& path)
165 : IoThread(fileSystem, callback), path(path) 156 : IoThread(jsEngine, callback), path(path)
166 { 157 {
167 } 158 }
168 159
169 void Run() 160 void Run()
170 { 161 {
171 std::string error; 162 std::string error;
172 try 163 try
173 { 164 {
174 fileSystem.Remove(path); 165 fileSystem.Remove(path);
175 } 166 }
176 catch (std::exception& e) 167 catch (std::exception& e)
177 { 168 {
178 error = e.what(); 169 error = e.what();
179 } 170 }
180 catch (...) 171 catch (...)
181 { 172 {
182 error = "Unknown error while removing " + path; 173 error = "Unknown error while removing " + path;
183 } 174 }
184 175
185 const v8::Locker locker(isolate); 176 const JsEngine::Context context(jsEngine);
186 const v8::HandleScope handleScope; 177 JsValuePtr errorValue = jsEngine.NewValue(error);
187 const v8::Context::Scope contextScope(context); 178 JsValueList params;
188 v8::Handle<v8::Value> errorValue = Utils::ToV8String(error); 179 params.push_back(errorValue);
189 callback->Call(callback, 1, &errorValue); 180 callback->Call(params);
190 delete this; 181 delete this;
191 } 182 }
192 183
193 private: 184 private:
194 std::string path; 185 std::string path;
195 }; 186 };
196 187
197 class StatThread : public IoThread 188 class StatThread : public IoThread
198 { 189 {
199 public: 190 public:
200 StatThread(FileSystem& fileSystem, v8::Handle<v8::Function> callback, 191 StatThread(JsEngine& jsEngine, JsValuePtr callback,
201 const std::string& path) 192 const std::string& path)
202 : IoThread(fileSystem, callback), path(path) 193 : IoThread(jsEngine, callback), path(path)
203 { 194 {
204 } 195 }
205 196
206 void Run() 197 void Run()
207 { 198 {
208 std::string error; 199 std::string error;
209 FileSystem::StatResult statResult; 200 FileSystem::StatResult statResult;
210 try 201 try
211 { 202 {
212 statResult = fileSystem.Stat(path); 203 statResult = fileSystem.Stat(path);
213 } 204 }
214 catch (std::exception& e) 205 catch (std::exception& e)
215 { 206 {
216 error = e.what(); 207 error = e.what();
217 } 208 }
218 catch (...) 209 catch (...)
219 { 210 {
220 error = "Unknown error while calling stat on " + path; 211 error = "Unknown error while calling stat on " + path;
221 } 212 }
222 213
223 const v8::Locker locker(isolate); 214 const JsEngine::Context context(jsEngine);
224 const v8::HandleScope handleScope; 215 JsValuePtr result = jsEngine.NewObject();
225 const v8::Context::Scope contextScope(context); 216 result->SetProperty("exists", statResult.exists);
226 v8::Handle<v8::Object> result = v8::Object::New(); 217 result->SetProperty("isFile", statResult.isFile);
227 result->Set(v8::String::New("exists"), 218 result->SetProperty("isDirectory", statResult.isDirectory);
228 v8::Boolean::New(statResult.exists)); 219 result->SetProperty("lastModified", statResult.lastModified);
229 result->Set(v8::String::New("isFile"), 220 result->SetProperty("error", error);
230 v8::Boolean::New(statResult.isFile)); 221
231 result->Set(v8::String::New("isDirectory"), 222 JsValueList params;
232 v8::Boolean::New(statResult.isDirectory)); 223 params.push_back(result);
233 result->Set(v8::String::New("lastModified"), 224 callback->Call(params);
234 v8::Integer::New(statResult.lastModified));
235 result->Set(v8::String::New("error"), Utils::ToV8String(error));
236 callback->Call(callback, 1,
237 reinterpret_cast<v8::Handle<v8::Value>*>(&result));
238 delete this; 225 delete this;
239 } 226 }
240 227
241 private: 228 private:
242 std::string path; 229 std::string path;
243 }; 230 };
244 231
245 v8::Handle<v8::Value> ReadCallback(const v8::Arguments& arguments) 232 v8::Handle<v8::Value> ReadCallback(const v8::Arguments& arguments)
246 { 233 {
247 const v8::Handle<const v8::External> external = 234 AdblockPlus::JsEngine& jsEngine = AdblockPlus::JsEngine::FromArguments(argum ents);
248 v8::Handle<const v8::External>::Cast(arguments.Data()); 235 AdblockPlus::JsValueList converted = jsEngine.ConvertArguments(arguments);
249 FileSystem* const fileSystem = static_cast<FileSystem*>(external->Value()); 236
250 if (arguments.Length() != 2) 237 if (converted.size() != 2)
251 return v8::ThrowException(v8::String::New( 238 return v8::ThrowException(v8::String::New(
252 "_fileSystem.read requires 2 parameters")); 239 "_fileSystem.read requires 2 parameters"));
253 const std::string path = Utils::FromV8String(arguments[0]); 240 if (!converted[1]->IsFunction())
254 v8::Handle<v8::Value> callbackValue = arguments[1];
255 if (!callbackValue->IsFunction())
256 return v8::ThrowException(v8::String::New( 241 return v8::ThrowException(v8::String::New(
257 "Second argument to _fileSystem.read must be a function")); 242 "Second argument to _fileSystem.read must be a function"));
258 v8::Handle<v8::Function> callback = 243 ReadThread* const readThread = new ReadThread(jsEngine, converted[1],
259 v8::Handle<v8::Function>::Cast(callbackValue); 244 converted[0]->AsString());
260 ReadThread* const readThread = new ReadThread(*fileSystem, callback, path);
261 readThread->Start(); 245 readThread->Start();
262 return v8::Undefined(); 246 return v8::Undefined();
263 } 247 }
264 248
265 v8::Handle<v8::Value> WriteCallback(const v8::Arguments& arguments) 249 v8::Handle<v8::Value> WriteCallback(const v8::Arguments& arguments)
266 { 250 {
267 const v8::Handle<const v8::External> external = 251 AdblockPlus::JsEngine& jsEngine = AdblockPlus::JsEngine::FromArguments(argum ents);
268 v8::Handle<const v8::External>::Cast(arguments.Data()); 252 AdblockPlus::JsValueList converted = jsEngine.ConvertArguments(arguments);
269 FileSystem* const fileSystem = static_cast<FileSystem*>(external->Value()); 253
270 if (arguments.Length() != 3) 254 if (converted.size() != 3)
271 return v8::ThrowException(v8::String::New( 255 return v8::ThrowException(v8::String::New(
272 "_fileSystem.write requires 3 parameters")); 256 "_fileSystem.write requires 3 parameters"));
273 const std::string path = Utils::FromV8String(arguments[0]); 257 if (!converted[2]->IsFunction())
274 const std::string content = Utils::FromV8String(arguments[1]);
275 v8::Handle<v8::Value> callbackValue = arguments[2];
276 if (!callbackValue->IsFunction())
277 return v8::ThrowException(v8::String::New( 258 return v8::ThrowException(v8::String::New(
278 "Third argument to _fileSystem.write must be a function")); 259 "Third argument to _fileSystem.write must be a function"));
279 v8::Handle<v8::Function> callback = 260 WriteThread* const writeThread = new WriteThread(jsEngine, converted[2],
280 v8::Handle<v8::Function>::Cast(callbackValue); 261 converted[0]->AsString(), converted[1]->AsString());
281 WriteThread* const writeThread = new WriteThread(*fileSystem, callback,
282 path, content);
283 writeThread->Start(); 262 writeThread->Start();
284 return v8::Undefined(); 263 return v8::Undefined();
285 } 264 }
286 265
287 v8::Handle<v8::Value> MoveCallback(const v8::Arguments& arguments) 266 v8::Handle<v8::Value> MoveCallback(const v8::Arguments& arguments)
288 { 267 {
289 const v8::Handle<const v8::External> external = 268 AdblockPlus::JsEngine& jsEngine = AdblockPlus::JsEngine::FromArguments(argum ents);
290 v8::Handle<const v8::External>::Cast(arguments.Data()); 269 AdblockPlus::JsValueList converted = jsEngine.ConvertArguments(arguments);
291 FileSystem* const fileSystem = static_cast<FileSystem*>(external->Value()); 270
292 if (arguments.Length() != 3) 271 if (converted.size() != 3)
293 return v8::ThrowException(v8::String::New( 272 return v8::ThrowException(v8::String::New(
294 "_fileSystem.move requires 3 parameters")); 273 "_fileSystem.move requires 3 parameters"));
295 const std::string fromPath = Utils::FromV8String(arguments[0]); 274 if (!converted[2]->IsFunction())
296 const std::string toPath = Utils::FromV8String(arguments[1]);
297 v8::Handle<v8::Value> callbackValue = arguments[2];
298 if (!callbackValue->IsFunction())
299 return v8::ThrowException(v8::String::New( 275 return v8::ThrowException(v8::String::New(
300 "Third argument to _fileSystem.move must be a function")); 276 "Third argument to _fileSystem.move must be a function"));
301 v8::Handle<v8::Function> callback = 277 MoveThread* const moveThread = new MoveThread(jsEngine, converted[2],
302 v8::Handle<v8::Function>::Cast(callbackValue); 278 converted[0]->AsString(), converted[1]->AsString());
303 MoveThread* const moveThread = new MoveThread(*fileSystem, callback,
304 fromPath, toPath);
305 moveThread->Start(); 279 moveThread->Start();
306 return v8::Undefined(); 280 return v8::Undefined();
307 } 281 }
308 282
309 v8::Handle<v8::Value> RemoveCallback(const v8::Arguments& arguments) 283 v8::Handle<v8::Value> RemoveCallback(const v8::Arguments& arguments)
310 { 284 {
311 const v8::Handle<const v8::External> external = 285 AdblockPlus::JsEngine& jsEngine = AdblockPlus::JsEngine::FromArguments(argum ents);
312 v8::Handle<const v8::External>::Cast(arguments.Data()); 286 AdblockPlus::JsValueList converted = jsEngine.ConvertArguments(arguments);
313 FileSystem* const fileSystem = static_cast<FileSystem*>(external->Value()); 287
314 if (arguments.Length() != 2) 288 if (converted.size() != 2)
315 return v8::ThrowException(v8::String::New( 289 return v8::ThrowException(v8::String::New(
316 "_fileSystem.remove requires 2 parameters")); 290 "_fileSystem.remove requires 2 parameters"));
317 const std::string path = Utils::FromV8String(arguments[0]); 291 if (!converted[1]->IsFunction())
318 v8::Handle<v8::Value> callbackValue = arguments[1];
319 if (!callbackValue->IsFunction())
320 return v8::ThrowException(v8::String::New( 292 return v8::ThrowException(v8::String::New(
321 "Second argument to _fileSystem.remove must be a function")); 293 "Second argument to _fileSystem.remove must be a function"));
322 v8::Handle<v8::Function> callback = 294 RemoveThread* const removeThread = new RemoveThread(jsEngine, converted[1],
323 v8::Handle<v8::Function>::Cast(callbackValue); 295 converted[0]->AsString());
324 RemoveThread* const removeThread = new RemoveThread(*fileSystem, callback,
325 path);
326 removeThread->Start(); 296 removeThread->Start();
327 return v8::Undefined(); 297 return v8::Undefined();
328 } 298 }
329 299
330 v8::Handle<v8::Value> StatCallback(const v8::Arguments& arguments) 300 v8::Handle<v8::Value> StatCallback(const v8::Arguments& arguments)
331 { 301 {
332 const v8::Handle<const v8::External> external = 302 AdblockPlus::JsEngine& jsEngine = AdblockPlus::JsEngine::FromArguments(argum ents);
333 v8::Handle<const v8::External>::Cast(arguments.Data()); 303 AdblockPlus::JsValueList converted = jsEngine.ConvertArguments(arguments);
334 FileSystem* const fileSystem = static_cast<FileSystem*>(external->Value()); 304
335 if (arguments.Length() != 2) 305 if (converted.size() != 2)
336 return v8::ThrowException(v8::String::New( 306 return v8::ThrowException(v8::String::New(
337 "_fileSystem.stat requires 2 parameters")); 307 "_fileSystem.stat requires 2 parameters"));
338 const std::string path = Utils::FromV8String(arguments[0]); 308 if (!converted[1]->IsFunction())
339 v8::Handle<v8::Value> callbackValue = arguments[1];
340 if (!callbackValue->IsFunction())
341 return v8::ThrowException(v8::String::New( 309 return v8::ThrowException(v8::String::New(
342 "Second argument to _fileSystem.stat must be a function")); 310 "Second argument to _fileSystem.stat must be a function"));
343 v8::Handle<v8::Function> callback = 311 StatThread* const statThread = new StatThread(jsEngine, converted[1],
344 v8::Handle<v8::Function>::Cast(callbackValue); 312 converted[0]->AsString());
345 StatThread* const statThread = new StatThread(*fileSystem, callback, path);
346 statThread->Start(); 313 statThread->Start();
347 return v8::Undefined(); 314 return v8::Undefined();
348 } 315 }
349 } 316 }
350 317
351 v8::Handle<v8::ObjectTemplate> 318 v8::Handle<v8::ObjectTemplate>
352 FileSystemJsObject::Create(FileSystem& fileSystem) 319 FileSystemJsObject::Create(JsEngine& jsEngine)
353 { 320 {
354 const v8::Locker locker(v8::Isolate::GetCurrent());
355 v8::HandleScope handleScope; 321 v8::HandleScope handleScope;
356 const v8::Handle<v8::ObjectTemplate> file = v8::ObjectTemplate::New(); 322 const v8::Local<v8::ObjectTemplate> file = v8::ObjectTemplate::New();
357 const v8::Local<v8::External> fileSystemExternal = 323 const v8::Local<v8::External> jsEngineExternal =
358 v8::External::New(&fileSystem); 324 v8::External::New(&jsEngine);
359 file->Set(v8::String::New("read"), 325 file->Set(v8::String::New("read"),
360 v8::FunctionTemplate::New(ReadCallback, fileSystemExternal)); 326 v8::FunctionTemplate::New(ReadCallback, jsEngineExternal));
361 file->Set(v8::String::New("write"), 327 file->Set(v8::String::New("write"),
362 v8::FunctionTemplate::New(WriteCallback, fileSystemExternal)); 328 v8::FunctionTemplate::New(WriteCallback, jsEngineExternal));
363 file->Set(v8::String::New("move"), 329 file->Set(v8::String::New("move"),
364 v8::FunctionTemplate::New(MoveCallback, fileSystemExternal)); 330 v8::FunctionTemplate::New(MoveCallback, jsEngineExternal));
365 file->Set(v8::String::New("remove"), 331 file->Set(v8::String::New("remove"),
366 v8::FunctionTemplate::New(RemoveCallback, fileSystemExternal)); 332 v8::FunctionTemplate::New(RemoveCallback, jsEngineExternal));
367 file->Set(v8::String::New("stat"), 333 file->Set(v8::String::New("stat"),
368 v8::FunctionTemplate::New(StatCallback, fileSystemExternal)); 334 v8::FunctionTemplate::New(StatCallback, jsEngineExternal));
369 return handleScope.Close(file); 335 return handleScope.Close(file);
370 } 336 }
OLDNEW
« no previous file with comments | « src/FileSystemJsObject.h ('k') | src/GlobalJsObject.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld