2012-04-03 03:34:16 +02:00
|
|
|
// Copyright (c) 2008 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.
|
|
|
|
|
2024-04-30 17:45:07 +02:00
|
|
|
#include "cef/libcef/browser/stream_impl.h"
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
#include <stdlib.h>
|
2024-04-30 17:45:07 +02:00
|
|
|
|
2024-05-13 23:36:09 +02:00
|
|
|
#include <algorithm>
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
#include "base/files/file_util.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "base/logging.h"
|
2024-05-13 23:36:09 +02:00
|
|
|
#include "base/numerics/safe_conversions.h"
|
2014-02-11 23:44:35 +01:00
|
|
|
#include "base/threading/thread_restrictions.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// Static functions
|
|
|
|
|
|
|
|
CefRefPtr<CefStreamReader> CefStreamReader::CreateForFile(
|
|
|
|
const CefString& fileName) {
|
2014-02-11 23:44:35 +01:00
|
|
|
DCHECK(!fileName.empty());
|
|
|
|
|
|
|
|
// TODO(cef): Do not allow file IO on all threads (issue #1187).
|
2022-11-15 18:50:53 +01:00
|
|
|
base::ScopedAllowBlockingForTesting allow_blocking;
|
2014-02-11 23:44:35 +01:00
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
CefRefPtr<CefStreamReader> reader;
|
2014-02-10 20:26:35 +01:00
|
|
|
FILE* file = base::OpenFile(base::FilePath(fileName), "rb");
|
2023-01-02 23:59:03 +01:00
|
|
|
if (file) {
|
2012-04-03 03:34:16 +02:00
|
|
|
reader = new CefFileReader(file, true);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
return reader;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefStreamReader> CefStreamReader::CreateForData(void* data,
|
|
|
|
size_t size) {
|
2020-01-15 14:36:24 +01:00
|
|
|
DCHECK(data != nullptr);
|
2017-05-17 11:29:28 +02:00
|
|
|
DCHECK(size > 0);
|
2012-04-03 03:34:16 +02:00
|
|
|
CefRefPtr<CefStreamReader> reader;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (data && size > 0) {
|
2024-05-13 23:36:09 +02:00
|
|
|
reader = new CefBytesReader(data, size);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
return reader;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefStreamReader> CefStreamReader::CreateForHandler(
|
|
|
|
CefRefPtr<CefReadHandler> handler) {
|
|
|
|
DCHECK(handler.get());
|
|
|
|
CefRefPtr<CefStreamReader> reader;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (handler.get()) {
|
2012-04-03 03:34:16 +02:00
|
|
|
reader = new CefHandlerReader(handler);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
return reader;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefStreamWriter> CefStreamWriter::CreateForFile(
|
|
|
|
const CefString& fileName) {
|
|
|
|
DCHECK(!fileName.empty());
|
2014-02-11 23:44:35 +01:00
|
|
|
|
|
|
|
// TODO(cef): Do not allow file IO on all threads (issue #1187).
|
2022-11-15 18:50:53 +01:00
|
|
|
base::ScopedAllowBlockingForTesting allow_blocking;
|
2014-02-11 23:44:35 +01:00
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
CefRefPtr<CefStreamWriter> writer;
|
2014-02-10 20:26:35 +01:00
|
|
|
FILE* file = base::OpenFile(base::FilePath(fileName), "wb");
|
2023-01-02 23:59:03 +01:00
|
|
|
if (file) {
|
2012-04-03 03:34:16 +02:00
|
|
|
writer = new CefFileWriter(file, true);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
return writer;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefStreamWriter> CefStreamWriter::CreateForHandler(
|
|
|
|
CefRefPtr<CefWriteHandler> handler) {
|
|
|
|
DCHECK(handler.get());
|
|
|
|
CefRefPtr<CefStreamWriter> writer;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (handler.get()) {
|
2012-04-03 03:34:16 +02:00
|
|
|
writer = new CefHandlerWriter(handler);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
return writer;
|
|
|
|
}
|
|
|
|
|
|
|
|
// CefFileReader
|
|
|
|
|
|
|
|
CefFileReader::CefFileReader(FILE* file, bool close)
|
2017-05-17 11:29:28 +02:00
|
|
|
: close_(close), file_(file) {}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
CefFileReader::~CefFileReader() {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (close_) {
|
2014-02-10 20:26:35 +01:00
|
|
|
base::CloseFile(file_);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t CefFileReader::Read(void* ptr, size_t size, size_t n) {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2012-04-03 03:34:16 +02:00
|
|
|
return fread(ptr, size, n, file_);
|
|
|
|
}
|
|
|
|
|
2023-06-01 16:06:15 +02:00
|
|
|
int CefFileReader::Seek(int64_t offset, int whence) {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2022-01-24 18:58:02 +01:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2012-04-03 03:34:16 +02:00
|
|
|
return _fseeki64(file_, offset, whence);
|
|
|
|
#else
|
|
|
|
return fseek(file_, offset, whence);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-06-01 16:06:15 +02:00
|
|
|
int64_t CefFileReader::Tell() {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2022-01-24 18:58:02 +01:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2012-04-03 03:34:16 +02:00
|
|
|
return _ftelli64(file_);
|
|
|
|
#else
|
|
|
|
return ftell(file_);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int CefFileReader::Eof() {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2012-04-03 03:34:16 +02:00
|
|
|
return feof(file_);
|
|
|
|
}
|
|
|
|
|
|
|
|
// CefFileWriter
|
|
|
|
|
|
|
|
CefFileWriter::CefFileWriter(FILE* file, bool close)
|
2017-05-17 11:29:28 +02:00
|
|
|
: file_(file), close_(close) {}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
CefFileWriter::~CefFileWriter() {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (close_) {
|
2014-02-10 20:26:35 +01:00
|
|
|
base::CloseFile(file_);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t CefFileWriter::Write(const void* ptr, size_t size, size_t n) {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2012-04-03 03:34:16 +02:00
|
|
|
return (size_t)fwrite(ptr, size, n, file_);
|
|
|
|
}
|
|
|
|
|
2023-06-01 16:06:15 +02:00
|
|
|
int CefFileWriter::Seek(int64_t offset, int whence) {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2012-04-03 03:34:16 +02:00
|
|
|
return fseek(file_, offset, whence);
|
|
|
|
}
|
|
|
|
|
2023-06-01 16:06:15 +02:00
|
|
|
int64_t CefFileWriter::Tell() {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2012-04-03 03:34:16 +02:00
|
|
|
return ftell(file_);
|
|
|
|
}
|
|
|
|
|
|
|
|
int CefFileWriter::Flush() {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2012-04-03 03:34:16 +02:00
|
|
|
return fflush(file_);
|
|
|
|
}
|
|
|
|
|
|
|
|
// CefBytesReader
|
|
|
|
|
2024-05-13 23:36:09 +02:00
|
|
|
CefBytesReader::CefBytesReader(void* data, int64_t datasize) {
|
|
|
|
SetData(data, datasize);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t CefBytesReader::Read(void* ptr, size_t size, size_t n) {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2024-05-13 23:36:09 +02:00
|
|
|
size_t s = (data_.size() - offset_) / size;
|
|
|
|
size_t ret = std::min(n, s);
|
|
|
|
memcpy(ptr, data_.data() + offset_, ret * size);
|
2012-04-03 03:34:16 +02:00
|
|
|
offset_ += ret * size;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-06-01 16:06:15 +02:00
|
|
|
int CefBytesReader::Seek(int64_t offset, int whence) {
|
2012-04-03 03:34:16 +02:00
|
|
|
int rv = -1L;
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2024-05-13 23:36:09 +02:00
|
|
|
|
|
|
|
const int64_t size = base::checked_cast<int64_t>(data_.size());
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
switch (whence) {
|
2017-05-17 11:29:28 +02:00
|
|
|
case SEEK_CUR:
|
2024-05-13 23:36:09 +02:00
|
|
|
if (offset_ + offset > size || offset_ + offset < 0) {
|
2017-05-17 11:29:28 +02:00
|
|
|
break;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-05-17 11:29:28 +02:00
|
|
|
offset_ += offset;
|
|
|
|
rv = 0;
|
2012-04-03 03:34:16 +02:00
|
|
|
break;
|
2017-05-17 11:29:28 +02:00
|
|
|
case SEEK_END: {
|
2023-06-01 16:06:15 +02:00
|
|
|
int64_t offset_abs = std::abs(offset);
|
2024-05-13 23:36:09 +02:00
|
|
|
if (offset_abs > size) {
|
2017-05-17 11:29:28 +02:00
|
|
|
break;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2024-05-13 23:36:09 +02:00
|
|
|
offset_ = size - offset_abs;
|
2017-05-17 11:29:28 +02:00
|
|
|
rv = 0;
|
2012-04-03 03:34:16 +02:00
|
|
|
break;
|
2017-05-17 11:29:28 +02:00
|
|
|
}
|
|
|
|
case SEEK_SET:
|
2024-05-13 23:36:09 +02:00
|
|
|
if (offset > size || offset < 0) {
|
2017-05-17 11:29:28 +02:00
|
|
|
break;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-05-17 11:29:28 +02:00
|
|
|
offset_ = offset;
|
|
|
|
rv = 0;
|
2012-04-03 03:34:16 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2023-06-01 16:06:15 +02:00
|
|
|
int64_t CefBytesReader::Tell() {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2012-04-03 03:34:16 +02:00
|
|
|
return offset_;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CefBytesReader::Eof() {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2024-05-13 23:36:09 +02:00
|
|
|
return (offset_ >= base::checked_cast<int64_t>(data_.size()));
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2024-05-13 23:36:09 +02:00
|
|
|
void CefBytesReader::SetData(void* data, int64_t datasize) {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
offset_ = 0;
|
|
|
|
|
2024-05-13 23:36:09 +02:00
|
|
|
if (data && datasize > 0) {
|
|
|
|
data_.reserve(datasize);
|
|
|
|
std::copy(static_cast<unsigned char*>(data),
|
|
|
|
static_cast<unsigned char*>(data) + datasize,
|
|
|
|
std::back_inserter(data_));
|
2012-04-03 03:34:16 +02:00
|
|
|
} else {
|
2024-05-13 23:36:09 +02:00
|
|
|
data_.clear();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
}
|