summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/network/encoded_form_data_element_mojom_traits.cc
blob: d95ad2f00f06875d10233b8345057c1d7dd05ded (plain)
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
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <utility>

#include "third_party/blink/renderer/platform/network/encoded_form_data_element_mojom_traits.h"

#include "base/feature_list.h"
#include "mojo/public/cpp/base/file_mojom_traits.h"
#include "mojo/public/cpp/base/file_path_mojom_traits.h"
#include "mojo/public/cpp/base/time_mojom_traits.h"
#include "mojo/public/cpp/bindings/array_traits_wtf_vector.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "mojo/public/cpp/bindings/string_traits_wtf.h"
#include "services/network/public/cpp/features.h"
#include "services/network/public/mojom/data_pipe_getter.mojom-blink.h"
#include "third_party/blink/public/mojom/blob/blob.mojom-blink.h"
#include "third_party/blink/public/mojom/blob/blob_registry.mojom-blink.h"
#include "third_party/blink/public/platform/file_path_conversion.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/renderer/platform/network/wrapped_data_pipe_getter.h"

namespace mojo {

// static
network::mojom::DataElementType
StructTraits<blink::mojom::FetchAPIDataElementDataView,
             blink::FormDataElement>::type(const blink::FormDataElement& data) {
  switch (data.type_) {
    case blink::FormDataElement::kData:
      return network::mojom::DataElementType::kBytes;
    case blink::FormDataElement::kEncodedFile:
      return network::mojom::DataElementType::kFile;
    case blink::FormDataElement::kEncodedBlob: {
      if (data.optional_blob_data_handle_)
        return network::mojom::DataElementType::kDataPipe;
      return network::mojom::DataElementType::kBlob;
    }
    case blink::FormDataElement::kDataPipe:
      return network::mojom::DataElementType::kDataPipe;
  }
  NOTREACHED();
  return network::mojom::DataElementType::kUnknown;
}

// static
base::span<const uint8_t>
StructTraits<blink::mojom::FetchAPIDataElementDataView,
             blink::FormDataElement>::buf(const blink::FormDataElement& data) {
  return base::make_span(reinterpret_cast<const uint8_t*>(data.data_.data()),
                         data.data_.size());
}

// static
base::File
StructTraits<blink::mojom::FetchAPIDataElementDataView,
             blink::FormDataElement>::file(const blink::FormDataElement& data) {
  return base::File();
}

// static
base::FilePath
StructTraits<blink::mojom::FetchAPIDataElementDataView,
             blink::FormDataElement>::path(const blink::FormDataElement& data) {
  return base::FilePath::FromUTF8Unsafe(data.filename_.Utf8());
}

// static
mojo::PendingRemote<network::mojom::blink::DataPipeGetter> StructTraits<
    blink::mojom::FetchAPIDataElementDataView,
    blink::FormDataElement>::data_pipe_getter(const blink::FormDataElement&
                                                  data) {
  if (data.type_ == blink::FormDataElement::kDataPipe) {
    if (!data.data_pipe_getter_)
      return mojo::NullRemote();
    mojo::PendingRemote<network::mojom::blink::DataPipeGetter> data_pipe_getter;
    data.data_pipe_getter_->GetDataPipeGetter()->Clone(
        data_pipe_getter.InitWithNewPipeAndPassReceiver());
    return data_pipe_getter;
  }
  if (data.type_ == blink::FormDataElement::kEncodedBlob) {
    if (data.optional_blob_data_handle_) {
      mojo::Remote<blink::mojom::blink::Blob> blob_remote(
          mojo::PendingRemote<blink::mojom::blink::Blob>(
              data.optional_blob_data_handle_->CloneBlobRemote().PassPipe(),
              blink::mojom::blink::Blob::Version_));
      mojo::PendingRemote<network::mojom::blink::DataPipeGetter>
          data_pipe_getter_remote;
      blob_remote->AsDataPipeGetter(
          data_pipe_getter_remote.InitWithNewPipeAndPassReceiver());
      return data_pipe_getter_remote;
    }
  }
  return mojo::NullRemote();
}

// static
base::Time StructTraits<blink::mojom::FetchAPIDataElementDataView,
                        blink::FormDataElement>::
    expected_modification_time(const blink::FormDataElement& data) {
  if (data.type_ == blink::FormDataElement::kEncodedFile)
    return data.expected_file_modification_time_.value_or(base::Time());
  return base::Time();
}

// static
bool StructTraits<blink::mojom::FetchAPIDataElementDataView,
                  blink::FormDataElement>::
    Read(blink::mojom::FetchAPIDataElementDataView data,
         blink::FormDataElement* out) {
  network::mojom::DataElementType data_type;
  if (!data.ReadType(&data_type)) {
    return false;
  }
  out->file_start_ = data.offset();
  out->file_length_ = data.length();

  switch (data_type) {
    case network::mojom::DataElementType::kBytes: {
      out->type_ = blink::FormDataElement::kData;
      // TODO(richard.li): Delete this workaround when type of
      // blink::FormDataElement::data_ is changed to WTF::Vector<uint8_t>
      WTF::Vector<uint8_t> buf;
      if (!data.ReadBuf(&buf)) {
        return false;
      }
      out->data_.AppendRange(buf.begin(), buf.end());
      break;
    }
    case network::mojom::DataElementType::kFile: {
      out->type_ = blink::FormDataElement::kEncodedFile;
      base::FilePath file_path;
      base::Time expected_time;
      if (!data.ReadPath(&file_path) ||
          !data.ReadExpectedModificationTime(&expected_time)) {
        return false;
      }
      if (expected_time.is_null())
        out->expected_file_modification_time_ = base::nullopt;
      else
        out->expected_file_modification_time_ = expected_time;
      out->filename_ = blink::FilePathToString(file_path);
      break;
    }
    case network::mojom::DataElementType::kDataPipe: {
      out->type_ = blink::FormDataElement::kDataPipe;
      auto data_pipe_ptr_remote = data.TakeDataPipeGetter<
          mojo::PendingRemote<network::mojom::blink::DataPipeGetter>>();
      DCHECK(data_pipe_ptr_remote.is_valid());

      out->data_pipe_getter_ =
          base::MakeRefCounted<blink::WrappedDataPipeGetter>(
              std::move(data_pipe_ptr_remote));
      break;
    }
    case network::mojom::DataElementType::kBlob:
    case network::mojom::DataElementType::kUnknown:
    case network::mojom::DataElementType::kChunkedDataPipe:
    case network::mojom::DataElementType::kRawFile:
      NOTREACHED();
      return false;
  }
  return true;
}

}  // namespace mojo