Skip to content

Commit

Permalink
Merge pull request #11545 from brave/add-title-for-speedreader
Browse files Browse the repository at this point in the history
add title to speedreader documents
  • Loading branch information
pes10k authored Dec 14, 2021
2 parents d16829d + e8b0f37 commit e4c2c36
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
1 change: 1 addition & 0 deletions components/speedreader/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ static_library("speedreader") {
"//components/prefs:prefs",
"//components/sessions:sessions",
"//content/public/browser",
"//crypto",
"//services/network/public/cpp",
"//services/network/public/mojom",
"//third_party/blink/public/common",
Expand Down
1 change: 1 addition & 0 deletions components/speedreader/DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ include_rules = [
"+components/prefs",
"+components/sessions/content",
"+content/public/browser",
"+crypto",
"+services/network/public",
"+services/network/public/cpp",
"+services/network/public/mojom",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@ pub fn extract_dom<S: ::std::hash::BuildHasher>(
let charset_blob = format!("<meta charset=\"{}\"/>", charset);
content = charset_blob + &content;
}
if !meta.title.is_empty() {
let title_blob = format!("<title>{}</title>", &meta.title);
content = title_blob + &content;
}
Ok(Product { meta, content })
}

Expand Down
24 changes: 19 additions & 5 deletions components/speedreader/speedreader_rewriter_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

#include <utility>

#include "base/base64.h"
#include "base/bind.h"
#include "base/containers/span.h"
#include "base/feature_list.h"
#include "base/files/file_util.h"
#include "base/logging.h"
Expand All @@ -19,13 +21,27 @@
#include "brave/components/speedreader/speedreader_component.h"
#include "brave/components/speedreader/speedreader_util.h"
#include "components/grit/brave_components_resources.h"
#include "crypto/sha2.h"
#include "ui/base/resource/resource_bundle.h"
#include "url/gurl.h"

namespace speedreader {

namespace {

std::string WrapStylesheetWithCSP(const std::string& stylesheet) {
const std::string style_hash = crypto::SHA256HashString(stylesheet);
const std::string style_hash_b64 =
base::Base64Encode(base::as_bytes(base::make_span(style_hash)));

return "<meta http-equiv=\"Content-Security-Policy\" content=\""
"script-src 'none'; style-src 'sha256-" +
style_hash_b64 +
"'\">\n"
"<style id=\"brave_speedreader_style\">" +
stylesheet + "</style>";
}

std::string GetDistilledPageStylesheet(const base::FilePath& stylesheet_path) {
std::string stylesheet;
const bool success = base::ReadFileToString(stylesheet_path, &stylesheet);
Expand All @@ -36,7 +52,7 @@ std::string GetDistilledPageStylesheet(const base::FilePath& stylesheet_path) {
IDR_SPEEDREADER_STYLE_DESKTOP);
}

return "<style id=\"brave_speedreader_style\">" + stylesheet + "</style>";
return WrapStylesheetWithCSP(stylesheet);
}

} // namespace
Expand All @@ -46,11 +62,9 @@ SpeedreaderRewriterService::SpeedreaderRewriterService(
: component_(new speedreader::SpeedreaderComponent(delegate)),
speedreader_(new speedreader::SpeedReader) {
// Load the built-in stylesheet as the default
content_stylesheet_ =
"<style id=\"brave_speedreader_style\">" +
content_stylesheet_ = WrapStylesheetWithCSP(
ui::ResourceBundle::GetSharedInstance().LoadDataResourceString(
IDR_SPEEDREADER_STYLE_DESKTOP) +
"</style>";
IDR_SPEEDREADER_STYLE_DESKTOP));

// Check the paths from the component as observer may register
// later than the paths were available in the component.
Expand Down

0 comments on commit e4c2c36

Please sign in to comment.