[email protected] | a353643 | 2013-03-26 08:14:31 | [diff] [blame] | 1 | // 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 | |
treib | 3be61a9 | 2017-04-25 13:03:38 | [diff] [blame] | 8 | #include <memory> |
treib | d718237 | 2017-04-28 11:39:11 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <vector> |
treib | 3be61a9 | 2017-04-25 13:03:38 | [diff] [blame] | 11 | |
avi | b896c71 | 2015-12-26 02:10:43 | [diff] [blame] | 12 | #include "base/macros.h" |
treib | 3be61a9 | 2017-04-25 13:03:38 | [diff] [blame] | 13 | #include "base/memory/weak_ptr.h" |
treib | d718237 | 2017-04-28 11:39:11 | [diff] [blame] | 14 | #include "base/optional.h" |
| 15 | #include "base/scoped_observer.h" |
treib | 83e22f1 | 2017-05-10 15:12:05 | [diff] [blame] | 16 | #include "base/time/time.h" |
Marc Treib | 9028a6a | 2017-12-06 16:21:51 | [diff] [blame] | 17 | #include "build/build_config.h" |
Ramya Nagarajan | 90f3b266 | 2018-05-25 15:21:20 | [diff] [blame] | 18 | #include "chrome/browser/search/background/ntp_background_service_observer.h" |
treib | d718237 | 2017-04-28 11:39:11 | [diff] [blame] | 19 | #include "chrome/browser/search/one_google_bar/one_google_bar_service_observer.h" |
Kyle Milka | 64e205c | 2018-06-07 17:27:31 | [diff] [blame^] | 20 | #include "components/prefs/pref_registry_simple.h" |
[email protected] | a353643 | 2013-03-26 08:14:31 | [diff] [blame] | 21 | #include "content/public/browser/url_data_source.h" |
| 22 | |
Marc Treib | 9028a6a | 2017-12-06 16:21:51 | [diff] [blame] | 23 | #if defined(OS_ANDROID) |
| 24 | #error "Instant is only used on desktop"; |
| 25 | #endif |
| 26 | |
treib | d718237 | 2017-04-28 11:39:11 | [diff] [blame] | 27 | struct OneGoogleBarData; |
Ramya Nagarajan | 90f3b266 | 2018-05-25 15:21:20 | [diff] [blame] | 28 | class NtpBackgroundService; |
treib | d718237 | 2017-04-28 11:39:11 | [diff] [blame] | 29 | class OneGoogleBarService; |
[email protected] | 5df96ab | 2013-07-17 23:14:48 | [diff] [blame] | 30 | class Profile; |
| 31 | |
Chris Pickel | 3a227ae6 | 2017-08-24 10:47:26 | [diff] [blame] | 32 | namespace search_provider_logos { |
| 33 | class LogoService; |
| 34 | } // namespace search_provider_logos |
| 35 | |
Marc Treib | 15d8020 | 2017-12-08 16:38:47 | [diff] [blame] | 36 | // 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. |
treib | d718237 | 2017-04-28 11:39:11 | [diff] [blame] | 43 | class LocalNtpSource : public content::URLDataSource, |
Ramya Nagarajan | 90f3b266 | 2018-05-25 15:21:20 | [diff] [blame] | 44 | public NtpBackgroundServiceObserver, |
treib | d718237 | 2017-04-28 11:39:11 | [diff] [blame] | 45 | public OneGoogleBarServiceObserver { |
[email protected] | a353643 | 2013-03-26 08:14:31 | [diff] [blame] | 46 | public: |
[email protected] | 5df96ab | 2013-07-17 23:14:48 | [diff] [blame] | 47 | explicit LocalNtpSource(Profile* profile); |
[email protected] | a353643 | 2013-03-26 08:14:31 | [diff] [blame] | 48 | |
| 49 | private: |
treib | 3be61a9 | 2017-04-25 13:03:38 | [diff] [blame] | 50 | class GoogleSearchProviderTracker; |
Chris Pickel | 3a227ae6 | 2017-08-24 10:47:26 | [diff] [blame] | 51 | class DesktopLogoObserver; |
treib | 3be61a9 | 2017-04-25 13:03:38 | [diff] [blame] | 52 | |
Ramya Nagarajan | 90f3b266 | 2018-05-25 15:21:20 | [diff] [blame] | 53 | 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 | |
treib | 83e22f1 | 2017-05-10 15:12:05 | [diff] [blame] | 64 | 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 Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 75 | ~LocalNtpSource() override; |
[email protected] | a353643 | 2013-03-26 08:14:31 | [diff] [blame] | 76 | |
| 77 | // Overridden from content::URLDataSource: |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 78 | std::string GetSource() const override; |
| 79 | void StartDataRequest( |
[email protected] | a353643 | 2013-03-26 08:14:31 | [diff] [blame] | 80 | const std::string& path, |
jam | edcd8b01 | 2016-09-20 02:03:58 | [diff] [blame] | 81 | const content::ResourceRequestInfo::WebContentsGetter& wc_getter, |
mostynb | fb66cb4f | 2014-10-07 09:15:42 | [diff] [blame] | 82 | const content::URLDataSource::GotDataCallback& callback) override; |
Daniel Cheng | a542fca | 2014-10-21 09:51:29 | [diff] [blame] | 83 | std::string GetMimeType(const std::string& path) const override; |
toyoshim | 501ba31 | 2016-11-30 08:25:55 | [diff] [blame] | 84 | bool AllowCaching() const override; |
jam | cf6f762 | 2017-05-03 22:16:28 | [diff] [blame] | 85 | bool ShouldServiceRequest(const GURL& url, |
| 86 | content::ResourceContext* resource_context, |
| 87 | int render_process_id) const override; |
treib | 3be61a9 | 2017-04-25 13:03:38 | [diff] [blame] | 88 | std::string GetContentSecurityPolicyScriptSrc() const override; |
wychen | d6eb779 | 2016-06-04 00:52:53 | [diff] [blame] | 89 | std::string GetContentSecurityPolicyChildSrc() const override; |
[email protected] | a353643 | 2013-03-26 08:14:31 | [diff] [blame] | 90 | |
Ramya Nagarajan | 90f3b266 | 2018-05-25 15:21:20 | [diff] [blame] | 91 | // Overridden from NtpBackgroundServiceObserver: |
| 92 | void OnCollectionInfoAvailable() override; |
Ramya Nagarajan | c3265a28 | 2018-05-30 04:41:30 | [diff] [blame] | 93 | void OnCollectionImagesAvailable() override; |
Ramya Nagarajan | 35640b8 | 2018-06-05 17:26:20 | [diff] [blame] | 94 | void OnNtpBackgroundServiceShuttingDown() override; |
Ramya Nagarajan | 90f3b266 | 2018-05-25 15:21:20 | [diff] [blame] | 95 | |
treib | d718237 | 2017-04-28 11:39:11 | [diff] [blame] | 96 | // Overridden from OneGoogleBarServiceObserver: |
Marc Treib | c3b58c8 | 2017-08-01 12:56:58 | [diff] [blame] | 97 | void OnOneGoogleBarDataUpdated() override; |
treib | d718237 | 2017-04-28 11:39:11 | [diff] [blame] | 98 | void OnOneGoogleBarServiceShuttingDown() override; |
| 99 | |
treib | 83e22f1 | 2017-05-10 15:12:05 | [diff] [blame] | 100 | void ServeOneGoogleBar(const base::Optional<OneGoogleBarData>& data); |
treib | d718237 | 2017-04-28 11:39:11 | [diff] [blame] | 101 | |
treib | d718237 | 2017-04-28 11:39:11 | [diff] [blame] | 102 | Profile* const profile_; |
| 103 | |
Ramya Nagarajan | c3265a28 | 2018-05-30 04:41:30 | [diff] [blame] | 104 | std::vector<NtpBackgroundRequest> ntp_background_collections_requests_; |
| 105 | std::vector<NtpBackgroundRequest> ntp_background_image_info_requests_; |
Ramya Nagarajan | 90f3b266 | 2018-05-25 15:21:20 | [diff] [blame] | 106 | |
| 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 | |
treib | d718237 | 2017-04-28 11:39:11 | [diff] [blame] | 114 | OneGoogleBarService* one_google_bar_service_; |
| 115 | |
| 116 | ScopedObserver<OneGoogleBarService, OneGoogleBarServiceObserver> |
| 117 | one_google_bar_service_observer_; |
| 118 | |
Chris Pickel | 3a227ae6 | 2017-08-24 10:47:26 | [diff] [blame] | 119 | search_provider_logos::LogoService* logo_service_; |
| 120 | std::unique_ptr<DesktopLogoObserver> logo_observer_; |
| 121 | |
treib | 3be61a9 | 2017-04-25 13:03:38 | [diff] [blame] | 122 | std::unique_ptr<GoogleSearchProviderTracker> google_tracker_; |
Ondrej Skopek | 4fbead8 | 2017-08-17 13:11:32 | [diff] [blame] | 123 | |
treib | 3be61a9 | 2017-04-25 13:03:38 | [diff] [blame] | 124 | base::WeakPtrFactory<LocalNtpSource> weak_ptr_factory_; |
| 125 | |
[email protected] | a353643 | 2013-03-26 08:14:31 | [diff] [blame] | 126 | DISALLOW_COPY_AND_ASSIGN(LocalNtpSource); |
| 127 | }; |
| 128 | |
| 129 | #endif // CHROME_BROWSER_SEARCH_LOCAL_NTP_SOURCE_H_ |