[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 | void DidFinishNavigation( |
| 38 | content::NavigationHandle* navigation_handle) override { |
Marc Treib | bfe0171 | 2017-09-26 11:49:45 | [diff] [blame] | 39 | // TODO(treib): Verify if this is necessary - NavigationEntryCommitted |
| 40 | // should already cover all cases. |
Marc Treib | 5cacd24 | 2017-09-06 07:48:25 | [diff] [blame] | 41 | if (navigation_handle->HasCommitted() && |
Marc Treib | bfe0171 | 2017-09-26 11:49:45 | [diff] [blame] | 42 | navigation_handle->IsInMainFrame() && |
| 43 | search::IsInstantNTP(web_contents())) { |
Marc Treib | 5cacd24 | 2017-09-06 07:48:25 | [diff] [blame] | 44 | callback_.Run(); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | base::Closure callback_; |
| 49 | |
| 50 | DISALLOW_COPY_AND_ASSIGN(TabObserver); |
| 51 | }; |
[email protected] | fd2b9ce | 2010-08-11 04:03:57 | [diff] [blame] | 52 | |
Marc Treib | f4e8daff | 2017-09-27 08:40:34 | [diff] [blame^] | 53 | InstantController::InstantController(Profile* profile, |
| 54 | TabStripModel* tab_strip_model) |
| 55 | : profile_(profile), tab_strip_observer_(this) { |
| 56 | tab_strip_observer_.Add(tab_strip_model); |
| 57 | } |
Marc Treib | bfe0171 | 2017-09-26 11:49:45 | [diff] [blame] | 58 | |
| 59 | InstantController::~InstantController() = default; |
| 60 | |
Marc Treib | f4e8daff | 2017-09-27 08:40:34 | [diff] [blame^] | 61 | void InstantController::TabDetachedAt(content::WebContents* contents, |
| 62 | int index) { |
| 63 | StopWatchingTab(contents); |
| 64 | } |
| 65 | |
| 66 | void InstantController::TabDeactivated(content::WebContents* contents) { |
| 67 | StopWatchingTab(contents); |
| 68 | } |
| 69 | |
| 70 | void InstantController::ActiveTabChanged(content::WebContents* old_contents, |
| 71 | content::WebContents* new_contents, |
| 72 | int index, |
| 73 | int reason) { |
| 74 | StartWatchingTab(new_contents); |
| 75 | } |
| 76 | |
| 77 | void InstantController::TabReplacedAt(TabStripModel* tab_strip_model, |
| 78 | content::WebContents* old_contents, |
| 79 | content::WebContents* new_contents, |
| 80 | int index) { |
| 81 | StopWatchingTab(old_contents); |
| 82 | } |
| 83 | |
| 84 | void InstantController::StartWatchingTab(content::WebContents* web_contents) { |
Marc Treib | bfe0171 | 2017-09-26 11:49:45 | [diff] [blame] | 85 | if (!tab_observer_ || tab_observer_->web_contents() != web_contents) { |
| 86 | tab_observer_ = std::make_unique<TabObserver>( |
| 87 | web_contents, base::Bind(&InstantController::UpdateInfoForInstantTab, |
| 88 | base::Unretained(this))); |
| 89 | // If this tab is an NTP, immediately send it the required info. |
| 90 | if (search::IsInstantNTP(web_contents)) { |
| 91 | UpdateInfoForInstantTab(); |
| 92 | } |
| 93 | } |
Marc Treib | 7376bd8 | 2017-09-26 08:03:20 | [diff] [blame] | 94 | } |
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame] | 95 | |
Marc Treib | f4e8daff | 2017-09-27 08:40:34 | [diff] [blame^] | 96 | void InstantController::StopWatchingTab(content::WebContents* web_contents) { |
Marc Treib | bfe0171 | 2017-09-26 11:49:45 | [diff] [blame] | 97 | if (tab_observer_ && tab_observer_->web_contents() == web_contents) { |
| 98 | tab_observer_ = nullptr; |
| 99 | } |
Marc Treib | 7376bd8 | 2017-09-26 08:03:20 | [diff] [blame] | 100 | } |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 101 | |
[email protected] | 8cc9e53 | 2013-05-06 21:01:47 | [diff] [blame] | 102 | void InstantController::UpdateInfoForInstantTab() { |
Marc Treib | f4e8daff | 2017-09-27 08:40:34 | [diff] [blame^] | 103 | InstantService* instant_service = |
| 104 | InstantServiceFactory::GetForProfile(profile_); |
treib | 93f4a87 | 2017-04-28 13:29:52 | [diff] [blame] | 105 | if (instant_service) { |
| 106 | instant_service->UpdateThemeInfo(); |
| 107 | instant_service->UpdateMostVisitedItemsInfo(); |
[email protected] | 8cc9e53 | 2013-05-06 21:01:47 | [diff] [blame] | 108 | } |
| 109 | } |