Chromium Code Reviews| Index: chrome/browser/instant/instant_page.cc |
| diff --git a/chrome/browser/instant/instant_page.cc b/chrome/browser/instant/instant_page.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..1162c0cb3bc86be9b4faa6fd1e77a3723dfda428 |
| --- /dev/null |
| +++ b/chrome/browser/instant/instant_page.cc |
| @@ -0,0 +1,234 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/instant/instant_page.h" |
| + |
| +#include "base/utf_string_conversions.h" |
| +#include "chrome/common/render_messages.h" |
| +#include "content/public/browser/web_contents.h" |
| +#include "ui/base/resource/resource_bundle.h" |
| +#include "ui/gfx/font.h" |
| + |
| +InstantPage::Delegate::~Delegate() { |
| +} |
| + |
| +InstantPage::~InstantPage() { |
| +} |
| + |
| +void InstantPage::Update(const string16& text, |
| + size_t selection_start, |
| + size_t selection_end, |
| + bool verbatim) { |
| + Send(new ChromeViewMsg_SearchBoxChange(routing_id(), text, verbatim, |
| + selection_start, selection_end)); |
| +} |
| + |
| +void InstantPage::Submit(const string16& text) { |
| + Send(new ChromeViewMsg_SearchBoxSubmit(routing_id(), text)); |
| +} |
| + |
| +void InstantPage::Cancel(const string16& text) { |
| + Send(new ChromeViewMsg_SearchBoxCancel(routing_id(), text)); |
| +} |
| + |
| +void InstantPage::SetPopupBounds(const gfx::Rect& bounds) { |
| + Send(new ChromeViewMsg_SearchBoxPopupResize(routing_id(), bounds)); |
| +} |
| + |
| +void InstantPage::SetMarginSize(const int start, const int end) { |
| + Send(new ChromeViewMsg_SearchBoxMarginChange(routing_id(), start, end)); |
| +} |
| + |
| +void InstantPage::DetermineIfPageSupportsInstant() { |
| + Send(new ChromeViewMsg_DetermineIfPageSupportsInstant(routing_id())); |
| +} |
| + |
| +void InstantPage::SendAutocompleteResults( |
| + const std::vector<InstantAutocompleteResult>& results) { |
| + Send(new ChromeViewMsg_SearchBoxAutocompleteResults(routing_id(), results)); |
| +} |
| + |
| +void InstantPage::UpOrDownKeyPressed(int count) { |
| + Send(new ChromeViewMsg_SearchBoxUpOrDownKeyPressed(routing_id(), count)); |
| +} |
| + |
| +void InstantPage::SearchModeChanged(const chrome::search::Mode& mode) { |
| + Send(new ChromeViewMsg_SearchBoxModeChanged(routing_id(), mode)); |
| +} |
| + |
| +void InstantPage::SendThemeBackgroundInfo( |
| + const ThemeBackgroundInfo& theme_info) { |
| + Send(new ChromeViewMsg_SearchBoxThemeChanged(routing_id(), theme_info)); |
| +} |
| + |
| +void InstantPage::SendThemeAreaHeight(int height) { |
| + Send(new ChromeViewMsg_SearchBoxThemeAreaHeightChanged(routing_id(), height)); |
| +} |
| + |
| +void InstantPage::SetDisplayInstantResults(bool display_instant_results) { |
| + Send(new ChromeViewMsg_SearchBoxSetDisplayInstantResults( |
| + routing_id(), display_instant_results)); |
| +} |
| + |
| +void InstantPage::KeyCaptureChanged(bool is_key_capture_enabled) { |
| + Send(new ChromeViewMsg_SearchBoxKeyCaptureChanged( |
| + routing_id(), is_key_capture_enabled)); |
| +} |
| + |
| +InstantPage::InstantPage(Delegate* delegate) |
| + : delegate_(delegate), |
| + contents_(NULL), |
| + supports_instant_(false) { |
| +} |
| + |
| +void InstantPage::SetContents(content::WebContents* contents) { |
| + Observe(contents); |
| + contents_ = contents; |
| +} |
| + |
| +bool InstantPage::ShouldProcessRenderViewGone() const { |
| + return true; |
| +} |
| + |
| +bool InstantPage::ShouldProcessAboutToNavigateMainFrame() const { |
| + return true; |
| +} |
| + |
| +bool InstantPage::ShouldProcessSetSuggestions() const { |
| + return true; |
| +} |
| + |
| +bool InstantPage::ShouldProcessShowInstantPreview() const { |
| + return true; |
| +} |
| + |
| +bool InstantPage::ShouldProcessStartCapturingKeyStrokes() const { |
| + return true; |
| +} |
| + |
| +bool InstantPage::ShouldProcessStopCapturingKeyStrokes() const { |
| + return true; |
| +} |
| + |
| +bool InstantPage::ShouldProcessNavigateToURL() const { |
| + return true; |
| +} |
| + |
| +void InstantPage::DidFinishLoad( |
| + int64 /* frame_id */, |
| + const GURL& /* validated_url */, |
| + bool is_main_frame, |
| + content::RenderViewHost* /* render_view_host */) { |
| + if (is_main_frame && !supports_instant_) |
| + DetermineIfPageSupportsInstant(); |
| +} |
| + |
| +bool InstantPage::OnMessageReceived(const IPC::Message& message) { |
| + bool handled = true; |
| + IPC_BEGIN_MESSAGE_MAP(InstantPage, message) |
| + IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SetSuggestions, OnSetSuggestions) |
| + IPC_MESSAGE_HANDLER(ChromeViewHostMsg_InstantSupportDetermined, |
| + OnInstantSupportDetermined) |
| + IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ShowInstantPreview, |
| + OnShowInstantPreview) |
| + IPC_MESSAGE_HANDLER(ChromeViewHostMsg_StartCapturingKeyStrokes, |
| + OnStartCapturingKeyStrokes); |
| + IPC_MESSAGE_HANDLER(ChromeViewHostMsg_StopCapturingKeyStrokes, |
| + OnStopCapturingKeyStrokes); |
| + IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxNavigate, |
| + OnSearchBoxNavigate); |
| + IPC_MESSAGE_UNHANDLED(handled = false) |
| + IPC_END_MESSAGE_MAP() |
| + return handled; |
| +} |
| + |
| +void InstantPage::RenderViewGone(base::TerminationStatus status) { |
|
sreeram
2013/01/29 13:35:42
/* status */
samarth
2013/01/31 16:24:57
Done.
|
| + if (ShouldProcessRenderViewGone()) |
| + delegate_->InstantPageRenderViewGone(contents_); |
| +} |
| + |
| +void InstantPage::DidCommitProvisionalLoadForFrame( |
| + int64 frame_id, |
| + bool is_main_frame, |
| + const GURL& url, |
| + content::PageTransition transition_type, |
| + content::RenderViewHost* render_view_host) { |
|
sreeram
2013/01/29 13:35:42
Comment out names that are unused.
samarth
2013/01/31 16:24:57
Done.
|
| + if (is_main_frame && ShouldProcessAboutToNavigateMainFrame()) |
| + delegate_->InstantPageAboutToNavigateMainFrame(contents_, url); |
| +} |
| + |
| +void InstantPage::OnSetSuggestions( |
| + int page_id, |
| + const std::vector<InstantSuggestion>& suggestions) { |
| + if (contents_ && contents_->IsActiveEntry(page_id)) { |
| + OnInstantSupportDetermined(page_id, true); |
| + if (ShouldProcessSetSuggestions()) |
| + delegate_->SetSuggestions(contents_, suggestions); |
| + } |
| +} |
| + |
| +void InstantPage::OnInstantSupportDetermined(int page_id, |
| + bool supports_instant) { |
| + if (!contents_ || !contents_->IsActiveEntry(page_id) || supports_instant_) { |
| + // Nothing to do if the page already supports instant. |
| + return; |
| + } |
| + |
| + supports_instant_ = supports_instant; |
| + delegate_->InstantSupportDetermined(contents_, supports_instant); |
| + |
| + if (supports_instant) { |
| + // Inform the renderer process of the Omnibox's font information. |
| + // TODO(samarth): this logic doesn't really belong here. Move this to |
| + // InstantController. |
| + const gfx::Font& omnibox_font = |
| + ui::ResourceBundle::GetSharedInstance().GetFont( |
| + ui::ResourceBundle::MediumFont); |
| + string16 omnibox_font_name = UTF8ToUTF16(omnibox_font.GetFontName()); |
| + size_t omnibox_font_size = omnibox_font.GetFontSize(); |
|
sreeram
2013/01/29 13:35:42
Note: Shishir is fixing this in https://codereview
samarth
2013/01/31 16:24:57
Noted.
|
| + |
| + Send(new ChromeViewMsg_SearchBoxFontInformation( |
| + routing_id(), omnibox_font_name, omnibox_font_size)); |
| + } else { |
| + // If the page doesn't support Instant, stop listening to it. |
| + Observe(NULL); |
| + } |
| +} |
| + |
| +void InstantPage::OnShowInstantPreview(int page_id, |
| + InstantShownReason reason, |
| + int height, |
| + InstantSizeUnits units) { |
| + if (contents_ && contents_->IsActiveEntry(page_id)) { |
| + OnInstantSupportDetermined(page_id, true); |
| + if (ShouldProcessShowInstantPreview()) |
| + delegate_->ShowInstantPreview(contents_, reason, height, units); |
| + } |
| +} |
| + |
| +void InstantPage::OnStartCapturingKeyStrokes(int page_id) { |
| + if (contents_ && contents_->IsActiveEntry(page_id)) { |
| + OnInstantSupportDetermined(page_id, true); |
| + if (ShouldProcessStartCapturingKeyStrokes()) |
| + delegate_->StartCapturingKeyStrokes(contents_); |
| + } |
| +} |
| + |
| +void InstantPage::OnStopCapturingKeyStrokes(int page_id) { |
| + if (contents_ && contents_->IsActiveEntry(page_id)) { |
| + OnInstantSupportDetermined(page_id, true); |
| + if (ShouldProcessStopCapturingKeyStrokes()) |
| + delegate_->StopCapturingKeyStrokes(contents_); |
| + } |
| +} |
| + |
| +void InstantPage::OnSearchBoxNavigate(int page_id, |
| + const GURL& url, |
| + content::PageTransition transition) { |
| + if (contents_ && contents_->IsActiveEntry(page_id)) { |
| + OnInstantSupportDetermined(page_id, true); |
| + if (ShouldProcessNavigateToURL()) |
| + delegate_->NavigateToURL(contents_, url, transition); |
| + } |
| +} |