SharedWorkerProcess.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. #include "config.h"
  26. #include "SharedWorkerProcess.h"
  27. #if ENABLE(SHARED_WORKER_PROCESS)
  28. #include "ArgumentCoders.h"
  29. #include "Attachment.h"
  30. #include "SharedWorkerProcessCreationParameters.h"
  31. #include "SharedWorkerProcessProxyMessages.h"
  32. #include "WebProcessConnection.h"
  33. #include <WebCore/NotImplemented.h>
  34. #include <WebCore/RunLoop.h>
  35. #if PLATFORM(MAC)
  36. #include <crt_externs.h>
  37. #endif
  38. #if USE(UNIX_DOMAIN_SOCKETS)
  39. #include <errno.h>
  40. #include <fcntl.h>
  41. #include <sys/resource.h>
  42. #include <sys/socket.h>
  43. #include <unistd.h>
  44. #ifdef SOCK_SEQPACKET
  45. #define SOCKET_TYPE SOCK_SEQPACKET
  46. #else
  47. #if PLATFORM(GTK)
  48. #define SOCKET_TYPE SOCK_STREAM
  49. #else
  50. #define SOCKET_TYPE SOCK_DGRAM
  51. #endif
  52. #endif // SOCK_SEQPACKET
  53. #endif // USE(UNIX_DOMAIN_SOCKETS)
  54. using namespace WebCore;
  55. namespace WebKit {
  56. SharedWorkerProcess& SharedWorkerProcess::shared()
  57. {
  58. DEFINE_STATIC_LOCAL(SharedWorkerProcess, process, ());
  59. return process;
  60. }
  61. SharedWorkerProcess::SharedWorkerProcess()
  62. : m_minimumLifetimeTimer(RunLoop::main(), this, &SharedWorkerProcess::minimumLifetimeTimerFired)
  63. {
  64. }
  65. SharedWorkerProcess::~SharedWorkerProcess()
  66. {
  67. }
  68. void SharedWorkerProcess::removeWebProcessConnection(WebProcessConnection* webProcessConnection)
  69. {
  70. size_t vectorIndex = m_webProcessConnections.find(webProcessConnection);
  71. ASSERT(vectorIndex != notFound);
  72. m_webProcessConnections.remove(vectorIndex);
  73. enableTermination();
  74. }
  75. bool SharedWorkerProcess::shouldTerminate()
  76. {
  77. ASSERT(m_webProcessConnections.isEmpty());
  78. return true;
  79. }
  80. void SharedWorkerProcess::didReceiveMessage(CoreIPC::Connection* connection, CoreIPC::MessageDecoder& decoder)
  81. {
  82. didReceiveSharedWorkerProcessMessage(connection, decoder);
  83. }
  84. void SharedWorkerProcess::didClose(CoreIPC::Connection*)
  85. {
  86. // The UI process has crashed, just go ahead and quit.
  87. RunLoop::current()->stop();
  88. }
  89. void SharedWorkerProcess::didReceiveInvalidMessage(CoreIPC::Connection*, CoreIPC::StringReference, CoreIPC::StringReference)
  90. {
  91. }
  92. void SharedWorkerProcess::initializeSharedWorkerProcess(const SharedWorkerProcessCreationParameters& parameters)
  93. {
  94. setMinimumLifetime(parameters.minimumLifetime);
  95. setTerminationTimeout(parameters.terminationTimeout);
  96. }
  97. void SharedWorkerProcess::createWebProcessConnection()
  98. {
  99. #if PLATFORM(MAC)
  100. // Create the listening port.
  101. mach_port_t listeningPort;
  102. mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &listeningPort);
  103. // Create a listening connection.
  104. RefPtr<WebProcessConnection> connection = WebProcessConnection::create(CoreIPC::Connection::Identifier(listeningPort));
  105. m_webProcessConnections.append(connection.release());
  106. CoreIPC::Attachment clientPort(listeningPort, MACH_MSG_TYPE_MAKE_SEND);
  107. parentProcessConnection()->send(Messages::SharedWorkerProcessProxy::DidCreateWebProcessConnection(clientPort), 0);
  108. #elif USE(UNIX_DOMAIN_SOCKETS)
  109. int sockets[2];
  110. if (socketpair(AF_UNIX, SOCKET_TYPE, 0, sockets) == -1) {
  111. ASSERT_NOT_REACHED();
  112. return;
  113. }
  114. // Don't expose the shared worker process socket to the web process.
  115. while (fcntl(sockets[1], F_SETFD, FD_CLOEXEC) == -1) {
  116. if (errno != EINTR) {
  117. ASSERT_NOT_REACHED();
  118. while (close(sockets[0]) == -1 && errno == EINTR) { }
  119. while (close(sockets[1]) == -1 && errno == EINTR) { }
  120. return;
  121. }
  122. }
  123. // Don't expose the shared worker process socket to possible future web processes.
  124. while (fcntl(sockets[0], F_SETFD, FD_CLOEXEC) == -1) {
  125. if (errno != EINTR) {
  126. ASSERT_NOT_REACHED();
  127. while (close(sockets[0]) == -1 && errno == EINTR) { }
  128. while (close(sockets[1]) == -1 && errno == EINTR) { }
  129. return;
  130. }
  131. }
  132. RefPtr<WebProcessConnection> connection = WebProcessConnection::create(sockets[1]);
  133. m_webProcessConnections.append(connection.release());
  134. CoreIPC::Attachment clientSocket(sockets[0]);
  135. parentProcessConnection()->send(Messages::SharedWorkerProcessProxy::DidCreateWebProcessConnection(clientSocket), 0);
  136. #else
  137. notImplemented();
  138. #endif
  139. disableTermination();
  140. }
  141. void SharedWorkerProcess::setMinimumLifetime(double lifetime)
  142. {
  143. if (lifetime <= 0.0)
  144. return;
  145. disableTermination();
  146. m_minimumLifetimeTimer.startOneShot(lifetime);
  147. }
  148. void SharedWorkerProcess::minimumLifetimeTimerFired()
  149. {
  150. enableTermination();
  151. }
  152. #if !PLATFORM(MAC)
  153. void SharedWorkerProcess::initializeProcess(const ChildProcessInitializationParameters&)
  154. {
  155. }
  156. void SharedWorkerProcess::initializeProcessName(const ChildProcessInitializationParameters&)
  157. {
  158. }
  159. #endif
  160. } // namespace WebKit
  161. #endif // ENABLE(SHARED_WORKER_PROCESS)