handle_win.cc 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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/handle_win.h"
  5. #include <utility>
  6. #include "base/memory/ref_counted.h"
  7. #include "base/notreached.h"
  8. #include "base/strings/string_number_conversions.h"
  9. #include "base/strings/stringprintf.h"
  10. #include "ipc/handle_attachment_win.h"
  11. #include "ipc/ipc_message.h"
  12. namespace IPC {
  13. HandleWin::HandleWin() : handle_(INVALID_HANDLE_VALUE) {}
  14. HandleWin::HandleWin(const HANDLE& handle) : handle_(handle) {}
  15. // static
  16. void ParamTraits<HandleWin>::Write(base::Pickle* m, const param_type& p) {
  17. scoped_refptr<IPC::internal::HandleAttachmentWin> attachment(
  18. new IPC::internal::HandleAttachmentWin(p.get_handle()));
  19. if (!m->WriteAttachment(std::move(attachment)))
  20. NOTREACHED();
  21. }
  22. // static
  23. bool ParamTraits<HandleWin>::Read(const base::Pickle* m,
  24. base::PickleIterator* iter,
  25. param_type* r) {
  26. scoped_refptr<base::Pickle::Attachment> base_attachment;
  27. if (!m->ReadAttachment(iter, &base_attachment))
  28. return false;
  29. MessageAttachment* attachment =
  30. static_cast<MessageAttachment*>(base_attachment.get());
  31. if (attachment->GetType() != MessageAttachment::Type::WIN_HANDLE)
  32. return false;
  33. IPC::internal::HandleAttachmentWin* handle_attachment =
  34. static_cast<IPC::internal::HandleAttachmentWin*>(attachment);
  35. r->set_handle(handle_attachment->Take());
  36. return true;
  37. }
  38. // static
  39. void ParamTraits<HandleWin>::Log(const param_type& p, std::string* l) {
  40. l->append(base::StringPrintf("0x%p", p.get_handle()));
  41. }
  42. } // namespace IPC