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"
26 OmniboxCurrentPageDelegateImpl::OmniboxCurrentPageDelegateImpl(
27 OmniboxEditController
* controller
,
29 : controller_(controller
),
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
)
74 // Strip the keyword + leading space off the input, but don't exceed
75 // fill_into_edit. An obvious case is that the user may not have entered
76 // a leading space and is asking to launch this extension without any
78 size_t prefix_length
=
79 std::min(match
.keyword
.length() + 1, match
.fill_into_edit
.length());
80 extensions::ExtensionOmniboxEventRouter::OnInputEntered(
81 controller_
->GetWebContents(),
82 template_url
->GetExtensionId(),
83 base::UTF16ToUTF8(match
.fill_into_edit
.substr(prefix_length
)),
89 void OmniboxCurrentPageDelegateImpl::OnInputStateChanged() {
90 if (!controller_
->GetWebContents())
92 SearchTabHelper::FromWebContents(
93 controller_
->GetWebContents())->OmniboxInputStateChanged();
96 void OmniboxCurrentPageDelegateImpl::OnFocusChanged(
97 OmniboxFocusState state
,
98 OmniboxFocusChangeReason reason
) {
99 if (!controller_
->GetWebContents())
101 SearchTabHelper::FromWebContents(
102 controller_
->GetWebContents())->OmniboxFocusChanged(state
, reason
);
105 void OmniboxCurrentPageDelegateImpl::DoPrerender(
106 const AutocompleteMatch
& match
) {
107 content::WebContents
* web_contents
= controller_
->GetWebContents();
108 gfx::Rect container_bounds
= web_contents
->GetContainerBounds();
110 InstantSearchPrerenderer
* prerenderer
=
111 InstantSearchPrerenderer::GetForProfile(profile_
);
112 if (prerenderer
&& prerenderer
->IsAllowed(match
, web_contents
)) {
114 web_contents
->GetController().GetDefaultSessionStorageNamespace(),
115 container_bounds
.size());
119 predictors::AutocompleteActionPredictorFactory::GetForProfile(profile_
)->
121 match
.destination_url
,
122 web_contents
->GetController().GetDefaultSessionStorageNamespace(),
123 container_bounds
.size());
126 void OmniboxCurrentPageDelegateImpl::SetSuggestionToPrefetch(
127 const InstantSuggestion
& suggestion
) {
128 DCHECK(chrome::IsInstantExtendedAPIEnabled());
129 content::WebContents
* web_contents
= controller_
->GetWebContents();
131 SearchTabHelper::FromWebContents(web_contents
)->IsSearchResultsPage()) {
132 if (chrome::ShouldPrefetchSearchResultsOnSRP()) {
133 SearchTabHelper::FromWebContents(web_contents
)->
134 SetSuggestionToPrefetch(suggestion
);
137 InstantSearchPrerenderer
* prerenderer
=
138 InstantSearchPrerenderer::GetForProfile(profile_
);
140 prerenderer
->Prerender(suggestion
);