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-present eyeo GmbH | 3 * Copyright (C) 2006-present 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 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 } | 65 } |
66 v8::Platform* platform; | 66 v8::Platform* platform; |
67 public: | 67 public: |
68 static void Init() | 68 static void Init() |
69 { | 69 { |
70 // it's threadsafe since C++11 and it will be instantiated only once and | 70 // it's threadsafe since C++11 and it will be instantiated only once and |
71 // destroyed at the application exit | 71 // destroyed at the application exit |
72 static V8Initializer initializer; | 72 static V8Initializer initializer; |
73 } | 73 } |
74 }; | 74 }; |
| 75 |
| 76 /** |
| 77 * Scope based isolate manager. Creates a new isolate instance on |
| 78 * constructing and disposes it on destructing. In addition it initilizes V8. |
| 79 */ |
| 80 class ScopedV8Isolate : public AdblockPlus::IV8IsolateProvider |
| 81 { |
| 82 public: |
| 83 ScopedV8Isolate() |
| 84 { |
| 85 V8Initializer::Init(); |
| 86 v8::Isolate::CreateParams isolateParams; |
| 87 isolateParams.array_buffer_allocator = v8::ArrayBuffer::Allocator::NewDefa
ultAllocator(); |
| 88 isolate = v8::Isolate::New(isolateParams); |
| 89 } |
| 90 |
| 91 ~ScopedV8Isolate() |
| 92 { |
| 93 isolate->Dispose(); |
| 94 isolate = nullptr; |
| 95 } |
| 96 |
| 97 v8::Isolate* Get() override |
| 98 { |
| 99 return isolate; |
| 100 } |
| 101 private: |
| 102 ScopedV8Isolate(const ScopedV8Isolate&); |
| 103 ScopedV8Isolate& operator=(const ScopedV8Isolate&); |
| 104 |
| 105 v8::Isolate* isolate; |
| 106 }; |
75 } | 107 } |
76 | 108 |
77 using namespace AdblockPlus; | 109 using namespace AdblockPlus; |
78 | 110 |
79 AdblockPlus::ScopedV8Isolate::ScopedV8Isolate() | |
80 { | |
81 V8Initializer::Init(); | |
82 v8::Isolate::CreateParams isolateParams; | |
83 isolateParams.array_buffer_allocator = v8::ArrayBuffer::Allocator::NewDefaultA
llocator(); | |
84 isolate = v8::Isolate::New(isolateParams); | |
85 } | |
86 | |
87 AdblockPlus::ScopedV8Isolate::~ScopedV8Isolate() | |
88 { | |
89 isolate->Dispose(); | |
90 isolate = nullptr; | |
91 } | |
92 | |
93 JsEngine::JsWeakValuesList::~JsWeakValuesList() | 111 JsEngine::JsWeakValuesList::~JsWeakValuesList() |
94 { | 112 { |
95 } | 113 } |
96 | 114 |
97 void JsEngine::NotifyLowMemory() | 115 void JsEngine::NotifyLowMemory() |
98 { | 116 { |
99 const JsContext context(*this); | 117 const JsContext context(*this); |
100 GetIsolate()->MemoryPressureNotification(v8::MemoryPressureLevel::kCritical); | 118 GetIsolate()->MemoryPressureNotification(v8::MemoryPressureLevel::kCritical); |
101 } | 119 } |
102 | 120 |
(...skipping 20 matching lines...) Expand all Loading... |
123 void JsEngine::CallTimerTask(const JsWeakValuesID& timerParamsID) | 141 void JsEngine::CallTimerTask(const JsWeakValuesID& timerParamsID) |
124 { | 142 { |
125 auto timerParams = TakeJsValues(timerParamsID); | 143 auto timerParams = TakeJsValues(timerParamsID); |
126 JsValue callback = std::move(timerParams[0]); | 144 JsValue callback = std::move(timerParams[0]); |
127 | 145 |
128 timerParams.erase(timerParams.begin()); // remove callback placeholder | 146 timerParams.erase(timerParams.begin()); // remove callback placeholder |
129 timerParams.erase(timerParams.begin()); // remove timeout param | 147 timerParams.erase(timerParams.begin()); // remove timeout param |
130 callback.Call(timerParams); | 148 callback.Call(timerParams); |
131 } | 149 } |
132 | 150 |
133 AdblockPlus::JsEngine::JsEngine(Platform& platform) | 151 AdblockPlus::JsEngine::JsEngine(Platform& platform, std::unique_ptr<IV8IsolatePr
ovider> isolate) |
134 : platform(platform) | 152 : platform(platform) |
| 153 , isolate(std::move(isolate)) |
135 { | 154 { |
136 } | 155 } |
137 | 156 |
138 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::New(const AppInfo& appInfo, | 157 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::New(const AppInfo& appInfo, |
139 Platform& platform) | 158 Platform& platform, std::unique_ptr<IV8IsolateProvider> isolate) |
140 { | 159 { |
141 JsEnginePtr result(new JsEngine(platform)); | 160 if (!isolate) |
| 161 { |
| 162 isolate.reset(new ScopedV8Isolate()); |
| 163 } |
| 164 JsEnginePtr result(new JsEngine(platform, std::move(isolate))); |
142 | 165 |
143 const v8::Locker locker(result->GetIsolate()); | 166 const v8::Locker locker(result->GetIsolate()); |
144 const v8::Isolate::Scope isolateScope(result->GetIsolate()); | 167 const v8::Isolate::Scope isolateScope(result->GetIsolate()); |
145 const v8::HandleScope handleScope(result->GetIsolate()); | 168 const v8::HandleScope handleScope(result->GetIsolate()); |
146 | 169 |
147 result->context.reset(new v8::Global<v8::Context>(result->GetIsolate(), | 170 result->context.reset(new v8::Global<v8::Context>(result->GetIsolate(), |
148 v8::Context::New(result->GetIsolate()))); | 171 v8::Context::New(result->GetIsolate()))); |
149 auto global = result->GetGlobalObject(); | 172 auto global = result->GetGlobalObject(); |
150 AdblockPlus::GlobalJsObject::Setup(*result, appInfo, global); | 173 AdblockPlus::GlobalJsObject::Setup(*result, appInfo, global); |
151 return result; | 174 return result; |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
300 list.push_back(JsValue(shared_from_this(), arguments[i])); | 323 list.push_back(JsValue(shared_from_this(), arguments[i])); |
301 return list; | 324 return list; |
302 } | 325 } |
303 | 326 |
304 void AdblockPlus::JsEngine::SetGlobalProperty(const std::string& name, | 327 void AdblockPlus::JsEngine::SetGlobalProperty(const std::string& name, |
305 const AdblockPlus::JsValue& value) | 328 const AdblockPlus::JsValue& value) |
306 { | 329 { |
307 auto global = GetGlobalObject(); | 330 auto global = GetGlobalObject(); |
308 global.SetProperty(name, value); | 331 global.SetProperty(name, value); |
309 } | 332 } |
OLD | NEW |