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 "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.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" |
8 #include "chrome/common/render_messages.h" | 11 #include "chrome/common/render_messages.h" |
9 #include "chrome/common/url_constants.h" | 12 #include "chrome/common/url_constants.h" |
10 #include "content/public/browser/web_contents.h" | 13 #include "content/public/browser/web_contents.h" |
11 #include "ui/base/resource/resource_bundle.h" | 14 #include "ui/base/resource/resource_bundle.h" |
12 #include "ui/gfx/font.h" | 15 #include "ui/gfx/font.h" |
13 | 16 |
14 InstantPage::Delegate::~Delegate() { | 17 InstantPage::Delegate::~Delegate() { |
15 } | 18 } |
16 | 19 |
17 InstantPage::~InstantPage() { | 20 InstantPage::~InstantPage() { |
| 21 if (extended_enabled_ && contents()) |
| 22 GetSearchModel()->RemoveObserver(this); |
18 } | 23 } |
19 | 24 |
20 bool InstantPage::IsLocal() const { | 25 bool InstantPage::IsLocal() const { |
21 return contents() && | 26 return contents() && |
22 (contents()->GetURL() == GURL(chrome::kChromeSearchLocalNtpUrl) || | 27 (contents()->GetURL() == GURL(chrome::kChromeSearchLocalNtpUrl) || |
23 contents()->GetURL() == GURL(chrome::kChromeSearchLocalGoogleNtpUrl)); | 28 contents()->GetURL() == GURL(chrome::kChromeSearchLocalGoogleNtpUrl)); |
24 } | 29 } |
25 | 30 |
26 void InstantPage::Update(const string16& text, | 31 void InstantPage::Update(const string16& text, |
27 size_t selection_start, | 32 size_t selection_start, |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 } | 105 } |
101 | 106 |
102 void InstantPage::SendMostVisitedItems( | 107 void InstantPage::SendMostVisitedItems( |
103 const std::vector<InstantMostVisitedItemIDPair>& items) { | 108 const std::vector<InstantMostVisitedItemIDPair>& items) { |
104 Send(new ChromeViewMsg_SearchBoxMostVisitedItemsChanged(routing_id(), items)); | 109 Send(new ChromeViewMsg_SearchBoxMostVisitedItemsChanged(routing_id(), items)); |
105 } | 110 } |
106 | 111 |
107 InstantPage::InstantPage(Delegate* delegate, const std::string& instant_url) | 112 InstantPage::InstantPage(Delegate* delegate, const std::string& instant_url) |
108 : delegate_(delegate), | 113 : delegate_(delegate), |
109 instant_url_(instant_url), | 114 instant_url_(instant_url), |
| 115 extended_enabled_(chrome::IsInstantExtendedAPIEnabled()), |
110 supports_instant_(false) { | 116 supports_instant_(false) { |
111 } | 117 } |
112 | 118 |
113 void InstantPage::SetContents(content::WebContents* contents) { | 119 void InstantPage::SetContents(content::WebContents* contents) { |
| 120 if (extended_enabled_ && web_contents()) |
| 121 GetSearchModel()->RemoveObserver(this); |
| 122 |
114 Observe(contents); | 123 Observe(contents); |
| 124 |
| 125 if (extended_enabled_ && contents) { |
| 126 SearchModel* model = GetSearchModel(); |
| 127 model->AddObserver(this); |
| 128 |
| 129 // Already know whether the page supports instant. |
| 130 if (model->instant_support() != INSTANT_SUPPORT_UNKNOWN) |
| 131 SetSupportsInstant(model->instant_support() == INSTANT_SUPPORT_YES); |
| 132 } |
115 } | 133 } |
116 | 134 |
117 bool InstantPage::ShouldProcessRenderViewCreated() { | 135 bool InstantPage::ShouldProcessRenderViewCreated() { |
118 return false; | 136 return false; |
119 } | 137 } |
120 | 138 |
121 bool InstantPage::ShouldProcessRenderViewGone() { | 139 bool InstantPage::ShouldProcessRenderViewGone() { |
122 return false; | 140 return false; |
123 } | 141 } |
124 | 142 |
(...skipping 20 matching lines...) Expand all Loading... |
145 void InstantPage::RenderViewCreated(content::RenderViewHost* render_view_host) { | 163 void InstantPage::RenderViewCreated(content::RenderViewHost* render_view_host) { |
146 if (ShouldProcessRenderViewCreated()) | 164 if (ShouldProcessRenderViewCreated()) |
147 delegate_->InstantPageRenderViewCreated(contents()); | 165 delegate_->InstantPageRenderViewCreated(contents()); |
148 } | 166 } |
149 | 167 |
150 void InstantPage::DidFinishLoad( | 168 void InstantPage::DidFinishLoad( |
151 int64 /* frame_id */, | 169 int64 /* frame_id */, |
152 const GURL& /* validated_url */, | 170 const GURL& /* validated_url */, |
153 bool is_main_frame, | 171 bool is_main_frame, |
154 content::RenderViewHost* /* render_view_host */) { | 172 content::RenderViewHost* /* render_view_host */) { |
155 if (is_main_frame && !supports_instant_) | 173 // For Instant Extended, SearchTabHelper keeps track of Instant support. |
| 174 if (!extended_enabled_ && |
| 175 is_main_frame && !supports_instant_) |
156 DetermineIfPageSupportsInstant(); | 176 DetermineIfPageSupportsInstant(); |
157 } | 177 } |
158 | 178 |
159 bool InstantPage::OnMessageReceived(const IPC::Message& message) { | 179 bool InstantPage::OnMessageReceived(const IPC::Message& message) { |
160 bool handled = true; | 180 bool handled = true; |
161 IPC_BEGIN_MESSAGE_MAP(InstantPage, message) | 181 IPC_BEGIN_MESSAGE_MAP(InstantPage, message) |
162 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SetSuggestions, OnSetSuggestions) | 182 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SetSuggestions, OnSetSuggestions) |
163 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_InstantSupportDetermined, | 183 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_InstantSupportDetermined, |
164 OnInstantSupportDetermined) | 184 OnInstantSupportDetermined) |
165 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ShowInstantOverlay, | 185 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ShowInstantOverlay, |
(...skipping 20 matching lines...) Expand all Loading... |
186 void InstantPage::DidCommitProvisionalLoadForFrame( | 206 void InstantPage::DidCommitProvisionalLoadForFrame( |
187 int64 /* frame_id */, | 207 int64 /* frame_id */, |
188 bool is_main_frame, | 208 bool is_main_frame, |
189 const GURL& url, | 209 const GURL& url, |
190 content::PageTransition /* transition_type */, | 210 content::PageTransition /* transition_type */, |
191 content::RenderViewHost* /* render_view_host */) { | 211 content::RenderViewHost* /* render_view_host */) { |
192 if (is_main_frame && ShouldProcessAboutToNavigateMainFrame()) | 212 if (is_main_frame && ShouldProcessAboutToNavigateMainFrame()) |
193 delegate_->InstantPageAboutToNavigateMainFrame(contents(), url); | 213 delegate_->InstantPageAboutToNavigateMainFrame(contents(), url); |
194 } | 214 } |
195 | 215 |
| 216 void InstantPage::ModelChanged(const SearchModel::State& old_state, |
| 217 const SearchModel::State& new_state) { |
| 218 if (old_state.instant_support != new_state.instant_support) |
| 219 SetSupportsInstant(new_state.instant_support == INSTANT_SUPPORT_YES); |
| 220 } |
| 221 |
| 222 SearchModel* InstantPage::GetSearchModel() { |
| 223 return contents() ? |
| 224 SearchTabHelper::FromWebContents(contents())->model() : NULL; |
| 225 } |
| 226 |
| 227 void InstantPage::SetSupportsInstant(bool supports_instant) { |
| 228 // Nothing to do if the page already supports Instant. |
| 229 if (supports_instant_) |
| 230 return; |
| 231 |
| 232 supports_instant_ = supports_instant; |
| 233 delegate_->InstantSupportDetermined(contents(), supports_instant); |
| 234 |
| 235 // If the page doesn't support Instant, stop listening to it. |
| 236 if (!supports_instant) |
| 237 SetContents(NULL); |
| 238 } |
| 239 |
196 void InstantPage::OnSetSuggestions( | 240 void InstantPage::OnSetSuggestions( |
197 int page_id, | 241 int page_id, |
198 const std::vector<InstantSuggestion>& suggestions) { | 242 const std::vector<InstantSuggestion>& suggestions) { |
199 if (contents()->IsActiveEntry(page_id)) { | 243 if (contents()->IsActiveEntry(page_id)) { |
200 OnInstantSupportDetermined(page_id, true); | 244 SetSupportsInstant(true); |
201 if (ShouldProcessSetSuggestions()) | 245 if (ShouldProcessSetSuggestions()) |
202 delegate_->SetSuggestions(contents(), suggestions); | 246 delegate_->SetSuggestions(contents(), suggestions); |
203 } | 247 } |
204 } | 248 } |
205 | 249 |
206 void InstantPage::OnInstantSupportDetermined(int page_id, | 250 void InstantPage::OnInstantSupportDetermined(int page_id, |
207 bool supports_instant) { | 251 bool supports_instant) { |
208 if (!contents()->IsActiveEntry(page_id) || supports_instant_) { | 252 // For Instant Extended, SearchTabHelper keeps track of Instant support. |
209 // Nothing to do if the page already supports Instant. | 253 if (!extended_enabled_ && contents()->IsActiveEntry(page_id)) |
210 return; | 254 SetSupportsInstant(supports_instant); |
211 } | |
212 | |
213 supports_instant_ = supports_instant; | |
214 delegate_->InstantSupportDetermined(contents(), supports_instant); | |
215 | |
216 // If the page doesn't support Instant, stop listening to it. | |
217 if (!supports_instant) | |
218 Observe(NULL); | |
219 } | 255 } |
220 | 256 |
221 void InstantPage::OnShowInstantOverlay(int page_id, | 257 void InstantPage::OnShowInstantOverlay(int page_id, |
222 int height, | 258 int height, |
223 InstantSizeUnits units) { | 259 InstantSizeUnits units) { |
224 if (contents()->IsActiveEntry(page_id)) { | 260 if (contents()->IsActiveEntry(page_id)) { |
225 OnInstantSupportDetermined(page_id, true); | 261 SetSupportsInstant(true); |
226 if (ShouldProcessShowInstantOverlay()) | 262 if (ShouldProcessShowInstantOverlay()) |
227 delegate_->ShowInstantOverlay(contents(), height, units); | 263 delegate_->ShowInstantOverlay(contents(), height, units); |
228 } | 264 } |
229 } | 265 } |
230 | 266 |
231 void InstantPage::OnFocusOmnibox(int page_id, OmniboxFocusState state) { | 267 void InstantPage::OnFocusOmnibox(int page_id, OmniboxFocusState state) { |
232 if (contents()->IsActiveEntry(page_id)) { | 268 if (contents()->IsActiveEntry(page_id)) { |
233 OnInstantSupportDetermined(page_id, true); | 269 SetSupportsInstant(true); |
234 if (ShouldProcessFocusOmnibox()) | 270 if (ShouldProcessFocusOmnibox()) |
235 delegate_->FocusOmnibox(contents(), state); | 271 delegate_->FocusOmnibox(contents(), state); |
236 } | 272 } |
237 } | 273 } |
238 | 274 |
239 void InstantPage::OnSearchBoxNavigate(int page_id, | 275 void InstantPage::OnSearchBoxNavigate(int page_id, |
240 const GURL& url, | 276 const GURL& url, |
241 content::PageTransition transition, | 277 content::PageTransition transition, |
242 WindowOpenDisposition disposition) { | 278 WindowOpenDisposition disposition) { |
243 if (contents()->IsActiveEntry(page_id)) { | 279 if (contents()->IsActiveEntry(page_id)) { |
244 OnInstantSupportDetermined(page_id, true); | 280 SetSupportsInstant(true); |
245 if (ShouldProcessNavigateToURL()) | 281 if (ShouldProcessNavigateToURL()) |
246 delegate_->NavigateToURL(contents(), url, transition, disposition); | 282 delegate_->NavigateToURL(contents(), url, transition, disposition); |
247 } | 283 } |
248 } | 284 } |
249 | 285 |
250 void InstantPage::OnDeleteMostVisitedItem(InstantRestrictedID restricted_id) { | 286 void InstantPage::OnDeleteMostVisitedItem(InstantRestrictedID restricted_id) { |
251 delegate_->DeleteMostVisitedItem(restricted_id); | 287 delegate_->DeleteMostVisitedItem(restricted_id); |
252 } | 288 } |
253 | 289 |
254 void InstantPage::OnUndoMostVisitedDeletion(InstantRestrictedID restricted_id) { | 290 void InstantPage::OnUndoMostVisitedDeletion(InstantRestrictedID restricted_id) { |
255 delegate_->UndoMostVisitedDeletion(restricted_id); | 291 delegate_->UndoMostVisitedDeletion(restricted_id); |
256 } | 292 } |
257 | 293 |
258 void InstantPage::OnUndoAllMostVisitedDeletions() { | 294 void InstantPage::OnUndoAllMostVisitedDeletions() { |
259 delegate_->UndoAllMostVisitedDeletions(); | 295 delegate_->UndoAllMostVisitedDeletions(); |
260 } | 296 } |
OLD | NEW |