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 "RuntimeEnabledFeatures.h"
35 #include "bindings/v8/V8Binding.h"
36 #include "bindings/v8/V8Initializer.h"
37 #include "core/Init.h"
38 #include "core/dom/Microtask.h"
39 #include "core/frame/Settings.h"
40 #include "core/page/Page.h"
41 #include "core/workers/WorkerGlobalScopeProxy.h"
42 #include "gin/public/v8_platform.h"
43 #include "platform/LayoutTestSupport.h"
44 #include "platform/Logging.h"
45 #include "platform/graphics/ImageDecodingStore.h"
46 #include "platform/graphics/media/MediaPlayer.h"
47 #include "platform/heap/Heap.h"
48 #include "platform/heap/glue/MessageLoopInterruptor.h"
49 #include "platform/heap/glue/PendingGCRunner.h"
50 #include "public/platform/Platform.h"
51 #include "public/platform/WebPrerenderingSupport.h"
52 #include "public/platform/WebThread.h"
53 #include "web/IndexedDBClientImpl.h"
54 #include "web/WebMediaPlayerClientImpl.h"
55 #include "wtf/Assertions.h"
56 #include "wtf/CryptographicallyRandomNumber.h"
57 #include "wtf/MainThread.h"
59 #include "wtf/text/AtomicString.h"
60 #include "wtf/text/TextEncoding.h"
67 class EndOfTaskRunner : public WebThread::TaskObserver {
69 virtual void willProcessTask() { }
70 virtual void didProcessTask()
72 WebCore::Microtask::performCheckpoint();
78 static WebThread::TaskObserver* s_endOfTaskRunner = 0;
79 static WebThread::TaskObserver* s_pendingGCRunner = 0;
80 static WebCore::ThreadState::Interruptor* s_messageLoopInterruptor = 0;
81 static WebCore::ThreadState::Interruptor* s_isolateInterruptor = 0;
83 // Make sure we are not re-initialized in the same address space.
84 // Doing so may cause hard to reproduce crashes.
85 static bool s_webKitInitialized = false;
87 static bool generateEntropy(unsigned char* buffer, size_t length)
89 if (Platform::current()) {
90 Platform::current()->cryptographicallyRandomValues(buffer, length);
96 void initialize(Platform* platform)
98 initializeWithoutV8(platform);
100 v8::V8::InitializePlatform(gin::V8Platform::Get());
101 v8::Isolate* isolate = v8::Isolate::New();
103 WebCore::V8Initializer::initializeMainThreadIfNeeded(isolate);
104 v8::V8::SetEntropySource(&generateEntropy);
105 v8::V8::SetArrayBufferAllocator(WebCore::v8ArrayBufferAllocator());
106 v8::V8::Initialize();
107 isolate->SetAutorunMicrotasks(false);
108 WebCore::V8PerIsolateData::ensureInitialized(isolate);
110 s_isolateInterruptor = new WebCore::V8IsolateInterruptor(v8::Isolate::GetCurrent());
111 WebCore::ThreadState::current()->addInterruptor(s_isolateInterruptor);
113 // currentThread will always be non-null in production, but can be null in Chromium unit tests.
114 if (WebThread* currentThread = platform->currentThread()) {
115 ASSERT(!s_endOfTaskRunner);
116 s_endOfTaskRunner = new EndOfTaskRunner;
117 currentThread->addTaskObserver(s_endOfTaskRunner);
121 v8::Isolate* mainThreadIsolate()
123 return WebCore::V8PerIsolateData::mainThreadIsolate();
126 static double currentTimeFunction()
128 return Platform::current()->currentTime();
131 static double monotonicallyIncreasingTimeFunction()
133 return Platform::current()->monotonicallyIncreasingTime();
136 static void cryptographicallyRandomValues(unsigned char* buffer, size_t length)
138 Platform::current()->cryptographicallyRandomValues(buffer, length);
141 static void callOnMainThreadFunction(WTF::MainThreadFunction function, void* context)
143 Platform::current()->callOnMainThread(function, context);
146 void initializeWithoutV8(Platform* platform)
148 ASSERT(!s_webKitInitialized);
149 s_webKitInitialized = true;
152 Platform::initialize(platform);
154 WTF::setRandomSource(cryptographicallyRandomValues);
155 WTF::initialize(currentTimeFunction, monotonicallyIncreasingTimeFunction);
156 WTF::initializeMainThread(callOnMainThreadFunction);
157 WebCore::Heap::init();
159 WebCore::ThreadState::attachMainThread();
160 // currentThread will always be non-null in production, but can be null in Chromium unit tests.
161 if (WebThread* currentThread = platform->currentThread()) {
162 ASSERT(!s_pendingGCRunner);
163 s_pendingGCRunner = new WebCore::PendingGCRunner;
164 currentThread->addTaskObserver(s_pendingGCRunner);
166 ASSERT(!s_messageLoopInterruptor);
167 s_messageLoopInterruptor = new WebCore::MessageLoopInterruptor(currentThread);
168 WebCore::ThreadState::current()->addInterruptor(s_messageLoopInterruptor);
171 WebCore::ImageDecodingStore::initializeOnce();
173 // There are some code paths (for example, running WebKit in the browser
174 // process and calling into LocalStorage before anything else) where the
175 // UTF8 string encoding tables are used on a background thread before
176 // they're set up. This is a problem because their set up routines assert
177 // they're running on the main WebKitThread. It might be possible to make
178 // the initialization thread-safe, but given that so many code paths use
179 // this, initializing this lazily probably doesn't buy us much.
182 WebCore::setIndexedDBClientCreateFunction(blink::IndexedDBClientImpl::create);
184 WebCore::MediaPlayer::setMediaEngineCreateFunction(blink::WebMediaPlayerClientImpl::create);
189 // currentThread will always be non-null in production, but can be null in Chromium unit tests.
190 if (Platform::current()->currentThread()) {
191 ASSERT(s_endOfTaskRunner);
192 Platform::current()->currentThread()->removeTaskObserver(s_endOfTaskRunner);
193 delete s_endOfTaskRunner;
194 s_endOfTaskRunner = 0;
197 ASSERT(s_isolateInterruptor);
198 WebCore::ThreadState::current()->removeInterruptor(s_isolateInterruptor);
200 // currentThread will always be non-null in production, but can be null in Chromium unit tests.
201 if (Platform::current()->currentThread()) {
202 ASSERT(s_pendingGCRunner);
203 delete s_pendingGCRunner;
204 s_pendingGCRunner = 0;
206 ASSERT(s_messageLoopInterruptor);
207 WebCore::ThreadState::current()->removeInterruptor(s_messageLoopInterruptor);
208 delete s_messageLoopInterruptor;
209 s_messageLoopInterruptor = 0;
212 // Detach the main thread before starting the shutdown sequence
213 // so that the main thread won't get involved in a GC during the shutdown.
214 WebCore::ThreadState::detachMainThread();
216 v8::Isolate* isolate = WebCore::V8PerIsolateData::mainThreadIsolate();
217 WebCore::V8PerIsolateData::dispose(isolate);
224 void shutdownWithoutV8()
226 ASSERT(!s_endOfTaskRunner);
227 WebCore::ImageDecodingStore::shutdown();
229 WebCore::Heap::shutdown();
231 Platform::shutdown();
232 WebPrerenderingSupport::shutdown();
235 void setLayoutTestMode(bool value)
237 WebCore::setIsRunningLayoutTest(value);
240 bool layoutTestMode()
242 return WebCore::isRunningLayoutTest();
245 void setFontAntialiasingEnabledForTest(bool value)
247 WebCore::setFontAntialiasingEnabledForTest(value);
250 bool fontAntialiasingEnabledForTest()
252 return WebCore::isFontAntialiasingEnabledForTest();
255 void enableLogChannel(const char* name)
258 WTFLogChannel* channel = WebCore::getChannelFromName(name);
260 channel->state = WTFLogChannelOn;
261 #endif // !LOG_DISABLED
264 void resetPluginCache(bool reloadPages)
266 WebCore::Page::refreshPlugins(reloadPages);