2012-04-03 03:34:16 +02:00
|
|
|
// Copyright (c) 2012 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 "include/wrapper/cef_stream_resource_handler.h"
|
2014-07-11 22:10:05 +02:00
|
|
|
|
2014-01-31 00:15:55 +01:00
|
|
|
#include <algorithm>
|
2014-07-11 22:10:05 +02:00
|
|
|
|
2014-07-15 19:19:43 +02:00
|
|
|
#include "include/base/cef_bind.h"
|
2014-07-11 22:10:05 +02:00
|
|
|
#include "include/base/cef_logging.h"
|
|
|
|
#include "include/base/cef_macros.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "include/cef_callback.h"
|
|
|
|
#include "include/cef_request.h"
|
|
|
|
#include "include/cef_stream.h"
|
2014-07-15 19:19:43 +02:00
|
|
|
#include "include/wrapper/cef_closure_task.h"
|
2014-07-11 22:10:05 +02:00
|
|
|
#include "include/wrapper/cef_helpers.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2014-01-31 00:15:55 +01:00
|
|
|
// Class that represents a readable/writable character buffer.
|
|
|
|
class CefStreamResourceHandler::Buffer {
|
|
|
|
public:
|
|
|
|
Buffer()
|
2014-07-15 19:19:43 +02:00
|
|
|
: size_(0),
|
2014-01-31 00:15:55 +01:00
|
|
|
bytes_requested_(0),
|
|
|
|
bytes_written_(0),
|
|
|
|
bytes_read_(0) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void Reset(int new_size) {
|
|
|
|
if (size_ < new_size) {
|
|
|
|
size_ = new_size;
|
2014-07-15 19:19:43 +02:00
|
|
|
buffer_.reset(new char[size_]);
|
2014-01-31 00:15:55 +01:00
|
|
|
DCHECK(buffer_);
|
|
|
|
}
|
|
|
|
bytes_requested_ = new_size;
|
|
|
|
bytes_written_ = 0;
|
|
|
|
bytes_read_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsEmpty() const {
|
|
|
|
return (bytes_written_ == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CanRead() const {
|
|
|
|
return (bytes_read_ < bytes_written_);
|
|
|
|
}
|
|
|
|
|
|
|
|
int WriteTo(void* data_out, int bytes_to_read) {
|
|
|
|
const int write_size =
|
|
|
|
std::min(bytes_to_read, bytes_written_ - bytes_read_);
|
|
|
|
if (write_size > 0) {
|
2014-07-15 19:19:43 +02:00
|
|
|
memcpy(data_out, buffer_ .get() + bytes_read_, write_size);
|
2014-01-31 00:15:55 +01:00
|
|
|
bytes_read_ += write_size;
|
|
|
|
}
|
|
|
|
return write_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ReadFrom(CefRefPtr<CefStreamReader> reader) {
|
|
|
|
// Read until the buffer is full or until Read() returns 0 to indicate no
|
|
|
|
// more data.
|
|
|
|
int bytes_read;
|
|
|
|
do {
|
|
|
|
bytes_read = static_cast<int>(
|
2014-07-15 19:19:43 +02:00
|
|
|
reader->Read(buffer_.get() + bytes_written_, 1,
|
2014-01-31 00:15:55 +01:00
|
|
|
bytes_requested_ - bytes_written_));
|
|
|
|
bytes_written_ += bytes_read;
|
|
|
|
} while (bytes_read != 0 && bytes_written_ < bytes_requested_);
|
|
|
|
|
|
|
|
return bytes_written_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-05-25 01:35:43 +02:00
|
|
|
SCOPED_PTR(char[]) buffer_;
|
2014-01-31 00:15:55 +01:00
|
|
|
int size_;
|
|
|
|
int bytes_requested_;
|
|
|
|
int bytes_written_;
|
|
|
|
int bytes_read_;
|
2014-07-11 22:10:05 +02:00
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(Buffer);
|
2014-01-31 00:15:55 +01:00
|
|
|
};
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
CefStreamResourceHandler::CefStreamResourceHandler(
|
|
|
|
const CefString& mime_type,
|
|
|
|
CefRefPtr<CefStreamReader> stream)
|
|
|
|
: status_code_(200),
|
2014-01-31 00:15:55 +01:00
|
|
|
status_text_("OK"),
|
2012-04-03 03:34:16 +02:00
|
|
|
mime_type_(mime_type),
|
2014-07-15 19:19:43 +02:00
|
|
|
stream_(stream)
|
2014-01-31 00:15:55 +01:00
|
|
|
#ifndef NDEBUG
|
|
|
|
, buffer_owned_by_file_thread_(false)
|
|
|
|
#endif
|
|
|
|
{
|
2012-04-03 03:34:16 +02:00
|
|
|
DCHECK(!mime_type_.empty());
|
|
|
|
DCHECK(stream_.get());
|
2014-01-31 00:15:55 +01:00
|
|
|
read_on_file_thread_ = stream_->MayBlock();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefStreamResourceHandler::CefStreamResourceHandler(
|
|
|
|
int status_code,
|
2014-01-31 00:15:55 +01:00
|
|
|
const CefString& status_text,
|
2012-04-03 03:34:16 +02:00
|
|
|
const CefString& mime_type,
|
|
|
|
CefResponse::HeaderMap header_map,
|
|
|
|
CefRefPtr<CefStreamReader> stream)
|
|
|
|
: status_code_(status_code),
|
2014-01-31 00:15:55 +01:00
|
|
|
status_text_(status_text),
|
2012-04-03 03:34:16 +02:00
|
|
|
mime_type_(mime_type),
|
|
|
|
header_map_(header_map),
|
2015-03-04 20:01:53 +01:00
|
|
|
stream_(stream)
|
|
|
|
#ifndef NDEBUG
|
|
|
|
, buffer_owned_by_file_thread_(false)
|
|
|
|
#endif
|
|
|
|
{
|
2012-04-03 03:34:16 +02:00
|
|
|
DCHECK(!mime_type_.empty());
|
|
|
|
DCHECK(stream_.get());
|
2014-01-31 00:15:55 +01:00
|
|
|
read_on_file_thread_ = stream_->MayBlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
CefStreamResourceHandler::~CefStreamResourceHandler() {
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefStreamResourceHandler::ProcessRequest(CefRefPtr<CefRequest> request,
|
|
|
|
CefRefPtr<CefCallback> callback) {
|
|
|
|
callback->Continue();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefStreamResourceHandler::GetResponseHeaders(
|
|
|
|
CefRefPtr<CefResponse> response,
|
|
|
|
int64& response_length,
|
|
|
|
CefString& redirectUrl) {
|
|
|
|
response->SetStatus(status_code_);
|
2014-01-31 00:15:55 +01:00
|
|
|
response->SetStatusText(status_text_);
|
2012-04-03 03:34:16 +02:00
|
|
|
response->SetMimeType(mime_type_);
|
|
|
|
|
|
|
|
if (!header_map_.empty())
|
|
|
|
response->SetHeaderMap(header_map_);
|
|
|
|
|
|
|
|
response_length = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefStreamResourceHandler::ReadResponse(void* data_out,
|
|
|
|
int bytes_to_read,
|
|
|
|
int& bytes_read,
|
|
|
|
CefRefPtr<CefCallback> callback) {
|
2014-01-31 00:15:55 +01:00
|
|
|
DCHECK_GT(bytes_to_read, 0);
|
|
|
|
|
|
|
|
if (read_on_file_thread_) {
|
2014-07-15 01:52:48 +02:00
|
|
|
#ifndef NDEBUG
|
2014-01-31 00:15:55 +01:00
|
|
|
DCHECK(!buffer_owned_by_file_thread_);
|
2014-07-15 01:52:48 +02:00
|
|
|
#endif
|
2014-01-31 00:15:55 +01:00
|
|
|
if (buffer_ && (buffer_->CanRead() || buffer_->IsEmpty())) {
|
|
|
|
if (buffer_->CanRead()) {
|
|
|
|
// Provide data from the buffer.
|
|
|
|
bytes_read = buffer_->WriteTo(data_out, bytes_to_read);
|
|
|
|
return (bytes_read > 0);
|
|
|
|
} else {
|
|
|
|
// End of the steam.
|
|
|
|
bytes_read = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Perform another read on the file thread.
|
|
|
|
bytes_read = 0;
|
|
|
|
#ifndef NDEBUG
|
|
|
|
buffer_owned_by_file_thread_ = true;
|
|
|
|
#endif
|
|
|
|
CefPostTask(TID_FILE,
|
2014-07-15 19:19:43 +02:00
|
|
|
base::Bind(&CefStreamResourceHandler::ReadOnFileThread, this,
|
|
|
|
bytes_to_read, callback));
|
2014-01-31 00:15:55 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Read until the buffer is full or until Read() returns 0 to indicate no
|
|
|
|
// more data.
|
|
|
|
bytes_read = 0;
|
|
|
|
int read = 0;
|
|
|
|
do {
|
|
|
|
read = static_cast<int>(
|
|
|
|
stream_->Read(static_cast<char*>(data_out) + bytes_read, 1,
|
|
|
|
bytes_to_read - bytes_read));
|
|
|
|
bytes_read += read;
|
|
|
|
} while (read != 0 && bytes_read < bytes_to_read);
|
|
|
|
|
|
|
|
return (bytes_read > 0);
|
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefStreamResourceHandler::Cancel() {
|
|
|
|
}
|
2014-01-31 00:15:55 +01:00
|
|
|
|
|
|
|
void CefStreamResourceHandler::ReadOnFileThread(
|
|
|
|
int bytes_to_read,
|
|
|
|
CefRefPtr<CefCallback> callback) {
|
|
|
|
CEF_REQUIRE_FILE_THREAD();
|
2014-07-15 01:52:48 +02:00
|
|
|
#ifndef NDEBUG
|
2014-01-31 00:15:55 +01:00
|
|
|
DCHECK(buffer_owned_by_file_thread_);
|
2014-07-15 01:52:48 +02:00
|
|
|
#endif
|
2014-01-31 00:15:55 +01:00
|
|
|
|
|
|
|
if (!buffer_)
|
2014-07-15 19:19:43 +02:00
|
|
|
buffer_.reset(new Buffer());
|
2014-01-31 00:15:55 +01:00
|
|
|
buffer_->Reset(bytes_to_read);
|
|
|
|
buffer_->ReadFrom(stream_);
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
buffer_owned_by_file_thread_ = false;
|
|
|
|
#endif
|
|
|
|
callback->Continue();
|
|
|
|
}
|