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_extension.h" | 5 #include "chrome/renderer/searchbox/searchbox_extension.h" |
6 | 6 |
7 #include "base/string_number_conversions.h" | 7 #include "base/string_number_conversions.h" |
8 #include "base/stringprintf.h" | 8 #include "base/stringprintf.h" |
9 #include "chrome/common/extensions/extension.h" | 9 #include "chrome/common/extensions/extension.h" |
10 #include "chrome/renderer/searchbox/searchbox.h" | 10 #include "chrome/renderer/searchbox/searchbox.h" |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 static const char kDispatchUpOrDownKeyPressEventScript[] = | 124 static const char kDispatchUpOrDownKeyPressEventScript[] = |
125 "if (window.chrome &&" | 125 "if (window.chrome &&" |
126 " window.chrome.searchBox &&" | 126 " window.chrome.searchBox &&" |
127 " window.chrome.searchBox.onkeypress &&" | 127 " window.chrome.searchBox.onkeypress &&" |
128 " typeof window.chrome.searchBox.onkeypress == 'function') {" | 128 " typeof window.chrome.searchBox.onkeypress == 'function') {" |
129 " for (var i = 0; i < %d; ++i)" | 129 " for (var i = 0; i < %d; ++i)" |
130 " window.chrome.searchBox.onkeypress({keyCode: %d});" | 130 " window.chrome.searchBox.onkeypress({keyCode: %d});" |
131 " true;" | 131 " true;" |
132 "}"; | 132 "}"; |
133 | 133 |
| 134 static const char kDispatchKeyCaptureChangeScript[] = |
| 135 "if (window.chrome &&" |
| 136 " window.chrome.searchBox &&" |
| 137 " window.chrome.searchBox.onkeycapturechange &&" |
| 138 " typeof window.chrome.searchBox.onkeycapturechange == 'function') {" |
| 139 " window.chrome.searchBox.onkeycapturechange();" |
| 140 " true;" |
| 141 "}"; |
| 142 |
134 static const char kDispatchContextChangeEventScript[] = | 143 static const char kDispatchContextChangeEventScript[] = |
135 "if (window.chrome &&" | 144 "if (window.chrome &&" |
136 " window.chrome.searchBox &&" | 145 " window.chrome.searchBox &&" |
137 " window.chrome.searchBox.oncontextchange &&" | 146 " window.chrome.searchBox.oncontextchange &&" |
138 " typeof window.chrome.searchBox.oncontextchange == 'function') {" | 147 " typeof window.chrome.searchBox.oncontextchange == 'function') {" |
139 " window.chrome.searchBox.oncontextchange();" | 148 " window.chrome.searchBox.oncontextchange();" |
140 " true;" | 149 " true;" |
141 "}"; | 150 "}"; |
142 | 151 |
143 static const char kDispatchThemeChangeEventScript[] = | 152 static const char kDispatchThemeChangeEventScript[] = |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 static v8::Handle<v8::Value> GetThemeBackgroundInfo( | 222 static v8::Handle<v8::Value> GetThemeBackgroundInfo( |
214 const v8::Arguments& args); | 223 const v8::Arguments& args); |
215 | 224 |
216 // Gets the theme area height that the entire theme background image should | 225 // Gets the theme area height that the entire theme background image should |
217 // fill up. | 226 // fill up. |
218 // Call only when overlay is showing NTP page and GetThemeBackgroundInfo | 227 // Call only when overlay is showing NTP page and GetThemeBackgroundInfo |
219 // returns a non-empty image_url and an image_vertical_alignment that is not | 228 // returns a non-empty image_url and an image_vertical_alignment that is not |
220 // "top". | 229 // "top". |
221 static v8::Handle<v8::Value> GetThemeAreaHeight(const v8::Arguments& args); | 230 static v8::Handle<v8::Value> GetThemeAreaHeight(const v8::Arguments& args); |
222 | 231 |
| 232 // Gets whether the browser is capturing key strokes. |
| 233 static v8::Handle<v8::Value> IsKeyCaptureEnabled(const v8::Arguments& args); |
| 234 |
223 // Navigates the window to a URL represented by either a URL string or a | 235 // Navigates the window to a URL represented by either a URL string or a |
224 // restricted ID. | 236 // restricted ID. |
225 static v8::Handle<v8::Value> NavigateContentWindow(const v8::Arguments& args); | 237 static v8::Handle<v8::Value> NavigateContentWindow(const v8::Arguments& args); |
226 | 238 |
227 // Sets ordered suggestions. Valid for current |value|. | 239 // Sets ordered suggestions. Valid for current |value|. |
228 static v8::Handle<v8::Value> SetSuggestions(const v8::Arguments& args); | 240 static v8::Handle<v8::Value> SetSuggestions(const v8::Arguments& args); |
229 | 241 |
230 // Sets the text to be autocompleted into the search box. | 242 // Sets the text to be autocompleted into the search box. |
231 static v8::Handle<v8::Value> SetQuerySuggestion(const v8::Arguments& args); | 243 static v8::Handle<v8::Value> SetQuerySuggestion(const v8::Arguments& args); |
232 | 244 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 if (name->Equals(v8::String::New("GetHeight"))) | 292 if (name->Equals(v8::String::New("GetHeight"))) |
281 return v8::FunctionTemplate::New(GetHeight); | 293 return v8::FunctionTemplate::New(GetHeight); |
282 if (name->Equals(v8::String::New("GetAutocompleteResults"))) | 294 if (name->Equals(v8::String::New("GetAutocompleteResults"))) |
283 return v8::FunctionTemplate::New(GetAutocompleteResults); | 295 return v8::FunctionTemplate::New(GetAutocompleteResults); |
284 if (name->Equals(v8::String::New("GetContext"))) | 296 if (name->Equals(v8::String::New("GetContext"))) |
285 return v8::FunctionTemplate::New(GetContext); | 297 return v8::FunctionTemplate::New(GetContext); |
286 if (name->Equals(v8::String::New("GetThemeBackgroundInfo"))) | 298 if (name->Equals(v8::String::New("GetThemeBackgroundInfo"))) |
287 return v8::FunctionTemplate::New(GetThemeBackgroundInfo); | 299 return v8::FunctionTemplate::New(GetThemeBackgroundInfo); |
288 if (name->Equals(v8::String::New("GetThemeAreaHeight"))) | 300 if (name->Equals(v8::String::New("GetThemeAreaHeight"))) |
289 return v8::FunctionTemplate::New(GetThemeAreaHeight); | 301 return v8::FunctionTemplate::New(GetThemeAreaHeight); |
| 302 if (name->Equals(v8::String::New("IsKeyCaptureEnabled"))) |
| 303 return v8::FunctionTemplate::New(IsKeyCaptureEnabled); |
290 if (name->Equals(v8::String::New("NavigateContentWindow"))) | 304 if (name->Equals(v8::String::New("NavigateContentWindow"))) |
291 return v8::FunctionTemplate::New(NavigateContentWindow); | 305 return v8::FunctionTemplate::New(NavigateContentWindow); |
292 if (name->Equals(v8::String::New("SetSuggestions"))) | 306 if (name->Equals(v8::String::New("SetSuggestions"))) |
293 return v8::FunctionTemplate::New(SetSuggestions); | 307 return v8::FunctionTemplate::New(SetSuggestions); |
294 if (name->Equals(v8::String::New("SetQuerySuggestion"))) | 308 if (name->Equals(v8::String::New("SetQuerySuggestion"))) |
295 return v8::FunctionTemplate::New(SetQuerySuggestion); | 309 return v8::FunctionTemplate::New(SetQuerySuggestion); |
296 if (name->Equals(v8::String::New("SetQuerySuggestionFromAutocompleteResult"))) | 310 if (name->Equals(v8::String::New("SetQuerySuggestionFromAutocompleteResult"))) |
297 return v8::FunctionTemplate::New(SetQuerySuggestionFromAutocompleteResult); | 311 return v8::FunctionTemplate::New(SetQuerySuggestionFromAutocompleteResult); |
298 if (name->Equals(v8::String::New("SetQuery"))) | 312 if (name->Equals(v8::String::New("SetQuery"))) |
299 return v8::FunctionTemplate::New(SetQuery); | 313 return v8::FunctionTemplate::New(SetQuery); |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
420 ranking_data->Set(v8::String::New("relevance"), | 434 ranking_data->Set(v8::String::New("relevance"), |
421 v8::Int32::New(results[i].relevance)); | 435 v8::Int32::New(results[i].relevance)); |
422 result->Set(v8::String::New("rankingData"), ranking_data); | 436 result->Set(v8::String::New("rankingData"), ranking_data); |
423 | 437 |
424 results_array->Set(i, result); | 438 results_array->Set(i, result); |
425 } | 439 } |
426 return results_array; | 440 return results_array; |
427 } | 441 } |
428 | 442 |
429 // static | 443 // static |
| 444 v8::Handle<v8::Value> SearchBoxExtensionWrapper::IsKeyCaptureEnabled( |
| 445 const v8::Arguments& args) { |
| 446 content::RenderView* render_view = GetRenderView(); |
| 447 if (!render_view) return v8::Undefined(); |
| 448 |
| 449 return v8::Boolean::New(SearchBox::Get(render_view)-> |
| 450 is_key_capture_enabled()); |
| 451 } |
| 452 |
| 453 // static |
430 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetContext( | 454 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetContext( |
431 const v8::Arguments& args) { | 455 const v8::Arguments& args) { |
432 content::RenderView* render_view = GetRenderView(); | 456 content::RenderView* render_view = GetRenderView(); |
433 if (!render_view) return v8::Undefined(); | 457 if (!render_view) return v8::Undefined(); |
434 | 458 |
435 const chrome::search::Mode& mode = SearchBox::Get(render_view)->mode(); | 459 const chrome::search::Mode& mode = SearchBox::Get(render_view)->mode(); |
436 DVLOG(1) << "GetContext: " << mode.origin << ":" << mode.mode; | 460 DVLOG(1) << "GetContext: " << mode.origin << ":" << mode.mode; |
437 v8::Handle<v8::Object> context = v8::Object::New(); | 461 v8::Handle<v8::Object> context = v8::Object::New(); |
438 // TODO(jered): Remove isNewTabPage, it's deprecated. | 462 // TODO(jered): Remove isNewTabPage, it's deprecated. |
439 context->Set(v8::String::New("isNewTabPage"), | 463 context->Set(v8::String::New("isNewTabPage"), |
(...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
798 | 822 |
799 // static | 823 // static |
800 void SearchBoxExtension::DispatchUpOrDownKeyPress(WebKit::WebFrame* frame, | 824 void SearchBoxExtension::DispatchUpOrDownKeyPress(WebKit::WebFrame* frame, |
801 int count) { | 825 int count) { |
802 Dispatch(frame, WebKit::WebString::fromUTF8( | 826 Dispatch(frame, WebKit::WebString::fromUTF8( |
803 base::StringPrintf(kDispatchUpOrDownKeyPressEventScript, abs(count), | 827 base::StringPrintf(kDispatchUpOrDownKeyPressEventScript, abs(count), |
804 count < 0 ? ui::VKEY_UP : ui::VKEY_DOWN))); | 828 count < 0 ? ui::VKEY_UP : ui::VKEY_DOWN))); |
805 } | 829 } |
806 | 830 |
807 // static | 831 // static |
| 832 void SearchBoxExtension::DispatchKeyCaptureChange(WebKit::WebFrame* frame) { |
| 833 DVLOG(1) << "DispatchKeyCaptureChange"; |
| 834 Dispatch(frame, kDispatchKeyCaptureChangeScript); |
| 835 } |
| 836 |
| 837 // static |
808 void SearchBoxExtension::DispatchContextChange(WebKit::WebFrame* frame) { | 838 void SearchBoxExtension::DispatchContextChange(WebKit::WebFrame* frame) { |
809 DVLOG(1) << "DispatchContextChange"; | 839 DVLOG(1) << "DispatchContextChange"; |
810 Dispatch(frame, kDispatchContextChangeEventScript); | 840 Dispatch(frame, kDispatchContextChangeEventScript); |
811 } | 841 } |
812 | 842 |
813 // static | 843 // static |
814 void SearchBoxExtension::DispatchThemeChange(WebKit::WebFrame* frame) { | 844 void SearchBoxExtension::DispatchThemeChange(WebKit::WebFrame* frame) { |
815 Dispatch(frame, kDispatchThemeChangeEventScript); | 845 Dispatch(frame, kDispatchThemeChangeEventScript); |
816 } | 846 } |
817 | 847 |
818 // static | 848 // static |
819 void SearchBoxExtension::DispatchThemeAreaHeightChange( | 849 void SearchBoxExtension::DispatchThemeAreaHeightChange( |
820 WebKit::WebFrame* frame) { | 850 WebKit::WebFrame* frame) { |
821 Dispatch(frame, kDispatchThemeAreaHeightChangeEventScript); | 851 Dispatch(frame, kDispatchThemeAreaHeightChangeEventScript); |
822 } | 852 } |
823 | 853 |
824 // static | 854 // static |
825 v8::Extension* SearchBoxExtension::Get() { | 855 v8::Extension* SearchBoxExtension::Get() { |
826 return new SearchBoxExtensionWrapper(ResourceBundle::GetSharedInstance(). | 856 return new SearchBoxExtensionWrapper(ResourceBundle::GetSharedInstance(). |
827 GetRawDataResource(IDR_SEARCHBOX_API)); | 857 GetRawDataResource(IDR_SEARCHBOX_API)); |
828 } | 858 } |
829 | 859 |
830 } // namespace extensions_v8 | 860 } // namespace extensions_v8 |
OLD | NEW |