Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 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 | 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/renderer/searchbox/searchbox.h" | 5 #include "chrome/renderer/searchbox/searchbox.h" |
| 6 | 6 |
| 7 #include "chrome/common/render_messages.h" | 7 #include "chrome/common/render_messages.h" |
| 8 #include "chrome/renderer/searchbox/searchbox_extension.h" | 8 #include "chrome/renderer/searchbox/searchbox_extension.h" |
| 9 #include "content/public/renderer/render_view.h" | 9 #include "content/public/renderer/render_view.h" |
| 10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
| 11 | 11 |
| 12 SearchBox::SearchBox(content::RenderView* render_view) | 12 SearchBox::SearchBox(content::RenderView* render_view) |
| 13 : content::RenderViewObserver(render_view), | 13 : content::RenderViewObserver(render_view), |
| 14 content::RenderViewObserverTracker<SearchBox>(render_view), | 14 content::RenderViewObserverTracker<SearchBox>(render_view), |
| 15 verbatim_(false), | 15 verbatim_(false), |
| 16 selection_start_(0), | 16 selection_start_(0), |
| 17 selection_end_(0), | 17 selection_end_(0), |
| 18 results_base_(0), | 18 results_base_(0), |
| 19 start_margin_(0), | |
| 20 end_margin_(0), | |
| 19 last_results_base_(0), | 21 last_results_base_(0), |
| 20 theme_area_height_(0), | 22 theme_area_height_(0), |
| 21 display_instant_results_(false) { | 23 display_instant_results_(false) { |
| 22 } | 24 } |
| 23 | 25 |
| 24 SearchBox::~SearchBox() { | 26 SearchBox::~SearchBox() { |
| 25 } | 27 } |
| 26 | 28 |
| 27 void SearchBox::SetSuggestions( | 29 void SearchBox::SetSuggestions( |
| 28 const std::vector<InstantSuggestion>& suggestions) { | 30 const std::vector<InstantSuggestion>& suggestions) { |
| 29 if (!suggestions.empty() && | 31 if (!suggestions.empty() && |
| 30 suggestions[0].behavior == INSTANT_COMPLETE_REPLACE) { | 32 suggestions[0].behavior == INSTANT_COMPLETE_REPLACE) { |
| 31 query_ = suggestions[0].text; | 33 query_ = suggestions[0].text; |
| 32 verbatim_ = true; | 34 verbatim_ = true; |
| 33 selection_start_ = selection_end_ = query_.size(); | 35 selection_start_ = selection_end_ = query_.size(); |
| 34 } | 36 } |
| 35 // Explicitly allow empty vector to be sent to the browser. | 37 // Explicitly allow empty vector to be sent to the browser. |
| 36 render_view()->Send(new ChromeViewHostMsg_SetSuggestions( | 38 render_view()->Send(new ChromeViewHostMsg_SetSuggestions( |
| 37 render_view()->GetRoutingID(), render_view()->GetPageId(), suggestions)); | 39 render_view()->GetRoutingID(), render_view()->GetPageId(), suggestions)); |
| 38 } | 40 } |
| 39 | 41 |
| 40 void SearchBox::ShowInstantPreview(InstantShownReason reason, | 42 void SearchBox::ShowInstantPreview(InstantShownReason reason, |
| 41 int height, | 43 int height, |
| 42 InstantSizeUnits units) { | 44 InstantSizeUnits units) { |
| 43 render_view()->Send(new ChromeViewHostMsg_ShowInstantPreview( | 45 render_view()->Send(new ChromeViewHostMsg_ShowInstantPreview( |
| 44 render_view()->GetRoutingID(), render_view()->GetPageId(), reason, | 46 render_view()->GetRoutingID(), render_view()->GetPageId(), reason, |
| 45 height, units)); | 47 height, units)); |
| 46 } | 48 } |
| 47 | 49 |
| 48 gfx::Rect SearchBox::GetRect() { | 50 int SearchBox::GetStartMargin() const { |
| 49 // Need to adjust for scale. | 51 return static_cast<int>(start_margin_ / GetZoom()); |
| 50 if (rect_.IsEmpty()) | 52 } |
| 51 return rect_; | 53 |
| 52 WebKit::WebView* web_view = render_view()->GetWebView(); | 54 int SearchBox::GetEndMargin() const { |
| 53 if (!web_view) | 55 return static_cast<int>(end_margin_ / GetZoom()); |
| 54 return rect_; | 56 } |
| 55 double zoom = WebKit::WebView::zoomLevelToZoomFactor(web_view->zoomLevel()); | 57 |
| 56 if (zoom == 0) | 58 gfx::Rect SearchBox::GetPopupBounds() const { |
| 57 return rect_; | 59 double zoom = GetZoom(); |
| 58 return gfx::Rect(static_cast<int>(static_cast<float>(rect_.x()) / zoom), | 60 return gfx::Rect(static_cast<int>(popup_bounds_.x() / zoom), |
| 59 static_cast<int>(static_cast<float>(rect_.y()) / zoom), | 61 static_cast<int>(popup_bounds_.y() / zoom), |
| 60 static_cast<int>(static_cast<float>(rect_.width()) / zoom), | 62 static_cast<int>(popup_bounds_.width() / zoom), |
| 61 static_cast<int>(static_cast<float>(rect_.height()) / zoom)); | 63 static_cast<int>(popup_bounds_.height() / zoom)); |
| 62 } | 64 } |
| 63 | 65 |
| 64 const std::vector<InstantAutocompleteResult>& | 66 const std::vector<InstantAutocompleteResult>& |
| 65 SearchBox::GetAutocompleteResults() { | 67 SearchBox::GetAutocompleteResults() { |
| 66 // Remember the last requested autocomplete_results to account for race | 68 // Remember the last requested autocomplete_results to account for race |
| 67 // conditions between autocomplete providers returning new data and the user | 69 // conditions between autocomplete providers returning new data and the user |
| 68 // clicking on a suggestion. | 70 // clicking on a suggestion. |
| 69 last_autocomplete_results_ = autocomplete_results_; | 71 last_autocomplete_results_ = autocomplete_results_; |
| 70 last_results_base_ = results_base_; | 72 last_results_base_ = results_base_; |
| 71 return autocomplete_results_; | 73 return autocomplete_results_; |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 86 int SearchBox::GetThemeAreaHeight() { | 88 int SearchBox::GetThemeAreaHeight() { |
| 87 return theme_area_height_; | 89 return theme_area_height_; |
| 88 } | 90 } |
| 89 | 91 |
| 90 bool SearchBox::OnMessageReceived(const IPC::Message& message) { | 92 bool SearchBox::OnMessageReceived(const IPC::Message& message) { |
| 91 bool handled = true; | 93 bool handled = true; |
| 92 IPC_BEGIN_MESSAGE_MAP(SearchBox, message) | 94 IPC_BEGIN_MESSAGE_MAP(SearchBox, message) |
| 93 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxChange, OnChange) | 95 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxChange, OnChange) |
| 94 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxSubmit, OnSubmit) | 96 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxSubmit, OnSubmit) |
| 95 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxCancel, OnCancel) | 97 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxCancel, OnCancel) |
| 96 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxResize, OnResize) | 98 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxPopupResize, OnPopupResize) |
| 99 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxMarginChange, OnMarginChange) | |
| 97 IPC_MESSAGE_HANDLER(ChromeViewMsg_DetermineIfPageSupportsInstant, | 100 IPC_MESSAGE_HANDLER(ChromeViewMsg_DetermineIfPageSupportsInstant, |
| 98 OnDetermineIfPageSupportsInstant) | 101 OnDetermineIfPageSupportsInstant) |
| 99 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxAutocompleteResults, | 102 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxAutocompleteResults, |
| 100 OnAutocompleteResults) | 103 OnAutocompleteResults) |
| 101 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxUpOrDownKeyPressed, | 104 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxUpOrDownKeyPressed, |
| 102 OnUpOrDownKeyPressed) | 105 OnUpOrDownKeyPressed) |
| 103 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxModeChanged, | 106 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxModeChanged, |
| 104 OnModeChanged) | 107 OnModeChanged) |
| 105 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxSetDisplayInstantResults, | 108 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxSetDisplayInstantResults, |
| 106 OnSetDisplayInstantResults) | 109 OnSetDisplayInstantResults) |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 145 verbatim_ = true; | 148 verbatim_ = true; |
| 146 selection_start_ = selection_end_ = query_.size(); | 149 selection_start_ = selection_end_ = query_.size(); |
| 147 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | 150 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
| 148 DVLOG(1) << render_view() << " OnCancel"; | 151 DVLOG(1) << render_view() << " OnCancel"; |
| 149 extensions_v8::SearchBoxExtension::DispatchCancel( | 152 extensions_v8::SearchBoxExtension::DispatchCancel( |
| 150 render_view()->GetWebView()->mainFrame()); | 153 render_view()->GetWebView()->mainFrame()); |
| 151 } | 154 } |
| 152 Reset(); | 155 Reset(); |
| 153 } | 156 } |
| 154 | 157 |
| 155 void SearchBox::OnResize(const gfx::Rect& bounds) { | 158 void SearchBox::OnPopupResize(const gfx::Rect& bounds) { |
| 156 rect_ = bounds; | 159 popup_bounds_ = bounds; |
| 157 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | 160 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
| 158 DVLOG(1) << render_view() << " OnResize"; | 161 DVLOG(1) << render_view() << " OnResize"; |
|
sreeram
2012/12/05 00:14:46
Nit: OnResize -> OnPopupResize
melevin
2012/12/06 23:13:00
Done.
| |
| 159 extensions_v8::SearchBoxExtension::DispatchResize( | 162 extensions_v8::SearchBoxExtension::DispatchResize( |
| 160 render_view()->GetWebView()->mainFrame()); | 163 render_view()->GetWebView()->mainFrame()); |
| 161 } | 164 } |
| 162 } | 165 } |
| 163 | 166 |
| 167 void SearchBox::OnMarginChange(int start, int end) { | |
| 168 start_margin_ = start; | |
| 169 end_margin_ = end; | |
| 170 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | |
| 171 extensions_v8::SearchBoxExtension::DispatchMarginChange( | |
| 172 render_view()->GetWebView()->mainFrame()); | |
| 173 } | |
| 174 } | |
| 175 | |
| 164 void SearchBox::OnDetermineIfPageSupportsInstant() { | 176 void SearchBox::OnDetermineIfPageSupportsInstant() { |
| 165 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | 177 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
| 166 bool result = extensions_v8::SearchBoxExtension::PageSupportsInstant( | 178 bool result = extensions_v8::SearchBoxExtension::PageSupportsInstant( |
| 167 render_view()->GetWebView()->mainFrame()); | 179 render_view()->GetWebView()->mainFrame()); |
| 168 DVLOG(1) << render_view() << " PageSupportsInstant: " << result; | 180 DVLOG(1) << render_view() << " PageSupportsInstant: " << result; |
| 169 render_view()->Send(new ChromeViewHostMsg_InstantSupportDetermined( | 181 render_view()->Send(new ChromeViewHostMsg_InstantSupportDetermined( |
| 170 render_view()->GetRoutingID(), render_view()->GetPageId(), result)); | 182 render_view()->GetRoutingID(), render_view()->GetPageId(), result)); |
| 171 } | 183 } |
| 172 } | 184 } |
| 173 | 185 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 212 } | 224 } |
| 213 | 225 |
| 214 void SearchBox::OnThemeAreaHeightChanged(int height) { | 226 void SearchBox::OnThemeAreaHeightChanged(int height) { |
| 215 theme_area_height_ = height; | 227 theme_area_height_ = height; |
| 216 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | 228 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
| 217 extensions_v8::SearchBoxExtension::DispatchThemeAreaHeightChange( | 229 extensions_v8::SearchBoxExtension::DispatchThemeAreaHeightChange( |
| 218 render_view()->GetWebView()->mainFrame()); | 230 render_view()->GetWebView()->mainFrame()); |
| 219 } | 231 } |
| 220 } | 232 } |
| 221 | 233 |
| 234 double SearchBox::GetZoom() const { | |
| 235 WebKit::WebView* web_view = render_view()->GetWebView(); | |
| 236 if (web_view) { | |
| 237 double zoom = WebKit::WebView::zoomLevelToZoomFactor(web_view->zoomLevel()); | |
| 238 if (zoom != 0) | |
| 239 return zoom; | |
| 240 } | |
| 241 return 1.0; | |
| 242 } | |
| 243 | |
| 222 void SearchBox::Reset() { | 244 void SearchBox::Reset() { |
| 223 query_.clear(); | 245 query_.clear(); |
| 224 verbatim_ = false; | 246 verbatim_ = false; |
| 225 selection_start_ = 0; | 247 selection_start_ = 0; |
| 226 selection_end_ = 0; | 248 selection_end_ = 0; |
| 227 results_base_ = 0; | 249 results_base_ = 0; |
| 228 rect_ = gfx::Rect(); | 250 popup_bounds_ = gfx::Rect(); |
| 251 start_margin_ = 0; | |
| 252 end_margin_ = 0; | |
| 229 autocomplete_results_.clear(); | 253 autocomplete_results_.clear(); |
| 230 mode_ = chrome::search::Mode(); | 254 mode_ = chrome::search::Mode(); |
| 231 theme_info_ = ThemeBackgroundInfo(); | 255 theme_info_ = ThemeBackgroundInfo(); |
| 232 theme_area_height_ = 0; | 256 theme_area_height_ = 0; |
| 233 display_instant_results_ = false; | 257 display_instant_results_ = false; |
| 234 } | 258 } |
| OLD | NEW |