Chromium Code Reviews| Index: chrome/browser/instant/instant_api_wrapper.cc |
| diff --git a/chrome/browser/instant/instant_api_wrapper.cc b/chrome/browser/instant/instant_api_wrapper.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..376cc02c2439ceaacbe3fb4288b86a5ca20d5767 |
| --- /dev/null |
| +++ b/chrome/browser/instant/instant_api_wrapper.cc |
| @@ -0,0 +1,209 @@ |
| +// Copyright 2012 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_api_wrapper.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" |
| + |
| +InstantAPIWrapper::Controller::~Controller() { |
| +} |
| + |
| +InstantAPIWrapper::Receiver::~Receiver() { |
| +} |
| + |
| +InstantAPIWrapper::InstantAPIWrapper(Controller* controller, |
| + Receiver* receiver) |
| + : controller_(controller), |
| + receiver_(receiver) { |
| +} |
| + |
| +InstantAPIWrapper::~InstantAPIWrapper() { |
| +} |
| + |
| +void InstantAPIWrapper::SetContents(content::WebContents* contents) { |
| + Observe(contents); |
| +} |
| + |
| +void InstantAPIWrapper::Update(const string16& text, |
| + size_t selection_start, |
| + size_t selection_end, |
| + bool verbatim) { |
| + controller_->OnUpdate(); |
| + Send(new ChromeViewMsg_SearchBoxChange(routing_id(), text, verbatim, |
| + selection_start, selection_end)); |
| +} |
| + |
| +void InstantAPIWrapper::Submit(const string16& text) { |
| + Send(new ChromeViewMsg_SearchBoxSubmit(routing_id(), text)); |
| +} |
| + |
| +void InstantAPIWrapper::Cancel(const string16& text) { |
| + Send(new ChromeViewMsg_SearchBoxCancel(routing_id(), text)); |
| +} |
| + |
| +void InstantAPIWrapper::SetPopupBounds(const gfx::Rect& bounds) { |
| + Send(new ChromeViewMsg_SearchBoxPopupResize(routing_id(), bounds)); |
| +} |
| + |
| +void InstantAPIWrapper::SetMarginSize(const int start, const int end) { |
| + Send(new ChromeViewMsg_SearchBoxMarginChange(routing_id(), start, end)); |
| +} |
| + |
| +void InstantAPIWrapper::DetermineIfPageSupportsInstant() { |
| + Send(new ChromeViewMsg_DetermineIfPageSupportsInstant(routing_id())); |
| +} |
| + |
| +void InstantAPIWrapper::SendAutocompleteResults( |
| + const std::vector<InstantAutocompleteResult>& results) { |
| + Send(new ChromeViewMsg_SearchBoxAutocompleteResults(routing_id(), results)); |
| +} |
| + |
| +void InstantAPIWrapper::UpOrDownKeyPressed(int count) { |
| + Send(new ChromeViewMsg_SearchBoxUpOrDownKeyPressed(routing_id(), count)); |
| +} |
| + |
| +void InstantAPIWrapper::SearchModeChanged(const chrome::search::Mode& mode) { |
| + Send(new ChromeViewMsg_SearchBoxModeChanged(routing_id(), mode)); |
| +} |
| + |
| +void InstantAPIWrapper::SendThemeBackgroundInfo( |
| + const ThemeBackgroundInfo& theme_info) { |
| + Send(new ChromeViewMsg_SearchBoxThemeChanged(routing_id(), theme_info)); |
| +} |
| + |
| +void InstantAPIWrapper::SendThemeAreaHeight(int height) { |
| + Send(new ChromeViewMsg_SearchBoxThemeAreaHeightChanged(routing_id(), height)); |
| +} |
| + |
| +void InstantAPIWrapper::SetDisplayInstantResults(bool display_instant_results) { |
| + Send(new ChromeViewMsg_SearchBoxSetDisplayInstantResults( |
| + routing_id(), display_instant_results)); |
| +} |
| + |
| +void InstantAPIWrapper::KeyCaptureChanged(bool is_key_capture_enabled) { |
| + Send(new ChromeViewMsg_SearchBoxKeyCaptureChanged( |
| + routing_id(), is_key_capture_enabled)); |
| +} |
| + |
| +void InstantAPIWrapper::DidFinishLoad( |
| + int64 /* frame_id */, |
| + const GURL& /* validated_url */, |
| + bool is_main_frame, |
| + content::RenderViewHost* /* render_view_host */) { |
| + if (is_main_frame) |
| + DetermineIfPageSupportsInstant(); |
| +} |
| + |
| +bool InstantAPIWrapper::OnMessageReceived(const IPC::Message& message) { |
| + bool handled = true; |
| + IPC_BEGIN_MESSAGE_MAP(InstantAPIWrapper, message) |
| + IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SetSuggestions, SetSuggestions) |
|
dhollowa
2013/01/14 19:59:39
We should change these handler names to OnSetSugge
samarth
2013/01/22 15:59:06
Done.
|
| + IPC_MESSAGE_HANDLER(ChromeViewHostMsg_InstantSupportDetermined, |
| + InstantSupportDetermined) |
| + IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ShowInstantPreview, |
| + ShowInstantPreview) |
| + IPC_MESSAGE_HANDLER(ChromeViewHostMsg_StartCapturingKeyStrokes, |
| + StartCapturingKeyStrokes); |
| + IPC_MESSAGE_HANDLER(ChromeViewHostMsg_StopCapturingKeyStrokes, |
| + StopCapturingKeyStrokes); |
| + IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxNavigate, |
| + SearchBoxNavigate); |
| + IPC_MESSAGE_UNHANDLED(handled = false) |
| + IPC_END_MESSAGE_MAP() |
| + return handled; |
| +} |
| + |
| +void InstantAPIWrapper::RenderViewGone(base::TerminationStatus status) { |
| + if (controller_->OnRenderViewGone()) |
| + receiver_->InstantPageRenderViewGone(web_contents()); |
| +} |
| + |
| +void InstantAPIWrapper::DidCommitProvisionalLoadForFrame( |
| + int64 frame_id, |
| + bool is_main_frame, |
| + const GURL& url, |
| + content::PageTransition transition_type, |
| + content::RenderViewHost* render_view_host) { |
| + if (is_main_frame && controller_->OnAboutToNavigateMainFrame()) |
| + receiver_->InstantPageAboutToNavigateMainFrame(web_contents(), url); |
| +} |
| + |
| +void InstantAPIWrapper::SetSuggestions( |
| + int page_id, |
| + const std::vector<InstantSuggestion>& suggestions) { |
| + if (web_contents()->IsActiveEntry(page_id)) { |
| + InstantSupportDetermined(page_id, true); |
| + if (controller_->OnSetSuggestions()) |
| + receiver_->SetSuggestions(web_contents(), suggestions); |
| + } |
| +} |
| + |
| +void InstantAPIWrapper::InstantSupportDetermined(int page_id, bool result) { |
| + if (!web_contents()->IsActiveEntry(page_id) || |
| + controller_->supports_instant()) { |
| + // Nothing to do if the page already supports instant. |
| + return; |
| + } |
| + |
| + controller_->set_supports_instant(result); |
| + receiver_->InstantSupportDetermined(web_contents(), result); |
| + |
| + if (result) { |
|
Jered
2013/01/14 18:22:34
result -> supports_instant
samarth
2013/01/22 15:59:06
Done.
|
| + // 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(); |
| + |
| + Send(new ChromeViewMsg_SearchBoxFontInformation( |
| + routing_id(), omnibox_font_name, omnibox_font_size)); |
| + } else { |
| + // If the page doesn't support Instant, stop communicating with it. |
| + SetContents(NULL); |
| + } |
| +} |
| + |
| +void InstantAPIWrapper::ShowInstantPreview(int page_id, |
| + InstantShownReason reason, |
| + int height, |
| + InstantSizeUnits units) { |
| + if (web_contents()->IsActiveEntry(page_id)) { |
| + InstantSupportDetermined(page_id, true); |
| + if (controller_->OnShowInstantPreview()) |
| + receiver_->ShowInstantPreview(web_contents(), reason, height, units); |
| + } |
| +} |
| + |
| +void InstantAPIWrapper::StartCapturingKeyStrokes(int page_id) { |
| + if (web_contents()->IsActiveEntry(page_id)) { |
| + InstantSupportDetermined(page_id, true); |
| + if (controller_->OnStartCapturingKeyStrokes()) |
| + receiver_->StartCapturingKeyStrokes(web_contents()); |
| + } |
| +} |
| + |
| +void InstantAPIWrapper::StopCapturingKeyStrokes(int page_id) { |
| + if (web_contents()->IsActiveEntry(page_id)) { |
| + InstantSupportDetermined(page_id, true); |
| + if (controller_->OnStopCapturingKeyStrokes()) |
| + receiver_->StopCapturingKeyStrokes(web_contents()); |
| + } |
| +} |
| + |
| +void InstantAPIWrapper::SearchBoxNavigate(int page_id, |
| + const GURL& url, |
| + content::PageTransition transition) { |
| + if (web_contents()->IsActiveEntry(page_id)) { |
| + InstantSupportDetermined(page_id, true); |
| + if (controller_->OnNavigateToURL()) |
| + receiver_->NavigateToURL(web_contents(), url, transition); |
| + } |
| +} |