2014-07-01 00:30:29 +02:00
|
|
|
// Copyright (c) 2014 The Chromium Embedded Framework Authors. All rights
|
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that
|
|
|
|
// can be found in the LICENSE file.
|
|
|
|
|
2016-11-18 00:52:42 +01:00
|
|
|
#include "tests/cefclient/browser/bytes_write_handler.h"
|
2014-07-01 00:30:29 +02:00
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
|
2014-07-11 22:10:05 +02:00
|
|
|
#include "include/wrapper/cef_helpers.h"
|
2014-07-01 00:30:29 +02:00
|
|
|
|
2015-01-23 20:09:34 +01:00
|
|
|
namespace client {
|
|
|
|
|
2014-07-01 00:30:29 +02:00
|
|
|
BytesWriteHandler::BytesWriteHandler(size_t grow)
|
2017-05-17 11:29:28 +02:00
|
|
|
: grow_(grow), datasize_(grow), offset_(0) {
|
2014-07-11 22:10:05 +02:00
|
|
|
DCHECK_GT(grow, 0U);
|
2014-07-01 00:30:29 +02:00
|
|
|
data_ = malloc(grow);
|
2020-01-15 15:28:12 +01:00
|
|
|
DCHECK(data_ != nullptr);
|
2014-07-01 00:30:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
BytesWriteHandler::~BytesWriteHandler() {
|
|
|
|
if (data_)
|
|
|
|
free(data_);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t BytesWriteHandler::Write(const void* ptr, size_t size, size_t n) {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2014-07-01 00:30:29 +02:00
|
|
|
size_t rv;
|
|
|
|
if (offset_ + static_cast<int64>(size * n) >= datasize_ &&
|
|
|
|
Grow(size * n) == 0) {
|
|
|
|
rv = 0;
|
|
|
|
} else {
|
|
|
|
memcpy(reinterpret_cast<char*>(data_) + offset_, ptr, size * n);
|
|
|
|
offset_ += size * n;
|
|
|
|
rv = n;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
int BytesWriteHandler::Seek(int64 offset, int whence) {
|
|
|
|
int rv = -1L;
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2014-07-01 00:30:29 +02:00
|
|
|
switch (whence) {
|
2017-05-17 11:29:28 +02:00
|
|
|
case SEEK_CUR:
|
|
|
|
if (offset_ + offset > datasize_ || offset_ + offset < 0)
|
|
|
|
break;
|
|
|
|
offset_ += offset;
|
|
|
|
rv = 0;
|
2014-07-01 00:30:29 +02:00
|
|
|
break;
|
2017-05-17 11:29:28 +02:00
|
|
|
case SEEK_END: {
|
|
|
|
int64 offset_abs = std::abs(offset);
|
|
|
|
if (offset_abs > datasize_)
|
|
|
|
break;
|
|
|
|
offset_ = datasize_ - offset_abs;
|
|
|
|
rv = 0;
|
2014-07-01 00:30:29 +02:00
|
|
|
break;
|
2017-05-17 11:29:28 +02:00
|
|
|
}
|
|
|
|
case SEEK_SET:
|
|
|
|
if (offset > datasize_ || offset < 0)
|
|
|
|
break;
|
|
|
|
offset_ = offset;
|
|
|
|
rv = 0;
|
2014-07-01 00:30:29 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64 BytesWriteHandler::Tell() {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2014-07-01 00:30:29 +02:00
|
|
|
return offset_;
|
|
|
|
}
|
|
|
|
|
|
|
|
int BytesWriteHandler::Flush() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t BytesWriteHandler::Grow(size_t size) {
|
2018-10-10 13:44:02 +02:00
|
|
|
lock_.AssertAcquired();
|
2014-07-01 00:30:29 +02:00
|
|
|
size_t rv;
|
|
|
|
size_t s = (size > grow_ ? size : grow_);
|
|
|
|
void* tmp = realloc(data_, datasize_ + s);
|
2020-01-15 15:28:12 +01:00
|
|
|
DCHECK(tmp != nullptr);
|
2014-07-01 00:30:29 +02:00
|
|
|
if (tmp) {
|
|
|
|
data_ = tmp;
|
|
|
|
datasize_ += s;
|
|
|
|
rv = datasize_;
|
|
|
|
} else {
|
|
|
|
rv = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
2015-01-23 20:09:34 +01:00
|
|
|
|
|
|
|
} // namespace client
|