| 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/i18n/rtl.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "base/stringprintf.h" | 9 #include "base/stringprintf.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| (...skipping 435 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 446 | 446 |
| 447 static const char kDispatchBarsHiddenEventScript[] = | 447 static const char kDispatchBarsHiddenEventScript[] = |
| 448 "if (window.chrome &&" | 448 "if (window.chrome &&" |
| 449 " window.chrome.searchBox &&" | 449 " window.chrome.searchBox &&" |
| 450 " window.chrome.searchBox.onbarshidden &&" | 450 " window.chrome.searchBox.onbarshidden &&" |
| 451 " typeof window.chrome.searchBox.onbarshidden == 'function') {" | 451 " typeof window.chrome.searchBox.onbarshidden == 'function') {" |
| 452 " window.chrome.searchBox.onbarshidden();" | 452 " window.chrome.searchBox.onbarshidden();" |
| 453 " true;" | 453 " true;" |
| 454 "}"; | 454 "}"; |
| 455 | 455 |
| 456 static const char kDispatchFocusChangedScript[] = |
| 457 "if (window.chrome &&" |
| 458 " window.chrome.embeddedSearch &&" |
| 459 " window.chrome.embeddedSearch.searchBox &&" |
| 460 " window.chrome.embeddedSearch.searchBox.onfocuschange &&" |
| 461 " typeof window.chrome.embeddedSearch.searchBox.onfocuschange ==" |
| 462 " 'function') {" |
| 463 " window.chrome.embeddedSearch.searchBox.onfocuschange();" |
| 464 " true;" |
| 465 "}"; |
| 466 |
| 456 // ---------------------------------------------------------------------------- | 467 // ---------------------------------------------------------------------------- |
| 457 | 468 |
| 458 class SearchBoxExtensionWrapper : public v8::Extension { | 469 class SearchBoxExtensionWrapper : public v8::Extension { |
| 459 public: | 470 public: |
| 460 explicit SearchBoxExtensionWrapper(const base::StringPiece& code); | 471 explicit SearchBoxExtensionWrapper(const base::StringPiece& code); |
| 461 | 472 |
| 462 // Allows v8's javascript code to call the native functions defined | 473 // Allows v8's javascript code to call the native functions defined |
| 463 // in this class for window.chrome. | 474 // in this class for window.chrome. |
| 464 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( | 475 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( |
| 465 v8::Handle<v8::String> name) OVERRIDE; | 476 v8::Handle<v8::String> name) OVERRIDE; |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 590 // GetRenderViewWithCheckedOrigin() enforces that only code in the origin | 601 // GetRenderViewWithCheckedOrigin() enforces that only code in the origin |
| 591 // chrome-search://suggestion can call this function. | 602 // chrome-search://suggestion can call this function. |
| 592 static v8::Handle<v8::Value> GetSuggestionData(const v8::Arguments& args); | 603 static v8::Handle<v8::Value> GetSuggestionData(const v8::Arguments& args); |
| 593 | 604 |
| 594 // Gets the raw data for a most visited item including its raw URL. | 605 // Gets the raw data for a most visited item including its raw URL. |
| 595 // GetRenderViewWithCheckedOrigin() enforces that only code in the origin | 606 // GetRenderViewWithCheckedOrigin() enforces that only code in the origin |
| 596 // chrome-search://most-visited can call this function. | 607 // chrome-search://most-visited can call this function. |
| 597 static v8::Handle<v8::Value> GetMostVisitedItemData( | 608 static v8::Handle<v8::Value> GetMostVisitedItemData( |
| 598 const v8::Arguments& args); | 609 const v8::Arguments& args); |
| 599 | 610 |
| 611 // Gets whether the omnibox has focus or not. |
| 612 static v8::Handle<v8::Value> IsFocused(const v8::Arguments& args); |
| 613 |
| 600 private: | 614 private: |
| 601 DISALLOW_COPY_AND_ASSIGN(SearchBoxExtensionWrapper); | 615 DISALLOW_COPY_AND_ASSIGN(SearchBoxExtensionWrapper); |
| 602 }; | 616 }; |
| 603 | 617 |
| 604 SearchBoxExtensionWrapper::SearchBoxExtensionWrapper( | 618 SearchBoxExtensionWrapper::SearchBoxExtensionWrapper( |
| 605 const base::StringPiece& code) | 619 const base::StringPiece& code) |
| 606 : v8::Extension(kSearchBoxExtensionName, code.data(), 0, 0, code.size()) { | 620 : v8::Extension(kSearchBoxExtensionName, code.data(), 0, 0, code.size()) { |
| 607 } | 621 } |
| 608 | 622 |
| 609 v8::Handle<v8::FunctionTemplate> SearchBoxExtensionWrapper::GetNativeFunction( | 623 v8::Handle<v8::FunctionTemplate> SearchBoxExtensionWrapper::GetNativeFunction( |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 673 if (name->Equals(v8::String::New("UndoMostVisitedDeletion"))) | 687 if (name->Equals(v8::String::New("UndoMostVisitedDeletion"))) |
| 674 return v8::FunctionTemplate::New(UndoMostVisitedDeletion); | 688 return v8::FunctionTemplate::New(UndoMostVisitedDeletion); |
| 675 if (name->Equals(v8::String::New("ShowBars"))) | 689 if (name->Equals(v8::String::New("ShowBars"))) |
| 676 return v8::FunctionTemplate::New(ShowBars); | 690 return v8::FunctionTemplate::New(ShowBars); |
| 677 if (name->Equals(v8::String::New("HideBars"))) | 691 if (name->Equals(v8::String::New("HideBars"))) |
| 678 return v8::FunctionTemplate::New(HideBars); | 692 return v8::FunctionTemplate::New(HideBars); |
| 679 if (name->Equals(v8::String::New("GetSuggestionData"))) | 693 if (name->Equals(v8::String::New("GetSuggestionData"))) |
| 680 return v8::FunctionTemplate::New(GetSuggestionData); | 694 return v8::FunctionTemplate::New(GetSuggestionData); |
| 681 if (name->Equals(v8::String::New("GetMostVisitedItemData"))) | 695 if (name->Equals(v8::String::New("GetMostVisitedItemData"))) |
| 682 return v8::FunctionTemplate::New(GetMostVisitedItemData); | 696 return v8::FunctionTemplate::New(GetMostVisitedItemData); |
| 697 if (name->Equals(v8::String::New("IsFocused"))) |
| 698 return v8::FunctionTemplate::New(IsFocused); |
| 683 return v8::Handle<v8::FunctionTemplate>(); | 699 return v8::Handle<v8::FunctionTemplate>(); |
| 684 } | 700 } |
| 685 | 701 |
| 686 // static | 702 // static |
| 687 content::RenderView* SearchBoxExtensionWrapper::GetRenderView() { | 703 content::RenderView* SearchBoxExtensionWrapper::GetRenderView() { |
| 688 WebKit::WebFrame* webframe = WebKit::WebFrame::frameForCurrentContext(); | 704 WebKit::WebFrame* webframe = WebKit::WebFrame::frameForCurrentContext(); |
| 689 if (!webframe) return NULL; | 705 if (!webframe) return NULL; |
| 690 | 706 |
| 691 WebKit::WebView* webview = webframe->view(); | 707 WebKit::WebView* webview = webframe->view(); |
| 692 if (!webview) return NULL; // can happen during closing | 708 if (!webview) return NULL; // can happen during closing |
| (...skipping 694 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1387 InstantRestrictedID restricted_id = args[0]->IntegerValue(); | 1403 InstantRestrictedID restricted_id = args[0]->IntegerValue(); |
| 1388 InstantMostVisitedItem mv_item; | 1404 InstantMostVisitedItem mv_item; |
| 1389 if (!SearchBox::Get(render_view)->GetMostVisitedItemWithID( | 1405 if (!SearchBox::Get(render_view)->GetMostVisitedItemWithID( |
| 1390 restricted_id, &mv_item)) { | 1406 restricted_id, &mv_item)) { |
| 1391 return v8::Undefined(); | 1407 return v8::Undefined(); |
| 1392 } | 1408 } |
| 1393 return GenerateMostVisitedItem(restricted_id, mv_item); | 1409 return GenerateMostVisitedItem(restricted_id, mv_item); |
| 1394 } | 1410 } |
| 1395 | 1411 |
| 1396 // static | 1412 // static |
| 1413 v8::Handle<v8::Value> SearchBoxExtensionWrapper::IsFocused( |
| 1414 const v8::Arguments& args) { |
| 1415 content::RenderView* render_view = GetRenderView(); |
| 1416 if (!render_view) return v8::Undefined(); |
| 1417 |
| 1418 bool is_focused = SearchBox::Get(render_view)->is_focused(); |
| 1419 DVLOG(1) << render_view << " IsFocused: " << is_focused; |
| 1420 return v8::Boolean::New(is_focused); |
| 1421 } |
| 1422 |
| 1423 // static |
| 1397 void SearchBoxExtension::DispatchChange(WebKit::WebFrame* frame) { | 1424 void SearchBoxExtension::DispatchChange(WebKit::WebFrame* frame) { |
| 1398 Dispatch(frame, kDispatchChangeEventScript); | 1425 Dispatch(frame, kDispatchChangeEventScript); |
| 1399 } | 1426 } |
| 1400 | 1427 |
| 1401 // static | 1428 // static |
| 1402 void SearchBoxExtension::DispatchSubmit(WebKit::WebFrame* frame) { | 1429 void SearchBoxExtension::DispatchSubmit(WebKit::WebFrame* frame) { |
| 1403 Dispatch(frame, kDispatchSubmitEventScript); | 1430 Dispatch(frame, kDispatchSubmitEventScript); |
| 1404 } | 1431 } |
| 1405 | 1432 |
| 1406 // static | 1433 // static |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1456 void SearchBoxExtension::DispatchMostVisitedChanged( | 1483 void SearchBoxExtension::DispatchMostVisitedChanged( |
| 1457 WebKit::WebFrame* frame) { | 1484 WebKit::WebFrame* frame) { |
| 1458 Dispatch(frame, kDispatchMostVisitedChangedScript); | 1485 Dispatch(frame, kDispatchMostVisitedChangedScript); |
| 1459 } | 1486 } |
| 1460 | 1487 |
| 1461 // static | 1488 // static |
| 1462 void SearchBoxExtension::DispatchBarsHidden(WebKit::WebFrame* frame) { | 1489 void SearchBoxExtension::DispatchBarsHidden(WebKit::WebFrame* frame) { |
| 1463 Dispatch(frame, kDispatchBarsHiddenEventScript); | 1490 Dispatch(frame, kDispatchBarsHiddenEventScript); |
| 1464 } | 1491 } |
| 1465 | 1492 |
| 1493 // static |
| 1494 void SearchBoxExtension::DispatchFocusChange(WebKit::WebFrame* frame) { |
| 1495 Dispatch(frame, kDispatchFocusChangedScript); |
| 1496 } |
| 1497 |
| 1466 } // namespace extensions_v8 | 1498 } // namespace extensions_v8 |
| OLD | NEW |