Commit d1f761a6 authored by Kyle Milka's avatar Kyle Milka Committed by Commit Bot

Revert "[NTP] Initiate server-side resource requests earlier"

This reverts commit ed15ce53.

Previous change prevented promos and search suggestions from appearing properly.

Required manual resolution for merge conflicts.

Bug: 770640
Change-Id: I37add613a23567d57912623c02441cdb68d261e0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1507249
Commit-Queue: Kyle Milka <kmilka@chromium.org>
Reviewed-by: default avatarKristi Park <kristipark@chromium.org>
Cr-Commit-Position: refs/heads/master@{#638638}
parent 30381c30
...@@ -129,7 +129,6 @@ var IDS = { ...@@ -129,7 +129,6 @@ var IDS = {
NOTIFICATION_CLOSE_BUTTON: 'mv-notice-x', NOTIFICATION_CLOSE_BUTTON: 'mv-notice-x',
NOTIFICATION_MESSAGE: 'mv-msg', NOTIFICATION_MESSAGE: 'mv-msg',
NTP_CONTENTS: 'ntp-contents', NTP_CONTENTS: 'ntp-contents',
OGB: 'one-google',
PROMO: 'promo', PROMO: 'promo',
RESTORE_ALL_LINK: 'mv-restore', RESTORE_ALL_LINK: 'mv-restore',
SUGGESTIONS: 'suggestions', SUGGESTIONS: 'suggestions',
...@@ -968,15 +967,32 @@ function handlePostMessage(event) { ...@@ -968,15 +967,32 @@ function handlePostMessage(event) {
if (cmd === 'loaded') { if (cmd === 'loaded') {
tilesAreLoaded = true; tilesAreLoaded = true;
if (configData.isGooglePage) { if (configData.isGooglePage) {
// Show search suggestions, promo, and the OGB if they were previously // Show search suggestions if they were previously hidden.
// hidden.
if ($(IDS.SUGGESTIONS)) { if ($(IDS.SUGGESTIONS)) {
$(IDS.SUGGESTIONS).style.visibility = 'visible'; $(IDS.SUGGESTIONS).style.visibility = 'visible';
} }
if ($(IDS.PROMO)) { if (!$('one-google-loader')) {
$(IDS.PROMO).classList.remove(CLASSES.HIDE_PROMO); // Load the OneGoogleBar script. It'll create a global variable name
// "og" which is a dict corresponding to the native OneGoogleBarData
// type. We do this only after all the tiles have loaded, to avoid
// slowing down the main page load.
var ogScript = document.createElement('script');
ogScript.id = 'one-google-loader';
ogScript.src = 'chrome-search://local-ntp/one-google.js';
document.body.appendChild(ogScript);
ogScript.onload = function() {
injectOneGoogleBar(og);
};
}
if (!$('promo-loader')) {
var promoScript = document.createElement('script');
promoScript.id = 'promo-loader';
promoScript.src = 'chrome-search://local-ntp/promo.js';
document.body.appendChild(promoScript);
promoScript.onload = function() {
injectPromo(promo);
};
} }
$(IDS.OGB).classList.remove('hidden');
$(customBackgrounds.IDS.CUSTOM_LINKS_RESTORE_DEFAULT) $(customBackgrounds.IDS.CUSTOM_LINKS_RESTORE_DEFAULT)
.classList.toggle( .classList.toggle(
customBackgrounds.CLASSES.OPTION_DISABLED, customBackgrounds.CLASSES.OPTION_DISABLED,
...@@ -1013,15 +1029,11 @@ function handlePostMessage(event) { ...@@ -1013,15 +1029,11 @@ function handlePostMessage(event) {
} }
} }
/** function showSearchSuggestions() {
* Request data for search suggestions, promo, and the OGB. Insert it into // Inject search suggestions as early as possible to avoid shifting of other
* the page once it's available. For search suggestions this should be almost // elements.
* immediately as cached data is always used. Promos and the OGB may need
* to wait for the asynchronous network request to complete.
*/
function requestAndInsertGoogleResources() {
if (!$('search-suggestions-loader')) { if (!$('search-suggestions-loader')) {
let ssScript = document.createElement('script'); var ssScript = document.createElement('script');
ssScript.id = 'search-suggestions-loader'; ssScript.id = 'search-suggestions-loader';
ssScript.src = 'chrome-search://local-ntp/search-suggestions.js'; ssScript.src = 'chrome-search://local-ntp/search-suggestions.js';
ssScript.async = false; ssScript.async = false;
...@@ -1030,26 +1042,6 @@ function requestAndInsertGoogleResources() { ...@@ -1030,26 +1042,6 @@ function requestAndInsertGoogleResources() {
injectSearchSuggestions(search_suggestions); injectSearchSuggestions(search_suggestions);
}; };
} }
if (!$('one-google-loader')) {
// Load the OneGoogleBar script. It'll create a global variable |og| which
// is a JSON object corresponding to the native OneGoogleBarData type.
let ogScript = document.createElement('script');
ogScript.id = 'one-google-loader';
ogScript.src = 'chrome-search://local-ntp/one-google.js';
document.body.appendChild(ogScript);
ogScript.onload = function() {
injectOneGoogleBar(og);
};
}
if (!$('promo-loader')) {
let promoScript = document.createElement('script');
promoScript.id = 'promo-loader';
promoScript.src = 'chrome-search://local-ntp/promo.js';
document.body.appendChild(promoScript);
promoScript.onload = function() {
injectPromo(promo);
};
}
} }
...@@ -1115,7 +1107,7 @@ function init() { ...@@ -1115,7 +1107,7 @@ function init() {
var searchboxApiHandle = embeddedSearchApiHandle.searchBox; var searchboxApiHandle = embeddedSearchApiHandle.searchBox;
if (configData.isGooglePage) { if (configData.isGooglePage) {
requestAndInsertGoogleResources(); showSearchSuggestions();
enableMDIcons(); enableMDIcons();
ntpApiHandle.onaddcustomlinkdone = onAddCustomLinkDone; ntpApiHandle.onaddcustomlinkdone = onAddCustomLinkDone;
...@@ -1338,7 +1330,6 @@ function injectPromo(promo) { ...@@ -1338,7 +1330,6 @@ function injectPromo(promo) {
let promoContainer = document.createElement('div'); let promoContainer = document.createElement('div');
promoContainer.id = IDS.PROMO; promoContainer.id = IDS.PROMO;
promoContainer.innerHTML += promo.promoHtml; promoContainer.innerHTML += promo.promoHtml;
promoContainer.classList.add(CLASSES.HIDE_PROMO);
$(IDS.NTP_CONTENTS).appendChild(promoContainer); $(IDS.NTP_CONTENTS).appendChild(promoContainer);
if (promo.promoLogUrl) { if (promo.promoLogUrl) {
...@@ -1375,10 +1366,6 @@ function injectSearchSuggestions(suggestions) { ...@@ -1375,10 +1366,6 @@ function injectSearchSuggestions(suggestions) {
* doesn't block the main page load. * doesn't block the main page load.
*/ */
function injectOneGoogleBar(ogb) { function injectOneGoogleBar(ogb) {
if (ogb.barHtml == '') {
return;
}
var inHeadStyle = document.createElement('style'); var inHeadStyle = document.createElement('style');
inHeadStyle.type = 'text/css'; inHeadStyle.type = 'text/css';
inHeadStyle.appendChild(document.createTextNode(ogb.inHeadStyle)); inHeadStyle.appendChild(document.createTextNode(ogb.inHeadStyle));
...@@ -1391,8 +1378,9 @@ function injectOneGoogleBar(ogb) { ...@@ -1391,8 +1378,9 @@ function injectOneGoogleBar(ogb) {
renderOneGoogleBarTheme(); renderOneGoogleBarTheme();
var ogElem = $(IDS.OGB); var ogElem = $('one-google');
ogElem.innerHTML = ogb.barHtml; ogElem.innerHTML = ogb.barHtml;
ogElem.classList.remove('hidden');
var afterBarScript = document.createElement('script'); var afterBarScript = document.createElement('script');
afterBarScript.type = 'text/javascript'; afterBarScript.type = 'text/javascript';
......
...@@ -367,42 +367,28 @@ base::Value ConvertCollectionImageToDict( ...@@ -367,42 +367,28 @@ base::Value ConvertCollectionImageToDict(
return images; return images;
} }
scoped_refptr<base::RefCountedString> GetOGBString( std::unique_ptr<base::DictionaryValue> ConvertOGBDataToDict(
const base::Optional<OneGoogleBarData>& og) { const OneGoogleBarData& og) {
base::DictionaryValue dict; auto result = std::make_unique<base::DictionaryValue>();
if (og.has_value()) { result->SetString("barHtml", og.bar_html);
dict.SetString("barHtml", og->bar_html); result->SetString("inHeadScript", og.in_head_script);
dict.SetString("inHeadScript", og->in_head_script); result->SetString("inHeadStyle", og.in_head_style);
dict.SetString("inHeadStyle", og->in_head_style); result->SetString("afterBarScript", og.after_bar_script);
dict.SetString("afterBarScript", og->after_bar_script); result->SetString("endOfBodyHtml", og.end_of_body_html);
dict.SetString("endOfBodyHtml", og->end_of_body_html); result->SetString("endOfBodyScript", og.end_of_body_script);
dict.SetString("endOfBodyScript", og->end_of_body_script); return result;
} else {
dict.SetString("barHtml", std::string());
}
std::string js;
base::JSONWriter::Write(dict, &js);
js = "var og = " + js + ";";
return scoped_refptr<base::RefCountedString>(
base::RefCountedString::TakeString(&js));
} }
scoped_refptr<base::RefCountedString> GetPromoString( std::unique_ptr<base::DictionaryValue> ConvertPromoDataToDict(
const base::Optional<PromoData>& promo) { const base::Optional<PromoData>& promo) {
base::DictionaryValue dict; auto result = std::make_unique<base::DictionaryValue>();
if (promo.has_value()) { if (promo.has_value()) {
dict.SetString("promoHtml", promo->promo_html); result->SetString("promoHtml", promo->promo_html);
dict.SetString("promoLogUrl", promo->promo_log_url.spec()); result->SetString("promoLogUrl", promo->promo_log_url.spec());
} else { } else {
dict.SetString("promoHtml", std::string()); result->SetString("promoHtml", std::string());
} }
return result;
std::string js;
base::JSONWriter::Write(dict, &js);
js = "var promo = " + js + ";";
return scoped_refptr<base::RefCountedString>(
base::RefCountedString::TakeString(&js));
} }
std::unique_ptr<base::DictionaryValue> ConvertSearchSuggestDataToDict( std::unique_ptr<base::DictionaryValue> ConvertSearchSuggestDataToDict(
...@@ -545,10 +531,6 @@ class LocalNtpSource::SearchConfigurationProvider ...@@ -545,10 +531,6 @@ class LocalNtpSource::SearchConfigurationProvider
UpdateConfigData(); UpdateConfigData();
} }
bool DefaultSearchProviderIsGoogle() {
return search::DefaultSearchProviderIsGoogle(service_);
}
~SearchConfigurationProvider() override { ~SearchConfigurationProvider() override {
if (service_) if (service_)
service_->RemoveObserver(this); service_->RemoveObserver(this);
...@@ -830,32 +812,40 @@ void LocalNtpSource::StartDataRequest( ...@@ -830,32 +812,40 @@ void LocalNtpSource::StartDataRequest(
if (stripped_path == kOneGoogleBarScriptFilename) { if (stripped_path == kOneGoogleBarScriptFilename) {
if (!one_google_bar_service_) { if (!one_google_bar_service_) {
callback.Run(nullptr); callback.Run(nullptr);
} else { return;
ServeOneGoogleBarWhenAvailable(callback);
} }
one_google_bar_requests_.emplace_back(base::TimeTicks::Now(), callback);
one_google_bar_service_->Refresh();
return; return;
} }
if (stripped_path == kPromoScriptFilename) { if (stripped_path == kPromoScriptFilename) {
if (!promo_service_) { if (!promo_service_) {
callback.Run(nullptr); callback.Run(nullptr);
} else { return;
ServePromoWhenAvailable(callback);
} }
// TODO(crbug/909931): There's no need to fetch the promo on each load,
// we can sometimes use cached data.
promo_requests_.emplace_back(base::TimeTicks::Now(), callback);
promo_service_->Refresh();
return; return;
} }
// Search suggestions always used a cached value, so there is no need to
// refresh the data until the old data is used.
if (stripped_path == kSearchSuggestionsScriptFilename) { if (stripped_path == kSearchSuggestionsScriptFilename) {
if (!search_suggest_service_) { if (!search_suggest_service_) {
callback.Run(nullptr); callback.Run(nullptr);
} else { return;
ServeSearchSuggestionsIfAvailable(callback);
pending_search_suggest_request_ = base::TimeTicks::Now();
search_suggest_service_->Refresh();
} }
MaybeServeSearchSuggestions(callback);
search_suggest_requests_.emplace_back(base::TimeTicks::Now());
search_suggest_service_->Refresh();
return; return;
} }
...@@ -891,10 +881,6 @@ void LocalNtpSource::StartDataRequest( ...@@ -891,10 +881,6 @@ void LocalNtpSource::StartDataRequest(
#endif // !defined(GOOGLE_CHROME_BUILD) #endif // !defined(GOOGLE_CHROME_BUILD)
if (stripped_path == kMainHtmlFilename) { if (stripped_path == kMainHtmlFilename) {
if (search_config_provider_->DefaultSearchProviderIsGoogle()) {
InitiatePromoAndOGBRequests();
}
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();
...@@ -1150,40 +1136,37 @@ void LocalNtpSource::OnPromoServiceShuttingDown() { ...@@ -1150,40 +1136,37 @@ void LocalNtpSource::OnPromoServiceShuttingDown() {
void LocalNtpSource::OnSearchSuggestDataUpdated() { void LocalNtpSource::OnSearchSuggestDataUpdated() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI); DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (pending_search_suggest_request_.has_value()) {
return;
}
SearchSuggestLoader::Status result = SearchSuggestLoader::Status result =
search_suggest_service_->search_suggest_status(); search_suggest_service_->search_suggest_status();
base::TimeDelta delta = base::TimeTicks now = base::TimeTicks::Now();
base::TimeTicks::Now() - *pending_search_suggest_request_; for (const auto& request : search_suggest_requests_) {
UMA_HISTOGRAM_MEDIUM_TIMES("NewTabPage.SearchSuggestions.RequestLatency", base::TimeDelta delta = now - request.start_time;
delta); UMA_HISTOGRAM_MEDIUM_TIMES("NewTabPage.SearchSuggestions.RequestLatency",
SearchSuggestionsRequestStatus request_status = delta);
SearchSuggestionsRequestStatus::UNKNOWN_ERROR; SearchSuggestionsRequestStatus request_status =
SearchSuggestionsRequestStatus::UNKNOWN_ERROR;
if (result == SearchSuggestLoader::Status::SIGNED_OUT) {
request_status = SearchSuggestionsRequestStatus::SIGNED_OUT; if (result == SearchSuggestLoader::Status::SIGNED_OUT) {
} else if (result == SearchSuggestLoader::Status::OPTED_OUT) { request_status = SearchSuggestionsRequestStatus::SIGNED_OUT;
request_status = SearchSuggestionsRequestStatus::OPTED_OUT; } else if (result == SearchSuggestLoader::Status::OPTED_OUT) {
} else if (result == SearchSuggestLoader::Status::IMPRESSION_CAP) { request_status = SearchSuggestionsRequestStatus::OPTED_OUT;
request_status = SearchSuggestionsRequestStatus::IMPRESSION_CAP; } else if (result == SearchSuggestLoader::Status::IMPRESSION_CAP) {
} else if (result == SearchSuggestLoader::Status::REQUESTS_FROZEN) { request_status = SearchSuggestionsRequestStatus::IMPRESSION_CAP;
request_status = SearchSuggestionsRequestStatus::FROZEN; } else if (result == SearchSuggestLoader::Status::REQUESTS_FROZEN) {
} else if (result == SearchSuggestLoader::Status::OK) { request_status = SearchSuggestionsRequestStatus::FROZEN;
request_status = SearchSuggestionsRequestStatus::SENT; } else if (result == SearchSuggestLoader::Status::OK) {
UMA_HISTOGRAM_MEDIUM_TIMES( request_status = SearchSuggestionsRequestStatus::SENT;
"NewTabPage.SearchSuggestions.RequestLatency.Success", delta); UMA_HISTOGRAM_MEDIUM_TIMES(
} else if (result == SearchSuggestLoader::Status::FATAL_ERROR) { "NewTabPage.SearchSuggestions.RequestLatency.Success", delta);
request_status = SearchSuggestionsRequestStatus::FATAL_ERROR; } else if (result == SearchSuggestLoader::Status::FATAL_ERROR) {
UMA_HISTOGRAM_MEDIUM_TIMES( request_status = SearchSuggestionsRequestStatus::FATAL_ERROR;
"NewTabPage.SearchSuggestions.RequestLatency.Failure", delta); UMA_HISTOGRAM_MEDIUM_TIMES(
"NewTabPage.SearchSuggestions.RequestLatency.Failure", delta);
}
UMA_HISTOGRAM_ENUMERATION("NewTabPage.SearchSuggestions.RequestStatus",
request_status);
} }
UMA_HISTOGRAM_ENUMERATION("NewTabPage.SearchSuggestions.RequestStatus", search_suggest_requests_.clear();
request_status);
pending_search_suggest_request_ = base::nullopt;
} }
void LocalNtpSource::OnSearchSuggestServiceShuttingDown() { void LocalNtpSource::OnSearchSuggestServiceShuttingDown() {
...@@ -1192,8 +1175,7 @@ void LocalNtpSource::OnSearchSuggestServiceShuttingDown() { ...@@ -1192,8 +1175,7 @@ void LocalNtpSource::OnSearchSuggestServiceShuttingDown() {
search_suggest_service_observer_.RemoveAll(); search_suggest_service_observer_.RemoveAll();
search_suggest_service_ = nullptr; search_suggest_service_ = nullptr;
} }
void LocalNtpSource::MaybeServeSearchSuggestions(
void LocalNtpSource::ServeSearchSuggestionsIfAvailable(
const content::URLDataSource::GotDataCallback& callback) { const content::URLDataSource::GotDataCallback& callback) {
base::Optional<SearchSuggestData> data = base::Optional<SearchSuggestData> data =
search_suggest_service_->search_suggest_data(); search_suggest_service_->search_suggest_data();
...@@ -1213,89 +1195,59 @@ void LocalNtpSource::ServeOneGoogleBar( ...@@ -1213,89 +1195,59 @@ void LocalNtpSource::ServeOneGoogleBar(
const base::Optional<OneGoogleBarData>& data) { const base::Optional<OneGoogleBarData>& data) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI); DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (!pending_one_google_bar_request_.has_value()) { if (one_google_bar_requests_.empty())
return; return;
}
scoped_refptr<base::RefCountedString> result; scoped_refptr<base::RefCountedString> result;
if (data.has_value()) { if (data.has_value()) {
result = GetOGBString(data); std::string js;
} base::JSONWriter::Write(*ConvertOGBDataToDict(*data), &js);
js = "var og = " + js + ";";
base::TimeDelta delta = result = base::RefCountedString::TakeString(&js);
base::TimeTicks::Now() - *pending_one_google_bar_request_;
UMA_HISTOGRAM_MEDIUM_TIMES("NewTabPage.OneGoogleBar.RequestLatency", delta);
if (result) {
UMA_HISTOGRAM_MEDIUM_TIMES("NewTabPage.OneGoogleBar.RequestLatency.Success",
delta);
} else {
UMA_HISTOGRAM_MEDIUM_TIMES("NewTabPage.OneGoogleBar.RequestLatency.Failure",
delta);
}
for (const auto& callback : one_google_bar_callbacks_) {
callback.Run(result);
} }
pending_one_google_bar_request_ = base::nullopt;
one_google_bar_callbacks_.clear();
}
void LocalNtpSource::ServeOneGoogleBarWhenAvailable(
const content::URLDataSource::GotDataCallback& callback) {
base::Optional<OneGoogleBarData> data =
one_google_bar_service_->one_google_bar_data();
if (!pending_one_google_bar_request_.has_value()) { base::TimeTicks now = base::TimeTicks::Now();
callback.Run(GetOGBString(data)); for (const auto& request : one_google_bar_requests_) {
} else { request.callback.Run(result);
one_google_bar_callbacks_.emplace_back(callback); base::TimeDelta delta = now - request.start_time;
UMA_HISTOGRAM_MEDIUM_TIMES("NewTabPage.OneGoogleBar.RequestLatency", delta);
if (result) {
UMA_HISTOGRAM_MEDIUM_TIMES(
"NewTabPage.OneGoogleBar.RequestLatency.Success", delta);
} else {
UMA_HISTOGRAM_MEDIUM_TIMES(
"NewTabPage.OneGoogleBar.RequestLatency.Failure", delta);
}
} }
one_google_bar_requests_.clear();
} }
void LocalNtpSource::ServePromo(const base::Optional<PromoData>& data) { void LocalNtpSource::ServePromo(const base::Optional<PromoData>& data) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI); DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (!pending_promo_request_.has_value()) { if (promo_requests_.empty())
return; return;
}
scoped_refptr<base::RefCountedString> result = GetPromoString(data);
base::TimeDelta delta = base::TimeTicks::Now() - *pending_promo_request_; scoped_refptr<base::RefCountedString> result;
UMA_HISTOGRAM_MEDIUM_TIMES("NewTabPage.Promos.RequestLatency", delta); std::string js;
if (result) { base::JSONWriter::Write(*ConvertPromoDataToDict(data), &js);
UMA_HISTOGRAM_MEDIUM_TIMES("NewTabPage.Promos.RequestLatency.Success", js = "var promo = " + js + ";";
delta); result = base::RefCountedString::TakeString(&js);
} else {
UMA_HISTOGRAM_MEDIUM_TIMES("NewTabPage.Promos.RequestLatency.Failure",
delta);
}
for (const auto& callback : promo_callbacks_) {
callback.Run(result);
}
pending_promo_request_ = base::nullopt;
promo_callbacks_.clear();
}
void LocalNtpSource::ServePromoWhenAvailable(
const content::URLDataSource::GotDataCallback& callback) {
base::Optional<PromoData> data = promo_service_->promo_data();
if (!pending_promo_request_.has_value()) {
callback.Run(GetPromoString(data));
} else {
promo_callbacks_.emplace_back(callback);
}
}
void LocalNtpSource::InitiatePromoAndOGBRequests() { base::TimeTicks now = base::TimeTicks::Now();
if (one_google_bar_service_) { for (const auto& request : promo_requests_) {
pending_one_google_bar_request_ = base::TimeTicks::Now(); request.callback.Run(result);
one_google_bar_service_->Refresh(); base::TimeDelta delta = now - request.start_time;
} UMA_HISTOGRAM_MEDIUM_TIMES("NewTabPage.Promos.RequestLatency", delta);
if (promo_service_) { if (result) {
pending_promo_request_ = base::TimeTicks::Now(); UMA_HISTOGRAM_MEDIUM_TIMES("NewTabPage.Promos.RequestLatency.Success",
promo_service_->Refresh(); delta);
} else {
UMA_HISTOGRAM_MEDIUM_TIMES("NewTabPage.Promos.RequestLatency.Failure",
delta);
}
} }
promo_requests_.clear();
} }
LocalNtpSource::NtpBackgroundRequest::NtpBackgroundRequest( LocalNtpSource::NtpBackgroundRequest::NtpBackgroundRequest(
...@@ -1307,3 +1259,31 @@ LocalNtpSource::NtpBackgroundRequest::NtpBackgroundRequest( ...@@ -1307,3 +1259,31 @@ LocalNtpSource::NtpBackgroundRequest::NtpBackgroundRequest(
const NtpBackgroundRequest&) = default; const NtpBackgroundRequest&) = default;
LocalNtpSource::NtpBackgroundRequest::~NtpBackgroundRequest() = default; LocalNtpSource::NtpBackgroundRequest::~NtpBackgroundRequest() = default;
LocalNtpSource::OneGoogleBarRequest::OneGoogleBarRequest(
base::TimeTicks start_time,
const content::URLDataSource::GotDataCallback& callback)
: start_time(start_time), callback(callback) {}
LocalNtpSource::OneGoogleBarRequest::OneGoogleBarRequest(
const OneGoogleBarRequest&) = default;
LocalNtpSource::OneGoogleBarRequest::~OneGoogleBarRequest() = default;
LocalNtpSource::PromoRequest::PromoRequest(
base::TimeTicks start_time,
const content::URLDataSource::GotDataCallback& callback)
: start_time(start_time), callback(callback) {}
LocalNtpSource::PromoRequest::PromoRequest(const PromoRequest&) = default;
LocalNtpSource::PromoRequest::~PromoRequest() = default;
LocalNtpSource::SearchSuggestRequest::SearchSuggestRequest(
base::TimeTicks start_time)
: start_time(start_time) {}
LocalNtpSource::SearchSuggestRequest::SearchSuggestRequest(
const SearchSuggestRequest&) = default;
LocalNtpSource::SearchSuggestRequest::~SearchSuggestRequest() = default;
...@@ -69,6 +69,35 @@ class LocalNtpSource : public content::URLDataSource, ...@@ -69,6 +69,35 @@ class LocalNtpSource : public content::URLDataSource,
content::URLDataSource::GotDataCallback callback; content::URLDataSource::GotDataCallback callback;
}; };
struct OneGoogleBarRequest {
OneGoogleBarRequest(
base::TimeTicks start_time,
const content::URLDataSource::GotDataCallback& callback);
OneGoogleBarRequest(const OneGoogleBarRequest&);
~OneGoogleBarRequest();
base::TimeTicks start_time;
content::URLDataSource::GotDataCallback callback;
};
struct PromoRequest {
PromoRequest(base::TimeTicks start_time,
const content::URLDataSource::GotDataCallback& callback);
PromoRequest(const PromoRequest&);
~PromoRequest();
base::TimeTicks start_time;
content::URLDataSource::GotDataCallback callback;
};
struct SearchSuggestRequest {
explicit SearchSuggestRequest(base::TimeTicks start_time);
explicit SearchSuggestRequest(const SearchSuggestRequest&);
~SearchSuggestRequest();
base::TimeTicks start_time;
};
// Overridden from content::URLDataSource: // Overridden from content::URLDataSource:
std::string GetSource() const override; std::string GetSource() const override;
void StartDataRequest( void StartDataRequest(
...@@ -102,32 +131,13 @@ class LocalNtpSource : public content::URLDataSource, ...@@ -102,32 +131,13 @@ class LocalNtpSource : public content::URLDataSource,
void OnSearchSuggestDataUpdated() override; void OnSearchSuggestDataUpdated() override;
void OnSearchSuggestServiceShuttingDown() override; void OnSearchSuggestServiceShuttingDown() override;
// Called when the OGB data is available and serves |data| to any pending
// request from the NTP.
void ServeOneGoogleBar(const base::Optional<OneGoogleBarData>& data); void ServeOneGoogleBar(const base::Optional<OneGoogleBarData>& data);
// Called when the page requests OGB data. If the data is available it
// is returned immediately, otherwise it is returned when it becomes available
// in ServeOneGoogleBar.
void ServeOneGoogleBarWhenAvailable(
const content::URLDataSource::GotDataCallback& callback);
// Called when the promo data is available and serves |data| to any pending
// requests from the NTP.
void ServePromo(const base::Optional<PromoData>& data); void ServePromo(const base::Optional<PromoData>& data);
// Called when the page requests promo data. If the data is available it
// is returned immediately, otherwise it is returned when it becomes
// available in ServePromo.
void ServePromoWhenAvailable(
const content::URLDataSource::GotDataCallback& callback);
// If suggestion data is available return it immediately, otherwise no search void MaybeServeSearchSuggestions(
// suggestions will be shown on this NTP load.
void ServeSearchSuggestionsIfAvailable(
const content::URLDataSource::GotDataCallback& callback); const content::URLDataSource::GotDataCallback& callback);
// Start requests for the promo and OGB.
void InitiatePromoAndOGBRequests();
Profile* const profile_; Profile* const profile_;
std::vector<NtpBackgroundRequest> ntp_background_collections_requests_; std::vector<NtpBackgroundRequest> ntp_background_collections_requests_;
...@@ -138,23 +148,20 @@ class LocalNtpSource : public content::URLDataSource, ...@@ -138,23 +148,20 @@ class LocalNtpSource : public content::URLDataSource,
ScopedObserver<NtpBackgroundService, NtpBackgroundServiceObserver> ScopedObserver<NtpBackgroundService, NtpBackgroundServiceObserver>
ntp_background_service_observer_; ntp_background_service_observer_;
base::Optional<base::TimeTicks> pending_one_google_bar_request_; std::vector<OneGoogleBarRequest> one_google_bar_requests_;
std::vector<content::URLDataSource::GotDataCallback>
one_google_bar_callbacks_;
OneGoogleBarService* one_google_bar_service_; OneGoogleBarService* one_google_bar_service_;
ScopedObserver<OneGoogleBarService, OneGoogleBarServiceObserver> ScopedObserver<OneGoogleBarService, OneGoogleBarServiceObserver>
one_google_bar_service_observer_; one_google_bar_service_observer_;
base::Optional<base::TimeTicks> pending_promo_request_; std::vector<PromoRequest> promo_requests_;
std::vector<content::URLDataSource::GotDataCallback> promo_callbacks_;
PromoService* promo_service_; PromoService* promo_service_;
ScopedObserver<PromoService, PromoServiceObserver> promo_service_observer_; ScopedObserver<PromoService, PromoServiceObserver> promo_service_observer_;
base::Optional<base::TimeTicks> pending_search_suggest_request_; std::vector<SearchSuggestRequest> search_suggest_requests_;
SearchSuggestService* search_suggest_service_; SearchSuggestService* search_suggest_service_;
......
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