2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "public/web/WebKit.h"
34 #include "bindings/core/v8/ScriptStreamerThread.h"
35 #include "bindings/core/v8/V8Binding.h"
36 #include "bindings/core/v8/V8GCController.h"
37 #include "bindings/core/v8/V8Initializer.h"
38 #include "core/Init.h"
39 #include "core/animation/AnimationClock.h"
40 #include "core/dom/Microtask.h"
41 #include "core/frame/Settings.h"
42 #include "core/page/Page.h"
43 #include "core/workers/WorkerGlobalScopeProxy.h"
44 #include "gin/public/v8_platform.h"
45 #include "modules/InitModules.h"
46 #include "platform/LayoutTestSupport.h"
47 #include "platform/Logging.h"
48 #include "platform/RuntimeEnabledFeatures.h"
49 #include "platform/graphics/ImageDecodingStore.h"
50 #include "platform/heap/Heap.h"
51 #include "platform/heap/glue/MessageLoopInterruptor.h"
52 #include "platform/heap/glue/PendingGCRunner.h"
53 #include "public/platform/Platform.h"
54 #include "public/platform/WebPrerenderingSupport.h"
55 #include "public/platform/WebThread.h"
56 #include "web/IndexedDBClientImpl.h"
57 #include "wtf/Assertions.h"
58 #include "wtf/CryptographicallyRandomNumber.h"
59 #include "wtf/MainThread.h"
60 #include "wtf/Partitions.h"
62 #include "wtf/text/AtomicString.h"
63 #include "wtf/text/TextEncoding.h"
70 class EndOfTaskRunner
: public WebThread::TaskObserver
{
72 void willProcessTask() override
74 AnimationClock::notifyTaskStart();
76 void didProcessTask() override
78 Microtask::performCheckpoint(mainThreadIsolate());
79 V8GCController::reportDOMMemoryUsageToV8(mainThreadIsolate());
80 V8Initializer::reportRejectedPromisesOnMainThread();
84 class MainThreadTaskRunner
: public WebTaskRunner::Task
{
85 WTF_MAKE_NONCOPYABLE(MainThreadTaskRunner
);
87 MainThreadTaskRunner(WTF::MainThreadFunction
* function
, void* context
)
88 : m_function(function
)
89 , m_context(context
) { }
93 m_function(m_context
);
96 WTF::MainThreadFunction
* m_function
;
102 static WebThread::TaskObserver
* s_endOfTaskRunner
= 0;
103 static WebThread::TaskObserver
* s_pendingGCRunner
= 0;
105 // Make sure we are not re-initialized in the same address space.
106 // Doing so may cause hard to reproduce crashes.
107 static bool s_webKitInitialized
= false;
109 void initialize(Platform
* platform
)
111 initializeWithoutV8(platform
);
113 V8Initializer::initializeMainThreadIfNeeded();
115 OwnPtr
<V8IsolateInterruptor
> interruptor
= adoptPtr(new V8IsolateInterruptor(V8PerIsolateData::mainThreadIsolate()));
116 ThreadState::current()->addInterruptor(interruptor
.release());
117 ThreadState::current()->registerTraceDOMWrappers(V8PerIsolateData::mainThreadIsolate(), V8GCController::traceDOMWrappers
);
119 // currentThread is null if we are running on a thread without a message loop.
120 if (WebThread
* currentThread
= platform
->currentThread()) {
121 ASSERT(!s_endOfTaskRunner
);
122 s_endOfTaskRunner
= new EndOfTaskRunner
;
123 currentThread
->addTaskObserver(s_endOfTaskRunner
);
127 v8::Isolate
* mainThreadIsolate()
129 return V8PerIsolateData::mainThreadIsolate();
132 static double currentTimeFunction()
134 return Platform::current()->currentTime();
137 static double monotonicallyIncreasingTimeFunction()
139 return Platform::current()->monotonicallyIncreasingTime();
142 static double systemTraceTimeFunction()
144 return Platform::current()->systemTraceTime();
147 static void histogramEnumerationFunction(const char* name
, int sample
, int boundaryValue
)
149 Platform::current()->histogramEnumeration(name
, sample
, boundaryValue
);
152 static void cryptographicallyRandomValues(unsigned char* buffer
, size_t length
)
154 Platform::current()->cryptographicallyRandomValues(buffer
, length
);
157 static void callOnMainThreadFunction(WTF::MainThreadFunction function
, void* context
)
159 Platform::current()->mainThread()->taskRunner()->postTask(FROM_HERE
, new MainThreadTaskRunner(function
, context
));
162 static void adjustAmountOfExternalAllocatedMemory(int size
)
164 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(size
);
167 void initializeWithoutV8(Platform
* platform
)
169 ASSERT(!s_webKitInitialized
);
170 s_webKitInitialized
= true;
173 Platform::initialize(platform
);
175 WTF::setRandomSource(cryptographicallyRandomValues
);
176 WTF::initialize(currentTimeFunction
, monotonicallyIncreasingTimeFunction
, systemTraceTimeFunction
, histogramEnumerationFunction
, adjustAmountOfExternalAllocatedMemory
);
177 WTF::initializeMainThread(callOnMainThreadFunction
);
180 ThreadState::attachMainThread();
181 // currentThread() is null if we are running on a thread without a message loop.
182 if (WebThread
* currentThread
= platform
->currentThread()) {
183 ASSERT(!s_pendingGCRunner
);
184 s_pendingGCRunner
= new PendingGCRunner
;
185 currentThread
->addTaskObserver(s_pendingGCRunner
);
187 OwnPtr
<MessageLoopInterruptor
> interruptor
= adoptPtr(new MessageLoopInterruptor(currentThread
->taskRunner()));
188 ThreadState::current()->addInterruptor(interruptor
.release());
191 DEFINE_STATIC_LOCAL(ModulesInitializer
, initializer
, ());
194 setIndexedDBClientCreateFunction(IndexedDBClientImpl::create
);
199 // currentThread() is null if we are running on a thread without a message loop.
200 if (Platform::current()->currentThread()) {
201 // We don't need to (cannot) remove s_endOfTaskRunner from the current
202 // message loop, because the message loop is already destructed before
203 // the shutdown() is called.
204 delete s_endOfTaskRunner
;
205 s_endOfTaskRunner
= 0;
208 // currentThread() is null if we are running on a thread without a message loop.
209 if (Platform::current()->currentThread()) {
210 ASSERT(s_pendingGCRunner
);
211 delete s_pendingGCRunner
;
212 s_pendingGCRunner
= 0;
215 // Shutdown V8-related background threads before V8 is ramped down. Note
216 // that this will wait the thread to stop its operations.
217 ScriptStreamerThread::shutdown();
219 v8::Isolate
* isolate
= V8PerIsolateData::mainThreadIsolate();
220 V8PerIsolateData::willBeDestroyed(isolate
);
222 // Make sure we stop WorkerThreads before the main thread's ThreadState
223 // and later shutdown steps starts freeing up resources needed during
224 // worker termination.
225 WorkerThread::terminateAndWaitForAllWorkers();
227 ModulesInitializer::terminateThreads();
229 // Detach the main thread before starting the shutdown sequence
230 // so that the main thread won't get involved in a GC during the shutdown.
231 ThreadState::detachMainThread();
233 V8PerIsolateData::destroy(isolate
);
238 void shutdownWithoutV8()
240 ASSERT(!s_endOfTaskRunner
);
241 CoreInitializer::shutdown();
244 Platform::shutdown();
245 WebPrerenderingSupport::shutdown();
248 void setLayoutTestMode(bool value
)
250 LayoutTestSupport::setIsRunningLayoutTest(value
);
253 bool layoutTestMode()
255 return LayoutTestSupport::isRunningLayoutTest();
258 void setFontAntialiasingEnabledForTest(bool value
)
260 LayoutTestSupport::setFontAntialiasingEnabledForTest(value
);
263 bool fontAntialiasingEnabledForTest()
265 return LayoutTestSupport::isFontAntialiasingEnabledForTest();
268 void enableLogChannel(const char* name
)
271 WTFLogChannel
* channel
= getChannelFromName(name
);
273 channel
->state
= WTFLogChannelOn
;
274 #endif // !LOG_DISABLED
277 void resetPluginCache(bool reloadPages
)
279 ASSERT(!reloadPages
);
280 Page::refreshPlugins();
283 void decommitFreeableMemory()
285 WTF::Partitions::decommitFreeableMemory();