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 (contents()) |
| 26 GetSearchModel()->RemoveObserver(this); |
22 } | 27 } |
23 | 28 |
24 bool InstantPage::supports_instant() const { | 29 bool InstantPage::supports_instant() const { |
25 return supports_instant_; | 30 return supports_instant_; |
26 } | 31 } |
27 | 32 |
28 bool InstantPage::IsLocal() const { | 33 bool InstantPage::IsLocal() const { |
29 return contents() && | 34 return contents() && |
30 (contents()->GetURL() == GURL(chrome::kChromeSearchLocalNtpUrl) || | 35 (contents()->GetURL() == GURL(chrome::kChromeSearchLocalNtpUrl) || |
31 contents()->GetURL() == GURL(chrome::kChromeSearchLocalGoogleNtpUrl)); | 36 contents()->GetURL() == GURL(chrome::kChromeSearchLocalGoogleNtpUrl)); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 const gfx::Font& omnibox_font = | 69 const gfx::Font& omnibox_font = |
65 ui::ResourceBundle::GetSharedInstance().GetFont( | 70 ui::ResourceBundle::GetSharedInstance().GetFont( |
66 ui::ResourceBundle::MediumFont); | 71 ui::ResourceBundle::MediumFont); |
67 #endif | 72 #endif |
68 string16 omnibox_font_name = UTF8ToUTF16(omnibox_font.GetFontName()); | 73 string16 omnibox_font_name = UTF8ToUTF16(omnibox_font.GetFontName()); |
69 size_t omnibox_font_size = omnibox_font.GetFontSize(); | 74 size_t omnibox_font_size = omnibox_font.GetFontSize(); |
70 Send(new ChromeViewMsg_SearchBoxFontInformation( | 75 Send(new ChromeViewMsg_SearchBoxFontInformation( |
71 routing_id(), omnibox_font_name, omnibox_font_size)); | 76 routing_id(), omnibox_font_name, omnibox_font_size)); |
72 } | 77 } |
73 | 78 |
74 void InstantPage::DetermineIfPageSupportsInstant() { | |
75 if (IsLocal()) { | |
76 // Local pages always support Instant. That's why we keep them around. | |
77 int page_id = contents()->GetController().GetActiveEntry()->GetPageID(); | |
78 OnInstantSupportDetermined(page_id, true); | |
79 } else { | |
80 Send(new ChromeViewMsg_DetermineIfPageSupportsInstant(routing_id())); | |
81 } | |
82 } | |
83 | |
84 void InstantPage::SendAutocompleteResults( | 79 void InstantPage::SendAutocompleteResults( |
85 const std::vector<InstantAutocompleteResult>& results) { | 80 const std::vector<InstantAutocompleteResult>& results) { |
86 Send(new ChromeViewMsg_SearchBoxAutocompleteResults(routing_id(), results)); | 81 Send(new ChromeViewMsg_SearchBoxAutocompleteResults(routing_id(), results)); |
87 } | 82 } |
88 | 83 |
89 void InstantPage::UpOrDownKeyPressed(int count) { | 84 void InstantPage::UpOrDownKeyPressed(int count) { |
90 Send(new ChromeViewMsg_SearchBoxUpOrDownKeyPressed(routing_id(), count)); | 85 Send(new ChromeViewMsg_SearchBoxUpOrDownKeyPressed(routing_id(), count)); |
91 } | 86 } |
92 | 87 |
93 void InstantPage::EscKeyPressed() { | 88 void InstantPage::EscKeyPressed() { |
(...skipping 24 matching lines...) Expand all Loading... |
118 } | 113 } |
119 | 114 |
120 void InstantPage::SendMostVisitedItems( | 115 void InstantPage::SendMostVisitedItems( |
121 const std::vector<InstantMostVisitedItemIDPair>& items) { | 116 const std::vector<InstantMostVisitedItemIDPair>& items) { |
122 Send(new ChromeViewMsg_SearchBoxMostVisitedItemsChanged(routing_id(), items)); | 117 Send(new ChromeViewMsg_SearchBoxMostVisitedItemsChanged(routing_id(), items)); |
123 } | 118 } |
124 | 119 |
125 InstantPage::InstantPage(Delegate* delegate, const std::string& instant_url) | 120 InstantPage::InstantPage(Delegate* delegate, const std::string& instant_url) |
126 : delegate_(delegate), | 121 : delegate_(delegate), |
127 instant_url_(instant_url), | 122 instant_url_(instant_url), |
128 supports_instant_(false), | 123 supports_instant_(false) { |
129 instant_support_determined_(false) { | |
130 } | 124 } |
131 | 125 |
132 void InstantPage::SetContents(content::WebContents* contents) { | 126 void InstantPage::SetContents(content::WebContents* contents) { |
| 127 if (web_contents()) |
| 128 GetSearchModel()->RemoveObserver(this); |
| 129 |
133 Observe(contents); | 130 Observe(contents); |
| 131 |
| 132 if (!contents) { |
| 133 supports_instant_ = false; |
| 134 return; |
| 135 } |
| 136 |
| 137 SearchModel* model = GetSearchModel(); |
| 138 model->AddObserver(this); |
| 139 |
| 140 // Already know whether the page supports instant. |
| 141 if (model->instant_support() != INSTANT_SUPPORT_UNKNOWN) |
| 142 SetSupportsInstant(model->instant_support() == INSTANT_SUPPORT_YES); |
134 } | 143 } |
135 | 144 |
136 bool InstantPage::ShouldProcessRenderViewCreated() { | 145 bool InstantPage::ShouldProcessRenderViewCreated() { |
137 return false; | 146 return false; |
138 } | 147 } |
139 | 148 |
140 bool InstantPage::ShouldProcessRenderViewGone() { | 149 bool InstantPage::ShouldProcessRenderViewGone() { |
141 return false; | 150 return false; |
142 } | 151 } |
143 | 152 |
(...skipping 15 matching lines...) Expand all Loading... |
159 | 168 |
160 bool InstantPage::ShouldProcessNavigateToURL() { | 169 bool InstantPage::ShouldProcessNavigateToURL() { |
161 return false; | 170 return false; |
162 } | 171 } |
163 | 172 |
164 void InstantPage::RenderViewCreated(content::RenderViewHost* render_view_host) { | 173 void InstantPage::RenderViewCreated(content::RenderViewHost* render_view_host) { |
165 if (ShouldProcessRenderViewCreated()) | 174 if (ShouldProcessRenderViewCreated()) |
166 delegate_->InstantPageRenderViewCreated(contents()); | 175 delegate_->InstantPageRenderViewCreated(contents()); |
167 } | 176 } |
168 | 177 |
169 void InstantPage::DidFinishLoad( | |
170 int64 /* frame_id */, | |
171 const GURL& /* validated_url */, | |
172 bool is_main_frame, | |
173 content::RenderViewHost* /* render_view_host */) { | |
174 if (is_main_frame && !supports_instant_) | |
175 DetermineIfPageSupportsInstant(); | |
176 } | |
177 | |
178 bool InstantPage::OnMessageReceived(const IPC::Message& message) { | 178 bool InstantPage::OnMessageReceived(const IPC::Message& message) { |
179 bool handled = true; | 179 bool handled = true; |
180 IPC_BEGIN_MESSAGE_MAP(InstantPage, message) | 180 IPC_BEGIN_MESSAGE_MAP(InstantPage, message) |
181 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SetSuggestions, OnSetSuggestions) | 181 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SetSuggestions, OnSetSuggestions) |
182 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_InstantSupportDetermined, | |
183 OnInstantSupportDetermined) | |
184 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ShowInstantOverlay, | 182 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ShowInstantOverlay, |
185 OnShowInstantOverlay) | 183 OnShowInstantOverlay) |
186 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_FocusOmnibox, OnFocusOmnibox) | 184 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_FocusOmnibox, OnFocusOmnibox) |
187 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxNavigate, | 185 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxNavigate, |
188 OnSearchBoxNavigate); | 186 OnSearchBoxNavigate); |
189 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem, | 187 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxDeleteMostVisitedItem, |
190 OnDeleteMostVisitedItem); | 188 OnDeleteMostVisitedItem); |
191 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion, | 189 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxUndoMostVisitedDeletion, |
192 OnUndoMostVisitedDeletion); | 190 OnUndoMostVisitedDeletion); |
193 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions, | 191 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxUndoAllMostVisitedDeletions, |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 int64 /* frame_id */, | 224 int64 /* frame_id */, |
227 bool is_main_frame, | 225 bool is_main_frame, |
228 const GURL& /* validated_url */, | 226 const GURL& /* validated_url */, |
229 int /* error_code */, | 227 int /* error_code */, |
230 const string16& /* error_description */, | 228 const string16& /* error_description */, |
231 content::RenderViewHost* /* render_view_host */) { | 229 content::RenderViewHost* /* render_view_host */) { |
232 if (is_main_frame) | 230 if (is_main_frame) |
233 delegate_->InstantPageLoadFailed(contents()); | 231 delegate_->InstantPageLoadFailed(contents()); |
234 } | 232 } |
235 | 233 |
| 234 void InstantPage::ModelChanged(const SearchModel::State& old_state, |
| 235 const SearchModel::State& new_state) { |
| 236 if (old_state.instant_support != new_state.instant_support) |
| 237 SetSupportsInstant(new_state.instant_support == INSTANT_SUPPORT_YES); |
| 238 } |
| 239 |
| 240 SearchModel* InstantPage::GetSearchModel() { |
| 241 return contents() ? |
| 242 SearchTabHelper::FromWebContents(contents())->model() : NULL; |
| 243 } |
| 244 |
| 245 void InstantPage::SetSupportsInstant(bool supports_instant) { |
| 246 // Nothing to do if the page already supports Instant. |
| 247 if (supports_instant_) |
| 248 return; |
| 249 |
| 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 SetContents(NULL); |
| 256 } |
| 257 |
236 void InstantPage::OnSetSuggestions( | 258 void InstantPage::OnSetSuggestions( |
237 int page_id, | 259 int page_id, |
238 const std::vector<InstantSuggestion>& suggestions) { | 260 const std::vector<InstantSuggestion>& suggestions) { |
239 if (!contents()->IsActiveEntry(page_id)) | 261 if (!contents()->IsActiveEntry(page_id)) |
240 return; | 262 return; |
241 | 263 |
242 OnInstantSupportDetermined(page_id, true); | 264 SearchTabHelper::FromWebContents(contents())->InstantSupportChanged(true); |
243 if (!ShouldProcessSetSuggestions()) | 265 if (!ShouldProcessSetSuggestions()) |
244 return; | 266 return; |
245 | 267 |
246 delegate_->SetSuggestions(contents(), suggestions); | 268 delegate_->SetSuggestions(contents(), suggestions); |
247 } | 269 } |
248 | 270 |
249 void InstantPage::OnInstantSupportDetermined(int page_id, | |
250 bool supports_instant) { | |
251 if (!contents()->IsActiveEntry(page_id) || supports_instant_) { | |
252 // Nothing to do if the page already supports Instant. | |
253 return; | |
254 } | |
255 | |
256 instant_support_determined_ = true; | |
257 supports_instant_ = supports_instant; | |
258 delegate_->InstantSupportDetermined(contents(), supports_instant); | |
259 | |
260 // If the page doesn't support Instant, stop listening to it. | |
261 if (!supports_instant) | |
262 Observe(NULL); | |
263 } | |
264 | |
265 void InstantPage::OnShowInstantOverlay(int page_id, | 271 void InstantPage::OnShowInstantOverlay(int page_id, |
266 int height, | 272 int height, |
267 InstantSizeUnits units) { | 273 InstantSizeUnits units) { |
268 if (!contents()->IsActiveEntry(page_id)) | 274 if (!contents()->IsActiveEntry(page_id)) |
269 return; | 275 return; |
270 | 276 |
271 OnInstantSupportDetermined(page_id, true); | 277 SearchTabHelper::FromWebContents(contents())->InstantSupportChanged(true); |
272 delegate_->LogDropdownShown(); | 278 delegate_->LogDropdownShown(); |
273 if (!ShouldProcessShowInstantOverlay()) | 279 if (!ShouldProcessShowInstantOverlay()) |
274 return; | 280 return; |
275 | 281 |
276 delegate_->ShowInstantOverlay(contents(), height, units); | 282 delegate_->ShowInstantOverlay(contents(), height, units); |
277 } | 283 } |
278 | 284 |
279 void InstantPage::OnFocusOmnibox(int page_id, OmniboxFocusState state) { | 285 void InstantPage::OnFocusOmnibox(int page_id, OmniboxFocusState state) { |
280 if (!contents()->IsActiveEntry(page_id)) | 286 if (!contents()->IsActiveEntry(page_id)) |
281 return; | 287 return; |
282 | 288 |
283 OnInstantSupportDetermined(page_id, true); | 289 SearchTabHelper::FromWebContents(contents())->InstantSupportChanged(true); |
284 if (!ShouldProcessFocusOmnibox()) | 290 if (!ShouldProcessFocusOmnibox()) |
285 return; | 291 return; |
286 | 292 |
287 delegate_->FocusOmnibox(contents(), state); | 293 delegate_->FocusOmnibox(contents(), state); |
288 } | 294 } |
289 | 295 |
290 void InstantPage::OnSearchBoxNavigate(int page_id, | 296 void InstantPage::OnSearchBoxNavigate(int page_id, |
291 const GURL& url, | 297 const GURL& url, |
292 content::PageTransition transition, | 298 content::PageTransition transition, |
293 WindowOpenDisposition disposition, | 299 WindowOpenDisposition disposition, |
294 bool is_search_type) { | 300 bool is_search_type) { |
295 if (!contents()->IsActiveEntry(page_id)) | 301 if (!contents()->IsActiveEntry(page_id)) |
296 return; | 302 return; |
297 | 303 |
298 OnInstantSupportDetermined(page_id, true); | 304 SearchTabHelper::FromWebContents(contents())->InstantSupportChanged(true); |
299 if (!ShouldProcessNavigateToURL()) | 305 if (!ShouldProcessNavigateToURL()) |
300 return; | 306 return; |
301 | 307 |
302 delegate_->NavigateToURL( | 308 delegate_->NavigateToURL( |
303 contents(), url, transition, disposition, is_search_type); | 309 contents(), url, transition, disposition, is_search_type); |
304 } | 310 } |
305 | 311 |
306 void InstantPage::OnDeleteMostVisitedItem(const GURL& url) { | 312 void InstantPage::OnDeleteMostVisitedItem(const GURL& url) { |
| 313 // TODO(kmadhusu): Call delegate_->DeleteMostVisitedItem() only if the message |
| 314 // is for the active entry and if the page should process the message. |
307 delegate_->DeleteMostVisitedItem(url); | 315 delegate_->DeleteMostVisitedItem(url); |
308 } | 316 } |
309 | 317 |
310 void InstantPage::OnUndoMostVisitedDeletion(const GURL& url) { | 318 void InstantPage::OnUndoMostVisitedDeletion(const GURL& url) { |
| 319 // TODO(kmadhusu): Call delegate_->UndoMostVisitedDeletion() only if the |
| 320 // message is for the active entry and if the page should process the message. |
311 delegate_->UndoMostVisitedDeletion(url); | 321 delegate_->UndoMostVisitedDeletion(url); |
312 } | 322 } |
313 | 323 |
314 void InstantPage::OnUndoAllMostVisitedDeletions() { | 324 void InstantPage::OnUndoAllMostVisitedDeletions() { |
| 325 // TODO(kmadhusu): Call delegate_->UndoAllMostVisitedDeletions() only if the |
| 326 // message is for the active entry and if the page should process the message. |
315 delegate_->UndoAllMostVisitedDeletions(); | 327 delegate_->UndoAllMostVisitedDeletions(); |
316 } | 328 } |
OLD | NEW |