blob: 90471d54c2fc8772bc2e3901e6979aef46ae4864 [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:
24 TabObserver(content::WebContents* web_contents, const base::Closure& callback)
25 : content::WebContentsObserver(web_contents), callback_(callback) {}
26 ~TabObserver() override = default;
27
28 private:
29 // Overridden from content::WebContentsObserver:
Marc Treibbfe01712017-09-26 11:49:4530 void NavigationEntryCommitted(
31 const content::LoadCommittedDetails& load_details) override {
32 if (load_details.is_main_frame && search::IsInstantNTP(web_contents())) {
33 callback_.Run();
34 }
35 }
36
Marc Treib5cacd242017-09-06 07:48:2537 base::Closure callback_;
38
39 DISALLOW_COPY_AND_ASSIGN(TabObserver);
40};
[email protected]fd2b9ce2010-08-11 04:03:5741
Marc Treibf4e8daff2017-09-27 08:40:3442InstantController::InstantController(Profile* profile,
43 TabStripModel* tab_strip_model)
Elly Fong-Jones4e3d25c72019-08-12 18:17:1544 : profile_(profile) {
45 tab_strip_model->AddObserver(this);
Marc Treibf4e8daff2017-09-27 08:40:3446}
Marc Treibbfe01712017-09-26 11:49:4547
48InstantController::~InstantController() = default;
49
sangwoo.ko1ae265f12018-10-18 08:30:2850void InstantController::OnTabStripModelChanged(
51 TabStripModel* tab_strip_model,
52 const TabStripModelChange& change,
53 const TabStripSelectionChange& selection) {
54 if (tab_strip_model->empty() || !selection.active_tab_changed())
55 return;
Marc Treibf4e8daff2017-09-27 08:40:3456
sangwoo.ko1ae265f12018-10-18 08:30:2857 if (selection.old_contents)
58 StopWatchingTab(selection.old_contents);
Marc Treibf4e8daff2017-09-27 08:40:3459
sangwoo.ko1ae265f12018-10-18 08:30:2860 if (selection.new_contents)
61 StartWatchingTab(selection.new_contents);
Marc Treibf4e8daff2017-09-27 08:40:3462}
63
64void InstantController::StartWatchingTab(content::WebContents* web_contents) {
Marc Treibbfe01712017-09-26 11:49:4565 if (!tab_observer_ || tab_observer_->web_contents() != web_contents) {
66 tab_observer_ = std::make_unique<TabObserver>(
67 web_contents, base::Bind(&InstantController::UpdateInfoForInstantTab,
68 base::Unretained(this)));
69 // If this tab is an NTP, immediately send it the required info.
70 if (search::IsInstantNTP(web_contents)) {
71 UpdateInfoForInstantTab();
72 }
73 }
Marc Treib7376bd82017-09-26 08:03:2074}
[email protected]e41982a72012-11-20 07:16:5175
Marc Treibf4e8daff2017-09-27 08:40:3476void InstantController::StopWatchingTab(content::WebContents* web_contents) {
Marc Treibbfe01712017-09-26 11:49:4577 if (tab_observer_ && tab_observer_->web_contents() == web_contents) {
78 tab_observer_ = nullptr;
79 }
Marc Treib7376bd82017-09-26 08:03:2080}
[email protected]0c9406632013-02-08 01:13:3381
[email protected]8cc9e532013-05-06 21:01:4782void InstantController::UpdateInfoForInstantTab() {
Marc Treibf4e8daff2017-09-27 08:40:3483 InstantService* instant_service =
84 InstantServiceFactory::GetForProfile(profile_);
treib93f4a872017-04-28 13:29:5285 if (instant_service) {
Dan Beam19c81782019-11-12 00:09:0386 instant_service->UpdateNtpTheme();
Kristi Park6424e602019-06-11 18:01:4387 instant_service->UpdateMostVisitedInfo();
[email protected]8cc9e532013-05-06 21:01:4788 }
89}