1/*
2 * Copyright (C) 2013 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#ifndef ChildProcessEntryPoint_h
27#define ChildProcessEntryPoint_h
28
29#import "ChildProcess.h"
30#import "CommandLine.h"
31#import "WebKit2Initialize.h"
32#import <WebCore/RunLoop.h>
33#import <WebKitSystemInterface.h>
34#import <sysexits.h>
35
36namespace WebKit {
37
38class ChildProcessMainDelegate {
39public:
40    ChildProcessMainDelegate(const CommandLine& commandLine)
41        : m_commandLine(commandLine)
42    {
43    }
44
45    virtual ~ChildProcessMainDelegate();
46
47    virtual void installSignalHandlers();
48    virtual void doPreInitializationWork();
49
50    virtual bool getConnectionIdentifier(CoreIPC::Connection::Identifier& identifier);
51    virtual bool getClientIdentifier(String& clientIdentifier);
52    virtual bool getClientProcessName(String& clientProcessName);
53    virtual bool getExtraInitializationData(HashMap<String, String>& extraInitializationData);
54
55    virtual void startRunLoop();
56    virtual void doPostRunWork();
57
58protected:
59    const CommandLine& m_commandLine;
60};
61
62template<typename ChildProcessType, typename ChildProcessMainDelegateType>
63int ChildProcessMain(int argc, char** argv)
64{
65    CommandLine commandLine;
66    if (!commandLine.parse(argc, argv))
67        return EXIT_FAILURE;
68
69    ChildProcessMainDelegateType delegate(commandLine);
70
71    @autoreleasepool {
72        delegate.installSignalHandlers();
73        delegate.doPreInitializationWork();
74
75        InitializeWebKit2();
76
77        ChildProcessInitializationParameters parameters;
78        if (!delegate.getConnectionIdentifier(parameters.connectionIdentifier))
79            return EXIT_FAILURE;
80
81        if (!delegate.getClientIdentifier(parameters.clientIdentifier))
82            return EXIT_FAILURE;
83
84        if (!delegate.getClientProcessName(parameters.uiProcessName))
85            return EXIT_FAILURE;
86
87        if (!delegate.getExtraInitializationData(parameters.extraInitializationData))
88            return EXIT_FAILURE;
89
90        // FIXME: This should be moved to ChildProcessMac if it is still necessary.
91        String localization = commandLine["localization"];
92        RetainPtr<CFStringRef> cfLocalization = adoptCF(CFStringCreateWithCharacters(0, reinterpret_cast<const UniChar*>(localization.characters()), localization.length()));
93        if (cfLocalization)
94            WKSetDefaultLocalization(cfLocalization.get());
95
96        ChildProcessType::shared().initialize(parameters);
97    }
98
99    delegate.startRunLoop();
100
101    @autoreleasepool {
102        delegate.doPostRunWork();
103    }
104
105    return 0;
106}
107
108} // namespace WebKit
109
110#endif // ChildProcessEntryPoint_h
111