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

[NTP] Initiate server-side resource requests earlier

Previously requests for promos and the OGB were not sent until
the NTP had almost finished loading. Instead start these requests
when the request for the main HTML page is received.

With this change promos and the OBG will appear along with search
suggestions and the most visited tiles if they're available (which
is now fairly likely). Otherwise they will appear once ready.

Also removes a console error when the OGB failed to load.

Bug: 770640
Change-Id: Iff7a29a002325853115678f12c89c29869c076e5
Reviewed-on: https://chromium-review.googlesource.com/c/1476148
Commit-Queue: Kyle Milka <kmilka@chromium.org>
Reviewed-by: default avatarKristi Park <kristipark@chromium.org>
Cr-Commit-Position: refs/heads/master@{#636345}
parent 1de47fd5
...@@ -128,6 +128,7 @@ var IDS = { ...@@ -128,6 +128,7 @@ 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',
...@@ -966,32 +967,15 @@ function handlePostMessage(event) { ...@@ -966,32 +967,15 @@ function handlePostMessage(event) {
if (cmd === 'loaded') { if (cmd === 'loaded') {
tilesAreLoaded = true; tilesAreLoaded = true;
if (configData.isGooglePage) { if (configData.isGooglePage) {
// Show search suggestions if they were previously hidden. // Show search suggestions, promo, and the OGB if they were previously
// hidden.
if ($(IDS.SUGGESTIONS)) { if ($(IDS.SUGGESTIONS)) {
$(IDS.SUGGESTIONS).style.visibility = 'visible'; $(IDS.SUGGESTIONS).style.visibility = 'visible';
} }
if (!$('one-google-loader')) { if ($(IDS.PROMO)) {
// Load the OneGoogleBar script. It'll create a global variable name $(IDS.PROMO).style.display = 'block';
// "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,
...@@ -1028,11 +1012,15 @@ function handlePostMessage(event) { ...@@ -1028,11 +1012,15 @@ function handlePostMessage(event) {
} }
} }
function showSearchSuggestions() { /**
// Inject search suggestions as early as possible to avoid shifting of other * Request data for search suggestions, promo, and the OGB. Insert it into
// elements. * the page once it's available. For search suggestions this should be almost
* 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')) {
var ssScript = document.createElement('script'); let 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;
...@@ -1041,6 +1029,26 @@ function showSearchSuggestions() { ...@@ -1041,6 +1029,26 @@ function showSearchSuggestions() {
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);
};
}
} }
...@@ -1106,7 +1114,7 @@ function init() { ...@@ -1106,7 +1114,7 @@ function init() {
var searchboxApiHandle = embeddedSearchApiHandle.searchBox; var searchboxApiHandle = embeddedSearchApiHandle.searchBox;
if (configData.isGooglePage) { if (configData.isGooglePage) {
showSearchSuggestions(); requestAndInsertGoogleResources();
enableMDIcons(); enableMDIcons();
ntpApiHandle.onaddcustomlinkdone = onAddCustomLinkDone; ntpApiHandle.onaddcustomlinkdone = onAddCustomLinkDone;
...@@ -1327,6 +1335,7 @@ function injectPromo(promo) { ...@@ -1327,6 +1335,7 @@ 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.style.display = 'none';
$(IDS.NTP_CONTENTS).appendChild(promoContainer); $(IDS.NTP_CONTENTS).appendChild(promoContainer);
if (promo.promoLogUrl) { if (promo.promoLogUrl) {
...@@ -1363,6 +1372,10 @@ function injectSearchSuggestions(suggestions) { ...@@ -1363,6 +1372,10 @@ 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));
...@@ -1375,9 +1388,8 @@ function injectOneGoogleBar(ogb) { ...@@ -1375,9 +1388,8 @@ function injectOneGoogleBar(ogb) {
renderOneGoogleBarTheme(); renderOneGoogleBarTheme();
var ogElem = $('one-google'); var ogElem = $(IDS.OGB);
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,35 +69,6 @@ class LocalNtpSource : public content::URLDataSource, ...@@ -69,35 +69,6 @@ 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(
...@@ -133,13 +104,32 @@ class LocalNtpSource : public content::URLDataSource, ...@@ -133,13 +104,32 @@ 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);
void MaybeServeSearchSuggestions( // If suggestion data is available return it immediately, otherwise no search
// 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_;
...@@ -152,20 +142,23 @@ class LocalNtpSource : public content::URLDataSource, ...@@ -152,20 +142,23 @@ class LocalNtpSource : public content::URLDataSource,
ScopedObserver<NtpBackgroundService, NtpBackgroundServiceObserver> ScopedObserver<NtpBackgroundService, NtpBackgroundServiceObserver>
ntp_background_service_observer_; ntp_background_service_observer_;
std::vector<OneGoogleBarRequest> one_google_bar_requests_; base::Optional<base::TimeTicks> pending_one_google_bar_request_;
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_;
std::vector<PromoRequest> promo_requests_; base::Optional<base::TimeTicks> pending_promo_request_;
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_;
std::vector<SearchSuggestRequest> search_suggest_requests_; base::Optional<base::TimeTicks> pending_search_suggest_request_;
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