1/*
2 * Copyright (C) 2011, 2014 Igalia S.L.
3 * Copyright (C) 2011 Apple Inc.
4 * Copyright (C) 2012 Samsung Electronics
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
19 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
25 * THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "config.h"
29#include "PluginProcessMainUnix.h"
30
31#if ENABLE(PLUGIN_PROCESS)
32
33#include "ChildProcessMain.h"
34#include "Logging.h"
35#include "NetscapePlugin.h"
36#include "PluginProcess.h"
37#include <WebCore/FileSystem.h>
38#include <stdlib.h>
39#include <wtf/text/CString.h>
40
41#if PLATFORM(GTK)
42#include <gtk/gtk.h>
43#elif PLATFORM(EFL) && HAVE_ECORE_X
44#include <Ecore_X.h>
45#endif
46
47namespace WebKit {
48
49#if defined(XP_UNIX)
50
51#if !LOG_DISABLED
52static const char xErrorString[] = "The program '%s' received an X Window System error.\n"
53    "This probably reflects a bug in a browser plugin.\n"
54    "The error was '%s'.\n"
55    "  (Details: serial %ld error_code %d request_code %d minor_code %d)\n";
56#endif // !LOG_DISABLED
57
58static CString programName;
59
60static int webkitXError(Display* xdisplay, XErrorEvent* error)
61{
62    char errorMessage[64];
63    XGetErrorText(xdisplay, error->error_code, errorMessage, 63);
64
65    LOG(Plugins, xErrorString, programName.data(), errorMessage, error->serial, error->error_code, error->request_code, error->minor_code);
66
67    return 0;
68}
69#endif // XP_UNIX
70
71class PluginProcessMain final: public ChildProcessMainBase {
72public:
73    bool platformInitialize() override
74    {
75#if PLATFORM(GTK)
76        gtk_init(nullptr, nullptr);
77#elif PLATFORM(EFL)
78#ifdef HAVE_ECORE_X
79        if (!ecore_x_init(0))
80#endif
81            return false;
82#endif
83
84        return true;
85    }
86
87    bool parseCommandLine(int argc, char** argv) override
88    {
89        ASSERT(argc == 3);
90        if (argc != 3)
91            return false;
92
93        if (!strcmp(argv[1], "-scanPlugin"))
94#if PLUGIN_ARCHITECTURE(X11)
95            exit(NetscapePluginModule::scanPlugin(argv[2]) ? EXIT_SUCCESS : EXIT_FAILURE);
96#else
97            exit(EXIT_FAILURE);
98#endif
99
100#if defined(XP_UNIX)
101        programName = WebCore::pathGetFileName(argv[0]).utf8();
102        XSetErrorHandler(webkitXError);
103#endif
104
105        m_parameters.extraInitializationData.add("plugin-path", argv[2]);
106        return ChildProcessMainBase::parseCommandLine(argc, argv);
107    }
108};
109
110int PluginProcessMainUnix(int argc, char** argv)
111{
112    return ChildProcessMain<PluginProcess, PluginProcessMain>(argc, argv);
113}
114
115} // namespace WebKit
116
117#endif
118