This source file includes following definitions.
- IsContentsFrom
- EnsureSearchTermsAreSet
- SubmitQuery
- SearchModeChanged
- ActiveTabChanged
- TabDeactivated
- LogDebugEvent
- ClearDebugEvents
- profile
- instant_tab
- InstantSupportChanged
- InstantSupportDetermined
- InstantPageAboutToNavigateMainFrame
- ResetInstantTab
- UpdateInfoForInstantTab
- GetInstantService
#include "chrome/browser/ui/search/instant_controller.h"
#include "base/prefs/pref_service.h"
#include "base/strings/stringprintf.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/content_settings/content_settings_provider.h"
#include "chrome/browser/content_settings/host_content_settings_map.h"
#include "chrome/browser/platform_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search/instant_service.h"
#include "chrome/browser/search/instant_service_factory.h"
#include "chrome/browser/search/search.h"
#include "chrome/browser/search_engines/search_terms_data.h"
#include "chrome/browser/search_engines/template_url_service.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/browser/ui/browser_instant_controller.h"
#include "chrome/browser/ui/search/instant_tab.h"
#include "chrome/browser/ui/search/search_tab_helper.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/content_settings_types.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/search_urls.h"
#include "chrome/common/url_constants.h"
#include "components/sessions/serialized_navigation_entry.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_view.h"
#include "net/base/escape.h"
#include "net/base/network_change_notifier.h"
#include "url/gurl.h"
#if defined(TOOLKIT_VIEWS)
#include "ui/views/widget/widget.h"
#endif
namespace {
bool IsContentsFrom(const InstantPage* page,
const content::WebContents* contents) {
return page && (page->contents() == contents);
}
void EnsureSearchTermsAreSet(content::WebContents* contents,
const base::string16& search_terms) {
content::NavigationController* controller = &contents->GetController();
if (chrome::GetSearchTerms(contents) == search_terms ||
controller->GetTransientEntry())
return;
const content::NavigationEntry* entry = controller->GetLastCommittedEntry();
content::NavigationEntry* transient = controller->CreateNavigationEntry(
entry->GetURL(),
entry->GetReferrer(),
entry->GetTransitionType(),
false,
std::string(),
contents->GetBrowserContext());
transient->SetExtraData(sessions::kSearchTermsKey, search_terms);
controller->SetTransientEntry(transient);
SearchTabHelper::FromWebContents(contents)->NavigationEntryUpdated();
}
}
InstantController::InstantController(BrowserInstantController* browser)
: browser_(browser) {
}
InstantController::~InstantController() {
}
bool InstantController::SubmitQuery(const base::string16& search_terms) {
if (instant_tab_ && instant_tab_->supports_instant() &&
search_mode_.is_origin_search()) {
SearchTabHelper::FromWebContents(instant_tab_->contents())->Submit(
search_terms);
instant_tab_->contents()->GetView()->Focus();
EnsureSearchTermsAreSet(instant_tab_->contents(), search_terms);
return true;
}
return false;
}
void InstantController::SearchModeChanged(const SearchMode& old_mode,
const SearchMode& new_mode) {
LOG_INSTANT_DEBUG_EVENT(this, base::StringPrintf(
"SearchModeChanged: [origin:mode] %d:%d to %d:%d", old_mode.origin,
old_mode.mode, new_mode.origin, new_mode.mode));
search_mode_ = new_mode;
ResetInstantTab();
}
void InstantController::ActiveTabChanged() {
LOG_INSTANT_DEBUG_EVENT(this, "ActiveTabChanged");
ResetInstantTab();
}
void InstantController::TabDeactivated(content::WebContents* contents) {
if (chrome::IsInstantNTP(contents))
InstantTab::EmitNtpStatistics(contents);
}
void InstantController::LogDebugEvent(const std::string& info) const {
DVLOG(1) << info;
debug_events_.push_front(std::make_pair(
base::Time::Now().ToInternalValue(), info));
static const size_t kMaxDebugEventSize = 2000;
if (debug_events_.size() > kMaxDebugEventSize)
debug_events_.pop_back();
}
void InstantController::ClearDebugEvents() {
debug_events_.clear();
}
Profile* InstantController::profile() const {
return browser_->profile();
}
InstantTab* InstantController::instant_tab() const {
return instant_tab_.get();
}
void InstantController::InstantSupportChanged(
InstantSupportState instant_support) {
if (instant_support != INSTANT_SUPPORT_YES)
return;
ResetInstantTab();
}
void InstantController::InstantSupportDetermined(
const content::WebContents* contents,
bool supports_instant) {
DCHECK(IsContentsFrom(instant_tab(), contents));
if (!supports_instant)
base::MessageLoop::current()->DeleteSoon(FROM_HERE, instant_tab_.release());
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_INSTANT_TAB_SUPPORT_DETERMINED,
content::Source<InstantController>(this),
content::NotificationService::NoDetails());
}
void InstantController::InstantPageAboutToNavigateMainFrame(
const content::WebContents* contents,
const GURL& url) {
DCHECK(IsContentsFrom(instant_tab(), contents));
UpdateInfoForInstantTab();
}
void InstantController::ResetInstantTab() {
if (!search_mode_.is_origin_default()) {
content::WebContents* active_tab = browser_->GetActiveWebContents();
if (!instant_tab_ || active_tab != instant_tab_->contents()) {
instant_tab_.reset(new InstantTab(this, browser_->profile()));
instant_tab_->Init(active_tab);
UpdateInfoForInstantTab();
}
} else {
instant_tab_.reset();
}
}
void InstantController::UpdateInfoForInstantTab() {
if (instant_tab_) {
InstantService* instant_service = GetInstantService();
if (instant_service) {
instant_service->UpdateThemeInfo();
instant_service->UpdateMostVisitedItemsInfo();
}
}
}
InstantService* InstantController::GetInstantService() const {
return InstantServiceFactory::GetForProfile(profile());
}