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

Side by Side Diff: src/FileSystemJsObject.cpp

Issue 29367003: Issue #4711 - Rewrite legacy thread facilities
Patch Set: Created Dec. 7, 2016, 4:44 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 /* 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-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 #include <AdblockPlus/FileSystem.h> 18 #include <AdblockPlus/FileSystem.h>
19 #include <stdexcept> 19 #include <stdexcept>
20 #include <sstream> 20 #include <sstream>
21 #include <vector> 21 #include <vector>
22 22
23 #include <AdblockPlus/JsValue.h> 23 #include <AdblockPlus/JsValue.h>
24 #include "FileSystemJsObject.h" 24 #include "FileSystemJsObject.h"
25 #include "JsContext.h" 25 #include "JsContext.h"
26 #include "Thread.h" 26 #include "Scheduler.h"
27 #include "Utils.h" 27 #include "Utils.h"
28 28
29 using namespace AdblockPlus; 29 using namespace AdblockPlus;
30 30
31 namespace 31 namespace
32 { 32 {
33 class IoThread : public Thread 33 class IoTask
34 { 34 {
35 protected:
36 virtual void operator()() = 0;
37 JsEnginePtr jsEngine;
38 FileSystemPtr fileSystem;
39 JsValuePtr callback;
40
35 public: 41 public:
36 IoThread(JsEnginePtr jsEngine, JsValuePtr callback) 42 IoTask(JsEnginePtr jsEngine, JsValuePtr callback)
37 : Thread(true), jsEngine(jsEngine), fileSystem(jsEngine->GetFileSystem()), 43 : jsEngine(jsEngine), fileSystem(jsEngine->GetFileSystem()),
38 callback(callback) 44 callback(callback)
39 { 45 {
40 } 46 }
41 47
42 protected: 48 virtual ~IoTask() {}
43 JsEnginePtr jsEngine;
44 FileSystemPtr fileSystem;
45 JsValuePtr callback;
46 }; 49 };
47 50
48 class ReadThread : public IoThread 51 class ReadTask : public IoTask
49 { 52 {
50 public: 53 public:
51 ReadThread(JsEnginePtr jsEngine, JsValuePtr callback, 54 ReadTask(JsEnginePtr jsEngine, JsValuePtr callback,
52 const std::string& path) 55 const std::string& path)
53 : IoThread(jsEngine, callback), path(path) 56 : IoTask(jsEngine, callback), path(path)
54 { 57 {
55 } 58 }
56 59
57 void Run() 60 void operator()()
58 { 61 {
59 std::string content; 62 std::string content;
60 std::string error; 63 std::string error;
61 try 64 try
62 { 65 {
63 std::shared_ptr<std::istream> stream = fileSystem->Read(path); 66 std::shared_ptr<std::istream> stream = fileSystem->Read(path);
64 content = Utils::Slurp(*stream); 67 content = Utils::Slurp(*stream);
65 } 68 }
66 catch (std::exception& e) 69 catch (std::exception& e)
67 { 70 {
(...skipping 10 matching lines...) Expand all
78 result->SetProperty("error", error); 81 result->SetProperty("error", error);
79 JsValueList params; 82 JsValueList params;
80 params.push_back(result); 83 params.push_back(result);
81 callback->Call(params); 84 callback->Call(params);
82 } 85 }
83 86
84 private: 87 private:
85 std::string path; 88 std::string path;
86 }; 89 };
87 90
88 class WriteThread : public IoThread 91 class WriteTask : public IoTask
89 { 92 {
90 public: 93 public:
91 WriteThread(JsEnginePtr jsEngine, JsValuePtr callback, 94 WriteTask(JsEnginePtr jsEngine, JsValuePtr callback,
92 const std::string& path, const std::string& content) 95 const std::string& path, const std::string& content)
93 : IoThread(jsEngine, callback), path(path), content(content) 96 : IoTask(jsEngine, callback), path(path), content(content)
94 { 97 {
95 } 98 }
96 99
97 void Run() 100 void operator()()
98 { 101 {
99 std::string error; 102 std::string error;
100 try 103 try
101 { 104 {
102 std::shared_ptr<std::iostream> stream(new std::stringstream); 105 std::shared_ptr<std::iostream> stream(new std::stringstream);
103 *stream << content; 106 *stream << content;
104 fileSystem->Write(path, stream); 107 fileSystem->Write(path, stream);
105 } 108 }
106 catch (std::exception& e) 109 catch (std::exception& e)
107 { 110 {
108 error = e.what(); 111 error = e.what();
109 } 112 }
110 catch (...) 113 catch (...)
111 { 114 {
112 error = "Unknown error while writing to " + path; 115 error = "Unknown error while writing to " + path;
113 } 116 }
114 117
115 const JsContext context(jsEngine); 118 const JsContext context(jsEngine);
116 JsValuePtr errorValue = jsEngine->NewValue(error); 119 JsValuePtr errorValue = jsEngine->NewValue(error);
117 JsValueList params; 120 JsValueList params;
118 params.push_back(errorValue); 121 params.push_back(errorValue);
119 callback->Call(params); 122 callback->Call(params);
120 } 123 }
121 124
122 private: 125 private:
123 std::string path; 126 std::string path;
124 std::string content; 127 std::string content;
125 }; 128 };
126 129
127 class MoveThread : public IoThread 130 class MoveTask : public IoTask
128 { 131 {
129 public: 132 public:
130 MoveThread(JsEnginePtr jsEngine, JsValuePtr callback, 133 MoveTask(JsEnginePtr jsEngine, JsValuePtr callback,
131 const std::string& fromPath, const std::string& toPath) 134 const std::string& fromPath, const std::string& toPath)
132 : IoThread(jsEngine, callback), fromPath(fromPath), toPath(toPath) 135 : IoTask(jsEngine, callback), fromPath(fromPath), toPath(toPath)
133 { 136 {
134 } 137 }
135 138
136 void Run() 139 void operator()()
137 { 140 {
138 std::string error; 141 std::string error;
139 try 142 try
140 { 143 {
141 fileSystem->Move(fromPath, toPath); 144 fileSystem->Move(fromPath, toPath);
142 } 145 }
143 catch (std::exception& e) 146 catch (std::exception& e)
144 { 147 {
145 error = e.what(); 148 error = e.what();
146 } 149 }
147 catch (...) 150 catch (...)
148 { 151 {
149 error = "Unknown error while moving " + fromPath + " to " + toPath; 152 error = "Unknown error while moving " + fromPath + " to " + toPath;
150 } 153 }
151 154
152 const JsContext context(jsEngine); 155 const JsContext context(jsEngine);
153 JsValuePtr errorValue = jsEngine->NewValue(error); 156 JsValuePtr errorValue = jsEngine->NewValue(error);
154 JsValueList params; 157 JsValueList params;
155 params.push_back(errorValue); 158 params.push_back(errorValue);
156 callback->Call(params); 159 callback->Call(params);
157 } 160 }
158 161
159 private: 162 private:
160 std::string fromPath; 163 std::string fromPath;
161 std::string toPath; 164 std::string toPath;
162 }; 165 };
163 166
164 class RemoveThread : public IoThread 167 class RemoveTask : public IoTask
165 { 168 {
166 public: 169 public:
167 RemoveThread(JsEnginePtr jsEngine, JsValuePtr callback, 170 RemoveTask(JsEnginePtr jsEngine, JsValuePtr callback,
168 const std::string& path) 171 const std::string& path)
169 : IoThread(jsEngine, callback), path(path) 172 : IoTask(jsEngine, callback), path(path)
170 { 173 {
171 } 174 }
172 175
173 void Run() 176 void operator()()
174 { 177 {
175 std::string error; 178 std::string error;
176 try 179 try
177 { 180 {
178 fileSystem->Remove(path); 181 fileSystem->Remove(path);
179 } 182 }
180 catch (std::exception& e) 183 catch (std::exception& e)
181 { 184 {
182 error = e.what(); 185 error = e.what();
183 } 186 }
184 catch (...) 187 catch (...)
185 { 188 {
186 error = "Unknown error while removing " + path; 189 error = "Unknown error while removing " + path;
187 } 190 }
188 191
189 const JsContext context(jsEngine); 192 const JsContext context(jsEngine);
190 JsValuePtr errorValue = jsEngine->NewValue(error); 193 JsValuePtr errorValue = jsEngine->NewValue(error);
191 JsValueList params; 194 JsValueList params;
192 params.push_back(errorValue); 195 params.push_back(errorValue);
193 callback->Call(params); 196 callback->Call(params);
194 } 197 }
195 198
196 private: 199 private:
197 std::string path; 200 std::string path;
198 }; 201 };
199 202
200 203
201 class StatThread : public IoThread 204 class StatTask : public IoTask
202 { 205 {
203 public: 206 public:
204 StatThread(JsEnginePtr jsEngine, JsValuePtr callback, 207 StatTask(JsEnginePtr jsEngine, JsValuePtr callback,
205 const std::string& path) 208 const std::string& path)
206 : IoThread(jsEngine, callback), path(path) 209 : IoTask(jsEngine, callback), path(path)
207 { 210 {
208 } 211 }
209 212
210 void Run() 213 void operator()()
211 { 214 {
212 std::string error; 215 std::string error;
213 FileSystem::StatResult statResult; 216 FileSystem::StatResult statResult;
214 try 217 try
215 { 218 {
216 statResult = fileSystem->Stat(path); 219 statResult = fileSystem->Stat(path);
217 } 220 }
218 catch (std::exception& e) 221 catch (std::exception& e)
219 { 222 {
220 error = e.what(); 223 error = e.what();
(...skipping 25 matching lines...) Expand all
246 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments); 249 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments);
247 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments); 250 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments);
248 251
249 v8::Isolate* isolate = arguments.GetIsolate(); 252 v8::Isolate* isolate = arguments.GetIsolate();
250 if (converted.size() != 2) 253 if (converted.size() != 2)
251 return v8::ThrowException(Utils::ToV8String(isolate, 254 return v8::ThrowException(Utils::ToV8String(isolate,
252 "_fileSystem.read requires 2 parameters")); 255 "_fileSystem.read requires 2 parameters"));
253 if (!converted[1]->IsFunction()) 256 if (!converted[1]->IsFunction())
254 return v8::ThrowException(Utils::ToV8String(isolate, 257 return v8::ThrowException(Utils::ToV8String(isolate,
255 "Second argument to _fileSystem.read must be a function")); 258 "Second argument to _fileSystem.read must be a function"));
256 ReadThread* const readThread = new ReadThread(jsEngine, converted[1], 259 const auto readTask = std::make_shared<ReadTask>(jsEngine, converted[1],
257 converted[0]->AsString()); 260 converted[0]->AsString());
258 readThread->Start(); 261 AdblockPlus::Scheduler::StartImmediatelyInSingleUseThread(AdblockPlus::MakeH eapFunction(readTask));
259 return v8::Undefined(); 262 return v8::Undefined();
260 } 263 }
261 264
262 v8::Handle<v8::Value> WriteCallback(const v8::Arguments& arguments) 265 v8::Handle<v8::Value> WriteCallback(const v8::Arguments& arguments)
263 { 266 {
264 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments); 267 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments);
265 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments); 268 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments);
266 269
267 v8::Isolate* isolate = arguments.GetIsolate(); 270 v8::Isolate* isolate = arguments.GetIsolate();
268 if (converted.size() != 3) 271 if (converted.size() != 3)
269 return v8::ThrowException(Utils::ToV8String(isolate, 272 return v8::ThrowException(Utils::ToV8String(isolate,
270 "_fileSystem.write requires 3 parameters")); 273 "_fileSystem.write requires 3 parameters"));
271 if (!converted[2]->IsFunction()) 274 if (!converted[2]->IsFunction())
272 return v8::ThrowException(Utils::ToV8String(isolate, 275 return v8::ThrowException(Utils::ToV8String(isolate,
273 "Third argument to _fileSystem.write must be a function")); 276 "Third argument to _fileSystem.write must be a function"));
274 WriteThread* const writeThread = new WriteThread(jsEngine, converted[2], 277 const auto writeTask = std::make_shared<WriteTask>(jsEngine, converted[2],
275 converted[0]->AsString(), converted[1]->AsString()); 278 converted[0]->AsString(), converted[1]->AsString());
276 writeThread->Start(); 279 AdblockPlus::Scheduler::StartImmediatelyInSingleUseThread(AdblockPlus::MakeH eapFunction(writeTask));
277 return v8::Undefined(); 280 return v8::Undefined();
278 } 281 }
279 282
280 v8::Handle<v8::Value> MoveCallback(const v8::Arguments& arguments) 283 v8::Handle<v8::Value> MoveCallback(const v8::Arguments& arguments)
281 { 284 {
282 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments); 285 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments);
283 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments); 286 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments);
284 287
285 v8::Isolate* isolate = arguments.GetIsolate(); 288 v8::Isolate* isolate = arguments.GetIsolate();
286 if (converted.size() != 3) 289 if (converted.size() != 3)
287 return v8::ThrowException(Utils::ToV8String(isolate, 290 return v8::ThrowException(Utils::ToV8String(isolate,
288 "_fileSystem.move requires 3 parameters")); 291 "_fileSystem.move requires 3 parameters"));
289 if (!converted[2]->IsFunction()) 292 if (!converted[2]->IsFunction())
290 return v8::ThrowException(Utils::ToV8String(isolate, 293 return v8::ThrowException(Utils::ToV8String(isolate,
291 "Third argument to _fileSystem.move must be a function")); 294 "Third argument to _fileSystem.move must be a function"));
292 MoveThread* const moveThread = new MoveThread(jsEngine, converted[2], 295 const auto moveTask = std::make_shared<MoveTask>(jsEngine, converted[2],
293 converted[0]->AsString(), converted[1]->AsString()); 296 converted[0]->AsString(), converted[1]->AsString());
294 moveThread->Start(); 297 AdblockPlus::Scheduler::StartImmediatelyInSingleUseThread(AdblockPlus::MakeH eapFunction(moveTask));
295 return v8::Undefined(); 298 return v8::Undefined();
296 } 299 }
297 300
298 v8::Handle<v8::Value> RemoveCallback(const v8::Arguments& arguments) 301 v8::Handle<v8::Value> RemoveCallback(const v8::Arguments& arguments)
299 { 302 {
300 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments); 303 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments);
301 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments); 304 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments);
302 305
303 v8::Isolate* isolate = arguments.GetIsolate(); 306 v8::Isolate* isolate = arguments.GetIsolate();
304 if (converted.size() != 2) 307 if (converted.size() != 2)
305 return v8::ThrowException(Utils::ToV8String(isolate, 308 return v8::ThrowException(Utils::ToV8String(isolate,
306 "_fileSystem.remove requires 2 parameters")); 309 "_fileSystem.remove requires 2 parameters"));
307 if (!converted[1]->IsFunction()) 310 if (!converted[1]->IsFunction())
308 return v8::ThrowException(Utils::ToV8String(isolate, 311 return v8::ThrowException(Utils::ToV8String(isolate,
309 "Second argument to _fileSystem.remove must be a function")); 312 "Second argument to _fileSystem.remove must be a function"));
310 RemoveThread* const removeThread = new RemoveThread(jsEngine, converted[1], 313 const auto removeTask = std::make_shared<RemoveTask>(jsEngine, converted[1],
311 converted[0]->AsString()); 314 converted[0]->AsString());
312 removeThread->Start(); 315 AdblockPlus::Scheduler::StartImmediatelyInSingleUseThread(AdblockPlus::MakeH eapFunction(removeTask));
313 return v8::Undefined(); 316 return v8::Undefined();
314 } 317 }
315 318
316 v8::Handle<v8::Value> StatCallback(const v8::Arguments& arguments) 319 v8::Handle<v8::Value> StatCallback(const v8::Arguments& arguments)
317 { 320 {
318 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments); 321 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments);
319 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments); 322 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments);
320 323
321 v8::Isolate* isolate = arguments.GetIsolate(); 324 v8::Isolate* isolate = arguments.GetIsolate();
322 if (converted.size() != 2) 325 if (converted.size() != 2)
323 return v8::ThrowException(Utils::ToV8String(isolate, 326 return v8::ThrowException(Utils::ToV8String(isolate,
324 "_fileSystem.stat requires 2 parameters")); 327 "_fileSystem.stat requires 2 parameters"));
325 if (!converted[1]->IsFunction()) 328 if (!converted[1]->IsFunction())
326 return v8::ThrowException(Utils::ToV8String(isolate, 329 return v8::ThrowException(Utils::ToV8String(isolate,
327 "Second argument to _fileSystem.stat must be a function")); 330 "Second argument to _fileSystem.stat must be a function"));
328 StatThread* const statThread = new StatThread(jsEngine, converted[1], 331 const auto statTask = std::make_shared<StatTask>(jsEngine, converted[1],
329 converted[0]->AsString()); 332 converted[0]->AsString());
330 statThread->Start(); 333 AdblockPlus::Scheduler::StartImmediatelyInSingleUseThread(AdblockPlus::MakeH eapFunction(statTask));
331 return v8::Undefined(); 334 return v8::Undefined();
332 } 335 }
333 336
334 v8::Handle<v8::Value> ResolveCallback(const v8::Arguments& arguments) 337 v8::Handle<v8::Value> ResolveCallback(const v8::Arguments& arguments)
335 { 338 {
336 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments); 339 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments);
337 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments); 340 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments);
338 341
339 v8::Isolate* isolate = arguments.GetIsolate(); 342 v8::Isolate* isolate = arguments.GetIsolate();
340 if (converted.size() != 1) 343 if (converted.size() != 1)
(...skipping 11 matching lines...) Expand all
352 JsValuePtr FileSystemJsObject::Setup(JsEnginePtr jsEngine, JsValuePtr obj) 355 JsValuePtr FileSystemJsObject::Setup(JsEnginePtr jsEngine, JsValuePtr obj)
353 { 356 {
354 obj->SetProperty("read", jsEngine->NewCallback(::ReadCallback)); 357 obj->SetProperty("read", jsEngine->NewCallback(::ReadCallback));
355 obj->SetProperty("write", jsEngine->NewCallback(::WriteCallback)); 358 obj->SetProperty("write", jsEngine->NewCallback(::WriteCallback));
356 obj->SetProperty("move", jsEngine->NewCallback(::MoveCallback)); 359 obj->SetProperty("move", jsEngine->NewCallback(::MoveCallback));
357 obj->SetProperty("remove", jsEngine->NewCallback(::RemoveCallback)); 360 obj->SetProperty("remove", jsEngine->NewCallback(::RemoveCallback));
358 obj->SetProperty("stat", jsEngine->NewCallback(::StatCallback)); 361 obj->SetProperty("stat", jsEngine->NewCallback(::StatCallback));
359 obj->SetProperty("resolve", jsEngine->NewCallback(::ResolveCallback)); 362 obj->SetProperty("resolve", jsEngine->NewCallback(::ResolveCallback));
360 return obj; 363 return obj;
361 } 364 }
OLDNEW
« no previous file with comments | « libadblockplus.gyp ('k') | src/FilterEngine.cpp » ('j') | src/GlobalJsObject.cpp » ('J')

Powered by Google App Engine
This is Rietveld