blob: 3e0038e6400a910597d933ba49b5b17b55404e2e [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]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
Marc Treibbe5f3feb2017-09-04 09:53:1643// Tracks render process host IDs that are associated with Instant, i.e.
44// processes that are used to render an NTP. Also responsible for keeping
45// necessary information (most visited tiles and theme info) updated in those
46// renderer processes.
[email protected]540380fc2014-03-14 10:10:3447class InstantService : public KeyedService,
[email protected]c4b2af22014-05-11 19:48:5348 public content::NotificationObserver,
treibbb0c5af52016-12-09 17:34:1549 public history::TopSitesObserver,
50 public ntp_tiles::MostVisitedSites::Observer {
[email protected]c5dec6292013-01-25 04:54:5251 public:
[email protected]672c8c12013-03-07 12:30:0652 explicit InstantService(Profile* profile);
Daniel Chenga542fca2014-10-21 09:51:2953 ~InstantService() override;
[email protected]c5dec6292013-01-25 04:54:5254
55 // Add, remove, and query RenderProcessHost IDs that are associated with
56 // Instant processes.
57 void AddInstantProcess(int process_id);
[email protected]c5dec6292013-01-25 04:54:5258 bool IsInstantProcess(int process_id) const;
59
[email protected]6af41782013-06-22 13:49:1160 // Adds/Removes InstantService observers.
61 void AddObserver(InstantServiceObserver* observer);
62 void RemoveObserver(InstantServiceObserver* observer);
63
[email protected]d572bfd2013-02-14 06:14:2064#if defined(UNIT_TEST)
65 int GetInstantProcessCount() const {
66 return process_ids_.size();
67 }
68#endif
[email protected]c5dec6292013-01-25 04:54:5269
treib2df1b7d2016-11-23 09:16:3270 // Invoked whenever an NTP is opened. Causes an async refresh of Most Visited
71 // items.
72 void OnNewTabPageOpened();
73
Xi Cheng72db9c0c2017-08-30 17:05:4074 // Most visited item APIs.
75 //
treib2df1b7d2016-11-23 09:16:3276 // Invoked when the Instant page wants to delete a Most Visited item.
[email protected]280be992013-06-04 03:19:5177 void DeleteMostVisitedItem(const GURL& url);
treib2df1b7d2016-11-23 09:16:3278 // Invoked when the Instant page wants to undo the deletion.
[email protected]280be992013-06-04 03:19:5179 void UndoMostVisitedDeletion(const GURL& url);
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
[email protected]c5dec6292013-01-25 04:54:5296 private:
[email protected]4ff347e2013-07-22 19:39:0097 friend class InstantExtendedTest;
[email protected]c8a118e2013-09-24 21:33:4098 friend class InstantServiceTest;
[email protected]c8a118e2013-09-24 21:33:4099 friend class InstantUnitTestBase;
[email protected]4ff347e2013-07-22 19:39:00100
[email protected]df113a12013-07-24 04:56:08101 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, ProcessIsolation);
zminfddc3e32015-12-09 22:59:08102 FRIEND_TEST_ALL_PREFIXES(InstantServiceTest, GetSuggestionFromClientSide);
[email protected]4ff347e2013-07-22 19:39:00103
[email protected]c4b2af22014-05-11 19:48:53104 // KeyedService:
Daniel Chenga542fca2014-10-21 09:51:29105 void Shutdown() override;
[email protected]c5dec6292013-01-25 04:54:52106
[email protected]c4b2af22014-05-11 19:48:53107 // content::NotificationObserver:
Daniel Chenga542fca2014-10-21 09:51:29108 void Observe(int type,
109 const content::NotificationSource& source,
110 const content::NotificationDetails& details) override;
[email protected]54e0ee62013-02-01 17:25:03111
sdefresneedf9e01f2015-01-13 19:45:41112 // TopSitesObserver:
113 void TopSitesLoaded(history::TopSites* top_sites) override;
fserbdb575112015-06-29 21:31:59114 void TopSitesChanged(history::TopSites* top_sites,
115 ChangeReason change_reason) override;
sdefresneedf9e01f2015-01-13 19:45:41116
treibc6892192017-02-15 13:03:37117 void OnSearchEngineBaseURLChanged(
118 SearchEngineBaseURLTracker::ChangeReason change_reason);
119
[email protected]2309e912013-10-01 01:33:30120 // Called when a renderer process is terminated.
121 void OnRendererProcessTerminated(int process_id);
122
[email protected]280be992013-06-04 03:19:51123 // Called when we get new most visited items from TopSites, registered as an
124 // async callback. Parses them and sends them to the renderer via
treib94638f32016-07-14 10:39:11125 // NotifyAboutMostVisitedItems.
treibbb0c5af52016-12-09 17:34:15126 void OnTopSitesReceived(const history::MostVisitedURLList& data);
127
128 // ntp_tiles::MostVisitedSites::Observer implementation.
Friedrich Horschig9e8749f02017-08-10 09:17:31129 void OnURLsAvailable(
130 const std::map<ntp_tiles::SectionType, ntp_tiles::NTPTilesVector>&
131 sections) override;
treibbb0c5af52016-12-09 17:34:15132 void OnIconMadeAvailable(const GURL& site_url) override;
[email protected]280be992013-06-04 03:19:51133
[email protected]ed68ae32013-06-29 20:46:48134 void NotifyAboutMostVisitedItems();
Marc Treib5891b282017-08-22 09:44:30135 void NotifyAboutThemeInfo();
[email protected]ed68ae32013-06-29 20:46:48136
brettw9b0866f2016-12-11 02:34:06137#if !defined(OS_ANDROID)
Marc Treib5891b282017-08-22 09:44:30138 void BuildThemeInfo();
pcc3a7259562015-04-15 22:21:07139#endif
[email protected]6af41782013-06-22 13:49:11140
[email protected]672c8c12013-03-07 12:30:06141 Profile* const profile_;
142
treibc6892192017-02-15 13:03:37143 std::unique_ptr<SearchEngineBaseURLTracker> search_engine_base_url_tracker_;
[email protected]c4b2af22014-05-11 19:48:53144
[email protected]c5dec6292013-01-25 04:54:52145 // The process ids associated with Instant processes.
146 std::set<int> process_ids_;
147
fserba485e912015-08-05 20:26:08148 // InstantMostVisitedItems from TopSites.
[email protected]ab01dd752013-06-08 05:13:36149 std::vector<InstantMostVisitedItem> most_visited_items_;
[email protected]e7868c82013-03-12 03:55:56150
[email protected]6af41782013-06-22 13:49:11151 // Theme-related data for NTP overlay to adopt themes.
dcheng4af48582016-04-19 00:29:35152 std::unique_ptr<ThemeBackgroundInfo> theme_info_;
[email protected]6af41782013-06-22 13:49:11153
brettwd195c952015-06-02 17:31:12154 base::ObserverList<InstantServiceObserver> observers_;
[email protected]6af41782013-06-22 13:49:11155
[email protected]54e0ee62013-02-01 17:25:03156 content::NotificationRegistrar registrar_;
157
[email protected]672c8c12013-03-07 12:30:06158 scoped_refptr<InstantIOContext> instant_io_context_;
159
treibbb0c5af52016-12-09 17:34:15160 // Data sources for NTP tiles (aka Most Visited tiles). Only one of these will
161 // be non-null.
162 std::unique_ptr<ntp_tiles::MostVisitedSites> most_visited_sites_;
163 scoped_refptr<history::TopSites> top_sites_;
164
mohan.reddy74595362014-09-23 20:35:34165 // Used for Top Sites async retrieval.
166 base::WeakPtrFactory<InstantService> weak_ptr_factory_;
167
[email protected]c5dec6292013-01-25 04:54:52168 DISALLOW_COPY_AND_ASSIGN(InstantService);
169};
170
[email protected]a7b8e43d2013-03-18 18:52:43171#endif // CHROME_BROWSER_SEARCH_INSTANT_SERVICE_H_