blob: db8e7a071573ce66681aad4a00abaf944bd185c7 [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
dcheng4af48582016-04-19 00:29:358#include <memory>
[email protected]c5dec6292013-01-25 04:54:529#include <set>
[email protected]ab01dd752013-06-08 05:13:3610#include <vector>
[email protected]c5dec6292013-01-25 04:54:5211
[email protected]c4b2af22014-05-11 19:48:5312#include "base/gtest_prod_util.h"
avib896c712015-12-26 02:10:4313#include "base/macros.h"
treibbb0c5af52016-12-09 17:34:1514#include "base/memory/ref_counted.h"
[email protected]280be992013-06-04 03:19:5115#include "base/memory/weak_ptr.h"
[email protected]6af41782013-06-22 13:49:1116#include "base/observer_list.h"
brettw9b0866f2016-12-11 02:34:0617#include "build/build_config.h"
treibc6892192017-02-15 13:03:3718#include "chrome/browser/search/search_engine_base_url_tracker.h"
sdefresnebc766ef2014-09-25 09:28:1319#include "components/history/core/browser/history_types.h"
sdefresneedf9e01f2015-01-13 19:45:4120#include "components/history/core/browser/top_sites_observer.h"
[email protected]540380fc2014-03-14 10:10:3421#include "components/keyed_service/core/keyed_service.h"
treibbb0c5af52016-12-09 17:34:1522#include "components/ntp_tiles/most_visited_sites.h"
23#include "components/ntp_tiles/ntp_tile.h"
[email protected]54e0ee62013-02-01 17:25:0324#include "content/public/browser/notification_observer.h"
25#include "content/public/browser/notification_registrar.h"
[email protected]c4b2af22014-05-11 19:48:5326#include "url/gurl.h"
[email protected]c5dec6292013-01-25 04:54:5227
[email protected]672c8c12013-03-07 12:30:0628class InstantIOContext;
[email protected]c4b2af22014-05-11 19:48:5329class InstantSearchPrerenderer;
[email protected]6af41782013-06-22 13:49:1130class InstantServiceObserver;
[email protected]672c8c12013-03-07 12:30:0631class Profile;
treibc6892192017-02-15 13:03:3732struct InstantMostVisitedItem;
[email protected]c4b2af22014-05-11 19:48:5333struct ThemeBackgroundInfo;
[email protected]672c8c12013-03-07 12:30:0634
[email protected]4ff347e2013-07-22 19:39:0035namespace content {
[email protected]777590052014-01-17 22:11:5436class RenderProcessHost;
[email protected]4ff347e2013-07-22 19:39:0037}
38
treibbb0c5af52016-12-09 17:34:1539namespace history {
40class TopSites;
41}
42
[email protected]d572bfd2013-02-14 06:14:2043// Tracks render process host IDs that are associated with Instant.
[email protected]540380fc2014-03-14 10:10:3444class InstantService : public KeyedService,
[email protected]c4b2af22014-05-11 19:48:5345 public content::NotificationObserver,
treibbb0c5af52016-12-09 17:34:1546 public history::TopSitesObserver,
47 public ntp_tiles::MostVisitedSites::Observer {
[email protected]c5dec6292013-01-25 04:54:5248 public:
[email protected]672c8c12013-03-07 12:30:0649 explicit InstantService(Profile* profile);
Daniel Chenga542fca2014-10-21 09:51:2950 ~InstantService() override;
[email protected]c5dec6292013-01-25 04:54:5251
52 // Add, remove, and query RenderProcessHost IDs that are associated with
53 // Instant processes.
54 void AddInstantProcess(int process_id);
[email protected]c5dec6292013-01-25 04:54:5255 bool IsInstantProcess(int process_id) const;
56
[email protected]6af41782013-06-22 13:49:1157 // Adds/Removes InstantService observers.
58 void AddObserver(InstantServiceObserver* observer);
59 void RemoveObserver(InstantServiceObserver* observer);
60
[email protected]d572bfd2013-02-14 06:14:2061#if defined(UNIT_TEST)
62 int GetInstantProcessCount() const {
63 return process_ids_.size();
64 }
65#endif
[email protected]c5dec6292013-01-25 04:54:5266
treib2df1b7d2016-11-23 09:16:3267 // Invoked whenever an NTP is opened. Causes an async refresh of Most Visited
68 // items.
69 void OnNewTabPageOpened();
70
[email protected]8b169b4b2013-03-23 18:24:4671 // Most visited item API.
[email protected]e7868c82013-03-12 03:55:5672
treib2df1b7d2016-11-23 09:16:3273 // Invoked when the Instant page wants to delete a Most Visited item.
[email protected]280be992013-06-04 03:19:5174 void DeleteMostVisitedItem(const GURL& url);
75
treib2df1b7d2016-11-23 09:16:3276 // Invoked when the Instant page wants to undo the deletion.
[email protected]280be992013-06-04 03:19:5177 void UndoMostVisitedDeletion(const GURL& url);
78
treib2df1b7d2016-11-23 09:16:3279 // Invoked when the Instant page wants to undo all Most Visited deletions.
[email protected]280be992013-06-04 03:19:5180 void UndoAllMostVisitedDeletions();
81
[email protected]6af41782013-06-22 13:49:1182 // Invoked by the InstantController to update theme information for NTP.
83 //
84 // TODO(kmadhusu): Invoking this from InstantController shouldn't be
85 // necessary. Investigate more and remove this from here.
86 void UpdateThemeInfo();
87
[email protected]ed68ae32013-06-29 20:46:4888 // Invoked by the InstantController to update most visited items details for
89 // NTP.
90 void UpdateMostVisitedItemsInfo();
91
[email protected]2309e912013-10-01 01:33:3092 // Sends the current set of search URLs to a renderer process.
93 void SendSearchURLsToRenderer(content::RenderProcessHost* rph);
94
treib809206532016-11-10 15:48:3395 InstantSearchPrerenderer* GetInstantSearchPrerenderer();
[email protected]e97887c2013-12-11 01:27:3196
[email protected]c5dec6292013-01-25 04:54:5297 private:
[email protected]4ff347e2013-07-22 19:39:0098 friend class InstantExtendedTest;
[email protected]c8a118e2013-09-24 21:33:4099 friend class InstantServiceTest;
[email protected]4ff347e2013-07-22 19:39:00100 friend class InstantTestBase;
[email protected]c8a118e2013-09-24 21:33:40101 friend class InstantUnitTestBase;
[email protected]4ff347e2013-07-22 19:39:00102
[email protected]4ff347e2013-07-22 19:39:00103 FRIEND_TEST_ALL_PREFIXES(InstantExtendedManualTest,
104 MANUAL_SearchesFromFakebox);
[email protected]df113a12013-07-24 04:56:08105 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, ProcessIsolation);
[email protected]c4b2af22014-05-11 19:48:53106 FRIEND_TEST_ALL_PREFIXES(InstantServiceEnabledTest,
107 SendsSearchURLsToRenderer);
zminfddc3e32015-12-09 22:59:08108 FRIEND_TEST_ALL_PREFIXES(InstantServiceTest, GetSuggestionFromServiceSide);
109 FRIEND_TEST_ALL_PREFIXES(InstantServiceTest, GetSuggestionFromClientSide);
[email protected]4ff347e2013-07-22 19:39:00110
[email protected]c4b2af22014-05-11 19:48:53111 // KeyedService:
Daniel Chenga542fca2014-10-21 09:51:29112 void Shutdown() override;
[email protected]c5dec6292013-01-25 04:54:52113
[email protected]c4b2af22014-05-11 19:48:53114 // content::NotificationObserver:
Daniel Chenga542fca2014-10-21 09:51:29115 void Observe(int type,
116 const content::NotificationSource& source,
117 const content::NotificationDetails& details) override;
[email protected]54e0ee62013-02-01 17:25:03118
sdefresneedf9e01f2015-01-13 19:45:41119 // TopSitesObserver:
120 void TopSitesLoaded(history::TopSites* top_sites) override;
fserbdb575112015-06-29 21:31:59121 void TopSitesChanged(history::TopSites* top_sites,
122 ChangeReason change_reason) override;
sdefresneedf9e01f2015-01-13 19:45:41123
treibc6892192017-02-15 13:03:37124 void OnSearchEngineBaseURLChanged(
125 SearchEngineBaseURLTracker::ChangeReason change_reason);
126
[email protected]2309e912013-10-01 01:33:30127 // Called when a renderer process is terminated.
128 void OnRendererProcessTerminated(int process_id);
129
[email protected]280be992013-06-04 03:19:51130 // Called when we get new most visited items from TopSites, registered as an
131 // async callback. Parses them and sends them to the renderer via
treib94638f32016-07-14 10:39:11132 // NotifyAboutMostVisitedItems.
treibbb0c5af52016-12-09 17:34:15133 void OnTopSitesReceived(const history::MostVisitedURLList& data);
134
135 // ntp_tiles::MostVisitedSites::Observer implementation.
136 void OnMostVisitedURLsAvailable(
137 const ntp_tiles::NTPTilesVector& tiles) override;
138 void OnIconMadeAvailable(const GURL& site_url) override;
[email protected]280be992013-06-04 03:19:51139
[email protected]ed68ae32013-06-29 20:46:48140 // Notifies the observer about the last known most visited items.
141 void NotifyAboutMostVisitedItems();
142
brettw9b0866f2016-12-11 02:34:06143#if !defined(OS_ANDROID)
[email protected]6af41782013-06-22 13:49:11144 // Theme changed notification handler.
estade68691b282015-12-11 21:50:40145 void OnThemeChanged();
pcc3a7259562015-04-15 22:21:07146#endif
[email protected]6af41782013-06-22 13:49:11147
treib809206532016-11-10 15:48:33148 void ResetInstantSearchPrerendererIfNecessary();
[email protected]e97887c2013-12-11 01:27:31149
[email protected]672c8c12013-03-07 12:30:06150 Profile* const profile_;
151
treibc6892192017-02-15 13:03:37152 std::unique_ptr<SearchEngineBaseURLTracker> search_engine_base_url_tracker_;
[email protected]c4b2af22014-05-11 19:48:53153
[email protected]c5dec6292013-01-25 04:54:52154 // The process ids associated with Instant processes.
155 std::set<int> process_ids_;
156
fserba485e912015-08-05 20:26:08157 // InstantMostVisitedItems from TopSites.
[email protected]ab01dd752013-06-08 05:13:36158 std::vector<InstantMostVisitedItem> most_visited_items_;
[email protected]e7868c82013-03-12 03:55:56159
[email protected]6af41782013-06-22 13:49:11160 // Theme-related data for NTP overlay to adopt themes.
dcheng4af48582016-04-19 00:29:35161 std::unique_ptr<ThemeBackgroundInfo> theme_info_;
[email protected]6af41782013-06-22 13:49:11162
brettwd195c952015-06-02 17:31:12163 base::ObserverList<InstantServiceObserver> observers_;
[email protected]6af41782013-06-22 13:49:11164
[email protected]54e0ee62013-02-01 17:25:03165 content::NotificationRegistrar registrar_;
166
[email protected]672c8c12013-03-07 12:30:06167 scoped_refptr<InstantIOContext> instant_io_context_;
168
[email protected]e97887c2013-12-11 01:27:31169 // Set to NULL if the default search provider does not support Instant.
dcheng4af48582016-04-19 00:29:35170 std::unique_ptr<InstantSearchPrerenderer> instant_prerenderer_;
[email protected]e97887c2013-12-11 01:27:31171
treibbb0c5af52016-12-09 17:34:15172 // Data sources for NTP tiles (aka Most Visited tiles). Only one of these will
173 // be non-null.
174 std::unique_ptr<ntp_tiles::MostVisitedSites> most_visited_sites_;
175 scoped_refptr<history::TopSites> top_sites_;
176
mohan.reddy74595362014-09-23 20:35:34177 // Used for Top Sites async retrieval.
178 base::WeakPtrFactory<InstantService> weak_ptr_factory_;
179
[email protected]c5dec6292013-01-25 04:54:52180 DISALLOW_COPY_AND_ASSIGN(InstantService);
181};
182
[email protected]a7b8e43d2013-03-18 18:52:43183#endif // CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_