| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/search/instant_tab.h" | 5 #include "chrome/browser/ui/search/instant_tab.h" |
| 6 | 6 |
| 7 #include "chrome/browser/ui/search/search_model.h" | 7 #include "chrome/browser/ui/search/search_model.h" |
| 8 #include "chrome/browser/ui/search/search_tab_helper.h" | 8 #include "chrome/browser/ui/search/search_tab_helper.h" |
| 9 #include "chrome/common/url_constants.h" | 9 #include "chrome/common/url_constants.h" |
| 10 #include "content/public/browser/render_frame_host.h" | 10 #include "content/public/browser/render_frame_host.h" |
| 11 #include "content/public/browser/web_contents.h" | 11 #include "content/public/browser/web_contents.h" |
| 12 | 12 |
| 13 InstantTab::Delegate::~Delegate() { | 13 InstantTab::Delegate::~Delegate() {} |
| 14 } | 14 |
| 15 InstantTab::InstantTab(Delegate* delegate, content::WebContents* web_contents) |
| 16 : delegate_(delegate), pending_web_contents_(web_contents) {} |
| 15 | 17 |
| 16 InstantTab::~InstantTab() { | 18 InstantTab::~InstantTab() { |
| 17 if (web_contents()) { | 19 if (web_contents()) { |
| 18 SearchTabHelper::FromWebContents(web_contents())->model()->RemoveObserver( | 20 SearchTabHelper::FromWebContents(web_contents())->model()->RemoveObserver( |
| 19 this); | 21 this); |
| 20 } | 22 } |
| 21 } | 23 } |
| 22 | 24 |
| 23 bool InstantTab::supports_instant() const { | 25 void InstantTab::Init() { |
| 24 return web_contents() && | 26 if (!pending_web_contents_) |
| 25 SearchTabHelper::FromWebContents(web_contents())->SupportsInstant(); | |
| 26 } | |
| 27 | |
| 28 bool InstantTab::IsLocal() const { | |
| 29 return web_contents() && | |
| 30 web_contents()->GetURL() == GURL(chrome::kChromeSearchLocalNtpUrl); | |
| 31 } | |
| 32 | |
| 33 InstantTab::InstantTab(Delegate* delegate) | |
| 34 : delegate_(delegate) { | |
| 35 } | |
| 36 | |
| 37 void InstantTab::Init(content::WebContents* new_web_contents) { | |
| 38 ClearContents(); | |
| 39 | |
| 40 if (!new_web_contents) | |
| 41 return; | 27 return; |
| 42 | 28 |
| 43 Observe(new_web_contents); | 29 Observe(pending_web_contents_); |
| 30 pending_web_contents_ = nullptr; |
| 31 |
| 44 SearchModel* model = | 32 SearchModel* model = |
| 45 SearchTabHelper::FromWebContents(web_contents())->model(); | 33 SearchTabHelper::FromWebContents(web_contents())->model(); |
| 46 model->AddObserver(this); | 34 model->AddObserver(this); |
| 47 | 35 |
| 48 // Already know whether the page supports instant. | 36 // Already know whether the page supports instant. |
| 49 if (model->instant_support() != INSTANT_SUPPORT_UNKNOWN) | 37 if (model->instant_support() != INSTANT_SUPPORT_UNKNOWN) |
| 50 InstantSupportDetermined(model->instant_support() == INSTANT_SUPPORT_YES); | 38 InstantSupportDetermined(model->instant_support() == INSTANT_SUPPORT_YES); |
| 51 } | 39 } |
| 52 | 40 |
| 53 void InstantTab::DidCommitProvisionalLoadForFrame( | 41 void InstantTab::DidCommitProvisionalLoadForFrame( |
| 54 content::RenderFrameHost* render_frame_host, | 42 content::RenderFrameHost* render_frame_host, |
| 55 const GURL& url, | 43 const GURL& url, |
| 56 ui::PageTransition /* transition_type */) { | 44 ui::PageTransition /* transition_type */) { |
| 57 if (!render_frame_host->GetParent()) { | 45 if (!render_frame_host->GetParent()) |
| 58 delegate_->InstantTabAboutToNavigateMainFrame(web_contents(), url); | 46 delegate_->InstantTabAboutToNavigateMainFrame(web_contents(), url); |
| 59 } | |
| 60 } | 47 } |
| 61 | 48 |
| 62 void InstantTab::ModelChanged(const SearchModel::State& old_state, | 49 void InstantTab::ModelChanged(const SearchModel::State& old_state, |
| 63 const SearchModel::State& new_state) { | 50 const SearchModel::State& new_state) { |
| 64 if (old_state.instant_support != new_state.instant_support) | 51 if (old_state.instant_support != new_state.instant_support) |
| 65 InstantSupportDetermined(new_state.instant_support == INSTANT_SUPPORT_YES); | 52 InstantSupportDetermined(new_state.instant_support == INSTANT_SUPPORT_YES); |
| 66 } | 53 } |
| 67 | 54 |
| 68 void InstantTab::InstantSupportDetermined(bool supports_instant) { | 55 void InstantTab::InstantSupportDetermined(bool supports_instant) { |
| 69 delegate_->InstantSupportDetermined(web_contents(), supports_instant); | 56 delegate_->InstantSupportDetermined(web_contents(), supports_instant); |
| 70 | 57 |
| 71 // If the page doesn't support Instant, stop listening to it. | 58 // If the page doesn't support Instant, stop listening to it. |
| 72 if (!supports_instant) | 59 if (!supports_instant) { |
| 73 ClearContents(); | 60 if (web_contents()) { |
| 61 SearchTabHelper::FromWebContents(web_contents())->model()->RemoveObserver( |
| 62 this); |
| 63 } |
| 64 |
| 65 Observe(nullptr); |
| 66 } |
| 74 } | 67 } |
| 75 | |
| 76 void InstantTab::ClearContents() { | |
| 77 if (web_contents()) { | |
| 78 SearchTabHelper::FromWebContents(web_contents())->model()->RemoveObserver( | |
| 79 this); | |
| 80 } | |
| 81 | |
| 82 Observe(NULL); | |
| 83 } | |
| OLD | NEW |