ChildProcessProxy.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (C) 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. #ifndef ChildProcessProxy_h
  26. #define ChildProcessProxy_h
  27. #include "Connection.h"
  28. #include "MessageReceiverMap.h"
  29. #include "ProcessLauncher.h"
  30. #include <wtf/ThreadSafeRefCounted.h>
  31. namespace WebKit {
  32. class ChildProcessProxy : ProcessLauncher::Client, public CoreIPC::Connection::Client, public ThreadSafeRefCounted<ChildProcessProxy> {
  33. WTF_MAKE_NONCOPYABLE(ChildProcessProxy);
  34. public:
  35. ChildProcessProxy();
  36. virtual ~ChildProcessProxy();
  37. // FIXME: This function does an unchecked upcast, and it is only used in a deprecated code path. Would like to get rid of it.
  38. static ChildProcessProxy* fromConnection(CoreIPC::Connection*);
  39. void connect();
  40. void terminate();
  41. template<typename T> bool send(const T& message, uint64_t destinationID, unsigned messageSendFlags = 0);
  42. template<typename U> bool sendSync(const U& message, const typename U::Reply&, uint64_t destinationID, double timeout = 1);
  43. CoreIPC::Connection* connection() const
  44. {
  45. ASSERT(m_connection);
  46. return m_connection.get();
  47. }
  48. void addMessageReceiver(CoreIPC::StringReference messageReceiverName, CoreIPC::MessageReceiver*);
  49. void addMessageReceiver(CoreIPC::StringReference messageReceiverName, uint64_t destinationID, CoreIPC::MessageReceiver*);
  50. void removeMessageReceiver(CoreIPC::StringReference messageReceiverName, uint64_t destinationID);
  51. bool isValid() const { return m_connection; }
  52. bool isLaunching() const;
  53. bool canSendMessage() const { return isValid() || isLaunching(); }
  54. PlatformProcessIdentifier processIdentifier() const { return m_processLauncher->processIdentifier(); }
  55. protected:
  56. void clearConnection();
  57. void abortProcessLaunchIfNeeded();
  58. // ProcessLauncher::Client
  59. virtual void didFinishLaunching(ProcessLauncher*, CoreIPC::Connection::Identifier) OVERRIDE;
  60. bool dispatchMessage(CoreIPC::Connection*, CoreIPC::MessageDecoder&);
  61. bool dispatchSyncMessage(CoreIPC::Connection*, CoreIPC::MessageDecoder&, OwnPtr<CoreIPC::MessageEncoder>&);
  62. private:
  63. virtual void getLaunchOptions(ProcessLauncher::LaunchOptions&) = 0;
  64. virtual void connectionWillOpen(CoreIPC::Connection*);
  65. virtual void connectionWillClose(CoreIPC::Connection*);
  66. bool sendMessage(PassOwnPtr<CoreIPC::MessageEncoder>, unsigned messageSendFlags);
  67. Vector<std::pair<OwnPtr<CoreIPC::MessageEncoder>, unsigned>> m_pendingMessages;
  68. RefPtr<ProcessLauncher> m_processLauncher;
  69. RefPtr<CoreIPC::Connection> m_connection;
  70. CoreIPC::MessageReceiverMap m_messageReceiverMap;
  71. };
  72. template<typename T>
  73. bool ChildProcessProxy::send(const T& message, uint64_t destinationID, unsigned messageSendFlags)
  74. {
  75. COMPILE_ASSERT(!T::isSync, AsyncMessageExpected);
  76. OwnPtr<CoreIPC::MessageEncoder> encoder = CoreIPC::MessageEncoder::create(T::receiverName(), T::name(), destinationID);
  77. encoder->encode(message);
  78. return sendMessage(encoder.release(), messageSendFlags);
  79. }
  80. template<typename U>
  81. bool ChildProcessProxy::sendSync(const U& message, const typename U::Reply& reply, uint64_t destinationID, double timeout)
  82. {
  83. COMPILE_ASSERT(U::isSync, SyncMessageExpected);
  84. if (!m_connection)
  85. return false;
  86. return connection()->sendSync(message, reply, destinationID, timeout);
  87. }
  88. } // namespace WebKit
  89. #endif // ChildProcessProxy_h