File: fake_data_writer_factory.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (267 lines) | stat: -rw-r--r-- 9,603 bytes parent folder | download | duplicates (6)
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "chrome/browser/webshare/win/fake_data_writer_factory.h"

#include <robuffer.h>
#include <windows.foundation.h>
#include <wrl/async.h>

#include "base/memory/weak_ptr.h"
#include "base/test/fake_iasync_operation_win.h"
#include "chrome/browser/webshare/win/fake_buffer.h"
#include "testing/gtest/include/gtest/gtest.h"

using ABI::Windows::Foundation::AsyncStatus;
using ABI::Windows::Foundation::DateTime;
using ABI::Windows::Foundation::IAsyncOperation;
using ABI::Windows::Foundation::IAsyncOperationWithProgress;
using ABI::Windows::Foundation::IAsyncOperationWithProgressCompletedHandler;
using ABI::Windows::Foundation::TimeSpan;
using ABI::Windows::Storage::Streams::ByteOrder;
using ABI::Windows::Storage::Streams::IBuffer;
using ABI::Windows::Storage::Streams::IDataWriter;
using ABI::Windows::Storage::Streams::IOutputStream;
using ABI::Windows::Storage::Streams::UnicodeEncoding;
using Microsoft::WRL::Callback;
using Microsoft::WRL::ComPtr;
using Microsoft::WRL::Make;
using Microsoft::WRL::RuntimeClass;
using Microsoft::WRL::RuntimeClassFlags;
using Windows::Storage::Streams::IBufferByteAccess;

