blob: b7858d911430c31a73faaae6f4888443e9bde22e [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"
Ramya Nagarajan90f3b2662018-05-25 15:21:2018#include "chrome/browser/search/background/ntp_background_service_observer.h"
treibd7182372017-04-28 11:39:1119#include "chrome/browser/search/one_google_bar/one_google_bar_service_observer.h"
Kyle Milkac7955f02018-11-30 21:55:2720#include "chrome/browser/search/promos/promo_service_observer.h"
Kyle Milka64e205c2018-06-07 17:27:3121#include "components/prefs/pref_registry_simple.h"
[email protected]a3536432013-03-26 08:14:3122#include "content/public/browser/url_data_source.h"
23
Marc Treib9028a6a2017-12-06 16:21:5124#if defined(OS_ANDROID)
25#error "Instant is only used on desktop";
26#endif
27
treibd7182372017-04-28 11:39:1128struct OneGoogleBarData;
Kyle Milka2f966bb52018-12-06 21:10:0829struct PromoData;
Ramya Nagarajan90f3b2662018-05-25 15:21:2030class NtpBackgroundService;
treibd7182372017-04-28 11:39:1131class OneGoogleBarService;
Kyle Milkac7955f02018-11-30 21:55:2732class PromoService;
[email protected]5df96ab2013-07-17 23:14:4833class Profile;
34
Chris Pickel3a227ae62017-08-24 10:47:2635namespace search_provider_logos {
36class LogoService;
37} // namespace search_provider_logos
38
Marc Treib15d80202017-12-08 16:38:4739// Serves HTML and resources for the local New Tab page, i.e.
40// chrome-search://local-ntp/local-ntp.html.
41// WARNING: Due to the threading model of URLDataSource, some methods of this
42// class are called on the UI thread, others on the IO thread. All data members
43// live on the UI thread, so make sure not to access them from the IO thread!
44// To prevent accidental access, all methods that get called on the IO thread
45// are implemented as non-member functions.
treibd7182372017-04-28 11:39:1146class LocalNtpSource : public content::URLDataSource,
Ramya Nagarajan90f3b2662018-05-25 15:21:2047 public NtpBackgroundServiceObserver,
Kyle Milkac7955f02018-11-30 21:55:2748 public OneGoogleBarServiceObserver,
49 public PromoServiceObserver {
[email protected]a3536432013-03-26 08:14:3150 public:
[email protected]5df96ab2013-07-17 23:14:4851 explicit LocalNtpSource(Profile* profile);
Lei Zhang366cb5252018-08-31 02:01:2052 ~LocalNtpSource() override;
[email protected]a3536432013-03-26 08:14:3153
54 private:
Mathieu Perreault899c3bf2018-07-31 20:32:2255 class SearchConfigurationProvider;
Chris Pickel3a227ae62017-08-24 10:47:2656 class DesktopLogoObserver;
treib3be61a92017-04-25 13:03:3857
Ramya Nagarajan90f3b2662018-05-25 15:21:2058 struct NtpBackgroundRequest {
59 NtpBackgroundRequest(
60 base::TimeTicks start_time,
61 const content::URLDataSource::GotDataCallback& callback);
62 NtpBackgroundRequest(const NtpBackgroundRequest&);
63 ~NtpBackgroundRequest();
64
65 base::TimeTicks start_time;
66 content::URLDataSource::GotDataCallback callback;
67 };
68
treib83e22f12017-05-10 15:12:0569 struct OneGoogleBarRequest {
70 OneGoogleBarRequest(
71 base::TimeTicks start_time,
72 const content::URLDataSource::GotDataCallback& callback);
73 OneGoogleBarRequest(const OneGoogleBarRequest&);
74 ~OneGoogleBarRequest();
75
76 base::TimeTicks start_time;
77 content::URLDataSource::GotDataCallback callback;
78 };
79
Kyle Milkac7955f02018-11-30 21:55:2780 struct PromoRequest {
81 PromoRequest(base::TimeTicks start_time,
82 const content::URLDataSource::GotDataCallback& callback);
83 PromoRequest(const PromoRequest&);
84 ~PromoRequest();
85
86 base::TimeTicks start_time;
87 content::URLDataSource::GotDataCallback callback;
88 };
89
[email protected]a3536432013-03-26 08:14:3190 // Overridden from content::URLDataSource:
Daniel Chenga542fca2014-10-21 09:51:2991 std::string GetSource() const override;
92 void StartDataRequest(
[email protected]a3536432013-03-26 08:14:3193 const std::string& path,
jamedcd8b012016-09-20 02:03:5894 const content::ResourceRequestInfo::WebContentsGetter& wc_getter,
mostynbfb66cb4f2014-10-07 09:15:4295 const content::URLDataSource::GotDataCallback& callback) override;
Daniel Chenga542fca2014-10-21 09:51:2996 std::string GetMimeType(const std::string& path) const override;
toyoshim501ba312016-11-30 08:25:5597 bool AllowCaching() const override;
jamcf6f7622017-05-03 22:16:2898 bool ShouldServiceRequest(const GURL& url,
99 content::ResourceContext* resource_context,
100 int render_process_id) const override;
Mathieu Perreault899c3bf2018-07-31 20:32:22101 bool ShouldAddContentSecurityPolicy() const override;
102
103 // The Content Security Policy for the Local NTP.
104 std::string GetContentSecurityPolicy() const;
[email protected]a3536432013-03-26 08:14:31105
Ramya Nagarajan90f3b2662018-05-25 15:21:20106 // Overridden from NtpBackgroundServiceObserver:
107 void OnCollectionInfoAvailable() override;
Ramya Nagarajanc3265a282018-05-30 04:41:30108 void OnCollectionImagesAvailable() override;
Ramya Nagarajan974f15f2018-06-22 18:45:38109 void OnAlbumInfoAvailable() override;
Ramya Nagarajana29136f2018-07-12 01:20:38110 void OnAlbumPhotosAvailable() override;
Ramya Nagarajan35640b82018-06-05 17:26:20111 void OnNtpBackgroundServiceShuttingDown() override;
Ramya Nagarajan90f3b2662018-05-25 15:21:20112
treibd7182372017-04-28 11:39:11113 // Overridden from OneGoogleBarServiceObserver:
Marc Treibc3b58c82017-08-01 12:56:58114 void OnOneGoogleBarDataUpdated() override;
treibd7182372017-04-28 11:39:11115 void OnOneGoogleBarServiceShuttingDown() override;
116
Kyle Milkac7955f02018-11-30 21:55:27117 // Overridden from PromoServiceObserver:
118 void OnPromoDataUpdated() override;
119 void OnPromoServiceShuttingDown() override;
120
treib83e22f12017-05-10 15:12:05121 void ServeOneGoogleBar(const base::Optional<OneGoogleBarData>& data);
treibd7182372017-04-28 11:39:11122
Kyle Milka2f966bb52018-12-06 21:10:08123 void ServePromo(const base::Optional<PromoData>& data);
124
treibd7182372017-04-28 11:39:11125 Profile* const profile_;
126
Ramya Nagarajanc3265a282018-05-30 04:41:30127 std::vector<NtpBackgroundRequest> ntp_background_collections_requests_;
128 std::vector<NtpBackgroundRequest> ntp_background_image_info_requests_;
Ramya Nagarajan974f15f2018-06-22 18:45:38129 std::vector<NtpBackgroundRequest> ntp_background_albums_requests_;
Ramya Nagarajana29136f2018-07-12 01:20:38130 std::vector<NtpBackgroundRequest> ntp_background_photos_requests_;
Ramya Nagarajan90f3b2662018-05-25 15:21:20131
132 NtpBackgroundService* ntp_background_service_;
133
134 ScopedObserver<NtpBackgroundService, NtpBackgroundServiceObserver>
135 ntp_background_service_observer_;
136
137 std::vector<OneGoogleBarRequest> one_google_bar_requests_;
138
treibd7182372017-04-28 11:39:11139 OneGoogleBarService* one_google_bar_service_;
140
141 ScopedObserver<OneGoogleBarService, OneGoogleBarServiceObserver>
142 one_google_bar_service_observer_;
143
Kyle Milka2f966bb52018-12-06 21:10:08144 std::vector<PromoRequest> promo_requests_;
145
Kyle Milkac7955f02018-11-30 21:55:27146 PromoService* promo_service_;
147
148 ScopedObserver<PromoService, PromoServiceObserver> promo_service_observer_;
149
Chris Pickel3a227ae62017-08-24 10:47:26150 search_provider_logos::LogoService* logo_service_;
151 std::unique_ptr<DesktopLogoObserver> logo_observer_;
152
Mathieu Perreault899c3bf2018-07-31 20:32:22153 std::unique_ptr<SearchConfigurationProvider> search_config_provider_;
Ondrej Skopek4fbead82017-08-17 13:11:32154
treib3be61a92017-04-25 13:03:38155 base::WeakPtrFactory<LocalNtpSource> weak_ptr_factory_;
156
[email protected]a3536432013-03-26 08:14:31157 DISALLOW_COPY_AND_ASSIGN(LocalNtpSource);
158};
159
160#endif // CHROME_BROWSER_SEARCH_LOCAL_NTP_SOURCE_H_