blob: d2868de209d60daa91d4386c95172c4c2a896c41 [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 Milka64e205c2018-06-07 17:27:3120#include "components/prefs/pref_registry_simple.h"
[email protected]a3536432013-03-26 08:14:3121#include "content/public/browser/url_data_source.h"
22
Marc Treib9028a6a2017-12-06 16:21:5123#if defined(OS_ANDROID)
24#error "Instant is only used on desktop";
25#endif
26
treibd7182372017-04-28 11:39:1127struct OneGoogleBarData;
Ramya Nagarajan90f3b2662018-05-25 15:21:2028class NtpBackgroundService;
treibd7182372017-04-28 11:39:1129class OneGoogleBarService;
[email protected]5df96ab2013-07-17 23:14:4830class Profile;
31
Chris Pickel3a227ae62017-08-24 10:47:2632namespace search_provider_logos {
33class LogoService;
34} // namespace search_provider_logos
35
Marc Treib15d80202017-12-08 16:38:4736// Serves HTML and resources for the local New Tab page, i.e.
37// chrome-search://local-ntp/local-ntp.html.
38// WARNING: Due to the threading model of URLDataSource, some methods of this
39// class are called on the UI thread, others on the IO thread. All data members
40// live on the UI thread, so make sure not to access them from the IO thread!
41// To prevent accidental access, all methods that get called on the IO thread
42// are implemented as non-member functions.
treibd7182372017-04-28 11:39:1143class LocalNtpSource : public content::URLDataSource,
Ramya Nagarajan90f3b2662018-05-25 15:21:2044 public NtpBackgroundServiceObserver,
treibd7182372017-04-28 11:39:1145 public OneGoogleBarServiceObserver {
[email protected]a3536432013-03-26 08:14:3146 public:
[email protected]5df96ab2013-07-17 23:14:4847 explicit LocalNtpSource(Profile* profile);
[email protected]a3536432013-03-26 08:14:3148
49 private:
treib3be61a92017-04-25 13:03:3850 class GoogleSearchProviderTracker;
Chris Pickel3a227ae62017-08-24 10:47:2651 class DesktopLogoObserver;
treib3be61a92017-04-25 13:03:3852
Ramya Nagarajan90f3b2662018-05-25 15:21:2053 struct NtpBackgroundRequest {
54 NtpBackgroundRequest(
55 base::TimeTicks start_time,
56 const content::URLDataSource::GotDataCallback& callback);
57 NtpBackgroundRequest(const NtpBackgroundRequest&);
58 ~NtpBackgroundRequest();
59
60 base::TimeTicks start_time;
61 content::URLDataSource::GotDataCallback callback;
62 };
63
treib83e22f12017-05-10 15:12:0564 struct OneGoogleBarRequest {
65 OneGoogleBarRequest(
66 base::TimeTicks start_time,
67 const content::URLDataSource::GotDataCallback& callback);
68 OneGoogleBarRequest(const OneGoogleBarRequest&);
69 ~OneGoogleBarRequest();
70
71 base::TimeTicks start_time;
72 content::URLDataSource::GotDataCallback callback;
73 };
74
Daniel Chenga542fca2014-10-21 09:51:2975 ~LocalNtpSource() override;
[email protected]a3536432013-03-26 08:14:3176
77 // Overridden from content::URLDataSource:
Daniel Chenga542fca2014-10-21 09:51:2978 std::string GetSource() const override;
79 void StartDataRequest(
[email protected]a3536432013-03-26 08:14:3180 const std::string& path,
jamedcd8b012016-09-20 02:03:5881 const content::ResourceRequestInfo::WebContentsGetter& wc_getter,
mostynbfb66cb4f2014-10-07 09:15:4282 const content::URLDataSource::GotDataCallback& callback) override;
Daniel Chenga542fca2014-10-21 09:51:2983 std::string GetMimeType(const std::string& path) const override;
toyoshim501ba312016-11-30 08:25:5584 bool AllowCaching() const override;
jamcf6f7622017-05-03 22:16:2885 bool ShouldServiceRequest(const GURL& url,
86 content::ResourceContext* resource_context,
87 int render_process_id) const override;
treib3be61a92017-04-25 13:03:3888 std::string GetContentSecurityPolicyScriptSrc() const override;
wychend6eb7792016-06-04 00:52:5389 std::string GetContentSecurityPolicyChildSrc() const override;
[email protected]a3536432013-03-26 08:14:3190
Ramya Nagarajan90f3b2662018-05-25 15:21:2091 // Overridden from NtpBackgroundServiceObserver:
92 void OnCollectionInfoAvailable() override;
Ramya Nagarajanc3265a282018-05-30 04:41:3093 void OnCollectionImagesAvailable() override;
Ramya Nagarajan35640b82018-06-05 17:26:2094 void OnNtpBackgroundServiceShuttingDown() override;
Ramya Nagarajan90f3b2662018-05-25 15:21:2095
treibd7182372017-04-28 11:39:1196 // Overridden from OneGoogleBarServiceObserver:
Marc Treibc3b58c82017-08-01 12:56:5897 void OnOneGoogleBarDataUpdated() override;
treibd7182372017-04-28 11:39:1198 void OnOneGoogleBarServiceShuttingDown() override;
99
treib83e22f12017-05-10 15:12:05100 void ServeOneGoogleBar(const base::Optional<OneGoogleBarData>& data);
treibd7182372017-04-28 11:39:11101
treibd7182372017-04-28 11:39:11102 Profile* const profile_;
103
Ramya Nagarajanc3265a282018-05-30 04:41:30104 std::vector<NtpBackgroundRequest> ntp_background_collections_requests_;
105 std::vector<NtpBackgroundRequest> ntp_background_image_info_requests_;
Ramya Nagarajan90f3b2662018-05-25 15:21:20106
107 NtpBackgroundService* ntp_background_service_;
108
109 ScopedObserver<NtpBackgroundService, NtpBackgroundServiceObserver>
110 ntp_background_service_observer_;
111
112 std::vector<OneGoogleBarRequest> one_google_bar_requests_;
113
treibd7182372017-04-28 11:39:11114 OneGoogleBarService* one_google_bar_service_;
115
116 ScopedObserver<OneGoogleBarService, OneGoogleBarServiceObserver>
117 one_google_bar_service_observer_;
118
Chris Pickel3a227ae62017-08-24 10:47:26119 search_provider_logos::LogoService* logo_service_;
120 std::unique_ptr<DesktopLogoObserver> logo_observer_;
121
treib3be61a92017-04-25 13:03:38122 std::unique_ptr<GoogleSearchProviderTracker> google_tracker_;
Ondrej Skopek4fbead82017-08-17 13:11:32123
treib3be61a92017-04-25 13:03:38124 base::WeakPtrFactory<LocalNtpSource> weak_ptr_factory_;
125
[email protected]a3536432013-03-26 08:14:31126 DISALLOW_COPY_AND_ASSIGN(LocalNtpSource);
127};
128
129#endif // CHROME_BROWSER_SEARCH_LOCAL_NTP_SOURCE_H_