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" |
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 using WebKit::WebView; | 13 using WebKit::WebView; |
13 | 14 |
14 SearchBox::SearchBox(content::RenderView* render_view) | 15 SearchBox::SearchBox(content::RenderView* render_view) |
15 : content::RenderViewObserver(render_view), | 16 : content::RenderViewObserver(render_view), |
16 content::RenderViewObserverTracker<SearchBox>(render_view), | 17 content::RenderViewObserverTracker<SearchBox>(render_view), |
17 verbatim_(false), | 18 verbatim_(false), |
18 selection_start_(0), | 19 selection_start_(0), |
19 selection_end_(0) { | 20 selection_end_(0), |
| 21 rid_base_(0), |
| 22 key_code_(0), |
| 23 is_focused_(false) { |
20 } | 24 } |
21 | 25 |
22 SearchBox::~SearchBox() { | 26 SearchBox::~SearchBox() { |
23 } | 27 } |
24 | 28 |
25 void SearchBox::SetSuggestions(const std::vector<std::string>& suggestions, | 29 void SearchBox::SetSuggestions( |
26 InstantCompleteBehavior behavior) { | 30 const std::vector<InstantSuggestion>& suggestions) { |
| 31 if (!suggestions.empty() && |
| 32 suggestions[0].behavior == INSTANT_COMPLETE_REPLACE) { |
| 33 value_ = UTF8ToUTF16(suggestions[0].text); |
| 34 verbatim_ = true; |
| 35 selection_start_ = selection_end_ = value_.size(); |
| 36 } |
27 // Explicitly allow empty vector to be sent to the browser. | 37 // Explicitly allow empty vector to be sent to the browser. |
28 render_view()->Send(new ChromeViewHostMsg_SetSuggestions( | 38 render_view()->Send(new ChromeViewHostMsg_SetSuggestions( |
29 render_view()->GetRoutingID(), render_view()->GetPageId(), suggestions, | 39 render_view()->GetRoutingID(), render_view()->GetPageId(), suggestions)); |
30 behavior)); | 40 } |
| 41 |
| 42 void SearchBox::SetInstantPreviewHeight(int height, InstantSizeUnits units) { |
| 43 render_view()->Send(new ChromeViewHostMsg_SetInstantPreviewHeight( |
| 44 render_view()->GetRoutingID(), render_view()->GetPageId(), height, |
| 45 units)); |
31 } | 46 } |
32 | 47 |
33 gfx::Rect SearchBox::GetRect() { | 48 gfx::Rect SearchBox::GetRect() { |
34 // Need to adjust for scale. | 49 // Need to adjust for scale. |
35 if (rect_.IsEmpty()) | 50 if (rect_.IsEmpty()) |
36 return rect_; | 51 return rect_; |
37 WebView* web_view = render_view()->GetWebView(); | 52 WebView* web_view = render_view()->GetWebView(); |
38 if (!web_view) | 53 if (!web_view) |
39 return rect_; | 54 return rect_; |
40 double zoom = WebView::zoomLevelToZoomFactor(web_view->zoomLevel()); | 55 double zoom = WebView::zoomLevelToZoomFactor(web_view->zoomLevel()); |
41 if (zoom == 0) | 56 if (zoom == 0) |
42 return rect_; | 57 return rect_; |
43 return gfx::Rect(static_cast<int>(static_cast<float>(rect_.x()) / zoom), | 58 return gfx::Rect(static_cast<int>(static_cast<float>(rect_.x()) / zoom), |
44 static_cast<int>(static_cast<float>(rect_.y()) / zoom), | 59 static_cast<int>(static_cast<float>(rect_.y()) / zoom), |
45 static_cast<int>(static_cast<float>(rect_.width()) / zoom), | 60 static_cast<int>(static_cast<float>(rect_.width()) / zoom), |
46 static_cast<int>(static_cast<float>(rect_.height()) / zoom)); | 61 static_cast<int>(static_cast<float>(rect_.height()) / zoom)); |
47 } | 62 } |
48 | 63 |
49 bool SearchBox::OnMessageReceived(const IPC::Message& message) { | 64 bool SearchBox::OnMessageReceived(const IPC::Message& message) { |
50 bool handled = true; | 65 bool handled = true; |
51 IPC_BEGIN_MESSAGE_MAP(SearchBox, message) | 66 IPC_BEGIN_MESSAGE_MAP(SearchBox, message) |
52 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxChange, OnChange) | 67 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxChange, OnChange) |
53 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxSubmit, OnSubmit) | 68 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxSubmit, OnSubmit) |
54 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxCancel, OnCancel) | 69 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxCancel, OnCancel) |
55 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxResize, OnResize) | 70 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxResize, OnResize) |
56 IPC_MESSAGE_HANDLER(ChromeViewMsg_DetermineIfPageSupportsInstant, | 71 IPC_MESSAGE_HANDLER(ChromeViewMsg_DetermineIfPageSupportsInstant, |
57 OnDetermineIfPageSupportsInstant) | 72 OnDetermineIfPageSupportsInstant) |
| 73 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxNativeSuggestions, |
| 74 OnNativeSuggestions) |
| 75 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxKeyPress, OnKeyPress) |
| 76 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxLastQuery, OnLastQuery) |
| 77 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxCurrentURL, OnCurrentURL) |
| 78 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxFocused, OnFocused) |
| 79 IPC_MESSAGE_HANDLER(ChromeViewMsg_SearchBoxBlurred, OnBlurred) |
58 IPC_MESSAGE_UNHANDLED(handled = false) | 80 IPC_MESSAGE_UNHANDLED(handled = false) |
59 IPC_END_MESSAGE_MAP() | 81 IPC_END_MESSAGE_MAP() |
60 return handled; | 82 return handled; |
61 } | 83 } |
62 | 84 |
63 void SearchBox::OnChange(const string16& value, | 85 void SearchBox::OnChange(const string16& value, |
64 bool verbatim, | 86 bool verbatim, |
65 int selection_start, | 87 int selection_start, |
66 int selection_end) { | 88 int selection_end) { |
67 value_ = value; | 89 value_ = value; |
68 verbatim_ = verbatim; | 90 verbatim_ = verbatim; |
69 selection_start_ = selection_start; | 91 selection_start_ = selection_start; |
70 selection_end_ = selection_end; | 92 selection_end_ = selection_end; |
71 if (!render_view()->GetWebView() || !render_view()->GetWebView()->mainFrame()) | 93 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
72 return; | 94 extensions_v8::SearchBoxExtension::DispatchChange( |
73 extensions_v8::SearchBoxExtension::DispatchChange( | 95 render_view()->GetWebView()->mainFrame()); |
74 render_view()->GetWebView()->mainFrame()); | 96 } |
75 } | 97 } |
76 | 98 |
77 void SearchBox::OnSubmit(const string16& value, bool verbatim) { | 99 void SearchBox::OnSubmit(const string16& value, bool verbatim) { |
78 value_ = value; | 100 value_ = value; |
79 verbatim_ = verbatim; | 101 verbatim_ = verbatim; |
80 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | 102 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
81 extensions_v8::SearchBoxExtension::DispatchSubmit( | 103 extensions_v8::SearchBoxExtension::DispatchSubmit( |
82 render_view()->GetWebView()->mainFrame()); | 104 render_view()->GetWebView()->mainFrame()); |
83 } | 105 } |
84 Reset(); | 106 Reset(); |
85 } | 107 } |
86 | 108 |
87 void SearchBox::OnCancel() { | 109 void SearchBox::OnCancel() { |
88 verbatim_ = false; | 110 verbatim_ = false; |
89 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { | 111 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
90 extensions_v8::SearchBoxExtension::DispatchCancel( | 112 extensions_v8::SearchBoxExtension::DispatchCancel( |
91 render_view()->GetWebView()->mainFrame()); | 113 render_view()->GetWebView()->mainFrame()); |
92 } | 114 } |
93 Reset(); | 115 Reset(); |
94 } | 116 } |
95 | 117 |
96 void SearchBox::OnResize(const gfx::Rect& bounds) { | 118 void SearchBox::OnResize(const gfx::Rect& bounds) { |
97 rect_ = bounds; | 119 rect_ = bounds; |
98 if (!render_view()->GetWebView() || !render_view()->GetWebView()->mainFrame()) | 120 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
99 return; | 121 extensions_v8::SearchBoxExtension::DispatchResize( |
100 extensions_v8::SearchBoxExtension::DispatchResize( | 122 render_view()->GetWebView()->mainFrame()); |
101 render_view()->GetWebView()->mainFrame()); | 123 } |
102 } | 124 } |
103 | 125 |
104 void SearchBox::OnDetermineIfPageSupportsInstant(const string16& value, | 126 void SearchBox::OnDetermineIfPageSupportsInstant(const string16& value, |
105 bool verbatim, | 127 bool verbatim, |
106 int selection_start, | 128 int selection_start, |
107 int selection_end) { | 129 int selection_end) { |
108 value_ = value; | 130 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
109 verbatim_ = verbatim; | 131 bool result = extensions_v8::SearchBoxExtension::PageSupportsInstant( |
110 selection_start_ = selection_start; | 132 render_view()->GetWebView()->mainFrame()); |
111 selection_end_ = selection_end; | 133 render_view()->Send(new ChromeViewHostMsg_InstantSupportDetermined( |
112 bool result = extensions_v8::SearchBoxExtension::PageSupportsInstant( | 134 render_view()->GetRoutingID(), render_view()->GetPageId(), result)); |
113 render_view()->GetWebView()->mainFrame()); | 135 } |
114 render_view()->Send(new ChromeViewHostMsg_InstantSupportDetermined( | 136 } |
115 render_view()->GetRoutingID(), render_view()->GetPageId(), result)); | 137 |
| 138 void SearchBox::OnNativeSuggestions( |
| 139 const std::vector<InstantNativeSuggestionsParts>& native_suggestions) { |
| 140 rid_base_ += native_suggestions_.size(); |
| 141 native_suggestions_ = native_suggestions; |
| 142 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
| 143 extensions_v8::SearchBoxExtension::DispatchNativeSuggestions( |
| 144 render_view()->GetWebView()->mainFrame()); |
| 145 } |
| 146 } |
| 147 |
| 148 void SearchBox::OnKeyPress(int key_code) { |
| 149 key_code_ = key_code; |
| 150 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
| 151 extensions_v8::SearchBoxExtension::DispatchKeyPress( |
| 152 render_view()->GetWebView()->mainFrame()); |
| 153 } |
| 154 } |
| 155 |
| 156 void SearchBox::OnLastQuery(const std::string& last_query) { |
| 157 last_query_ = last_query; |
| 158 } |
| 159 |
| 160 void SearchBox::OnCurrentURL(const std::string& current_url) { |
| 161 current_url_ = current_url; |
| 162 } |
| 163 |
| 164 void SearchBox::OnFocused() { |
| 165 is_focused_ = true; |
| 166 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
| 167 extensions_v8::SearchBoxExtension::DispatchFocus( |
| 168 render_view()->GetWebView()->mainFrame()); |
| 169 } |
| 170 } |
| 171 |
| 172 void SearchBox::OnBlurred() { |
| 173 is_focused_ = false; |
| 174 if (render_view()->GetWebView() && render_view()->GetWebView()->mainFrame()) { |
| 175 extensions_v8::SearchBoxExtension::DispatchBlur( |
| 176 render_view()->GetWebView()->mainFrame()); |
| 177 } |
116 } | 178 } |
117 | 179 |
118 void SearchBox::Reset() { | 180 void SearchBox::Reset() { |
| 181 value_.clear(); |
119 verbatim_ = false; | 182 verbatim_ = false; |
120 value_ = string16(); | |
121 selection_start_ = selection_end_ = 0; | 183 selection_start_ = selection_end_ = 0; |
| 184 rid_base_ = 0; |
122 rect_ = gfx::Rect(); | 185 rect_ = gfx::Rect(); |
| 186 native_suggestions_.clear(); |
| 187 key_code_ = 0; |
| 188 last_query_.clear(); |
| 189 current_url_.clear(); |
| 190 is_focused_ = false; |
123 } | 191 } |
OLD | NEW |