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.
|
|
|
|
|
|
|
|
#include "libcef/browser/stream_impl.h"
|
|
|
|
#include <stdlib.h>
|
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"
|
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).
|
|
|
|
base::ThreadRestrictions::ScopedAllowIO allow_io;
|
|
|
|
|
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");
|
2012-04-03 03:34:16 +02:00
|
|
|
if (file)
|
|
|
|
reader = new CefFileReader(file, true);
|
|
|
|
return reader;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefStreamReader> CefStreamReader::CreateForData(void* data,
|
|
|
|
size_t size) {
|
|
|
|
DCHECK(data != NULL);
|
|
|
|
DCHECK(size > 0); // NOLINT(readability/check)
|
|
|
|
CefRefPtr<CefStreamReader> reader;
|
|
|
|
if (data && size > 0)
|
|
|
|
reader = new CefBytesReader(data, size, true);
|
|
|
|
return reader;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefStreamReader> CefStreamReader::CreateForHandler(
|
|
|
|
CefRefPtr<CefReadHandler> handler) {
|
|
|
|
DCHECK(handler.get());
|
|
|
|
CefRefPtr<CefStreamReader> reader;
|
|
|
|
if (handler.get())
|
|
|
|
reader = new CefHandlerReader(handler);
|
|
|
|
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).
|
|
|
|
base::ThreadRestrictions::ScopedAllowIO allow_io;
|
|
|
|
|
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");
|
2012-04-03 03:34:16 +02:00
|
|
|
if (file)
|
|
|
|
writer = new CefFileWriter(file, true);
|
|
|
|
return writer;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefStreamWriter> CefStreamWriter::CreateForHandler(
|
|
|
|
CefRefPtr<CefWriteHandler> handler) {
|
|
|
|
DCHECK(handler.get());
|
|
|
|
CefRefPtr<CefStreamWriter> writer;
|
|
|
|
if (handler.get())
|
|
|
|
writer = new CefHandlerWriter(handler);
|
|
|
|
return writer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// CefFileReader
|
|
|
|
|
|
|
|
CefFileReader::CefFileReader(FILE* file, bool close)
|
|
|
|
: close_(close), file_(file) {
|
|
|
|
}
|
|
|
|
|
|
|
|
CefFileReader::~CefFileReader() {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2012-04-03 03:34:16 +02:00
|
|
|
if (close_)
|
2014-02-10 20:26:35 +01:00
|
|
|
base::CloseFile(file_);
|
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_);
|
|
|
|
}
|
|
|
|
|
|
|
|
int CefFileReader::Seek(int64 offset, int whence) {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2012-04-03 03:34:16 +02:00
|
|
|
#if defined(OS_WIN)
|
|
|
|
return _fseeki64(file_, offset, whence);
|
|
|
|
#else
|
|
|
|
return fseek(file_, offset, whence);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int64 CefFileReader::Tell() {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2012-04-03 03:34:16 +02:00
|
|
|
#if defined(OS_WIN)
|
|
|
|
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)
|
|
|
|
: file_(file),
|
|
|
|
close_(close) {
|
|
|
|
}
|
|
|
|
|
|
|
|
CefFileWriter::~CefFileWriter() {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2012-04-03 03:34:16 +02:00
|
|
|
if (close_)
|
2014-02-10 20:26:35 +01:00
|
|
|
base::CloseFile(file_);
|
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_);
|
|
|
|
}
|
|
|
|
|
|
|
|
int CefFileWriter::Seek(int64 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
int64 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
|
|
|
|
|
|
|
|
CefBytesReader::CefBytesReader(void* data, int64 datasize, bool copy)
|
|
|
|
: data_(NULL),
|
|
|
|
datasize_(0),
|
|
|
|
copy_(false),
|
|
|
|
offset_(0) {
|
|
|
|
SetData(data, datasize, copy);
|
|
|
|
}
|
|
|
|
|
|
|
|
CefBytesReader::~CefBytesReader() {
|
|
|
|
SetData(NULL, 0, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
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_);
|
2012-04-03 03:34:16 +02:00
|
|
|
size_t s = (datasize_ - offset_) / size;
|
|
|
|
size_t ret = (n < s ? n : s);
|
|
|
|
memcpy(ptr, (reinterpret_cast<char*>(data_)) + offset_, ret * size);
|
|
|
|
offset_ += ret * size;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CefBytesReader::Seek(int64 offset, int whence) {
|
|
|
|
int rv = -1L;
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2012-04-03 03:34:16 +02:00
|
|
|
switch (whence) {
|
|
|
|
case SEEK_CUR:
|
|
|
|
if (offset_ + offset > datasize_ || offset_ + offset < 0)
|
|
|
|
break;
|
|
|
|
offset_ += offset;
|
|
|
|
rv = 0;
|
|
|
|
break;
|
|
|
|
case SEEK_END: {
|
2014-05-29 19:15:34 +02:00
|
|
|
int64 offset_abs = std::abs(offset);
|
2012-04-03 03:34:16 +02:00
|
|
|
if (offset_abs > datasize_)
|
|
|
|
break;
|
|
|
|
offset_ = datasize_ - offset_abs;
|
|
|
|
rv = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SEEK_SET:
|
|
|
|
if (offset > datasize_ || offset < 0)
|
|
|
|
break;
|
|
|
|
offset_ = offset;
|
|
|
|
rv = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64 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_);
|
2012-04-03 03:34:16 +02:00
|
|
|
return (offset_ >= datasize_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefBytesReader::SetData(void* data, int64 datasize, bool copy) {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2012-04-03 03:34:16 +02:00
|
|
|
if (copy_)
|
|
|
|
free(data_);
|
|
|
|
|
|
|
|
copy_ = copy;
|
|
|
|
offset_ = 0;
|
|
|
|
datasize_ = datasize;
|
|
|
|
|
|
|
|
if (copy) {
|
|
|
|
data_ = malloc(datasize);
|
|
|
|
DCHECK(data_ != NULL);
|
|
|
|
if (data_)
|
|
|
|
memcpy(data_, data, datasize);
|
|
|
|
} else {
|
|
|
|
data_ = data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// CefBytesWriter
|
|
|
|
|
|
|
|
CefBytesWriter::CefBytesWriter(size_t grow)
|
|
|
|
: grow_(grow),
|
|
|
|
datasize_(grow),
|
|
|
|
offset_(0) {
|
|
|
|
DCHECK(grow > 0); // NOLINT(readability/check)
|
|
|
|
data_ = malloc(grow);
|
|
|
|
DCHECK(data_ != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
CefBytesWriter::~CefBytesWriter() {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2012-04-03 03:34:16 +02:00
|
|
|
if (data_)
|
|
|
|
free(data_);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t CefBytesWriter::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
|
|
|
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 CefBytesWriter::Seek(int64 offset, int whence) {
|
|
|
|
int rv = -1L;
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2012-04-03 03:34:16 +02:00
|
|
|
switch (whence) {
|
|
|
|
case SEEK_CUR:
|
|
|
|
if (offset_ + offset > datasize_ || offset_ + offset < 0)
|
|
|
|
break;
|
|
|
|
offset_ += offset;
|
|
|
|
rv = 0;
|
|
|
|
break;
|
|
|
|
case SEEK_END: {
|
2014-05-29 19:15:34 +02:00
|
|
|
int64 offset_abs = std::abs(offset);
|
2012-04-03 03:34:16 +02:00
|
|
|
if (offset_abs > datasize_)
|
|
|
|
break;
|
|
|
|
offset_ = datasize_ - offset_abs;
|
|
|
|
rv = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SEEK_SET:
|
|
|
|
if (offset > datasize_ || offset < 0)
|
|
|
|
break;
|
|
|
|
offset_ = offset;
|
|
|
|
rv = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64 CefBytesWriter::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 CefBytesWriter::Flush() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CefBytesWriter::GetDataString() {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2012-04-03 03:34:16 +02:00
|
|
|
std::string str(reinterpret_cast<char*>(data_), offset_);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t CefBytesWriter::Grow(size_t size) {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2012-04-03 03:34:16 +02:00
|
|
|
size_t rv;
|
|
|
|
size_t s = (size > grow_ ? size : grow_);
|
|
|
|
void* tmp = realloc(data_, datasize_ + s);
|
|
|
|
DCHECK(tmp != NULL);
|
|
|
|
if (tmp) {
|
|
|
|
data_ = tmp;
|
|
|
|
datasize_ += s;
|
|
|
|
rv = datasize_;
|
|
|
|
} else {
|
|
|
|
rv = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|