ChildProcess.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #ifndef ChildProcess_h
  26. #define ChildProcess_h
  27. #include "Connection.h"
  28. #include "MessageReceiverMap.h"
  29. #include "MessageSender.h"
  30. #include <WebCore/RunLoop.h>
  31. #include <wtf/HashMap.h>
  32. #include <wtf/RetainPtr.h>
  33. #include <wtf/text/StringHash.h>
  34. #include <wtf/text/WTFString.h>
  35. namespace WebKit {
  36. class SandboxInitializationParameters;
  37. struct ChildProcessInitializationParameters {
  38. String uiProcessName;
  39. String clientIdentifier;
  40. CoreIPC::Connection::Identifier connectionIdentifier;
  41. HashMap<String, String> extraInitializationData;
  42. };
  43. class ChildProcess : protected CoreIPC::Connection::Client, public CoreIPC::MessageSender {
  44. WTF_MAKE_NONCOPYABLE(ChildProcess);
  45. public:
  46. void initialize(const ChildProcessInitializationParameters&);
  47. // disable and enable termination of the process. when disableTermination is called, the
  48. // process won't terminate unless a corresponding disableTermination call is made.
  49. void disableTermination();
  50. void enableTermination();
  51. void addMessageReceiver(CoreIPC::StringReference messageReceiverName, CoreIPC::MessageReceiver*);
  52. void addMessageReceiver(CoreIPC::StringReference messageReceiverName, uint64_t destinationID, CoreIPC::MessageReceiver*);
  53. void removeMessageReceiver(CoreIPC::StringReference messageReceiverName, uint64_t destinationID);
  54. #if PLATFORM(MAC)
  55. void setProcessSuppressionEnabled(bool);
  56. bool processSuppressionEnabled() const { return !m_processSuppressionAssertion; }
  57. void incrementActiveTaskCount();
  58. void decrementActiveTaskCount();
  59. void setApplicationIsDaemon();
  60. #else
  61. void incrementActiveTaskCount() { }
  62. void decrementActiveTaskCount() { }
  63. #endif
  64. CoreIPC::Connection* parentProcessConnection() const { return m_connection.get(); }
  65. CoreIPC::MessageReceiverMap& messageReceiverMap() { return m_messageReceiverMap; }
  66. protected:
  67. explicit ChildProcess();
  68. virtual ~ChildProcess();
  69. void setTerminationTimeout(double seconds) { m_terminationTimeout = seconds; }
  70. virtual void initializeProcess(const ChildProcessInitializationParameters&);
  71. virtual void initializeProcessName(const ChildProcessInitializationParameters&);
  72. virtual void initializeSandbox(const ChildProcessInitializationParameters&, SandboxInitializationParameters&);
  73. virtual void initializeConnection(CoreIPC::Connection*);
  74. virtual bool shouldTerminate() = 0;
  75. virtual void terminate();
  76. private:
  77. // CoreIPC::MessageSender
  78. virtual CoreIPC::Connection* messageSenderConnection() OVERRIDE;
  79. virtual uint64_t messageSenderDestinationID() OVERRIDE;
  80. void terminationTimerFired();
  81. void platformInitialize();
  82. // The timeout, in seconds, before this process will be terminated if termination
  83. // has been enabled. If the timeout is 0 seconds, the process will be terminated immediately.
  84. double m_terminationTimeout;
  85. // A termination counter; when the counter reaches zero, the process will be terminated
  86. // after a given period of time.
  87. unsigned m_terminationCounter;
  88. WebCore::RunLoop::Timer<ChildProcess> m_terminationTimer;
  89. RefPtr<CoreIPC::Connection> m_connection;
  90. CoreIPC::MessageReceiverMap m_messageReceiverMap;
  91. #if PLATFORM(MAC)
  92. void suspensionHysteresisTimerFired();
  93. void setProcessSuppressionEnabledInternal(bool);
  94. size_t m_activeTaskCount;
  95. bool m_shouldSuspend;
  96. WebCore::RunLoop::Timer<ChildProcess> m_suspensionHysteresisTimer;
  97. RetainPtr<id> m_processSuppressionAssertion;
  98. #endif
  99. };
  100. } // namespace WebKit
  101. #endif // ChildProcess_h