1/*
2 * Copyright (C) 2010, 2011 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#import "config.h"
27
28#if ENABLE(NETSCAPE_PLUGIN_API)
29
30#import "ChildProcessEntryPoint.h"
31#import "EnvironmentUtilities.h"
32#import "NetscapePluginModule.h"
33#import "PluginProcess.h"
34#import "WKBase.h"
35#import <wtf/RunLoop.h>
36
37#if USE(APPKIT)
38@interface NSApplication (WebNSApplicationDetails)
39-(void)_installAutoreleasePoolsOnCurrentThreadIfNecessary;
40@end
41#endif
42
43using namespace WebCore;
44
45namespace WebKit {
46
47class PluginProcessMainDelegate : public ChildProcessMainDelegate {
48public:
49    PluginProcessMainDelegate(const CommandLine& commandLine)
50        : ChildProcessMainDelegate(commandLine)
51    {
52    }
53
54    virtual void doPreInitializationWork()
55    {
56        // Remove the PluginProcess shim from the DYLD_INSERT_LIBRARIES environment variable so any processes
57        // spawned by the PluginProcess don't try to insert the shim and crash.
58        EnvironmentUtilities::stripValuesEndingWithString("DYLD_INSERT_LIBRARIES", "/PluginProcessShim.dylib");
59
60#if USE(APPKIT)
61#if defined(__i386__)
62        // We must set the state of AppleMagnifiedMode before NSApplication initialization so that the value will be in
63        // place before Cocoa startup logic runs and caches the value.
64        NSDictionary *defaults = [[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithBool:YES], @"AppleMagnifiedMode", nil];
65        [[NSUserDefaults standardUserDefaults] registerDefaults:defaults];
66        [defaults release];
67#endif
68
69        // Initialize AppKit.
70        [NSApplication sharedApplication];
71
72        // Installs autorelease pools on the current runloop which prevents memory from accumulating between user events.
73        // FIXME: Remove when <rdar://problem/8929426> is fixed.
74        [NSApp _installAutoreleasePoolsOnCurrentThreadIfNecessary];
75#endif
76
77        // Check if we're being spawned to write a MIME type preferences file.
78        String pluginPath = m_commandLine["createPluginMIMETypesPreferences"];
79        if (!pluginPath.isEmpty()) {
80            // We are never going to get to the actual initialization, so initialize WebKit2 now.
81            InitializeWebKit2();
82
83            if (!NetscapePluginModule::createPluginMIMETypesPreferences(pluginPath))
84                exit(EXIT_FAILURE);
85            exit(EXIT_SUCCESS);
86        }
87    }
88
89    virtual bool getExtraInitializationData(HashMap<String, String>& extraInitializationData)
90    {
91        String pluginPath = m_commandLine["plugin-path"];
92        if (pluginPath.isEmpty())
93            return false;
94        extraInitializationData.add("plugin-path", pluginPath);
95
96        String disableSandbox = m_commandLine["disable-sandbox"];
97        if (!disableSandbox.isEmpty())
98            extraInitializationData.add("disable-sandbox", disableSandbox);
99
100        return true;
101    }
102
103    virtual void startRunLoop() override
104    {
105        ASSERT(NSApp);
106        [NSApp run];
107    }
108
109    virtual void doPostRunWork()
110    {
111#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
112        // If we have private temporary and cache directories, clean them up.
113        if (getenv("DIRHELPER_USER_DIR_SUFFIX")) {
114            char darwinDirectory[PATH_MAX];
115            if (confstr(_CS_DARWIN_USER_TEMP_DIR, darwinDirectory, sizeof(darwinDirectory)))
116                [[NSFileManager defaultManager] removeItemAtPath:[[NSFileManager defaultManager] stringWithFileSystemRepresentation:darwinDirectory length:strlen(darwinDirectory)] error:nil];
117            if (confstr(_CS_DARWIN_USER_CACHE_DIR, darwinDirectory, sizeof(darwinDirectory)))
118                [[NSFileManager defaultManager] removeItemAtPath:[[NSFileManager defaultManager] stringWithFileSystemRepresentation:darwinDirectory length:strlen(darwinDirectory)] error:nil];
119        }
120#endif
121    }
122};
123
124} // namespace WebKit
125
126using namespace WebKit;
127
128extern "C" WK_EXPORT int PluginProcessMain(int argc, char** argv);
129
130int PluginProcessMain(int argc, char** argv)
131{
132    return ChildProcessMain<PluginProcess, PluginProcessMainDelegate>(argc, argv);
133}
134
135#endif // ENABLE(NETSCAPE_PLUGIN_API)
136