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/autocomplete/autocomplete_match.h"
9 #include "chrome/browser/extensions/api/omnibox/omnibox_api.h"
10 #include "chrome/browser/predictors/autocomplete_action_predictor.h"
11 #include "chrome/browser/predictors/autocomplete_action_predictor_factory.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/search/search.h"
14 #include "chrome/browser/search_engines/template_url_service.h"
15 #include "chrome/browser/search_engines/template_url_service_factory.h"
16 #include "chrome/browser/sessions/session_tab_helper.h"
17 #include "chrome/browser/ui/omnibox/omnibox_edit_controller.h"
18 #include "chrome/browser/ui/search/instant_search_prerenderer.h"
19 #include "chrome/browser/ui/search/search_tab_helper.h"
20 #include "content/public/browser/navigation_controller.h"
21 #include "content/public/browser/web_contents.h"
22 #include "content/public/browser/web_contents_view.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.
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
)),
85 void OmniboxCurrentPageDelegateImpl::NotifySearchTabHelper(
86 bool user_input_in_progress
,
88 if (!controller_
->GetWebContents())
90 SearchTabHelper::FromWebContents(
91 controller_
->GetWebContents())->OmniboxEditModelChanged(
92 user_input_in_progress
, cancelling
);
95 void OmniboxCurrentPageDelegateImpl::DoPrerender(
96 const AutocompleteMatch
& match
) {
97 content::WebContents
* web_contents
= controller_
->GetWebContents();
98 gfx::Rect container_bounds
;
99 web_contents
->GetView()->GetContainerBounds(&container_bounds
);
101 InstantSearchPrerenderer
* prerenderer
=
102 InstantSearchPrerenderer::GetForProfile(profile_
);
103 if (prerenderer
&& prerenderer
->IsAllowed(match
, web_contents
)) {
105 web_contents
->GetController().GetSessionStorageNamespaceMap(),
106 container_bounds
.size());
110 predictors::AutocompleteActionPredictorFactory::GetForProfile(profile_
)->
112 match
.destination_url
,
113 web_contents
->GetController().GetSessionStorageNamespaceMap(),
114 container_bounds
.size());