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

Side by Side Diff: src/JsEngine.cpp

Issue 29500602: Issue 5450 - introduce the Platform class (Closed) Base URL: https://github.com/adblockplus/libadblockplus.git
Patch Set: Created July 28, 2017, 12:40 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
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.h> 18 #include <AdblockPlus.h>
19 #include "GlobalJsObject.h" 19 #include "GlobalJsObject.h"
20 #include "JsContext.h" 20 #include "JsContext.h"
21 #include "JsError.h" 21 #include "JsError.h"
22 #include "Utils.h" 22 #include "Utils.h"
23 #include "DefaultTimer.h"
24 #include "DefaultWebRequest.h"
25 #include "DefaultFileSystem.h"
26 #include <libplatform/libplatform.h> 23 #include <libplatform/libplatform.h>
24 #include <AdblockPlus/Platform.h>
27 25
28 namespace 26 namespace
29 { 27 {
30 v8::Handle<v8::Script> CompileScript(v8::Isolate* isolate, 28 v8::Handle<v8::Script> CompileScript(v8::Isolate* isolate,
31 const std::string& source, const std::string& filename) 29 const std::string& source, const std::string& filename)
32 { 30 {
33 using AdblockPlus::Utils::ToV8String; 31 using AdblockPlus::Utils::ToV8String;
34 const v8::Handle<v8::String> v8Source = ToV8String(isolate, source); 32 const v8::Handle<v8::String> v8Source = ToV8String(isolate, source);
35 if (filename.length()) 33 if (filename.length())
36 { 34 {
(...skipping 27 matching lines...) Expand all
64 } 62 }
65 v8::Platform* platform; 63 v8::Platform* platform;
66 public: 64 public:
67 static void Init() 65 static void Init()
68 { 66 {
69 // it's threadsafe since C++11 and it will be instantiated only once and 67 // it's threadsafe since C++11 and it will be instantiated only once and
70 // destroyed at the application exit 68 // destroyed at the application exit
71 static V8Initializer initializer; 69 static V8Initializer initializer;
72 } 70 }
73 }; 71 };
74
75 void DummyScheduler(const AdblockPlus::SchedulerTask& task)
76 {
77 std::thread(task).detach();
78 }
79 } 72 }
80 73
81 using namespace AdblockPlus; 74 using namespace AdblockPlus;
82 75
83 TimerPtr AdblockPlus::CreateDefaultTimer()
84 {
85 return TimerPtr(new DefaultTimer());
86 }
87
88 FileSystemPtr AdblockPlus::CreateDefaultFileSystem(const Scheduler& scheduler)
89 {
90 return FileSystemPtr(new DefaultFileSystem(scheduler, std::unique_ptr<DefaultF ileSystemSync>(new DefaultFileSystemSync())));
91 }
92
93 WebRequestPtr AdblockPlus::CreateDefaultWebRequest(const Scheduler& scheduler)
94 {
95 return WebRequestPtr(new DefaultWebRequest(scheduler, std::unique_ptr<DefaultW ebRequestSync>(new DefaultWebRequestSync())));
96 }
97
98 LogSystemPtr AdblockPlus::CreateDefaultLogSystem()
99 {
100 return LogSystemPtr(new DefaultLogSystem());
101 }
102
103 AdblockPlus::ScopedV8Isolate::ScopedV8Isolate() 76 AdblockPlus::ScopedV8Isolate::ScopedV8Isolate()
104 { 77 {
105 V8Initializer::Init(); 78 V8Initializer::Init();
106 v8::Isolate::CreateParams isolateParams; 79 v8::Isolate::CreateParams isolateParams;
107 isolateParams.array_buffer_allocator = v8::ArrayBuffer::Allocator::NewDefaultA llocator(); 80 isolateParams.array_buffer_allocator = v8::ArrayBuffer::Allocator::NewDefaultA llocator();
108 isolate = v8::Isolate::New(isolateParams); 81 isolate = v8::Isolate::New(isolateParams);
109 } 82 }
110 83
111 AdblockPlus::ScopedV8Isolate::~ScopedV8Isolate() 84 AdblockPlus::ScopedV8Isolate::~ScopedV8Isolate()
112 { 85 {
(...skipping 17 matching lines...) Expand all
130 if (arguments.Length() < 2) 103 if (arguments.Length() < 2)
131 throw std::runtime_error("setTimeout requires at least 2 parameters"); 104 throw std::runtime_error("setTimeout requires at least 2 parameters");
132 105
133 if (!arguments[0]->IsFunction()) 106 if (!arguments[0]->IsFunction())
134 throw std::runtime_error("First argument to setTimeout must be a function"); 107 throw std::runtime_error("First argument to setTimeout must be a function");
135 108
136 auto jsValueArguments = jsEngine->ConvertArguments(arguments); 109 auto jsValueArguments = jsEngine->ConvertArguments(arguments);
137 auto timerParamsID = jsEngine->StoreJsValues(jsValueArguments); 110 auto timerParamsID = jsEngine->StoreJsValues(jsValueArguments);
138 111
139 std::weak_ptr<JsEngine> weakJsEngine = jsEngine; 112 std::weak_ptr<JsEngine> weakJsEngine = jsEngine;
140 jsEngine->timer->SetTimer(std::chrono::milliseconds(arguments[1]->IntegerValue ()), [weakJsEngine, timerParamsID] 113 jsEngine->platform.GetTimer().SetTimer(std::chrono::milliseconds(arguments[1]- >IntegerValue()), [weakJsEngine, timerParamsID]
141 { 114 {
142 if (auto jsEngine = weakJsEngine.lock()) 115 if (auto jsEngine = weakJsEngine.lock())
143 jsEngine->CallTimerTask(timerParamsID); 116 jsEngine->CallTimerTask(timerParamsID);
144 }); 117 });
145 } 118 }
146 119
147 void JsEngine::CallTimerTask(const JsWeakValuesID& timerParamsID) 120 void JsEngine::CallTimerTask(const JsWeakValuesID& timerParamsID)
148 { 121 {
149 auto timerParams = TakeJsValues(timerParamsID); 122 auto timerParams = TakeJsValues(timerParamsID);
150 JsValue callback = std::move(timerParams[0]); 123 JsValue callback = std::move(timerParams[0]);
151 124
152 timerParams.erase(timerParams.begin()); // remove callback placeholder 125 timerParams.erase(timerParams.begin()); // remove callback placeholder
153 timerParams.erase(timerParams.begin()); // remove timeout param 126 timerParams.erase(timerParams.begin()); // remove timeout param
154 callback.Call(timerParams); 127 callback.Call(timerParams);
155 } 128 }
156 129
157 AdblockPlus::JsEngine::JsEngine(TimerPtr timer, FileSystemPtr fileSystem, 130 AdblockPlus::JsEngine::JsEngine(Platform& platform)
158 WebRequestPtr webRequest, LogSystemPtr logSystem) 131 : platform(platform)
159 : fileSystem(std::move(fileSystem))
160 , timer(std::move(timer))
161 , webRequest(std::move(webRequest))
162 , logSystem(std::move(logSystem))
163 { 132 {
164 } 133 }
165 134
166 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::New(const AppInfo& appInfo, 135 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::New(const AppInfo& appInfo,
167 TimerPtr timer, FileSystemPtr fileSystem, WebRequestPtr webRequest, LogSystemP tr logSystem) 136 Platform& platform)
168 { 137 {
169 JsEnginePtr result(new JsEngine(timer ? std::move(timer) : CreateDefaultTimer( ), 138 JsEnginePtr result(new JsEngine(platform));
170 fileSystem ? std::move(fileSystem) : CreateDefaultFileSystem(::DummySchedule r),
171 webRequest ? std::move(webRequest) : CreateDefaultWebRequest(::DummySchedule r),
172 logSystem ? std::move(logSystem) : CreateDefaultLogSystem()));
173 139
174 const v8::Locker locker(result->GetIsolate()); 140 const v8::Locker locker(result->GetIsolate());
175 const v8::Isolate::Scope isolateScope(result->GetIsolate()); 141 const v8::Isolate::Scope isolateScope(result->GetIsolate());
176 const v8::HandleScope handleScope(result->GetIsolate()); 142 const v8::HandleScope handleScope(result->GetIsolate());
177 143
178 result->context.reset(new v8::Global<v8::Context>(result->GetIsolate(), 144 result->context.reset(new v8::Global<v8::Context>(result->GetIsolate(),
179 v8::Context::New(result->GetIsolate()))); 145 v8::Context::New(result->GetIsolate())));
180 auto global = result->GetGlobalObject(); 146 auto global = result->GetGlobalObject();
181 AdblockPlus::GlobalJsObject::Setup(*result, appInfo, global); 147 AdblockPlus::GlobalJsObject::Setup(*result, appInfo, global);
182 return result; 148 return result;
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
325 291
326 AdblockPlus::JsValueList AdblockPlus::JsEngine::ConvertArguments(const v8::Funct ionCallbackInfo<v8::Value>& arguments) 292 AdblockPlus::JsValueList AdblockPlus::JsEngine::ConvertArguments(const v8::Funct ionCallbackInfo<v8::Value>& arguments)
327 { 293 {
328 const JsContext context(*this); 294 const JsContext context(*this);
329 JsValueList list; 295 JsValueList list;
330 for (int i = 0; i < arguments.Length(); i++) 296 for (int i = 0; i < arguments.Length(); i++)
331 list.push_back(JsValue(shared_from_this(), arguments[i])); 297 list.push_back(JsValue(shared_from_this(), arguments[i]));
332 return list; 298 return list;
333 } 299 }
334 300
335 AdblockPlus::FileSystemPtr AdblockPlus::JsEngine::GetAsyncFileSystem() const
336 {
337 return fileSystem;
338 }
339
340 AdblockPlus::LogSystem& AdblockPlus::JsEngine::GetLogSystem()
341 {
342 return *logSystem;
343 }
344
345 void AdblockPlus::JsEngine::SetGlobalProperty(const std::string& name, 301 void AdblockPlus::JsEngine::SetGlobalProperty(const std::string& name,
346 const AdblockPlus::JsValue& value) 302 const AdblockPlus::JsValue& value)
347 { 303 {
348 auto global = GetGlobalObject(); 304 auto global = GetGlobalObject();
349 global.SetProperty(name, value); 305 global.SetProperty(name, value);
350 } 306 }
OLDNEW

Powered by Google App Engine
This is Rietveld