omnibox_current_page_delegate_impl.cc revision 0529e5d033099cbfc42635f6f6183833b09dff6e
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.
4
5#include "chrome/browser/ui/omnibox/omnibox_current_page_delegate_impl.h"
6
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"
24#include "url/gurl.h"
25
26OmniboxCurrentPageDelegateImpl::OmniboxCurrentPageDelegateImpl(
27    OmniboxEditController* controller,
28    Profile* profile)
29    : controller_(controller),
30      profile_(profile) {}
31
32OmniboxCurrentPageDelegateImpl::~OmniboxCurrentPageDelegateImpl() {}
33
34bool OmniboxCurrentPageDelegateImpl::CurrentPageExists() const {
35  return (controller_->GetWebContents() != NULL);
36}
37
38const GURL& OmniboxCurrentPageDelegateImpl::GetURL() const {
39  return controller_->GetWebContents()->GetURL();
40}
41
42bool OmniboxCurrentPageDelegateImpl::IsInstantNTP() const {
43  return chrome::IsInstantNTP(controller_->GetWebContents());
44}
45
46bool OmniboxCurrentPageDelegateImpl::IsSearchResultsPage() const {
47  Profile* profile = Profile::FromBrowserContext(
48      controller_->GetWebContents()->GetBrowserContext());
49  return TemplateURLServiceFactory::GetForProfile(profile)->
50      IsSearchResultsPageFromDefaultSearchProvider(GetURL());
51}
52
53bool OmniboxCurrentPageDelegateImpl::IsLoading() const {
54  return controller_->GetWebContents()->IsLoading();
55}
56
57content::NavigationController&
58    OmniboxCurrentPageDelegateImpl::GetNavigationController() const {
59  return controller_->GetWebContents()->GetController();
60}
61
62const SessionID& OmniboxCurrentPageDelegateImpl::GetSessionID() const {
63  return SessionTabHelper::FromWebContents(
64      controller_->GetWebContents())->session_id();
65}
66
67bool 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;
73
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);
81
82  return true;
83}
84
85void OmniboxCurrentPageDelegateImpl::OnInputStateChanged() {
86  if (!controller_->GetWebContents())
87    return;
88  SearchTabHelper::FromWebContents(
89      controller_->GetWebContents())->OmniboxInputStateChanged();
90}
91
92void OmniboxCurrentPageDelegateImpl::OnFocusChanged(
93    OmniboxFocusState state,
94    OmniboxFocusChangeReason reason) {
95  if (!controller_->GetWebContents())
96    return;
97  SearchTabHelper::FromWebContents(
98      controller_->GetWebContents())->OmniboxFocusChanged(state, reason);
99}
100
101void OmniboxCurrentPageDelegateImpl::DoPrerender(
102    const AutocompleteMatch& match) {
103  content::WebContents* web_contents = controller_->GetWebContents();
104  gfx::Rect container_bounds;
105  web_contents->GetView()->GetContainerBounds(&container_bounds);
106
107  InstantSearchPrerenderer* prerenderer =
108      InstantSearchPrerenderer::GetForProfile(profile_);
109  if (prerenderer && prerenderer->IsAllowed(match, web_contents)) {
110    prerenderer->Init(
111        web_contents->GetController().GetSessionStorageNamespaceMap(),
112        container_bounds.size());
113    return;
114  }
115
116  predictors::AutocompleteActionPredictorFactory::GetForProfile(profile_)->
117      StartPrerendering(
118          match.destination_url,
119          web_contents->GetController().GetSessionStorageNamespaceMap(),
120          container_bounds.size());
121}
122
123void OmniboxCurrentPageDelegateImpl::SetSuggestionToPrefetch(
124      const InstantSuggestion& suggestion) {
125  content::WebContents* web_contents = controller_->GetWebContents();
126  if (web_contents &&
127      SearchTabHelper::FromWebContents(web_contents)->IsSearchResultsPage()) {
128    if (chrome::ShouldPrefetchSearchResultsOnSRP() ||
129        chrome::ShouldPrefetchSearchResults()) {
130      SearchTabHelper::FromWebContents(web_contents)->
131          SetSuggestionToPrefetch(suggestion);
132    }
133  } else if (chrome::ShouldPrefetchSearchResults()) {
134      InstantSearchPrerenderer* prerenderer =
135          InstantSearchPrerenderer::GetForProfile(profile_);
136      if (prerenderer)
137        prerenderer->Prerender(suggestion);
138  }
139}
140