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';
......
This diff is collapsed.
...@@ -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