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.
34 #include "IDBFactoryBackendProxy.h"
35 #include "RuntimeEnabledFeatures.h"
36 #include "WebMediaPlayerClientImpl.h"
37 #include "bindings/v8/V8Binding.h"
38 #include "bindings/v8/V8Initializer.h"
39 #include "bindings/v8/V8RecursionScope.h"
40 #include "core/Init.h"
41 #include "core/dom/Microtask.h"
42 #include "core/page/Page.h"
43 #include "core/frame/Settings.h"
44 #include "core/workers/WorkerGlobalScopeProxy.h"
45 #include "heap/Heap.h"
46 #include "heap/glue/MessageLoopInterruptor.h"
47 #include "heap/glue/PendingGCRunner.h"
48 #include "platform/LayoutTestSupport.h"
49 #include "platform/Logging.h"
50 #include "platform/graphics/ImageDecodingStore.h"
51 #include "platform/graphics/media/MediaPlayer.h"
52 #include "public/platform/Platform.h"
53 #include "public/platform/WebPrerenderingSupport.h"
54 #include "public/platform/WebThread.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);
97 static void assertV8RecursionScope()
99 ASSERT(!isMainThread() || WebCore::V8RecursionScope::properlyUsed());
103 void initialize(Platform* platform)
105 initializeWithoutV8(platform);
107 v8::Isolate* isolate = v8::Isolate::GetCurrent();
108 WebCore::V8Initializer::initializeMainThreadIfNeeded(isolate);
109 v8::V8::SetEntropySource(&generateEntropy);
110 v8::V8::SetArrayBufferAllocator(WebCore::v8ArrayBufferAllocator());
111 v8::V8::Initialize();
112 WebCore::V8PerIsolateData::ensureInitialized(isolate);
114 s_isolateInterruptor = new WebCore::V8IsolateInterruptor(v8::Isolate::GetCurrent());
115 WebCore::ThreadState::current()->addInterruptor(s_isolateInterruptor);
117 // currentThread will always be non-null in production, but can be null in Chromium unit tests.
118 if (WebThread* currentThread = platform->currentThread()) {
120 v8::V8::AddCallCompletedCallback(&assertV8RecursionScope);
122 ASSERT(!s_endOfTaskRunner);
123 s_endOfTaskRunner = new EndOfTaskRunner;
124 currentThread->addTaskObserver(s_endOfTaskRunner);
128 v8::Isolate* mainThreadIsolate()
130 return WebCore::V8PerIsolateData::mainThreadIsolate();
133 static double currentTimeFunction()
135 return Platform::current()->currentTime();
138 static double monotonicallyIncreasingTimeFunction()
140 return Platform::current()->monotonicallyIncreasingTime();
143 static void cryptographicallyRandomValues(unsigned char* buffer, size_t length)
145 Platform::current()->cryptographicallyRandomValues(buffer, length);
148 static void callOnMainThreadFunction(WTF::MainThreadFunction function, void* context)
150 Platform::current()->callOnMainThread(function, context);
153 void initializeWithoutV8(Platform* platform)
155 ASSERT(!s_webKitInitialized);
156 s_webKitInitialized = true;
159 Platform::initialize(platform);
161 WTF::setRandomSource(cryptographicallyRandomValues);
162 WTF::initialize(currentTimeFunction, monotonicallyIncreasingTimeFunction);
163 WTF::initializeMainThread(callOnMainThreadFunction);
164 WebCore::Heap::init();
165 if (WebThread* currentThread = platform->currentThread()) {
166 ASSERT(!s_pendingGCRunner);
167 s_pendingGCRunner = new WebCore::PendingGCRunner;
168 currentThread->addTaskObserver(s_pendingGCRunner);
170 ASSERT(!s_messageLoopInterruptor);
171 s_messageLoopInterruptor = new WebCore::MessageLoopInterruptor(currentThread);
172 WebCore::ThreadState::current()->addInterruptor(s_messageLoopInterruptor);
175 WebCore::ImageDecodingStore::initializeOnce();
177 // There are some code paths (for example, running WebKit in the browser
178 // process and calling into LocalStorage before anything else) where the
179 // UTF8 string encoding tables are used on a background thread before
180 // they're set up. This is a problem because their set up routines assert
181 // they're running on the main WebKitThread. It might be possible to make
182 // the initialization thread-safe, but given that so many code paths use
183 // this, initializing this lazily probably doesn't buy us much.
186 WebCore::setIDBFactoryBackendInterfaceCreateFunction(blink::IDBFactoryBackendProxy::create);
188 WebCore::MediaPlayer::setMediaEngineCreateFunction(blink::WebMediaPlayerClientImpl::create);
193 // currentThread will always be non-null in production, but can be null in Chromium unit tests.
194 if (Platform::current()->currentThread()) {
195 ASSERT(s_endOfTaskRunner);
197 v8::V8::RemoveCallCompletedCallback(&assertV8RecursionScope);
199 Platform::current()->currentThread()->removeTaskObserver(s_endOfTaskRunner);
200 delete s_endOfTaskRunner;
201 s_endOfTaskRunner = 0;
204 ASSERT(s_isolateInterruptor);
205 WebCore::ThreadState::current()->removeInterruptor(s_isolateInterruptor);
207 WebCore::V8PerIsolateData::dispose(WebCore::V8PerIsolateData::mainThreadIsolate());
213 void shutdownWithoutV8()
215 ASSERT(!s_endOfTaskRunner);
216 WebCore::ImageDecodingStore::shutdown();
218 if (Platform::current()->currentThread()) {
219 ASSERT(s_pendingGCRunner);
220 delete s_pendingGCRunner;
221 s_pendingGCRunner = 0;
223 ASSERT(s_messageLoopInterruptor);
224 WebCore::ThreadState::current()->removeInterruptor(s_messageLoopInterruptor);
225 delete s_messageLoopInterruptor;
226 s_messageLoopInterruptor = 0;
228 WebCore::Heap::shutdown();
230 Platform::shutdown();
231 WebPrerenderingSupport::shutdown();
234 void setLayoutTestMode(bool value)
236 WebCore::setIsRunningLayoutTest(value);
239 bool layoutTestMode()
241 return WebCore::isRunningLayoutTest();
244 void enableLogChannel(const char* name)
247 WTFLogChannel* channel = WebCore::getChannelFromName(name);
249 channel->state = WTFLogChannelOn;
250 #endif // !LOG_DISABLED
253 void resetPluginCache(bool reloadPages)
255 WebCore::Page::refreshPlugins(reloadPages);