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", "//pdf",
"//skia", "//skia",
"//storage/browser", "//storage/browser",
"//third_party/brotli",
"//third_party/cld", "//third_party/cld",
"//third_party/hunspell", "//third_party/hunspell",
"//third_party/leveldatabase", "//third_party/leveldatabase",

View File

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

View File

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

View File

@ -28,8 +28,13 @@ class InternalHandlerDelegate {
CefRefPtr<CefStreamReader> stream; CefRefPtr<CefStreamReader> stream;
int stream_size; 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; int resource_id;
enum Encoding {
ENCODING_NONE,
ENCODING_BROTLI,
} encoding;
// Option 3: Redirect to the specified URL. // Option 3: Redirect to the specified URL.
GURL redirect_url; GURL redirect_url;