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/search/instant_page.h"
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"
22 InstantPage::Delegate::~Delegate() {
25 InstantPage::~InstantPage() {
27 SearchTabHelper::FromWebContents(web_contents())->model()->RemoveObserver(
32 bool InstantPage::supports_instant() const {
33 return web_contents() &&
34 SearchTabHelper::FromWebContents(web_contents())->SupportsInstant();
37 const std::string
& InstantPage::instant_url() const {
41 bool InstantPage::IsLocal() const {
42 return web_contents() &&
43 web_contents()->GetURL() == GURL(chrome::kChromeSearchLocalNtpUrl
);
46 InstantPage::InstantPage(Delegate
* delegate
, const std::string
& instant_url
,
47 Profile
* profile
, bool is_incognito
)
50 instant_url_(instant_url
),
51 is_incognito_(is_incognito
) {
54 void InstantPage::SetContents(content::WebContents
* new_web_contents
) {
57 if (!new_web_contents
)
60 Observe(new_web_contents
);
62 SearchTabHelper::FromWebContents(web_contents())->model();
63 model
->AddObserver(this);
65 // Already know whether the page supports instant.
66 if (model
->instant_support() != INSTANT_SUPPORT_UNKNOWN
)
67 InstantSupportDetermined(model
->instant_support() == INSTANT_SUPPORT_YES
);
70 bool InstantPage::ShouldProcessAboutToNavigateMainFrame() {
74 void InstantPage::DidCommitProvisionalLoadForFrame(
75 content::RenderFrameHost
* render_frame_host
,
77 ui::PageTransition
/* transition_type */) {
78 if (!render_frame_host
->GetParent() &&
79 ShouldProcessAboutToNavigateMainFrame())
80 delegate_
->InstantPageAboutToNavigateMainFrame(web_contents(), url
);
83 void 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
);
89 void InstantPage::InstantSupportDetermined(bool supports_instant
) {
90 delegate_
->InstantSupportDetermined(web_contents(), supports_instant
);
92 // If the page doesn't support Instant, stop listening to it.
93 if (!supports_instant
)
97 void InstantPage::ClearContents() {
99 SearchTabHelper::FromWebContents(web_contents())->model()->RemoveObserver(