Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/instant/instant_api_wrapper.h" | |
| 6 | |
| 7 #include "base/utf_string_conversions.h" | |
| 8 #include "chrome/common/render_messages.h" | |
| 9 #include "content/public/browser/web_contents.h" | |
| 10 #include "ui/base/resource/resource_bundle.h" | |
| 11 #include "ui/gfx/font.h" | |
| 12 | |
| 13 InstantAPIWrapper::Controller::~Controller() { | |
| 14 } | |
| 15 | |
| 16 InstantAPIWrapper::Receiver::~Receiver() { | |
| 17 } | |
| 18 | |
| 19 InstantAPIWrapper::InstantAPIWrapper(Controller* controller, | |
| 20 Receiver* receiver) | |
| 21 : controller_(controller), | |
| 22 receiver_(receiver) { | |
| 23 } | |
| 24 | |
| 25 InstantAPIWrapper::~InstantAPIWrapper() { | |
| 26 } | |
| 27 | |
| 28 void InstantAPIWrapper::SetContents(content::WebContents* contents) { | |
| 29 Observe(contents); | |
| 30 } | |
| 31 | |
| 32 void InstantAPIWrapper::Update(const string16& text, | |
| 33 size_t selection_start, | |
| 34 size_t selection_end, | |
| 35 bool verbatim) { | |
| 36 controller_->OnUpdate(); | |
| 37 Send(new ChromeViewMsg_SearchBoxChange(routing_id(), text, verbatim, | |
| 38 selection_start, selection_end)); | |
| 39 } | |
| 40 | |
| 41 void InstantAPIWrapper::Submit(const string16& text) { | |
| 42 Send(new ChromeViewMsg_SearchBoxSubmit(routing_id(), text)); | |
| 43 } | |
| 44 | |
| 45 void InstantAPIWrapper::Cancel(const string16& text) { | |
| 46 Send(new ChromeViewMsg_SearchBoxCancel(routing_id(), text)); | |
| 47 } | |
| 48 | |
| 49 void InstantAPIWrapper::SetPopupBounds(const gfx::Rect& bounds) { | |
| 50 Send(new ChromeViewMsg_SearchBoxPopupResize(routing_id(), bounds)); | |
| 51 } | |
| 52 | |
| 53 void InstantAPIWrapper::SetMarginSize(const int start, const int end) { | |
| 54 Send(new ChromeViewMsg_SearchBoxMarginChange(routing_id(), start, end)); | |
| 55 } | |
| 56 | |
| 57 void InstantAPIWrapper::DetermineIfPageSupportsInstant() { | |
| 58 Send(new ChromeViewMsg_DetermineIfPageSupportsInstant(routing_id())); | |
| 59 } | |
| 60 | |
| 61 void InstantAPIWrapper::SendAutocompleteResults( | |
| 62 const std::vector<InstantAutocompleteResult>& results) { | |
| 63 Send(new ChromeViewMsg_SearchBoxAutocompleteResults(routing_id(), results)); | |
| 64 } | |
| 65 | |
| 66 void InstantAPIWrapper::UpOrDownKeyPressed(int count) { | |
| 67 Send(new ChromeViewMsg_SearchBoxUpOrDownKeyPressed(routing_id(), count)); | |
| 68 } | |
| 69 | |
| 70 void InstantAPIWrapper::SearchModeChanged(const chrome::search::Mode& mode) { | |
| 71 Send(new ChromeViewMsg_SearchBoxModeChanged(routing_id(), mode)); | |
| 72 } | |
| 73 | |
| 74 void InstantAPIWrapper::SendThemeBackgroundInfo( | |
| 75 const ThemeBackgroundInfo& theme_info) { | |
| 76 Send(new ChromeViewMsg_SearchBoxThemeChanged(routing_id(), theme_info)); | |
| 77 } | |
| 78 | |
| 79 void InstantAPIWrapper::SendThemeAreaHeight(int height) { | |
| 80 Send(new ChromeViewMsg_SearchBoxThemeAreaHeightChanged(routing_id(), height)); | |
| 81 } | |
| 82 | |
| 83 void InstantAPIWrapper::SetDisplayInstantResults(bool display_instant_results) { | |
| 84 Send(new ChromeViewMsg_SearchBoxSetDisplayInstantResults( | |
| 85 routing_id(), display_instant_results)); | |
| 86 } | |
| 87 | |
| 88 void InstantAPIWrapper::KeyCaptureChanged(bool is_key_capture_enabled) { | |
| 89 Send(new ChromeViewMsg_SearchBoxKeyCaptureChanged( | |
| 90 routing_id(), is_key_capture_enabled)); | |
| 91 } | |
| 92 | |
| 93 void InstantAPIWrapper::DidFinishLoad( | |
| 94 int64 /* frame_id */, | |
| 95 const GURL& /* validated_url */, | |
| 96 bool is_main_frame, | |
| 97 content::RenderViewHost* /* render_view_host */) { | |
| 98 if (is_main_frame) | |
| 99 DetermineIfPageSupportsInstant(); | |
| 100 } | |
| 101 | |
| 102 bool InstantAPIWrapper::OnMessageReceived(const IPC::Message& message) { | |
| 103 bool handled = true; | |
| 104 IPC_BEGIN_MESSAGE_MAP(InstantAPIWrapper, message) | |
| 105 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.
| |
| 106 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_InstantSupportDetermined, | |
| 107 InstantSupportDetermined) | |
| 108 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ShowInstantPreview, | |
| 109 ShowInstantPreview) | |
| 110 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_StartCapturingKeyStrokes, | |
| 111 StartCapturingKeyStrokes); | |
| 112 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_StopCapturingKeyStrokes, | |
| 113 StopCapturingKeyStrokes); | |
| 114 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxNavigate, | |
| 115 SearchBoxNavigate); | |
| 116 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 117 IPC_END_MESSAGE_MAP() | |
| 118 return handled; | |
| 119 } | |
| 120 | |
| 121 void InstantAPIWrapper::RenderViewGone(base::TerminationStatus status) { | |
| 122 if (controller_->OnRenderViewGone()) | |
| 123 receiver_->InstantPageRenderViewGone(web_contents()); | |
| 124 } | |
| 125 | |
| 126 void InstantAPIWrapper::DidCommitProvisionalLoadForFrame( | |
| 127 int64 frame_id, | |
| 128 bool is_main_frame, | |
| 129 const GURL& url, | |
| 130 content::PageTransition transition_type, | |
| 131 content::RenderViewHost* render_view_host) { | |
| 132 if (is_main_frame && controller_->OnAboutToNavigateMainFrame()) | |
| 133 receiver_->InstantPageAboutToNavigateMainFrame(web_contents(), url); | |
| 134 } | |
| 135 | |
| 136 void InstantAPIWrapper::SetSuggestions( | |
| 137 int page_id, | |
| 138 const std::vector<InstantSuggestion>& suggestions) { | |
| 139 if (web_contents()->IsActiveEntry(page_id)) { | |
| 140 InstantSupportDetermined(page_id, true); | |
| 141 if (controller_->OnSetSuggestions()) | |
| 142 receiver_->SetSuggestions(web_contents(), suggestions); | |
| 143 } | |
| 144 } | |
| 145 | |
| 146 void InstantAPIWrapper::InstantSupportDetermined(int page_id, bool result) { | |
| 147 if (!web_contents()->IsActiveEntry(page_id) || | |
| 148 controller_->supports_instant()) { | |
| 149 // Nothing to do if the page already supports instant. | |
| 150 return; | |
| 151 } | |
| 152 | |
| 153 controller_->set_supports_instant(result); | |
| 154 receiver_->InstantSupportDetermined(web_contents(), result); | |
| 155 | |
| 156 if (result) { | |
|
Jered
2013/01/14 18:22:34
result -> supports_instant
samarth
2013/01/22 15:59:06
Done.
| |
| 157 // Inform the renderer process of the Omnibox's font information. | |
| 158 // TODO(samarth): this logic doesn't really belong here. Move this to | |
| 159 // InstantController. | |
| 160 const gfx::Font& omnibox_font = | |
| 161 ui::ResourceBundle::GetSharedInstance().GetFont( | |
| 162 ui::ResourceBundle::MediumFont); | |
| 163 string16 omnibox_font_name = UTF8ToUTF16(omnibox_font.GetFontName()); | |
| 164 size_t omnibox_font_size = omnibox_font.GetFontSize(); | |
| 165 | |
| 166 Send(new ChromeViewMsg_SearchBoxFontInformation( | |
| 167 routing_id(), omnibox_font_name, omnibox_font_size)); | |
| 168 } else { | |
| 169 // If the page doesn't support Instant, stop communicating with it. | |
| 170 SetContents(NULL); | |
| 171 } | |
| 172 } | |
| 173 | |
| 174 void InstantAPIWrapper::ShowInstantPreview(int page_id, | |
| 175 InstantShownReason reason, | |
| 176 int height, | |
| 177 InstantSizeUnits units) { | |
| 178 if (web_contents()->IsActiveEntry(page_id)) { | |
| 179 InstantSupportDetermined(page_id, true); | |
| 180 if (controller_->OnShowInstantPreview()) | |
| 181 receiver_->ShowInstantPreview(web_contents(), reason, height, units); | |
| 182 } | |
| 183 } | |
| 184 | |
| 185 void InstantAPIWrapper::StartCapturingKeyStrokes(int page_id) { | |
| 186 if (web_contents()->IsActiveEntry(page_id)) { | |
| 187 InstantSupportDetermined(page_id, true); | |
| 188 if (controller_->OnStartCapturingKeyStrokes()) | |
| 189 receiver_->StartCapturingKeyStrokes(web_contents()); | |
| 190 } | |
| 191 } | |
| 192 | |
| 193 void InstantAPIWrapper::StopCapturingKeyStrokes(int page_id) { | |
| 194 if (web_contents()->IsActiveEntry(page_id)) { | |
| 195 InstantSupportDetermined(page_id, true); | |
| 196 if (controller_->OnStopCapturingKeyStrokes()) | |
| 197 receiver_->StopCapturingKeyStrokes(web_contents()); | |
| 198 } | |
| 199 } | |
| 200 | |
| 201 void InstantAPIWrapper::SearchBoxNavigate(int page_id, | |
| 202 const GURL& url, | |
| 203 content::PageTransition transition) { | |
| 204 if (web_contents()->IsActiveEntry(page_id)) { | |
| 205 InstantSupportDetermined(page_id, true); | |
| 206 if (controller_->OnNavigateToURL()) | |
| 207 receiver_->NavigateToURL(web_contents(), url, transition); | |
| 208 } | |
| 209 } | |
| OLD | NEW |