Commit 773623d2 authored by Marc Treib's avatar Marc Treib Committed by Commit Bot

Local NTP: Load config.js dynamically

With this, no checksum is required for config.js anymore, which fixes a
race condition between serving the checksum and serving the contents
themselves. It also lets us remove quite a lot of plumbing :)

Bug: 792490, 791969
Cq-Include-Trybots: master.tryserver.chromium.linux:closure_compilation
Change-Id: I609ef274325bddb49bcbe6e90d58d35b7a385920
Reviewed-on: https://chromium-review.googlesource.com/811185
Commit-Queue: Marc Treib <treib@chromium.org>
Reviewed-by: default avatarChris Pickel <sfiera@chromium.org>
Cr-Commit-Position: refs/heads/master@{#522428}
parent 03413422
...@@ -54,6 +54,7 @@ html { ...@@ -54,6 +54,7 @@ html {
body { body {
background-attachment: fixed !important; background-attachment: fixed !important;
cursor: default; cursor: default;
display: none;
font-family: arial, sans-serif; font-family: arial, sans-serif;
font-size: small; font-size: small;
height: 100%; height: 100%;
...@@ -61,6 +62,10 @@ body { ...@@ -61,6 +62,10 @@ body {
overflow-x: hidden; overflow-x: hidden;
} }
body.inited {
display: block;
}
/* Button defaults vary by platform. Reset CSS so that the NTP can use buttons /* Button defaults vary by platform. Reset CSS so that the NTP can use buttons
* as a kind of clickable div. */ * as a kind of clickable div. */
button { button {
......
...@@ -7,8 +7,6 @@ ...@@ -7,8 +7,6 @@
<link rel="stylesheet" href="chrome-search://local-ntp/theme.css"></link> <link rel="stylesheet" href="chrome-search://local-ntp/theme.css"></link>
<link rel="stylesheet" href="chrome-search://local-ntp/local-ntp.css"></link> <link rel="stylesheet" href="chrome-search://local-ntp/local-ntp.css"></link>
<link rel="stylesheet" href="chrome-search://local-ntp/voice.css"></link> <link rel="stylesheet" href="chrome-search://local-ntp/voice.css"></link>
<script src="chrome-search://local-ntp/config.js"
{{CONFIG_INTEGRITY}}></script>
<script src="chrome-search://local-ntp/local-ntp.js" <script src="chrome-search://local-ntp/local-ntp.js"
{{LOCAL_NTP_INTEGRITY}}></script> {{LOCAL_NTP_INTEGRITY}}></script>
<script src="chrome-search://local-ntp/voice.js" <script src="chrome-search://local-ntp/voice.js"
......
...@@ -73,6 +73,7 @@ var CLASSES = { ...@@ -73,6 +73,7 @@ var CLASSES = {
FAKEBOX_DRAG_FOCUS: 'fakebox-drag-focused', FAKEBOX_DRAG_FOCUS: 'fakebox-drag-focused',
HIDE_FAKEBOX_AND_LOGO: 'hide-fakebox-logo', HIDE_FAKEBOX_AND_LOGO: 'hide-fakebox-logo',
HIDE_NOTIFICATION: 'mv-notice-hide', HIDE_NOTIFICATION: 'mv-notice-hide',
INITED: 'inited', // Reveals the <body> once init() is done.
LEFT_ALIGN_ATTRIBUTION: 'left-align-attribution', LEFT_ALIGN_ATTRIBUTION: 'left-align-attribution',
// Vertically centers the most visited section for a non-Google provided page. // Vertically centers the most visited section for a non-Google provided page.
NON_GOOGLE_PAGE: 'non-google-page', NON_GOOGLE_PAGE: 'non-google-page',
...@@ -732,6 +733,17 @@ function init() { ...@@ -732,6 +733,17 @@ function init() {
}; };
window.addEventListener('message', handlePostMessage); window.addEventListener('message', handlePostMessage);
document.body.classList.add(CLASSES.INITED);
}
function loadConfig() {
var configScript = document.createElement('script');
configScript.type = 'text/javascript';
configScript.src = 'chrome-search://local-ntp/config.js';
configScript.onload = init;
document.head.appendChild(configScript);
} }
...@@ -739,7 +751,7 @@ function init() { ...@@ -739,7 +751,7 @@ function init() {
* Binds event listeners. * Binds event listeners.
*/ */
function listen() { function listen() {
document.addEventListener('DOMContentLoaded', init); document.addEventListener('DOMContentLoaded', loadConfig);
} }
......
...@@ -45,8 +45,6 @@ ...@@ -45,8 +45,6 @@
#include "components/search_provider_logos/logo_tracker.h" #include "components/search_provider_logos/logo_tracker.h"
#include "components/strings/grit/components_strings.h" #include "components/strings/grit/components_strings.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "crypto/secure_hash.h"
#include "net/base/hash_value.h"
#include "net/base/url_util.h" #include "net/base/url_util.h"
#include "net/url_request/url_request.h" #include "net/url_request/url_request.h"
#include "third_party/skia/include/core/SkColor.h" #include "third_party/skia/include/core/SkColor.h"
...@@ -184,28 +182,12 @@ std::string GetConfigData(bool is_google, const GURL& google_base_url) { ...@@ -184,28 +182,12 @@ std::string GetConfigData(bool is_google, const GURL& google_base_url) {
return config_data_js; return config_data_js;
} }
std::string GetIntegritySha256Value(const std::string& data) {
// Compute the sha256 hash.
net::SHA256HashValue hash_value;
std::unique_ptr<crypto::SecureHash> hash(
crypto::SecureHash::Create(crypto::SecureHash::SHA256));
hash->Update(data.data(), data.size());
hash->Finish(&hash_value, sizeof(hash_value));
// Base64-encode it.
base::StringPiece hash_value_str(
reinterpret_cast<const char*>(hash_value.data), sizeof(hash_value));
std::string result;
base::Base64Encode(hash_value_str, &result);
return result;
}
std::string GetThemeCSS(Profile* profile) { std::string GetThemeCSS(Profile* profile) {
SkColor background_color = SkColor background_color =
ThemeService::GetThemeProviderForProfile(profile) ThemeService::GetThemeProviderForProfile(profile)
.GetColor(ThemeProperties::COLOR_NTP_BACKGROUND); .GetColor(ThemeProperties::COLOR_NTP_BACKGROUND);
return base::StringPrintf("body { background-color: #%02X%02X%02X; }", return base::StringPrintf("html { background-color: #%02X%02X%02X; }",
SkColorGetR(background_color), SkColorGetR(background_color),
SkColorGetG(background_color), SkColorGetG(background_color),
SkColorGetB(background_color)); SkColorGetB(background_color));
...@@ -277,20 +259,8 @@ std::unique_ptr<base::DictionaryValue> ConvertLogoMetadataToDict( ...@@ -277,20 +259,8 @@ std::unique_ptr<base::DictionaryValue> ConvertLogoMetadataToDict(
class LocalNtpSource::GoogleSearchProviderTracker class LocalNtpSource::GoogleSearchProviderTracker
: public TemplateURLServiceObserver { : public TemplateURLServiceObserver {
public: public:
using SearchProviderIsGoogleChangedCallback = explicit GoogleSearchProviderTracker(TemplateURLService* service)
base::Callback<void(bool is_google)>; : service_(service), is_google_(false) {
using GoogleBaseUrlChangedCallback =
base::Callback<void(const GURL& google_base_url)>;
GoogleSearchProviderTracker(
TemplateURLService* service,
const SearchProviderIsGoogleChangedCallback& is_google_callback,
const GoogleBaseUrlChangedCallback& google_base_url_callback)
: service_(service),
is_google_callback_(is_google_callback),
google_base_url_callback_(google_base_url_callback),
is_google_(false) {
DCHECK(service_); DCHECK(service_);
service_->AddObserver(this); service_->AddObserver(this);
is_google_ = search::DefaultSearchProviderIsGoogle(service_); is_google_ = search::DefaultSearchProviderIsGoogle(service_);
...@@ -308,15 +278,8 @@ class LocalNtpSource::GoogleSearchProviderTracker ...@@ -308,15 +278,8 @@ class LocalNtpSource::GoogleSearchProviderTracker
private: private:
void OnTemplateURLServiceChanged() override { void OnTemplateURLServiceChanged() override {
bool old_is_google = is_google_;
is_google_ = search::DefaultSearchProviderIsGoogle(service_); is_google_ = search::DefaultSearchProviderIsGoogle(service_);
if (is_google_ != old_is_google)
is_google_callback_.Run(is_google_);
const GURL old_google_base_url = google_base_url_;
google_base_url_ = GURL(service_->search_terms_data().GoogleBaseURLValue()); google_base_url_ = GURL(service_->search_terms_data().GoogleBaseURLValue());
if (google_base_url_ != old_google_base_url)
google_base_url_callback_.Run(google_base_url_);
} }
void OnTemplateURLServiceShuttingDown() override { void OnTemplateURLServiceShuttingDown() override {
...@@ -325,8 +288,6 @@ class LocalNtpSource::GoogleSearchProviderTracker ...@@ -325,8 +288,6 @@ class LocalNtpSource::GoogleSearchProviderTracker
} }
TemplateURLService* service_; TemplateURLService* service_;
SearchProviderIsGoogleChangedCallback is_google_callback_;
GoogleBaseUrlChangedCallback google_base_url_callback_;
bool is_google_; bool is_google_;
GURL google_base_url_; GURL google_base_url_;
...@@ -447,8 +408,6 @@ LocalNtpSource::LocalNtpSource(Profile* profile) ...@@ -447,8 +408,6 @@ LocalNtpSource::LocalNtpSource(Profile* profile)
OneGoogleBarServiceFactory::GetForProfile(profile_)), OneGoogleBarServiceFactory::GetForProfile(profile_)),
one_google_bar_service_observer_(this), one_google_bar_service_observer_(this),
logo_service_(nullptr), logo_service_(nullptr),
default_search_provider_is_google_(false),
default_search_provider_is_google_io_thread_(false),
weak_ptr_factory_(this) { weak_ptr_factory_(this) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI); DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
...@@ -465,15 +424,8 @@ LocalNtpSource::LocalNtpSource(Profile* profile) ...@@ -465,15 +424,8 @@ LocalNtpSource::LocalNtpSource(Profile* profile)
TemplateURLService* template_url_service = TemplateURLService* template_url_service =
TemplateURLServiceFactory::GetForProfile(profile_); TemplateURLServiceFactory::GetForProfile(profile_);
if (template_url_service) { if (template_url_service) {
google_tracker_ = base::MakeUnique<GoogleSearchProviderTracker>( google_tracker_ =
template_url_service, base::MakeUnique<GoogleSearchProviderTracker>(template_url_service);
base::Bind(&LocalNtpSource::DefaultSearchProviderIsGoogleChanged,
base::Unretained(this)),
base::Bind(&LocalNtpSource::GoogleBaseUrlChanged,
base::Unretained(this)));
DefaultSearchProviderIsGoogleChanged(
google_tracker_->DefaultSearchProviderIsGoogle());
GoogleBaseUrlChanged(google_tracker_->GetGoogleBaseUrl());
} }
} }
...@@ -492,7 +444,8 @@ void LocalNtpSource::StartDataRequest( ...@@ -492,7 +444,8 @@ void LocalNtpSource::StartDataRequest(
std::string stripped_path = StripParameters(path); std::string stripped_path = StripParameters(path);
if (stripped_path == kConfigDataFilename) { if (stripped_path == kConfigDataFilename) {
std::string config_data_js = std::string config_data_js =
GetConfigData(default_search_provider_is_google_, google_base_url_); GetConfigData(google_tracker_->DefaultSearchProviderIsGoogle(),
google_tracker_->GetGoogleBaseUrl());
callback.Run(base::RefCountedString::TakeString(&config_data_js)); callback.Run(base::RefCountedString::TakeString(&config_data_js));
return; return;
} }
...@@ -551,13 +504,6 @@ void LocalNtpSource::StartDataRequest( ...@@ -551,13 +504,6 @@ void LocalNtpSource::StartDataRequest(
std::string html = ui::ResourceBundle::GetSharedInstance() std::string html = ui::ResourceBundle::GetSharedInstance()
.GetRawDataResource(IDR_LOCAL_NTP_HTML) .GetRawDataResource(IDR_LOCAL_NTP_HTML)
.as_string(); .as_string();
std::string config_integrity = base::StringPrintf(
kIntegrityFormat,
GetIntegritySha256Value(
GetConfigData(default_search_provider_is_google_, google_base_url_))
.c_str());
base::ReplaceFirstSubstringAfterOffset(&html, 0, "{{CONFIG_INTEGRITY}}",
config_integrity);
std::string local_ntp_integrity = std::string local_ntp_integrity =
base::StringPrintf(kIntegrityFormat, LOCAL_NTP_JS_INTEGRITY); base::StringPrintf(kIntegrityFormat, LOCAL_NTP_JS_INTEGRITY);
base::ReplaceFirstSubstringAfterOffset(&html, 0, "{{LOCAL_NTP_INTEGRITY}}", base::ReplaceFirstSubstringAfterOffset(&html, 0, "{{LOCAL_NTP_INTEGRITY}}",
...@@ -642,11 +588,7 @@ std::string LocalNtpSource::GetContentSecurityPolicyScriptSrc() const { ...@@ -642,11 +588,7 @@ std::string LocalNtpSource::GetContentSecurityPolicyScriptSrc() const {
#endif // !defined(GOOGLE_CHROME_BUILD) #endif // !defined(GOOGLE_CHROME_BUILD)
return base::StringPrintf( return base::StringPrintf(
"script-src 'strict-dynamic' 'sha256-%s' 'sha256-%s' 'sha256-%s';", "script-src 'strict-dynamic' 'sha256-%s' 'sha256-%s';",
GetIntegritySha256Value(
GetConfigData(default_search_provider_is_google_io_thread_,
google_base_url_io_thread_))
.c_str(),
LOCAL_NTP_JS_INTEGRITY, VOICE_JS_INTEGRITY); LOCAL_NTP_JS_INTEGRITY, VOICE_JS_INTEGRITY);
} }
...@@ -705,40 +647,6 @@ void LocalNtpSource::ServeOneGoogleBar( ...@@ -705,40 +647,6 @@ void LocalNtpSource::ServeOneGoogleBar(
one_google_bar_requests_.clear(); one_google_bar_requests_.clear();
} }
void LocalNtpSource::DefaultSearchProviderIsGoogleChanged(bool is_google) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
default_search_provider_is_google_ = is_google;
content::BrowserThread::PostTask(
content::BrowserThread::IO, FROM_HERE,
base::BindOnce(
&LocalNtpSource::SetDefaultSearchProviderIsGoogleOnIOThread,
weak_ptr_factory_.GetWeakPtr(), is_google));
}
void LocalNtpSource::SetDefaultSearchProviderIsGoogleOnIOThread(
bool is_google) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
default_search_provider_is_google_io_thread_ = is_google;
}
void LocalNtpSource::GoogleBaseUrlChanged(const GURL& google_base_url) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
google_base_url_ = google_base_url;
content::BrowserThread::PostTask(
content::BrowserThread::IO, FROM_HERE,
base::BindOnce(&LocalNtpSource::SetGoogleBaseUrlOnIOThread,
weak_ptr_factory_.GetWeakPtr(), google_base_url));
}
void LocalNtpSource::SetGoogleBaseUrlOnIOThread(const GURL& google_base_url) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
google_base_url_io_thread_ = google_base_url;
}
LocalNtpSource::OneGoogleBarRequest::OneGoogleBarRequest( LocalNtpSource::OneGoogleBarRequest::OneGoogleBarRequest(
base::TimeTicks start_time, base::TimeTicks start_time,
const content::URLDataSource::GotDataCallback& callback) const content::URLDataSource::GotDataCallback& callback)
......
...@@ -74,14 +74,6 @@ class LocalNtpSource : public content::URLDataSource, ...@@ -74,14 +74,6 @@ class LocalNtpSource : public content::URLDataSource,
void ServeOneGoogleBar(const base::Optional<OneGoogleBarData>& data); void ServeOneGoogleBar(const base::Optional<OneGoogleBarData>& data);
void DefaultSearchProviderIsGoogleChanged(bool is_google);
void SetDefaultSearchProviderIsGoogleOnIOThread(bool is_google);
void GoogleBaseUrlChanged(const GURL& google_base_url);
void SetGoogleBaseUrlOnIOThread(const GURL& google_base_url);
Profile* const profile_; Profile* const profile_;
OneGoogleBarService* one_google_bar_service_; OneGoogleBarService* one_google_bar_service_;
...@@ -95,13 +87,6 @@ class LocalNtpSource : public content::URLDataSource, ...@@ -95,13 +87,6 @@ class LocalNtpSource : public content::URLDataSource,
std::vector<OneGoogleBarRequest> one_google_bar_requests_; std::vector<OneGoogleBarRequest> one_google_bar_requests_;
std::unique_ptr<GoogleSearchProviderTracker> google_tracker_; std::unique_ptr<GoogleSearchProviderTracker> google_tracker_;
bool default_search_provider_is_google_;
// A copy of |default_search_provider_is_google_| for use on the IO thread.
bool default_search_provider_is_google_io_thread_;
GURL google_base_url_;
// A copy of |google_base_url_| for use on the IO thread.
GURL google_base_url_io_thread_;
base::WeakPtrFactory<LocalNtpSource> weak_ptr_factory_; base::WeakPtrFactory<LocalNtpSource> weak_ptr_factory_;
......
...@@ -5,11 +5,11 @@ ...@@ -5,11 +5,11 @@
found in the LICENSE file. --> found in the LICENSE file. -->
<head> <head>
<script>window.localNTPUnitTest = true;</script> <script>window.localNTPUnitTest = true;</script>
<script src="chrome-search://local-ntp/config.js" charset="utf-8"></script> <link rel="stylesheet" href="chrome-search://local-ntp/local-ntp.css"></link>
<link rel="stylesheet" href="chrome-search://local-ntp/voice.css"></link>
<script src="chrome-search://local-ntp/local-ntp.js" charset="utf-8"></script> <script src="chrome-search://local-ntp/local-ntp.js" charset="utf-8"></script>
<script src="chrome-search://local-ntp/voice.js" charset="utf-8"></script> <script src="chrome-search://local-ntp/voice.js" charset="utf-8"></script>
<script src="chrome-search://most-visited/util.js" charset="utf-8"></script> <script src="chrome-search://local-ntp/config.js" charset="utf-8"></script>
<link rel="stylesheet" href="chrome-search://local-ntp/local-ntp.css"></link>
<script src="test_utils.js" charset="utf-8"></script> <script src="test_utils.js" charset="utf-8"></script>
<script src="local_ntp_browsertest.js" charset="utf-8"></script> <script src="local_ntp_browsertest.js" charset="utf-8"></script>
<template id="local-ntp-template"> <template id="local-ntp-template">
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment