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

Delta Between Two Patch Sets: src/FileSystemJsObject.cpp

Issue 29449592: Issue 5183 - Provide async interface for FileSystem (Closed) Base URL: https://hg.adblockplus.org/libadblockplus/
Left Patch Set: Rebased. Corrected most of the review issues. Created July 4, 2017, 7:57 p.m.
Right Patch Set: Rebase on master. Last changes. Created July 7, 2017, 1:36 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/FilterEngine.cpp » ('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 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2017 eyeo GmbH 3 * Copyright (C) 2006-2017 eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 29 matching lines...) Expand all
40 return ThrowExceptionInJS(isolate, "_fileSystem.read requires 2 parameters "); 40 return ThrowExceptionInJS(isolate, "_fileSystem.read requires 2 parameters ");
41 if (!converted[1].IsFunction()) 41 if (!converted[1].IsFunction())
42 return ThrowExceptionInJS(isolate, "Second argument to _fileSystem.read mu st be a function"); 42 return ThrowExceptionInJS(isolate, "Second argument to _fileSystem.read mu st be a function");
43 43
44 JsValueList values; 44 JsValueList values;
45 values.push_back(converted[1]); 45 values.push_back(converted[1]);
46 auto weakCallback = jsEngine->StoreJsValues(values); 46 auto weakCallback = jsEngine->StoreJsValues(values);
47 std::weak_ptr<JsEngine> weakJsEngine = jsEngine; 47 std::weak_ptr<JsEngine> weakJsEngine = jsEngine;
48 jsEngine->GetAsyncFileSystem()->Read(converted[0].AsString(), 48 jsEngine->GetAsyncFileSystem()->Read(converted[0].AsString(),
49 [weakJsEngine, weakCallback] 49 [weakJsEngine, weakCallback]
50 (std::string&& content, const std::string& error) 50 (IFileSystem::IOBuffer&& content, const std::string& error)
51 { 51 {
52 auto jsEngine = weakJsEngine.lock(); 52 auto jsEngine = weakJsEngine.lock();
53 if (!jsEngine) 53 if (!jsEngine)
54 return; 54 return;
55 55
56 const JsContext context(*jsEngine); 56 const JsContext context(*jsEngine);
57 auto result = jsEngine->NewObject(); 57 auto result = jsEngine->NewObject();
58 result.SetProperty("content", std::move(content)); 58 result.SetStringBufferProperty("content", std::move(content));
59 if (!error.empty()) 59 if (!error.empty())
60 result.SetProperty("error", error); 60 result.SetProperty("error", error);
61 jsEngine->TakeJsValues(weakCallback)[0].Call(result); 61 jsEngine->TakeJsValues(weakCallback)[0].Call(result);
62 }); 62 });
63 } 63 }
64 64
65 void WriteCallback(const v8::FunctionCallbackInfo<v8::Value>& arguments) 65 void WriteCallback(const v8::FunctionCallbackInfo<v8::Value>& arguments)
66 { 66 {
67 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments); 67 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments);
68 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments); 68 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments);
69 69
70 v8::Isolate* isolate = arguments.GetIsolate(); 70 v8::Isolate* isolate = arguments.GetIsolate();
71 if (converted.size() != 3) 71 if (converted.size() != 3)
72 return ThrowExceptionInJS(isolate, "_fileSystem.write requires 3 parameter s"); 72 return ThrowExceptionInJS(isolate, "_fileSystem.write requires 3 parameter s");
73 if (!converted[2].IsFunction()) 73 if (!converted[2].IsFunction())
74 return ThrowExceptionInJS(isolate, "Third argument to _fileSystem.write mu st be a function"); 74 return ThrowExceptionInJS(isolate, "Third argument to _fileSystem.write mu st be a function");
75 75
76 JsValueList values; 76 JsValueList values;
77 values.push_back(converted[2]); 77 values.push_back(converted[2]);
78 auto weakCallback = jsEngine->StoreJsValues(values); 78 auto weakCallback = jsEngine->StoreJsValues(values);
79 std::weak_ptr<JsEngine> weakJsEngine = jsEngine; 79 std::weak_ptr<JsEngine> weakJsEngine = jsEngine;
80 auto content = converted[1].AsStringBuffer();
80 jsEngine->GetAsyncFileSystem()->Write(converted[0].AsString(), 81 jsEngine->GetAsyncFileSystem()->Write(converted[0].AsString(),
81 converted[1].AsString(), 82 content,
82 [weakJsEngine, weakCallback](const std::string& error) 83 [weakJsEngine, weakCallback](const std::string& error)
83 { 84 {
84 auto jsEngine = weakJsEngine.lock(); 85 auto jsEngine = weakJsEngine.lock();
85 if (!jsEngine) 86 if (!jsEngine)
86 return; 87 return;
87 88
88 const JsContext context(*jsEngine); 89 const JsContext context(*jsEngine);
89 JsValueList params; 90 JsValueList params;
90 if (!error.empty()) 91 if (!error.empty())
91 params.push_back(jsEngine->NewValue(error)); 92 params.push_back(jsEngine->NewValue(error));
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
211 JsValue& FileSystemJsObject::Setup(JsEngine& jsEngine, JsValue& obj) 212 JsValue& FileSystemJsObject::Setup(JsEngine& jsEngine, JsValue& obj)
212 { 213 {
213 obj.SetProperty("read", jsEngine.NewCallback(::ReadCallback)); 214 obj.SetProperty("read", jsEngine.NewCallback(::ReadCallback));
214 obj.SetProperty("write", jsEngine.NewCallback(::WriteCallback)); 215 obj.SetProperty("write", jsEngine.NewCallback(::WriteCallback));
215 obj.SetProperty("move", jsEngine.NewCallback(::MoveCallback)); 216 obj.SetProperty("move", jsEngine.NewCallback(::MoveCallback));
216 obj.SetProperty("remove", jsEngine.NewCallback(::RemoveCallback)); 217 obj.SetProperty("remove", jsEngine.NewCallback(::RemoveCallback));
217 obj.SetProperty("stat", jsEngine.NewCallback(::StatCallback)); 218 obj.SetProperty("stat", jsEngine.NewCallback(::StatCallback));
218 obj.SetProperty("resolve", jsEngine.NewCallback(::ResolveCallback)); 219 obj.SetProperty("resolve", jsEngine.NewCallback(::ResolveCallback));
219 return obj; 220 return obj;
220 } 221 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld