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 is_key_capture_enabled_(false), | 22 is_key_capture_enabled_(false), |
21 theme_area_height_(0), | 23 theme_area_height_(0), |
22 display_instant_results_(false) { | 24 display_instant_results_(false) { |
23 } | 25 } |
24 | 26 |
25 SearchBox::~SearchBox() { | 27 SearchBox::~SearchBox() { |
26 } | 28 } |
27 | 29 |
28 void SearchBox::SetSuggestions( | 30 void SearchBox::SetSuggestions( |
(...skipping 20 matching lines...) Expand all Loading... |
49 void SearchBox::StartCapturingKeyStrokes() { | 51 void SearchBox::StartCapturingKeyStrokes() { |
50 render_view()->Send(new ChromeViewHostMsg_StartCapturingKeyStrokes( | 52 render_view()->Send(new ChromeViewHostMsg_StartCapturingKeyStrokes( |
51 render_view()->GetRoutingID(), render_view()->GetPageId())); | 53 render_view()->GetRoutingID(), render_view()->GetPageId())); |
52 } | 54 } |
53 | 55 |
54 void SearchBox::StopCapturingKeyStrokes() { | 56 void SearchBox::StopCapturingKeyStrokes() { |
55 render_view()->Send(new ChromeViewHostMsg_StopCapturingKeyStrokes( | 57 render_view()->Send(new ChromeViewHostMsg_StopCapturingKeyStrokes( |
56 render_view()->GetRoutingID(), render_view()->GetPageId())); | 58 render_view()->GetRoutingID(), render_view()->GetPageId())); |
57 } | 59 } |
58 | 60 |
59 gfx::Rect SearchBox::GetRect() { | 61 int SearchBox::GetStartMargin() const { |
60 // Need to adjust for scale. | 62 return static_cast<int>(start_margin_ / GetZoom()); |
61 if (rect_.IsEmpty()) | 63 } |
62 return rect_; | 64 |
63 WebKit::WebView* web_view = render_view()->GetWebView(); | 65 int SearchBox::GetEndMargin() const { |
64 if (!web_view) | 66 return static_cast<int>(end_margin_ / GetZoom()); |
65 return rect_; | 67 } |
66 double zoom = WebKit::WebView::zoomLevelToZoomFactor(web_view->zoomLevel()); | 68 |
67 if (zoom == 0) | 69 gfx::Rect SearchBox::GetPopupBounds() const { |
68 return rect_; | 70 double zoom = GetZoom(); |
69 return gfx::Rect(static_cast<int>(static_cast<float>(rect_.x()) / zoom), | 71 return gfx::Rect(static_cast<int>(popup_bounds_.x() / zoom), |
70 static_cast<int>(static_cast<float>(rect_.y()) / zoom), | 72 static_cast<int>(popup_bounds_.y() / zoom), |
71 static_cast<int>(static_cast<float>(rect_.width()) / zoom), | 73 static_cast<int>(popup_bounds_.width() / zoom), |
72 static_cast<int>(static_cast<float>(rect_.height()) / zoom)); | 74 static_cast<int>(popup_bounds_.height() / zoom)); |
73 } | 75 } |
74 | 76 |
75 const std::vector<InstantAutocompleteResult>& | 77 const std::vector<InstantAutocompleteResult>& |
76 SearchBox::GetAutocompleteResults() { | 78 SearchBox::GetAutocompleteResults() { |
77 // Remember the last requested autocomplete_results to account for race | 79 // Remember the last requested autocomplete_results to account for race |
78 // conditions between autocomplete providers returning new data and the user | 80 // conditions between autocomplete providers returning new data and the user |
79 // clicking on a suggestion. | 81 // clicking on a suggestion. |
80 last_autocomplete_results_ = autocomplete_results_; | 82 last_autocomplete_results_ = autocomplete_results_; |
81 last_results_base_ = results_base_; | 83 last_results_base_ = results_base_; |
82 return autocomplete_results_; | 84 return autocomplete_results_; |
(...skipping 14 matching lines...) Expand all Loading... |
97 int SearchBox::GetThemeAreaHeight() { | 99 int SearchBox::GetThemeAreaHeight() { |
98 return theme_area_height_; | 100 return theme_area_height_; |
99 } | 101 } |
100 | 102 |
101 bool SearchBox::OnMessageReceived(const IPC::Message& message) { | 103 bool SearchBox::OnMessageReceived(const IPC::Message& message) { |
102 bool handled = true; | 104 bool handled = true; |
103 IPC_BEGIN_MESSAGE_MAP(SearchBox, message) | 105 IPC_BEGIN_MESSAGE_MAP(SearchBox, message) |
104 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxChange, OnChange) | 106 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxChange, OnChange) |
105 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxSubmit, OnSubmit) | 107 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxSubmit, OnSubmit) |
106 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxCancel, OnCancel) | 108 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxCancel, OnCancel) |
107 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxResize, OnResize) | 109 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxPopupResize, OnPopupResize) |
| 110 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxMarginChange, OnMarginChange) |
108 IPC_MESSAGE_HANDLER(ChromeViewMsg_DetermineIfPageSupportsInstant, | 111 IPC_MESSAGE_HANDLER(ChromeViewMsg_DetermineIfPageSupportsInstant, |
109 OnDetermineIfPageSupportsInstant) | 112 OnDetermineIfPageSupportsInstant) |
110 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxAutocompleteResults, | 113 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxAutocompleteResults, |
111 OnAutocompleteResults) | 114 OnAutocompleteResults) |
112 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxUpOrDownKeyPressed, | 115 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxUpOrDownKeyPressed, |
113 OnUpOrDownKeyPressed) | 116 OnUpOrDownKeyPressed) |
114 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxModeChanged, | 117 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxModeChanged, |
115 OnModeChanged) | 118 OnModeChanged) |
116 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxSetDisplayInstantResults, | 119 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxSetDisplayInstantResults, |
117 OnSetDisplayInstantResults) | 120 OnSetDisplayInstantResults) |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 verbatim_ = true; | 165 verbatim_ = true; |
163 selection_start_ = selection_end_ = query_.size(); | 166 selection_start_ = selection_end_ = query_.size(); |
164 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | 167 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
165 DVLOG(1) << render_view() << " OnCancel"; | 168 DVLOG(1) << render_view() << " OnCancel"; |
166 extensions_v8::SearchBoxExtension::DispatchCancel( | 169 extensions_v8::SearchBoxExtension::DispatchCancel( |
167 render_view()->GetWebView()->mainFrame()); | 170 render_view()->GetWebView()->mainFrame()); |
168 } | 171 } |
169 Reset(); | 172 Reset(); |
170 } | 173 } |
171 | 174 |
172 void SearchBox::OnResize(const gfx::Rect& bounds) { | 175 void SearchBox::OnPopupResize(const gfx::Rect& bounds) { |
173 rect_ = bounds; | 176 popup_bounds_ = bounds; |
174 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | 177 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
175 DVLOG(1) << render_view() << " OnResize"; | 178 DVLOG(1) << render_view() << " OnPopupResize"; |
176 extensions_v8::SearchBoxExtension::DispatchResize( | 179 extensions_v8::SearchBoxExtension::DispatchResize( |
177 render_view()->GetWebView()->mainFrame()); | 180 render_view()->GetWebView()->mainFrame()); |
178 } | 181 } |
179 } | 182 } |
180 | 183 |
| 184 void SearchBox::OnMarginChange(int start, int end) { |
| 185 start_margin_ = start; |
| 186 end_margin_ = end; |
| 187 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
| 188 extensions_v8::SearchBoxExtension::DispatchMarginChange( |
| 189 render_view()->GetWebView()->mainFrame()); |
| 190 } |
| 191 } |
| 192 |
181 void SearchBox::OnDetermineIfPageSupportsInstant() { | 193 void SearchBox::OnDetermineIfPageSupportsInstant() { |
182 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | 194 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
183 bool result = extensions_v8::SearchBoxExtension::PageSupportsInstant( | 195 bool result = extensions_v8::SearchBoxExtension::PageSupportsInstant( |
184 render_view()->GetWebView()->mainFrame()); | 196 render_view()->GetWebView()->mainFrame()); |
185 DVLOG(1) << render_view() << " PageSupportsInstant: " << result; | 197 DVLOG(1) << render_view() << " PageSupportsInstant: " << result; |
186 render_view()->Send(new ChromeViewHostMsg_InstantSupportDetermined( | 198 render_view()->Send(new ChromeViewHostMsg_InstantSupportDetermined( |
187 render_view()->GetRoutingID(), render_view()->GetPageId(), result)); | 199 render_view()->GetRoutingID(), render_view()->GetPageId(), result)); |
188 } | 200 } |
189 } | 201 } |
190 | 202 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 } | 251 } |
240 | 252 |
241 void SearchBox::OnThemeAreaHeightChanged(int height) { | 253 void SearchBox::OnThemeAreaHeightChanged(int height) { |
242 theme_area_height_ = height; | 254 theme_area_height_ = height; |
243 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | 255 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
244 extensions_v8::SearchBoxExtension::DispatchThemeAreaHeightChange( | 256 extensions_v8::SearchBoxExtension::DispatchThemeAreaHeightChange( |
245 render_view()->GetWebView()->mainFrame()); | 257 render_view()->GetWebView()->mainFrame()); |
246 } | 258 } |
247 } | 259 } |
248 | 260 |
| 261 double SearchBox::GetZoom() const { |
| 262 WebKit::WebView* web_view = render_view()->GetWebView(); |
| 263 if (web_view) { |
| 264 double zoom = WebKit::WebView::zoomLevelToZoomFactor(web_view->zoomLevel()); |
| 265 if (zoom != 0) |
| 266 return zoom; |
| 267 } |
| 268 return 1.0; |
| 269 } |
| 270 |
249 void SearchBox::Reset() { | 271 void SearchBox::Reset() { |
250 query_.clear(); | 272 query_.clear(); |
251 verbatim_ = false; | 273 verbatim_ = false; |
252 selection_start_ = 0; | 274 selection_start_ = 0; |
253 selection_end_ = 0; | 275 selection_end_ = 0; |
254 results_base_ = 0; | 276 results_base_ = 0; |
255 rect_ = gfx::Rect(); | 277 popup_bounds_ = gfx::Rect(); |
| 278 start_margin_ = 0; |
| 279 end_margin_ = 0; |
256 autocomplete_results_.clear(); | 280 autocomplete_results_.clear(); |
257 is_key_capture_enabled_ = false; | 281 is_key_capture_enabled_ = false; |
258 mode_ = chrome::search::Mode(); | 282 mode_ = chrome::search::Mode(); |
259 theme_info_ = ThemeBackgroundInfo(); | 283 theme_info_ = ThemeBackgroundInfo(); |
260 theme_area_height_ = 0; | 284 theme_area_height_ = 0; |
261 // Don't reset display_instant_results_ to prevent clearing it on committed | 285 // Don't reset display_instant_results_ to prevent clearing it on committed |
262 // results pages in extended mode. Otherwise resetting it is a no-op because | 286 // results pages in extended mode. Otherwise resetting it is a no-op because |
263 // a new loader is created when it changes; see crbug.com/164662. | 287 // a new loader is created when it changes; see crbug.com/164662. |
264 } | 288 } |
OLD | NEW |