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
|
|
|
|
|
|
|
#include "include/base/cef_logging.h"
|
2019-12-31 13:24:10 +01:00
|
|
|
#include "include/cef_task.h"
|
2014-07-11 22:10:05 +02:00
|
|
|
#include "include/wrapper/cef_helpers.h"
|
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),
|
2019-12-31 13:24:10 +01:00
|
|
|
stream_(stream) {
|
2012-04-03 03:34:16 +02:00
|
|
|
DCHECK(!mime_type_.empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
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),
|
2019-12-31 13:24:10 +01:00
|
|
|
stream_(stream) {
|
2012-04-03 03:34:16 +02:00
|
|
|
DCHECK(!mime_type_.empty());
|
2014-01-31 00:15:55 +01:00
|
|
|
}
|
|
|
|
|
2019-12-31 13:24:10 +01:00
|
|
|
bool CefStreamResourceHandler::Open(CefRefPtr<CefRequest> request,
|
|
|
|
bool& handle_request,
|
|
|
|
CefRefPtr<CefCallback> callback) {
|
|
|
|
DCHECK(!CefCurrentlyOn(TID_UI) && !CefCurrentlyOn(TID_IO));
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2019-12-31 13:24:10 +01:00
|
|
|
// Continue the request immediately.
|
|
|
|
handle_request = true;
|
2012-04-03 03:34:16 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefStreamResourceHandler::GetResponseHeaders(
|
|
|
|
CefRefPtr<CefResponse> response,
|
2023-06-01 16:07:20 +02:00
|
|
|
int64_t& response_length,
|
2012-04-03 03:34:16 +02:00
|
|
|
CefString& redirectUrl) {
|
2019-12-31 13:24:10 +01:00
|
|
|
CEF_REQUIRE_IO_THREAD();
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
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_);
|
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!header_map_.empty()) {
|
2012-04-03 03:34:16 +02:00
|
|
|
response->SetHeaderMap(header_map_);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2020-08-21 23:18:45 +02:00
|
|
|
response_length = stream_ ? -1 : 0;
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2019-12-31 13:24:10 +01:00
|
|
|
bool CefStreamResourceHandler::Read(
|
|
|
|
void* data_out,
|
|
|
|
int bytes_to_read,
|
|
|
|
int& bytes_read,
|
|
|
|
CefRefPtr<CefResourceReadCallback> callback) {
|
|
|
|
DCHECK(!CefCurrentlyOn(TID_UI) && !CefCurrentlyOn(TID_IO));
|
2014-01-31 00:15:55 +01:00
|
|
|
DCHECK_GT(bytes_to_read, 0);
|
2020-08-21 23:18:45 +02:00
|
|
|
DCHECK(stream_);
|
2014-01-31 00:15:55 +01:00
|
|
|
|
2019-12-31 13:24:10 +01:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
void CefStreamResourceHandler::Cancel() {}
|