Upstream version 7.36.149.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 "public/web/WebKit.h"
33
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"
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 void initialize(Platform* platform)
97 {
98     initializeWithoutV8(platform);
99
100     v8::V8::InitializePlatform(gin::V8Platform::Get());
101     v8::Isolate* isolate = v8::Isolate::New();
102     isolate->Enter();
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);
109
110     s_isolateInterruptor = new WebCore::V8IsolateInterruptor(v8::Isolate::GetCurrent());
111     WebCore::ThreadState::current()->addInterruptor(s_isolateInterruptor);
112
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);
118     }
119 }
120
121 v8::Isolate* mainThreadIsolate()
122 {
123     return WebCore::V8PerIsolateData::mainThreadIsolate();
124 }
125
126 static double currentTimeFunction()
127 {
128     return Platform::current()->currentTime();
129 }
130
131 static double monotonicallyIncreasingTimeFunction()
132 {
133     return Platform::current()->monotonicallyIncreasingTime();
134 }
135
136 static void cryptographicallyRandomValues(unsigned char* buffer, size_t length)
137 {
138     Platform::current()->cryptographicallyRandomValues(buffer, length);
139 }
140
141 static void callOnMainThreadFunction(WTF::MainThreadFunction function, void* context)
142 {
143     Platform::current()->callOnMainThread(function, context);
144 }
145
146 void initializeWithoutV8(Platform* platform)
147 {
148     ASSERT(!s_webKitInitialized);
149     s_webKitInitialized = true;
150
151     ASSERT(platform);
152     Platform::initialize(platform);
153
154     WTF::setRandomSource(cryptographicallyRandomValues);
155     WTF::initialize(currentTimeFunction, monotonicallyIncreasingTimeFunction);
156     WTF::initializeMainThread(callOnMainThreadFunction);
157     WebCore::Heap::init();
158
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);
165
166         ASSERT(!s_messageLoopInterruptor);
167         s_messageLoopInterruptor = new WebCore::MessageLoopInterruptor(currentThread);
168         WebCore::ThreadState::current()->addInterruptor(s_messageLoopInterruptor);
169     }
170     WebCore::init();
171     WebCore::ImageDecodingStore::initializeOnce();
172
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.
180     WTF::UTF8Encoding();
181
182     WebCore::setIndexedDBClientCreateFunction(blink::IndexedDBClientImpl::create);
183
184     WebCore::MediaPlayer::setMediaEngineCreateFunction(blink::WebMediaPlayerClientImpl::create);
185 }
186
187 void shutdown()
188 {
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;
195     }
196
197     ASSERT(s_isolateInterruptor);
198     WebCore::ThreadState::current()->removeInterruptor(s_isolateInterruptor);
199
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;
205
206         ASSERT(s_messageLoopInterruptor);
207         WebCore::ThreadState::current()->removeInterruptor(s_messageLoopInterruptor);
208         delete s_messageLoopInterruptor;
209         s_messageLoopInterruptor = 0;
210     }
211
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();
215
216     v8::Isolate* isolate = WebCore::V8PerIsolateData::mainThreadIsolate();
217     WebCore::V8PerIsolateData::dispose(isolate);
218     isolate->Exit();
219     isolate->Dispose();
220
221     shutdownWithoutV8();
222 }
223
224 void shutdownWithoutV8()
225 {
226     ASSERT(!s_endOfTaskRunner);
227     WebCore::ImageDecodingStore::shutdown();
228     WebCore::shutdown();
229     WebCore::Heap::shutdown();
230     WTF::shutdown();
231     Platform::shutdown();
232     WebPrerenderingSupport::shutdown();
233 }
234
235 void setLayoutTestMode(bool value)
236 {
237     WebCore::setIsRunningLayoutTest(value);
238 }
239
240 bool layoutTestMode()
241 {
242     return WebCore::isRunningLayoutTest();
243 }
244
245 void setFontAntialiasingEnabledForTest(bool value)
246 {
247     WebCore::setFontAntialiasingEnabledForTest(value);
248 }
249
250 bool fontAntialiasingEnabledForTest()
251 {
252     return WebCore::isFontAntialiasingEnabledForTest();
253 }
254
255 void enableLogChannel(const char* name)
256 {
257 #if !LOG_DISABLED
258     WTFLogChannel* channel = WebCore::getChannelFromName(name);
259     if (channel)
260         channel->state = WTFLogChannelOn;
261 #endif // !LOG_DISABLED
262 }
263
264 void resetPluginCache(bool reloadPages)
265 {
266     WebCore::Page::refreshPlugins(reloadPages);
267 }
268
269 } // namespace blink