blob: dd808525e797ec988384dc6fa9d0340303858260 [file] [log] [blame]
[email protected]e41982a72012-11-20 07:16:511// Copyright 2012 The Chromium Authors. All rights reserved.
[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)) {}
Marc Treib5cacd242017-09-06 07:48:2528 ~TabObserver() override = default;
29
30 private:
31 // Overridden from content::WebContentsObserver:
Marc Treibbfe01712017-09-26 11:49:4532 void NavigationEntryCommitted(
33 const content::LoadCommittedDetails& load_details) override {
34 if (load_details.is_main_frame && search::IsInstantNTP(web_contents())) {
35 callback_.Run();
36 }
37 }
38
Ella Ge4cd25382021-01-06 08:59:2839 base::RepeatingClosure callback_;
Marc Treib5cacd242017-09-06 07:48:2540
41 DISALLOW_COPY_AND_ASSIGN(TabObserver);
42};
[email protected]fd2b9ce2010-08-11 04:03:5743
Marc Treibf4e8daff2017-09-27 08:40:3444InstantController::InstantController(Profile* profile,
45 TabStripModel* tab_strip_model)
Elly Fong-Jones4e3d25c72019-08-12 18:17:1546 : profile_(profile) {
47 tab_strip_model->AddObserver(this);
Marc Treibf4e8daff2017-09-27 08:40:3448}
Marc Treibbfe01712017-09-26 11:49:4549
50InstantController::~InstantController() = default;
51
sangwoo.ko1ae265f12018-10-18 08:30:2852void InstantController::OnTabStripModelChanged(
53 TabStripModel* tab_strip_model,
54 const TabStripModelChange& change,
55 const TabStripSelectionChange& selection) {
56 if (tab_strip_model->empty() || !selection.active_tab_changed())
57 return;
Marc Treibf4e8daff2017-09-27 08:40:3458
sangwoo.ko1ae265f12018-10-18 08:30:2859 if (selection.old_contents)
60 StopWatchingTab(selection.old_contents);
Marc Treibf4e8daff2017-09-27 08:40:3461
sangwoo.ko1ae265f12018-10-18 08:30:2862 if (selection.new_contents)
63 StartWatchingTab(selection.new_contents);
Marc Treibf4e8daff2017-09-27 08:40:3464}
65
66void InstantController::StartWatchingTab(content::WebContents* web_contents) {
Marc Treibbfe01712017-09-26 11:49:4567 if (!tab_observer_ || tab_observer_->web_contents() != web_contents) {
68 tab_observer_ = std::make_unique<TabObserver>(
Ella Ge4cd25382021-01-06 08:59:2869 web_contents,
70 base::BindRepeating(&InstantController::UpdateInfoForInstantTab,
71 base::Unretained(this)));
Marc Treibbfe01712017-09-26 11:49:4572 // If this tab is an NTP, immediately send it the required info.
73 if (search::IsInstantNTP(web_contents)) {
74 UpdateInfoForInstantTab();
75 }
76 }
Marc Treib7376bd82017-09-26 08:03:2077}
[email protected]e41982a72012-11-20 07:16:5178
Marc Treibf4e8daff2017-09-27 08:40:3479void InstantController::StopWatchingTab(content::WebContents* web_contents) {
Marc Treibbfe01712017-09-26 11:49:4580 if (tab_observer_ && tab_observer_->web_contents() == web_contents) {
81 tab_observer_ = nullptr;
82 }
Marc Treib7376bd82017-09-26 08:03:2083}
[email protected]0c9406632013-02-08 01:13:3384
[email protected]8cc9e532013-05-06 21:01:4785void InstantController::UpdateInfoForInstantTab() {
Marc Treibf4e8daff2017-09-27 08:40:3486 InstantService* instant_service =
87 InstantServiceFactory::GetForProfile(profile_);
treib93f4a872017-04-28 13:29:5288 if (instant_service) {
Dan Beam19c81782019-11-12 00:09:0389 instant_service->UpdateNtpTheme();
Kristi Park6424e602019-06-11 18:01:4390 instant_service->UpdateMostVisitedInfo();
[email protected]8cc9e532013-05-06 21:01:4791 }
92}