blob: c5e45440ef71fa30ce699c28a9047b4ab5ed7b68 [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>
dcheng4a9d9822015-12-26 22:35:308#include <utility>
avi655876a2015-12-25 07:18:159
Marc Treib5cacd242017-09-06 07:48:2510#include "base/bind.h"
11#include "base/callback.h"
[email protected]d58688c62013-07-03 23:09:1212#include "chrome/browser/profiles/profile.h"
[email protected]a7b8e43d2013-03-18 18:52:4313#include "chrome/browser/search/instant_service.h"
14#include "chrome/browser/search/instant_service_factory.h"
Marc Treibbfe01712017-09-26 11:49:4515#include "chrome/browser/search/search.h"
[email protected]4418dbb2012-10-25 03:21:5416#include "chrome/browser/ui/browser_instant_controller.h"
Marc Treibbfe01712017-09-26 11:49:4517#include "chrome/common/url_constants.h"
18#include "content/public/browser/navigation_details.h"
Marc Treib5cacd242017-09-06 07:48:2519#include "content/public/browser/navigation_handle.h"
[email protected]3d6a8952012-12-14 03:18:0720#include "content/public/browser/web_contents.h"
Marc Treib5cacd242017-09-06 07:48:2521#include "content/public/browser/web_contents_observer.h"
22
23class 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 Treibbfe01712017-09-26 11:49:4531 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 Treib5cacd242017-09-06 07:48:2538 void DidFinishNavigation(
39 content::NavigationHandle* navigation_handle) override {
Marc Treibbfe01712017-09-26 11:49:4540 // TODO(treib): Verify if this is necessary - NavigationEntryCommitted
41 // should already cover all cases.
Marc Treib5cacd242017-09-06 07:48:2542 if (navigation_handle->HasCommitted() &&
Marc Treibbfe01712017-09-26 11:49:4543 navigation_handle->IsInMainFrame() &&
44 search::IsInstantNTP(web_contents())) {
Marc Treib5cacd242017-09-06 07:48:2545 callback_.Run();
46 }
47 }
48
49 base::Closure callback_;
50
51 DISALLOW_COPY_AND_ASSIGN(TabObserver);
52};
[email protected]fd2b9ce2010-08-11 04:03:5753
Marc Treib7376bd82017-09-26 08:03:2054InstantController::InstantController(
55 BrowserInstantController* browser_instant_controller)
Marc Treibbfe01712017-09-26 11:49:4556 : browser_instant_controller_(browser_instant_controller) {}
57
58InstantController::~InstantController() = default;
59
60void 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 Treib7376bd82017-09-26 08:03:2070}
[email protected]e41982a72012-11-20 07:16:5171
Marc Treibbfe01712017-09-26 11:49:4572void InstantController::OnTabDeactivated(content::WebContents* web_contents) {
73 if (tab_observer_ && tab_observer_->web_contents() == web_contents) {
74 tab_observer_ = nullptr;
75 }
Marc Treib7376bd82017-09-26 08:03:2076}
[email protected]0c9406632013-02-08 01:13:3377
Marc Treibbfe01712017-09-26 11:49:4578void InstantController::OnTabDetached(content::WebContents* web_contents) {
79 OnTabDeactivated(web_contents);
[email protected]e41982a72012-11-20 07:16:5180}
81
[email protected]0c9406632013-02-08 01:13:3382void 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]66be51812013-03-05 22:28:0992void InstantController::ClearDebugEvents() {
93 debug_events_.clear();
94}
95
[email protected]8cc9e532013-05-06 21:01:4796void InstantController::UpdateInfoForInstantTab() {
Marc Treib7376bd82017-09-26 08:03:2097 InstantService* instant_service = InstantServiceFactory::GetForProfile(
98 browser_instant_controller_->profile());
treib93f4a872017-04-28 13:29:5299 if (instant_service) {
100 instant_service->UpdateThemeInfo();
101 instant_service->UpdateMostVisitedItemsInfo();
[email protected]8cc9e532013-05-06 21:01:47102 }
103}