Update To 11.40.268.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 "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"
62 #include "wtf/WTF.h"
63 #include "wtf/text/AtomicString.h"
64 #include "wtf/text/TextEncoding.h"
65 #include <v8.h>
66
67 namespace blink {
68
69 namespace {
70
71 class EndOfTaskRunner : public WebThread::TaskObserver {
72 public:
73     virtual void willProcessTask() override
74     {
75         AnimationClock::notifyTaskStart();
76     }
77     virtual void didProcessTask() override
78     {
79         Microtask::performCheckpoint();
80         V8GCController::reportDOMMemoryUsageToV8(mainThreadIsolate());
81         V8Initializer::reportRejectedPromises();
82     }
83 };
84
85 } // namespace
86
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;
91
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;
95
96 void initialize(Platform* platform)
97 {
98     initializeWithoutV8(platform);
99
100     V8Initializer::initializeMainThreadIfNeeded();
101
102     s_isolateInterruptor = new V8IsolateInterruptor(V8PerIsolateData::mainThreadIsolate());
103     ThreadState::current()->addInterruptor(s_isolateInterruptor);
104     ThreadState::current()->registerTraceDOMWrappers(V8PerIsolateData::mainThreadIsolate(), V8GCController::traceDOMWrappers);
105
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);
111     }
112 }
113
114 v8::Isolate* mainThreadIsolate()
115 {
116     return V8PerIsolateData::mainThreadIsolate();
117 }
118
119 static double currentTimeFunction()
120 {
121     return Platform::current()->currentTime();
122 }
123
124 static double monotonicallyIncreasingTimeFunction()
125 {
126     return Platform::current()->monotonicallyIncreasingTime();
127 }
128
129 static void cryptographicallyRandomValues(unsigned char* buffer, size_t length)
130 {
131     Platform::current()->cryptographicallyRandomValues(buffer, length);
132 }
133
134 static void callOnMainThreadFunction(WTF::MainThreadFunction function, void* context)
135 {
136     Platform::current()->callOnMainThread(function, context);
137 }
138
139 void initializeWithoutV8(Platform* platform)
140 {
141     ASSERT(!s_webKitInitialized);
142     s_webKitInitialized = true;
143
144     ASSERT(platform);
145     Platform::initialize(platform);
146
147     WTF::setRandomSource(cryptographicallyRandomValues);
148     WTF::initialize(currentTimeFunction, monotonicallyIncreasingTimeFunction);
149     WTF::initializeMainThread(callOnMainThreadFunction);
150     Heap::init();
151
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);
158
159         ASSERT(!s_messageLoopInterruptor);
160         s_messageLoopInterruptor = new MessageLoopInterruptor(currentThread);
161         ThreadState::current()->addInterruptor(s_messageLoopInterruptor);
162     }
163
164     DEFINE_STATIC_LOCAL(ModulesInitializer, initializer, ());
165     initializer.init();
166
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.
174     WTF::UTF8Encoding();
175
176     setIndexedDBClientCreateFunction(IndexedDBClientImpl::create);
177
178     MediaPlayer::setMediaEngineCreateFunction(WebMediaPlayerClientImpl::create);
179 }
180
181 void shutdown()
182 {
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;
190     }
191
192     ASSERT(s_isolateInterruptor);
193     ThreadState::current()->removeInterruptor(s_isolateInterruptor);
194
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;
200
201         ASSERT(s_messageLoopInterruptor);
202         ThreadState::current()->removeInterruptor(s_messageLoopInterruptor);
203         delete s_messageLoopInterruptor;
204         s_messageLoopInterruptor = 0;
205     }
206
207     v8::Isolate* isolate = V8PerIsolateData::mainThreadIsolate();
208     V8PerIsolateData::willBeDestroyed(isolate);
209
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();
214
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();
218
219     V8PerIsolateData::destroy(isolate);
220
221     shutdownWithoutV8();
222 }
223
224 void shutdownWithoutV8()
225 {
226     ASSERT(!s_endOfTaskRunner);
227     CoreInitializer::shutdown();
228     Scheduler::shutdown();
229     Heap::shutdown();
230     WTF::shutdown();
231     Platform::shutdown();
232     WebPrerenderingSupport::shutdown();
233 }
234
235 void setLayoutTestMode(bool value)
236 {
237     LayoutTestSupport::setIsRunningLayoutTest(value);
238 }
239
240 bool layoutTestMode()
241 {
242     return LayoutTestSupport::isRunningLayoutTest();
243 }
244
245 void setFontAntialiasingEnabledForTest(bool value)
246 {
247     LayoutTestSupport::setFontAntialiasingEnabledForTest(value);
248 }
249
250 bool fontAntialiasingEnabledForTest()
251 {
252     return LayoutTestSupport::isFontAntialiasingEnabledForTest();
253 }
254
255 void enableLogChannel(const char* name)
256 {
257 #if !LOG_DISABLED
258     WTFLogChannel* channel = getChannelFromName(name);
259     if (channel)
260         channel->state = WTFLogChannelOn;
261 #endif // !LOG_DISABLED
262 }
263
264 void resetPluginCache(bool reloadPages)
265 {
266     Page::refreshPlugins(reloadPages);
267 }
268
269 } // namespace blink