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" | |
8 #include "chrome/browser/ui/search/search_tab_helper.h" | |
9 #include "chrome/common/url_constants.h" | |
10 #include "content/public/browser/navigation_handle.h" | 7 #include "content/public/browser/navigation_handle.h" |
11 #include "content/public/browser/render_frame_host.h" | |
12 #include "content/public/browser/web_contents.h" | 8 #include "content/public/browser/web_contents.h" |
13 | 9 |
14 InstantTab::Delegate::~Delegate() {} | 10 InstantTab::Delegate::~Delegate() {} |
15 | 11 |
16 InstantTab::InstantTab(Delegate* delegate, content::WebContents* web_contents) | 12 InstantTab::InstantTab(Delegate* delegate, content::WebContents* web_contents) |
17 : delegate_(delegate), pending_web_contents_(web_contents) {} | 13 : delegate_(delegate), pending_web_contents_(web_contents) {} |
18 | 14 |
19 InstantTab::~InstantTab() { | 15 InstantTab::~InstantTab() {} |
20 if (web_contents()) { | |
21 SearchTabHelper::FromWebContents(web_contents())->model()->RemoveObserver( | |
22 this); | |
23 } | |
24 } | |
25 | 16 |
26 void InstantTab::Init() { | 17 void InstantTab::Init() { |
27 if (!pending_web_contents_) | 18 if (!pending_web_contents_) |
28 return; | 19 return; |
29 | 20 |
30 Observe(pending_web_contents_); | 21 Observe(pending_web_contents_); |
31 pending_web_contents_ = nullptr; | 22 pending_web_contents_ = nullptr; |
32 | |
33 SearchModel* model = | |
34 SearchTabHelper::FromWebContents(web_contents())->model(); | |
35 model->AddObserver(this); | |
36 | |
37 // Already know whether the page supports instant. | |
38 if (model->instant_support() != INSTANT_SUPPORT_UNKNOWN) | |
39 InstantSupportDetermined(model->instant_support() == INSTANT_SUPPORT_YES); | |
40 } | 23 } |
41 | 24 |
42 void InstantTab::DidFinishNavigation( | 25 void InstantTab::DidFinishNavigation( |
43 content::NavigationHandle* navigation_handle) { | 26 content::NavigationHandle* navigation_handle) { |
44 if (navigation_handle->HasCommitted() && navigation_handle->IsInMainFrame()) { | 27 if (navigation_handle->HasCommitted() && navigation_handle->IsInMainFrame()) { |
45 delegate_->InstantTabAboutToNavigateMainFrame( | 28 delegate_->InstantTabAboutToNavigateMainFrame( |
46 web_contents(), navigation_handle->GetURL()); | 29 web_contents(), navigation_handle->GetURL()); |
47 } | 30 } |
48 } | 31 } |
49 | |
50 void InstantTab::ModelChanged(const SearchModel::State& old_state, | |
51 const SearchModel::State& new_state) { | |
52 if (old_state.instant_support != new_state.instant_support) | |
53 InstantSupportDetermined(new_state.instant_support == INSTANT_SUPPORT_YES); | |
54 } | |
55 | |
56 void InstantTab::InstantSupportDetermined(bool supports_instant) { | |
57 delegate_->InstantSupportDetermined(web_contents(), supports_instant); | |
58 | |
59 // If the page doesn't support Instant, stop listening to it. | |
60 if (!supports_instant) { | |
61 if (web_contents()) { | |
62 SearchTabHelper::FromWebContents(web_contents())->model()->RemoveObserver( | |
63 this); | |
64 } | |
65 | |
66 Observe(nullptr); | |
67 } | |
68 } | |
OLD | NEW |