| 1 | /* |
| 2 | * Copyright (C) 2010, 2012 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 | #pragma once |
| 27 | |
| 28 | #include "Connection.h" |
| 29 | #include <WebCore/ProcessIdentifier.h> |
| 30 | #include <wtf/HashMap.h> |
| 31 | #include <wtf/ProcessID.h> |
| 32 | #include <wtf/RefPtr.h> |
| 33 | #include <wtf/Threading.h> |
| 34 | #include <wtf/WeakPtr.h> |
| 35 | #include <wtf/text/StringHash.h> |
| 36 | #include <wtf/text/WTFString.h> |
| 37 | |
| 38 | #if PLATFORM(WIN) |
| 39 | #include <wtf/win/Win32Handle.h> |
| 40 | #endif |
| 41 | |
| 42 | namespace WebKit { |
| 43 | |
| 44 | #if PLATFORM(GTK) || PLATFORM(WPE) |
| 45 | enum class SandboxPermission { |
| 46 | ReadOnly, |
| 47 | ReadWrite, |
| 48 | }; |
| 49 | #endif |
| 50 | |
| 51 | class ProcessLauncher : public ThreadSafeRefCounted<ProcessLauncher>, public CanMakeWeakPtr<ProcessLauncher> { |
| 52 | public: |
| 53 | class Client { |
| 54 | public: |
| 55 | virtual ~Client() { } |
| 56 | |
| 57 | virtual void didFinishLaunching(ProcessLauncher*, IPC::Connection::Identifier) = 0; |
| 58 | virtual bool isJITEnabled() const { return true; } |
| 59 | }; |
| 60 | |
| 61 | enum class ProcessType { |
| 62 | Web, |
| 63 | #if ENABLE(NETSCAPE_PLUGIN_API) |
| 64 | Plugin32, |
| 65 | Plugin64, |
| 66 | #endif |
| 67 | Network |
| 68 | }; |
| 69 | |
| 70 | struct LaunchOptions { |
| 71 | ProcessType processType; |
| 72 | WebCore::ProcessIdentifier processIdentifier; |
| 73 | HashMap<String, String> ; |
| 74 | bool nonValidInjectedCodeAllowed { false }; |
| 75 | bool shouldMakeProcessLaunchFailForTesting { false }; |
| 76 | CString customWebContentServiceBundleIdentifier; |
| 77 | |
| 78 | #if PLATFORM(GTK) || PLATFORM(WPE) |
| 79 | HashMap<CString, SandboxPermission> extraWebProcessSandboxPaths; |
| 80 | #if ENABLE(DEVELOPER_MODE) |
| 81 | String processCmdPrefix; |
| 82 | #endif |
| 83 | #endif |
| 84 | }; |
| 85 | |
| 86 | static Ref<ProcessLauncher> create(Client* client, const LaunchOptions& launchOptions) |
| 87 | { |
| 88 | return adoptRef(*new ProcessLauncher(client, launchOptions)); |
| 89 | } |
| 90 | |
| 91 | bool isLaunching() const { return m_isLaunching; } |
| 92 | ProcessID processIdentifier() const { return m_processIdentifier; } |
| 93 | |
| 94 | void terminateProcess(); |
| 95 | void invalidate(); |
| 96 | |
| 97 | private: |
| 98 | ProcessLauncher(Client*, const LaunchOptions& launchOptions); |
| 99 | |
| 100 | void launchProcess(); |
| 101 | void didFinishLaunchingProcess(ProcessID, IPC::Connection::Identifier); |
| 102 | |
| 103 | void platformInvalidate(); |
| 104 | |
| 105 | Client* m_client; |
| 106 | |
| 107 | #if PLATFORM(COCOA) |
| 108 | OSObjectPtr<xpc_connection_t> m_xpcConnection; |
| 109 | #endif |
| 110 | |
| 111 | #if PLATFORM(WIN) |
| 112 | WTF::Win32Handle m_hProcess; |
| 113 | #endif |
| 114 | |
| 115 | const LaunchOptions m_launchOptions; |
| 116 | bool m_isLaunching; |
| 117 | ProcessID m_processIdentifier; |
| 118 | }; |
| 119 | |
| 120 | } // namespace WebKit |
| 121 | |