| 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/i18n/rtl.h" |
| 7 #include "base/string_number_conversions.h" | 8 #include "base/string_number_conversions.h" |
| 8 #include "base/stringprintf.h" | 9 #include "base/stringprintf.h" |
| 9 #include "chrome/common/extensions/extension.h" | 10 #include "chrome/common/extensions/extension.h" |
| 10 #include "chrome/renderer/searchbox/searchbox.h" | 11 #include "chrome/renderer/searchbox/searchbox.h" |
| 11 #include "content/public/renderer/render_view.h" | 12 #include "content/public/renderer/render_view.h" |
| 12 #include "grit/renderer_resources.h" | 13 #include "grit/renderer_resources.h" |
| 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScriptSource.h" | 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScriptSource.h" |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
| 16 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLReques
t.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLReques
t.h" |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 static const char kDispatchThemeAreaHeightChangeEventScript[] = | 153 static const char kDispatchThemeAreaHeightChangeEventScript[] = |
| 153 "if (window.chrome &&" | 154 "if (window.chrome &&" |
| 154 " window.chrome.searchBox &&" | 155 " window.chrome.searchBox &&" |
| 155 " window.chrome.searchBox.onthemeareaheightchange &&" | 156 " window.chrome.searchBox.onthemeareaheightchange &&" |
| 156 " typeof window.chrome.searchBox.onthemeareaheightchange ==" | 157 " typeof window.chrome.searchBox.onthemeareaheightchange ==" |
| 157 " 'function') {" | 158 " 'function') {" |
| 158 " window.chrome.searchBox.onthemeareaheightchange();" | 159 " window.chrome.searchBox.onthemeareaheightchange();" |
| 159 " true;" | 160 " true;" |
| 160 "}"; | 161 "}"; |
| 161 | 162 |
| 163 static const char kDispatchMarginChangeEventScript[] = |
| 164 "if (window.chrome &&" |
| 165 " window.chrome.searchBox &&" |
| 166 " window.chrome.searchBox.onmarginchange &&" |
| 167 " typeof window.chrome.searchBox.onmarginchange == 'function') {" |
| 168 " window.chrome.searchBox.onmarginchange();" |
| 169 " true;" |
| 170 "}"; |
| 171 |
| 162 // ---------------------------------------------------------------------------- | 172 // ---------------------------------------------------------------------------- |
| 163 | 173 |
| 164 class SearchBoxExtensionWrapper : public v8::Extension { | 174 class SearchBoxExtensionWrapper : public v8::Extension { |
| 165 public: | 175 public: |
| 166 explicit SearchBoxExtensionWrapper(const base::StringPiece& code); | 176 explicit SearchBoxExtensionWrapper(const base::StringPiece& code); |
| 167 | 177 |
| 168 // Allows v8's javascript code to call the native functions defined | 178 // Allows v8's javascript code to call the native functions defined |
| 169 // in this class for window.chrome. | 179 // in this class for window.chrome. |
| 170 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( | 180 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( |
| 171 v8::Handle<v8::String> name); | 181 v8::Handle<v8::String> name); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 194 // Gets the y coordinate (relative to |window|) of the right edge of the | 204 // Gets the y coordinate (relative to |window|) of the right edge of the |
| 195 // region of the search box that overlaps the window. | 205 // region of the search box that overlaps the window. |
| 196 static v8::Handle<v8::Value> GetY(const v8::Arguments& args); | 206 static v8::Handle<v8::Value> GetY(const v8::Arguments& args); |
| 197 | 207 |
| 198 // Gets the width of the region of the search box that overlaps the window. | 208 // Gets the width of the region of the search box that overlaps the window. |
| 199 static v8::Handle<v8::Value> GetWidth(const v8::Arguments& args); | 209 static v8::Handle<v8::Value> GetWidth(const v8::Arguments& args); |
| 200 | 210 |
| 201 // Gets the height of the region of the search box that overlaps the window. | 211 // Gets the height of the region of the search box that overlaps the window. |
| 202 static v8::Handle<v8::Value> GetHeight(const v8::Arguments& args); | 212 static v8::Handle<v8::Value> GetHeight(const v8::Arguments& args); |
| 203 | 213 |
| 214 // Gets the width of the margin from the start-edge of the page to the start |
| 215 // of the suggestions dropdown. |
| 216 static v8::Handle<v8::Value> GetStartMargin(const v8::Arguments& args); |
| 217 |
| 218 // Gets the width of the margin from the end-edge of the page to the end of |
| 219 // the suggestions dropdown. |
| 220 static v8::Handle<v8::Value> GetEndMargin(const v8::Arguments& args); |
| 221 |
| 222 // Returns true if the Searchbox itself is oriented right-to-left. |
| 223 static v8::Handle<v8::Value> GetRightToLeft(const v8::Arguments& args); |
| 224 |
| 204 // Gets the autocomplete results from search box. | 225 // Gets the autocomplete results from search box. |
| 205 static v8::Handle<v8::Value> GetAutocompleteResults( | 226 static v8::Handle<v8::Value> GetAutocompleteResults( |
| 206 const v8::Arguments& args); | 227 const v8::Arguments& args); |
| 207 | 228 |
| 208 // Gets the current session context. | 229 // Gets the current session context. |
| 209 static v8::Handle<v8::Value> GetContext(const v8::Arguments& args); | 230 static v8::Handle<v8::Value> GetContext(const v8::Arguments& args); |
| 210 | 231 |
| 211 // Gets whether to display Instant results. | 232 // Gets whether to display Instant results. |
| 212 static v8::Handle<v8::Value> GetDisplayInstantResults( | 233 static v8::Handle<v8::Value> GetDisplayInstantResults( |
| 213 const v8::Arguments& args); | 234 const v8::Arguments& args); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 276 if (name->Equals(v8::String::New("GetSelectionEnd"))) | 297 if (name->Equals(v8::String::New("GetSelectionEnd"))) |
| 277 return v8::FunctionTemplate::New(GetSelectionEnd); | 298 return v8::FunctionTemplate::New(GetSelectionEnd); |
| 278 if (name->Equals(v8::String::New("GetX"))) | 299 if (name->Equals(v8::String::New("GetX"))) |
| 279 return v8::FunctionTemplate::New(GetX); | 300 return v8::FunctionTemplate::New(GetX); |
| 280 if (name->Equals(v8::String::New("GetY"))) | 301 if (name->Equals(v8::String::New("GetY"))) |
| 281 return v8::FunctionTemplate::New(GetY); | 302 return v8::FunctionTemplate::New(GetY); |
| 282 if (name->Equals(v8::String::New("GetWidth"))) | 303 if (name->Equals(v8::String::New("GetWidth"))) |
| 283 return v8::FunctionTemplate::New(GetWidth); | 304 return v8::FunctionTemplate::New(GetWidth); |
| 284 if (name->Equals(v8::String::New("GetHeight"))) | 305 if (name->Equals(v8::String::New("GetHeight"))) |
| 285 return v8::FunctionTemplate::New(GetHeight); | 306 return v8::FunctionTemplate::New(GetHeight); |
| 307 if (name->Equals(v8::String::New("GetStartMargin"))) |
| 308 return v8::FunctionTemplate::New(GetStartMargin); |
| 309 if (name->Equals(v8::String::New("GetEndMargin"))) |
| 310 return v8::FunctionTemplate::New(GetEndMargin); |
| 311 if (name->Equals(v8::String::New("GetRightToLeft"))) |
| 312 return v8::FunctionTemplate::New(GetRightToLeft); |
| 286 if (name->Equals(v8::String::New("GetAutocompleteResults"))) | 313 if (name->Equals(v8::String::New("GetAutocompleteResults"))) |
| 287 return v8::FunctionTemplate::New(GetAutocompleteResults); | 314 return v8::FunctionTemplate::New(GetAutocompleteResults); |
| 288 if (name->Equals(v8::String::New("GetContext"))) | 315 if (name->Equals(v8::String::New("GetContext"))) |
| 289 return v8::FunctionTemplate::New(GetContext); | 316 return v8::FunctionTemplate::New(GetContext); |
| 290 if (name->Equals(v8::String::New("GetDisplayInstantResults"))) | 317 if (name->Equals(v8::String::New("GetDisplayInstantResults"))) |
| 291 return v8::FunctionTemplate::New(GetDisplayInstantResults); | 318 return v8::FunctionTemplate::New(GetDisplayInstantResults); |
| 292 if (name->Equals(v8::String::New("GetThemeBackgroundInfo"))) | 319 if (name->Equals(v8::String::New("GetThemeBackgroundInfo"))) |
| 293 return v8::FunctionTemplate::New(GetThemeBackgroundInfo); | 320 return v8::FunctionTemplate::New(GetThemeBackgroundInfo); |
| 294 if (name->Equals(v8::String::New("GetThemeAreaHeight"))) | 321 if (name->Equals(v8::String::New("GetThemeAreaHeight"))) |
| 295 return v8::FunctionTemplate::New(GetThemeAreaHeight); | 322 return v8::FunctionTemplate::New(GetThemeAreaHeight); |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 364 | 391 |
| 365 return v8::Uint32::New(SearchBox::Get(render_view)->selection_end()); | 392 return v8::Uint32::New(SearchBox::Get(render_view)->selection_end()); |
| 366 } | 393 } |
| 367 | 394 |
| 368 // static | 395 // static |
| 369 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetX( | 396 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetX( |
| 370 const v8::Arguments& args) { | 397 const v8::Arguments& args) { |
| 371 content::RenderView* render_view = GetRenderView(); | 398 content::RenderView* render_view = GetRenderView(); |
| 372 if (!render_view) return v8::Undefined(); | 399 if (!render_view) return v8::Undefined(); |
| 373 | 400 |
| 374 return v8::Int32::New(SearchBox::Get(render_view)->GetRect().x()); | 401 return v8::Int32::New(SearchBox::Get(render_view)->GetPopupBounds().x()); |
| 375 } | 402 } |
| 376 | 403 |
| 377 // static | 404 // static |
| 378 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetY( | 405 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetY( |
| 379 const v8::Arguments& args) { | 406 const v8::Arguments& args) { |
| 380 content::RenderView* render_view = GetRenderView(); | 407 content::RenderView* render_view = GetRenderView(); |
| 381 if (!render_view) return v8::Undefined(); | 408 if (!render_view) return v8::Undefined(); |
| 382 | 409 |
| 383 return v8::Int32::New(SearchBox::Get(render_view)->GetRect().y()); | 410 return v8::Int32::New(SearchBox::Get(render_view)->GetPopupBounds().y()); |
| 384 } | 411 } |
| 385 | 412 |
| 386 // static | 413 // static |
| 387 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetWidth( | 414 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetWidth( |
| 388 const v8::Arguments& args) { | 415 const v8::Arguments& args) { |
| 389 content::RenderView* render_view = GetRenderView(); | 416 content::RenderView* render_view = GetRenderView(); |
| 390 if (!render_view) return v8::Undefined(); | 417 if (!render_view) return v8::Undefined(); |
| 391 | 418 |
| 392 return v8::Int32::New(SearchBox::Get(render_view)->GetRect().width()); | 419 return v8::Int32::New(SearchBox::Get(render_view)->GetPopupBounds().width()); |
| 393 } | 420 } |
| 394 | 421 |
| 395 // static | 422 // static |
| 396 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetHeight( | 423 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetHeight( |
| 397 const v8::Arguments& args) { | 424 const v8::Arguments& args) { |
| 398 content::RenderView* render_view = GetRenderView(); | 425 content::RenderView* render_view = GetRenderView(); |
| 399 if (!render_view) return v8::Undefined(); | 426 if (!render_view) return v8::Undefined(); |
| 400 | 427 |
| 401 return v8::Int32::New(SearchBox::Get(render_view)->GetRect().height()); | 428 return v8::Int32::New(SearchBox::Get(render_view)->GetPopupBounds().height()); |
| 402 } | 429 } |
| 403 | 430 |
| 404 // static | 431 // static |
| 432 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetStartMargin( |
| 433 const v8::Arguments& args) { |
| 434 content::RenderView* render_view = GetRenderView(); |
| 435 if (!render_view) return v8::Undefined(); |
| 436 return v8::Int32::New(SearchBox::Get(render_view)->GetStartMargin()); |
| 437 } |
| 438 |
| 439 // static |
| 440 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetEndMargin( |
| 441 const v8::Arguments& args) { |
| 442 content::RenderView* render_view = GetRenderView(); |
| 443 if (!render_view) return v8::Undefined(); |
| 444 return v8::Int32::New(SearchBox::Get(render_view)->GetEndMargin()); |
| 445 } |
| 446 |
| 447 // static |
| 448 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetRightToLeft( |
| 449 const v8::Arguments& args) { |
| 450 return v8::Boolean::New(base::i18n::IsRTL()); |
| 451 } |
| 452 |
| 453 // static |
| 405 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetAutocompleteResults( | 454 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetAutocompleteResults( |
| 406 const v8::Arguments& args) { | 455 const v8::Arguments& args) { |
| 407 content::RenderView* render_view = GetRenderView(); | 456 content::RenderView* render_view = GetRenderView(); |
| 408 if (!render_view) return v8::Undefined(); | 457 if (!render_view) return v8::Undefined(); |
| 409 | 458 |
| 410 DVLOG(1) << render_view << " GetAutocompleteResults"; | 459 DVLOG(1) << render_view << " GetAutocompleteResults"; |
| 411 const std::vector<InstantAutocompleteResult>& results = | 460 const std::vector<InstantAutocompleteResult>& results = |
| 412 SearchBox::Get(render_view)->GetAutocompleteResults(); | 461 SearchBox::Get(render_view)->GetAutocompleteResults(); |
| 413 size_t results_base = SearchBox::Get(render_view)->results_base(); | 462 size_t results_base = SearchBox::Get(render_view)->results_base(); |
| 414 | 463 |
| (...skipping 402 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 817 void SearchBoxExtension::DispatchContextChange(WebKit::WebFrame* frame) { | 866 void SearchBoxExtension::DispatchContextChange(WebKit::WebFrame* frame) { |
| 818 Dispatch(frame, kDispatchContextChangeEventScript); | 867 Dispatch(frame, kDispatchContextChangeEventScript); |
| 819 } | 868 } |
| 820 | 869 |
| 821 // static | 870 // static |
| 822 void SearchBoxExtension::DispatchThemeChange(WebKit::WebFrame* frame) { | 871 void SearchBoxExtension::DispatchThemeChange(WebKit::WebFrame* frame) { |
| 823 Dispatch(frame, kDispatchThemeChangeEventScript); | 872 Dispatch(frame, kDispatchThemeChangeEventScript); |
| 824 } | 873 } |
| 825 | 874 |
| 826 // static | 875 // static |
| 876 void SearchBoxExtension::DispatchMarginChange(WebKit::WebFrame* frame) { |
| 877 Dispatch(frame, kDispatchMarginChangeEventScript); |
| 878 } |
| 879 |
| 880 // static |
| 827 void SearchBoxExtension::DispatchThemeAreaHeightChange( | 881 void SearchBoxExtension::DispatchThemeAreaHeightChange( |
| 828 WebKit::WebFrame* frame) { | 882 WebKit::WebFrame* frame) { |
| 829 Dispatch(frame, kDispatchThemeAreaHeightChangeEventScript); | 883 Dispatch(frame, kDispatchThemeAreaHeightChangeEventScript); |
| 830 } | 884 } |
| 831 | 885 |
| 832 // static | 886 // static |
| 833 v8::Extension* SearchBoxExtension::Get() { | 887 v8::Extension* SearchBoxExtension::Get() { |
| 834 return new SearchBoxExtensionWrapper(ResourceBundle::GetSharedInstance(). | 888 return new SearchBoxExtensionWrapper(ResourceBundle::GetSharedInstance(). |
| 835 GetRawDataResource(IDR_SEARCHBOX_API)); | 889 GetRawDataResource(IDR_SEARCHBOX_API)); |
| 836 } | 890 } |
| 837 | 891 |
| 838 } // namespace extensions_v8 | 892 } // namespace extensions_v8 |
| OLD | NEW |