2010-10-03 23:04:50 +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.
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
#include "libcef/stream_impl.h"
|
2010-12-07 02:30:26 +01:00
|
|
|
#include <stdlib.h>
|
2012-01-10 00:46:23 +01:00
|
|
|
#include "base/logging.h"
|
2010-10-03 23:04:50 +02:00
|
|
|
|
|
|
|
// Static functions
|
|
|
|
|
|
|
|
CefRefPtr<CefStreamReader> CefStreamReader::CreateForFile(
|
2012-01-10 00:46:23 +01:00
|
|
|
const CefString& fileName) {
|
2010-10-03 23:04:50 +02:00
|
|
|
CefRefPtr<CefStreamReader> reader;
|
2010-11-22 18:49:46 +01:00
|
|
|
std::string fileNameStr = fileName;
|
2012-01-10 00:46:23 +01:00
|
|
|
FILE* file = fopen(fileNameStr.c_str(), "rb");
|
|
|
|
if (file)
|
2010-11-22 18:49:46 +01:00
|
|
|
reader = new CefFileReader(file, true);
|
2010-10-03 23:04:50 +02:00
|
|
|
return reader;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefStreamReader> CefStreamReader::CreateForData(void* data,
|
2012-01-10 00:46:23 +01:00
|
|
|
size_t size) {
|
2010-10-03 23:04:50 +02:00
|
|
|
DCHECK(data != NULL);
|
2012-01-10 00:46:23 +01:00
|
|
|
DCHECK(size > 0); // NOLINT(readability/check)
|
2010-10-03 23:04:50 +02:00
|
|
|
CefRefPtr<CefStreamReader> reader;
|
2012-01-10 00:46:23 +01:00
|
|
|
if (data && size > 0)
|
2010-10-03 23:04:50 +02:00
|
|
|
reader = new CefBytesReader(data, size, true);
|
|
|
|
return reader;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefStreamReader> CefStreamReader::CreateForHandler(
|
2012-01-10 00:46:23 +01:00
|
|
|
CefRefPtr<CefReadHandler> handler) {
|
2010-10-03 23:04:50 +02:00
|
|
|
DCHECK(handler.get());
|
|
|
|
CefRefPtr<CefStreamReader> reader;
|
2012-01-10 00:46:23 +01:00
|
|
|
if (handler.get())
|
2010-10-03 23:04:50 +02:00
|
|
|
reader = new CefHandlerReader(handler);
|
|
|
|
return reader;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefStreamWriter> CefStreamWriter::CreateForFile(
|
2012-01-10 00:46:23 +01:00
|
|
|
const CefString& fileName) {
|
2010-10-03 23:04:50 +02:00
|
|
|
DCHECK(!fileName.empty());
|
|
|
|
CefRefPtr<CefStreamWriter> writer;
|
2010-11-22 18:49:46 +01:00
|
|
|
std::string fileNameStr = fileName;
|
2012-01-10 00:46:23 +01:00
|
|
|
FILE* file = fopen(fileNameStr.c_str(), "wb");
|
|
|
|
if (file)
|
2010-10-03 23:04:50 +02:00
|
|
|
writer = new CefFileWriter(file, true);
|
|
|
|
return writer;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefStreamWriter> CefStreamWriter::CreateForHandler(
|
2012-01-10 00:46:23 +01:00
|
|
|
CefRefPtr<CefWriteHandler> handler) {
|
2010-10-03 23:04:50 +02:00
|
|
|
DCHECK(handler.get());
|
|
|
|
CefRefPtr<CefStreamWriter> writer;
|
2012-01-10 00:46:23 +01:00
|
|
|
if (handler.get())
|
2010-10-03 23:04:50 +02:00
|
|
|
writer = new CefHandlerWriter(handler);
|
|
|
|
return writer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// CefFileReader
|
|
|
|
|
|
|
|
CefFileReader::CefFileReader(FILE* file, bool close)
|
2012-01-10 00:46:23 +01:00
|
|
|
: close_(close), file_(file) {
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
CefFileReader::~CefFileReader() {
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
2012-01-10 00:46:23 +01:00
|
|
|
if (close_)
|
2010-10-03 23:04:50 +02:00
|
|
|
fclose(file_);
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
size_t CefFileReader::Read(void* ptr, size_t size, size_t n) {
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
|
|
|
return fread(ptr, size, n, file_);
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
int CefFileReader::Seek(int64 offset, int whence) {
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
2012-01-10 00:46:23 +01:00
|
|
|
#if defined(OS_WIN)
|
|
|
|
return _fseeki64(file_, offset, whence);
|
|
|
|
#else
|
2011-05-20 16:42:25 +02:00
|
|
|
return fseek(file_, offset, whence);
|
2012-01-10 00:46:23 +01:00
|
|
|
#endif
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
int64 CefFileReader::Tell() {
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
2012-01-10 00:46:23 +01:00
|
|
|
#if defined(OS_WIN)
|
|
|
|
return _ftelli64(file_);
|
|
|
|
#else
|
2011-05-20 16:42:25 +02:00
|
|
|
return ftell(file_);
|
2012-01-10 00:46:23 +01:00
|
|
|
#endif
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
int CefFileReader::Eof() {
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
|
|
|
return feof(file_);
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// CefFileWriter
|
|
|
|
|
|
|
|
CefFileWriter::CefFileWriter(FILE* file, bool close)
|
2012-01-10 00:46:23 +01:00
|
|
|
: file_(file),
|
|
|
|
close_(close) {
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
CefFileWriter::~CefFileWriter() {
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
2012-01-10 00:46:23 +01:00
|
|
|
if (close_)
|
2010-10-03 23:04:50 +02:00
|
|
|
fclose(file_);
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
size_t CefFileWriter::Write(const void* ptr, size_t size, size_t n) {
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
|
|
|
return (size_t)fwrite(ptr, size, n, file_);
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
int CefFileWriter::Seek(int64 offset, int whence) {
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
|
|
|
return fseek(file_, offset, whence);
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
int64 CefFileWriter::Tell() {
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
|
|
|
return ftell(file_);
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
int CefFileWriter::Flush() {
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
|
|
|
return fflush(file_);
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// CefBytesReader
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
CefBytesReader::CefBytesReader(void* data, int64 datasize, bool copy)
|
|
|
|
: data_(NULL),
|
|
|
|
datasize_(0),
|
|
|
|
copy_(false),
|
|
|
|
offset_(0) {
|
2010-10-03 23:04:50 +02:00
|
|
|
SetData(data, datasize, copy);
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
CefBytesReader::~CefBytesReader() {
|
2010-10-03 23:04:50 +02:00
|
|
|
SetData(NULL, 0, false);
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
size_t CefBytesReader::Read(void* ptr, size_t size, size_t n) {
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
2010-10-03 23:04:50 +02:00
|
|
|
size_t s = (datasize_ - offset_) / size;
|
|
|
|
size_t ret = (n < s ? n : s);
|
2012-01-10 00:46:23 +01:00
|
|
|
memcpy(ptr, (reinterpret_cast<char*>(data_)) + offset_, ret * size);
|
2010-10-03 23:04:50 +02:00
|
|
|
offset_ += ret * size;
|
2011-05-20 16:42:25 +02:00
|
|
|
return ret;
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
int CefBytesReader::Seek(int64 offset, int whence) {
|
2010-10-03 23:04:50 +02:00
|
|
|
int rv = -1L;
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
2012-01-10 00:46:23 +01:00
|
|
|
switch (whence) {
|
2010-10-03 23:04:50 +02:00
|
|
|
case SEEK_CUR:
|
2012-01-10 00:46:23 +01:00
|
|
|
if (offset_ + offset > datasize_ || offset_ + offset < 0)
|
|
|
|
break;
|
2010-10-03 23:04:50 +02:00
|
|
|
offset_ += offset;
|
|
|
|
rv = 0;
|
|
|
|
break;
|
2012-01-10 00:46:23 +01:00
|
|
|
case SEEK_END: {
|
|
|
|
int64 offset_abs = abs(offset);
|
|
|
|
if (offset_abs > datasize_)
|
2010-10-03 23:04:50 +02:00
|
|
|
break;
|
2012-01-10 00:46:23 +01:00
|
|
|
offset_ = datasize_ - offset_abs;
|
2010-10-03 23:04:50 +02:00
|
|
|
rv = 0;
|
|
|
|
break;
|
2012-01-10 00:46:23 +01:00
|
|
|
}
|
2010-10-03 23:04:50 +02:00
|
|
|
case SEEK_SET:
|
2012-01-10 00:46:23 +01:00
|
|
|
if (offset > datasize_ || offset < 0)
|
|
|
|
break;
|
2010-10-03 23:04:50 +02:00
|
|
|
offset_ = offset;
|
|
|
|
rv = 0;
|
|
|
|
break;
|
|
|
|
}
|
2012-01-10 00:46:23 +01:00
|
|
|
|
|
|
|
return rv;
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
int64 CefBytesReader::Tell() {
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
|
|
|
return offset_;
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
int CefBytesReader::Eof() {
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
|
|
|
return (offset_ >= datasize_);
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
2012-01-10 00:46:23 +01:00
|
|
|
|
|
|
|
void CefBytesReader::SetData(void* data, int64 datasize, bool copy) {
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
2012-01-10 00:46:23 +01:00
|
|
|
if (copy_)
|
2010-10-03 23:04:50 +02:00
|
|
|
free(data_);
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
copy_ = copy;
|
|
|
|
offset_ = 0;
|
|
|
|
datasize_ = datasize;
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
if (copy) {
|
2010-10-03 23:04:50 +02:00
|
|
|
data_ = malloc(datasize);
|
|
|
|
DCHECK(data_ != NULL);
|
2012-01-10 00:46:23 +01:00
|
|
|
if (data_)
|
2010-10-03 23:04:50 +02:00
|
|
|
memcpy(data_, data, datasize);
|
|
|
|
} else {
|
|
|
|
data_ = data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// CefBytesWriter
|
|
|
|
|
|
|
|
CefBytesWriter::CefBytesWriter(size_t grow)
|
2012-01-10 00:46:23 +01:00
|
|
|
: grow_(grow),
|
|
|
|
datasize_(grow),
|
|
|
|
offset_(0) {
|
|
|
|
DCHECK(grow > 0); // NOLINT(readability/check)
|
2010-10-03 23:04:50 +02:00
|
|
|
data_ = malloc(grow);
|
|
|
|
DCHECK(data_ != NULL);
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
CefBytesWriter::~CefBytesWriter() {
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
2012-01-10 00:46:23 +01:00
|
|
|
if (data_)
|
2010-10-03 23:04:50 +02:00
|
|
|
free(data_);
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
size_t CefBytesWriter::Write(const void* ptr, size_t size, size_t n) {
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
2010-10-03 23:04:50 +02:00
|
|
|
size_t rv;
|
2012-01-10 00:46:23 +01:00
|
|
|
if (offset_ + static_cast<int64>(size * n) >= datasize_ &&
|
|
|
|
Grow(size * n) == 0) {
|
2010-10-03 23:04:50 +02:00
|
|
|
rv = 0;
|
|
|
|
} else {
|
2012-01-10 00:46:23 +01:00
|
|
|
memcpy(reinterpret_cast<char*>(data_) + offset_, ptr, size * n);
|
2010-10-03 23:04:50 +02:00
|
|
|
offset_ += size * n;
|
|
|
|
rv = n;
|
|
|
|
}
|
2011-05-20 16:42:25 +02:00
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
int CefBytesWriter::Seek(int64 offset, int whence) {
|
2010-10-03 23:04:50 +02:00
|
|
|
int rv = -1L;
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
2012-01-10 00:46:23 +01:00
|
|
|
switch (whence) {
|
2010-10-03 23:04:50 +02:00
|
|
|
case SEEK_CUR:
|
2012-01-10 00:46:23 +01:00
|
|
|
if (offset_ + offset > datasize_ || offset_ + offset < 0)
|
|
|
|
break;
|
2010-10-03 23:04:50 +02:00
|
|
|
offset_ += offset;
|
2012-01-10 00:46:23 +01:00
|
|
|
rv = 0;
|
2010-10-03 23:04:50 +02:00
|
|
|
break;
|
2012-01-10 00:46:23 +01:00
|
|
|
case SEEK_END: {
|
|
|
|
int64 offset_abs = abs(offset);
|
|
|
|
if (offset_abs > datasize_)
|
2010-10-03 23:04:50 +02:00
|
|
|
break;
|
2012-01-10 00:46:23 +01:00
|
|
|
offset_ = datasize_ - offset_abs;
|
|
|
|
rv = 0;
|
|
|
|
break;
|
|
|
|
}
|
2010-10-03 23:04:50 +02:00
|
|
|
case SEEK_SET:
|
2012-01-10 00:46:23 +01:00
|
|
|
if (offset > datasize_ || offset < 0)
|
|
|
|
break;
|
2010-10-03 23:04:50 +02:00
|
|
|
offset_ = offset;
|
2012-01-10 00:46:23 +01:00
|
|
|
rv = 0;
|
2010-10-03 23:04:50 +02:00
|
|
|
break;
|
|
|
|
}
|
2012-01-10 00:46:23 +01:00
|
|
|
|
|
|
|
return rv;
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
int64 CefBytesWriter::Tell() {
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
|
|
|
return offset_;
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
int CefBytesWriter::Flush() {
|
2010-10-03 23:04:50 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
std::string CefBytesWriter::GetDataString() {
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
2012-01-10 00:46:23 +01:00
|
|
|
std::string str(reinterpret_cast<char*>(data_), offset_);
|
2010-10-03 23:04:50 +02:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
size_t CefBytesWriter::Grow(size_t size) {
|
2011-05-20 16:42:25 +02:00
|
|
|
AutoLock lock_scope(this);
|
2010-10-03 23:04:50 +02:00
|
|
|
size_t rv;
|
|
|
|
size_t s = (size > grow_ ? size : grow_);
|
2012-01-10 00:46:23 +01:00
|
|
|
void* tmp = realloc(data_, datasize_ + s);
|
2010-10-03 23:04:50 +02:00
|
|
|
DCHECK(tmp != NULL);
|
2012-01-10 00:46:23 +01:00
|
|
|
if (tmp) {
|
|
|
|
data_ = tmp;
|
|
|
|
datasize_ += s;
|
2010-10-03 23:04:50 +02:00
|
|
|
rv = datasize_;
|
|
|
|
} else {
|
|
|
|
rv = 0;
|
|
|
|
}
|
2011-05-20 16:42:25 +02:00
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
return rv;
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|