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_extension.h" | 5 #include "chrome/renderer/searchbox/searchbox_extension.h" |
6 | 6 |
7 #include <ctype.h> | 7 #include <ctype.h> |
8 | 8 |
| 9 #include "base/i18n/rtl.h" |
9 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
10 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
11 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
12 #include "chrome/renderer/searchbox/searchbox.h" | 13 #include "chrome/renderer/searchbox/searchbox.h" |
13 #include "content/public/renderer/render_view.h" | 14 #include "content/public/renderer/render_view.h" |
14 #include "grit/renderer_resources.h" | 15 #include "grit/renderer_resources.h" |
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScriptSource.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScriptSource.h" |
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
18 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLReques
t.h" | 19 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLReques
t.h" |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 | 141 |
141 static const char kDispatchContextChangeEventScript[] = | 142 static const char kDispatchContextChangeEventScript[] = |
142 "if (window.chrome &&" | 143 "if (window.chrome &&" |
143 " window.chrome.searchBox &&" | 144 " window.chrome.searchBox &&" |
144 " window.chrome.searchBox.oncontextchange &&" | 145 " window.chrome.searchBox.oncontextchange &&" |
145 " typeof window.chrome.searchBox.oncontextchange == 'function') {" | 146 " typeof window.chrome.searchBox.oncontextchange == 'function') {" |
146 " window.chrome.searchBox.oncontextchange();" | 147 " window.chrome.searchBox.oncontextchange();" |
147 " true;" | 148 " true;" |
148 "}"; | 149 "}"; |
149 | 150 |
| 151 static const char kDispatchMarginChangeEventScript[] = |
| 152 "if (window.chrome &&" |
| 153 " window.chrome.searchBox &&" |
| 154 " window.chrome.searchBox.onmarginchange &&" |
| 155 " typeof window.chrome.searchBox.onmarginchange == 'function') {" |
| 156 " window.chrome.searchBox.onmarginchange();" |
| 157 " true;" |
| 158 "}"; |
| 159 |
150 // ---------------------------------------------------------------------------- | 160 // ---------------------------------------------------------------------------- |
151 | 161 |
152 class SearchBoxExtensionWrapper : public v8::Extension { | 162 class SearchBoxExtensionWrapper : public v8::Extension { |
153 public: | 163 public: |
154 explicit SearchBoxExtensionWrapper(const base::StringPiece& code); | 164 explicit SearchBoxExtensionWrapper(const base::StringPiece& code); |
155 | 165 |
156 // Allows v8's javascript code to call the native functions defined | 166 // Allows v8's javascript code to call the native functions defined |
157 // in this class for window.chrome. | 167 // in this class for window.chrome. |
158 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( | 168 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( |
159 v8::Handle<v8::String> name); | 169 v8::Handle<v8::String> name); |
(...skipping 22 matching lines...) Expand all Loading... |
182 // Gets the y coordinate (relative to |window|) of the right edge of the | 192 // Gets the y coordinate (relative to |window|) of the right edge of the |
183 // region of the search box that overlaps the window. | 193 // region of the search box that overlaps the window. |
184 static v8::Handle<v8::Value> GetY(const v8::Arguments& args); | 194 static v8::Handle<v8::Value> GetY(const v8::Arguments& args); |
185 | 195 |
186 // Gets the width of the region of the search box that overlaps the window. | 196 // Gets the width of the region of the search box that overlaps the window. |
187 static v8::Handle<v8::Value> GetWidth(const v8::Arguments& args); | 197 static v8::Handle<v8::Value> GetWidth(const v8::Arguments& args); |
188 | 198 |
189 // Gets the height of the region of the search box that overlaps the window. | 199 // Gets the height of the region of the search box that overlaps the window. |
190 static v8::Handle<v8::Value> GetHeight(const v8::Arguments& args); | 200 static v8::Handle<v8::Value> GetHeight(const v8::Arguments& args); |
191 | 201 |
| 202 // Gets the width of the margin from the start of the page to the start of |
| 203 // the suggestions dropdown. |
| 204 static v8::Handle<v8::Value> GetStartMargin(const v8::Arguments& args); |
| 205 |
| 206 // Gets the width of the margin from the start of the page to the end of |
| 207 // the suggestions dropdown. |
| 208 static v8::Handle<v8::Value> GetEndMargin(const v8::Arguments& args); |
| 209 |
| 210 // Returns true if the Searchbox itself is oriented right-to-left. |
| 211 static v8::Handle<v8::Value> GetRightToLeft(const v8::Arguments& args); |
| 212 |
192 // Gets the autocomplete results from search box. | 213 // Gets the autocomplete results from search box. |
193 static v8::Handle<v8::Value> GetAutocompleteResults( | 214 static v8::Handle<v8::Value> GetAutocompleteResults( |
194 const v8::Arguments& args); | 215 const v8::Arguments& args); |
195 | 216 |
196 // Gets whether the search box is focused. | 217 // Gets whether the search box is focused. |
197 static v8::Handle<v8::Value> GetIsFocused(const v8::Arguments& args); | 218 static v8::Handle<v8::Value> GetIsFocused(const v8::Arguments& args); |
198 | 219 |
199 // Gets the current session context. | 220 // Gets the current session context. |
200 static v8::Handle<v8::Value> GetContext(const v8::Arguments& args); | 221 static v8::Handle<v8::Value> GetContext(const v8::Arguments& args); |
201 | 222 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 } else if (name->Equals(v8::String::New("GetSelectionEnd"))) { | 269 } else if (name->Equals(v8::String::New("GetSelectionEnd"))) { |
249 return v8::FunctionTemplate::New(GetSelectionEnd); | 270 return v8::FunctionTemplate::New(GetSelectionEnd); |
250 } else if (name->Equals(v8::String::New("GetX"))) { | 271 } else if (name->Equals(v8::String::New("GetX"))) { |
251 return v8::FunctionTemplate::New(GetX); | 272 return v8::FunctionTemplate::New(GetX); |
252 } else if (name->Equals(v8::String::New("GetY"))) { | 273 } else if (name->Equals(v8::String::New("GetY"))) { |
253 return v8::FunctionTemplate::New(GetY); | 274 return v8::FunctionTemplate::New(GetY); |
254 } else if (name->Equals(v8::String::New("GetWidth"))) { | 275 } else if (name->Equals(v8::String::New("GetWidth"))) { |
255 return v8::FunctionTemplate::New(GetWidth); | 276 return v8::FunctionTemplate::New(GetWidth); |
256 } else if (name->Equals(v8::String::New("GetHeight"))) { | 277 } else if (name->Equals(v8::String::New("GetHeight"))) { |
257 return v8::FunctionTemplate::New(GetHeight); | 278 return v8::FunctionTemplate::New(GetHeight); |
| 279 } else if (name->Equals(v8::String::New("GetStartMargin"))) { |
| 280 return v8::FunctionTemplate::New(GetStartMargin); |
| 281 } else if (name->Equals(v8::String::New("GetEndMargin"))) { |
| 282 return v8::FunctionTemplate::New(GetEndMargin); |
| 283 } else if (name->Equals(v8::String::New("GetRightToLeft"))) { |
| 284 return v8::FunctionTemplate::New(GetRightToLeft); |
258 } else if (name->Equals(v8::String::New("GetAutocompleteResults"))) { | 285 } else if (name->Equals(v8::String::New("GetAutocompleteResults"))) { |
259 return v8::FunctionTemplate::New(GetAutocompleteResults); | 286 return v8::FunctionTemplate::New(GetAutocompleteResults); |
260 } else if (name->Equals(v8::String::New("GetIsFocused"))) { | 287 } else if (name->Equals(v8::String::New("GetIsFocused"))) { |
261 return v8::FunctionTemplate::New(GetIsFocused); | 288 return v8::FunctionTemplate::New(GetIsFocused); |
262 } else if (name->Equals(v8::String::New("GetContext"))) { | 289 } else if (name->Equals(v8::String::New("GetContext"))) { |
263 return v8::FunctionTemplate::New(GetContext); | 290 return v8::FunctionTemplate::New(GetContext); |
264 } else if (name->Equals(v8::String::New("NavigateContentWindow"))) { | 291 } else if (name->Equals(v8::String::New("NavigateContentWindow"))) { |
265 return v8::FunctionTemplate::New(NavigateContentWindow); | 292 return v8::FunctionTemplate::New(NavigateContentWindow); |
266 } else if (name->Equals(v8::String::New("SetSuggestions"))) { | 293 } else if (name->Equals(v8::String::New("SetSuggestions"))) { |
267 return v8::FunctionTemplate::New(SetSuggestions); | 294 return v8::FunctionTemplate::New(SetSuggestions); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 content::RenderView* render_view = GetRenderView(); | 350 content::RenderView* render_view = GetRenderView(); |
324 if (!render_view) return v8::Undefined(); | 351 if (!render_view) return v8::Undefined(); |
325 return v8::Uint32::New(SearchBox::Get(render_view)->selection_end()); | 352 return v8::Uint32::New(SearchBox::Get(render_view)->selection_end()); |
326 } | 353 } |
327 | 354 |
328 // static | 355 // static |
329 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetX( | 356 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetX( |
330 const v8::Arguments& args) { | 357 const v8::Arguments& args) { |
331 content::RenderView* render_view = GetRenderView(); | 358 content::RenderView* render_view = GetRenderView(); |
332 if (!render_view) return v8::Undefined(); | 359 if (!render_view) return v8::Undefined(); |
333 return v8::Int32::New(SearchBox::Get(render_view)->GetRect().x()); | 360 return v8::Int32::New(SearchBox::Get(render_view)->GetPopupBounds().x()); |
334 } | 361 } |
335 | 362 |
336 // static | 363 // static |
337 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetY( | 364 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetY( |
338 const v8::Arguments& args) { | 365 const v8::Arguments& args) { |
339 content::RenderView* render_view = GetRenderView(); | 366 content::RenderView* render_view = GetRenderView(); |
340 if (!render_view) return v8::Undefined(); | 367 if (!render_view) return v8::Undefined(); |
341 return v8::Int32::New(SearchBox::Get(render_view)->GetRect().y()); | 368 return v8::Int32::New(SearchBox::Get(render_view)->GetPopupBounds().y()); |
342 } | 369 } |
343 | 370 |
344 // static | 371 // static |
345 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetWidth( | 372 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetWidth( |
346 const v8::Arguments& args) { | 373 const v8::Arguments& args) { |
347 content::RenderView* render_view = GetRenderView(); | 374 content::RenderView* render_view = GetRenderView(); |
348 if (!render_view) return v8::Undefined(); | 375 if (!render_view) return v8::Undefined(); |
349 return v8::Int32::New(SearchBox::Get(render_view)->GetRect().width()); | 376 return v8::Int32::New(SearchBox::Get(render_view)->GetPopupBounds().width()); |
350 } | 377 } |
351 | 378 |
352 // static | 379 // static |
353 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetHeight( | 380 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetHeight( |
354 const v8::Arguments& args) { | 381 const v8::Arguments& args) { |
355 content::RenderView* render_view = GetRenderView(); | 382 content::RenderView* render_view = GetRenderView(); |
356 if (!render_view) return v8::Undefined(); | 383 if (!render_view) return v8::Undefined(); |
357 return v8::Int32::New(SearchBox::Get(render_view)->GetRect().height()); | 384 return v8::Int32::New(SearchBox::Get(render_view)->GetPopupBounds().height()); |
358 } | 385 } |
359 | 386 |
360 // static | 387 // static |
| 388 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetStartMargin( |
| 389 const v8::Arguments& args) { |
| 390 content::RenderView* render_view = GetRenderView(); |
| 391 if (!render_view) return v8::Undefined(); |
| 392 return v8::Int32::New(SearchBox::Get(render_view)->GetOmniboxBounds().x()); |
| 393 } |
| 394 |
| 395 // static |
| 396 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetEndMargin( |
| 397 const v8::Arguments& args) { |
| 398 content::RenderView* render_view = GetRenderView(); |
| 399 if (!render_view) return v8::Undefined(); |
| 400 const gfx::Rect rect = SearchBox::Get(render_view)->GetOmniboxBounds(); |
| 401 return v8::Int32::New(rect.x() + rect.width()); |
| 402 } |
| 403 |
| 404 // static |
| 405 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetRightToLeft( |
| 406 const v8::Arguments& args) { |
| 407 return v8::Boolean::New(base::i18n::IsRTL()); |
| 408 } |
| 409 |
| 410 // static |
361 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetAutocompleteResults( | 411 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetAutocompleteResults( |
362 const v8::Arguments& args) { | 412 const v8::Arguments& args) { |
363 content::RenderView* render_view = GetRenderView(); | 413 content::RenderView* render_view = GetRenderView(); |
364 if (!render_view) return v8::Undefined(); | 414 if (!render_view) return v8::Undefined(); |
365 const std::vector<InstantAutocompleteResult>& results = | 415 const std::vector<InstantAutocompleteResult>& results = |
366 SearchBox::Get(render_view)->GetAutocompleteResults(); | 416 SearchBox::Get(render_view)->GetAutocompleteResults(); |
367 const size_t results_base = SearchBox::Get(render_view)->results_base(); | 417 const size_t results_base = SearchBox::Get(render_view)->results_base(); |
368 v8::Handle<v8::Array> results_array = v8::Array::New(results.size()); | 418 v8::Handle<v8::Array> results_array = v8::Array::New(results.size()); |
369 for (size_t i = 0; i < results.size(); ++i) { | 419 for (size_t i = 0; i < results.size(); ++i) { |
370 v8::Handle<v8::Object> result = v8::Object::New(); | 420 v8::Handle<v8::Object> result = v8::Object::New(); |
(...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
703 void SearchBoxExtension::DispatchBlur(WebKit::WebFrame* frame) { | 753 void SearchBoxExtension::DispatchBlur(WebKit::WebFrame* frame) { |
704 Dispatch(frame, kDispatchBlurEventScript); | 754 Dispatch(frame, kDispatchBlurEventScript); |
705 } | 755 } |
706 | 756 |
707 // static | 757 // static |
708 void SearchBoxExtension::DispatchContextChange(WebKit::WebFrame* frame) { | 758 void SearchBoxExtension::DispatchContextChange(WebKit::WebFrame* frame) { |
709 Dispatch(frame, kDispatchContextChangeEventScript); | 759 Dispatch(frame, kDispatchContextChangeEventScript); |
710 } | 760 } |
711 | 761 |
712 // static | 762 // static |
| 763 void SearchBoxExtension::DispatchMarginChange(WebKit::WebFrame* frame) { |
| 764 Dispatch(frame, kDispatchMarginChangeEventScript); |
| 765 } |
| 766 |
| 767 // static |
713 bool SearchBoxExtension::PageSupportsInstant(WebKit::WebFrame* frame) { | 768 bool SearchBoxExtension::PageSupportsInstant(WebKit::WebFrame* frame) { |
714 if (!frame) return false; | 769 if (!frame) return false; |
715 | 770 |
716 v8::Handle<v8::Value> v = frame->executeScriptAndReturnValue( | 771 v8::Handle<v8::Value> v = frame->executeScriptAndReturnValue( |
717 WebKit::WebScriptSource(kSupportsInstantScript)); | 772 WebKit::WebScriptSource(kSupportsInstantScript)); |
718 bool supports_instant = !v.IsEmpty() && v->BooleanValue(); | 773 bool supports_instant = !v.IsEmpty() && v->BooleanValue(); |
719 | 774 |
720 // Send a resize message to tell the page that Chrome is actively using the | 775 // Send a resize message to tell the page that Chrome is actively using the |
721 // searchbox API with it. The page uses the message to transition from | 776 // searchbox API with it. The page uses the message to transition from |
722 // "homepage" mode to "search" mode. | 777 // "homepage" mode to "search" mode. |
723 if (supports_instant) | 778 if (supports_instant) |
724 DispatchResize(frame); | 779 DispatchResize(frame); |
725 | 780 |
726 return supports_instant; | 781 return supports_instant; |
727 } | 782 } |
728 | 783 |
729 // static | 784 // static |
730 v8::Extension* SearchBoxExtension::Get() { | 785 v8::Extension* SearchBoxExtension::Get() { |
731 const base::StringPiece code = | 786 const base::StringPiece code = |
732 ResourceBundle::GetSharedInstance().GetRawDataResource( | 787 ResourceBundle::GetSharedInstance().GetRawDataResource( |
733 IDR_SEARCHBOX_API); | 788 IDR_SEARCHBOX_API); |
734 return new SearchBoxExtensionWrapper(code); | 789 return new SearchBoxExtensionWrapper(code); |
735 } | 790 } |
736 | 791 |
737 } // namespace extensions_v8 | 792 } // namespace extensions_v8 |
OLD | NEW |