• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/web_contents.h"
19 #include "content/public/common/frame_navigate_params.h"
20 
~Delegate()21 InstantPage::Delegate::~Delegate() {
22 }
23 
~InstantPage()24 InstantPage::~InstantPage() {
25   if (contents())
26     SearchTabHelper::FromWebContents(contents())->model()->RemoveObserver(this);
27 }
28 
supports_instant() const29 bool InstantPage::supports_instant() const {
30   return contents() ?
31       SearchTabHelper::FromWebContents(contents())->SupportsInstant() : false;
32 }
33 
instant_url() const34 const std::string& InstantPage::instant_url() const {
35   return instant_url_;
36 }
37 
IsLocal() const38 bool InstantPage::IsLocal() const {
39   return contents() &&
40       contents()->GetURL() == GURL(chrome::kChromeSearchLocalNtpUrl);
41 }
42 
InstantPage(Delegate * delegate,const std::string & instant_url,Profile * profile,bool is_incognito)43 InstantPage::InstantPage(Delegate* delegate, const std::string& instant_url,
44                          Profile* profile, bool is_incognito)
45     : profile_(profile),
46       delegate_(delegate),
47       instant_url_(instant_url),
48       is_incognito_(is_incognito) {
49 }
50 
SetContents(content::WebContents * web_contents)51 void InstantPage::SetContents(content::WebContents* web_contents) {
52   ClearContents();
53 
54   if (!web_contents)
55     return;
56 
57   Observe(web_contents);
58   SearchModel* model = SearchTabHelper::FromWebContents(contents())->model();
59   model->AddObserver(this);
60 
61   // Already know whether the page supports instant.
62   if (model->instant_support() != INSTANT_SUPPORT_UNKNOWN)
63     InstantSupportDetermined(model->instant_support() == INSTANT_SUPPORT_YES);
64 }
65 
ShouldProcessAboutToNavigateMainFrame()66 bool InstantPage::ShouldProcessAboutToNavigateMainFrame() {
67   return false;
68 }
69 
DidCommitProvisionalLoadForFrame(int64,const base::string16 & frame_unique_name,bool is_main_frame,const GURL & url,content::PageTransition,content::RenderViewHost *)70 void InstantPage::DidCommitProvisionalLoadForFrame(
71     int64 /* frame_id */,
72     const base::string16& frame_unique_name,
73     bool is_main_frame,
74     const GURL& url,
75     content::PageTransition /* transition_type */,
76     content::RenderViewHost* /* render_view_host */) {
77   if (is_main_frame && ShouldProcessAboutToNavigateMainFrame())
78     delegate_->InstantPageAboutToNavigateMainFrame(contents(), url);
79 }
80 
ModelChanged(const SearchModel::State & old_state,const SearchModel::State & new_state)81 void InstantPage::ModelChanged(const SearchModel::State& old_state,
82                                const SearchModel::State& new_state) {
83   if (old_state.instant_support != new_state.instant_support)
84     InstantSupportDetermined(new_state.instant_support == INSTANT_SUPPORT_YES);
85 }
86 
InstantSupportDetermined(bool supports_instant)87 void InstantPage::InstantSupportDetermined(bool supports_instant) {
88   delegate_->InstantSupportDetermined(contents(), supports_instant);
89 
90   // If the page doesn't support Instant, stop listening to it.
91   if (!supports_instant)
92     ClearContents();
93 }
94 
ClearContents()95 void InstantPage::ClearContents() {
96   if (contents())
97     SearchTabHelper::FromWebContents(contents())->model()->RemoveObserver(this);
98 
99   Observe(NULL);
100 }
101