| 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 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 if (name->Equals(v8::String::New("GetSelectionEnd"))) | 289 if (name->Equals(v8::String::New("GetSelectionEnd"))) |
| 269 return v8::FunctionTemplate::New(GetSelectionEnd); | 290 return v8::FunctionTemplate::New(GetSelectionEnd); |
| 270 if (name->Equals(v8::String::New("GetX"))) | 291 if (name->Equals(v8::String::New("GetX"))) |
| 271 return v8::FunctionTemplate::New(GetX); | 292 return v8::FunctionTemplate::New(GetX); |
| 272 if (name->Equals(v8::String::New("GetY"))) | 293 if (name->Equals(v8::String::New("GetY"))) |
| 273 return v8::FunctionTemplate::New(GetY); | 294 return v8::FunctionTemplate::New(GetY); |
| 274 if (name->Equals(v8::String::New("GetWidth"))) | 295 if (name->Equals(v8::String::New("GetWidth"))) |
| 275 return v8::FunctionTemplate::New(GetWidth); | 296 return v8::FunctionTemplate::New(GetWidth); |
| 276 if (name->Equals(v8::String::New("GetHeight"))) | 297 if (name->Equals(v8::String::New("GetHeight"))) |
| 277 return v8::FunctionTemplate::New(GetHeight); | 298 return v8::FunctionTemplate::New(GetHeight); |
| 299 if (name->Equals(v8::String::New("GetStartMargin"))) |
| 300 return v8::FunctionTemplate::New(GetStartMargin); |
| 301 if (name->Equals(v8::String::New("GetEndMargin"))) |
| 302 return v8::FunctionTemplate::New(GetEndMargin); |
| 303 if (name->Equals(v8::String::New("GetRightToLeft"))) |
| 304 return v8::FunctionTemplate::New(GetRightToLeft); |
| 278 if (name->Equals(v8::String::New("GetAutocompleteResults"))) | 305 if (name->Equals(v8::String::New("GetAutocompleteResults"))) |
| 279 return v8::FunctionTemplate::New(GetAutocompleteResults); | 306 return v8::FunctionTemplate::New(GetAutocompleteResults); |
| 280 if (name->Equals(v8::String::New("GetContext"))) | 307 if (name->Equals(v8::String::New("GetContext"))) |
| 281 return v8::FunctionTemplate::New(GetContext); | 308 return v8::FunctionTemplate::New(GetContext); |
| 282 if (name->Equals(v8::String::New("GetDisplayInstantResults"))) | 309 if (name->Equals(v8::String::New("GetDisplayInstantResults"))) |
| 283 return v8::FunctionTemplate::New(GetDisplayInstantResults); | 310 return v8::FunctionTemplate::New(GetDisplayInstantResults); |
| 284 if (name->Equals(v8::String::New("GetThemeBackgroundInfo"))) | 311 if (name->Equals(v8::String::New("GetThemeBackgroundInfo"))) |
| 285 return v8::FunctionTemplate::New(GetThemeBackgroundInfo); | 312 return v8::FunctionTemplate::New(GetThemeBackgroundInfo); |
| 286 if (name->Equals(v8::String::New("GetThemeAreaHeight"))) | 313 if (name->Equals(v8::String::New("GetThemeAreaHeight"))) |
| 287 return v8::FunctionTemplate::New(GetThemeAreaHeight); | 314 return v8::FunctionTemplate::New(GetThemeAreaHeight); |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 352 | 379 |
| 353 return v8::Uint32::New(SearchBox::Get(render_view)->selection_end()); | 380 return v8::Uint32::New(SearchBox::Get(render_view)->selection_end()); |
| 354 } | 381 } |
| 355 | 382 |
| 356 // static | 383 // static |
| 357 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetX( | 384 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetX( |
| 358 const v8::Arguments& args) { | 385 const v8::Arguments& args) { |
| 359 content::RenderView* render_view = GetRenderView(); | 386 content::RenderView* render_view = GetRenderView(); |
| 360 if (!render_view) return v8::Undefined(); | 387 if (!render_view) return v8::Undefined(); |
| 361 | 388 |
| 362 return v8::Int32::New(SearchBox::Get(render_view)->GetRect().x()); | 389 return v8::Int32::New(SearchBox::Get(render_view)->GetPopupBounds().x()); |
| 363 } | 390 } |
| 364 | 391 |
| 365 // static | 392 // static |
| 366 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetY( | 393 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetY( |
| 367 const v8::Arguments& args) { | 394 const v8::Arguments& args) { |
| 368 content::RenderView* render_view = GetRenderView(); | 395 content::RenderView* render_view = GetRenderView(); |
| 369 if (!render_view) return v8::Undefined(); | 396 if (!render_view) return v8::Undefined(); |
| 370 | 397 |
| 371 return v8::Int32::New(SearchBox::Get(render_view)->GetRect().y()); | 398 return v8::Int32::New(SearchBox::Get(render_view)->GetPopupBounds().y()); |
| 372 } | 399 } |
| 373 | 400 |
| 374 // static | 401 // static |
| 375 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetWidth( | 402 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetWidth( |
| 376 const v8::Arguments& args) { | 403 const v8::Arguments& args) { |
| 377 content::RenderView* render_view = GetRenderView(); | 404 content::RenderView* render_view = GetRenderView(); |
| 378 if (!render_view) return v8::Undefined(); | 405 if (!render_view) return v8::Undefined(); |
| 379 | 406 |
| 380 return v8::Int32::New(SearchBox::Get(render_view)->GetRect().width()); | 407 return v8::Int32::New(SearchBox::Get(render_view)->GetPopupBounds().width()); |
| 381 } | 408 } |
| 382 | 409 |
| 383 // static | 410 // static |
| 384 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetHeight( | 411 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetHeight( |
| 385 const v8::Arguments& args) { | 412 const v8::Arguments& args) { |
| 386 content::RenderView* render_view = GetRenderView(); | 413 content::RenderView* render_view = GetRenderView(); |
| 387 if (!render_view) return v8::Undefined(); | 414 if (!render_view) return v8::Undefined(); |
| 388 | 415 |
| 389 return v8::Int32::New(SearchBox::Get(render_view)->GetRect().height()); | 416 return v8::Int32::New(SearchBox::Get(render_view)->GetPopupBounds().height()); |
| 390 } | 417 } |
| 391 | 418 |
| 392 // static | 419 // static |
| 420 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetStartMargin( |
| 421 const v8::Arguments& args) { |
| 422 content::RenderView* render_view = GetRenderView(); |
| 423 if (!render_view) return v8::Undefined(); |
| 424 return v8::Int32::New(SearchBox::Get(render_view)->GetStartMargin()); |
| 425 } |
| 426 |
| 427 // static |
| 428 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetEndMargin( |
| 429 const v8::Arguments& args) { |
| 430 content::RenderView* render_view = GetRenderView(); |
| 431 if (!render_view) return v8::Undefined(); |
| 432 return v8::Int32::New(SearchBox::Get(render_view)->GetEndMargin()); |
| 433 } |
| 434 |
| 435 // static |
| 436 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetRightToLeft( |
| 437 const v8::Arguments& args) { |
| 438 return v8::Boolean::New(base::i18n::IsRTL()); |
| 439 } |
| 440 |
| 441 // static |
| 393 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetAutocompleteResults( | 442 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetAutocompleteResults( |
| 394 const v8::Arguments& args) { | 443 const v8::Arguments& args) { |
| 395 content::RenderView* render_view = GetRenderView(); | 444 content::RenderView* render_view = GetRenderView(); |
| 396 if (!render_view) return v8::Undefined(); | 445 if (!render_view) return v8::Undefined(); |
| 397 | 446 |
| 398 DVLOG(1) << render_view << " GetAutocompleteResults"; | 447 DVLOG(1) << render_view << " GetAutocompleteResults"; |
| 399 const std::vector<InstantAutocompleteResult>& results = | 448 const std::vector<InstantAutocompleteResult>& results = |
| 400 SearchBox::Get(render_view)->GetAutocompleteResults(); | 449 SearchBox::Get(render_view)->GetAutocompleteResults(); |
| 401 size_t results_base = SearchBox::Get(render_view)->results_base(); | 450 size_t results_base = SearchBox::Get(render_view)->results_base(); |
| 402 | 451 |
| (...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 783 void SearchBoxExtension::DispatchContextChange(WebKit::WebFrame* frame) { | 832 void SearchBoxExtension::DispatchContextChange(WebKit::WebFrame* frame) { |
| 784 Dispatch(frame, kDispatchContextChangeEventScript); | 833 Dispatch(frame, kDispatchContextChangeEventScript); |
| 785 } | 834 } |
| 786 | 835 |
| 787 // static | 836 // static |
| 788 void SearchBoxExtension::DispatchThemeChange(WebKit::WebFrame* frame) { | 837 void SearchBoxExtension::DispatchThemeChange(WebKit::WebFrame* frame) { |
| 789 Dispatch(frame, kDispatchThemeChangeEventScript); | 838 Dispatch(frame, kDispatchThemeChangeEventScript); |
| 790 } | 839 } |
| 791 | 840 |
| 792 // static | 841 // static |
| 842 void SearchBoxExtension::DispatchMarginChange(WebKit::WebFrame* frame) { |
| 843 Dispatch(frame, kDispatchMarginChangeEventScript); |
| 844 } |
| 845 |
| 846 // static |
| 793 void SearchBoxExtension::DispatchThemeAreaHeightChange( | 847 void SearchBoxExtension::DispatchThemeAreaHeightChange( |
| 794 WebKit::WebFrame* frame) { | 848 WebKit::WebFrame* frame) { |
| 795 Dispatch(frame, kDispatchThemeAreaHeightChangeEventScript); | 849 Dispatch(frame, kDispatchThemeAreaHeightChangeEventScript); |
| 796 } | 850 } |
| 797 | 851 |
| 798 // static | 852 // static |
| 799 v8::Extension* SearchBoxExtension::Get() { | 853 v8::Extension* SearchBoxExtension::Get() { |
| 800 return new SearchBoxExtensionWrapper(ResourceBundle::GetSharedInstance(). | 854 return new SearchBoxExtensionWrapper(ResourceBundle::GetSharedInstance(). |
| 801 GetRawDataResource(IDR_SEARCHBOX_API)); | 855 GetRawDataResource(IDR_SEARCHBOX_API)); |
| 802 } | 856 } |
| 803 | 857 |
| 804 } // namespace extensions_v8 | 858 } // namespace extensions_v8 |
| OLD | NEW |