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.h" | 5 #include "chrome/renderer/searchbox.h" |
6 | 6 |
7 #include "base/utf_string_conversions.h" | |
sreeram
2012/08/09 22:13:51
We don't need this header, right?
Shishir
2012/08/10 18:16:57
Done.
| |
7 #include "chrome/common/render_messages.h" | 8 #include "chrome/common/render_messages.h" |
8 #include "chrome/renderer/searchbox_extension.h" | 9 #include "chrome/renderer/searchbox_extension.h" |
9 #include "content/public/renderer/render_view.h" | 10 #include "content/public/renderer/render_view.h" |
10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
11 | 12 |
12 SearchBox::SearchBox(content::RenderView* render_view) | 13 SearchBox::SearchBox(content::RenderView* render_view) |
13 : content::RenderViewObserver(render_view), | 14 : content::RenderViewObserver(render_view), |
14 content::RenderViewObserverTracker<SearchBox>(render_view), | 15 content::RenderViewObserverTracker<SearchBox>(render_view), |
15 verbatim_(false), | 16 verbatim_(false), |
16 selection_start_(0), | 17 selection_start_(0), |
17 selection_end_(0) { | 18 selection_end_(0), |
19 rid_base_(0), | |
20 key_code_(0), | |
21 is_focused_(false) { | |
18 } | 22 } |
19 | 23 |
20 SearchBox::~SearchBox() { | 24 SearchBox::~SearchBox() { |
21 } | 25 } |
22 | 26 |
23 void SearchBox::SetSuggestions(const std::vector<string16>& suggestions, | 27 void SearchBox::SetSuggestions( |
24 InstantCompleteBehavior behavior) { | 28 const std::vector<InstantSuggestion>& suggestions) { |
29 if (!suggestions.empty() && | |
30 suggestions[0].behavior == INSTANT_COMPLETE_REPLACE) { | |
31 value_ = suggestions[0].text; | |
32 verbatim_ = true; | |
33 selection_start_ = selection_end_ = value_.size(); | |
34 } | |
25 // Explicitly allow empty vector to be sent to the browser. | 35 // Explicitly allow empty vector to be sent to the browser. |
26 render_view()->Send(new ChromeViewHostMsg_SetSuggestions( | 36 render_view()->Send(new ChromeViewHostMsg_SetSuggestions( |
27 render_view()->GetRoutingID(), render_view()->GetPageId(), suggestions, | 37 render_view()->GetRoutingID(), render_view()->GetPageId(), suggestions)); |
28 behavior)); | 38 } |
39 | |
40 void SearchBox::SetInstantPreviewHeight(int height, InstantSizeUnits units) { | |
41 render_view()->Send(new ChromeViewHostMsg_SetInstantPreviewHeight( | |
42 render_view()->GetRoutingID(), render_view()->GetPageId(), height, | |
43 units)); | |
29 } | 44 } |
30 | 45 |
31 gfx::Rect SearchBox::GetRect() { | 46 gfx::Rect SearchBox::GetRect() { |
32 // Need to adjust for scale. | 47 // Need to adjust for scale. |
33 if (rect_.IsEmpty()) | 48 if (rect_.IsEmpty()) |
34 return rect_; | 49 return rect_; |
35 WebKit::WebView* web_view = render_view()->GetWebView(); | 50 WebKit::WebView* web_view = render_view()->GetWebView(); |
36 if (!web_view) | 51 if (!web_view) |
37 return rect_; | 52 return rect_; |
38 double zoom = WebKit::WebView::zoomLevelToZoomFactor(web_view->zoomLevel()); | 53 double zoom = WebKit::WebView::zoomLevelToZoomFactor(web_view->zoomLevel()); |
39 if (zoom == 0) | 54 if (zoom == 0) |
40 return rect_; | 55 return rect_; |
41 return gfx::Rect(static_cast<int>(static_cast<float>(rect_.x()) / zoom), | 56 return gfx::Rect(static_cast<int>(static_cast<float>(rect_.x()) / zoom), |
42 static_cast<int>(static_cast<float>(rect_.y()) / zoom), | 57 static_cast<int>(static_cast<float>(rect_.y()) / zoom), |
43 static_cast<int>(static_cast<float>(rect_.width()) / zoom), | 58 static_cast<int>(static_cast<float>(rect_.width()) / zoom), |
44 static_cast<int>(static_cast<float>(rect_.height()) / zoom)); | 59 static_cast<int>(static_cast<float>(rect_.height()) / zoom)); |
45 } | 60 } |
46 | 61 |
47 bool SearchBox::OnMessageReceived(const IPC::Message& message) { | 62 bool SearchBox::OnMessageReceived(const IPC::Message& message) { |
48 bool handled = true; | 63 bool handled = true; |
49 IPC_BEGIN_MESSAGE_MAP(SearchBox, message) | 64 IPC_BEGIN_MESSAGE_MAP(SearchBox, message) |
50 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxChange, OnChange) | 65 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxChange, OnChange) |
51 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxSubmit, OnSubmit) | 66 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxSubmit, OnSubmit) |
52 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxCancel, OnCancel) | 67 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxCancel, OnCancel) |
53 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxResize, OnResize) | 68 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxResize, OnResize) |
54 IPC_MESSAGE_HANDLER(ChromeViewMsg_DetermineIfPageSupportsInstant, | 69 IPC_MESSAGE_HANDLER(ChromeViewMsg_DetermineIfPageSupportsInstant, |
55 OnDetermineIfPageSupportsInstant) | 70 OnDetermineIfPageSupportsInstant) |
71 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxInstantAutocompleteResults, | |
72 OnInstantAutocompleteResults) | |
73 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxKeyPress, OnKeyPress) | |
74 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxLastQuery, OnLastQuery) | |
75 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxCurrentURL, OnCurrentURL) | |
76 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxFocused, OnFocused) | |
77 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxBlurred, OnBlurred) | |
56 IPC_MESSAGE_UNHANDLED(handled = false) | 78 IPC_MESSAGE_UNHANDLED(handled = false) |
57 IPC_END_MESSAGE_MAP() | 79 IPC_END_MESSAGE_MAP() |
58 return handled; | 80 return handled; |
59 } | 81 } |
60 | 82 |
61 void SearchBox::OnChange(const string16& value, | 83 void SearchBox::OnChange(const string16& value, |
62 bool verbatim, | 84 bool verbatim, |
63 size_t selection_start, | 85 size_t selection_start, |
64 size_t selection_end) { | 86 size_t selection_end) { |
65 value_ = value; | 87 value_ = value; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
104 | 126 |
105 void SearchBox::OnDetermineIfPageSupportsInstant() { | 127 void SearchBox::OnDetermineIfPageSupportsInstant() { |
106 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | 128 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
107 bool result = extensions_v8::SearchBoxExtension::PageSupportsInstant( | 129 bool result = extensions_v8::SearchBoxExtension::PageSupportsInstant( |
108 render_view()->GetWebView()->mainFrame()); | 130 render_view()->GetWebView()->mainFrame()); |
109 render_view()->Send(new ChromeViewHostMsg_InstantSupportDetermined( | 131 render_view()->Send(new ChromeViewHostMsg_InstantSupportDetermined( |
110 render_view()->GetRoutingID(), render_view()->GetPageId(), result)); | 132 render_view()->GetRoutingID(), render_view()->GetPageId(), result)); |
111 } | 133 } |
112 } | 134 } |
113 | 135 |
136 void SearchBox::OnInstantAutocompleteResults( | |
137 const std::vector<InstantAutocompleteResult>& results) { | |
138 rid_base_ += instant_autocomplete_results_.size(); | |
139 instant_autocomplete_results_ = results; | |
140 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | |
141 extensions_v8::SearchBoxExtension::DispatchNativeSuggestions( | |
142 render_view()->GetWebView()->mainFrame()); | |
143 } | |
144 } | |
145 | |
146 void SearchBox::OnKeyPress(int key_code) { | |
147 key_code_ = key_code; | |
148 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | |
149 extensions_v8::SearchBoxExtension::DispatchKeyPress( | |
150 render_view()->GetWebView()->mainFrame()); | |
151 } | |
152 } | |
153 | |
154 void SearchBox::OnLastQuery(const string16& last_query) { | |
155 last_query_ = last_query; | |
156 } | |
157 | |
158 void SearchBox::OnCurrentURL(const string16& current_url) { | |
159 current_url_ = current_url; | |
160 } | |
161 | |
162 void SearchBox::OnFocused() { | |
163 is_focused_ = true; | |
164 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | |
165 extensions_v8::SearchBoxExtension::DispatchFocus( | |
166 render_view()->GetWebView()->mainFrame()); | |
167 } | |
168 } | |
169 | |
170 void SearchBox::OnBlurred() { | |
171 is_focused_ = false; | |
172 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | |
173 extensions_v8::SearchBoxExtension::DispatchBlur( | |
174 render_view()->GetWebView()->mainFrame()); | |
175 } | |
176 } | |
177 | |
178 | |
114 void SearchBox::Reset() { | 179 void SearchBox::Reset() { |
115 value_.clear(); | 180 value_.clear(); |
116 verbatim_ = false; | 181 verbatim_ = false; |
117 selection_start_ = selection_end_ = 0; | 182 selection_start_ = selection_end_ = 0; |
183 rid_base_ = 0; | |
118 rect_ = gfx::Rect(); | 184 rect_ = gfx::Rect(); |
185 instant_autocomplete_results_.clear(); | |
186 key_code_ = 0; | |
187 last_query_.clear(); | |
188 current_url_.clear(); | |
189 is_focused_ = false; | |
119 } | 190 } |
OLD | NEW |