File: EncodedFormData.cpp

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (203 lines) | stat: -rw-r--r-- 7,022 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
 * Copyright (C) 2004, 2006, 2008, 2011 Apple Inc. All rights reserved.
 * Copyright (C) 2009 Google Inc. All rights reserved.
 * Copyright (C) 2012 Digia Plc. and/or its subsidiary(-ies)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB. If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "platform/network/EncodedFormData.h"

#include "platform/FileMetadata.h"
#include "platform/network/FormDataEncoder.h"
#include "wtf/text/CString.h"
#include "wtf/text/TextEncoding.h"

namespace blink {

bool FormDataElement::isSafeToSendToAnotherThread() const {
  return m_filename.isSafeToSendToAnotherThread() &&
         m_blobUUID.isSafeToSendToAnotherThread() &&
         m_fileSystemURL.isSafeToSendToAnotherThread();
}

inline EncodedFormData::EncodedFormData()
    : m_identifier(0), m_containsPasswordData(false) {}

inline EncodedFormData::EncodedFormData(const EncodedFormData& data)
    : RefCounted<EncodedFormData>(),
      m_elements(data.m_elements),
      m_identifier(data.m_identifier),
      m_containsPasswordData(data.m_containsPasswordData) {}

EncodedFormData::~EncodedFormData() {}

PassRefPtr<EncodedFormData> EncodedFormData::create() {
  return adoptRef(new EncodedFormData);
}

PassRefPtr<EncodedFormData> EncodedFormData::create(const void* data,
                                                    size_t size) {
  RefPtr<EncodedFormData> result = create();
  result->appendData(data, size);
  return result.release();
}

PassRefPtr<EncodedFormData> EncodedFormData::create(const CString& string) {
  RefPtr<EncodedFormData> result = create();
  result->appendData(string.data(), string.length());
  return result.release();
}

PassRefPtr<EncodedFormData> EncodedFormData::create(
    const Vector<char>& vector) {
  RefPtr<EncodedFormData> result = create();
  result->appendData(vector.data(), vector.size());
  return result.release();
}

PassRefPtr<EncodedFormData> EncodedFormData::copy() const {
  return adoptRef(new EncodedFormData(*this));
}

PassRefPtr<EncodedFormData> EncodedFormData::deepCopy() const {
  RefPtr<EncodedFormData> formData(create());

  formData->m_identifier = m_identifier;
  formData->m_boundary = m_boundary;
  formData->m_containsPasswordData = m_containsPasswordData;

  size_t n = m_elements.size();
  formData->m_elements.reserveInitialCapacity(n);
  for (size_t i = 0; i < n; ++i) {
    const FormDataElement& e = m_elements[i];
    switch (e.m_type) {
      case FormDataElement::data:
        formData->m_elements.uncheckedAppend(FormDataElement(e.m_data));
        break;
      case FormDataElement::encodedFile:
        formData->m_elements.uncheckedAppend(
            FormDataElement(e.m_filename.isolatedCopy(), e.m_fileStart,
                            e.m_fileLength, e.m_expectedFileModificationTime));
        break;
      case FormDataElement::encodedBlob:
        formData->m_elements.uncheckedAppend(FormDataElement(
            e.m_blobUUID.isolatedCopy(), e.m_optionalBlobDataHandle));
        break;
      case FormDataElement::encodedFileSystemURL:
        formData->m_elements.uncheckedAppend(
            FormDataElement(e.m_fileSystemURL.copy(), e.m_fileStart,
                            e.m_fileLength, e.m_expectedFileModificationTime));
        break;
    }
  }
  return formData.release();
}

void EncodedFormData::appendData(const void* data, size_t size) {
  if (m_elements.isEmpty() || m_elements.back().m_type != FormDataElement::data)
    m_elements.push_back(FormDataElement());
  FormDataElement& e = m_elements.back();
  size_t oldSize = e.m_data.size();
  e.m_data.grow(oldSize + size);
  memcpy(e.m_data.data() + oldSize, data, size);
}

void EncodedFormData::appendFile(const String& filename) {
  m_elements.push_back(FormDataElement(filename, 0, BlobDataItem::toEndOfFile,
                                       invalidFileTime()));
}

void EncodedFormData::appendFileRange(const String& filename,
                                      long long start,
                                      long long length,
                                      double expectedModificationTime) {
  m_elements.push_back(
      FormDataElement(filename, start, length, expectedModificationTime));
}

void EncodedFormData::appendBlob(const String& uuid,
                                 PassRefPtr<BlobDataHandle> optionalHandle) {
  m_elements.push_back(FormDataElement(uuid, std::move(optionalHandle)));
}

void EncodedFormData::appendFileSystemURL(const KURL& url) {
  m_elements.push_back(
      FormDataElement(url, 0, BlobDataItem::toEndOfFile, invalidFileTime()));
}

void EncodedFormData::appendFileSystemURLRange(
    const KURL& url,
    long long start,
    long long length,
    double expectedModificationTime) {
  m_elements.push_back(
      FormDataElement(url, start, length, expectedModificationTime));
}

void EncodedFormData::flatten(Vector<char>& data) const {
  // Concatenate all the byte arrays, but omit any files.
  data.clear();
  size_t n = m_elements.size();
  for (size_t i = 0; i < n; ++i) {
    const FormDataElement& e = m_elements[i];
    if (e.m_type == FormDataElement::data)
      data.append(e.m_data.data(), static_cast<size_t>(e.m_data.size()));
  }
}

String EncodedFormData::flattenToString() const {
  Vector<char> bytes;
  flatten(bytes);
  return Latin1Encoding().decode(reinterpret_cast<const char*>(bytes.data()),
                                 bytes.size());
}

unsigned long long EncodedFormData::sizeInBytes() const {
  unsigned size = 0;
  size_t n = m_elements.size();
  for (size_t i = 0; i < n; ++i) {
    const FormDataElement& e = m_elements[i];
    switch (e.m_type) {
      case FormDataElement::data:
        size += e.m_data.size();
        break;
      case FormDataElement::encodedFile:
        size += e.m_fileLength - e.m_fileStart;
        break;
      case FormDataElement::encodedBlob:
        if (e.m_optionalBlobDataHandle)
          size += e.m_optionalBlobDataHandle->size();
        break;
      case FormDataElement::encodedFileSystemURL:
        size += e.m_fileLength - e.m_fileStart;
        break;
    }
  }
  return size;
}

bool EncodedFormData::isSafeToSendToAnotherThread() const {
  if (!hasOneRef())
    return false;
  for (auto& element : m_elements) {
    if (!element.isSafeToSendToAnotherThread())
      return false;
  }
  return true;
}

}  // namespace blink