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