BlobRegistrationData.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (C) 2013 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 "BlobRegistrationData.h"
  27. #if ENABLE(BLOB) && ENABLE(NETWORK_PROCESS)
  28. #include "ArgumentCoders.h"
  29. #include "DataReference.h"
  30. #include "WebCoreArgumentCoders.h"
  31. #include <WebCore/BlobData.h>
  32. using namespace WebCore;
  33. namespace WebKit {
  34. BlobRegistrationData::BlobRegistrationData()
  35. {
  36. }
  37. BlobRegistrationData::BlobRegistrationData(PassOwnPtr<BlobData> data)
  38. : m_data(data)
  39. {
  40. const BlobDataItemList& items = m_data->items();
  41. size_t fileCount = 0;
  42. for (size_t i = 0, count = items.size(); i < count; ++i) {
  43. // File path can be empty when submitting a form file input without a file, see bug 111778.
  44. if (items[i].type == BlobDataItem::File && !items[i].path.isEmpty())
  45. ++fileCount;
  46. }
  47. m_sandboxExtensions.allocate(fileCount);
  48. size_t extensionIndex = 0;
  49. for (size_t i = 0, count = items.size(); i < count; ++i) {
  50. const BlobDataItem& item = items[i];
  51. if (item.type == BlobDataItem::File && !items[i].path.isEmpty())
  52. SandboxExtension::createHandle(item.path, SandboxExtension::ReadOnly, m_sandboxExtensions[extensionIndex++]);
  53. }
  54. }
  55. BlobRegistrationData::~BlobRegistrationData()
  56. {
  57. }
  58. PassOwnPtr<BlobData> BlobRegistrationData::releaseData() const
  59. {
  60. return m_data.release();
  61. }
  62. void BlobRegistrationData::encode(CoreIPC::ArgumentEncoder& encoder) const
  63. {
  64. encoder << m_data->contentType();
  65. encoder << m_data->contentDisposition();
  66. const BlobDataItemList& items = m_data->items();
  67. encoder << static_cast<uint64_t>(items.size());
  68. for (size_t i = 0, count = items.size(); i < count; ++i) {
  69. const BlobDataItem& item = items[i];
  70. encoder << static_cast<uint32_t>(item.type);
  71. switch (item.type) {
  72. case BlobDataItem::Data:
  73. // There is no way to create a partial data item.
  74. ASSERT(!item.offset);
  75. ASSERT(item.length == BlobDataItem::toEndOfFile);
  76. encoder << CoreIPC::DataReference(reinterpret_cast<const uint8_t*>(item.data->data()), item.data->length());
  77. break;
  78. case BlobDataItem::File:
  79. encoder << item.offset;
  80. encoder << item.length;
  81. encoder << item.expectedModificationTime;
  82. encoder << item.path;
  83. break;
  84. case BlobDataItem::Blob:
  85. encoder << item.offset;
  86. encoder << item.length;
  87. encoder << item.url;
  88. break;
  89. }
  90. }
  91. encoder << m_sandboxExtensions;
  92. }
  93. bool BlobRegistrationData::decode(CoreIPC::ArgumentDecoder& decoder, BlobRegistrationData& result)
  94. {
  95. ASSERT(!result.m_data);
  96. result.m_data = BlobData::create();
  97. String contentType;
  98. if (!decoder.decode(contentType))
  99. return false;
  100. result.m_data->setContentType(contentType);
  101. String contentDisposition;
  102. if (!decoder.decode(contentDisposition))
  103. return false;
  104. result.m_data->setContentDisposition(contentDisposition);
  105. uint64_t itemCount;
  106. if (!decoder.decode(itemCount))
  107. return false;
  108. for (uint64_t i = 0; i < itemCount; ++i) {
  109. uint32_t type;
  110. if (!decoder.decode(type))
  111. return false;
  112. switch (type) {
  113. case BlobDataItem::Data: {
  114. CoreIPC::DataReference data;
  115. if (!decoder.decode(data))
  116. return false;
  117. RefPtr<RawData> rawData = RawData::create();
  118. rawData->mutableData()->append(data.data(), data.size());
  119. result.m_data->appendData(rawData.release(), 0, BlobDataItem::toEndOfFile);
  120. break;
  121. }
  122. case BlobDataItem::File: {
  123. long long offset;
  124. if (!decoder.decode(offset))
  125. return false;
  126. long long length;
  127. if (!decoder.decode(length))
  128. return false;
  129. double expectedModificationTime;
  130. if (!decoder.decode(expectedModificationTime))
  131. return false;
  132. String path;
  133. if (!decoder.decode(path))
  134. return false;
  135. result.m_data->appendFile(path, offset, length, expectedModificationTime);
  136. break;
  137. }
  138. case BlobDataItem::Blob: {
  139. long long offset;
  140. if (!decoder.decode(offset))
  141. return false;
  142. long long length;
  143. if (!decoder.decode(length))
  144. return false;
  145. String url;
  146. if (!decoder.decode(url))
  147. return false;
  148. result.m_data->appendBlob(KURL(KURL(), url), offset, length);
  149. break;
  150. }
  151. default:
  152. return false;
  153. }
  154. }
  155. if (!decoder.decode(result.m_sandboxExtensions))
  156. return false;
  157. return true;
  158. }
  159. }
  160. #endif // ENABLE(BLOB) && ENABLE(NETWORK_PROCESS)