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 3 // found in the LICENSE file. 4 5 #include "chrome/browser/ui/search/instant_controller.h" 6 7 #include "base/prefs/pref_service.h" 8 #include "base/strings/stringprintf.h" 9 #include "chrome/browser/chrome_notification_types.h" 10 #include "chrome/browser/platform_util.h" 11 #include "chrome/browser/profiles/profile.h" 12 #include "chrome/browser/search/instant_service.h" 13 #include "chrome/browser/search/instant_service_factory.h" 14 #include "chrome/browser/search/search.h" 15 #include "chrome/browser/search_engines/template_url_service_factory.h" 16 #include "chrome/browser/ui/browser_instant_controller.h" 17 #include "chrome/browser/ui/search/instant_tab.h" 18 #include "chrome/browser/ui/search/search_tab_helper.h" 19 #include "chrome/common/chrome_switches.h" 20 #include "chrome/common/pref_names.h" 21 #include "chrome/common/search_urls.h" 22 #include "chrome/common/url_constants.h" 23 #include "components/search_engines/template_url_service.h" 24 #include "components/sessions/serialized_navigation_entry.h" 25 #include "content/public/browser/navigation_entry.h" 26 #include "content/public/browser/notification_service.h" 27 #include "content/public/browser/render_process_host.h" 28 #include "content/public/browser/render_widget_host_view.h" 29 #include "content/public/browser/web_contents.h" 30 #include "net/base/escape.h" 31 #include "net/base/network_change_notifier.h" 32 #include "url/gurl.h" 33 34 #if defined(TOOLKIT_VIEWS) 35 #include "ui/views/widget/widget.h" 36 #endif 37 38 namespace { 39 40 bool IsContentsFrom(const InstantPage* page, 41 const content::WebContents* contents) { 42 return page && (page->web_contents() == contents); 43 } 44 45 // Adds a transient NavigationEntry to the supplied |contents|'s 46 // NavigationController if the page's URL has not already been updated with the 47 // supplied |search_terms|. Sets the |search_terms| on the transient entry for 48 // search terms extraction to work correctly. 49 void EnsureSearchTermsAreSet(content::WebContents* contents, 50 const base::string16& search_terms) { 51 content::NavigationController* controller = &contents->GetController(); 52 53 // If search terms are already correct or there is already a transient entry 54 // (there shouldn't be), bail out early. 55 if (chrome::GetSearchTerms(contents) == search_terms || 56 controller->GetTransientEntry()) 57 return; 58 59 const content::NavigationEntry* entry = controller->GetLastCommittedEntry(); 60 content::NavigationEntry* transient = controller->CreateNavigationEntry( 61 entry->GetURL(), 62 entry->GetReferrer(), 63 entry->GetTransitionType(), 64 false, 65 std::string(), 66 contents->GetBrowserContext()); 67 transient->SetExtraData(sessions::kSearchTermsKey, search_terms); 68 controller->SetTransientEntry(transient); 69 70 SearchTabHelper::FromWebContents(contents)->NavigationEntryUpdated(); 71 } 72 73 } // namespace 74 75 InstantController::InstantController(BrowserInstantController* browser) 76 : browser_(browser) { 77 } 78 79 InstantController::~InstantController() { 80 } 81 82 bool InstantController::SubmitQuery(const base::string16& search_terms) { 83 if (instant_tab_ && instant_tab_->supports_instant() && 84 search_mode_.is_origin_search()) { 85 // Use |instant_tab_| to run the query if we're already on a search results 86 // page. (NOTE: in particular, we do not send the query to NTPs.) 87 SearchTabHelper::FromWebContents(instant_tab_->web_contents())->Submit( 88 search_terms); 89 instant_tab_->web_contents()->Focus(); 90 EnsureSearchTermsAreSet(instant_tab_->web_contents(), search_terms); 91 return true; 92 } 93 return false; 94 } 95 96 void InstantController::SearchModeChanged(const SearchMode& old_mode, 97 const SearchMode& new_mode) { 98 LOG_INSTANT_DEBUG_EVENT(this, base::StringPrintf( 99 "SearchModeChanged: [origin:mode] %d:%d to %d:%d", old_mode.origin, 100 old_mode.mode, new_mode.origin, new_mode.mode)); 101 102 search_mode_ = new_mode; 103 ResetInstantTab(); 104 } 105 106 void InstantController::ActiveTabChanged() { 107 LOG_INSTANT_DEBUG_EVENT(this, "ActiveTabChanged"); 108 ResetInstantTab(); 109 } 110 111 void InstantController::TabDeactivated(content::WebContents* contents) { 112 // If user is deactivating an NTP tab, log the number of mouseovers for this 113 // NTP session. 114 if (chrome::IsInstantNTP(contents)) 115 InstantTab::EmitNtpStatistics(contents); 116 } 117 118 void InstantController::LogDebugEvent(const std::string& info) const { 119 DVLOG(1) << info; 120 121 debug_events_.push_front(std::make_pair( 122 base::Time::Now().ToInternalValue(), info)); 123 static const size_t kMaxDebugEventSize = 2000; 124 if (debug_events_.size() > kMaxDebugEventSize) 125 debug_events_.pop_back(); 126 } 127 128 void InstantController::ClearDebugEvents() { 129 debug_events_.clear(); 130 } 131 132 Profile* InstantController::profile() const { 133 return browser_->profile(); 134 } 135 136 InstantTab* InstantController::instant_tab() const { 137 return instant_tab_.get(); 138 } 139 140 void InstantController::InstantSupportChanged( 141 InstantSupportState instant_support) { 142 // Handle INSTANT_SUPPORT_YES here because InstantPage is not hooked up to the 143 // active tab. Search model changed listener in InstantPage will handle other 144 // cases. 145 if (instant_support != INSTANT_SUPPORT_YES) 146 return; 147 148 ResetInstantTab(); 149 } 150 151 void InstantController::InstantSupportDetermined( 152 const content::WebContents* contents, 153 bool supports_instant) { 154 DCHECK(IsContentsFrom(instant_tab(), contents)); 155 156 if (!supports_instant) 157 base::MessageLoop::current()->DeleteSoon(FROM_HERE, instant_tab_.release()); 158 159 content::NotificationService::current()->Notify( 160 chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED, 161 content::Source<InstantController>(this), 162 content::NotificationService::NoDetails()); 163 } 164 165 void InstantController::InstantPageAboutToNavigateMainFrame( 166 const content::WebContents* contents, 167 const GURL& url) { 168 DCHECK(IsContentsFrom(instant_tab(), contents)); 169 170 // The Instant tab navigated. Send it the data it needs to display 171 // properly. 172 UpdateInfoForInstantTab(); 173 } 174 175 void InstantController::ResetInstantTab() { 176 if (!search_mode_.is_origin_default()) { 177 content::WebContents* active_tab = browser_->GetActiveWebContents(); 178 if (!instant_tab_ || active_tab != instant_tab_->web_contents()) { 179 instant_tab_.reset(new InstantTab(this, browser_->profile())); 180 instant_tab_->Init(active_tab); 181 UpdateInfoForInstantTab(); 182 } 183 } else { 184 instant_tab_.reset(); 185 } 186 } 187 188 void InstantController::UpdateInfoForInstantTab() { 189 if (instant_tab_) { 190 // Update theme details. 191 InstantService* instant_service = GetInstantService(); 192 if (instant_service) { 193 instant_service->UpdateThemeInfo(); 194 instant_service->UpdateMostVisitedItemsInfo(); 195 } 196 } 197 } 198 199 InstantService* InstantController::GetInstantService() const { 200 return InstantServiceFactory::GetForProfile(profile()); 201 } 202