| 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 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 static const char kDispatchThemeAreaHeightChangeEventScript[] = | 172 static const char kDispatchThemeAreaHeightChangeEventScript[] = |
| 172 "if (window.chrome &&" | 173 "if (window.chrome &&" |
| 173 " window.chrome.searchBox &&" | 174 " window.chrome.searchBox &&" |
| 174 " window.chrome.searchBox.onthemeareaheightchange &&" | 175 " window.chrome.searchBox.onthemeareaheightchange &&" |
| 175 " typeof window.chrome.searchBox.onthemeareaheightchange ==" | 176 " typeof window.chrome.searchBox.onthemeareaheightchange ==" |
| 176 " 'function') {" | 177 " 'function') {" |
| 177 " window.chrome.searchBox.onthemeareaheightchange();" | 178 " window.chrome.searchBox.onthemeareaheightchange();" |
| 178 " true;" | 179 " true;" |
| 179 "}"; | 180 "}"; |
| 180 | 181 |
| 182 static const char kDispatchMarginChangeEventScript[] = |
| 183 "if (window.chrome &&" |
| 184 " window.chrome.searchBox &&" |
| 185 " window.chrome.searchBox.onmarginchange &&" |
| 186 " typeof window.chrome.searchBox.onmarginchange == 'function') {" |
| 187 " window.chrome.searchBox.onmarginchange();" |
| 188 " true;" |
| 189 "}"; |
| 190 |
| 181 // ---------------------------------------------------------------------------- | 191 // ---------------------------------------------------------------------------- |
| 182 | 192 |
| 183 class SearchBoxExtensionWrapper : public v8::Extension { | 193 class SearchBoxExtensionWrapper : public v8::Extension { |
| 184 public: | 194 public: |
| 185 explicit SearchBoxExtensionWrapper(const base::StringPiece& code); | 195 explicit SearchBoxExtensionWrapper(const base::StringPiece& code); |
| 186 | 196 |
| 187 // Allows v8's javascript code to call the native functions defined | 197 // Allows v8's javascript code to call the native functions defined |
| 188 // in this class for window.chrome. | 198 // in this class for window.chrome. |
| 189 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( | 199 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( |
| 190 v8::Handle<v8::String> name); | 200 v8::Handle<v8::String> name); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 213 // Gets the y coordinate (relative to |window|) of the right edge of the | 223 // Gets the y coordinate (relative to |window|) of the right edge of the |
| 214 // region of the search box that overlaps the window. | 224 // region of the search box that overlaps the window. |
| 215 static v8::Handle<v8::Value> GetY(const v8::Arguments& args); | 225 static v8::Handle<v8::Value> GetY(const v8::Arguments& args); |
| 216 | 226 |
| 217 // Gets the width of the region of the search box that overlaps the window. | 227 // Gets the width of the region of the search box that overlaps the window. |
| 218 static v8::Handle<v8::Value> GetWidth(const v8::Arguments& args); | 228 static v8::Handle<v8::Value> GetWidth(const v8::Arguments& args); |
| 219 | 229 |
| 220 // Gets the height of the region of the search box that overlaps the window. | 230 // Gets the height of the region of the search box that overlaps the window. |
| 221 static v8::Handle<v8::Value> GetHeight(const v8::Arguments& args); | 231 static v8::Handle<v8::Value> GetHeight(const v8::Arguments& args); |
| 222 | 232 |
| 233 // Gets the width of the margin from the start-edge of the page to the start |
| 234 // of the suggestions dropdown. |
| 235 static v8::Handle<v8::Value> GetStartMargin(const v8::Arguments& args); |
| 236 |
| 237 // Gets the width of the margin from the end-edge of the page to the end of |
| 238 // the suggestions dropdown. |
| 239 static v8::Handle<v8::Value> GetEndMargin(const v8::Arguments& args); |
| 240 |
| 241 // Returns true if the Searchbox itself is oriented right-to-left. |
| 242 static v8::Handle<v8::Value> GetRightToLeft(const v8::Arguments& args); |
| 243 |
| 223 // Gets the autocomplete results from search box. | 244 // Gets the autocomplete results from search box. |
| 224 static v8::Handle<v8::Value> GetAutocompleteResults( | 245 static v8::Handle<v8::Value> GetAutocompleteResults( |
| 225 const v8::Arguments& args); | 246 const v8::Arguments& args); |
| 226 | 247 |
| 227 // Gets the current session context. | 248 // Gets the current session context. |
| 228 static v8::Handle<v8::Value> GetContext(const v8::Arguments& args); | 249 static v8::Handle<v8::Value> GetContext(const v8::Arguments& args); |
| 229 | 250 |
| 230 // Gets whether to display Instant results. | 251 // Gets whether to display Instant results. |
| 231 static v8::Handle<v8::Value> GetDisplayInstantResults( | 252 static v8::Handle<v8::Value> GetDisplayInstantResults( |
| 232 const v8::Arguments& args); | 253 const v8::Arguments& args); |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 if (name->Equals(v8::String::New("GetSelectionEnd"))) | 325 if (name->Equals(v8::String::New("GetSelectionEnd"))) |
| 305 return v8::FunctionTemplate::New(GetSelectionEnd); | 326 return v8::FunctionTemplate::New(GetSelectionEnd); |
| 306 if (name->Equals(v8::String::New("GetX"))) | 327 if (name->Equals(v8::String::New("GetX"))) |
| 307 return v8::FunctionTemplate::New(GetX); | 328 return v8::FunctionTemplate::New(GetX); |
| 308 if (name->Equals(v8::String::New("GetY"))) | 329 if (name->Equals(v8::String::New("GetY"))) |
| 309 return v8::FunctionTemplate::New(GetY); | 330 return v8::FunctionTemplate::New(GetY); |
| 310 if (name->Equals(v8::String::New("GetWidth"))) | 331 if (name->Equals(v8::String::New("GetWidth"))) |
| 311 return v8::FunctionTemplate::New(GetWidth); | 332 return v8::FunctionTemplate::New(GetWidth); |
| 312 if (name->Equals(v8::String::New("GetHeight"))) | 333 if (name->Equals(v8::String::New("GetHeight"))) |
| 313 return v8::FunctionTemplate::New(GetHeight); | 334 return v8::FunctionTemplate::New(GetHeight); |
| 335 if (name->Equals(v8::String::New("GetStartMargin"))) |
| 336 return v8::FunctionTemplate::New(GetStartMargin); |
| 337 if (name->Equals(v8::String::New("GetEndMargin"))) |
| 338 return v8::FunctionTemplate::New(GetEndMargin); |
| 339 if (name->Equals(v8::String::New("GetRightToLeft"))) |
| 340 return v8::FunctionTemplate::New(GetRightToLeft); |
| 314 if (name->Equals(v8::String::New("GetAutocompleteResults"))) | 341 if (name->Equals(v8::String::New("GetAutocompleteResults"))) |
| 315 return v8::FunctionTemplate::New(GetAutocompleteResults); | 342 return v8::FunctionTemplate::New(GetAutocompleteResults); |
| 316 if (name->Equals(v8::String::New("GetContext"))) | 343 if (name->Equals(v8::String::New("GetContext"))) |
| 317 return v8::FunctionTemplate::New(GetContext); | 344 return v8::FunctionTemplate::New(GetContext); |
| 318 if (name->Equals(v8::String::New("GetDisplayInstantResults"))) | 345 if (name->Equals(v8::String::New("GetDisplayInstantResults"))) |
| 319 return v8::FunctionTemplate::New(GetDisplayInstantResults); | 346 return v8::FunctionTemplate::New(GetDisplayInstantResults); |
| 320 if (name->Equals(v8::String::New("GetThemeBackgroundInfo"))) | 347 if (name->Equals(v8::String::New("GetThemeBackgroundInfo"))) |
| 321 return v8::FunctionTemplate::New(GetThemeBackgroundInfo); | 348 return v8::FunctionTemplate::New(GetThemeBackgroundInfo); |
| 322 if (name->Equals(v8::String::New("GetThemeAreaHeight"))) | 349 if (name->Equals(v8::String::New("GetThemeAreaHeight"))) |
| 323 return v8::FunctionTemplate::New(GetThemeAreaHeight); | 350 return v8::FunctionTemplate::New(GetThemeAreaHeight); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 398 | 425 |
| 399 return v8::Uint32::New(SearchBox::Get(render_view)->selection_end()); | 426 return v8::Uint32::New(SearchBox::Get(render_view)->selection_end()); |
| 400 } | 427 } |
| 401 | 428 |
| 402 // static | 429 // static |
| 403 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetX( | 430 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetX( |
| 404 const v8::Arguments& args) { | 431 const v8::Arguments& args) { |
| 405 content::RenderView* render_view = GetRenderView(); | 432 content::RenderView* render_view = GetRenderView(); |
| 406 if (!render_view) return v8::Undefined(); | 433 if (!render_view) return v8::Undefined(); |
| 407 | 434 |
| 408 return v8::Int32::New(SearchBox::Get(render_view)->GetRect().x()); | 435 return v8::Int32::New(SearchBox::Get(render_view)->GetPopupBounds().x()); |
| 409 } | 436 } |
| 410 | 437 |
| 411 // static | 438 // static |
| 412 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetY( | 439 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetY( |
| 413 const v8::Arguments& args) { | 440 const v8::Arguments& args) { |
| 414 content::RenderView* render_view = GetRenderView(); | 441 content::RenderView* render_view = GetRenderView(); |
| 415 if (!render_view) return v8::Undefined(); | 442 if (!render_view) return v8::Undefined(); |
| 416 | 443 |
| 417 return v8::Int32::New(SearchBox::Get(render_view)->GetRect().y()); | 444 return v8::Int32::New(SearchBox::Get(render_view)->GetPopupBounds().y()); |
| 418 } | 445 } |
| 419 | 446 |
| 420 // static | 447 // static |
| 421 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetWidth( | 448 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetWidth( |
| 422 const v8::Arguments& args) { | 449 const v8::Arguments& args) { |
| 423 content::RenderView* render_view = GetRenderView(); | 450 content::RenderView* render_view = GetRenderView(); |
| 424 if (!render_view) return v8::Undefined(); | 451 if (!render_view) return v8::Undefined(); |
| 425 | 452 |
| 426 return v8::Int32::New(SearchBox::Get(render_view)->GetRect().width()); | 453 return v8::Int32::New(SearchBox::Get(render_view)->GetPopupBounds().width()); |
| 427 } | 454 } |
| 428 | 455 |
| 429 // static | 456 // static |
| 430 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetHeight( | 457 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetHeight( |
| 431 const v8::Arguments& args) { | 458 const v8::Arguments& args) { |
| 432 content::RenderView* render_view = GetRenderView(); | 459 content::RenderView* render_view = GetRenderView(); |
| 433 if (!render_view) return v8::Undefined(); | 460 if (!render_view) return v8::Undefined(); |
| 434 | 461 |
| 435 return v8::Int32::New(SearchBox::Get(render_view)->GetRect().height()); | 462 return v8::Int32::New(SearchBox::Get(render_view)->GetPopupBounds().height()); |
| 436 } | 463 } |
| 437 | 464 |
| 438 // static | 465 // static |
| 466 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetStartMargin( |
| 467 const v8::Arguments& args) { |
| 468 content::RenderView* render_view = GetRenderView(); |
| 469 if (!render_view) return v8::Undefined(); |
| 470 return v8::Int32::New(SearchBox::Get(render_view)->GetStartMargin()); |
| 471 } |
| 472 |
| 473 // static |
| 474 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetEndMargin( |
| 475 const v8::Arguments& args) { |
| 476 content::RenderView* render_view = GetRenderView(); |
| 477 if (!render_view) return v8::Undefined(); |
| 478 return v8::Int32::New(SearchBox::Get(render_view)->GetEndMargin()); |
| 479 } |
| 480 |
| 481 // static |
| 482 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetRightToLeft( |
| 483 const v8::Arguments& args) { |
| 484 return v8::Boolean::New(base::i18n::IsRTL()); |
| 485 } |
| 486 |
| 487 // static |
| 439 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetAutocompleteResults( | 488 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetAutocompleteResults( |
| 440 const v8::Arguments& args) { | 489 const v8::Arguments& args) { |
| 441 content::RenderView* render_view = GetRenderView(); | 490 content::RenderView* render_view = GetRenderView(); |
| 442 if (!render_view) return v8::Undefined(); | 491 if (!render_view) return v8::Undefined(); |
| 443 | 492 |
| 444 DVLOG(1) << render_view << " GetAutocompleteResults"; | 493 DVLOG(1) << render_view << " GetAutocompleteResults"; |
| 445 const std::vector<InstantAutocompleteResult>& results = | 494 const std::vector<InstantAutocompleteResult>& results = |
| 446 SearchBox::Get(render_view)->GetAutocompleteResults(); | 495 SearchBox::Get(render_view)->GetAutocompleteResults(); |
| 447 size_t results_base = SearchBox::Get(render_view)->results_base(); | 496 size_t results_base = SearchBox::Get(render_view)->results_base(); |
| 448 | 497 |
| (...skipping 439 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 888 void SearchBoxExtension::DispatchContextChange(WebKit::WebFrame* frame) { | 937 void SearchBoxExtension::DispatchContextChange(WebKit::WebFrame* frame) { |
| 889 Dispatch(frame, kDispatchContextChangeEventScript); | 938 Dispatch(frame, kDispatchContextChangeEventScript); |
| 890 } | 939 } |
| 891 | 940 |
| 892 // static | 941 // static |
| 893 void SearchBoxExtension::DispatchThemeChange(WebKit::WebFrame* frame) { | 942 void SearchBoxExtension::DispatchThemeChange(WebKit::WebFrame* frame) { |
| 894 Dispatch(frame, kDispatchThemeChangeEventScript); | 943 Dispatch(frame, kDispatchThemeChangeEventScript); |
| 895 } | 944 } |
| 896 | 945 |
| 897 // static | 946 // static |
| 947 void SearchBoxExtension::DispatchMarginChange(WebKit::WebFrame* frame) { |
| 948 Dispatch(frame, kDispatchMarginChangeEventScript); |
| 949 } |
| 950 |
| 951 // static |
| 898 void SearchBoxExtension::DispatchThemeAreaHeightChange( | 952 void SearchBoxExtension::DispatchThemeAreaHeightChange( |
| 899 WebKit::WebFrame* frame) { | 953 WebKit::WebFrame* frame) { |
| 900 Dispatch(frame, kDispatchThemeAreaHeightChangeEventScript); | 954 Dispatch(frame, kDispatchThemeAreaHeightChangeEventScript); |
| 901 } | 955 } |
| 902 | 956 |
| 903 // static | 957 // static |
| 904 v8::Extension* SearchBoxExtension::Get() { | 958 v8::Extension* SearchBoxExtension::Get() { |
| 905 return new SearchBoxExtensionWrapper(ResourceBundle::GetSharedInstance(). | 959 return new SearchBoxExtensionWrapper(ResourceBundle::GetSharedInstance(). |
| 906 GetRawDataResource(IDR_SEARCHBOX_API)); | 960 GetRawDataResource(IDR_SEARCHBOX_API)); |
| 907 } | 961 } |
| 908 | 962 |
| 909 } // namespace extensions_v8 | 963 } // namespace extensions_v8 |
| OLD | NEW |