ShareableResource.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. #include "config.h"
  26. #include "ShareableResource.h"
  27. #include "ArgumentCoders.h"
  28. #include <WebCore/SharedBuffer.h>
  29. using namespace WebCore;
  30. namespace WebKit {
  31. ShareableResource::Handle::Handle()
  32. {
  33. }
  34. void ShareableResource::Handle::encode(CoreIPC::ArgumentEncoder& encoder) const
  35. {
  36. encoder << m_handle;
  37. encoder << m_offset;
  38. encoder << m_size;
  39. }
  40. bool ShareableResource::Handle::decode(CoreIPC::ArgumentDecoder& decoder, Handle& handle)
  41. {
  42. if (!decoder.decode(handle.m_handle))
  43. return false;
  44. if (!decoder.decode(handle.m_offset))
  45. return false;
  46. if (!decoder.decode(handle.m_size))
  47. return false;
  48. return true;
  49. }
  50. static void shareableResourceDeallocate(void *ptr, void *info)
  51. {
  52. (static_cast<ShareableResource*>(info))->deref(); // Balanced by ref() in createShareableResourceDeallocator()
  53. }
  54. static CFAllocatorRef createShareableResourceDeallocator(ShareableResource* resource)
  55. {
  56. resource->ref(); // Balanced by deref in shareableResourceDeallocate()
  57. CFAllocatorContext context = { 0,
  58. resource,
  59. NULL, // retain
  60. NULL, // release
  61. NULL, // copyDescription
  62. NULL, // allocate
  63. NULL, // reallocate
  64. shareableResourceDeallocate,
  65. NULL, // preferredSize
  66. };
  67. return CFAllocatorCreate(kCFAllocatorDefault, &context);
  68. }
  69. PassRefPtr<SharedBuffer> ShareableResource::Handle::tryWrapInSharedBuffer() const
  70. {
  71. RefPtr<ShareableResource> resource = ShareableResource::create(*this);
  72. if (!resource) {
  73. LOG_ERROR("Failed to recreate ShareableResource from handle.");
  74. return 0;
  75. }
  76. RetainPtr<CFAllocatorRef> deallocator = adoptCF(createShareableResourceDeallocator(resource.get()));
  77. RetainPtr<CFDataRef> data = adoptCF(CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, reinterpret_cast<const UInt8*>(resource->data()), static_cast<CFIndex>(resource->size()), deallocator.get()));
  78. return SharedBuffer::wrapCFData(data.get());
  79. }
  80. PassRefPtr<ShareableResource> ShareableResource::create(PassRefPtr<SharedMemory> sharedMemory, unsigned offset, unsigned size)
  81. {
  82. return adoptRef(new ShareableResource(sharedMemory, offset, size));
  83. }
  84. PassRefPtr<ShareableResource> ShareableResource::create(const Handle& handle)
  85. {
  86. RefPtr<SharedMemory> sharedMemory = SharedMemory::create(handle.m_handle, SharedMemory::ReadOnly);
  87. if (!sharedMemory)
  88. return 0;
  89. return create(sharedMemory.release(), handle.m_offset, handle.m_size);
  90. }
  91. ShareableResource::ShareableResource(PassRefPtr<SharedMemory> sharedMemory, unsigned offset, unsigned size)
  92. : m_sharedMemory(sharedMemory)
  93. , m_offset(offset)
  94. , m_size(size)
  95. {
  96. ASSERT(m_sharedMemory);
  97. ASSERT(m_offset + m_size <= m_sharedMemory->size());
  98. // FIXME (NetworkProcess): This data was received from another process. If it is bogus, should we assume that process is compromised and we should kill it?
  99. }
  100. ShareableResource::~ShareableResource()
  101. {
  102. }
  103. bool ShareableResource::createHandle(Handle& handle)
  104. {
  105. if (!m_sharedMemory->createHandle(handle.m_handle, SharedMemory::ReadOnly))
  106. return false;
  107. handle.m_offset = m_offset;
  108. handle.m_size = m_size;
  109. return true;
  110. }
  111. const char* ShareableResource::data() const
  112. {
  113. return static_cast<const char*>(m_sharedMemory->data()) + m_offset;
  114. }
  115. unsigned ShareableResource::size() const
  116. {
  117. return m_size;
  118. }
  119. } // namespace WebKit