blob: 0b4fc0980284ae945d5e2132536cb5a122c4aee4 [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
Marc Treib5cacd242017-09-06 07:48:259#include "base/bind.h"
10#include "base/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) {
58 if (tab_strip_model->empty() || !selection.active_tab_changed())
59 return;
Marc Treibf4e8daff2017-09-27 08:40:3460
sangwoo.ko1ae265f12018-10-18 08:30:2861 if (selection.old_contents)
62 StopWatchingTab(selection.old_contents);
Marc Treibf4e8daff2017-09-27 08:40:3463
sangwoo.ko1ae265f12018-10-18 08:30:2864 if (selection.new_contents)
65 StartWatchingTab(selection.new_contents);
Marc Treibf4e8daff2017-09-27 08:40:3466}
67
68void InstantController::StartWatchingTab(content::WebContents* web_contents) {
Marc Treibbfe01712017-09-26 11:49:4569 if (!tab_observer_ || tab_observer_->web_contents() != web_contents) {
70 tab_observer_ = std::make_unique<TabObserver>(
Ella Ge4cd25382021-01-06 08:59:2871 web_contents,
72 base::BindRepeating(&InstantController::UpdateInfoForInstantTab,
73 base::Unretained(this)));
Marc Treibbfe01712017-09-26 11:49:4574 // If this tab is an NTP, immediately send it the required info.
75 if (search::IsInstantNTP(web_contents)) {
76 UpdateInfoForInstantTab();
77 }
78 }
Marc Treib7376bd82017-09-26 08:03:2079}
[email protected]e41982a72012-11-20 07:16:5180
Marc Treibf4e8daff2017-09-27 08:40:3481void InstantController::StopWatchingTab(content::WebContents* web_contents) {
Marc Treibbfe01712017-09-26 11:49:4582 if (tab_observer_ && tab_observer_->web_contents() == web_contents) {
83 tab_observer_ = nullptr;
84 }
Marc Treib7376bd82017-09-26 08:03:2085}
[email protected]0c9406632013-02-08 01:13:3386
[email protected]8cc9e532013-05-06 21:01:4787void InstantController::UpdateInfoForInstantTab() {
Marc Treibf4e8daff2017-09-27 08:40:3488 InstantService* instant_service =
89 InstantServiceFactory::GetForProfile(profile_);
treib93f4a872017-04-28 13:29:5290 if (instant_service) {
Dan Beam19c81782019-11-12 00:09:0391 instant_service->UpdateNtpTheme();
Kristi Park6424e602019-06-11 18:01:4392 instant_service->UpdateMostVisitedInfo();
[email protected]8cc9e532013-05-06 21:01:4793 }
94}