Adding instrumentation to locate the source of jankiness
[chromium-blink-merge.git] / chrome / browser / ui / omnibox / omnibox_current_page_delegate_impl.cc
blob2d3b4dbd8e208ac1fb012f7bb9e6479068f9f84b
1 // Copyright 2013 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.
5 #include "chrome/browser/ui/omnibox/omnibox_current_page_delegate_impl.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/extensions/api/omnibox/omnibox_api.h"
9 #include "chrome/browser/predictors/autocomplete_action_predictor.h"
10 #include "chrome/browser/predictors/autocomplete_action_predictor_factory.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/search/search.h"
13 #include "chrome/browser/search_engines/template_url_service_factory.h"
14 #include "chrome/browser/sessions/session_tab_helper.h"
15 #include "chrome/browser/ui/omnibox/omnibox_edit_controller.h"
16 #include "chrome/browser/ui/search/instant_search_prerenderer.h"
17 #include "chrome/browser/ui/search/search_tab_helper.h"
18 #include "components/omnibox/autocomplete_match.h"
19 #include "components/search/search.h"
20 #include "components/search_engines/template_url_service.h"
21 #include "content/public/browser/navigation_controller.h"
22 #include "content/public/browser/web_contents.h"
23 #include "ui/base/window_open_disposition.h"
24 #include "url/gurl.h"
26 OmniboxCurrentPageDelegateImpl::OmniboxCurrentPageDelegateImpl(
27 OmniboxEditController* controller,
28 Profile* profile)
29 : controller_(controller),
30 profile_(profile) {}
32 OmniboxCurrentPageDelegateImpl::~OmniboxCurrentPageDelegateImpl() {}
34 bool OmniboxCurrentPageDelegateImpl::CurrentPageExists() const {
35 return (controller_->GetWebContents() != NULL);
38 const GURL& OmniboxCurrentPageDelegateImpl::GetURL() const {
39 return controller_->GetWebContents()->GetURL();
42 bool OmniboxCurrentPageDelegateImpl::IsInstantNTP() const {
43 return chrome::IsInstantNTP(controller_->GetWebContents());
46 bool OmniboxCurrentPageDelegateImpl::IsSearchResultsPage() const {
47 Profile* profile = Profile::FromBrowserContext(
48 controller_->GetWebContents()->GetBrowserContext());
49 return TemplateURLServiceFactory::GetForProfile(profile)->
50 IsSearchResultsPageFromDefaultSearchProvider(GetURL());
53 bool OmniboxCurrentPageDelegateImpl::IsLoading() const {
54 return controller_->GetWebContents()->IsLoading();
57 content::NavigationController&
58 OmniboxCurrentPageDelegateImpl::GetNavigationController() const {
59 return controller_->GetWebContents()->GetController();
62 const SessionID& OmniboxCurrentPageDelegateImpl::GetSessionID() const {
63 return SessionTabHelper::FromWebContents(
64 controller_->GetWebContents())->session_id();
67 bool OmniboxCurrentPageDelegateImpl::ProcessExtensionKeyword(
68 TemplateURL* template_url,
69 const AutocompleteMatch& match,
70 WindowOpenDisposition disposition) {
71 if (template_url->GetType() != TemplateURL::OMNIBOX_API_EXTENSION)
72 return false;
74 // Strip the keyword + leading space off the input.
75 size_t prefix_length = match.keyword.length() + 1;
76 extensions::ExtensionOmniboxEventRouter::OnInputEntered(
77 controller_->GetWebContents(),
78 template_url->GetExtensionId(),
79 base::UTF16ToUTF8(match.fill_into_edit.substr(prefix_length)),
80 disposition);
82 return true;
85 void OmniboxCurrentPageDelegateImpl::OnInputStateChanged() {
86 if (!controller_->GetWebContents())
87 return;
88 SearchTabHelper::FromWebContents(
89 controller_->GetWebContents())->OmniboxInputStateChanged();
92 void OmniboxCurrentPageDelegateImpl::OnFocusChanged(
93 OmniboxFocusState state,
94 OmniboxFocusChangeReason reason) {
95 if (!controller_->GetWebContents())
96 return;
97 SearchTabHelper::FromWebContents(
98 controller_->GetWebContents())->OmniboxFocusChanged(state, reason);
101 void OmniboxCurrentPageDelegateImpl::DoPrerender(
102 const AutocompleteMatch& match) {
103 content::WebContents* web_contents = controller_->GetWebContents();
104 gfx::Rect container_bounds = web_contents->GetContainerBounds();
106 InstantSearchPrerenderer* prerenderer =
107 InstantSearchPrerenderer::GetForProfile(profile_);
108 if (prerenderer && prerenderer->IsAllowed(match, web_contents)) {
109 prerenderer->Init(
110 web_contents->GetController().GetSessionStorageNamespaceMap(),
111 container_bounds.size());
112 return;
115 predictors::AutocompleteActionPredictorFactory::GetForProfile(profile_)->
116 StartPrerendering(
117 match.destination_url,
118 web_contents->GetController().GetSessionStorageNamespaceMap(),
119 container_bounds.size());
122 void OmniboxCurrentPageDelegateImpl::SetSuggestionToPrefetch(
123 const InstantSuggestion& suggestion) {
124 DCHECK(chrome::IsInstantExtendedAPIEnabled());
125 content::WebContents* web_contents = controller_->GetWebContents();
126 if (web_contents &&
127 SearchTabHelper::FromWebContents(web_contents)->IsSearchResultsPage()) {
128 if (chrome::ShouldPrefetchSearchResultsOnSRP()) {
129 SearchTabHelper::FromWebContents(web_contents)->
130 SetSuggestionToPrefetch(suggestion);
132 } else {
133 InstantSearchPrerenderer* prerenderer =
134 InstantSearchPrerenderer::GetForProfile(profile_);
135 if (prerenderer)
136 prerenderer->Prerender(suggestion);