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/search/instant_page.h" 6 7#include "chrome/browser/profiles/profile.h" 8#include "chrome/browser/search/search.h" 9#include "chrome/browser/ui/search/search_model.h" 10#include "chrome/browser/ui/search/search_tab_helper.h" 11#include "chrome/common/render_messages.h" 12#include "chrome/common/url_constants.h" 13#include "content/public/browser/navigation_controller.h" 14#include "content/public/browser/navigation_details.h" 15#include "content/public/browser/navigation_entry.h" 16#include "content/public/browser/notification_service.h" 17#include "content/public/browser/notification_source.h" 18#include "content/public/browser/render_frame_host.h" 19#include "content/public/browser/web_contents.h" 20#include "content/public/common/frame_navigate_params.h" 21 22InstantPage::Delegate::~Delegate() { 23} 24 25InstantPage::~InstantPage() { 26 if (web_contents()) { 27 SearchTabHelper::FromWebContents(web_contents())->model()->RemoveObserver( 28 this); 29 } 30} 31 32bool InstantPage::supports_instant() const { 33 return web_contents() && 34 SearchTabHelper::FromWebContents(web_contents())->SupportsInstant(); 35} 36 37const std::string& InstantPage::instant_url() const { 38 return instant_url_; 39} 40 41bool InstantPage::IsLocal() const { 42 return web_contents() && 43 web_contents()->GetURL() == GURL(chrome::kChromeSearchLocalNtpUrl); 44} 45 46InstantPage::InstantPage(Delegate* delegate, const std::string& instant_url, 47 Profile* profile, bool is_incognito) 48 : profile_(profile), 49 delegate_(delegate), 50 instant_url_(instant_url), 51 is_incognito_(is_incognito) { 52} 53 54void InstantPage::SetContents(content::WebContents* new_web_contents) { 55 ClearContents(); 56 57 if (!new_web_contents) 58 return; 59 60 Observe(new_web_contents); 61 SearchModel* model = 62 SearchTabHelper::FromWebContents(web_contents())->model(); 63 model->AddObserver(this); 64 65 // Already know whether the page supports instant. 66 if (model->instant_support() != INSTANT_SUPPORT_UNKNOWN) 67 InstantSupportDetermined(model->instant_support() == INSTANT_SUPPORT_YES); 68} 69 70bool InstantPage::ShouldProcessAboutToNavigateMainFrame() { 71 return false; 72} 73 74void InstantPage::DidCommitProvisionalLoadForFrame( 75 content::RenderFrameHost* render_frame_host, 76 const GURL& url, 77 ui::PageTransition /* transition_type */) { 78 if (!render_frame_host->GetParent() && 79 ShouldProcessAboutToNavigateMainFrame()) 80 delegate_->InstantPageAboutToNavigateMainFrame(web_contents(), url); 81} 82 83void InstantPage::ModelChanged(const SearchModel::State& old_state, 84 const SearchModel::State& new_state) { 85 if (old_state.instant_support != new_state.instant_support) 86 InstantSupportDetermined(new_state.instant_support == INSTANT_SUPPORT_YES); 87} 88 89void InstantPage::InstantSupportDetermined(bool supports_instant) { 90 delegate_->InstantSupportDetermined(web_contents(), supports_instant); 91 92 // If the page doesn't support Instant, stop listening to it. 93 if (!supports_instant) 94 ClearContents(); 95} 96 97void InstantPage::ClearContents() { 98 if (web_contents()) { 99 SearchTabHelper::FromWebContents(web_contents())->model()->RemoveObserver( 100 this); 101 } 102 103 Observe(NULL); 104} 105