namespace webshare {
namespace {

class FakeDataWriter final
    : public RuntimeClass<RuntimeClassFlags<Microsoft::WRL::WinRtClassicComMix>,
                          IDataWriter> {
 public:
  explicit FakeDataWriter(IOutputStream* output_stream)
      : output_stream_(output_stream) {}
  FakeDataWriter(const FakeDataWriter&) = delete;
  FakeDataWriter& operator=(const FakeDataWriter&) = delete;
  ~FakeDataWriter() final {
    EXPECT_FALSE(buffer_)
        << "FakeDataWriter destroyed with data pending storage.";
    EXPECT_FALSE(store_async_in_progress_)
        << "FakeDataWriter destroyed while store operation is in progress.";
    EXPECT_TRUE(flush_called_)
        << "FakeDataWriter destroyed without calling FlushAsync.";
  }

  // IDataWriter
  IFACEMETHODIMP get_UnstoredBufferLength(UINT32* value) final { NOTREACHED(); }
  IFACEMETHODIMP get_UnicodeEncoding(UnicodeEncoding* value) final {
    NOTREACHED();
  }
  IFACEMETHODIMP put_UnicodeEncoding(UnicodeEncoding value) final {
    return S_OK;
  }
  IFACEMETHODIMP
  get_ByteOrder(ByteOrder* value) final { NOTREACHED(); }
  IFACEMETHODIMP
  put_ByteOrder(ByteOrder value) final { return S_OK; }
  IFACEMETHODIMP WriteByte(BYTE value) final { NOTREACHED(); }
  IFACEMETHODIMP WriteBytes(UINT32 value_length, BYTE* value) final {
    if (store_async_in_progress_) {
      ADD_FAILURE()
          << "WriteBytes called while a store operation is in progress.";
      return E_ILLEGAL_METHOD_CALL;
    }
    if (flush_called_) {
      // Though it is technically legal to call FlushAsync multiple times, there
      // is no good reason to do so, so presumably points to a coding error.
      ADD_FAILURE() << "WriteBytes called after FlushAsync has been called.";
      return E_ILLEGAL_METHOD_CALL;
    }
    if (buffer_) {
      // Multiple 'write' calls would be allowed between calls to StoreAsync,
      // but would be a deviation from the preferred usage pattern of working
      // with files and streams through internally managed chunks. If a good
      // reason to break from this pattern is introduced this class should be
      // updated to keep a queue of IBuffers rather than a single one.
      ADD_FAILURE() << "WriteBytes called with data already pending storage.";
      return E_ILLEGAL_METHOD_CALL;
    }
    buffer_ = Make<FakeBuffer>(value_length);

    HRESULT hr = buffer_->put_Length(value_length);
    if (FAILED(hr)) {
      EXPECT_HRESULT_SUCCEEDED(hr);
      return hr;
    }

    ComPtr<IBufferByteAccess> buffer_byte_access;
    hr = buffer_.As(&buffer_byte_access);
    if (FAILED(hr)) {
      EXPECT_HRESULT_SUCCEEDED(hr);
      return hr;
    }

    byte* raw_buffer;
    hr = buffer_byte_access->Buffer(&raw_buffer);
    if (FAILED(hr)) {
      EXPECT_HRESULT_SUCCEEDED(hr);
      return hr;
    }

    for (UINT32 i = 0; i < value_length; i++) {
      raw_buffer[i] = value[i];
    }
    return S_OK;
  }
  IFACEMETHODIMP
  WriteBuffer(IBuffer* buffer) final { NOTREACHED(); }
  IFACEMETHODIMP
  WriteBufferRange(IBuffer* buffer, UINT32 start, UINT32 count) final {
    NOTREACHED();
  }
  IFACEMETHODIMP WriteBoolean(boolean value) final { NOTREACHED(); }
  IFACEMETHODIMP WriteGuid(GUID value) final { NOTREACHED(); }
  IFACEMETHODIMP WriteInt16(INT16 value) final { NOTREACHED(); }
  IFACEMETHODIMP WriteInt32(INT32 value) final { NOTREACHED(); }
  IFACEMETHODIMP WriteInt64(INT64 value) final { NOTREACHED(); }
  IFACEMETHODIMP WriteUInt16(UINT16 value) final { NOTREACHED(); }
  IFACEMETHODIMP WriteUInt32(UINT32 value) final { NOTREACHED(); }
  IFACEMETHODIMP WriteUInt64(UINT64 value) final { NOTREACHED(); }
  IFACEMETHODIMP WriteSingle(FLOAT value) final { NOTREACHED(); }
  IFACEMETHODIMP WriteDouble(DOUBLE value) final { NOTREACHED(); }
  IFACEMETHODIMP
  WriteDateTime(DateTime value) final { NOTREACHED(); }
  IFACEMETHODIMP
  WriteTimeSpan(TimeSpan value) final { NOTREACHED(); }
  IFACEMETHODIMP WriteString(HSTRING value, UINT32* code_unit_count) final {
    NOTREACHED();
  }
  IFACEMETHODIMP MeasureString(HSTRING value, UINT32* code_unit_count) final {
    NOTREACHED();
  }
  IFACEMETHODIMP
  StoreAsync(IAsyncOperation<UINT32>** operation) final {
    if (store_async_in_progress_) {
      ADD_FAILURE()
          << "StoreAsync called while a store operation is in progress.";
      return E_ILLEGAL_METHOD_CALL;
    }
    if (flush_called_) {
      // Though it is technically legal to call FlushAsync multiple times, there
      // is no good reason to do so, so presumably points to a coding error.
      ADD_FAILURE() << "StoreAsync called after FlushAsync has been called.";
      return E_ILLEGAL_METHOD_CALL;
    }
    if (!buffer_) {
      ADD_FAILURE() << "StoreAsync called with no data pending for storage.";
      return E_ILLEGAL_METHOD_CALL;
    }

    auto fake_iasync_operation = Make<base::win::FakeIAsyncOperation<UINT32>>();
    HRESULT hr = fake_iasync_operation->QueryInterface(IID_PPV_ARGS(operation));
    if (FAILED(hr)) {
      EXPECT_HRESULT_SUCCEEDED(hr);
      return hr;
    }

    ComPtr<IAsyncOperationWithProgress<UINT32, UINT32>> write_operation;
    hr = output_stream_->WriteAsync(buffer_.Get(), &write_operation);
    if (FAILED(hr)) {
      EXPECT_HRESULT_SUCCEEDED(hr);
      return hr;
    }
    buffer_.Reset();

    auto weak_ptr = weak_factory_.GetWeakPtr();
    hr = write_operation->put_Completed(
        Callback<IAsyncOperationWithProgressCompletedHandler<UINT32, UINT32>>(
            [weak_ptr, fake_iasync_operation](
                IAsyncOperationWithProgress<UINT32, UINT32>* async_operation,
                AsyncStatus async_status) {
              if (!weak_ptr) {
                ADD_FAILURE() << "Writer operation completed after "
                                 "FakeDataWriter was destroyed.";
                return E_UNEXPECTED;
              }
              weak_ptr->store_async_in_progress_ = false;

              EXPECT_EQ(async_status, AsyncStatus::Completed);
              UINT32 results;
              EXPECT_HRESULT_SUCCEEDED(async_operation->GetResults(&results));
              fake_iasync_operation->CompleteWithResults(results);
              return S_OK;
            })
            .Get());
    if (FAILED(hr)) {
      EXPECT_HRESULT_SUCCEEDED(hr);
      return hr;
    }

    store_async_in_progress_ = true;
    return S_OK;
  }
  IFACEMETHODIMP
  FlushAsync(IAsyncOperation<bool>** operation) final {
    if (store_async_in_progress_) {
      ADD_FAILURE()
          << "FlushAsync called while a store operation is in progress.";
      return E_ILLEGAL_METHOD_CALL;
    }
    if (flush_called_) {
      // Though it is technically legal to call FlushAsync multiple times, there
      // is no good reason to do so, so presumably points to a coding error.
      ADD_FAILURE() << "FlushAsync called multiple times.";
      return E_UNEXPECTED;
    }
    if (buffer_) {
      ADD_FAILURE() << "FlushAsync called with data pending storage.";
      return E_ILLEGAL_METHOD_CALL;
    }
    flush_called_ = true;
    HRESULT hr = output_stream_->FlushAsync(operation);
    if (FAILED(hr)) {
      EXPECT_HRESULT_SUCCEEDED(hr);
      return hr;
    }
    return hr;
  }
  IFACEMETHODIMP
  DetachBuffer(IBuffer** buffer) final { NOTREACHED(); }
  IFACEMETHODIMP DetachStream(IOutputStream** output_stream) final {
    NOTREACHED();
  }

 private:
  ComPtr<IBuffer> buffer_;
  bool flush_called_ = false;
  ComPtr<IOutputStream> output_stream_;
  bool store_async_in_progress_ = false;
  base::WeakPtrFactory<FakeDataWriter> weak_factory_{this};
};

}  // namespace

FakeDataWriterFactory::FakeDataWriterFactory() = default;
FakeDataWriterFactory::~FakeDataWriterFactory() = default;

IFACEMETHODIMP FakeDataWriterFactory::CreateDataWriter(
    IOutputStream* output_stream,
    IDataWriter** data_writer) {
  if (!output_stream) {
    ADD_FAILURE() << "CreateDataWriter called with null output_stream.";
    return E_INVALIDARG;
  }
  auto fake_data_writer = Make<FakeDataWriter>(output_stream);
  HRESULT hr = fake_data_writer->QueryInterface(IID_PPV_ARGS(data_writer));
  if (FAILED(hr)) {
    EXPECT_HRESULT_SUCCEEDED(hr);
    return hr;
  }
  return S_OK;
}

}  // namespace webshare