blob: 834ea1d95183a1acdb31f56c39f7037152d8a240 [file] [log] [blame]
[email protected]c5dec6292013-01-25 04:54:521// 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
[email protected]a7b8e43d2013-03-18 18:52:435#ifndef CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_
6#define CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_
[email protected]c5dec6292013-01-25 04:54:527
Friedrich Horschig9e8749f02017-08-10 09:17:318#include <map>
dcheng4af48582016-04-19 00:29:359#include <memory>
[email protected]c5dec6292013-01-25 04:54:5210#include <set>
[email protected]ab01dd752013-06-08 05:13:3611#include <vector>
[email protected]c5dec6292013-01-25 04:54:5212
[email protected]c4b2af22014-05-11 19:48:5313#include "base/gtest_prod_util.h"
avib896c712015-12-26 02:10:4314#include "base/macros.h"
treibbb0c5af52016-12-09 17:34:1515#include "base/memory/ref_counted.h"
[email protected]280be992013-06-04 03:19:5116#include "base/memory/weak_ptr.h"
[email protected]6af41782013-06-22 13:49:1117#include "base/observer_list.h"
brettw9b0866f2016-12-11 02:34:0618#include "build/build_config.h"
treibc6892192017-02-15 13:03:3719#include "chrome/browser/search/search_engine_base_url_tracker.h"
sdefresnebc766ef2014-09-25 09:28:1320#include "components/history/core/browser/history_types.h"
sdefresneedf9e01f2015-01-13 19:45:4121#include "components/history/core/browser/top_sites_observer.h"
[email protected]540380fc2014-03-14 10:10:3422#include "components/keyed_service/core/keyed_service.h"
treibbb0c5af52016-12-09 17:34:1523#include "components/ntp_tiles/most_visited_sites.h"
24#include "components/ntp_tiles/ntp_tile.h"
[email protected]54e0ee62013-02-01 17:25:0325#include "content/public/browser/notification_observer.h"
26#include "content/public/browser/notification_registrar.h"
[email protected]c4b2af22014-05-11 19:48:5327#include "url/gurl.h"
[email protected]c5dec6292013-01-25 04:54:5228
[email protected]672c8c12013-03-07 12:30:0629class InstantIOContext;
[email protected]c4b2af22014-05-11 19:48:5330class InstantSearchPrerenderer;
[email protected]6af41782013-06-22 13:49:1131class InstantServiceObserver;
[email protected]672c8c12013-03-07 12:30:0632class Profile;
treibc6892192017-02-15 13:03:3733struct InstantMostVisitedItem;
[email protected]c4b2af22014-05-11 19:48:5334struct ThemeBackgroundInfo;
[email protected]672c8c12013-03-07 12:30:0635
[email protected]4ff347e2013-07-22 19:39:0036namespace content {
[email protected]777590052014-01-17 22:11:5437class RenderProcessHost;
[email protected]4ff347e2013-07-22 19:39:0038}
39
treibbb0c5af52016-12-09 17:34:1540namespace history {
41class TopSites;
42}
43
[email protected]d572bfd2013-02-14 06:14:2044// Tracks render process host IDs that are associated with Instant.
[email protected]540380fc2014-03-14 10:10:3445class InstantService : public KeyedService,
[email protected]c4b2af22014-05-11 19:48:5346 public content::NotificationObserver,
treibbb0c5af52016-12-09 17:34:1547 public history::TopSitesObserver,
48 public ntp_tiles::MostVisitedSites::Observer {
[email protected]c5dec6292013-01-25 04:54:5249 public:
[email protected]672c8c12013-03-07 12:30:0650 explicit InstantService(Profile* profile);
Daniel Chenga542fca2014-10-21 09:51:2951 ~InstantService() override;
[email protected]c5dec6292013-01-25 04:54:5252
53 // Add, remove, and query RenderProcessHost IDs that are associated with
54 // Instant processes.
55 void AddInstantProcess(int process_id);
[email protected]c5dec6292013-01-25 04:54:5256 bool IsInstantProcess(int process_id) const;
57
[email protected]6af41782013-06-22 13:49:1158 // Adds/Removes InstantService observers.
59 void AddObserver(InstantServiceObserver* observer);
60 void RemoveObserver(InstantServiceObserver* observer);
61
[email protected]d572bfd2013-02-14 06:14:2062#if defined(UNIT_TEST)
63 int GetInstantProcessCount() const {
64 return process_ids_.size();
65 }
66#endif
[email protected]c5dec6292013-01-25 04:54:5267
treib2df1b7d2016-11-23 09:16:3268 // Invoked whenever an NTP is opened. Causes an async refresh of Most Visited
69 // items.
70 void OnNewTabPageOpened();
71
[email protected]8b169b4b2013-03-23 18:24:4672 // Most visited item API.
[email protected]e7868c82013-03-12 03:55:5673
treib2df1b7d2016-11-23 09:16:3274 // Invoked when the Instant page wants to delete a Most Visited item.
[email protected]280be992013-06-04 03:19:5175 void DeleteMostVisitedItem(const GURL& url);
76
treib2df1b7d2016-11-23 09:16:3277 // Invoked when the Instant page wants to undo the deletion.
[email protected]280be992013-06-04 03:19:5178 void UndoMostVisitedDeletion(const GURL& url);
79
treib2df1b7d2016-11-23 09:16:3280 // Invoked when the Instant page wants to undo all Most Visited deletions.
[email protected]280be992013-06-04 03:19:5181 void UndoAllMostVisitedDeletions();
82
[email protected]6af41782013-06-22 13:49:1183 // Invoked by the InstantController to update theme information for NTP.
84 //
85 // TODO(kmadhusu): Invoking this from InstantController shouldn't be
86 // necessary. Investigate more and remove this from here.
87 void UpdateThemeInfo();
88
[email protected]ed68ae32013-06-29 20:46:4889 // Invoked by the InstantController to update most visited items details for
90 // NTP.
91 void UpdateMostVisitedItemsInfo();
92
[email protected]2309e912013-10-01 01:33:3093 // Sends the current set of search URLs to a renderer process.
94 void SendSearchURLsToRenderer(content::RenderProcessHost* rph);
95
treib809206532016-11-10 15:48:3396 InstantSearchPrerenderer* GetInstantSearchPrerenderer();
[email protected]e97887c2013-12-11 01:27:3197
[email protected]c5dec6292013-01-25 04:54:5298 private:
[email protected]4ff347e2013-07-22 19:39:0099 friend class InstantExtendedTest;
[email protected]c8a118e2013-09-24 21:33:40100 friend class InstantServiceTest;
[email protected]4ff347e2013-07-22 19:39:00101 friend class InstantTestBase;
[email protected]c8a118e2013-09-24 21:33:40102 friend class InstantUnitTestBase;
[email protected]4ff347e2013-07-22 19:39:00103
[email protected]4ff347e2013-07-22 19:39:00104 FRIEND_TEST_ALL_PREFIXES(InstantExtendedManualTest,
105 MANUAL_SearchesFromFakebox);
[email protected]df113a12013-07-24 04:56:08106 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, ProcessIsolation);
[email protected]c4b2af22014-05-11 19:48:53107 FRIEND_TEST_ALL_PREFIXES(InstantServiceEnabledTest,
108 SendsSearchURLsToRenderer);
zminfddc3e32015-12-09 22:59:08109 FRIEND_TEST_ALL_PREFIXES(InstantServiceTest, GetSuggestionFromServiceSide);
110 FRIEND_TEST_ALL_PREFIXES(InstantServiceTest, GetSuggestionFromClientSide);
[email protected]4ff347e2013-07-22 19:39:00111
[email protected]c4b2af22014-05-11 19:48:53112 // KeyedService:
Daniel Chenga542fca2014-10-21 09:51:29113 void Shutdown() override;
[email protected]c5dec6292013-01-25 04:54:52114
[email protected]c4b2af22014-05-11 19:48:53115 // content::NotificationObserver:
Daniel Chenga542fca2014-10-21 09:51:29116 void Observe(int type,
117 const content::NotificationSource& source,
118 const content::NotificationDetails& details) override;
[email protected]54e0ee62013-02-01 17:25:03119
sdefresneedf9e01f2015-01-13 19:45:41120 // TopSitesObserver:
121 void TopSitesLoaded(history::TopSites* top_sites) override;
fserbdb575112015-06-29 21:31:59122 void TopSitesChanged(history::TopSites* top_sites,
123 ChangeReason change_reason) override;
sdefresneedf9e01f2015-01-13 19:45:41124
treibc6892192017-02-15 13:03:37125 void OnSearchEngineBaseURLChanged(
126 SearchEngineBaseURLTracker::ChangeReason change_reason);
127
[email protected]2309e912013-10-01 01:33:30128 // Called when a renderer process is terminated.
129 void OnRendererProcessTerminated(int process_id);
130
[email protected]280be992013-06-04 03:19:51131 // Called when we get new most visited items from TopSites, registered as an
132 // async callback. Parses them and sends them to the renderer via
treib94638f32016-07-14 10:39:11133 // NotifyAboutMostVisitedItems.
treibbb0c5af52016-12-09 17:34:15134 void OnTopSitesReceived(const history::MostVisitedURLList& data);
135
136 // ntp_tiles::MostVisitedSites::Observer implementation.
Friedrich Horschig9e8749f02017-08-10 09:17:31137 void OnURLsAvailable(
138 const std::map<ntp_tiles::SectionType, ntp_tiles::NTPTilesVector>&
139 sections) override;
treibbb0c5af52016-12-09 17:34:15140 void OnIconMadeAvailable(const GURL& site_url) override;
[email protected]280be992013-06-04 03:19:51141
[email protected]ed68ae32013-06-29 20:46:48142 // Notifies the observer about the last known most visited items.
143 void NotifyAboutMostVisitedItems();
144
brettw9b0866f2016-12-11 02:34:06145#if !defined(OS_ANDROID)
[email protected]6af41782013-06-22 13:49:11146 // Theme changed notification handler.
estade68691b282015-12-11 21:50:40147 void OnThemeChanged();
pcc3a7259562015-04-15 22:21:07148#endif
[email protected]6af41782013-06-22 13:49:11149
treib809206532016-11-10 15:48:33150 void ResetInstantSearchPrerendererIfNecessary();
[email protected]e97887c2013-12-11 01:27:31151
[email protected]672c8c12013-03-07 12:30:06152 Profile* const profile_;
153
treibc6892192017-02-15 13:03:37154 std::unique_ptr<SearchEngineBaseURLTracker> search_engine_base_url_tracker_;
[email protected]c4b2af22014-05-11 19:48:53155
[email protected]c5dec6292013-01-25 04:54:52156 // The process ids associated with Instant processes.
157 std::set<int> process_ids_;
158
fserba485e912015-08-05 20:26:08159 // InstantMostVisitedItems from TopSites.
[email protected]ab01dd752013-06-08 05:13:36160 std::vector<InstantMostVisitedItem> most_visited_items_;
[email protected]e7868c82013-03-12 03:55:56161
[email protected]6af41782013-06-22 13:49:11162 // Theme-related data for NTP overlay to adopt themes.
dcheng4af48582016-04-19 00:29:35163 std::unique_ptr<ThemeBackgroundInfo> theme_info_;
[email protected]6af41782013-06-22 13:49:11164
brettwd195c952015-06-02 17:31:12165 base::ObserverList<InstantServiceObserver> observers_;
[email protected]6af41782013-06-22 13:49:11166
[email protected]54e0ee62013-02-01 17:25:03167 content::NotificationRegistrar registrar_;
168
[email protected]672c8c12013-03-07 12:30:06169 scoped_refptr<InstantIOContext> instant_io_context_;
170
[email protected]e97887c2013-12-11 01:27:31171 // Set to NULL if the default search provider does not support Instant.
dcheng4af48582016-04-19 00:29:35172 std::unique_ptr<InstantSearchPrerenderer> instant_prerenderer_;
[email protected]e97887c2013-12-11 01:27:31173
treibbb0c5af52016-12-09 17:34:15174 // Data sources for NTP tiles (aka Most Visited tiles). Only one of these will
175 // be non-null.
176 std::unique_ptr<ntp_tiles::MostVisitedSites> most_visited_sites_;
177 scoped_refptr<history::TopSites> top_sites_;
178
mohan.reddy74595362014-09-23 20:35:34179 // Used for Top Sites async retrieval.
180 base::WeakPtrFactory<InstantService> weak_ptr_factory_;
181
[email protected]c5dec6292013-01-25 04:54:52182 DISALLOW_COPY_AND_ASSIGN(InstantService);
183};
184
[email protected]a7b8e43d2013-03-18 18:52:43185#endif // CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_