Home | History | Annotate | Download | only in omnibox
      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 "ui/base/window_open_disposition.h"
     23 #include "url/gurl.h"
     24 
     25 OmniboxCurrentPageDelegateImpl::OmniboxCurrentPageDelegateImpl(
     26     OmniboxEditController* controller,
     27     Profile* profile)
     28     : controller_(controller),
     29       profile_(profile) {}
     30 
     31 OmniboxCurrentPageDelegateImpl::~OmniboxCurrentPageDelegateImpl() {}
     32 
     33 bool OmniboxCurrentPageDelegateImpl::CurrentPageExists() const {
     34   return (controller_->GetWebContents() != NULL);
     35 }
     36 
     37 const GURL& OmniboxCurrentPageDelegateImpl::GetURL() const {
     38   return controller_->GetWebContents()->GetURL();
     39 }
     40 
     41 bool OmniboxCurrentPageDelegateImpl::IsInstantNTP() const {
     42   return chrome::IsInstantNTP(controller_->GetWebContents());
     43 }
     44 
     45 bool OmniboxCurrentPageDelegateImpl::IsSearchResultsPage() const {
     46   Profile* profile = Profile::FromBrowserContext(
     47       controller_->GetWebContents()->GetBrowserContext());
     48   return TemplateURLServiceFactory::GetForProfile(profile)->
     49       IsSearchResultsPageFromDefaultSearchProvider(GetURL());
     50 }
     51 
     52 bool OmniboxCurrentPageDelegateImpl::IsLoading() const {
     53   return controller_->GetWebContents()->IsLoading();
     54 }
     55 
     56 content::NavigationController&
     57     OmniboxCurrentPageDelegateImpl::GetNavigationController() const {
     58   return controller_->GetWebContents()->GetController();
     59 }
     60 
     61 const SessionID& OmniboxCurrentPageDelegateImpl::GetSessionID() const {
     62   return SessionTabHelper::FromWebContents(
     63       controller_->GetWebContents())->session_id();
     64 }
     65 
     66 bool OmniboxCurrentPageDelegateImpl::ProcessExtensionKeyword(
     67     TemplateURL* template_url,
     68     const AutocompleteMatch& match,
     69     WindowOpenDisposition disposition) {
     70   if (template_url->GetType() != TemplateURL::OMNIBOX_API_EXTENSION)
     71     return false;
     72 
     73   // Strip the keyword + leading space off the input.
     74   size_t prefix_length = match.keyword.length() + 1;
     75   extensions::ExtensionOmniboxEventRouter::OnInputEntered(
     76       controller_->GetWebContents(),
     77       template_url->GetExtensionId(),
     78       base::UTF16ToUTF8(match.fill_into_edit.substr(prefix_length)),
     79       disposition);
     80 
     81   return true;
     82 }
     83 
     84 void OmniboxCurrentPageDelegateImpl::OnInputStateChanged() {
     85   if (!controller_->GetWebContents())
     86     return;
     87   SearchTabHelper::FromWebContents(
     88       controller_->GetWebContents())->OmniboxInputStateChanged();
     89 }
     90 
     91 void OmniboxCurrentPageDelegateImpl::OnFocusChanged(
     92     OmniboxFocusState state,
     93     OmniboxFocusChangeReason reason) {
     94   if (!controller_->GetWebContents())
     95     return;
     96   SearchTabHelper::FromWebContents(
     97       controller_->GetWebContents())->OmniboxFocusChanged(state, reason);
     98 }
     99 
    100 void OmniboxCurrentPageDelegateImpl::DoPrerender(
    101     const AutocompleteMatch& match) {
    102   content::WebContents* web_contents = controller_->GetWebContents();
    103   gfx::Rect container_bounds = web_contents->GetContainerBounds();
    104 
    105   InstantSearchPrerenderer* prerenderer =
    106       InstantSearchPrerenderer::GetForProfile(profile_);
    107   if (prerenderer && prerenderer->IsAllowed(match, web_contents)) {
    108     prerenderer->Init(
    109         web_contents->GetController().GetSessionStorageNamespaceMap(),
    110         container_bounds.size());
    111     return;
    112   }
    113 
    114   predictors::AutocompleteActionPredictorFactory::GetForProfile(profile_)->
    115       StartPrerendering(
    116           match.destination_url,
    117           web_contents->GetController().GetSessionStorageNamespaceMap(),
    118           container_bounds.size());
    119 }
    120 
    121 void OmniboxCurrentPageDelegateImpl::SetSuggestionToPrefetch(
    122       const InstantSuggestion& suggestion) {
    123   content::WebContents* web_contents = controller_->GetWebContents();
    124   if (web_contents &&
    125       SearchTabHelper::FromWebContents(web_contents)->IsSearchResultsPage()) {
    126     if (chrome::ShouldPrefetchSearchResultsOnSRP() ||
    127         chrome::ShouldPrefetchSearchResults()) {
    128       SearchTabHelper::FromWebContents(web_contents)->
    129           SetSuggestionToPrefetch(suggestion);
    130     }
    131   } else if (chrome::ShouldPrefetchSearchResults()) {
    132       InstantSearchPrerenderer* prerenderer =
    133           InstantSearchPrerenderer::GetForProfile(profile_);
    134       if (prerenderer)
    135         prerenderer->Prerender(suggestion);
    136   }
    137 }
    138