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