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