WebData.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (C) 2010 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 WebData_h
  26. #define WebData_h
  27. #include "APIObject.h"
  28. #include "DataReference.h"
  29. #include <wtf/Forward.h>
  30. #include <wtf/Vector.h>
  31. namespace WebKit {
  32. // WebData - A data buffer type suitable for vending to an API.
  33. class WebData : public TypedAPIObject<APIObject::TypeData> {
  34. public:
  35. typedef void (*FreeDataFunction)(unsigned char*, const void* context);
  36. static PassRefPtr<WebData> createWithoutCopying(const unsigned char* bytes, size_t size, FreeDataFunction freeDataFunction, const void* context)
  37. {
  38. return adoptRef(new WebData(bytes, size, freeDataFunction, context));
  39. }
  40. static PassRefPtr<WebData> create(const unsigned char* bytes, size_t size)
  41. {
  42. unsigned char *copiedBytes = 0;
  43. if (size) {
  44. copiedBytes = static_cast<unsigned char*>(fastMalloc(size));
  45. memcpy(copiedBytes, bytes, size);
  46. }
  47. return createWithoutCopying(copiedBytes, size, fastFreeBytes, 0);
  48. }
  49. static PassRefPtr<WebData> create(const Vector<unsigned char>& buffer)
  50. {
  51. return create(buffer.data(), buffer.size());
  52. }
  53. ~WebData()
  54. {
  55. m_freeDataFunction(const_cast<unsigned char*>(m_bytes), m_context);
  56. }
  57. const unsigned char* bytes() const { return m_bytes; }
  58. size_t size() const { return m_size; }
  59. CoreIPC::DataReference dataReference() const { return CoreIPC::DataReference(m_bytes, m_size); }
  60. private:
  61. WebData(const unsigned char* bytes, size_t size, FreeDataFunction freeDataFunction, const void* context)
  62. : m_bytes(bytes)
  63. , m_size(size)
  64. , m_freeDataFunction(freeDataFunction)
  65. , m_context(context)
  66. {
  67. }
  68. static void fastFreeBytes(unsigned char* bytes, const void*)
  69. {
  70. if (bytes)
  71. fastFree(static_cast<void*>(bytes));
  72. }
  73. const unsigned char* m_bytes;
  74. size_t m_size;
  75. FreeDataFunction m_freeDataFunction;
  76. const void* m_context;
  77. };
  78. } // namespace WebKit
  79. #endif // WebData_h