ipc_mojo_param_traits.cc 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ipc/ipc_mojo_param_traits.h"
  5. #include "base/logging.h"
  6. #include "ipc/ipc_message_utils.h"
  7. #include "ipc/ipc_mojo_handle_attachment.h"
  8. #include "ipc/ipc_mojo_message_helper.h"
  9. namespace IPC {
  10. void ParamTraits<mojo::MessagePipeHandle>::Write(base::Pickle* m,
  11. const param_type& p) {
  12. WriteParam(m, p.is_valid());
  13. if (p.is_valid())
  14. MojoMessageHelper::WriteMessagePipeTo(m, mojo::ScopedMessagePipeHandle(p));
  15. }
  16. bool ParamTraits<mojo::MessagePipeHandle>::Read(const base::Pickle* m,
  17. base::PickleIterator* iter,
  18. param_type* r) {
  19. bool is_valid;
  20. if (!ReadParam(m, iter, &is_valid))
  21. return false;
  22. if (!is_valid)
  23. return true;
  24. mojo::ScopedMessagePipeHandle handle;
  25. if (!MojoMessageHelper::ReadMessagePipeFrom(m, iter, &handle))
  26. return false;
  27. DCHECK(handle.is_valid());
  28. *r = handle.release();
  29. return true;
  30. }
  31. void ParamTraits<mojo::MessagePipeHandle>::Log(const param_type& p,
  32. std::string* l) {
  33. l->append("mojo::MessagePipeHandle(");
  34. LogParam(static_cast<uint64_t>(p.value()), l);
  35. l->append(")");
  36. }
  37. void ParamTraits<mojo::DataPipeConsumerHandle>::Write(base::Pickle* m,
  38. const param_type& p) {
  39. WriteParam(m, p.is_valid());
  40. if (!p.is_valid())
  41. return;
  42. m->WriteAttachment(new internal::MojoHandleAttachment(
  43. mojo::ScopedHandle::From(mojo::ScopedDataPipeConsumerHandle(p))));
  44. }
  45. bool ParamTraits<mojo::DataPipeConsumerHandle>::Read(const base::Pickle* m,
  46. base::PickleIterator* iter,
  47. param_type* r) {
  48. bool is_valid;
  49. if (!ReadParam(m, iter, &is_valid))
  50. return false;
  51. if (!is_valid)
  52. return true;
  53. scoped_refptr<base::Pickle::Attachment> attachment;
  54. if (!m->ReadAttachment(iter, &attachment)) {
  55. DLOG(ERROR) << "Failed to read attachment for message pipe.";
  56. return false;
  57. }
  58. MessageAttachment::Type type =
  59. static_cast<MessageAttachment*>(attachment.get())->GetType();
  60. if (type != MessageAttachment::Type::MOJO_HANDLE) {
  61. DLOG(ERROR) << "Unexpected attachment type:" << static_cast<int>(type);
  62. return false;
  63. }
  64. mojo::ScopedDataPipeConsumerHandle handle;
  65. handle.reset(mojo::DataPipeConsumerHandle(
  66. static_cast<internal::MojoHandleAttachment*>(attachment.get())
  67. ->TakeHandle()
  68. .release()
  69. .value()));
  70. DCHECK(handle.is_valid());
  71. *r = handle.release();
  72. return true;
  73. }
  74. void ParamTraits<mojo::DataPipeConsumerHandle>::Log(const param_type& p,
  75. std::string* l) {
  76. l->append("mojo::DataPipeConsumerHandle(");
  77. LogParam(static_cast<uint64_t>(p.value()), l);
  78. l->append(")");
  79. }
  80. } // namespace IPC