OLD | NEW |
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 |
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/IFileSystem.h> | 18 #include <AdblockPlus/IFileSystem.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 "Utils.h" | 26 #include "Utils.h" |
| 27 #include <AdblockPlus/Platform.h> |
27 | 28 |
28 using namespace AdblockPlus; | 29 using namespace AdblockPlus; |
29 using AdblockPlus::Utils::ThrowExceptionInJS; | 30 using AdblockPlus::Utils::ThrowExceptionInJS; |
30 | 31 |
31 namespace | 32 namespace |
32 { | 33 { |
33 void ReadCallback(const v8::FunctionCallbackInfo<v8::Value>& arguments) | 34 void ReadCallback(const v8::FunctionCallbackInfo<v8::Value>& arguments) |
34 { | 35 { |
35 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg
uments); | 36 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg
uments); |
36 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments); | 37 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments); |
37 | 38 |
38 v8::Isolate* isolate = arguments.GetIsolate(); | 39 v8::Isolate* isolate = arguments.GetIsolate(); |
39 if (converted.size() != 2) | 40 if (converted.size() != 2) |
40 return ThrowExceptionInJS(isolate, "_fileSystem.read requires 2 parameters
"); | 41 return ThrowExceptionInJS(isolate, "_fileSystem.read requires 2 parameters
"); |
41 if (!converted[1].IsFunction()) | 42 if (!converted[1].IsFunction()) |
42 return ThrowExceptionInJS(isolate, "Second argument to _fileSystem.read mu
st be a function"); | 43 return ThrowExceptionInJS(isolate, "Second argument to _fileSystem.read mu
st be a function"); |
43 | 44 |
44 JsValueList values; | 45 JsValueList values; |
45 values.push_back(converted[1]); | 46 values.push_back(converted[1]); |
46 auto weakCallback = jsEngine->StoreJsValues(values); | 47 auto weakCallback = jsEngine->StoreJsValues(values); |
47 std::weak_ptr<JsEngine> weakJsEngine = jsEngine; | 48 std::weak_ptr<JsEngine> weakJsEngine = jsEngine; |
48 jsEngine->GetAsyncFileSystem()->Read(converted[0].AsString(), | 49 jsEngine->GetPlatform().GetFileSystem().Read(converted[0].AsString(), |
49 [weakJsEngine, weakCallback] | 50 [weakJsEngine, weakCallback] |
50 (IFileSystem::IOBuffer&& content, const std::string& error) | 51 (IFileSystem::IOBuffer&& content, const std::string& error) |
51 { | 52 { |
52 auto jsEngine = weakJsEngine.lock(); | 53 auto jsEngine = weakJsEngine.lock(); |
53 if (!jsEngine) | 54 if (!jsEngine) |
54 return; | 55 return; |
55 | 56 |
56 const JsContext context(*jsEngine); | 57 const JsContext context(*jsEngine); |
57 auto result = jsEngine->NewObject(); | 58 auto result = jsEngine->NewObject(); |
58 result.SetStringBufferProperty("content", std::move(content)); | 59 result.SetStringBufferProperty("content", std::move(content)); |
(...skipping 12 matching lines...) Expand all Loading... |
71 if (converted.size() != 3) | 72 if (converted.size() != 3) |
72 return ThrowExceptionInJS(isolate, "_fileSystem.write requires 3 parameter
s"); | 73 return ThrowExceptionInJS(isolate, "_fileSystem.write requires 3 parameter
s"); |
73 if (!converted[2].IsFunction()) | 74 if (!converted[2].IsFunction()) |
74 return ThrowExceptionInJS(isolate, "Third argument to _fileSystem.write mu
st be a function"); | 75 return ThrowExceptionInJS(isolate, "Third argument to _fileSystem.write mu
st be a function"); |
75 | 76 |
76 JsValueList values; | 77 JsValueList values; |
77 values.push_back(converted[2]); | 78 values.push_back(converted[2]); |
78 auto weakCallback = jsEngine->StoreJsValues(values); | 79 auto weakCallback = jsEngine->StoreJsValues(values); |
79 std::weak_ptr<JsEngine> weakJsEngine = jsEngine; | 80 std::weak_ptr<JsEngine> weakJsEngine = jsEngine; |
80 auto content = converted[1].AsStringBuffer(); | 81 auto content = converted[1].AsStringBuffer(); |
81 jsEngine->GetAsyncFileSystem()->Write(converted[0].AsString(), | 82 jsEngine->GetPlatform().GetFileSystem().Write(converted[0].AsString(), |
82 content, | 83 content, |
83 [weakJsEngine, weakCallback](const std::string& error) | 84 [weakJsEngine, weakCallback](const std::string& error) |
84 { | 85 { |
85 auto jsEngine = weakJsEngine.lock(); | 86 auto jsEngine = weakJsEngine.lock(); |
86 if (!jsEngine) | 87 if (!jsEngine) |
87 return; | 88 return; |
88 | 89 |
89 const JsContext context(*jsEngine); | 90 const JsContext context(*jsEngine); |
90 JsValueList params; | 91 JsValueList params; |
91 if (!error.empty()) | 92 if (!error.empty()) |
(...skipping 10 matching lines...) Expand all Loading... |
102 v8::Isolate* isolate = arguments.GetIsolate(); | 103 v8::Isolate* isolate = arguments.GetIsolate(); |
103 if (converted.size() != 3) | 104 if (converted.size() != 3) |
104 return ThrowExceptionInJS(isolate, "_fileSystem.move requires 3 parameters
"); | 105 return ThrowExceptionInJS(isolate, "_fileSystem.move requires 3 parameters
"); |
105 if (!converted[2].IsFunction()) | 106 if (!converted[2].IsFunction()) |
106 return ThrowExceptionInJS(isolate, "Third argument to _fileSystem.move mus
t be a function"); | 107 return ThrowExceptionInJS(isolate, "Third argument to _fileSystem.move mus
t be a function"); |
107 | 108 |
108 JsValueList values; | 109 JsValueList values; |
109 values.push_back(converted[2]); | 110 values.push_back(converted[2]); |
110 auto weakCallback = jsEngine->StoreJsValues(values); | 111 auto weakCallback = jsEngine->StoreJsValues(values); |
111 std::weak_ptr<JsEngine> weakJsEngine = jsEngine; | 112 std::weak_ptr<JsEngine> weakJsEngine = jsEngine; |
112 jsEngine->GetAsyncFileSystem()->Move(converted[0].AsString(), | 113 jsEngine->GetPlatform().GetFileSystem().Move(converted[0].AsString(), |
113 converted[1].AsString(), | 114 converted[1].AsString(), |
114 [weakJsEngine, weakCallback](const std::string& error) | 115 [weakJsEngine, weakCallback](const std::string& error) |
115 { | 116 { |
116 auto jsEngine = weakJsEngine.lock(); | 117 auto jsEngine = weakJsEngine.lock(); |
117 if (!jsEngine) | 118 if (!jsEngine) |
118 return; | 119 return; |
119 | 120 |
120 const JsContext context(*jsEngine); | 121 const JsContext context(*jsEngine); |
121 JsValueList params; | 122 JsValueList params; |
122 if (!error.empty()) | 123 if (!error.empty()) |
(...skipping 10 matching lines...) Expand all Loading... |
133 v8::Isolate* isolate = arguments.GetIsolate(); | 134 v8::Isolate* isolate = arguments.GetIsolate(); |
134 if (converted.size() != 2) | 135 if (converted.size() != 2) |
135 return ThrowExceptionInJS(isolate, "_fileSystem.remove requires 2 paramete
rs"); | 136 return ThrowExceptionInJS(isolate, "_fileSystem.remove requires 2 paramete
rs"); |
136 if (!converted[1].IsFunction()) | 137 if (!converted[1].IsFunction()) |
137 return ThrowExceptionInJS(isolate, "Second argument to _fileSystem.remove
must be a function"); | 138 return ThrowExceptionInJS(isolate, "Second argument to _fileSystem.remove
must be a function"); |
138 | 139 |
139 JsValueList values; | 140 JsValueList values; |
140 values.push_back(converted[1]); | 141 values.push_back(converted[1]); |
141 auto weakCallback = jsEngine->StoreJsValues(values); | 142 auto weakCallback = jsEngine->StoreJsValues(values); |
142 std::weak_ptr<JsEngine> weakJsEngine = jsEngine; | 143 std::weak_ptr<JsEngine> weakJsEngine = jsEngine; |
143 jsEngine->GetAsyncFileSystem()->Remove(converted[0].AsString(), | 144 jsEngine->GetPlatform().GetFileSystem().Remove(converted[0].AsString(), |
144 [weakJsEngine, weakCallback](const std::string& error) | 145 [weakJsEngine, weakCallback](const std::string& error) |
145 { | 146 { |
146 auto jsEngine = weakJsEngine.lock(); | 147 auto jsEngine = weakJsEngine.lock(); |
147 if (!jsEngine) | 148 if (!jsEngine) |
148 return; | 149 return; |
149 | 150 |
150 const JsContext context(*jsEngine); | 151 const JsContext context(*jsEngine); |
151 JsValueList params; | 152 JsValueList params; |
152 if (!error.empty()) | 153 if (!error.empty()) |
153 params.push_back(jsEngine->NewValue(error)); | 154 params.push_back(jsEngine->NewValue(error)); |
154 jsEngine->TakeJsValues(weakCallback)[0].Call(params); | 155 jsEngine->TakeJsValues(weakCallback)[0].Call(params); |
155 }); | 156 }); |
156 } | 157 } |
157 | 158 |
158 void StatCallback(const v8::FunctionCallbackInfo<v8::Value>& arguments) | 159 void StatCallback(const v8::FunctionCallbackInfo<v8::Value>& arguments) |
159 { | 160 { |
160 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg
uments); | 161 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg
uments); |
161 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments); | 162 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments); |
162 | 163 |
163 v8::Isolate* isolate = arguments.GetIsolate(); | 164 v8::Isolate* isolate = arguments.GetIsolate(); |
164 if (converted.size() != 2) | 165 if (converted.size() != 2) |
165 return ThrowExceptionInJS(isolate, "_fileSystem.stat requires 2 parameters
"); | 166 return ThrowExceptionInJS(isolate, "_fileSystem.stat requires 2 parameters
"); |
166 if (!converted[1].IsFunction()) | 167 if (!converted[1].IsFunction()) |
167 return ThrowExceptionInJS(isolate, "Second argument to _fileSystem.stat mu
st be a function"); | 168 return ThrowExceptionInJS(isolate, "Second argument to _fileSystem.stat mu
st be a function"); |
168 | 169 |
169 JsValueList values; | 170 JsValueList values; |
170 values.push_back(converted[1]); | 171 values.push_back(converted[1]); |
171 auto weakCallback = jsEngine->StoreJsValues(values); | 172 auto weakCallback = jsEngine->StoreJsValues(values); |
172 std::weak_ptr<JsEngine> weakJsEngine = jsEngine; | 173 std::weak_ptr<JsEngine> weakJsEngine = jsEngine; |
173 jsEngine->GetAsyncFileSystem()->Stat(converted[0].AsString(), | 174 jsEngine->GetPlatform().GetFileSystem().Stat(converted[0].AsString(), |
174 [weakJsEngine, weakCallback] | 175 [weakJsEngine, weakCallback] |
175 (const IFileSystem::StatResult& statResult, const std::string& error) | 176 (const IFileSystem::StatResult& statResult, const std::string& error) |
176 { | 177 { |
177 auto jsEngine = weakJsEngine.lock(); | 178 auto jsEngine = weakJsEngine.lock(); |
178 if (!jsEngine) | 179 if (!jsEngine) |
179 return; | 180 return; |
180 | 181 |
181 const JsContext context(*jsEngine); | 182 const JsContext context(*jsEngine); |
182 auto result = jsEngine->NewObject(); | 183 auto result = jsEngine->NewObject(); |
183 | 184 |
(...skipping 12 matching lines...) Expand all Loading... |
196 | 197 |
197 void ResolveCallback(const v8::FunctionCallbackInfo<v8::Value>& arguments) | 198 void ResolveCallback(const v8::FunctionCallbackInfo<v8::Value>& arguments) |
198 { | 199 { |
199 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg
uments); | 200 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::FromArguments(arg
uments); |
200 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments); | 201 AdblockPlus::JsValueList converted = jsEngine->ConvertArguments(arguments); |
201 | 202 |
202 v8::Isolate* isolate = arguments.GetIsolate(); | 203 v8::Isolate* isolate = arguments.GetIsolate(); |
203 if (converted.size() != 1) | 204 if (converted.size() != 1) |
204 return ThrowExceptionInJS(isolate, "_fileSystem.resolve requires 1 paramet
er"); | 205 return ThrowExceptionInJS(isolate, "_fileSystem.resolve requires 1 paramet
er"); |
205 | 206 |
206 std::string resolved = jsEngine->GetAsyncFileSystem()->Resolve(converted[0].
AsString()); | 207 std::string resolved = jsEngine->GetPlatform().GetFileSystem().Resolve(conve
rted[0].AsString()); |
207 arguments.GetReturnValue().Set(Utils::ToV8String(isolate, resolved)); | 208 arguments.GetReturnValue().Set(Utils::ToV8String(isolate, resolved)); |
208 } | 209 } |
209 } | 210 } |
210 | 211 |
211 | 212 |
212 JsValue& FileSystemJsObject::Setup(JsEngine& jsEngine, JsValue& obj) | 213 JsValue& FileSystemJsObject::Setup(JsEngine& jsEngine, JsValue& obj) |
213 { | 214 { |
214 obj.SetProperty("read", jsEngine.NewCallback(::ReadCallback)); | 215 obj.SetProperty("read", jsEngine.NewCallback(::ReadCallback)); |
215 obj.SetProperty("write", jsEngine.NewCallback(::WriteCallback)); | 216 obj.SetProperty("write", jsEngine.NewCallback(::WriteCallback)); |
216 obj.SetProperty("move", jsEngine.NewCallback(::MoveCallback)); | 217 obj.SetProperty("move", jsEngine.NewCallback(::MoveCallback)); |
217 obj.SetProperty("remove", jsEngine.NewCallback(::RemoveCallback)); | 218 obj.SetProperty("remove", jsEngine.NewCallback(::RemoveCallback)); |
218 obj.SetProperty("stat", jsEngine.NewCallback(::StatCallback)); | 219 obj.SetProperty("stat", jsEngine.NewCallback(::StatCallback)); |
219 obj.SetProperty("resolve", jsEngine.NewCallback(::ResolveCallback)); | 220 obj.SetProperty("resolve", jsEngine.NewCallback(::ResolveCallback)); |
220 return obj; | 221 return obj; |
221 } | 222 } |
OLD | NEW |