summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/blob/testing/fake_blob.cc
blob: f8e69fd93e4c5423f9a340b5ede6e4ed4fab6fea (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
// Copyright 2018 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 "third_party/blink/renderer/platform/blob/testing/fake_blob.h"

#include "mojo/public/cpp/bindings/strong_binding.h"
#include "mojo/public/cpp/system/data_pipe_utils.h"
#include "third_party/blink/public/platform/web_string.h"

namespace blink {
namespace {

class SimpleDataPipeGetter : public network::mojom::blink::DataPipeGetter {
 public:
  SimpleDataPipeGetter(const String& str) : str_(str) {}
  ~SimpleDataPipeGetter() override = default;

  // network::mojom::DataPipeGetter implementation:
  void Read(mojo::ScopedDataPipeProducerHandle handle,
            ReadCallback callback) override {
    std::move(callback).Run(0 /* OK */, str_.length());
    bool result = mojo::BlockingCopyFromString(WebString(str_).Utf8(), handle);
    DCHECK(result);
  }

  void Clone(network::mojom::blink::DataPipeGetterRequest request) override {
    mojo::MakeStrongBinding(std::make_unique<SimpleDataPipeGetter>(str_),
                            std::move(request));
  }

 private:
  String str_;

  DISALLOW_COPY_AND_ASSIGN(SimpleDataPipeGetter);
};

}  // namespace

FakeBlob::FakeBlob(const String& uuid, const String& body, State* state)
    : uuid_(uuid), body_(body), state_(state) {}

void FakeBlob::Clone(mojom::blink::BlobRequest request) {
  mojo::MakeStrongBinding(std::make_unique<FakeBlob>(uuid_, body_, state_),
                          std::move(request));
}

void FakeBlob::AsDataPipeGetter(
    network::mojom::blink::DataPipeGetterRequest request) {
  if (state_)
    state_->did_initiate_read_operation = true;
  mojo::MakeStrongBinding(std::make_unique<SimpleDataPipeGetter>(body_),
                          std::move(request));
}

void FakeBlob::ReadRange(uint64_t offset,
                         uint64_t length,
                         mojo::ScopedDataPipeProducerHandle,
                         mojom::blink::BlobReaderClientPtr) {
  NOTREACHED();
}

void FakeBlob::ReadAll(mojo::ScopedDataPipeProducerHandle handle,
                       mojom::blink::BlobReaderClientPtr client) {
  if (state_)
    state_->did_initiate_read_operation = true;
  if (client)
    client->OnCalculatedSize(body_.length(), body_.length());
  bool result = mojo::BlockingCopyFromString(WebString(body_).Utf8(), handle);
  DCHECK(result);
  if (client)
    client->OnComplete(0 /* OK */, body_.length());
}

void FakeBlob::ReadSideData(ReadSideDataCallback callback) {
  NOTREACHED();
}

void FakeBlob::GetInternalUUID(GetInternalUUIDCallback callback) {
  std::move(callback).Run(uuid_);
}

}  // namespace blink