blob: 746d6f1101c42ce43800c085c231d0120c37e334 [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"
[email protected]a3536432013-03-26 08:14:3120#include "content/public/browser/url_data_source.h"
21
Marc Treib9028a6a2017-12-06 16:21:5122#if defined(OS_ANDROID)
23#error "Instant is only used on desktop";
24#endif
25
treibd7182372017-04-28 11:39:1126struct OneGoogleBarData;
Ramya Nagarajan90f3b2662018-05-25 15:21:2027class NtpBackgroundService;
treibd7182372017-04-28 11:39:1128class OneGoogleBarService;
[email protected]5df96ab2013-07-17 23:14:4829class Profile;
30
Chris Pickel3a227ae62017-08-24 10:47:2631namespace search_provider_logos {
32class LogoService;
33} // namespace search_provider_logos
34
Marc Treib15d80202017-12-08 16:38:4735// Serves HTML and resources for the local New Tab page, i.e.
36// chrome-search://local-ntp/local-ntp.html.
37// WARNING: Due to the threading model of URLDataSource, some methods of this
38// class are called on the UI thread, others on the IO thread. All data members
39// live on the UI thread, so make sure not to access them from the IO thread!
40// To prevent accidental access, all methods that get called on the IO thread
41// are implemented as non-member functions.
treibd7182372017-04-28 11:39:1142class LocalNtpSource : public content::URLDataSource,
Ramya Nagarajan90f3b2662018-05-25 15:21:2043 public NtpBackgroundServiceObserver,
treibd7182372017-04-28 11:39:1144 public OneGoogleBarServiceObserver {
[email protected]a3536432013-03-26 08:14:3145 public:
[email protected]5df96ab2013-07-17 23:14:4846 explicit LocalNtpSource(Profile* profile);
[email protected]a3536432013-03-26 08:14:3147
48 private:
treib3be61a92017-04-25 13:03:3849 class GoogleSearchProviderTracker;
Chris Pickel3a227ae62017-08-24 10:47:2650 class DesktopLogoObserver;
treib3be61a92017-04-25 13:03:3851
Ramya Nagarajan90f3b2662018-05-25 15:21:2052 struct NtpBackgroundRequest {
53 NtpBackgroundRequest(
54 base::TimeTicks start_time,
55 const content::URLDataSource::GotDataCallback& callback);
56 NtpBackgroundRequest(const NtpBackgroundRequest&);
57 ~NtpBackgroundRequest();
58
59 base::TimeTicks start_time;
60 content::URLDataSource::GotDataCallback callback;
61 };
62
treib83e22f12017-05-10 15:12:0563 struct OneGoogleBarRequest {
64 OneGoogleBarRequest(
65 base::TimeTicks start_time,
66 const content::URLDataSource::GotDataCallback& callback);
67 OneGoogleBarRequest(const OneGoogleBarRequest&);
68 ~OneGoogleBarRequest();
69
70 base::TimeTicks start_time;
71 content::URLDataSource::GotDataCallback callback;
72 };
73
Daniel Chenga542fca2014-10-21 09:51:2974 ~LocalNtpSource() override;
[email protected]a3536432013-03-26 08:14:3175
76 // Overridden from content::URLDataSource:
Daniel Chenga542fca2014-10-21 09:51:2977 std::string GetSource() const override;
78 void StartDataRequest(
[email protected]a3536432013-03-26 08:14:3179 const std::string& path,
jamedcd8b012016-09-20 02:03:5880 const content::ResourceRequestInfo::WebContentsGetter& wc_getter,
mostynbfb66cb4f2014-10-07 09:15:4281 const content::URLDataSource::GotDataCallback& callback) override;
Daniel Chenga542fca2014-10-21 09:51:2982 std::string GetMimeType(const std::string& path) const override;
toyoshim501ba312016-11-30 08:25:5583 bool AllowCaching() const override;
jamcf6f7622017-05-03 22:16:2884 bool ShouldServiceRequest(const GURL& url,
85 content::ResourceContext* resource_context,
86 int render_process_id) const override;
treib3be61a92017-04-25 13:03:3887 std::string GetContentSecurityPolicyScriptSrc() const override;
wychend6eb7792016-06-04 00:52:5388 std::string GetContentSecurityPolicyChildSrc() const override;
[email protected]a3536432013-03-26 08:14:3189
Ramya Nagarajan90f3b2662018-05-25 15:21:2090 // Overridden from NtpBackgroundServiceObserver:
91 void OnCollectionInfoAvailable() override;
Ramya Nagarajanc3265a282018-05-30 04:41:3092 void OnCollectionImagesAvailable() override;
Ramya Nagarajan35640b82018-06-05 17:26:2093 void OnNtpBackgroundServiceShuttingDown() override;
Ramya Nagarajan90f3b2662018-05-25 15:21:2094
treibd7182372017-04-28 11:39:1195 // Overridden from OneGoogleBarServiceObserver:
Marc Treibc3b58c82017-08-01 12:56:5896 void OnOneGoogleBarDataUpdated() override;
treibd7182372017-04-28 11:39:1197 void OnOneGoogleBarServiceShuttingDown() override;
98
treib83e22f12017-05-10 15:12:0599 void ServeOneGoogleBar(const base::Optional<OneGoogleBarData>& data);
treibd7182372017-04-28 11:39:11100
treibd7182372017-04-28 11:39:11101 Profile* const profile_;
102
Ramya Nagarajanc3265a282018-05-30 04:41:30103 std::vector<NtpBackgroundRequest> ntp_background_collections_requests_;
104 std::vector<NtpBackgroundRequest> ntp_background_image_info_requests_;
Ramya Nagarajan90f3b2662018-05-25 15:21:20105
106 NtpBackgroundService* ntp_background_service_;
107
108 ScopedObserver<NtpBackgroundService, NtpBackgroundServiceObserver>
109 ntp_background_service_observer_;
110
111 std::vector<OneGoogleBarRequest> one_google_bar_requests_;
112
treibd7182372017-04-28 11:39:11113 OneGoogleBarService* one_google_bar_service_;
114
115 ScopedObserver<OneGoogleBarService, OneGoogleBarServiceObserver>
116 one_google_bar_service_observer_;
117
Chris Pickel3a227ae62017-08-24 10:47:26118 search_provider_logos::LogoService* logo_service_;
119 std::unique_ptr<DesktopLogoObserver> logo_observer_;
120
treib3be61a92017-04-25 13:03:38121 std::unique_ptr<GoogleSearchProviderTracker> google_tracker_;
Ondrej Skopek4fbead82017-08-17 13:11:32122
treib3be61a92017-04-25 13:03:38123 base::WeakPtrFactory<LocalNtpSource> weak_ptr_factory_;
124
[email protected]a3536432013-03-26 08:14:31125 DISALLOW_COPY_AND_ASSIGN(LocalNtpSource);
126};
127
128#endif // CHROME_BROWSER_SEARCH_LOCAL_NTP_SOURCE_H_