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/V8Binding.h"
35 #include "bindings/core/v8/V8GCController.h"
36 #include "bindings/core/v8/V8Initializer.h"
37 #include "core/Init.h"
38 #include "core/animation/AnimationClock.h"
39 #include "core/dom/Microtask.h"
40 #include "core/frame/Settings.h"
41 #include "core/page/Page.h"
42 #include "core/workers/WorkerGlobalScopeProxy.h"
43 #include "gin/public/v8_platform.h"
44 #include "modules/InitModules.h"
45 #include "platform/LayoutTestSupport.h"
46 #include "platform/Logging.h"
47 #include "platform/RuntimeEnabledFeatures.h"
48 #include "platform/graphics/ImageDecodingStore.h"
49 #include "platform/graphics/media/MediaPlayer.h"
50 #include "platform/heap/Heap.h"
51 #include "platform/heap/glue/MessageLoopInterruptor.h"
52 #include "platform/heap/glue/PendingGCRunner.h"
53 #include "platform/scheduler/Scheduler.h"
54 #include "public/platform/Platform.h"
55 #include "public/platform/WebPrerenderingSupport.h"
56 #include "public/platform/WebThread.h"
57 #include "web/IndexedDBClientImpl.h"
58 #include "web/WebMediaPlayerClientImpl.h"
59 #include "wtf/Assertions.h"
60 #include "wtf/CryptographicallyRandomNumber.h"
61 #include "wtf/MainThread.h"
63 #include "wtf/text/AtomicString.h"
64 #include "wtf/text/TextEncoding.h"
71 class EndOfTaskRunner : public WebThread::TaskObserver {
73 virtual void willProcessTask() override
75 AnimationClock::notifyTaskStart();
77 virtual void didProcessTask() override
79 Microtask::performCheckpoint();
80 V8GCController::reportDOMMemoryUsageToV8(mainThreadIsolate());
81 V8Initializer::reportRejectedPromises();
87 static WebThread::TaskObserver* s_endOfTaskRunner = 0;
88 static WebThread::TaskObserver* s_pendingGCRunner = 0;
89 static ThreadState::Interruptor* s_messageLoopInterruptor = 0;
90 static ThreadState::Interruptor* s_isolateInterruptor = 0;
92 // Make sure we are not re-initialized in the same address space.
93 // Doing so may cause hard to reproduce crashes.
94 static bool s_webKitInitialized = false;
96 void initialize(Platform* platform)
98 initializeWithoutV8(platform);
100 V8Initializer::initializeMainThreadIfNeeded();
102 s_isolateInterruptor = new V8IsolateInterruptor(V8PerIsolateData::mainThreadIsolate());
103 ThreadState::current()->addInterruptor(s_isolateInterruptor);
104 ThreadState::current()->registerTraceDOMWrappers(V8PerIsolateData::mainThreadIsolate(), V8GCController::traceDOMWrappers);
106 // currentThread will always be non-null in production, but can be null in Chromium unit tests.
107 if (WebThread* currentThread = platform->currentThread()) {
108 ASSERT(!s_endOfTaskRunner);
109 s_endOfTaskRunner = new EndOfTaskRunner;
110 currentThread->addTaskObserver(s_endOfTaskRunner);
114 v8::Isolate* mainThreadIsolate()
116 return V8PerIsolateData::mainThreadIsolate();
119 static double currentTimeFunction()
121 return Platform::current()->currentTime();
124 static double monotonicallyIncreasingTimeFunction()
126 return Platform::current()->monotonicallyIncreasingTime();
129 static void cryptographicallyRandomValues(unsigned char* buffer, size_t length)
131 Platform::current()->cryptographicallyRandomValues(buffer, length);
134 static void callOnMainThreadFunction(WTF::MainThreadFunction function, void* context)
136 Platform::current()->callOnMainThread(function, context);
139 void initializeWithoutV8(Platform* platform)
141 ASSERT(!s_webKitInitialized);
142 s_webKitInitialized = true;
145 Platform::initialize(platform);
147 WTF::setRandomSource(cryptographicallyRandomValues);
148 WTF::initialize(currentTimeFunction, monotonicallyIncreasingTimeFunction);
149 WTF::initializeMainThread(callOnMainThreadFunction);
152 ThreadState::attachMainThread();
153 // currentThread will always be non-null in production, but can be null in Chromium unit tests.
154 if (WebThread* currentThread = platform->currentThread()) {
155 ASSERT(!s_pendingGCRunner);
156 s_pendingGCRunner = new PendingGCRunner;
157 currentThread->addTaskObserver(s_pendingGCRunner);
159 ASSERT(!s_messageLoopInterruptor);
160 s_messageLoopInterruptor = new MessageLoopInterruptor(currentThread);
161 ThreadState::current()->addInterruptor(s_messageLoopInterruptor);
164 DEFINE_STATIC_LOCAL(ModulesInitializer, initializer, ());
167 // There are some code paths (for example, running WebKit in the browser
168 // process and calling into LocalStorage before anything else) where the
169 // UTF8 string encoding tables are used on a background thread before
170 // they're set up. This is a problem because their set up routines assert
171 // they're running on the main WebKitThread. It might be possible to make
172 // the initialization thread-safe, but given that so many code paths use
173 // this, initializing this lazily probably doesn't buy us much.
176 setIndexedDBClientCreateFunction(IndexedDBClientImpl::create);
178 MediaPlayer::setMediaEngineCreateFunction(WebMediaPlayerClientImpl::create);
183 // currentThread will always be non-null in production, but can be null in Chromium unit tests.
184 if (Platform::current()->currentThread()) {
185 // We don't need to (cannot) remove s_endOfTaskRunner from the current
186 // message loop, because the message loop is already destructed before
187 // the shutdown() is called.
188 delete s_endOfTaskRunner;
189 s_endOfTaskRunner = 0;
192 ASSERT(s_isolateInterruptor);
193 ThreadState::current()->removeInterruptor(s_isolateInterruptor);
195 // currentThread will always be non-null in production, but can be null in Chromium unit tests.
196 if (Platform::current()->currentThread()) {
197 ASSERT(s_pendingGCRunner);
198 delete s_pendingGCRunner;
199 s_pendingGCRunner = 0;
201 ASSERT(s_messageLoopInterruptor);
202 ThreadState::current()->removeInterruptor(s_messageLoopInterruptor);
203 delete s_messageLoopInterruptor;
204 s_messageLoopInterruptor = 0;
207 v8::Isolate* isolate = V8PerIsolateData::mainThreadIsolate();
208 V8PerIsolateData::willBeDestroyed(isolate);
210 // Make sure we stop WorkerThreads before the main thread's ThreadState
211 // and later shutdown steps starts freeing up resources needed during
212 // worker termination.
213 WorkerThread::terminateAndWaitForAllWorkers();
215 // Detach the main thread before starting the shutdown sequence
216 // so that the main thread won't get involved in a GC during the shutdown.
217 ThreadState::detachMainThread();
219 V8PerIsolateData::destroy(isolate);
224 void shutdownWithoutV8()
226 ASSERT(!s_endOfTaskRunner);
227 CoreInitializer::shutdown();
228 Scheduler::shutdown();
231 Platform::shutdown();
232 WebPrerenderingSupport::shutdown();
235 void setLayoutTestMode(bool value)
237 LayoutTestSupport::setIsRunningLayoutTest(value);
240 bool layoutTestMode()
242 return LayoutTestSupport::isRunningLayoutTest();
245 void setFontAntialiasingEnabledForTest(bool value)
247 LayoutTestSupport::setFontAntialiasingEnabledForTest(value);
250 bool fontAntialiasingEnabledForTest()
252 return LayoutTestSupport::isFontAntialiasingEnabledForTest();
255 void enableLogChannel(const char* name)
258 WTFLogChannel* channel = getChannelFromName(name);
260 channel->state = WTFLogChannelOn;
261 #endif // !LOG_DISABLED
264 void resetPluginCache(bool reloadPages)
266 Page::refreshPlugins(reloadPages);