[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" | ||||
[email protected] | d58688c6 | 2013-07-03 23:09:12 | [diff] [blame] | 12 | #include "chrome/browser/profiles/profile.h" |
[email protected] | a7b8e43d | 2013-03-18 18:52:43 | [diff] [blame] | 13 | #include "chrome/browser/search/instant_service.h" |
14 | #include "chrome/browser/search/instant_service_factory.h" | ||||
Marc Treib | bfe0171 | 2017-09-26 11:49:45 | [diff] [blame^] | 15 | #include "chrome/browser/search/search.h" |
[email protected] | 4418dbb | 2012-10-25 03:21:54 | [diff] [blame] | 16 | #include "chrome/browser/ui/browser_instant_controller.h" |
Marc Treib | bfe0171 | 2017-09-26 11:49:45 | [diff] [blame^] | 17 | #include "chrome/common/url_constants.h" |
18 | #include "content/public/browser/navigation_details.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: | ||||
Marc Treib | bfe0171 | 2017-09-26 11:49:45 | [diff] [blame^] | 31 | void NavigationEntryCommitted( |
32 | const content::LoadCommittedDetails& load_details) override { | ||||
33 | if (load_details.is_main_frame && search::IsInstantNTP(web_contents())) { | ||||
34 | callback_.Run(); | ||||
35 | } | ||||
36 | } | ||||
37 | |||||
Marc Treib | 5cacd24 | 2017-09-06 07:48:25 | [diff] [blame] | 38 | void DidFinishNavigation( |
39 | content::NavigationHandle* navigation_handle) override { | ||||
Marc Treib | bfe0171 | 2017-09-26 11:49:45 | [diff] [blame^] | 40 | // TODO(treib): Verify if this is necessary - NavigationEntryCommitted |
41 | // should already cover all cases. | ||||
Marc Treib | 5cacd24 | 2017-09-06 07:48:25 | [diff] [blame] | 42 | if (navigation_handle->HasCommitted() && |
Marc Treib | bfe0171 | 2017-09-26 11:49:45 | [diff] [blame^] | 43 | navigation_handle->IsInMainFrame() && |
44 | search::IsInstantNTP(web_contents())) { | ||||
Marc Treib | 5cacd24 | 2017-09-06 07:48:25 | [diff] [blame] | 45 | callback_.Run(); |
46 | } | ||||
47 | } | ||||
48 | |||||
49 | base::Closure callback_; | ||||
50 | |||||
51 | DISALLOW_COPY_AND_ASSIGN(TabObserver); | ||||
52 | }; | ||||
[email protected] | fd2b9ce | 2010-08-11 04:03:57 | [diff] [blame] | 53 | |
Marc Treib | 7376bd8 | 2017-09-26 08:03:20 | [diff] [blame] | 54 | InstantController::InstantController( |
55 | BrowserInstantController* browser_instant_controller) | ||||
Marc Treib | bfe0171 | 2017-09-26 11:49:45 | [diff] [blame^] | 56 | : browser_instant_controller_(browser_instant_controller) {} |
57 | |||||
58 | InstantController::~InstantController() = default; | ||||
59 | |||||
60 | void InstantController::OnTabActivated(content::WebContents* web_contents) { | ||||
61 | if (!tab_observer_ || tab_observer_->web_contents() != web_contents) { | ||||
62 | tab_observer_ = std::make_unique<TabObserver>( | ||||
63 | web_contents, base::Bind(&InstantController::UpdateInfoForInstantTab, | ||||
64 | base::Unretained(this))); | ||||
65 | // If this tab is an NTP, immediately send it the required info. | ||||
66 | if (search::IsInstantNTP(web_contents)) { | ||||
67 | UpdateInfoForInstantTab(); | ||||
68 | } | ||||
69 | } | ||||
Marc Treib | 7376bd8 | 2017-09-26 08:03:20 | [diff] [blame] | 70 | } |
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame] | 71 | |
Marc Treib | bfe0171 | 2017-09-26 11:49:45 | [diff] [blame^] | 72 | void InstantController::OnTabDeactivated(content::WebContents* web_contents) { |
73 | if (tab_observer_ && tab_observer_->web_contents() == web_contents) { | ||||
74 | tab_observer_ = nullptr; | ||||
75 | } | ||||
Marc Treib | 7376bd8 | 2017-09-26 08:03:20 | [diff] [blame] | 76 | } |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 77 | |
Marc Treib | bfe0171 | 2017-09-26 11:49:45 | [diff] [blame^] | 78 | void InstantController::OnTabDetached(content::WebContents* web_contents) { |
79 | OnTabDeactivated(web_contents); | ||||
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame] | 80 | } |
81 | |||||
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 82 | void InstantController::LogDebugEvent(const std::string& info) const { |
83 | DVLOG(1) << info; | ||||
84 | |||||
85 | debug_events_.push_front(std::make_pair( | ||||
86 | base::Time::Now().ToInternalValue(), info)); | ||||
87 | static const size_t kMaxDebugEventSize = 2000; | ||||
88 | if (debug_events_.size() > kMaxDebugEventSize) | ||||
89 | debug_events_.pop_back(); | ||||
90 | } | ||||
91 | |||||
[email protected] | 66be5181 | 2013-03-05 22:28:09 | [diff] [blame] | 92 | void InstantController::ClearDebugEvents() { |
93 | debug_events_.clear(); | ||||
94 | } | ||||
95 | |||||
[email protected] | 8cc9e53 | 2013-05-06 21:01:47 | [diff] [blame] | 96 | void InstantController::UpdateInfoForInstantTab() { |
Marc Treib | 7376bd8 | 2017-09-26 08:03:20 | [diff] [blame] | 97 | InstantService* instant_service = InstantServiceFactory::GetForProfile( |
98 | browser_instant_controller_->profile()); | ||||
treib | 93f4a87 | 2017-04-28 13:29:52 | [diff] [blame] | 99 | if (instant_service) { |
100 | instant_service->UpdateThemeInfo(); | ||||
101 | instant_service->UpdateMostVisitedItemsInfo(); | ||||
[email protected] | 8cc9e53 | 2013-05-06 21:01:47 | [diff] [blame] | 102 | } |
103 | } |