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: Updated patch after review. Created June 16, 2017, 9:52 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 23 matching lines...) Expand all
34 { 34 {
35 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments); 35 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments);
36 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments); 36 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments);
37 37
38 v8::Isolate* isolate = arguments.GetIsolate(); 38 v8::Isolate* isolate = arguments.GetIsolate();
39 if (converted.size() != 2) 39 if (converted.size() != 2)
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 JsValue callback(converted[1]); 44 JsValueList values;
45 values.push_back(converted[1]);
46 auto weakCallback = jsEngine->StoreJsValues(values);
45 std::weak_ptr<JsEngine> weakJsEngine = jsEngine; 47 std::weak_ptr<JsEngine> weakJsEngine = jsEngine;
46 jsEngine->GetAsyncFileSystem()->Read(converted[0].AsString(), 48 jsEngine->GetAsyncFileSystem()->Read(converted[0].AsString(),
47 [weakJsEngine, callback] 49 [weakJsEngine, weakCallback]
sergei 2017/07/03 09:25:54 `JsValue callback` should not be captured by value
hub 2017/07/04 19:58:27 I'll fix that here and there.
48 (std::string&& content, const std::string& error) 50 (IFileSystem::IOBuffer&& content, const std::string& error)
49 { 51 {
50 auto jsEngine = weakJsEngine.lock(); 52 auto jsEngine = weakJsEngine.lock();
51 if (!jsEngine) 53 if (!jsEngine)
52 return; 54 return;
53 55
54 const JsContext context(*jsEngine); 56 const JsContext context(*jsEngine);
55 auto result = jsEngine->NewObject(); 57 auto result = jsEngine->NewObject();
56 result.SetProperty("content", std::move(content)); 58 result.SetStringBufferProperty("content", std::move(content));
57 result.SetProperty("error", error); 59 if (!error.empty())
58 callback.Call(result); 60 result.SetProperty("error", error);
61 jsEngine->TakeJsValues(weakCallback)[0].Call(result);
59 }); 62 });
60 } 63 }
61 64
62 void WriteCallback(const v8::FunctionCallbackInfo<v8::Value>& arguments) 65 void WriteCallback(const v8::FunctionCallbackInfo<v8::Value>& arguments)
63 { 66 {
64 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments); 67 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments);
65 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments); 68 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments);
66 69
67 v8::Isolate* isolate = arguments.GetIsolate(); 70 v8::Isolate* isolate = arguments.GetIsolate();
68 if (converted.size() != 3) 71 if (converted.size() != 3)
69 return ThrowExceptionInJS(isolate, "_fileSystem.write requires 3 parameter s"); 72 return ThrowExceptionInJS(isolate, "_fileSystem.write requires 3 parameter s");
70 if (!converted[2].IsFunction()) 73 if (!converted[2].IsFunction())
71 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");
72 75
73 JsValue callback(converted[2]); 76 JsValueList values;
74 std::weak_ptr<JsEngine> weakJsEngine = jsEngine; 77 values.push_back(converted[2]);
78 auto weakCallback = jsEngine->StoreJsValues(values);
79 std::weak_ptr<JsEngine> weakJsEngine = jsEngine;
80 auto content = converted[1].AsStringBuffer();
75 jsEngine->GetAsyncFileSystem()->Write(converted[0].AsString(), 81 jsEngine->GetAsyncFileSystem()->Write(converted[0].AsString(),
76 converted[1].AsString(), 82 content,
77 [weakJsEngine, callback](const std::string& error) 83 [weakJsEngine, weakCallback](const std::string& error)
78 { 84 {
79 auto jsEngine = weakJsEngine.lock(); 85 auto jsEngine = weakJsEngine.lock();
80 if (!jsEngine) 86 if (!jsEngine)
81 return; 87 return;
82 88
83 const JsContext context(*jsEngine); 89 const JsContext context(*jsEngine);
84 JsValueList params; 90 JsValueList params;
85 params.push_back(jsEngine->NewValue(error)); 91 if (!error.empty())
86 callback.Call(params); 92 params.push_back(jsEngine->NewValue(error));
93 jsEngine->TakeJsValues(weakCallback)[0].Call(params);
87 }); 94 });
88 } 95 }
89 96
90 void MoveCallback(const v8::FunctionCallbackInfo<v8::Value>& arguments) 97 void MoveCallback(const v8::FunctionCallbackInfo<v8::Value>& arguments)
91 { 98 {
92 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments); 99 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments);
93 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments); 100 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments);
94 101
95 v8::Isolate* isolate = arguments.GetIsolate(); 102 v8::Isolate* isolate = arguments.GetIsolate();
96 if (converted.size() != 3) 103 if (converted.size() != 3)
97 return ThrowExceptionInJS(isolate, "_fileSystem.move requires 3 parameters "); 104 return ThrowExceptionInJS(isolate, "_fileSystem.move requires 3 parameters ");
98 if (!converted[2].IsFunction()) 105 if (!converted[2].IsFunction())
99 return ThrowExceptionInJS(isolate, "Third argument to _fileSystem.move mus t be a function"); 106 return ThrowExceptionInJS(isolate, "Third argument to _fileSystem.move mus t be a function");
100 107
101 JsValue callback(converted[2]); 108 JsValueList values;
109 values.push_back(converted[2]);
110 auto weakCallback = jsEngine->StoreJsValues(values);
102 std::weak_ptr<JsEngine> weakJsEngine = jsEngine; 111 std::weak_ptr<JsEngine> weakJsEngine = jsEngine;
103 jsEngine->GetAsyncFileSystem()->Move(converted[0].AsString(), 112 jsEngine->GetAsyncFileSystem()->Move(converted[0].AsString(),
104 converted[1].AsString(), 113 converted[1].AsString(),
105 [weakJsEngine, callback](const std::string& error) 114 [weakJsEngine, weakCallback](const std::string& error)
106 { 115 {
107 auto jsEngine = weakJsEngine.lock(); 116 auto jsEngine = weakJsEngine.lock();
108 if (!jsEngine) 117 if (!jsEngine)
109 return; 118 return;
110 119
111 const JsContext context(*jsEngine); 120 const JsContext context(*jsEngine);
112 auto errorValue = jsEngine->NewValue(error); 121 JsValueList params;
113 JsValueList params; 122 if (!error.empty())
114 params.push_back(errorValue); 123 params.push_back(jsEngine->NewValue(error));
sergei 2017/07/03 09:25:54 But the problem is that the empty string will be c
hub 2017/07/04 19:58:27 This mean we are changing the semantics of the API
115 callback.Call(params); 124 jsEngine->TakeJsValues(weakCallback)[0].Call(params);
116 }); 125 });
117 } 126 }
118 127
119 void RemoveCallback(const v8::FunctionCallbackInfo<v8::Value>& arguments) 128 void RemoveCallback(const v8::FunctionCallbackInfo<v8::Value>& arguments)
120 { 129 {
121 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments); 130 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments);
122 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments); 131 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments);
123 132
124 v8::Isolate* isolate = arguments.GetIsolate(); 133 v8::Isolate* isolate = arguments.GetIsolate();
125 if (converted.size() != 2) 134 if (converted.size() != 2)
126 return ThrowExceptionInJS(isolate, "_fileSystem.remove requires 2 paramete rs"); 135 return ThrowExceptionInJS(isolate, "_fileSystem.remove requires 2 paramete rs");
127 if (!converted[1].IsFunction()) 136 if (!converted[1].IsFunction())
128 return ThrowExceptionInJS(isolate, "Second argument to _fileSystem.remove must be a function"); 137 return ThrowExceptionInJS(isolate, "Second argument to _fileSystem.remove must be a function");
129 138
130 JsValue callback(converted[1]); 139 JsValueList values;
140 values.push_back(converted[1]);
141 auto weakCallback = jsEngine->StoreJsValues(values);
131 std::weak_ptr<JsEngine> weakJsEngine = jsEngine; 142 std::weak_ptr<JsEngine> weakJsEngine = jsEngine;
132 jsEngine->GetAsyncFileSystem()->Remove(converted[0].AsString(), 143 jsEngine->GetAsyncFileSystem()->Remove(converted[0].AsString(),
133 [weakJsEngine, callback](const std::string& error) 144 [weakJsEngine, weakCallback](const std::string& error)
134 { 145 {
135 auto jsEngine = weakJsEngine.lock(); 146 auto jsEngine = weakJsEngine.lock();
136 if (!jsEngine) 147 if (!jsEngine)
137 return; 148 return;
138 149
139 const JsContext context(*jsEngine); 150 const JsContext context(*jsEngine);
140 auto errorValue = jsEngine->NewValue(error); 151 JsValueList params;
141 JsValueList params; 152 if (!error.empty())
142 params.push_back(errorValue); 153 params.push_back(jsEngine->NewValue(error));
143 callback.Call(params); 154 jsEngine->TakeJsValues(weakCallback)[0].Call(params);
144 }); 155 });
145 } 156 }
146 157
147 void StatCallback(const v8::FunctionCallbackInfo<v8::Value>& arguments) 158 void StatCallback(const v8::FunctionCallbackInfo<v8::Value>& arguments)
148 { 159 {
149 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments); 160 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments);
150 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments); 161 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments);
151 162
152 v8::Isolate* isolate = arguments.GetIsolate(); 163 v8::Isolate* isolate = arguments.GetIsolate();
153 if (converted.size() != 2) 164 if (converted.size() != 2)
154 return ThrowExceptionInJS(isolate, "_fileSystem.stat requires 2 parameters "); 165 return ThrowExceptionInJS(isolate, "_fileSystem.stat requires 2 parameters ");
155 if (!converted[1].IsFunction()) 166 if (!converted[1].IsFunction())
156 return ThrowExceptionInJS(isolate, "Second argument to _fileSystem.stat mu st be a function"); 167 return ThrowExceptionInJS(isolate, "Second argument to _fileSystem.stat mu st be a function");
157 168
158 JsValue callback(converted[1]); 169 JsValueList values;
170 values.push_back(converted[1]);
171 auto weakCallback = jsEngine->StoreJsValues(values);
159 std::weak_ptr<JsEngine> weakJsEngine = jsEngine; 172 std::weak_ptr<JsEngine> weakJsEngine = jsEngine;
160 jsEngine->GetAsyncFileSystem()->Stat(converted[0].AsString(), 173 jsEngine->GetAsyncFileSystem()->Stat(converted[0].AsString(),
161 [weakJsEngine, callback] 174 [weakJsEngine, weakCallback]
162 (const IFileSystem::StatResult& statResult, const std::string& error) 175 (const IFileSystem::StatResult& statResult, const std::string& error)
163 { 176 {
164 auto jsEngine = weakJsEngine.lock(); 177 auto jsEngine = weakJsEngine.lock();
165 if (!jsEngine) 178 if (!jsEngine)
166 return; 179 return;
167 180
168 const JsContext context(*jsEngine); 181 const JsContext context(*jsEngine);
169 auto result = jsEngine->NewObject(); 182 auto result = jsEngine->NewObject();
170 183
171 result.SetProperty("exists", statResult.exists); 184 result.SetProperty("exists", statResult.exists);
172 result.SetProperty("isFile", statResult.isFile); 185 result.SetProperty("isFile", statResult.isFile);
173 result.SetProperty("isDirectory", statResult.isDirectory); 186 result.SetProperty("isDirectory", statResult.isDirectory);
174 result.SetProperty("lastModified", statResult.lastModified); 187 result.SetProperty("lastModified", statResult.lastModified);
175 result.SetProperty("error", error); 188 if (!error.empty())
189 result.SetProperty("error", error);
176 190
177 JsValueList params; 191 JsValueList params;
178 params.push_back(result); 192 params.push_back(result);
179 callback.Call(params); 193 jsEngine->TakeJsValues(weakCallback)[0].Call(params);
180 }); 194 });
181 } 195 }
182 196
183 void ResolveCallback(const v8::FunctionCallbackInfo<v8::Value>& arguments) 197 void ResolveCallback(const v8::FunctionCallbackInfo<v8::Value>& arguments)
184 { 198 {
185 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments); 199 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments);
186 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments); 200 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments);
187 201
188 v8::Isolate* isolate = arguments.GetIsolate(); 202 v8::Isolate* isolate = arguments.GetIsolate();
189 if (converted.size() != 1) 203 if (converted.size() != 1)
190 return ThrowExceptionInJS(isolate, "_fileSystem.resolve requires 1 paramet er"); 204 return ThrowExceptionInJS(isolate, "_fileSystem.resolve requires 1 paramet er");
191 205
192 std::string resolved = jsEngine->GetAsyncFileSystem()->Resolve(converted[0]. AsString()); 206 std::string resolved = jsEngine->GetAsyncFileSystem()->Resolve(converted[0]. AsString());
193 arguments.GetReturnValue().Set(Utils::ToV8String(isolate, resolved)); 207 arguments.GetReturnValue().Set(Utils::ToV8String(isolate, resolved));
194 } 208 }
195 } 209 }
196 210
197 211
198 JsValue& FileSystemJsObject::Setup(JsEngine& jsEngine, JsValue& obj) 212 JsValue& FileSystemJsObject::Setup(JsEngine& jsEngine, JsValue& obj)
199 { 213 {
200 obj.SetProperty("read", jsEngine.NewCallback(::ReadCallback)); 214 obj.SetProperty("read", jsEngine.NewCallback(::ReadCallback));
201 obj.SetProperty("write", jsEngine.NewCallback(::WriteCallback)); 215 obj.SetProperty("write", jsEngine.NewCallback(::WriteCallback));
202 obj.SetProperty("move", jsEngine.NewCallback(::MoveCallback)); 216 obj.SetProperty("move", jsEngine.NewCallback(::MoveCallback));
203 obj.SetProperty("remove", jsEngine.NewCallback(::RemoveCallback)); 217 obj.SetProperty("remove", jsEngine.NewCallback(::RemoveCallback));
204 obj.SetProperty("stat", jsEngine.NewCallback(::StatCallback)); 218 obj.SetProperty("stat", jsEngine.NewCallback(::StatCallback));
205 obj.SetProperty("resolve", jsEngine.NewCallback(::ResolveCallback)); 219 obj.SetProperty("resolve", jsEngine.NewCallback(::ResolveCallback));
206 return obj; 220 return obj;
207 } 221 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld