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/navigation_controller.h" | 13 #include "content/public/browser/navigation_controller.h" |
11 #include "content/public/browser/navigation_details.h" | 14 #include "content/public/browser/navigation_details.h" |
12 #include "content/public/browser/navigation_entry.h" | 15 #include "content/public/browser/navigation_entry.h" |
13 #include "content/public/browser/web_contents.h" | 16 #include "content/public/browser/web_contents.h" |
14 #include "content/public/common/frame_navigate_params.h" | 17 #include "content/public/common/frame_navigate_params.h" |
15 #include "ui/base/resource/resource_bundle.h" | 18 #include "ui/base/resource/resource_bundle.h" |
16 #include "ui/gfx/font.h" | 19 #include "ui/gfx/font.h" |
17 | 20 |
18 InstantPage::Delegate::~Delegate() { | 21 InstantPage::Delegate::~Delegate() { |
19 } | 22 } |
20 | 23 |
21 InstantPage::~InstantPage() { | 24 InstantPage::~InstantPage() { |
| 25 if (extended_enabled_ && contents()) |
| 26 GetSearchModel()->RemoveObserver(this); |
22 } | 27 } |
23 | 28 |
24 bool InstantPage::IsLocal() const { | 29 bool InstantPage::IsLocal() const { |
25 return contents() && | 30 return contents() && |
26 (contents()->GetURL() == GURL(chrome::kChromeSearchLocalNtpUrl) || | 31 (contents()->GetURL() == GURL(chrome::kChromeSearchLocalNtpUrl) || |
27 contents()->GetURL() == GURL(chrome::kChromeSearchLocalGoogleNtpUrl)); | 32 contents()->GetURL() == GURL(chrome::kChromeSearchLocalGoogleNtpUrl)); |
28 } | 33 } |
29 | 34 |
30 void InstantPage::Update(const string16& text, | 35 void InstantPage::Update(const string16& text, |
31 size_t selection_start, | 36 size_t selection_start, |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 } | 119 } |
115 | 120 |
116 void InstantPage::SendMostVisitedItems( | 121 void InstantPage::SendMostVisitedItems( |
117 const std::vector<InstantMostVisitedItemIDPair>& items) { | 122 const std::vector<InstantMostVisitedItemIDPair>& items) { |
118 Send(new ChromeViewMsg_SearchBoxMostVisitedItemsChanged(routing_id(), items)); | 123 Send(new ChromeViewMsg_SearchBoxMostVisitedItemsChanged(routing_id(), items)); |
119 } | 124 } |
120 | 125 |
121 InstantPage::InstantPage(Delegate* delegate, const std::string& instant_url) | 126 InstantPage::InstantPage(Delegate* delegate, const std::string& instant_url) |
122 : delegate_(delegate), | 127 : delegate_(delegate), |
123 instant_url_(instant_url), | 128 instant_url_(instant_url), |
| 129 extended_enabled_(chrome::IsInstantExtendedAPIEnabled()), |
124 supports_instant_(false), | 130 supports_instant_(false), |
125 instant_support_determined_(false) { | 131 instant_support_determined_(false) { |
126 } | 132 } |
127 | 133 |
128 void InstantPage::SetContents(content::WebContents* contents) { | 134 void InstantPage::SetContents(content::WebContents* contents) { |
| 135 if (extended_enabled_ && web_contents()) |
| 136 GetSearchModel()->RemoveObserver(this); |
| 137 |
129 Observe(contents); | 138 Observe(contents); |
| 139 |
| 140 if (extended_enabled_ && contents) { |
| 141 SearchModel* model = GetSearchModel(); |
| 142 model->AddObserver(this); |
| 143 |
| 144 // Already know whether the page supports instant. |
| 145 if (model->instant_support() != INSTANT_SUPPORT_UNKNOWN) |
| 146 SetSupportsInstant(model->instant_support() == INSTANT_SUPPORT_YES); |
| 147 } |
130 } | 148 } |
131 | 149 |
132 bool InstantPage::ShouldProcessRenderViewCreated() { | 150 bool InstantPage::ShouldProcessRenderViewCreated() { |
133 return false; | 151 return false; |
134 } | 152 } |
135 | 153 |
136 bool InstantPage::ShouldProcessRenderViewGone() { | 154 bool InstantPage::ShouldProcessRenderViewGone() { |
137 return false; | 155 return false; |
138 } | 156 } |
139 | 157 |
(...skipping 20 matching lines...) Expand all Loading... |
160 void InstantPage::RenderViewCreated(content::RenderViewHost* render_view_host) { | 178 void InstantPage::RenderViewCreated(content::RenderViewHost* render_view_host) { |
161 if (ShouldProcessRenderViewCreated()) | 179 if (ShouldProcessRenderViewCreated()) |
162 delegate_->InstantPageRenderViewCreated(contents()); | 180 delegate_->InstantPageRenderViewCreated(contents()); |
163 } | 181 } |
164 | 182 |
165 void InstantPage::DidFinishLoad( | 183 void InstantPage::DidFinishLoad( |
166 int64 /* frame_id */, | 184 int64 /* frame_id */, |
167 const GURL& /* validated_url */, | 185 const GURL& /* validated_url */, |
168 bool is_main_frame, | 186 bool is_main_frame, |
169 content::RenderViewHost* /* render_view_host */) { | 187 content::RenderViewHost* /* render_view_host */) { |
170 if (is_main_frame && !supports_instant_) | 188 // For Instant Extended, SearchTabHelper keeps track of Instant support. |
| 189 if (!extended_enabled_ && is_main_frame && !supports_instant_) |
171 DetermineIfPageSupportsInstant(); | 190 DetermineIfPageSupportsInstant(); |
172 } | 191 } |
173 | 192 |
174 bool InstantPage::OnMessageReceived(const IPC::Message& message) { | 193 bool InstantPage::OnMessageReceived(const IPC::Message& message) { |
175 bool handled = true; | 194 bool handled = true; |
176 IPC_BEGIN_MESSAGE_MAP(InstantPage, message) | 195 IPC_BEGIN_MESSAGE_MAP(InstantPage, message) |
177 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SetSuggestions, OnSetSuggestions) | 196 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SetSuggestions, OnSetSuggestions) |
178 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_InstantSupportDetermined, | 197 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_InstantSupportDetermined, |
179 OnInstantSupportDetermined) | 198 OnInstantSupportDetermined) |
180 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ShowInstantOverlay, | 199 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ShowInstantOverlay, |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 int64 /* frame_id */, | 241 int64 /* frame_id */, |
223 bool is_main_frame, | 242 bool is_main_frame, |
224 const GURL& /* validated_url */, | 243 const GURL& /* validated_url */, |
225 int /* error_code */, | 244 int /* error_code */, |
226 const string16& /* error_description */, | 245 const string16& /* error_description */, |
227 content::RenderViewHost* /* render_view_host */) { | 246 content::RenderViewHost* /* render_view_host */) { |
228 if (is_main_frame) | 247 if (is_main_frame) |
229 delegate_->InstantPageLoadFailed(contents()); | 248 delegate_->InstantPageLoadFailed(contents()); |
230 } | 249 } |
231 | 250 |
| 251 void InstantPage::ModelChanged(const SearchModel::State& old_state, |
| 252 const SearchModel::State& new_state) { |
| 253 if (old_state.instant_support != new_state.instant_support) |
| 254 SetSupportsInstant(new_state.instant_support == INSTANT_SUPPORT_YES); |
| 255 } |
| 256 |
| 257 SearchModel* InstantPage::GetSearchModel() { |
| 258 return contents() ? |
| 259 SearchTabHelper::FromWebContents(contents())->model() : NULL; |
| 260 } |
| 261 |
| 262 void InstantPage::SetSupportsInstant(bool supports_instant) { |
| 263 if (supports_instant_ == supports_instant) |
| 264 return; |
| 265 |
| 266 supports_instant_ = supports_instant; |
| 267 delegate_->InstantSupportDetermined(contents(), supports_instant); |
| 268 |
| 269 // If the page doesn't support Instant, stop listening to it. |
| 270 if (!supports_instant) |
| 271 SetContents(NULL); |
| 272 } |
| 273 |
232 void InstantPage::OnSetSuggestions( | 274 void InstantPage::OnSetSuggestions( |
233 int page_id, | 275 int page_id, |
234 const std::vector<InstantSuggestion>& suggestions) { | 276 const std::vector<InstantSuggestion>& suggestions) { |
235 if (contents()->IsActiveEntry(page_id)) { | 277 if (contents()->IsActiveEntry(page_id)) { |
236 OnInstantSupportDetermined(page_id, true); | 278 SearchTabHelper::FromWebContents(contents())-> |
| 279 OnInstantSupportDeterminedMsgReceived(page_id, true); |
237 if (ShouldProcessSetSuggestions()) | 280 if (ShouldProcessSetSuggestions()) |
238 delegate_->SetSuggestions(contents(), suggestions); | 281 delegate_->SetSuggestions(contents(), suggestions); |
239 } | 282 } |
240 } | 283 } |
241 | 284 |
242 void InstantPage::OnInstantSupportDetermined(int page_id, | 285 void InstantPage::OnInstantSupportDetermined(int page_id, |
243 bool supports_instant) { | 286 bool supports_instant) { |
244 if (!contents()->IsActiveEntry(page_id) || supports_instant_) { | 287 // TODO(kmadhusu): Get rid of this handler and the assoicated logic after |
245 // Nothing to do if the page already supports Instant. | 288 // committing crrev.com/14855014. |
246 return; | 289 // For Instant Extended, SearchTabHelper keeps track of Instant support. |
247 } | 290 if (!extended_enabled_ && contents()->IsActiveEntry(page_id)) |
248 | 291 SetSupportsInstant(supports_instant); |
249 instant_support_determined_ = true; | |
250 supports_instant_ = supports_instant; | |
251 delegate_->InstantSupportDetermined(contents(), supports_instant); | |
252 | |
253 // If the page doesn't support Instant, stop listening to it. | |
254 if (!supports_instant) | |
255 Observe(NULL); | |
256 } | 292 } |
257 | 293 |
258 void InstantPage::OnShowInstantOverlay(int page_id, | 294 void InstantPage::OnShowInstantOverlay(int page_id, |
259 int height, | 295 int height, |
260 InstantSizeUnits units) { | 296 InstantSizeUnits units) { |
261 if (contents()->IsActiveEntry(page_id)) { | 297 if (contents()->IsActiveEntry(page_id)) { |
262 OnInstantSupportDetermined(page_id, true); | 298 SearchTabHelper::FromWebContents(contents())-> |
| 299 OnInstantSupportDeterminedMsgReceived(page_id, true); |
263 if (ShouldProcessShowInstantOverlay()) | 300 if (ShouldProcessShowInstantOverlay()) |
264 delegate_->ShowInstantOverlay(contents(), height, units); | 301 delegate_->ShowInstantOverlay(contents(), height, units); |
265 } | 302 } |
266 } | 303 } |
267 | 304 |
268 void InstantPage::OnFocusOmnibox(int page_id, OmniboxFocusState state) { | 305 void InstantPage::OnFocusOmnibox(int page_id, OmniboxFocusState state) { |
269 if (contents()->IsActiveEntry(page_id)) { | 306 if (contents()->IsActiveEntry(page_id)) { |
270 OnInstantSupportDetermined(page_id, true); | 307 SearchTabHelper::FromWebContents(contents())-> |
| 308 OnInstantSupportDeterminedMsgReceived(page_id, true); |
271 if (ShouldProcessFocusOmnibox()) | 309 if (ShouldProcessFocusOmnibox()) |
272 delegate_->FocusOmnibox(contents(), state); | 310 delegate_->FocusOmnibox(contents(), state); |
273 } | 311 } |
274 } | 312 } |
275 | 313 |
276 void InstantPage::OnSearchBoxNavigate(int page_id, | 314 void InstantPage::OnSearchBoxNavigate(int page_id, |
277 const GURL& url, | 315 const GURL& url, |
278 content::PageTransition transition, | 316 content::PageTransition transition, |
279 WindowOpenDisposition disposition) { | 317 WindowOpenDisposition disposition) { |
280 if (contents()->IsActiveEntry(page_id)) { | 318 if (contents()->IsActiveEntry(page_id)) { |
281 OnInstantSupportDetermined(page_id, true); | 319 SearchTabHelper::FromWebContents(contents())-> |
| 320 OnInstantSupportDeterminedMsgReceived(page_id, true); |
282 if (ShouldProcessNavigateToURL()) | 321 if (ShouldProcessNavigateToURL()) |
283 delegate_->NavigateToURL(contents(), url, transition, disposition); | 322 delegate_->NavigateToURL(contents(), url, transition, disposition); |
284 } | 323 } |
285 } | 324 } |
286 | 325 |
287 void InstantPage::OnDeleteMostVisitedItem(InstantRestrictedID restricted_id) { | 326 void InstantPage::OnDeleteMostVisitedItem(InstantRestrictedID restricted_id) { |
288 delegate_->DeleteMostVisitedItem(restricted_id); | 327 delegate_->DeleteMostVisitedItem(restricted_id); |
289 } | 328 } |
290 | 329 |
291 void InstantPage::OnUndoMostVisitedDeletion(InstantRestrictedID restricted_id) { | 330 void InstantPage::OnUndoMostVisitedDeletion(InstantRestrictedID restricted_id) { |
292 delegate_->UndoMostVisitedDeletion(restricted_id); | 331 delegate_->UndoMostVisitedDeletion(restricted_id); |
293 } | 332 } |
294 | 333 |
295 void InstantPage::OnUndoAllMostVisitedDeletions() { | 334 void InstantPage::OnUndoAllMostVisitedDeletions() { |
296 delegate_->UndoAllMostVisitedDeletions(); | 335 delegate_->UndoAllMostVisitedDeletions(); |
297 } | 336 } |
OLD | NEW |