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" |
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 } | 23 } |
22 | 24 |
23 SearchBox::~SearchBox() { | 25 SearchBox::~SearchBox() { |
24 } | 26 } |
25 | 27 |
26 void SearchBox::SetSuggestions( | 28 void SearchBox::SetSuggestions( |
27 const std::vector<InstantSuggestion>& suggestions) { | 29 const std::vector<InstantSuggestion>& suggestions) { |
28 if (!suggestions.empty() && | 30 if (!suggestions.empty() && |
29 suggestions[0].behavior == INSTANT_COMPLETE_REPLACE) { | 31 suggestions[0].behavior == INSTANT_COMPLETE_REPLACE) { |
30 query_ = suggestions[0].text; | 32 query_ = suggestions[0].text; |
31 verbatim_ = true; | 33 verbatim_ = true; |
32 selection_start_ = selection_end_ = query_.size(); | 34 selection_start_ = selection_end_ = query_.size(); |
33 } | 35 } |
34 // Explicitly allow empty vector to be sent to the browser. | 36 // Explicitly allow empty vector to be sent to the browser. |
35 render_view()->Send(new ChromeViewHostMsg_SetSuggestions( | 37 render_view()->Send(new ChromeViewHostMsg_SetSuggestions( |
36 render_view()->GetRoutingID(), render_view()->GetPageId(), suggestions)); | 38 render_view()->GetRoutingID(), render_view()->GetPageId(), suggestions)); |
37 } | 39 } |
38 | 40 |
39 void SearchBox::ShowInstantPreview(InstantShownReason reason, | 41 void SearchBox::ShowInstantPreview(InstantShownReason reason, |
40 int height, | 42 int height, |
41 InstantSizeUnits units) { | 43 InstantSizeUnits units) { |
42 render_view()->Send(new ChromeViewHostMsg_ShowInstantPreview( | 44 render_view()->Send(new ChromeViewHostMsg_ShowInstantPreview( |
43 render_view()->GetRoutingID(), render_view()->GetPageId(), reason, | 45 render_view()->GetRoutingID(), render_view()->GetPageId(), reason, |
44 height, units)); | 46 height, units)); |
45 } | 47 } |
46 | 48 |
47 gfx::Rect SearchBox::GetRect() { | 49 int SearchBox::GetStartMargin() { |
48 // Need to adjust for scale. | 50 return static_cast<int>(start_margin_ / GetZoom()); |
49 if (rect_.IsEmpty()) | 51 } |
50 return rect_; | 52 |
51 WebKit::WebView* web_view = render_view()->GetWebView(); | 53 int SearchBox::GetEndMargin() { |
52 if (!web_view) | 54 return static_cast<int>(end_margin_ / GetZoom()); |
53 return rect_; | 55 } |
54 double zoom = WebKit::WebView::zoomLevelToZoomFactor(web_view->zoomLevel()); | 56 |
55 if (zoom == 0) | 57 gfx::Rect SearchBox::GetPopupBounds() { |
56 return rect_; | 58 double zoom = GetZoom(); |
dhollowa
2012/11/28 16:44:08
IMO |GetZoom| should return float. Double is exce
melevin
2012/11/28 20:28:19
That's true but we would have to convert it to a g
dhollowa
2012/11/28 20:44:33
Ya, ok.
| |
57 return gfx::Rect(static_cast<int>(static_cast<float>(rect_.x()) / zoom), | 59 return gfx::Rect(static_cast<int>(popup_bounds_.x() / zoom), |
58 static_cast<int>(static_cast<float>(rect_.y()) / zoom), | 60 static_cast<int>(popup_bounds_.y() / zoom), |
59 static_cast<int>(static_cast<float>(rect_.width()) / zoom), | 61 static_cast<int>(popup_bounds_.width() / zoom), |
60 static_cast<int>(static_cast<float>(rect_.height()) / zoom)); | 62 static_cast<int>(popup_bounds_.height() / zoom)); |
61 } | 63 } |
62 | 64 |
63 const std::vector<InstantAutocompleteResult>& | 65 const std::vector<InstantAutocompleteResult>& |
64 SearchBox::GetAutocompleteResults() { | 66 SearchBox::GetAutocompleteResults() { |
65 // Remember the last requested autocomplete_results to account for race | 67 // Remember the last requested autocomplete_results to account for race |
66 // conditions between autocomplete providers returning new data and the user | 68 // conditions between autocomplete providers returning new data and the user |
67 // clicking on a suggestion. | 69 // clicking on a suggestion. |
68 last_autocomplete_results_ = autocomplete_results_; | 70 last_autocomplete_results_ = autocomplete_results_; |
69 last_results_base_ = results_base_; | 71 last_results_base_ = results_base_; |
70 return autocomplete_results_; | 72 return autocomplete_results_; |
(...skipping 14 matching lines...) Expand all Loading... | |
85 int SearchBox::GetThemeAreaHeight() { | 87 int SearchBox::GetThemeAreaHeight() { |
86 return theme_area_height_; | 88 return theme_area_height_; |
87 } | 89 } |
88 | 90 |
89 bool SearchBox::OnMessageReceived(const IPC::Message& message) { | 91 bool SearchBox::OnMessageReceived(const IPC::Message& message) { |
90 bool handled = true; | 92 bool handled = true; |
91 IPC_BEGIN_MESSAGE_MAP(SearchBox, message) | 93 IPC_BEGIN_MESSAGE_MAP(SearchBox, message) |
92 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxChange, OnChange) | 94 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxChange, OnChange) |
93 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxSubmit, OnSubmit) | 95 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxSubmit, OnSubmit) |
94 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxCancel, OnCancel) | 96 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxCancel, OnCancel) |
95 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxResize, OnResize) | 97 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxPopupResize, OnPopupResize) |
98 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxMarginChange, OnMarginChange) | |
96 IPC_MESSAGE_HANDLER(ChromeViewMsg_DetermineIfPageSupportsInstant, | 99 IPC_MESSAGE_HANDLER(ChromeViewMsg_DetermineIfPageSupportsInstant, |
97 OnDetermineIfPageSupportsInstant) | 100 OnDetermineIfPageSupportsInstant) |
98 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxAutocompleteResults, | 101 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxAutocompleteResults, |
99 OnAutocompleteResults) | 102 OnAutocompleteResults) |
100 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxUpOrDownKeyPressed, | 103 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxUpOrDownKeyPressed, |
101 OnUpOrDownKeyPressed) | 104 OnUpOrDownKeyPressed) |
102 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxModeChanged, | 105 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxModeChanged, |
103 OnModeChanged) | 106 OnModeChanged) |
104 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxThemeChanged, | 107 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxThemeChanged, |
105 OnThemeChanged) | 108 OnThemeChanged) |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
139 query_ = query; | 142 query_ = query; |
140 verbatim_ = true; | 143 verbatim_ = true; |
141 selection_start_ = selection_end_ = query_.size(); | 144 selection_start_ = selection_end_ = query_.size(); |
142 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | 145 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
143 extensions_v8::SearchBoxExtension::DispatchCancel( | 146 extensions_v8::SearchBoxExtension::DispatchCancel( |
144 render_view()->GetWebView()->mainFrame()); | 147 render_view()->GetWebView()->mainFrame()); |
145 } | 148 } |
146 Reset(); | 149 Reset(); |
147 } | 150 } |
148 | 151 |
149 void SearchBox::OnResize(const gfx::Rect& bounds) { | 152 void SearchBox::OnPopupResize(const gfx::Rect& bounds) { |
150 rect_ = bounds; | 153 popup_bounds_ = bounds; |
151 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | 154 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
152 extensions_v8::SearchBoxExtension::DispatchResize( | 155 extensions_v8::SearchBoxExtension::DispatchResize( |
153 render_view()->GetWebView()->mainFrame()); | 156 render_view()->GetWebView()->mainFrame()); |
154 } | 157 } |
155 } | 158 } |
156 | 159 |
160 void SearchBox::OnMarginChange(int start, int end) { | |
161 start_margin_ = start; | |
162 end_margin_ = end; | |
163 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | |
164 extensions_v8::SearchBoxExtension::DispatchMarginChange( | |
165 render_view()->GetWebView()->mainFrame()); | |
166 } | |
167 } | |
168 | |
157 void SearchBox::OnDetermineIfPageSupportsInstant() { | 169 void SearchBox::OnDetermineIfPageSupportsInstant() { |
158 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | 170 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
159 bool result = extensions_v8::SearchBoxExtension::PageSupportsInstant( | 171 bool result = extensions_v8::SearchBoxExtension::PageSupportsInstant( |
160 render_view()->GetWebView()->mainFrame()); | 172 render_view()->GetWebView()->mainFrame()); |
161 render_view()->Send(new ChromeViewHostMsg_InstantSupportDetermined( | 173 render_view()->Send(new ChromeViewHostMsg_InstantSupportDetermined( |
162 render_view()->GetRoutingID(), render_view()->GetPageId(), result)); | 174 render_view()->GetRoutingID(), render_view()->GetPageId(), result)); |
163 } | 175 } |
164 } | 176 } |
165 | 177 |
166 void SearchBox::OnAutocompleteResults( | 178 void SearchBox::OnAutocompleteResults( |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
203 render_view()->GetWebView()->mainFrame()); | 215 render_view()->GetWebView()->mainFrame()); |
204 } | 216 } |
205 } | 217 } |
206 | 218 |
207 void SearchBox::Reset() { | 219 void SearchBox::Reset() { |
208 query_.clear(); | 220 query_.clear(); |
209 verbatim_ = false; | 221 verbatim_ = false; |
210 selection_start_ = 0; | 222 selection_start_ = 0; |
211 selection_end_ = 0; | 223 selection_end_ = 0; |
212 results_base_ = 0; | 224 results_base_ = 0; |
213 rect_ = gfx::Rect(); | 225 popup_bounds_ = gfx::Rect(); |
226 start_margin_ = 0; | |
227 end_margin_ = 0; | |
214 autocomplete_results_.clear(); | 228 autocomplete_results_.clear(); |
215 mode_ = chrome::search::Mode(); | 229 mode_ = chrome::search::Mode(); |
216 theme_info_ = ThemeBackgroundInfo(); | 230 theme_info_ = ThemeBackgroundInfo(); |
217 theme_area_height_ = 0; | 231 theme_area_height_ = 0; |
218 } | 232 } |
233 | |
234 double SearchBox::GetZoom() { | |
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 } | |
OLD | NEW |