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
OmniboxCurrentPageDelegateImpl(OmniboxEditController * controller,Profile * profile)26 OmniboxCurrentPageDelegateImpl::OmniboxCurrentPageDelegateImpl(
27 OmniboxEditController* controller,
28 Profile* profile)
29 : controller_(controller),
30 profile_(profile) {}
31
~OmniboxCurrentPageDelegateImpl()32 OmniboxCurrentPageDelegateImpl::~OmniboxCurrentPageDelegateImpl() {}
33
CurrentPageExists() const34 bool OmniboxCurrentPageDelegateImpl::CurrentPageExists() const {
35 return (controller_->GetWebContents() != NULL);
36 }
37
GetURL() const38 const GURL& OmniboxCurrentPageDelegateImpl::GetURL() const {
39 return controller_->GetWebContents()->GetURL();
40 }
41
IsInstantNTP() const42 bool OmniboxCurrentPageDelegateImpl::IsInstantNTP() const {
43 return chrome::IsInstantNTP(controller_->GetWebContents());
44 }
45
IsSearchResultsPage() const46 bool OmniboxCurrentPageDelegateImpl::IsSearchResultsPage() const {
47 Profile* profile = Profile::FromBrowserContext(
48 controller_->GetWebContents()->GetBrowserContext());
49 return TemplateURLServiceFactory::GetForProfile(profile)->
50 IsSearchResultsPageFromDefaultSearchProvider(GetURL());
51 }
52
IsLoading() const53 bool OmniboxCurrentPageDelegateImpl::IsLoading() const {
54 return controller_->GetWebContents()->IsLoading();
55 }
56
57 content::NavigationController&
GetNavigationController() const58 OmniboxCurrentPageDelegateImpl::GetNavigationController() const {
59 return controller_->GetWebContents()->GetController();
60 }
61
GetSessionID() const62 const SessionID& OmniboxCurrentPageDelegateImpl::GetSessionID() const {
63 return SessionTabHelper::FromWebContents(
64 controller_->GetWebContents())->session_id();
65 }
66
ProcessExtensionKeyword(TemplateURL * template_url,const AutocompleteMatch & match,WindowOpenDisposition disposition)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;
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 UTF16ToUTF8(match.fill_into_edit.substr(prefix_length)),
80 disposition);
81
82 return true;
83 }
84
NotifySearchTabHelper(bool user_input_in_progress,bool cancelling)85 void OmniboxCurrentPageDelegateImpl::NotifySearchTabHelper(
86 bool user_input_in_progress,
87 bool cancelling) {
88 if (!controller_->GetWebContents())
89 return;
90 SearchTabHelper::FromWebContents(
91 controller_->GetWebContents())->OmniboxEditModelChanged(
92 user_input_in_progress, cancelling);
93 }
94
DoPrerender(const AutocompleteMatch & match)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);
100
101 InstantSearchPrerenderer* prerenderer =
102 InstantSearchPrerenderer::GetForProfile(profile_);
103 if (prerenderer && prerenderer->IsAllowed(match, web_contents)) {
104 prerenderer->Init(
105 web_contents->GetController().GetSessionStorageNamespaceMap(),
106 container_bounds.size());
107 return;
108 }
109
110 predictors::AutocompleteActionPredictorFactory::GetForProfile(profile_)->
111 StartPrerendering(
112 match.destination_url,
113 web_contents->GetController().GetSessionStorageNamespaceMap(),
114 container_bounds.size());
115 }
116