[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors. All rights reserved. |
[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 | |||||
Marc Treib | 5cacd24 | 2017-09-06 07:48:25 | [diff] [blame] | 9 | #include "base/bind.h" |
10 | #include "base/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: | ||||
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 Treib | bfe0171 | 2017-09-26 11:49:45 | [diff] [blame] | 30 | 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 Treib | 5cacd24 | 2017-09-06 07:48:25 | [diff] [blame] | 37 | base::Closure callback_; |
38 | |||||
39 | DISALLOW_COPY_AND_ASSIGN(TabObserver); | ||||
40 | }; | ||||
[email protected] | fd2b9ce | 2010-08-11 04:03:57 | [diff] [blame] | 41 | |
Marc Treib | f4e8daff | 2017-09-27 08:40:34 | [diff] [blame] | 42 | InstantController::InstantController(Profile* profile, |
43 | TabStripModel* tab_strip_model) | ||||
Elly Fong-Jones | 4e3d25c7 | 2019-08-12 18:17:15 | [diff] [blame] | 44 | : profile_(profile) { |
45 | tab_strip_model->AddObserver(this); | ||||
Marc Treib | f4e8daff | 2017-09-27 08:40:34 | [diff] [blame] | 46 | } |
Marc Treib | bfe0171 | 2017-09-26 11:49:45 | [diff] [blame] | 47 | |
48 | InstantController::~InstantController() = default; | ||||
49 | |||||
sangwoo.ko | 1ae265f1 | 2018-10-18 08:30:28 | [diff] [blame] | 50 | void 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 Treib | f4e8daff | 2017-09-27 08:40:34 | [diff] [blame] | 56 | |
sangwoo.ko | 1ae265f1 | 2018-10-18 08:30:28 | [diff] [blame] | 57 | if (selection.old_contents) |
58 | StopWatchingTab(selection.old_contents); | ||||
Marc Treib | f4e8daff | 2017-09-27 08:40:34 | [diff] [blame] | 59 | |
sangwoo.ko | 1ae265f1 | 2018-10-18 08:30:28 | [diff] [blame] | 60 | if (selection.new_contents) |
61 | StartWatchingTab(selection.new_contents); | ||||
Marc Treib | f4e8daff | 2017-09-27 08:40:34 | [diff] [blame] | 62 | } |
63 | |||||
64 | void InstantController::StartWatchingTab(content::WebContents* web_contents) { | ||||
Marc Treib | bfe0171 | 2017-09-26 11:49:45 | [diff] [blame] | 65 | 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 Treib | 7376bd8 | 2017-09-26 08:03:20 | [diff] [blame] | 74 | } |
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame] | 75 | |
Marc Treib | f4e8daff | 2017-09-27 08:40:34 | [diff] [blame] | 76 | void InstantController::StopWatchingTab(content::WebContents* web_contents) { |
Marc Treib | bfe0171 | 2017-09-26 11:49:45 | [diff] [blame] | 77 | if (tab_observer_ && tab_observer_->web_contents() == web_contents) { |
78 | tab_observer_ = nullptr; | ||||
79 | } | ||||
Marc Treib | 7376bd8 | 2017-09-26 08:03:20 | [diff] [blame] | 80 | } |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 81 | |
[email protected] | 8cc9e53 | 2013-05-06 21:01:47 | [diff] [blame] | 82 | void InstantController::UpdateInfoForInstantTab() { |
Marc Treib | f4e8daff | 2017-09-27 08:40:34 | [diff] [blame] | 83 | InstantService* instant_service = |
84 | InstantServiceFactory::GetForProfile(profile_); | ||||
treib | 93f4a87 | 2017-04-28 13:29:52 | [diff] [blame] | 85 | if (instant_service) { |
Dan Beam | 19c8178 | 2019-11-12 00:09:03 | [diff] [blame^] | 86 | instant_service->UpdateNtpTheme(); |
Kristi Park | 6424e60 | 2019-06-11 18:01:43 | [diff] [blame] | 87 | instant_service->UpdateMostVisitedInfo(); |
[email protected] | 8cc9e53 | 2013-05-06 21:01:47 | [diff] [blame] | 88 | } |
89 | } |