blob: 0d299a5f2600e50287a344acf4754de0e5bc57a3 [file] [log] [blame]
[email protected]a3536432013-03-26 08:14:311// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CHROME_BROWSER_SEARCH_LOCAL_NTP_SOURCE_H_
6#define CHROME_BROWSER_SEARCH_LOCAL_NTP_SOURCE_H_
7
treib3be61a92017-04-25 13:03:388#include <memory>
treibd7182372017-04-28 11:39:119#include <string>
10#include <vector>
treib3be61a92017-04-25 13:03:3811
avib896c712015-12-26 02:10:4312#include "base/macros.h"
treib3be61a92017-04-25 13:03:3813#include "base/memory/weak_ptr.h"
treibd7182372017-04-28 11:39:1114#include "base/optional.h"
15#include "base/scoped_observer.h"
treib83e22f12017-05-10 15:12:0516#include "base/time/time.h"
Marc Treib9028a6a2017-12-06 16:21:5117#include "build/build_config.h"
Evan Stade0a86f982019-09-23 20:52:5818#include "chrome/browser/search/background/ntp_background_service.h"
Ramya Nagarajan90f3b2662018-05-25 15:21:2019#include "chrome/browser/search/background/ntp_background_service_observer.h"
Evan Stade0a86f982019-09-23 20:52:5820#include "chrome/browser/search/one_google_bar/one_google_bar_service.h"
treibd7182372017-04-28 11:39:1121#include "chrome/browser/search/one_google_bar/one_google_bar_service_observer.h"
Evan Stade0a86f982019-09-23 20:52:5822#include "chrome/browser/search/promos/promo_service.h"
Kyle Milkac7955f02018-11-30 21:55:2723#include "chrome/browser/search/promos/promo_service_observer.h"
Evan Stade0a86f982019-09-23 20:52:5824#include "chrome/browser/search/search_suggest/search_suggest_service.h"
Kyle Milka959eba4b2019-01-11 19:48:0625#include "chrome/browser/search/search_suggest/search_suggest_service_observer.h"
Kyle Milka64e205c2018-06-07 17:27:3126#include "components/prefs/pref_registry_simple.h"
[email protected]a3536432013-03-26 08:14:3127#include "content/public/browser/url_data_source.h"
28
Marc Treib9028a6a2017-12-06 16:21:5129#if defined(OS_ANDROID)
30#error "Instant is only used on desktop";
31#endif
32
treibd7182372017-04-28 11:39:1133struct OneGoogleBarData;
Kyle Milka2f966bb52018-12-06 21:10:0834struct PromoData;
[email protected]5df96ab2013-07-17 23:14:4835class Profile;
36
Chris Pickel3a227ae62017-08-24 10:47:2637namespace search_provider_logos {
38class LogoService;
39} // namespace search_provider_logos
40
Marc Treib15d80202017-12-08 16:38:4741// Serves HTML and resources for the local New Tab page, i.e.
42// chrome-search://local-ntp/local-ntp.html.
43// WARNING: Due to the threading model of URLDataSource, some methods of this
44// class are called on the UI thread, others on the IO thread. All data members
45// live on the UI thread, so make sure not to access them from the IO thread!
46// To prevent accidental access, all methods that get called on the IO thread
47// are implemented as non-member functions.
treibd7182372017-04-28 11:39:1148class LocalNtpSource : public content::URLDataSource,
Ramya Nagarajan90f3b2662018-05-25 15:21:2049 public NtpBackgroundServiceObserver,
Kyle Milkac7955f02018-11-30 21:55:2750 public OneGoogleBarServiceObserver,
Kyle Milka959eba4b2019-01-11 19:48:0651 public PromoServiceObserver,
52 public SearchSuggestServiceObserver {
[email protected]a3536432013-03-26 08:14:3153 public:
[email protected]5df96ab2013-07-17 23:14:4854 explicit LocalNtpSource(Profile* profile);
Lei Zhang366cb5252018-08-31 02:01:2055 ~LocalNtpSource() override;
[email protected]a3536432013-03-26 08:14:3156
57 private:
Mathieu Perreault899c3bf2018-07-31 20:32:2258 class SearchConfigurationProvider;
Chris Pickel3a227ae62017-08-24 10:47:2659 class DesktopLogoObserver;
treib3be61a92017-04-25 13:03:3860
Ramya Nagarajan90f3b2662018-05-25 15:21:2061 struct NtpBackgroundRequest {
danakjf4b9e942019-11-29 15:43:0462 NtpBackgroundRequest(base::TimeTicks start_time,
63 content::URLDataSource::GotDataCallback callback);
64 NtpBackgroundRequest(NtpBackgroundRequest&&);
65 NtpBackgroundRequest& operator=(NtpBackgroundRequest&&);
Ramya Nagarajan90f3b2662018-05-25 15:21:2066 ~NtpBackgroundRequest();
67
68 base::TimeTicks start_time;
69 content::URLDataSource::GotDataCallback callback;
70 };
71
[email protected]a3536432013-03-26 08:14:3172 // Overridden from content::URLDataSource:
Lucas Furukawa Gadani4b4eed02019-06-04 23:12:0473 std::string GetSource() override;
Daniel Chenga542fca2014-10-21 09:51:2974 void StartDataRequest(
Wei-Yin Chen (陳威尹)39f4ff32019-10-22 17:59:0975 const GURL& url,
John Abd-El-Malek92bf3602019-07-31 02:25:4876 const content::WebContents::Getter& wc_getter,
danakjf4b9e942019-11-29 15:43:0477 content::URLDataSource::GotDataCallback callback) override;
Lucas Furukawa Gadani4b4eed02019-06-04 23:12:0478 std::string GetMimeType(const std::string& path) override;
79 bool AllowCaching() override;
jamcf6f7622017-05-03 22:16:2880 bool ShouldServiceRequest(const GURL& url,
81 content::ResourceContext* resource_context,
Lucas Furukawa Gadani4b4eed02019-06-04 23:12:0482 int render_process_id) override;
83 bool ShouldAddContentSecurityPolicy() override;
Mathieu Perreault899c3bf2018-07-31 20:32:2284
85 // The Content Security Policy for the Local NTP.
Lucas Furukawa Gadani4b4eed02019-06-04 23:12:0486 std::string GetContentSecurityPolicy();
[email protected]a3536432013-03-26 08:14:3187
Ramya Nagarajan90f3b2662018-05-25 15:21:2088 // Overridden from NtpBackgroundServiceObserver:
89 void OnCollectionInfoAvailable() override;
Ramya Nagarajanc3265a282018-05-30 04:41:3090 void OnCollectionImagesAvailable() override;
Kyle Milkaae360e2d2019-08-01 01:34:0891 void OnNextCollectionImageAvailable() override {}
Ramya Nagarajan35640b82018-06-05 17:26:2092 void OnNtpBackgroundServiceShuttingDown() override;
Ramya Nagarajan90f3b2662018-05-25 15:21:2093
treibd7182372017-04-28 11:39:1194 // Overridden from OneGoogleBarServiceObserver:
Marc Treibc3b58c82017-08-01 12:56:5895 void OnOneGoogleBarDataUpdated() override;
treibd7182372017-04-28 11:39:1196 void OnOneGoogleBarServiceShuttingDown() override;
97
Kyle Milkac7955f02018-11-30 21:55:2798 // Overridden from PromoServiceObserver:
99 void OnPromoDataUpdated() override;
100 void OnPromoServiceShuttingDown() override;
101
Kyle Milka959eba4b2019-01-11 19:48:06102 // Overridden from SearchSuggestServiceObserver:
103 void OnSearchSuggestDataUpdated() override;
104 void OnSearchSuggestServiceShuttingDown() override;
105
Kyle Milka8a29fdd82019-05-03 02:34:42106 // Called when the OGB data is available and serves |data| to any pending
107 // request from the NTP.
treib83e22f12017-05-10 15:12:05108 void ServeOneGoogleBar(const base::Optional<OneGoogleBarData>& data);
Kyle Milka8a29fdd82019-05-03 02:34:42109 // Called when the page requests OGB data. If the data is available it
110 // is returned immediately, otherwise it is returned when it becomes available
111 // in ServeOneGoogleBar.
112 void ServeOneGoogleBarWhenAvailable(
danakjf4b9e942019-11-29 15:43:04113 content::URLDataSource::GotDataCallback callback);
Kyle Milkaed15ce52019-02-28 06:10:38114
Kyle Milka8a29fdd82019-05-03 02:34:42115 // Called when the promo data is available and serves |data| to any pending
116 // requests from the NTP.
117 void ServePromo(const base::Optional<PromoData>& data);
118 // Called when the page requests promo data. If the data is available it
119 // is returned immediately, otherwise it is returned when it becomes
120 // available in ServePromo.
121 void ServePromoWhenAvailable(
danakjf4b9e942019-11-29 15:43:04122 content::URLDataSource::GotDataCallback callback);
Kyle Milka8a29fdd82019-05-03 02:34:42123
124 // If suggestion data is available return it immediately, otherwise no search
125 // suggestions will be shown on this NTP load.
126 void ServeSearchSuggestionsIfAvailable(
danakjf4b9e942019-11-29 15:43:04127 content::URLDataSource::GotDataCallback callback);
Kyle Milka8a29fdd82019-05-03 02:34:42128
129 // Start requests for the promo and OGB.
130 void InitiatePromoAndOGBRequests();
131
treibd7182372017-04-28 11:39:11132 Profile* const profile_;
133
Ramya Nagarajanc3265a282018-05-30 04:41:30134 std::vector<NtpBackgroundRequest> ntp_background_collections_requests_;
135 std::vector<NtpBackgroundRequest> ntp_background_image_info_requests_;
Ramya Nagarajan90f3b2662018-05-25 15:21:20136
137 NtpBackgroundService* ntp_background_service_;
138
139 ScopedObserver<NtpBackgroundService, NtpBackgroundServiceObserver>
Evan Stade0a86f982019-09-23 20:52:58140 ntp_background_service_observer_{this};
Ramya Nagarajan90f3b2662018-05-25 15:21:20141
Kyle Milka8a29fdd82019-05-03 02:34:42142 base::Optional<base::TimeTicks> pending_one_google_bar_request_;
143 std::vector<content::URLDataSource::GotDataCallback>
144 one_google_bar_callbacks_;
Ramya Nagarajan90f3b2662018-05-25 15:21:20145
treibd7182372017-04-28 11:39:11146 OneGoogleBarService* one_google_bar_service_;
147
148 ScopedObserver<OneGoogleBarService, OneGoogleBarServiceObserver>
Evan Stade0a86f982019-09-23 20:52:58149 one_google_bar_service_observer_{this};
treibd7182372017-04-28 11:39:11150
Kyle Milka8a29fdd82019-05-03 02:34:42151 base::Optional<base::TimeTicks> pending_promo_request_;
152 std::vector<content::URLDataSource::GotDataCallback> promo_callbacks_;
Kyle Milka2f966bb52018-12-06 21:10:08153
Kyle Milkac7955f02018-11-30 21:55:27154 PromoService* promo_service_;
155
Evan Stade0a86f982019-09-23 20:52:58156 ScopedObserver<PromoService, PromoServiceObserver> promo_service_observer_{
157 this};
Kyle Milkac7955f02018-11-30 21:55:27158
Kyle Milka8a29fdd82019-05-03 02:34:42159 base::Optional<base::TimeTicks> pending_search_suggest_request_;
Kyle Milka959eba4b2019-01-11 19:48:06160
161 SearchSuggestService* search_suggest_service_;
162
163 ScopedObserver<SearchSuggestService, SearchSuggestServiceObserver>
Evan Stade0a86f982019-09-23 20:52:58164 search_suggest_service_observer_{this};
Kyle Milka959eba4b2019-01-11 19:48:06165
Chris Pickel3a227ae62017-08-24 10:47:26166 search_provider_logos::LogoService* logo_service_;
167 std::unique_ptr<DesktopLogoObserver> logo_observer_;
168
Mathieu Perreault899c3bf2018-07-31 20:32:22169 std::unique_ptr<SearchConfigurationProvider> search_config_provider_;
Ondrej Skopek4fbead82017-08-17 13:11:32170
Jeremy Roman495db682019-07-12 16:03:24171 base::WeakPtrFactory<LocalNtpSource> weak_ptr_factory_{this};
treib3be61a92017-04-25 13:03:38172
[email protected]a3536432013-03-26 08:14:31173 DISALLOW_COPY_AND_ASSIGN(LocalNtpSource);
174};
175
176#endif // CHROME_BROWSER_SEARCH_LOCAL_NTP_SOURCE_H_