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

Side by Side Diff: src/FileSystemJsObject.cpp

Issue 29417605: Issue 5034 - Part 3: Create plain JsValue instead of JsValuePtr (Closed) Base URL: https://hg.adblockplus.org/libadblockplus/
Patch Set: FIxed JsContext bug and a few others. Created April 19, 2017, 9:55 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-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 15 matching lines...) Expand all
26 #include "Thread.h" 26 #include "Thread.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 IoThread : public Thread
34 { 34 {
35 public: 35 public:
36 IoThread(const JsEnginePtr& jsEngine, const JsConstValuePtr& callback) 36 IoThread(const JsEnginePtr& jsEngine, const JsValue& callback)
37 : Thread(true), jsEngine(jsEngine), fileSystem(jsEngine->GetFileSystem()), 37 : Thread(true), jsEngine(jsEngine), fileSystem(jsEngine->GetFileSystem()),
38 callback(callback) 38 callback(callback)
39 { 39 {
40 } 40 }
41 41
42 protected: 42 protected:
43 JsEnginePtr jsEngine; 43 JsEnginePtr jsEngine;
44 FileSystemPtr fileSystem; 44 FileSystemPtr fileSystem;
45 JsConstValuePtr callback; 45 JsValue callback;
46 }; 46 };
47 47
48 class ReadThread : public IoThread 48 class ReadThread : public IoThread
49 { 49 {
50 public: 50 public:
51 ReadThread(const JsEnginePtr& jsEngine, const JsConstValuePtr& callback, 51 ReadThread(const JsEnginePtr& jsEngine, const JsValue& callback,
52 const std::string& path) 52 const std::string& path)
53 : IoThread(jsEngine, callback), path(path) 53 : IoThread(jsEngine, callback), path(path)
54 { 54 {
55 } 55 }
56 56
57 void Run() 57 void Run()
58 { 58 {
59 std::string content; 59 std::string content;
60 std::string error; 60 std::string error;
61 try 61 try
62 { 62 {
63 std::shared_ptr<std::istream> stream = fileSystem->Read(path); 63 std::shared_ptr<std::istream> stream = fileSystem->Read(path);
64 content = Utils::Slurp(*stream); 64 content = Utils::Slurp(*stream);
65 } 65 }
66 catch (std::exception& e) 66 catch (std::exception& e)
67 { 67 {
68 error = e.what(); 68 error = e.what();
69 } 69 }
70 catch (...) 70 catch (...)
71 { 71 {
72 error = "Unknown error while reading from " + path; 72 error = "Unknown error while reading from " + path;
73 } 73 }
74 74
75 const JsContext context(jsEngine); 75 const JsContext context(jsEngine);
76 JsValuePtr result = jsEngine->NewObject(); 76 auto result = jsEngine->NewObject();
77 result->SetProperty("content", content); 77 result.SetProperty("content", content);
78 result->SetProperty("error", error); 78 result.SetProperty("error", error);
79 JsConstValueList params; 79 JsValueList params;
80 params.push_back(result); 80 params.push_back(result);
81 callback->Call(params); 81 callback.Call(params);
82 } 82 }
83 83
84 private: 84 private:
85 std::string path; 85 std::string path;
86 }; 86 };
87 87
88 class WriteThread : public IoThread 88 class WriteThread : public IoThread
89 { 89 {
90 public: 90 public:
91 WriteThread(const JsEnginePtr& jsEngine, const JsConstValuePtr& callback, 91 WriteThread(const JsEnginePtr& jsEngine, const JsValue& callback,
92 const std::string& path, const std::string& content) 92 const std::string& path, const std::string& content)
93 : IoThread(jsEngine, callback), path(path), content(content) 93 : IoThread(jsEngine, callback), path(path), content(content)
94 { 94 {
95 } 95 }
96 96
97 void Run() 97 void Run()
98 { 98 {
99 std::string error; 99 std::string error;
100 try 100 try
101 { 101 {
102 std::stringstream stream; 102 std::stringstream stream;
103 stream << content; 103 stream << content;
104 fileSystem->Write(path, stream); 104 fileSystem->Write(path, stream);
105 } 105 }
106 catch (std::exception& e) 106 catch (std::exception& e)
107 { 107 {
108 error = e.what(); 108 error = e.what();
109 } 109 }
110 catch (...) 110 catch (...)
111 { 111 {
112 error = "Unknown error while writing to " + path; 112 error = "Unknown error while writing to " + path;
113 } 113 }
114 114
115 const JsContext context(jsEngine); 115 const JsContext context(jsEngine);
116 JsValuePtr errorValue = jsEngine->NewValue(error); 116 auto errorValue = jsEngine->NewValue(error);
117 JsConstValueList params; 117 JsValueList params;
118 params.push_back(errorValue); 118 params.push_back(errorValue);
119 callback->Call(params); 119 callback.Call(params);
120 } 120 }
121 121
122 private: 122 private:
123 std::string path; 123 std::string path;
124 std::string content; 124 std::string content;
125 }; 125 };
126 126
127 class MoveThread : public IoThread 127 class MoveThread : public IoThread
128 { 128 {
129 public: 129 public:
130 MoveThread(const JsEnginePtr& jsEngine, const JsConstValuePtr& callback, 130 MoveThread(const JsEnginePtr& jsEngine, const JsValue& callback,
131 const std::string& fromPath, const std::string& toPath) 131 const std::string& fromPath, const std::string& toPath)
132 : IoThread(jsEngine, callback), fromPath(fromPath), toPath(toPath) 132 : IoThread(jsEngine, callback), fromPath(fromPath), toPath(toPath)
133 { 133 {
134 } 134 }
135 135
136 void Run() 136 void Run()
137 { 137 {
138 std::string error; 138 std::string error;
139 try 139 try
140 { 140 {
141 fileSystem->Move(fromPath, toPath); 141 fileSystem->Move(fromPath, toPath);
142 } 142 }
143 catch (std::exception& e) 143 catch (std::exception& e)
144 { 144 {
145 error = e.what(); 145 error = e.what();
146 } 146 }
147 catch (...) 147 catch (...)
148 { 148 {
149 error = "Unknown error while moving " + fromPath + " to " + toPath; 149 error = "Unknown error while moving " + fromPath + " to " + toPath;
150 } 150 }
151 151
152 const JsContext context(jsEngine); 152 const JsContext context(jsEngine);
153 JsValuePtr errorValue = jsEngine->NewValue(error); 153 auto errorValue = jsEngine->NewValue(error);
154 JsConstValueList params; 154 JsValueList params;
155 params.push_back(errorValue); 155 params.push_back(errorValue);
156 callback->Call(params); 156 callback.Call(params);
157 } 157 }
158 158
159 private: 159 private:
160 std::string fromPath; 160 std::string fromPath;
161 std::string toPath; 161 std::string toPath;
162 }; 162 };
163 163
164 class RemoveThread : public IoThread 164 class RemoveThread : public IoThread
165 { 165 {
166 public: 166 public:
167 RemoveThread(const JsEnginePtr& jsEngine, const JsConstValuePtr& callback, 167 RemoveThread(const JsEnginePtr& jsEngine, const JsValue& callback,
168 const std::string& path) 168 const std::string& path)
169 : IoThread(jsEngine, callback), path(path) 169 : IoThread(jsEngine, callback), path(path)
170 { 170 {
171 } 171 }
172 172
173 void Run() 173 void Run()
174 { 174 {
175 std::string error; 175 std::string error;
176 try 176 try
177 { 177 {
178 fileSystem->Remove(path); 178 fileSystem->Remove(path);
179 } 179 }
180 catch (std::exception& e) 180 catch (std::exception& e)
181 { 181 {
182 error = e.what(); 182 error = e.what();
183 } 183 }
184 catch (...) 184 catch (...)
185 { 185 {
186 error = "Unknown error while removing " + path; 186 error = "Unknown error while removing " + path;
187 } 187 }
188 188
189 const JsContext context(jsEngine); 189 const JsContext context(jsEngine);
190 JsValuePtr errorValue = jsEngine->NewValue(error); 190 auto errorValue = jsEngine->NewValue(error);
191 JsConstValueList params; 191 JsValueList params;
192 params.push_back(errorValue); 192 params.push_back(errorValue);
193 callback->Call(params); 193 callback.Call(params);
194 } 194 }
195 195
196 private: 196 private:
197 std::string path; 197 std::string path;
198 }; 198 };
199 199
200 200
201 class StatThread : public IoThread 201 class StatThread : public IoThread
202 { 202 {
203 public: 203 public:
204 StatThread(const JsEnginePtr& jsEngine, const JsConstValuePtr& callback, 204 StatThread(const JsEnginePtr& jsEngine, const JsValue& callback,
205 const std::string& path) 205 const std::string& path)
206 : IoThread(jsEngine, callback), path(path) 206 : IoThread(jsEngine, callback), path(path)
207 { 207 {
208 } 208 }
209 209
210 void Run() 210 void Run()
211 { 211 {
212 std::string error; 212 std::string error;
213 FileSystem::StatResult statResult; 213 FileSystem::StatResult statResult;
214 try 214 try
215 { 215 {
216 statResult = fileSystem->Stat(path); 216 statResult = fileSystem->Stat(path);
217 } 217 }
218 catch (std::exception& e) 218 catch (std::exception& e)
219 { 219 {
220 error = e.what(); 220 error = e.what();
221 } 221 }
222 catch (...) 222 catch (...)
223 { 223 {
224 error = "Unknown error while calling stat on " + path; 224 error = "Unknown error while calling stat on " + path;
225 } 225 }
226 226
227 const JsContext context(jsEngine); 227 const JsContext context(jsEngine);
228 JsValuePtr result = jsEngine->NewObject(); 228 auto result = jsEngine->NewObject();
229 result->SetProperty("exists", statResult.exists); 229 result.SetProperty("exists", statResult.exists);
230 result->SetProperty("isFile", statResult.isFile); 230 result.SetProperty("isFile", statResult.isFile);
231 result->SetProperty("isDirectory", statResult.isDirectory); 231 result.SetProperty("isDirectory", statResult.isDirectory);
232 result->SetProperty("lastModified", statResult.lastModified); 232 result.SetProperty("lastModified", statResult.lastModified);
233 result->SetProperty("error", error); 233 result.SetProperty("error", error);
234 234
235 JsConstValueList params; 235 JsValueList params;
236 params.push_back(result); 236 params.push_back(result);
237 callback->Call(params); 237 callback.Call(params);
238 } 238 }
239 239
240 private: 240 private:
241 std::string path; 241 std::string path;
242 }; 242 };
243 243
244 v8::Handle<v8::Value> ReadCallback(const v8::Arguments& arguments) 244 v8::Handle<v8::Value> ReadCallback(const v8::Arguments& arguments)
245 { 245 {
246 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments); 246 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments);
247 AdblockPlus::JsConstValueList converted = jsEngine->ConvertArguments(argumen ts); 247 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments);
248 248
249 v8::Isolate* isolate = arguments.GetIsolate(); 249 v8::Isolate* isolate = arguments.GetIsolate();
250 if (converted.size() != 2) 250 if (converted.size() != 2)
251 return v8::ThrowException(Utils::ToV8String(isolate, 251 return v8::ThrowException(Utils::ToV8String(isolate,
252 "_fileSystem.read requires 2 parameters")); 252 "_fileSystem.read requires 2 parameters"));
253 if (!converted[1]->IsFunction()) 253 if (!converted[1].IsFunction())
254 return v8::ThrowException(Utils::ToV8String(isolate, 254 return v8::ThrowException(Utils::ToV8String(isolate,
255 "Second argument to _fileSystem.read must be a function")); 255 "Second argument to _fileSystem.read must be a function"));
256 ReadThread* const readThread = new ReadThread(jsEngine, converted[1], 256 ReadThread* const readThread = new ReadThread(jsEngine, converted[1],
257 converted[0]->AsString()); 257 converted[0].AsString());
258 readThread->Start(); 258 readThread->Start();
259 return v8::Undefined(); 259 return v8::Undefined();
260 } 260 }
261 261
262 v8::Handle<v8::Value> WriteCallback(const v8::Arguments& arguments) 262 v8::Handle<v8::Value> WriteCallback(const v8::Arguments& arguments)
263 { 263 {
264 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments); 264 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments);
265 AdblockPlus::JsConstValueList converted = jsEngine->ConvertArguments(argumen ts); 265 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments);
266 266
267 v8::Isolate* isolate = arguments.GetIsolate(); 267 v8::Isolate* isolate = arguments.GetIsolate();
268 if (converted.size() != 3) 268 if (converted.size() != 3)
269 return v8::ThrowException(Utils::ToV8String(isolate, 269 return v8::ThrowException(Utils::ToV8String(isolate,
270 "_fileSystem.write requires 3 parameters")); 270 "_fileSystem.write requires 3 parameters"));
271 if (!converted[2]->IsFunction()) 271 if (!converted[2].IsFunction())
272 return v8::ThrowException(Utils::ToV8String(isolate, 272 return v8::ThrowException(Utils::ToV8String(isolate,
273 "Third argument to _fileSystem.write must be a function")); 273 "Third argument to _fileSystem.write must be a function"));
274 WriteThread* const writeThread = new WriteThread(jsEngine, converted[2], 274 WriteThread* const writeThread = new WriteThread(jsEngine, converted[2],
275 converted[0]->AsString(), converted[1]->AsString()); 275 converted[0].AsString(), converted[1].AsString());
276 writeThread->Start(); 276 writeThread->Start();
277 return v8::Undefined(); 277 return v8::Undefined();
278 } 278 }
279 279
280 v8::Handle<v8::Value> MoveCallback(const v8::Arguments& arguments) 280 v8::Handle<v8::Value> MoveCallback(const v8::Arguments& arguments)
281 { 281 {
282 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments); 282 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments);
283 AdblockPlus::JsConstValueList converted = jsEngine->ConvertArguments(argumen ts); 283 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments);
284 284
285 v8::Isolate* isolate = arguments.GetIsolate(); 285 v8::Isolate* isolate = arguments.GetIsolate();
286 if (converted.size() != 3) 286 if (converted.size() != 3)
287 return v8::ThrowException(Utils::ToV8String(isolate, 287 return v8::ThrowException(Utils::ToV8String(isolate,
288 "_fileSystem.move requires 3 parameters")); 288 "_fileSystem.move requires 3 parameters"));
289 if (!converted[2]->IsFunction()) 289 if (!converted[2].IsFunction())
290 return v8::ThrowException(Utils::ToV8String(isolate, 290 return v8::ThrowException(Utils::ToV8String(isolate,
291 "Third argument to _fileSystem.move must be a function")); 291 "Third argument to _fileSystem.move must be a function"));
292 MoveThread* const moveThread = new MoveThread(jsEngine, converted[2], 292 MoveThread* const moveThread = new MoveThread(jsEngine, converted[2],
293 converted[0]->AsString(), converted[1]->AsString()); 293 converted[0].AsString(), converted[1].AsString());
294 moveThread->Start(); 294 moveThread->Start();
295 return v8::Undefined(); 295 return v8::Undefined();
296 } 296 }
297 297
298 v8::Handle<v8::Value> RemoveCallback(const v8::Arguments& arguments) 298 v8::Handle<v8::Value> RemoveCallback(const v8::Arguments& arguments)
299 { 299 {
300 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments); 300 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments);
301 AdblockPlus::JsConstValueList converted = jsEngine->ConvertArguments(argumen ts); 301 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments);
302 302
303 v8::Isolate* isolate = arguments.GetIsolate(); 303 v8::Isolate* isolate = arguments.GetIsolate();
304 if (converted.size() != 2) 304 if (converted.size() != 2)
305 return v8::ThrowException(Utils::ToV8String(isolate, 305 return v8::ThrowException(Utils::ToV8String(isolate,
306 "_fileSystem.remove requires 2 parameters")); 306 "_fileSystem.remove requires 2 parameters"));
307 if (!converted[1]->IsFunction()) 307 if (!converted[1].IsFunction())
308 return v8::ThrowException(Utils::ToV8String(isolate, 308 return v8::ThrowException(Utils::ToV8String(isolate,
309 "Second argument to _fileSystem.remove must be a function")); 309 "Second argument to _fileSystem.remove must be a function"));
310 RemoveThread* const removeThread = new RemoveThread(jsEngine, converted[1], 310 RemoveThread* const removeThread = new RemoveThread(jsEngine, converted[1],
311 converted[0]->AsString()); 311 converted[0].AsString());
312 removeThread->Start(); 312 removeThread->Start();
313 return v8::Undefined(); 313 return v8::Undefined();
314 } 314 }
315 315
316 v8::Handle<v8::Value> StatCallback(const v8::Arguments& arguments) 316 v8::Handle<v8::Value> StatCallback(const v8::Arguments& arguments)
317 { 317 {
318 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments); 318 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments);
319 AdblockPlus::JsConstValueList converted = jsEngine->ConvertArguments(argumen ts); 319 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments);
320 320
321 v8::Isolate* isolate = arguments.GetIsolate(); 321 v8::Isolate* isolate = arguments.GetIsolate();
322 if (converted.size() != 2) 322 if (converted.size() != 2)
323 return v8::ThrowException(Utils::ToV8String(isolate, 323 return v8::ThrowException(Utils::ToV8String(isolate,
324 "_fileSystem.stat requires 2 parameters")); 324 "_fileSystem.stat requires 2 parameters"));
325 if (!converted[1]->IsFunction()) 325 if (!converted[1].IsFunction())
326 return v8::ThrowException(Utils::ToV8String(isolate, 326 return v8::ThrowException(Utils::ToV8String(isolate,
327 "Second argument to _fileSystem.stat must be a function")); 327 "Second argument to _fileSystem.stat must be a function"));
328 StatThread* const statThread = new StatThread(jsEngine, converted[1], 328 StatThread* const statThread = new StatThread(jsEngine, converted[1],
329 converted[0]->AsString()); 329 converted[0].AsString());
330 statThread->Start(); 330 statThread->Start();
331 return v8::Undefined(); 331 return v8::Undefined();
332 } 332 }
333 333
334 v8::Handle<v8::Value> ResolveCallback(const v8::Arguments& arguments) 334 v8::Handle<v8::Value> ResolveCallback(const v8::Arguments& arguments)
335 { 335 {
336 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments); 336 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg uments);
337 AdblockPlus::JsConstValueList converted = jsEngine->ConvertArguments(argumen ts); 337 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments);
338 338
339 v8::Isolate* isolate = arguments.GetIsolate(); 339 v8::Isolate* isolate = arguments.GetIsolate();
340 if (converted.size() != 1) 340 if (converted.size() != 1)
341 return v8::ThrowException(Utils::ToV8String(isolate, 341 return v8::ThrowException(Utils::ToV8String(isolate,
342 "_fileSystem.resolve requires 1 parameter")); 342 "_fileSystem.resolve requires 1 parameter"));
343 343
344 std::string resolved = jsEngine->GetFileSystem()->Resolve(converted[0]->AsSt ring()); 344 std::string resolved = jsEngine->GetFileSystem()->Resolve(converted[0].AsStr ing());
345 345
346 return Utils::ToV8String(isolate, resolved); 346 return Utils::ToV8String(isolate, resolved);
347 } 347 }
348 348
349 } 349 }
350 350
351 351
352 JsValue& FileSystemJsObject::Setup(JsEngine& jsEngine, JsValue& obj) 352 JsValue& FileSystemJsObject::Setup(JsEngine& jsEngine, JsValue& obj)
353 { 353 {
354 obj.SetProperty("read", jsEngine.NewCallback(::ReadCallback)); 354 obj.SetProperty("read", jsEngine.NewCallback(::ReadCallback));
355 obj.SetProperty("write", jsEngine.NewCallback(::WriteCallback)); 355 obj.SetProperty("write", jsEngine.NewCallback(::WriteCallback));
356 obj.SetProperty("move", jsEngine.NewCallback(::MoveCallback)); 356 obj.SetProperty("move", jsEngine.NewCallback(::MoveCallback));
357 obj.SetProperty("remove", jsEngine.NewCallback(::RemoveCallback)); 357 obj.SetProperty("remove", jsEngine.NewCallback(::RemoveCallback));
358 obj.SetProperty("stat", jsEngine.NewCallback(::StatCallback)); 358 obj.SetProperty("stat", jsEngine.NewCallback(::StatCallback));
359 obj.SetProperty("resolve", jsEngine.NewCallback(::ResolveCallback)); 359 obj.SetProperty("resolve", jsEngine.NewCallback(::ResolveCallback));
360 return obj; 360 return obj;
361 } 361 }
OLDNEW
« no previous file with comments | « src/ConsoleJsObject.cpp ('k') | src/FilterEngine.cpp » ('j') | src/JsValue.cpp » ('J')

Powered by Google App Engine
This is Rietveld