Fix decoding of about:credits HTML (issue #1980)

This commit is contained in:
Marshall Greenblatt 2016-09-01 13:32:56 +03:00
parent 1e84e0cfff
commit ad1619dbd7
4 changed files with 40 additions and 3 deletions

View File

@ -614,6 +614,7 @@ static_library("libcef_static") {
"//pdf",
"//skia",
"//storage/browser",
"//third_party/brotli",
"//third_party/cld",
"//third_party/hunspell",
"//third_party/leveldatabase",

View File

@ -281,6 +281,7 @@ class Delegate : public InternalHandlerDelegate {
} else {
action->mime_type = "text/html";
action->resource_id = IDR_ABOUT_UI_CREDITS_HTML;
action->encoding = Action::ENCODING_BROTLI;
}
return true;
}

View File

@ -14,6 +14,7 @@
#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread_restrictions.h"
#include "net/base/mime_util.h"
#include "third_party/brotli/dec/decode.h"
#include "ui/base/resource/resource_bundle.h"
namespace scheme {
@ -28,7 +29,7 @@ base::FilePath FilePathFromASCII(const std::string& str) {
#endif
}
static std::string GetMimeType(const std::string& filename) {
std::string GetMimeType(const std::string& filename) {
// Requests should not block on the disk! On POSIX this goes to disk.
// http://code.google.com/p/chromium/issues/detail?id=59849
base::ThreadRestrictions::ScopedAllowIO allow_io;
@ -48,6 +49,24 @@ static std::string GetMimeType(const std::string& filename) {
return "text/plain";
}
bool DecodeBrotli(const base::StringPiece& encoded,
std::string& decoded) {
size_t decoded_size;
const uint8_t* encoded_response_buffer =
reinterpret_cast<const uint8_t*>(encoded.data());
if (!BrotliDecompressedSize(encoded.size(), encoded_response_buffer,
&decoded_size)) {
return false;
}
decoded.resize(decoded_size);
return BrotliDecompressBuffer(encoded.size(), encoded_response_buffer,
&decoded_size,
reinterpret_cast<uint8_t*>(&decoded[0])) ==
BROTLI_RESULT_SUCCESS;
}
class RedirectHandler : public CefResourceHandler {
public:
explicit RedirectHandler(const GURL& url)
@ -153,6 +172,16 @@ class InternalHandlerFactory : public CefSchemeHandlerFactory {
base::StringPiece piece = CefContentClient::Get()->GetDataResource(
action.resource_id, ui::SCALE_FACTOR_NONE);
if (!piece.empty()) {
std::string decoded;
if (action.encoding ==
InternalHandlerDelegate::Action::ENCODING_BROTLI &&
!DecodeBrotli(piece, decoded)) {
decoded = "Unable to decode content!";
}
if (!decoded.empty())
piece = base::StringPiece(decoded);
action.stream =
CefStreamReader::CreateForData(const_cast<char*>(piece.data()),
piece.size());
@ -183,7 +212,8 @@ class InternalHandlerFactory : public CefSchemeHandlerFactory {
InternalHandlerDelegate::Action::Action()
: stream_size(-1),
resource_id(-1) {
resource_id(-1),
encoding(ENCODING_NONE) {
}
CefRefPtr<CefSchemeHandlerFactory> CreateInternalHandlerFactory(

View File

@ -28,8 +28,13 @@ class InternalHandlerDelegate {
CefRefPtr<CefStreamReader> stream;
int stream_size;
// Option 2: Specify a resource id to load static content.
// Option 2: Specify a resource id to load static content. May include an
// optional encoding type.
int resource_id;
enum Encoding {
ENCODING_NONE,
ENCODING_BROTLI,
} encoding;
// Option 3: Redirect to the specified URL.
GURL redirect_url;