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

Delta Between Two Patch Sets: src/FileSystemJsObject.cpp

Issue 10296001: Implement File API (Closed)
Left Patch Set: Address issues Created April 15, 2013, 1:42 p.m.
Right Patch Set: Addressed the new issues Created April 16, 2013, 1:37 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « src/FileSystemJsObject.h ('k') | src/GlobalJsObject.h » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 "FileSystemJsObject.h" 6 #include "FileSystemJsObject.h"
7 #include "Utils.h" 7 #include "Utils.h"
8 #include "Thread.h" 8 #include "Thread.h"
9 #include "Utils.h" 9 #include "Utils.h"
10 10
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
45 : IoThread(fileSystem, callback), path(path) 45 : IoThread(fileSystem, callback), path(path)
46 { 46 {
47 } 47 }
48 48
49 void Run() 49 void Run()
50 { 50 {
51 std::string content; 51 std::string content;
52 std::string error; 52 std::string error;
53 try 53 try
54 { 54 {
55 std::auto_ptr<std::istream> stream = fileSystem.Read(path); 55 std::tr1::shared_ptr<std::istream> stream = fileSystem.Read(path);
56 content = Utils::Slurp(*stream); 56 content = Utils::Slurp(*stream);
57 } 57 }
58 catch (std::exception& e) 58 catch (std::exception& e)
59 { 59 {
60 error = e.what(); 60 error = e.what();
61 } 61 }
62 catch (...) 62 catch (...)
63 { 63 {
64 error = "Unknown error while reading from " + path; 64 error = "Unknown error while reading from " + path;
65 } 65 }
(...skipping 20 matching lines...) Expand all
86 const std::string& path, const std::string& content) 86 const std::string& path, const std::string& content)
87 : IoThread(fileSystem, callback), path(path), content(content) 87 : IoThread(fileSystem, callback), path(path), content(content)
88 { 88 {
89 } 89 }
90 90
91 void Run() 91 void Run()
92 { 92 {
93 std::string error; 93 std::string error;
94 try 94 try
95 { 95 {
96 fileSystem.Write(path, content); 96 std::tr1::shared_ptr<std::ostream> stream(new std::stringstream);
97 *stream << content;
98 fileSystem.Write(path, stream);
97 } 99 }
98 catch (std::exception& e) 100 catch (std::exception& e)
99 { 101 {
100 error = e.what(); 102 error = e.what();
101 } 103 }
102 catch (...) 104 catch (...)
103 { 105 {
104 error = "Unknown error while writing to " + path; 106 error = "Unknown error while writing to " + path;
105 } 107 }
106 108
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
235 reinterpret_cast<v8::Handle<v8::Value>*>(&result)); 237 reinterpret_cast<v8::Handle<v8::Value>*>(&result));
236 delete this; 238 delete this;
237 } 239 }
238 240
239 private: 241 private:
240 std::string path; 242 std::string path;
241 }; 243 };
242 244
243 v8::Handle<v8::Value> ReadCallback(const v8::Arguments& arguments) 245 v8::Handle<v8::Value> ReadCallback(const v8::Arguments& arguments)
244 { 246 {
245 std::vector<std::string> requiredTypes; 247 const v8::Handle<const v8::External> external =
246 requiredTypes.push_back("string"); 248 v8::Handle<const v8::External>::Cast(arguments.Data());
247 requiredTypes.push_back("function"); 249 FileSystem* const fileSystem = static_cast<FileSystem*>(external->Value());
Wladimir Palant 2013/04/16 06:50:13 requiredTypes is no longer used - and arguments.Le
248 v8::TryCatch tryCatch; 250 if (arguments.Length() != 2)
Wladimir Palant 2013/04/16 06:50:13 If you are using TryCatch then you might want to b
249 const v8::Handle<const v8::External> external = 251 return v8::ThrowException(v8::String::New(
250 v8::Handle<const v8::External>::Cast(arguments.Data()); 252 "_fileSystem.read requires 2 parameters"));
251 FileSystem* const fileSystem = static_cast<FileSystem*>(external->Value());
252 const std::string path = Utils::FromV8String(arguments[0]); 253 const std::string path = Utils::FromV8String(arguments[0]);
253 v8::Handle<v8::Value> callbackValue = arguments[1]; 254 v8::Handle<v8::Value> callbackValue = arguments[1];
254 if (!callbackValue->IsFunction()) 255 if (!callbackValue->IsFunction())
255 return v8::ThrowException(v8::String::New( 256 return v8::ThrowException(v8::String::New(
256 "Second argument to _fileSystem.read must be a function")); 257 "Second argument to _fileSystem.read must be a function"));
257 v8::Handle<v8::Function> callback = 258 v8::Handle<v8::Function> callback =
258 v8::Handle<v8::Function>::Cast(callbackValue); 259 v8::Handle<v8::Function>::Cast(callbackValue);
259 ReadThread* const readThread = new ReadThread(*fileSystem, callback, path); 260 ReadThread* const readThread = new ReadThread(*fileSystem, callback, path);
260 readThread->Start(); 261 readThread->Start();
261 if (tryCatch.HasCaught())
262 return tryCatch.Exception();
263 return v8::Undefined(); 262 return v8::Undefined();
264 } 263 }
265 264
266 v8::Handle<v8::Value> WriteCallback(const v8::Arguments& arguments) 265 v8::Handle<v8::Value> WriteCallback(const v8::Arguments& arguments)
267 { 266 {
268 const v8::Handle<const v8::External> external = 267 const v8::Handle<const v8::External> external =
269 v8::Handle<const v8::External>::Cast(arguments.Data()); 268 v8::Handle<const v8::External>::Cast(arguments.Data());
270 FileSystem* const fileSystem = static_cast<FileSystem*>(external->Value()); 269 FileSystem* const fileSystem = static_cast<FileSystem*>(external->Value());
270 if (arguments.Length() != 3)
271 return v8::ThrowException(v8::String::New(
272 "_fileSystem.write requires 3 parameters"));
271 const std::string path = Utils::FromV8String(arguments[0]); 273 const std::string path = Utils::FromV8String(arguments[0]);
272 const std::string content = Utils::FromV8String(arguments[1]); 274 const std::string content = Utils::FromV8String(arguments[1]);
273 v8::Handle<v8::Value> callbackValue = arguments[2]; 275 v8::Handle<v8::Value> callbackValue = arguments[2];
274 if (!callbackValue->IsFunction()) 276 if (!callbackValue->IsFunction())
275 return v8::ThrowException(v8::String::New( 277 return v8::ThrowException(v8::String::New(
276 "Third argument to _fileSystem.write must be a function")); 278 "Third argument to _fileSystem.write must be a function"));
277 v8::Handle<v8::Function> callback = 279 v8::Handle<v8::Function> callback =
278 v8::Handle<v8::Function>::Cast(callbackValue); 280 v8::Handle<v8::Function>::Cast(callbackValue);
279 WriteThread* const writeThread = new WriteThread(*fileSystem, callback, 281 WriteThread* const writeThread = new WriteThread(*fileSystem, callback,
280 path, content); 282 path, content);
281 writeThread->Start(); 283 writeThread->Start();
282 return v8::Undefined(); 284 return v8::Undefined();
283 } 285 }
284 286
285 v8::Handle<v8::Value> MoveCallback(const v8::Arguments& arguments) 287 v8::Handle<v8::Value> MoveCallback(const v8::Arguments& arguments)
286 { 288 {
287 const v8::Handle<const v8::External> external = 289 const v8::Handle<const v8::External> external =
288 v8::Handle<const v8::External>::Cast(arguments.Data()); 290 v8::Handle<const v8::External>::Cast(arguments.Data());
289 FileSystem* const fileSystem = static_cast<FileSystem*>(external->Value()); 291 FileSystem* const fileSystem = static_cast<FileSystem*>(external->Value());
292 if (arguments.Length() != 3)
293 return v8::ThrowException(v8::String::New(
294 "_fileSystem.move requires 3 parameters"));
290 const std::string fromPath = Utils::FromV8String(arguments[0]); 295 const std::string fromPath = Utils::FromV8String(arguments[0]);
291 const std::string toPath = Utils::FromV8String(arguments[1]); 296 const std::string toPath = Utils::FromV8String(arguments[1]);
292 v8::Handle<v8::Value> callbackValue = arguments[2]; 297 v8::Handle<v8::Value> callbackValue = arguments[2];
293 if (!callbackValue->IsFunction()) 298 if (!callbackValue->IsFunction())
294 return v8::ThrowException(v8::String::New( 299 return v8::ThrowException(v8::String::New(
295 "Third argument to _fileSystem.move must be a function")); 300 "Third argument to _fileSystem.move must be a function"));
296 v8::Handle<v8::Function> callback = 301 v8::Handle<v8::Function> callback =
297 v8::Handle<v8::Function>::Cast(callbackValue); 302 v8::Handle<v8::Function>::Cast(callbackValue);
298 MoveThread* const moveThread = new MoveThread(*fileSystem, callback, 303 MoveThread* const moveThread = new MoveThread(*fileSystem, callback,
299 fromPath, toPath); 304 fromPath, toPath);
300 moveThread->Start(); 305 moveThread->Start();
301 return v8::Undefined(); 306 return v8::Undefined();
302 } 307 }
303 308
304 v8::Handle<v8::Value> RemoveCallback(const v8::Arguments& arguments) 309 v8::Handle<v8::Value> RemoveCallback(const v8::Arguments& arguments)
305 { 310 {
306 const v8::Handle<const v8::External> external = 311 const v8::Handle<const v8::External> external =
307 v8::Handle<const v8::External>::Cast(arguments.Data()); 312 v8::Handle<const v8::External>::Cast(arguments.Data());
308 FileSystem* const fileSystem = static_cast<FileSystem*>(external->Value()); 313 FileSystem* const fileSystem = static_cast<FileSystem*>(external->Value());
314 if (arguments.Length() != 2)
315 return v8::ThrowException(v8::String::New(
316 "_fileSystem.remove requires 2 parameters"));
309 const std::string path = Utils::FromV8String(arguments[0]); 317 const std::string path = Utils::FromV8String(arguments[0]);
310 v8::Handle<v8::Value> callbackValue = arguments[1]; 318 v8::Handle<v8::Value> callbackValue = arguments[1];
311 if (!callbackValue->IsFunction()) 319 if (!callbackValue->IsFunction())
312 return v8::ThrowException(v8::String::New( 320 return v8::ThrowException(v8::String::New(
313 "Second argument to _fileSystem.remove must be a function")); 321 "Second argument to _fileSystem.remove must be a function"));
314 v8::Handle<v8::Function> callback = 322 v8::Handle<v8::Function> callback =
315 v8::Handle<v8::Function>::Cast(callbackValue); 323 v8::Handle<v8::Function>::Cast(callbackValue);
316 RemoveThread* const removeThread = new RemoveThread(*fileSystem, callback, 324 RemoveThread* const removeThread = new RemoveThread(*fileSystem, callback,
317 path); 325 path);
318 removeThread->Start(); 326 removeThread->Start();
319 return v8::Undefined(); 327 return v8::Undefined();
320 } 328 }
321 329
322 v8::Handle<v8::Value> StatCallback(const v8::Arguments& arguments) 330 v8::Handle<v8::Value> StatCallback(const v8::Arguments& arguments)
323 { 331 {
324 const v8::Handle<const v8::External> external = 332 const v8::Handle<const v8::External> external =
325 v8::Handle<const v8::External>::Cast(arguments.Data()); 333 v8::Handle<const v8::External>::Cast(arguments.Data());
326 FileSystem* const fileSystem = static_cast<FileSystem*>(external->Value()); 334 FileSystem* const fileSystem = static_cast<FileSystem*>(external->Value());
335 if (arguments.Length() != 2)
336 return v8::ThrowException(v8::String::New(
337 "_fileSystem.stat requires 2 parameters"));
327 const std::string path = Utils::FromV8String(arguments[0]); 338 const std::string path = Utils::FromV8String(arguments[0]);
328 v8::Handle<v8::Value> callbackValue = arguments[1]; 339 v8::Handle<v8::Value> callbackValue = arguments[1];
329 if (!callbackValue->IsFunction()) 340 if (!callbackValue->IsFunction())
330 return v8::ThrowException(v8::String::New( 341 return v8::ThrowException(v8::String::New(
331 "Second argument to _fileSystem.stat must be a function")); 342 "Second argument to _fileSystem.stat must be a function"));
332 v8::Handle<v8::Function> callback = 343 v8::Handle<v8::Function> callback =
333 v8::Handle<v8::Function>::Cast(callbackValue); 344 v8::Handle<v8::Function>::Cast(callbackValue);
334 StatThread* const statThread = new StatThread(*fileSystem, callback, path); 345 StatThread* const statThread = new StatThread(*fileSystem, callback, path);
335 statThread->Start(); 346 statThread->Start();
336 return v8::Undefined(); 347 return v8::Undefined();
(...skipping 13 matching lines...) Expand all
350 file->Set(v8::String::New("write"), 361 file->Set(v8::String::New("write"),
351 v8::FunctionTemplate::New(WriteCallback, fileSystemExternal)); 362 v8::FunctionTemplate::New(WriteCallback, fileSystemExternal));
352 file->Set(v8::String::New("move"), 363 file->Set(v8::String::New("move"),
353 v8::FunctionTemplate::New(MoveCallback, fileSystemExternal)); 364 v8::FunctionTemplate::New(MoveCallback, fileSystemExternal));
354 file->Set(v8::String::New("remove"), 365 file->Set(v8::String::New("remove"),
355 v8::FunctionTemplate::New(RemoveCallback, fileSystemExternal)); 366 v8::FunctionTemplate::New(RemoveCallback, fileSystemExternal));
356 file->Set(v8::String::New("stat"), 367 file->Set(v8::String::New("stat"),
357 v8::FunctionTemplate::New(StatCallback, fileSystemExternal)); 368 v8::FunctionTemplate::New(StatCallback, fileSystemExternal));
358 return handleScope.Close(file); 369 return handleScope.Close(file);
359 } 370 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld