| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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" |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 } | 38 } |
| 39 | 39 |
| 40 void SearchBox::ShowInstantPreview(InstantShownReason reason, | 40 void SearchBox::ShowInstantPreview(InstantShownReason reason, |
| 41 int height, | 41 int height, |
| 42 InstantSizeUnits units) { | 42 InstantSizeUnits units) { |
| 43 render_view()->Send(new ChromeViewHostMsg_ShowInstantPreview( | 43 render_view()->Send(new ChromeViewHostMsg_ShowInstantPreview( |
| 44 render_view()->GetRoutingID(), render_view()->GetPageId(), reason, | 44 render_view()->GetRoutingID(), render_view()->GetPageId(), reason, |
| 45 height, units)); | 45 height, units)); |
| 46 } | 46 } |
| 47 | 47 |
| 48 gfx::Rect SearchBox::GetRect() { | 48 gfx::Rect SearchBox::GetOmniboxBounds() { |
| 49 // Need to adjust for scale. | 49 return GetZoomedBounds(omnibox_bounds_); |
| 50 if (rect_.IsEmpty()) | 50 } |
| 51 return rect_; | 51 |
| 52 WebKit::WebView* web_view = render_view()->GetWebView(); | 52 gfx::Rect SearchBox::GetPopupBounds() { |
| 53 if (!web_view) | 53 return GetZoomedBounds(popup_bounds_); |
| 54 return rect_; | |
| 55 double zoom = WebKit::WebView::zoomLevelToZoomFactor(web_view->zoomLevel()); | |
| 56 if (zoom == 0) | |
| 57 return rect_; | |
| 58 return gfx::Rect(static_cast<int>(static_cast<float>(rect_.x()) / zoom), | |
| 59 static_cast<int>(static_cast<float>(rect_.y()) / zoom), | |
| 60 static_cast<int>(static_cast<float>(rect_.width()) / zoom), | |
| 61 static_cast<int>(static_cast<float>(rect_.height()) / zoom)); | |
| 62 } | 54 } |
| 63 | 55 |
| 64 const std::vector<InstantAutocompleteResult>& | 56 const std::vector<InstantAutocompleteResult>& |
| 65 SearchBox::GetAutocompleteResults() { | 57 SearchBox::GetAutocompleteResults() { |
| 66 // Remember the last requested autocomplete_results to account for race | 58 // Remember the last requested autocomplete_results to account for race |
| 67 // conditions between autocomplete providers returning new data and the user | 59 // conditions between autocomplete providers returning new data and the user |
| 68 // clicking on a suggestion. | 60 // clicking on a suggestion. |
| 69 last_autocomplete_results_ = autocomplete_results_; | 61 last_autocomplete_results_ = autocomplete_results_; |
| 70 last_results_base_ = results_base_; | 62 last_results_base_ = results_base_; |
| 71 return autocomplete_results_; | 63 return autocomplete_results_; |
| 72 } | 64 } |
| 73 | 65 |
| 74 const InstantAutocompleteResult* SearchBox::GetAutocompleteResultWithId( | 66 const InstantAutocompleteResult* SearchBox::GetAutocompleteResultWithId( |
| 75 size_t restricted_id) const { | 67 size_t restricted_id) const { |
| 76 if (restricted_id < last_results_base_ || | 68 if (restricted_id < last_results_base_ || |
| 77 restricted_id >= last_results_base_ + last_autocomplete_results_.size()) | 69 restricted_id >= last_results_base_ + last_autocomplete_results_.size()) |
| 78 return NULL; | 70 return NULL; |
| 79 return &last_autocomplete_results_[restricted_id - last_results_base_]; | 71 return &last_autocomplete_results_[restricted_id - last_results_base_]; |
| 80 } | 72 } |
| 81 | 73 |
| 82 bool SearchBox::OnMessageReceived(const IPC::Message& message) { | 74 bool SearchBox::OnMessageReceived(const IPC::Message& message) { |
| 83 bool handled = true; | 75 bool handled = true; |
| 84 IPC_BEGIN_MESSAGE_MAP(SearchBox, message) | 76 IPC_BEGIN_MESSAGE_MAP(SearchBox, message) |
| 85 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxChange, OnChange) | 77 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxChange, OnChange) |
| 86 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxSubmit, OnSubmit) | 78 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxSubmit, OnSubmit) |
| 87 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxCancel, OnCancel) | 79 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxCancel, OnCancel) |
| 88 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxResize, OnResize) | 80 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxPopupResize, OnPopupResize) |
| 81 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxOmniboxResize, OnOmniboxResize) |
| 89 IPC_MESSAGE_HANDLER(ChromeViewMsg_DetermineIfPageSupportsInstant, | 82 IPC_MESSAGE_HANDLER(ChromeViewMsg_DetermineIfPageSupportsInstant, |
| 90 OnDetermineIfPageSupportsInstant) | 83 OnDetermineIfPageSupportsInstant) |
| 91 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxAutocompleteResults, | 84 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxAutocompleteResults, |
| 92 OnAutocompleteResults) | 85 OnAutocompleteResults) |
| 93 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxUpOrDownKeyPressed, | 86 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxUpOrDownKeyPressed, |
| 94 OnUpOrDownKeyPressed) | 87 OnUpOrDownKeyPressed) |
| 95 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxFocus, OnFocus) | 88 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxFocus, OnFocus) |
| 96 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxBlur, OnBlur) | 89 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxBlur, OnBlur) |
| 97 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxActiveTabModeChanged, | 90 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxActiveTabModeChanged, |
| 98 OnActiveTabModeChanged) | 91 OnActiveTabModeChanged) |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 query_ = query; | 123 query_ = query; |
| 131 verbatim_ = true; | 124 verbatim_ = true; |
| 132 selection_start_ = selection_end_ = query_.size(); | 125 selection_start_ = selection_end_ = query_.size(); |
| 133 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | 126 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
| 134 extensions_v8::SearchBoxExtension::DispatchCancel( | 127 extensions_v8::SearchBoxExtension::DispatchCancel( |
| 135 render_view()->GetWebView()->mainFrame()); | 128 render_view()->GetWebView()->mainFrame()); |
| 136 } | 129 } |
| 137 Reset(); | 130 Reset(); |
| 138 } | 131 } |
| 139 | 132 |
| 140 void SearchBox::OnResize(const gfx::Rect& bounds) { | 133 void SearchBox::OnPopupResize(const gfx::Rect& bounds) { |
| 141 rect_ = bounds; | 134 popup_bounds_ = bounds; |
| 142 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | 135 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
| 143 extensions_v8::SearchBoxExtension::DispatchResize( | 136 extensions_v8::SearchBoxExtension::DispatchResize( |
| 144 render_view()->GetWebView()->mainFrame()); | 137 render_view()->GetWebView()->mainFrame()); |
| 145 } | 138 } |
| 146 } | 139 } |
| 147 | 140 |
| 141 void SearchBox::OnOmniboxResize(const gfx::Rect& bounds) { |
| 142 omnibox_bounds_ = bounds; |
| 143 |
| 144 // The omnibox bounds are used to compute the Instant page side margins. |
| 145 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
| 146 extensions_v8::SearchBoxExtension::DispatchMarginChange( |
| 147 render_view()->GetWebView()->mainFrame()); |
| 148 } |
| 149 } |
| 150 |
| 148 void SearchBox::OnDetermineIfPageSupportsInstant() { | 151 void SearchBox::OnDetermineIfPageSupportsInstant() { |
| 149 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | 152 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
| 150 bool result = extensions_v8::SearchBoxExtension::PageSupportsInstant( | 153 bool result = extensions_v8::SearchBoxExtension::PageSupportsInstant( |
| 151 render_view()->GetWebView()->mainFrame()); | 154 render_view()->GetWebView()->mainFrame()); |
| 152 render_view()->Send(new ChromeViewHostMsg_InstantSupportDetermined( | 155 render_view()->Send(new ChromeViewHostMsg_InstantSupportDetermined( |
| 153 render_view()->GetRoutingID(), render_view()->GetPageId(), result)); | 156 render_view()->GetRoutingID(), render_view()->GetPageId(), result)); |
| 154 } | 157 } |
| 155 } | 158 } |
| 156 | 159 |
| 157 void SearchBox::OnAutocompleteResults( | 160 void SearchBox::OnAutocompleteResults( |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 extensions_v8::SearchBoxExtension::DispatchContextChange( | 196 extensions_v8::SearchBoxExtension::DispatchContextChange( |
| 194 render_view()->GetWebView()->mainFrame()); | 197 render_view()->GetWebView()->mainFrame()); |
| 195 } | 198 } |
| 196 } | 199 } |
| 197 | 200 |
| 198 void SearchBox::Reset() { | 201 void SearchBox::Reset() { |
| 199 query_.clear(); | 202 query_.clear(); |
| 200 verbatim_ = false; | 203 verbatim_ = false; |
| 201 selection_start_ = selection_end_ = 0; | 204 selection_start_ = selection_end_ = 0; |
| 202 results_base_ = 0; | 205 results_base_ = 0; |
| 203 rect_ = gfx::Rect(); | 206 popup_bounds_ = gfx::Rect(); |
| 207 omnibox_bounds_ = gfx::Rect(); |
| 204 autocomplete_results_.clear(); | 208 autocomplete_results_.clear(); |
| 205 is_focused_ = false; | 209 is_focused_ = false; |
| 206 } | 210 } |
| 211 |
| 212 // Returns the input |bounds| rect scaled by the current zoom level. |
| 213 gfx::Rect SearchBox::GetZoomedBounds(const gfx::Rect bounds) { |
| 214 if (!bounds.IsEmpty()) { |
| 215 WebKit::WebView* web_view = render_view()->GetWebView(); |
| 216 if (web_view) { |
| 217 double zoom = |
| 218 WebKit::WebView::zoomLevelToZoomFactor(web_view->zoomLevel()); |
| 219 if (zoom != 0) { |
| 220 return gfx::Rect(static_cast<int>(bounds.x() / zoom), |
| 221 static_cast<int>(bounds.y() / zoom), |
| 222 static_cast<int>(bounds.width() / zoom), |
| 223 static_cast<int>(bounds.height() / zoom)); |
| 224 } |
| 225 } |
| 226 } |
| 227 return bounds; |
| 228 } |
| OLD | NEW |