Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | fd2b9ce | 2010-08-11 04:03:57 | [diff] [blame] | 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] | 03bb710 | 2013-03-17 22:44:47 | [diff] [blame] | 5 | #include "chrome/browser/ui/search/instant_controller.h" |
[email protected] | fd2b9ce | 2010-08-11 04:03:57 | [diff] [blame] | 6 | |
avi | 655876a | 2015-12-25 07:18:15 | [diff] [blame] | 7 | #include <stddef.h> |
8 | |||||
Avi Drissman | 9269d4ed | 2023-01-07 01:38:06 | [diff] [blame] | 9 | #include "base/functional/bind.h" |
10 | #include "base/functional/callback.h" | ||||
[email protected] | d58688c6 | 2013-07-03 23:09:12 | [diff] [blame] | 11 | #include "chrome/browser/profiles/profile.h" |
[email protected] | a7b8e43d | 2013-03-18 18:52:43 | [diff] [blame] | 12 | #include "chrome/browser/search/instant_service.h" |
13 | #include "chrome/browser/search/instant_service_factory.h" | ||||
Marc Treib | bfe0171 | 2017-09-26 11:49:45 | [diff] [blame] | 14 | #include "chrome/browser/search/search.h" |
Marc Treib | f4e8daff | 2017-09-27 08:40:34 | [diff] [blame] | 15 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
Marc Treib | bfe0171 | 2017-09-26 11:49:45 | [diff] [blame] | 16 | #include "chrome/common/url_constants.h" |
17 | #include "content/public/browser/navigation_details.h" | ||||
Marc Treib | 5cacd24 | 2017-09-06 07:48:25 | [diff] [blame] | 18 | #include "content/public/browser/navigation_handle.h" |
[email protected] | 3d6a895 | 2012-12-14 03:18:07 | [diff] [blame] | 19 | #include "content/public/browser/web_contents.h" |
Marc Treib | 5cacd24 | 2017-09-06 07:48:25 | [diff] [blame] | 20 | #include "content/public/browser/web_contents_observer.h" |
21 | |||||
22 | class InstantController::TabObserver : public content::WebContentsObserver { | ||||
23 | public: | ||||
Ella Ge | 4cd2538 | 2021-01-06 08:59:28 | [diff] [blame] | 24 | TabObserver(content::WebContents* web_contents, |
25 | base::RepeatingClosure callback) | ||||
26 | : content::WebContentsObserver(web_contents), | ||||
27 | callback_(std::move(callback)) {} | ||||
Peter Boström | 53c6c595 | 2021-09-17 09:41:26 | [diff] [blame] | 28 | |
29 | TabObserver(const TabObserver&) = delete; | ||||
30 | TabObserver& operator=(const TabObserver&) = delete; | ||||
31 | |||||
Marc Treib | 5cacd24 | 2017-09-06 07:48:25 | [diff] [blame] | 32 | ~TabObserver() override = default; |
33 | |||||
34 | private: | ||||
35 | // Overridden from content::WebContentsObserver: | ||||
Marc Treib | bfe0171 | 2017-09-26 11:49:45 | [diff] [blame] | 36 | void NavigationEntryCommitted( |
37 | const content::LoadCommittedDetails& load_details) override { | ||||
38 | if (load_details.is_main_frame && search::IsInstantNTP(web_contents())) { | ||||
39 | callback_.Run(); | ||||
40 | } | ||||
41 | } | ||||
42 | |||||
Ella Ge | 4cd2538 | 2021-01-06 08:59:28 | [diff] [blame] | 43 | base::RepeatingClosure callback_; |
Marc Treib | 5cacd24 | 2017-09-06 07:48:25 | [diff] [blame] | 44 | }; |
[email protected] | fd2b9ce | 2010-08-11 04:03:57 | [diff] [blame] | 45 | |
Marc Treib | f4e8daff | 2017-09-27 08:40:34 | [diff] [blame] | 46 | InstantController::InstantController(Profile* profile, |
47 | TabStripModel* tab_strip_model) | ||||
Elly Fong-Jones | 4e3d25c7 | 2019-08-12 18:17:15 | [diff] [blame] | 48 | : profile_(profile) { |
49 | tab_strip_model->AddObserver(this); | ||||
Marc Treib | f4e8daff | 2017-09-27 08:40:34 | [diff] [blame] | 50 | } |
Marc Treib | bfe0171 | 2017-09-26 11:49:45 | [diff] [blame] | 51 | |
52 | InstantController::~InstantController() = default; | ||||
53 | |||||
sangwoo.ko | 1ae265f1 | 2018-10-18 08:30:28 | [diff] [blame] | 54 | void InstantController::OnTabStripModelChanged( |
55 | TabStripModel* tab_strip_model, | ||||
56 | const TabStripModelChange& change, | ||||
57 | const TabStripSelectionChange& selection) { | ||||
Peter Kasting | a486324 | 2024-12-23 00:19:43 | [diff] [blame] | 58 | if (tab_strip_model->empty() || !selection.active_tab_changed()) { |
sangwoo.ko | 1ae265f1 | 2018-10-18 08:30:28 | [diff] [blame] | 59 | return; |
Peter Kasting | a486324 | 2024-12-23 00:19:43 | [diff] [blame] | 60 | } |
Marc Treib | f4e8daff | 2017-09-27 08:40:34 | [diff] [blame] | 61 | |
Peter Kasting | a486324 | 2024-12-23 00:19:43 | [diff] [blame] | 62 | if (selection.old_contents) { |
sangwoo.ko | 1ae265f1 | 2018-10-18 08:30:28 | [diff] [blame] | 63 | StopWatchingTab(selection.old_contents); |
Peter Kasting | a486324 | 2024-12-23 00:19:43 | [diff] [blame] | 64 | } |
Marc Treib | f4e8daff | 2017-09-27 08:40:34 | [diff] [blame] | 65 | |
Peter Kasting | a486324 | 2024-12-23 00:19:43 | [diff] [blame] | 66 | if (selection.new_contents) { |
sangwoo.ko | 1ae265f1 | 2018-10-18 08:30:28 | [diff] [blame] | 67 | StartWatchingTab(selection.new_contents); |
Peter Kasting | a486324 | 2024-12-23 00:19:43 | [diff] [blame] | 68 | } |
Marc Treib | f4e8daff | 2017-09-27 08:40:34 | [diff] [blame] | 69 | } |
70 | |||||
71 | void InstantController::StartWatchingTab(content::WebContents* web_contents) { | ||||
Marc Treib | bfe0171 | 2017-09-26 11:49:45 | [diff] [blame] | 72 | if (!tab_observer_ || tab_observer_->web_contents() != web_contents) { |
73 | tab_observer_ = std::make_unique<TabObserver>( | ||||
Ella Ge | 4cd2538 | 2021-01-06 08:59:28 | [diff] [blame] | 74 | web_contents, |
75 | base::BindRepeating(&InstantController::UpdateInfoForInstantTab, | ||||
76 | base::Unretained(this))); | ||||
Marc Treib | bfe0171 | 2017-09-26 11:49:45 | [diff] [blame] | 77 | // If this tab is an NTP, immediately send it the required info. |
78 | if (search::IsInstantNTP(web_contents)) { | ||||
79 | UpdateInfoForInstantTab(); | ||||
80 | } | ||||
81 | } | ||||
Marc Treib | 7376bd8 | 2017-09-26 08:03:20 | [diff] [blame] | 82 | } |
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame] | 83 | |
Marc Treib | f4e8daff | 2017-09-27 08:40:34 | [diff] [blame] | 84 | void InstantController::StopWatchingTab(content::WebContents* web_contents) { |
Marc Treib | bfe0171 | 2017-09-26 11:49:45 | [diff] [blame] | 85 | if (tab_observer_ && tab_observer_->web_contents() == web_contents) { |
86 | tab_observer_ = nullptr; | ||||
87 | } | ||||
Marc Treib | 7376bd8 | 2017-09-26 08:03:20 | [diff] [blame] | 88 | } |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 89 | |
[email protected] | 8cc9e53 | 2013-05-06 21:01:47 | [diff] [blame] | 90 | void InstantController::UpdateInfoForInstantTab() { |
Marc Treib | f4e8daff | 2017-09-27 08:40:34 | [diff] [blame] | 91 | InstantService* instant_service = |
92 | InstantServiceFactory::GetForProfile(profile_); | ||||
treib | 93f4a87 | 2017-04-28 13:29:52 | [diff] [blame] | 93 | if (instant_service) { |
Dan Beam | 19c8178 | 2019-11-12 00:09:03 | [diff] [blame] | 94 | instant_service->UpdateNtpTheme(); |
Kristi Park | 6424e60 | 2019-06-11 18:01:43 | [diff] [blame] | 95 | instant_service->UpdateMostVisitedInfo(); |
[email protected] | 8cc9e53 | 2013-05-06 21:01:47 | [diff] [blame] | 96 | } |
97 | } |