Merge TIZEN_WEBKIT2_REMOTE_WEB_INSPECTOR into TIZEN_REMOTE_WEB_INSPECTOR
[framework/web/webkit-efl.git] / Source / WebKit2 / WebProcess / WebKitMain.cpp
1 /*
2  * Copyright (C) 2010 Apple 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
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "CommandLine.h"
28
29 #include "PluginProcessMain.h"
30 #include "ProcessLauncher.h"
31 #include "WebProcessMain.h"
32 #include <wtf/text/CString.h>
33
34 #if PLATFORM(MAC)
35 #include <objc/objc-auto.h>
36 #elif PLATFORM(WIN)
37 #include <shlwapi.h>
38 #endif
39
40 using namespace WebKit;
41
42 static int WebKitMain(const CommandLine& commandLine)
43 {
44     ProcessLauncher::ProcessType processType;    
45     if (!ProcessLauncher::getProcessTypeFromString(commandLine["type"].utf8().data(), processType))
46         return EXIT_FAILURE;
47
48     switch (processType) {
49         case ProcessLauncher::WebProcess:
50             return WebProcessMain(commandLine);
51         case ProcessLauncher::PluginProcess:
52 #if ENABLE(PLUGIN_PROCESS)
53             return PluginProcessMain(commandLine);
54 #else
55             break;
56 #endif
57     }
58
59     return EXIT_FAILURE;
60 }
61
62 #if PLATFORM(MAC)
63
64 extern "C" WK_EXPORT int WebKitMain(int argc, char** argv);
65
66 int WebKitMain(int argc, char** argv)
67 {
68     ASSERT(!objc_collectingEnabled());
69     
70     CommandLine commandLine;
71     if (!commandLine.parse(argc, argv))
72         return EXIT_FAILURE;
73     
74     return WebKitMain(commandLine);
75 }
76
77 #elif PLATFORM(WIN)
78
79 static void enableDataExecutionPrevention()
80 {
81     // Enable Data Execution prevention at runtime rather than via /NXCOMPAT
82     // http://blogs.msdn.com/michael_howard/archive/2008/01/29/new-nx-apis-added-to-windows-vista-sp1-windows-xp-sp3-and-windows-server-2008.aspx
83
84     const DWORD enableDEP = 0x00000001;
85
86     HMODULE hMod = ::GetModuleHandleW(L"Kernel32.dll");
87     if (!hMod)
88         return;
89
90     typedef BOOL (WINAPI *PSETDEP)(DWORD);
91
92     PSETDEP procSet = reinterpret_cast<PSETDEP>(::GetProcAddress(hMod, "SetProcessDEPPolicy"));
93     if (!procSet)
94         return;
95
96     // Enable Data Execution Prevention, but allow ATL thunks (for compatibility with the version of ATL that ships with the Platform SDK).
97     procSet(enableDEP);
98 }
99
100 static void enableTerminationOnHeapCorruption()
101 {
102     // Enable termination on heap corruption on OSes that support it (Vista and XPSP3).
103     // http://msdn.microsoft.com/en-us/library/aa366705(VS.85).aspx
104
105     const HEAP_INFORMATION_CLASS heapEnableTerminationOnCorruption = static_cast<HEAP_INFORMATION_CLASS>(1);
106
107     HMODULE hMod = ::GetModuleHandleW(L"kernel32.dll");
108     if (!hMod)
109         return;
110
111     typedef BOOL (WINAPI*HSI)(HANDLE, HEAP_INFORMATION_CLASS, PVOID, SIZE_T);
112     HSI heapSetInformation = reinterpret_cast<HSI>(::GetProcAddress(hMod, "HeapSetInformation"));
113     if (!heapSetInformation)
114         return;
115
116     heapSetInformation(0, heapEnableTerminationOnCorruption, 0, 0);
117 }
118
119 static void disableUserModeCallbackExceptionFilter()
120 {
121     const DWORD PROCESS_CALLBACK_FILTER_ENABLED = 0x1;
122     typedef BOOL (NTAPI *getProcessUserModeExceptionPolicyPtr)(LPDWORD lpFlags);
123     typedef BOOL (NTAPI *setProcessUserModeExceptionPolicyPtr)(DWORD dwFlags);
124
125     HMODULE lib = LoadLibrary(TEXT("kernel32.dll"));
126     ASSERT(lib);
127
128     getProcessUserModeExceptionPolicyPtr getPolicyPtr = (getProcessUserModeExceptionPolicyPtr)GetProcAddress(lib, "GetProcessUserModeExceptionPolicy");
129     setProcessUserModeExceptionPolicyPtr setPolicyPtr = (setProcessUserModeExceptionPolicyPtr)GetProcAddress(lib, "SetProcessUserModeExceptionPolicy");
130
131     DWORD dwFlags;
132     if (!getPolicyPtr || !setPolicyPtr || !getPolicyPtr(&dwFlags)) {
133         FreeLibrary(lib);
134         return;
135     }
136
137     // If this flag isn't cleared, exceptions that are thrown when running in a 64-bit version of
138     // Windows are ignored, possibly leaving Safari in an inconsistent state that could cause an 
139     // unrelated exception to be thrown.
140     // http://support.microsoft.com/kb/976038
141     // http://blog.paulbetts.org/index.php/2010/07/20/the-case-of-the-disappearing-onload-exception-user-mode-callback-exceptions-in-x64/
142     setPolicyPtr(dwFlags & ~PROCESS_CALLBACK_FILTER_ENABLED);
143
144     FreeLibrary(lib);
145 }
146
147 #ifndef NDEBUG
148 static void pauseProcessIfNeeded(HMODULE module)
149 {
150     // Show an alert when Ctrl-Alt-Shift is held down during launch to give the user time to attach a
151     // debugger. This is useful for debugging problems that happen early in the web process's lifetime.
152     const unsigned short highBitMaskShort = 0x8000;
153     if (!getenv("WEBKIT2_PAUSE_WEB_PROCESS_ON_LAUNCH") && !((::GetKeyState(VK_CONTROL) & highBitMaskShort) && (::GetKeyState(VK_MENU) & highBitMaskShort) && (::GetKeyState(VK_SHIFT) & highBitMaskShort)))
154         return;
155
156     wchar_t path[MAX_PATH];
157     DWORD length = ::GetModuleFileNameW(module, path, WTF_ARRAY_LENGTH(path));
158     if (!length || length == WTF_ARRAY_LENGTH(path))
159         return;
160
161     wchar_t* startOfFilename = ::PathFindFileNameW(path);
162     String filenameString(startOfFilename, length - (startOfFilename - path));
163
164     String message = L"You can now attach a debugger to " + filenameString + L". You can use the same debugger for " + filenameString + L" and the UI process, if desired. Click OK when you are ready for " + filenameString + L" to continue.";
165     String title = filenameString + L" has launched";
166     ::MessageBoxW(0, message.charactersWithNullTermination(), title.charactersWithNullTermination(), MB_OK | MB_ICONINFORMATION);
167 }
168 #endif
169
170 extern "C" __declspec(dllexport) 
171 int WebKitMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpstrCmdLine, int nCmdShow)
172 {
173 #ifndef NDEBUG
174     pauseProcessIfNeeded(hInstance);
175 #endif
176
177     enableDataExecutionPrevention();
178
179     enableTerminationOnHeapCorruption();
180
181     disableUserModeCallbackExceptionFilter();
182
183     CommandLine commandLine;
184     if (!commandLine.parse(lpstrCmdLine))
185         return EXIT_FAILURE;
186
187     return WebKitMain(commandLine);
188 }
189
190 #endif