[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> |
dcheng | 4a9d982 | 2015-12-26 22:35:30 | [diff] [blame] | 8 | #include <utility> |
avi | 655876a | 2015-12-25 07:18:15 | [diff] [blame] | 9 | |
Marc Treib | 5cacd24 | 2017-09-06 07:48:25 | [diff] [blame] | 10 | #include "base/bind.h" |
| 11 | #include "base/callback.h" |
Marc Treib | 5891b28 | 2017-08-22 09:44:30 | [diff] [blame] | 12 | #include "base/memory/ptr_util.h" |
[email protected] | f92d658 | 2013-06-10 22:02:36 | [diff] [blame] | 13 | #include "base/strings/stringprintf.h" |
[email protected] | d58688c6 | 2013-07-03 23:09:12 | [diff] [blame] | 14 | #include "chrome/browser/profiles/profile.h" |
[email protected] | a7b8e43d | 2013-03-18 18:52:43 | [diff] [blame] | 15 | #include "chrome/browser/search/instant_service.h" |
| 16 | #include "chrome/browser/search/instant_service_factory.h" |
[email protected] | 4418dbb | 2012-10-25 03:21:54 | [diff] [blame] | 17 | #include "chrome/browser/ui/browser_instant_controller.h" |
Marc Treib | 7376bd8 | 2017-09-26 08:03:20 | [diff] [blame] | 18 | #include "chrome/browser/ui/search/search_tab_helper.h" |
Marc Treib | 5cacd24 | 2017-09-06 07:48:25 | [diff] [blame] | 19 | #include "content/public/browser/navigation_handle.h" |
[email protected] | 3d6a895 | 2012-12-14 03:18:07 | [diff] [blame] | 20 | #include "content/public/browser/web_contents.h" |
Marc Treib | 5cacd24 | 2017-09-06 07:48:25 | [diff] [blame] | 21 | #include "content/public/browser/web_contents_observer.h" |
| 22 | |
| 23 | class InstantController::TabObserver : public content::WebContentsObserver { |
| 24 | public: |
| 25 | TabObserver(content::WebContents* web_contents, const base::Closure& callback) |
| 26 | : content::WebContentsObserver(web_contents), callback_(callback) {} |
| 27 | ~TabObserver() override = default; |
| 28 | |
| 29 | private: |
| 30 | // Overridden from content::WebContentsObserver: |
| 31 | void DidFinishNavigation( |
| 32 | content::NavigationHandle* navigation_handle) override { |
| 33 | if (navigation_handle->HasCommitted() && |
| 34 | navigation_handle->IsInMainFrame()) { |
| 35 | callback_.Run(); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | base::Closure callback_; |
| 40 | |
| 41 | DISALLOW_COPY_AND_ASSIGN(TabObserver); |
| 42 | }; |
[email protected] | fd2b9ce | 2010-08-11 04:03:57 | [diff] [blame] | 43 | |
Marc Treib | 7376bd8 | 2017-09-26 08:03:20 | [diff] [blame] | 44 | InstantController::InstantController( |
| 45 | BrowserInstantController* browser_instant_controller) |
| 46 | : browser_instant_controller_(browser_instant_controller) { |
| 47 | browser_instant_controller_->search_model()->AddObserver(this); |
| 48 | } |
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame] | 49 | |
Marc Treib | 7376bd8 | 2017-09-26 08:03:20 | [diff] [blame] | 50 | InstantController::~InstantController() { |
| 51 | browser_instant_controller_->search_model()->RemoveObserver(this); |
| 52 | } |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 53 | |
Marc Treib | 7376bd8 | 2017-09-26 08:03:20 | [diff] [blame] | 54 | void InstantController::ModelChanged(SearchModel::Origin old_origin, |
| 55 | SearchModel::Origin new_origin) { |
| 56 | // The search mode in the active tab has changed. Bind |instant_tab_observer_| |
| 57 | // if the |new_origin| reflects an Instant NTP. |
| 58 | // Note: This can be called either because the SearchMode changed within the |
| 59 | // current tab, or because the active tab changed. In the latter case, this |
| 60 | // gets called before ActiveTabChanged(). |
| 61 | LogDebugEvent( |
| 62 | base::StringPrintf("ModelChanged: %d to %d", old_origin, new_origin)); |
[email protected] | cd533bf | 2012-12-04 19:14:59 | [diff] [blame] | 63 | |
[email protected] | cd533bf | 2012-12-04 19:14:59 | [diff] [blame] | 64 | ResetInstantTab(); |
[email protected] | 0b10c9ff | 2012-10-09 17:31:55 | [diff] [blame] | 65 | } |
| 66 | |
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame] | 67 | void InstantController::ActiveTabChanged() { |
treib | cdd7937 | 2016-07-25 13:20:11 | [diff] [blame] | 68 | LogDebugEvent("ActiveTabChanged"); |
[email protected] | d58688c6 | 2013-07-03 23:09:12 | [diff] [blame] | 69 | ResetInstantTab(); |
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame] | 70 | } |
| 71 | |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 72 | void InstantController::LogDebugEvent(const std::string& info) const { |
| 73 | DVLOG(1) << info; |
| 74 | |
| 75 | debug_events_.push_front(std::make_pair( |
| 76 | base::Time::Now().ToInternalValue(), info)); |
| 77 | static const size_t kMaxDebugEventSize = 2000; |
| 78 | if (debug_events_.size() > kMaxDebugEventSize) |
| 79 | debug_events_.pop_back(); |
| 80 | } |
| 81 | |
[email protected] | 66be5181 | 2013-03-05 22:28:09 | [diff] [blame] | 82 | void InstantController::ClearDebugEvents() { |
| 83 | debug_events_.clear(); |
| 84 | } |
| 85 | |
Marc Treib | 5cacd24 | 2017-09-06 07:48:25 | [diff] [blame] | 86 | void InstantController::InstantTabAboutToNavigateMainFrame() { |
| 87 | DCHECK(instant_tab_observer_); |
treib | 8897863 | 2017-05-16 15:42:15 | [diff] [blame] | 88 | // The Instant tab navigated (which means it had instant support both before |
| 89 | // and after the navigation). This may cause it to be assigned to a new |
| 90 | // renderer process, which doesn't have the most visited/theme data yet, so |
| 91 | // send it now. |
| 92 | // TODO(treib): This seems unnecessarily convoluted and fragile. Can't we just |
| 93 | // send this when the Instant process is created? |
[email protected] | 4ff347e | 2013-07-22 19:39:00 | [diff] [blame] | 94 | UpdateInfoForInstantTab(); |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 95 | } |
| 96 | |
[email protected] | cd533bf | 2012-12-04 19:14:59 | [diff] [blame] | 97 | void InstantController::ResetInstantTab() { |
Marc Treib | 7376bd8 | 2017-09-26 08:03:20 | [diff] [blame] | 98 | content::WebContents* active_tab = |
| 99 | browser_instant_controller_->GetActiveWebContents(); |
| 100 | if (active_tab && |
| 101 | SearchTabHelper::FromWebContents(active_tab)->model()->origin() == |
| 102 | SearchModel::Origin::NTP) { |
| 103 | // The active tab is an NTP. If we're not already tracking it, do so and |
| 104 | // also update the required info. |
Marc Treib | 5cacd24 | 2017-09-06 07:48:25 | [diff] [blame] | 105 | if (!instant_tab_observer_ || |
| 106 | active_tab != instant_tab_observer_->web_contents()) { |
| 107 | instant_tab_observer_ = base::MakeUnique<TabObserver>( |
| 108 | active_tab, |
| 109 | base::Bind(&InstantController::InstantTabAboutToNavigateMainFrame, |
| 110 | base::Unretained(this))); |
[email protected] | 8cc9e53 | 2013-05-06 21:01:47 | [diff] [blame] | 111 | UpdateInfoForInstantTab(); |
[email protected] | cd533bf | 2012-12-04 19:14:59 | [diff] [blame] | 112 | } |
[email protected] | cd533bf | 2012-12-04 19:14:59 | [diff] [blame] | 113 | } else { |
Marc Treib | 7376bd8 | 2017-09-26 08:03:20 | [diff] [blame] | 114 | instant_tab_observer_ = nullptr; |
[email protected] | cd533bf | 2012-12-04 19:14:59 | [diff] [blame] | 115 | } |
[email protected] | 0a38747 | 2010-10-07 00:18:20 | [diff] [blame] | 116 | } |
| 117 | |
[email protected] | 8cc9e53 | 2013-05-06 21:01:47 | [diff] [blame] | 118 | void InstantController::UpdateInfoForInstantTab() { |
Marc Treib | 5cacd24 | 2017-09-06 07:48:25 | [diff] [blame] | 119 | DCHECK(instant_tab_observer_); |
Marc Treib | 7376bd8 | 2017-09-26 08:03:20 | [diff] [blame] | 120 | InstantService* instant_service = InstantServiceFactory::GetForProfile( |
| 121 | browser_instant_controller_->profile()); |
treib | 93f4a87 | 2017-04-28 13:29:52 | [diff] [blame] | 122 | if (instant_service) { |
| 123 | instant_service->UpdateThemeInfo(); |
| 124 | instant_service->UpdateMostVisitedItemsInfo(); |
[email protected] | 8cc9e53 | 2013-05-06 21:01:47 | [diff] [blame] | 125 | } |
| 126 | } |