[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 <[email protected]>
Reviewed-by: Kristi Park <[email protected]>
Cr-Commit-Position: refs/heads/master@{#636345}
diff --git a/chrome/browser/search/local_ntp_source.h b/chrome/browser/search/local_ntp_source.h
index 0715b220..ce9d462 100644
--- a/chrome/browser/search/local_ntp_source.h
+++ b/chrome/browser/search/local_ntp_source.h
@@ -69,35 +69,6 @@
     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:
   std::string GetSource() const override;
   void StartDataRequest(
@@ -133,13 +104,32 @@
   void OnSearchSuggestDataUpdated() 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 ServePromo(const base::Optional<PromoData>& data);
-
-  void MaybeServeSearchSuggestions(
+  // 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);
+  // 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
+  // suggestions will be shown on this NTP load.
+  void ServeSearchSuggestionsIfAvailable(
+      const content::URLDataSource::GotDataCallback& callback);
+
+  // Start requests for the promo and OGB.
+  void InitiatePromoAndOGBRequests();
+
   Profile* const profile_;
 
   std::vector<NtpBackgroundRequest> ntp_background_collections_requests_;
@@ -152,20 +142,23 @@
   ScopedObserver<NtpBackgroundService, NtpBackgroundServiceObserver>
       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_;
 
   ScopedObserver<OneGoogleBarService, OneGoogleBarServiceObserver>
       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_;
 
   ScopedObserver<PromoService, PromoServiceObserver> promo_service_observer_;
 
-  std::vector<SearchSuggestRequest> search_suggest_requests_;
+  base::Optional<base::TimeTicks> pending_search_suggest_request_;
 
   SearchSuggestService* search_suggest_service_;