Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: chrome/browser/ui/search/instant_controller.cc

Issue 2166023004: Cleanup in InstantTab and InstantController (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: bring back Init() Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2012 The Chromium Authors. All rights reserved. 1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/search/instant_controller.h" 5 #include "chrome/browser/ui/search/instant_controller.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/location.h" 10 #include "base/location.h"
11 #include "base/strings/stringprintf.h" 11 #include "base/strings/stringprintf.h"
12 #include "base/threading/thread_task_runner_handle.h" 12 #include "base/threading/thread_task_runner_handle.h"
13 #include "chrome/browser/chrome_notification_types.h" 13 #include "chrome/browser/chrome_notification_types.h"
14 #include "chrome/browser/profiles/profile.h" 14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/search/instant_service.h" 15 #include "chrome/browser/search/instant_service.h"
16 #include "chrome/browser/search/instant_service_factory.h" 16 #include "chrome/browser/search/instant_service_factory.h"
17 #include "chrome/browser/search/search.h" 17 #include "chrome/browser/search/search.h"
18 #include "chrome/browser/ui/browser_instant_controller.h" 18 #include "chrome/browser/ui/browser_instant_controller.h"
19 #include "chrome/browser/ui/search/instant_tab.h" 19 #include "chrome/browser/ui/search/instant_tab.h"
20 #include "chrome/browser/ui/search/search_tab_helper.h" 20 #include "chrome/browser/ui/search/search_tab_helper.h"
21 #include "components/sessions/core/serialized_navigation_entry.h" 21 #include "components/sessions/core/serialized_navigation_entry.h"
22 #include "content/public/browser/navigation_controller.h" 22 #include "content/public/browser/navigation_controller.h"
23 #include "content/public/browser/navigation_entry.h" 23 #include "content/public/browser/navigation_entry.h"
24 #include "content/public/browser/notification_service.h" 24 #include "content/public/browser/notification_service.h"
25 #include "content/public/browser/web_contents.h" 25 #include "content/public/browser/web_contents.h"
26 #include "url/gurl.h" 26 #include "url/gurl.h"
27 27
28 // Macro used for logging debug events. |message| should be a std::string.
29 #define LOG_INSTANT_DEBUG_EVENT(controller, message) \
30 controller->LogDebugEvent(message)
31
32 namespace { 28 namespace {
33 29
34 bool IsContentsFrom(const InstantTab* page, 30 bool IsContentsFrom(const InstantTab* page,
35 const content::WebContents* contents) { 31 const content::WebContents* contents) {
36 return page && (page->web_contents() == contents); 32 return page && (page->web_contents() == contents);
37 } 33 }
38 34
39 // Adds a transient NavigationEntry to the supplied |contents|'s 35 // Adds a transient NavigationEntry to the supplied |contents|'s
40 // NavigationController if the page's URL has not already been updated with the 36 // NavigationController if the page's URL has not already been updated with the
41 // supplied |search_terms|. Sets the |search_terms| on the transient entry for 37 // supplied |search_terms|. Sets the |search_terms| on the transient entry for
(...skipping 23 matching lines...) Expand all
65 61
66 InstantController::InstantController(BrowserInstantController* browser) 62 InstantController::InstantController(BrowserInstantController* browser)
67 : browser_(browser) { 63 : browser_(browser) {
68 } 64 }
69 65
70 InstantController::~InstantController() { 66 InstantController::~InstantController() {
71 } 67 }
72 68
73 bool InstantController::SubmitQuery(const base::string16& search_terms, 69 bool InstantController::SubmitQuery(const base::string16& search_terms,
74 const EmbeddedSearchRequestParams& params) { 70 const EmbeddedSearchRequestParams& params) {
75 if (instant_tab_ && instant_tab_->supports_instant() && 71 if (!instant_tab_ || !instant_tab_->web_contents())
76 search_mode_.is_origin_search()) { 72 return false;
77 // Use |instant_tab_| to run the query if we're already on a search results 73
78 // page. (NOTE: in particular, we do not send the query to NTPs.) 74 SearchTabHelper* search_tab =
79 SearchTabHelper::FromWebContents(instant_tab_->web_contents())->Submit( 75 SearchTabHelper::FromWebContents(instant_tab_->web_contents());
80 search_terms, params); 76 if (!search_tab->SupportsInstant() || !search_mode_.is_origin_search())
81 instant_tab_->web_contents()->Focus(); 77 return false;
82 EnsureSearchTermsAreSet(instant_tab_->web_contents(), search_terms); 78
83 return true; 79 // Use |instant_tab_| to run the query if we're already on a search results
84 } 80 // page. (NOTE: in particular, we do not send the query to NTPs.)
85 return false; 81 search_tab->Submit(search_terms, params);
82 instant_tab_->web_contents()->Focus();
83 EnsureSearchTermsAreSet(instant_tab_->web_contents(), search_terms);
84 return true;
86 } 85 }
87 86
88 void InstantController::SearchModeChanged(const SearchMode& old_mode, 87 void InstantController::SearchModeChanged(const SearchMode& old_mode,
89 const SearchMode& new_mode) { 88 const SearchMode& new_mode) {
90 LOG_INSTANT_DEBUG_EVENT(this, base::StringPrintf( 89 LogDebugEvent(base::StringPrintf(
91 "SearchModeChanged: [origin:mode] %d:%d to %d:%d", old_mode.origin, 90 "SearchModeChanged: [origin:mode] %d:%d to %d:%d", old_mode.origin,
92 old_mode.mode, new_mode.origin, new_mode.mode)); 91 old_mode.mode, new_mode.origin, new_mode.mode));
93 92
94 search_mode_ = new_mode; 93 search_mode_ = new_mode;
95 ResetInstantTab(); 94 ResetInstantTab();
96 } 95 }
97 96
98 void InstantController::ActiveTabChanged() { 97 void InstantController::ActiveTabChanged() {
99 LOG_INSTANT_DEBUG_EVENT(this, "ActiveTabChanged"); 98 LogDebugEvent("ActiveTabChanged");
100 ResetInstantTab(); 99 ResetInstantTab();
101 } 100 }
102 101
103 void InstantController::LogDebugEvent(const std::string& info) const { 102 void InstantController::LogDebugEvent(const std::string& info) const {
104 DVLOG(1) << info; 103 DVLOG(1) << info;
105 104
106 debug_events_.push_front(std::make_pair( 105 debug_events_.push_front(std::make_pair(
107 base::Time::Now().ToInternalValue(), info)); 106 base::Time::Now().ToInternalValue(), info));
108 static const size_t kMaxDebugEventSize = 2000; 107 static const size_t kMaxDebugEventSize = 2000;
109 if (debug_events_.size() > kMaxDebugEventSize) 108 if (debug_events_.size() > kMaxDebugEventSize)
110 debug_events_.pop_back(); 109 debug_events_.pop_back();
111 } 110 }
112 111
113 void InstantController::ClearDebugEvents() { 112 void InstantController::ClearDebugEvents() {
114 debug_events_.clear(); 113 debug_events_.clear();
115 } 114 }
116 115
117 Profile* InstantController::profile() const {
118 return browser_->profile();
119 }
120
121 InstantTab* InstantController::instant_tab() const {
122 return instant_tab_.get();
123 }
124
125 void InstantController::InstantSupportChanged( 116 void InstantController::InstantSupportChanged(
126 InstantSupportState instant_support) { 117 InstantSupportState instant_support) {
127 // Handle INSTANT_SUPPORT_YES here because InstantTab is not hooked up to the 118 // Handle INSTANT_SUPPORT_YES here because InstantTab is not hooked up to the
128 // active tab. Search model changed listener in InstantTab will handle other 119 // active tab. Search model changed listener in InstantTab will handle other
129 // cases. 120 // cases.
130 if (instant_support != INSTANT_SUPPORT_YES) 121 if (instant_support != INSTANT_SUPPORT_YES)
131 return; 122 return;
132 123
133 ResetInstantTab(); 124 ResetInstantTab();
134 } 125 }
135 126
136 void InstantController::InstantSupportDetermined( 127 void InstantController::InstantSupportDetermined(
137 const content::WebContents* contents, 128 const content::WebContents* contents,
138 bool supports_instant) { 129 bool supports_instant) {
139 DCHECK(IsContentsFrom(instant_tab(), contents)); 130 DCHECK(IsContentsFrom(instant_tab_.get(), contents));
140 131
141 if (!supports_instant) 132 if (!supports_instant)
142 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, 133 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE,
143 instant_tab_.release()); 134 instant_tab_.release());
144 135
145 content::NotificationService::current()->Notify( 136 content::NotificationService::current()->Notify(
146 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED, 137 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED,
147 content::Source<InstantController>(this), 138 content::Source<InstantController>(this),
148 content::NotificationService::NoDetails()); 139 content::NotificationService::NoDetails());
149 } 140 }
150 141
151 void InstantController::InstantTabAboutToNavigateMainFrame( 142 void InstantController::InstantTabAboutToNavigateMainFrame(
152 const content::WebContents* contents, 143 const content::WebContents* contents,
153 const GURL& url) { 144 const GURL& url) {
154 DCHECK(IsContentsFrom(instant_tab(), contents)); 145 DCHECK(IsContentsFrom(instant_tab_.get(), contents));
155 146
156 // The Instant tab navigated. Send it the data it needs to display 147 // The Instant tab navigated. Send it the data it needs to display
157 // properly. 148 // properly.
158 UpdateInfoForInstantTab(); 149 UpdateInfoForInstantTab();
159 } 150 }
160 151
161 void InstantController::ResetInstantTab() { 152 void InstantController::ResetInstantTab() {
162 if (!search_mode_.is_origin_default()) { 153 if (!search_mode_.is_origin_default()) {
163 content::WebContents* active_tab = browser_->GetActiveWebContents(); 154 content::WebContents* active_tab = browser_->GetActiveWebContents();
164 if (!instant_tab_ || active_tab != instant_tab_->web_contents()) { 155 if (!instant_tab_ || active_tab != instant_tab_->web_contents()) {
165 instant_tab_.reset(new InstantTab(this)); 156 instant_tab_.reset(new InstantTab(this, active_tab));
166 instant_tab_->Init(active_tab); 157 instant_tab_->Init();
167 UpdateInfoForInstantTab(); 158 UpdateInfoForInstantTab();
168 } 159 }
169 } else { 160 } else {
170 instant_tab_.reset(); 161 instant_tab_.reset();
171 } 162 }
172 } 163 }
173 164
174 void InstantController::UpdateInfoForInstantTab() { 165 void InstantController::UpdateInfoForInstantTab() {
175 if (instant_tab_) { 166 if (instant_tab_) {
176 // Update theme details. 167 // Update theme details.
177 InstantService* instant_service = GetInstantService(); 168 InstantService* instant_service = GetInstantService();
178 if (instant_service) { 169 if (instant_service) {
179 instant_service->UpdateThemeInfo(); 170 instant_service->UpdateThemeInfo();
180 instant_service->UpdateMostVisitedItemsInfo(); 171 instant_service->UpdateMostVisitedItemsInfo();
181 } 172 }
182 } 173 }
183 } 174 }
184 175
185 InstantService* InstantController::GetInstantService() const { 176 InstantService* InstantController::GetInstantService() const {
186 return InstantServiceFactory::GetForProfile(profile()); 177 return InstantServiceFactory::GetForProfile(browser_->profile());
187 } 178 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698