blob: 78ff8b7da2a730716dbbb3aa37e971f3d0c5871c [file] [log] [blame]
Avi Drissman4a8573c2022-09-09 19:35:541// Copyright 2012 The Chromium Authors
[email protected]fd2b9ce2010-08-11 04:03:572// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]03bb7102013-03-17 22:44:475#include "chrome/browser/ui/search/instant_controller.h"
[email protected]fd2b9ce2010-08-11 04:03:576
avi655876a2015-12-25 07:18:157#include <stddef.h>
8
Avi Drissman9269d4ed2023-01-07 01:38:069#include "base/functional/bind.h"
10#include "base/functional/callback.h"
[email protected]d58688c62013-07-03 23:09:1211#include "chrome/browser/profiles/profile.h"
[email protected]a7b8e43d2013-03-18 18:52:4312#include "chrome/browser/search/instant_service.h"
13#include "chrome/browser/search/instant_service_factory.h"
Marc Treibbfe01712017-09-26 11:49:4514#include "chrome/browser/search/search.h"
Marc Treibf4e8daff2017-09-27 08:40:3415#include "chrome/browser/ui/tabs/tab_strip_model.h"
Marc Treibbfe01712017-09-26 11:49:4516#include "chrome/common/url_constants.h"
17#include "content/public/browser/navigation_details.h"
Marc Treib5cacd242017-09-06 07:48:2518#include "content/public/browser/navigation_handle.h"
[email protected]3d6a8952012-12-14 03:18:0719#include "content/public/browser/web_contents.h"
Marc Treib5cacd242017-09-06 07:48:2520#include "content/public/browser/web_contents_observer.h"
21
22class InstantController::TabObserver : public content::WebContentsObserver {
23 public:
Ella Ge4cd25382021-01-06 08:59:2824 TabObserver(content::WebContents* web_contents,
25 base::RepeatingClosure callback)
26 : content::WebContentsObserver(web_contents),
27 callback_(std::move(callback)) {}
Peter Boström53c6c5952021-09-17 09:41:2628
29 TabObserver(const TabObserver&) = delete;
30 TabObserver& operator=(const TabObserver&) = delete;
31
Marc Treib5cacd242017-09-06 07:48:2532 ~TabObserver() override = default;
33
34 private:
35 // Overridden from content::WebContentsObserver:
Marc Treibbfe01712017-09-26 11:49:4536 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 Ge4cd25382021-01-06 08:59:2843 base::RepeatingClosure callback_;
Marc Treib5cacd242017-09-06 07:48:2544};
[email protected]fd2b9ce2010-08-11 04:03:5745
Marc Treibf4e8daff2017-09-27 08:40:3446InstantController::InstantController(Profile* profile,
47 TabStripModel* tab_strip_model)
Elly Fong-Jones4e3d25c72019-08-12 18:17:1548 : profile_(profile) {
49 tab_strip_model->AddObserver(this);
Marc Treibf4e8daff2017-09-27 08:40:3450}
Marc Treibbfe01712017-09-26 11:49:4551
52InstantController::~InstantController() = default;
53
sangwoo.ko1ae265f12018-10-18 08:30:2854void InstantController::OnTabStripModelChanged(
55 TabStripModel* tab_strip_model,
56 const TabStripModelChange& change,
57 const TabStripSelectionChange& selection) {
Peter Kastinga4863242024-12-23 00:19:4358 if (tab_strip_model->empty() || !selection.active_tab_changed()) {
sangwoo.ko1ae265f12018-10-18 08:30:2859 return;
Peter Kastinga4863242024-12-23 00:19:4360 }
Marc Treibf4e8daff2017-09-27 08:40:3461
Peter Kastinga4863242024-12-23 00:19:4362 if (selection.old_contents) {
sangwoo.ko1ae265f12018-10-18 08:30:2863 StopWatchingTab(selection.old_contents);
Peter Kastinga4863242024-12-23 00:19:4364 }
Marc Treibf4e8daff2017-09-27 08:40:3465
Peter Kastinga4863242024-12-23 00:19:4366 if (selection.new_contents) {
sangwoo.ko1ae265f12018-10-18 08:30:2867 StartWatchingTab(selection.new_contents);
Peter Kastinga4863242024-12-23 00:19:4368 }
Marc Treibf4e8daff2017-09-27 08:40:3469}
70
71void InstantController::StartWatchingTab(content::WebContents* web_contents) {
Marc Treibbfe01712017-09-26 11:49:4572 if (!tab_observer_ || tab_observer_->web_contents() != web_contents) {
73 tab_observer_ = std::make_unique<TabObserver>(
Ella Ge4cd25382021-01-06 08:59:2874 web_contents,
75 base::BindRepeating(&InstantController::UpdateInfoForInstantTab,
76 base::Unretained(this)));
Marc Treibbfe01712017-09-26 11:49:4577 // If this tab is an NTP, immediately send it the required info.
78 if (search::IsInstantNTP(web_contents)) {
79 UpdateInfoForInstantTab();
80 }
81 }
Marc Treib7376bd82017-09-26 08:03:2082}
[email protected]e41982a72012-11-20 07:16:5183
Marc Treibf4e8daff2017-09-27 08:40:3484void InstantController::StopWatchingTab(content::WebContents* web_contents) {
Marc Treibbfe01712017-09-26 11:49:4585 if (tab_observer_ && tab_observer_->web_contents() == web_contents) {
86 tab_observer_ = nullptr;
87 }
Marc Treib7376bd82017-09-26 08:03:2088}
[email protected]0c9406632013-02-08 01:13:3389
[email protected]8cc9e532013-05-06 21:01:4790void InstantController::UpdateInfoForInstantTab() {
Marc Treibf4e8daff2017-09-27 08:40:3491 InstantService* instant_service =
92 InstantServiceFactory::GetForProfile(profile_);
treib93f4a872017-04-28 13:29:5293 if (instant_service) {
Dan Beam19c81782019-11-12 00:09:0394 instant_service->UpdateNtpTheme();
Kristi Park6424e602019-06-11 18:01:4395 instant_service->UpdateMostVisitedInfo();
[email protected]8cc9e532013-05-06 21:01:4796 }
97}