Upstream version 7.35.139.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / web / WebKit.cpp
1 /*
2  * Copyright (C) 2009 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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
13  * distribution.
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.
17  *
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.
29  */
30
31 #include "config.h"
32 #include "WebKit.h"
33
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"
58 #include "wtf/WTF.h"
59 #include "wtf/text/AtomicString.h"
60 #include "wtf/text/TextEncoding.h"
61 #include <v8.h>
62
63 namespace blink {
64
65 namespace {
66
67 class EndOfTaskRunner : public WebThread::TaskObserver {
68 public:
69     virtual void willProcessTask() { }
70     virtual void didProcessTask()
71     {
72         WebCore::Microtask::performCheckpoint();
73     }
74 };
75
76 } // namespace
77
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;
82
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;
86
87 static bool generateEntropy(unsigned char* buffer, size_t length)
88 {
89     if (Platform::current()) {
90         Platform::current()->cryptographicallyRandomValues(buffer, length);
91         return true;
92     }
93     return false;
94 }
95
96 #ifndef NDEBUG
97 static void assertV8RecursionScope()
98 {
99     ASSERT(!isMainThread() || WebCore::V8RecursionScope::properlyUsed());
100 }
101 #endif
102
103 void initialize(Platform* platform)
104 {
105     initializeWithoutV8(platform);
106
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);
113
114     s_isolateInterruptor = new WebCore::V8IsolateInterruptor(v8::Isolate::GetCurrent());
115     WebCore::ThreadState::current()->addInterruptor(s_isolateInterruptor);
116
117     // currentThread will always be non-null in production, but can be null in Chromium unit tests.
118     if (WebThread* currentThread = platform->currentThread()) {
119 #ifndef NDEBUG
120         v8::V8::AddCallCompletedCallback(&assertV8RecursionScope);
121 #endif
122         ASSERT(!s_endOfTaskRunner);
123         s_endOfTaskRunner = new EndOfTaskRunner;
124         currentThread->addTaskObserver(s_endOfTaskRunner);
125     }
126 }
127
128 v8::Isolate* mainThreadIsolate()
129 {
130     return WebCore::V8PerIsolateData::mainThreadIsolate();
131 }
132
133 static double currentTimeFunction()
134 {
135     return Platform::current()->currentTime();
136 }
137
138 static double monotonicallyIncreasingTimeFunction()
139 {
140     return Platform::current()->monotonicallyIncreasingTime();
141 }
142
143 static void cryptographicallyRandomValues(unsigned char* buffer, size_t length)
144 {
145     Platform::current()->cryptographicallyRandomValues(buffer, length);
146 }
147
148 static void callOnMainThreadFunction(WTF::MainThreadFunction function, void* context)
149 {
150     Platform::current()->callOnMainThread(function, context);
151 }
152
153 void initializeWithoutV8(Platform* platform)
154 {
155     ASSERT(!s_webKitInitialized);
156     s_webKitInitialized = true;
157
158     ASSERT(platform);
159     Platform::initialize(platform);
160
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);
169
170         ASSERT(!s_messageLoopInterruptor);
171         s_messageLoopInterruptor = new WebCore::MessageLoopInterruptor(currentThread);
172         WebCore::ThreadState::current()->addInterruptor(s_messageLoopInterruptor);
173     }
174     WebCore::init();
175     WebCore::ImageDecodingStore::initializeOnce();
176
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.
184     WTF::UTF8Encoding();
185
186     WebCore::setIDBFactoryBackendInterfaceCreateFunction(blink::IDBFactoryBackendProxy::create);
187
188     WebCore::MediaPlayer::setMediaEngineCreateFunction(blink::WebMediaPlayerClientImpl::create);
189 }
190
191 void shutdown()
192 {
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);
196 #ifndef NDEBUG
197         v8::V8::RemoveCallCompletedCallback(&assertV8RecursionScope);
198 #endif
199         Platform::current()->currentThread()->removeTaskObserver(s_endOfTaskRunner);
200         delete s_endOfTaskRunner;
201         s_endOfTaskRunner = 0;
202     }
203
204     ASSERT(s_isolateInterruptor);
205     WebCore::ThreadState::current()->removeInterruptor(s_isolateInterruptor);
206
207     WebCore::V8PerIsolateData::dispose(WebCore::V8PerIsolateData::mainThreadIsolate());
208     v8::V8::Dispose();
209
210     shutdownWithoutV8();
211 }
212
213 void shutdownWithoutV8()
214 {
215     ASSERT(!s_endOfTaskRunner);
216     WebCore::ImageDecodingStore::shutdown();
217     WebCore::shutdown();
218     if (Platform::current()->currentThread()) {
219         ASSERT(s_pendingGCRunner);
220         delete s_pendingGCRunner;
221         s_pendingGCRunner = 0;
222
223         ASSERT(s_messageLoopInterruptor);
224         WebCore::ThreadState::current()->removeInterruptor(s_messageLoopInterruptor);
225         delete s_messageLoopInterruptor;
226         s_messageLoopInterruptor = 0;
227     }
228     WebCore::Heap::shutdown();
229     WTF::shutdown();
230     Platform::shutdown();
231     WebPrerenderingSupport::shutdown();
232 }
233
234 void setLayoutTestMode(bool value)
235 {
236     WebCore::setIsRunningLayoutTest(value);
237 }
238
239 bool layoutTestMode()
240 {
241     return WebCore::isRunningLayoutTest();
242 }
243
244 void enableLogChannel(const char* name)
245 {
246 #if !LOG_DISABLED
247     WTFLogChannel* channel = WebCore::getChannelFromName(name);
248     if (channel)
249         channel->state = WTFLogChannelOn;
250 #endif // !LOG_DISABLED
251 }
252
253 void resetPluginCache(bool reloadPages)
254 {
255     WebCore::Page::refreshPlugins(reloadPages);
256 }
257
258 } // namespace blink