ChildProcess.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. #include "config.h"
  26. #include "ChildProcess.h"
  27. #include "SandboxInitializationParameters.h"
  28. #if !OS(WINDOWS)
  29. #include <unistd.h>
  30. #endif
  31. #if OS(PSP2)
  32. inline void _exit(int) { }
  33. #endif
  34. using namespace WebCore;
  35. namespace WebKit {
  36. ChildProcess::ChildProcess()
  37. : m_terminationTimeout(0)
  38. , m_terminationCounter(0)
  39. , m_terminationTimer(RunLoop::main(), this, &ChildProcess::terminationTimerFired)
  40. #if PLATFORM(MAC)
  41. , m_activeTaskCount(0)
  42. , m_shouldSuspend(false)
  43. , m_suspensionHysteresisTimer(RunLoop::main(), this, &ChildProcess::suspensionHysteresisTimerFired)
  44. #endif
  45. {
  46. }
  47. ChildProcess::~ChildProcess()
  48. {
  49. }
  50. NO_RETURN static void watchdogCallback()
  51. {
  52. // We use _exit here since the watchdog callback is called from another thread and we don't want
  53. // global destructors or atexit handlers to be called from this thread while the main thread is busy
  54. // doing its thing.
  55. _exit(EXIT_FAILURE);
  56. }
  57. static void didCloseOnConnectionWorkQueue(CoreIPC::Connection*)
  58. {
  59. // If the connection has been closed and we haven't responded in the main thread for 10 seconds
  60. // the process will exit forcibly.
  61. const double watchdogDelay = 10;
  62. WorkQueue::create("com.apple.WebKit.ChildProcess.WatchDogQueue")->dispatchAfterDelay(bind(static_cast<void(*)()>(watchdogCallback)), watchdogDelay);
  63. }
  64. void ChildProcess::initialize(const ChildProcessInitializationParameters& parameters)
  65. {
  66. platformInitialize();
  67. initializeProcess(parameters);
  68. initializeProcessName(parameters);
  69. SandboxInitializationParameters sandboxParameters;
  70. initializeSandbox(parameters, sandboxParameters);
  71. m_connection = CoreIPC::Connection::createClientConnection(parameters.connectionIdentifier, this, RunLoop::main());
  72. m_connection->setDidCloseOnConnectionWorkQueueCallback(didCloseOnConnectionWorkQueue);
  73. initializeConnection(m_connection.get());
  74. m_connection->open();
  75. }
  76. void ChildProcess::initializeProcess(const ChildProcessInitializationParameters&)
  77. {
  78. }
  79. void ChildProcess::initializeProcessName(const ChildProcessInitializationParameters&)
  80. {
  81. }
  82. void ChildProcess::initializeConnection(CoreIPC::Connection*)
  83. {
  84. }
  85. void ChildProcess::addMessageReceiver(CoreIPC::StringReference messageReceiverName, CoreIPC::MessageReceiver* messageReceiver)
  86. {
  87. m_messageReceiverMap.addMessageReceiver(messageReceiverName, messageReceiver);
  88. }
  89. void ChildProcess::addMessageReceiver(CoreIPC::StringReference messageReceiverName, uint64_t destinationID, CoreIPC::MessageReceiver* messageReceiver)
  90. {
  91. m_messageReceiverMap.addMessageReceiver(messageReceiverName, destinationID, messageReceiver);
  92. }
  93. void ChildProcess::removeMessageReceiver(CoreIPC::StringReference messageReceiverName, uint64_t destinationID)
  94. {
  95. m_messageReceiverMap.removeMessageReceiver(messageReceiverName, destinationID);
  96. }
  97. void ChildProcess::disableTermination()
  98. {
  99. m_terminationCounter++;
  100. m_terminationTimer.stop();
  101. }
  102. void ChildProcess::enableTermination()
  103. {
  104. ASSERT(m_terminationCounter > 0);
  105. m_terminationCounter--;
  106. if (m_terminationCounter)
  107. return;
  108. if (!m_terminationTimeout) {
  109. terminationTimerFired();
  110. return;
  111. }
  112. m_terminationTimer.startOneShot(m_terminationTimeout);
  113. }
  114. CoreIPC::Connection* ChildProcess::messageSenderConnection()
  115. {
  116. return m_connection.get();
  117. }
  118. uint64_t ChildProcess::messageSenderDestinationID()
  119. {
  120. return 0;
  121. }
  122. void ChildProcess::terminationTimerFired()
  123. {
  124. if (!shouldTerminate())
  125. return;
  126. terminate();
  127. }
  128. void ChildProcess::terminate()
  129. {
  130. m_connection->invalidate();
  131. RunLoop::main()->stop();
  132. }
  133. #if !PLATFORM(MAC)
  134. void ChildProcess::platformInitialize()
  135. {
  136. }
  137. void ChildProcess::initializeSandbox(const ChildProcessInitializationParameters&, SandboxInitializationParameters&)
  138. {
  139. }
  140. #endif
  141. } // namespace WebKit