| 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/stringprintf.h" | 8 #include "base/stringprintf.h" | 
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" | 
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" | 
| (...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 287   // Gets whether the browser is capturing key strokes. | 287   // Gets whether the browser is capturing key strokes. | 
| 288   static v8::Handle<v8::Value> IsKeyCaptureEnabled(const v8::Arguments& args); | 288   static v8::Handle<v8::Value> IsKeyCaptureEnabled(const v8::Arguments& args); | 
| 289 | 289 | 
| 290   // Gets the font family of the text in the omnibox. | 290   // Gets the font family of the text in the omnibox. | 
| 291   static v8::Handle<v8::Value> GetFont(const v8::Arguments& args); | 291   static v8::Handle<v8::Value> GetFont(const v8::Arguments& args); | 
| 292 | 292 | 
| 293   // Gets the font size of the text in the omnibox. | 293   // Gets the font size of the text in the omnibox. | 
| 294   static v8::Handle<v8::Value> GetFontSize(const v8::Arguments& args); | 294   static v8::Handle<v8::Value> GetFontSize(const v8::Arguments& args); | 
| 295 | 295 | 
| 296   // Navigates the window to a URL represented by either a URL string or a | 296   // Navigates the window to a URL represented by either a URL string or a | 
| 297   // restricted ID. | 297   // restricted ID. The two variants handle restricted IDs in their | 
|  | 298   // respective namespaces. | 
|  | 299   static v8::Handle<v8::Value> NavigateSearchBox(const v8::Arguments& args); | 
|  | 300   static v8::Handle<v8::Value> NavigateNewTabPage(const v8::Arguments& args); | 
|  | 301   // DEPRECATED: TODO(sreeram): Remove when google.com no longer uses this. | 
| 298   static v8::Handle<v8::Value> NavigateContentWindow(const v8::Arguments& args); | 302   static v8::Handle<v8::Value> NavigateContentWindow(const v8::Arguments& args); | 
| 299 | 303 | 
| 300   // Sets ordered suggestions. Valid for current |value|. | 304   // Sets ordered suggestions. Valid for current |value|. | 
| 301   static v8::Handle<v8::Value> SetSuggestions(const v8::Arguments& args); | 305   static v8::Handle<v8::Value> SetSuggestions(const v8::Arguments& args); | 
| 302 | 306 | 
| 303   // Sets the text to be autocompleted into the search box. | 307   // Sets the text to be autocompleted into the search box. | 
| 304   static v8::Handle<v8::Value> SetQuerySuggestion(const v8::Arguments& args); | 308   static v8::Handle<v8::Value> SetQuerySuggestion(const v8::Arguments& args); | 
| 305 | 309 | 
| 306   // Like |SetQuerySuggestion| but uses a restricted ID to identify the text. | 310   // Like |SetQuerySuggestion| but uses a restricted autocomplete result ID to | 
|  | 311   // identify the text. | 
| 307   static v8::Handle<v8::Value> SetQuerySuggestionFromAutocompleteResult( | 312   static v8::Handle<v8::Value> SetQuerySuggestionFromAutocompleteResult( | 
| 308       const v8::Arguments& args); | 313       const v8::Arguments& args); | 
| 309 | 314 | 
| 310   // Sets the search box text, completely replacing what the user typed. | 315   // Sets the search box text, completely replacing what the user typed. | 
| 311   static v8::Handle<v8::Value> SetQuery(const v8::Arguments& args); | 316   static v8::Handle<v8::Value> SetQuery(const v8::Arguments& args); | 
| 312 | 317 | 
| 313   // Like |SetQuery| but uses a restricted ID to identify the text. | 318   // Like |SetQuery| but uses a restricted autocomplete result ID to identify | 
|  | 319   // the text. | 
| 314   static v8::Handle<v8::Value> SetQueryFromAutocompleteResult( | 320   static v8::Handle<v8::Value> SetQueryFromAutocompleteResult( | 
| 315       const v8::Arguments& args); | 321       const v8::Arguments& args); | 
| 316 | 322 | 
| 317   // Requests the overlay be shown with the specified contents and height. | 323   // Requests the overlay be shown with the specified contents and height. | 
| 318   static v8::Handle<v8::Value> ShowOverlay(const v8::Arguments& args); | 324   static v8::Handle<v8::Value> ShowOverlay(const v8::Arguments& args); | 
| 319 | 325 | 
| 320   // Sets the focus to the omnibox. | 326   // Sets the focus to the omnibox. | 
| 321   static v8::Handle<v8::Value> FocusOmnibox(const v8::Arguments& args); | 327   static v8::Handle<v8::Value> FocusOmnibox(const v8::Arguments& args); | 
| 322 | 328 | 
| 323   // Start capturing user key strokes. | 329   // Start capturing user key strokes. | 
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 376   if (name->Equals(v8::String::New("GetDisplayInstantResults"))) | 382   if (name->Equals(v8::String::New("GetDisplayInstantResults"))) | 
| 377     return v8::FunctionTemplate::New(GetDisplayInstantResults); | 383     return v8::FunctionTemplate::New(GetDisplayInstantResults); | 
| 378   if (name->Equals(v8::String::New("GetThemeBackgroundInfo"))) | 384   if (name->Equals(v8::String::New("GetThemeBackgroundInfo"))) | 
| 379     return v8::FunctionTemplate::New(GetThemeBackgroundInfo); | 385     return v8::FunctionTemplate::New(GetThemeBackgroundInfo); | 
| 380   if (name->Equals(v8::String::New("IsKeyCaptureEnabled"))) | 386   if (name->Equals(v8::String::New("IsKeyCaptureEnabled"))) | 
| 381     return v8::FunctionTemplate::New(IsKeyCaptureEnabled); | 387     return v8::FunctionTemplate::New(IsKeyCaptureEnabled); | 
| 382   if (name->Equals(v8::String::New("GetFont"))) | 388   if (name->Equals(v8::String::New("GetFont"))) | 
| 383     return v8::FunctionTemplate::New(GetFont); | 389     return v8::FunctionTemplate::New(GetFont); | 
| 384   if (name->Equals(v8::String::New("GetFontSize"))) | 390   if (name->Equals(v8::String::New("GetFontSize"))) | 
| 385     return v8::FunctionTemplate::New(GetFontSize); | 391     return v8::FunctionTemplate::New(GetFontSize); | 
|  | 392   if (name->Equals(v8::String::New("NavigateSearchBox"))) | 
|  | 393     return v8::FunctionTemplate::New(NavigateSearchBox); | 
|  | 394   if (name->Equals(v8::String::New("NavigateNewTabPage"))) | 
|  | 395     return v8::FunctionTemplate::New(NavigateNewTabPage); | 
| 386   if (name->Equals(v8::String::New("NavigateContentWindow"))) | 396   if (name->Equals(v8::String::New("NavigateContentWindow"))) | 
| 387     return v8::FunctionTemplate::New(NavigateContentWindow); | 397     return v8::FunctionTemplate::New(NavigateContentWindow); | 
| 388   if (name->Equals(v8::String::New("SetSuggestions"))) | 398   if (name->Equals(v8::String::New("SetSuggestions"))) | 
| 389     return v8::FunctionTemplate::New(SetSuggestions); | 399     return v8::FunctionTemplate::New(SetSuggestions); | 
| 390   if (name->Equals(v8::String::New("SetQuerySuggestion"))) | 400   if (name->Equals(v8::String::New("SetQuerySuggestion"))) | 
| 391     return v8::FunctionTemplate::New(SetQuerySuggestion); | 401     return v8::FunctionTemplate::New(SetQuerySuggestion); | 
| 392   if (name->Equals(v8::String::New("SetQuerySuggestionFromAutocompleteResult"))) | 402   if (name->Equals(v8::String::New("SetQuerySuggestionFromAutocompleteResult"))) | 
| 393     return v8::FunctionTemplate::New(SetQuerySuggestionFromAutocompleteResult); | 403     return v8::FunctionTemplate::New(SetQuerySuggestionFromAutocompleteResult); | 
| 394   if (name->Equals(v8::String::New("SetQuery"))) | 404   if (name->Equals(v8::String::New("SetQuery"))) | 
| 395     return v8::FunctionTemplate::New(SetQuery); | 405     return v8::FunctionTemplate::New(SetQuery); | 
| (...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 673 // static | 683 // static | 
| 674 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetFontSize( | 684 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetFontSize( | 
| 675     const v8::Arguments& args) { | 685     const v8::Arguments& args) { | 
| 676   content::RenderView* render_view = GetRenderView(); | 686   content::RenderView* render_view = GetRenderView(); | 
| 677   if (!render_view) return v8::Undefined(); | 687   if (!render_view) return v8::Undefined(); | 
| 678 | 688 | 
| 679   return v8::Uint32::New(SearchBox::Get(render_view)->omnibox_font_size()); | 689   return v8::Uint32::New(SearchBox::Get(render_view)->omnibox_font_size()); | 
| 680 } | 690 } | 
| 681 | 691 | 
| 682 // static | 692 // static | 
| 683 v8::Handle<v8::Value> SearchBoxExtensionWrapper::NavigateContentWindow( | 693 v8::Handle<v8::Value> SearchBoxExtensionWrapper::NavigateSearchBox( | 
| 684     const v8::Arguments& args) { | 694     const v8::Arguments& args) { | 
| 685   content::RenderView* render_view = GetRenderView(); | 695   content::RenderView* render_view = GetRenderView(); | 
| 686   if (!render_view || !args.Length()) return v8::Undefined(); | 696   if (!render_view || !args.Length()) return v8::Undefined(); | 
| 687 | 697 | 
| 688   GURL destination_url; | 698   GURL destination_url; | 
| 689   content::PageTransition transition = content::PAGE_TRANSITION_TYPED; | 699   content::PageTransition transition = content::PAGE_TRANSITION_TYPED; | 
| 690   if (args[0]->IsNumber()) { | 700   if (args[0]->IsNumber()) { | 
| 691     const InstantAutocompleteResult* result = SearchBox::Get(render_view)-> | 701     const InstantAutocompleteResult* result = SearchBox::Get(render_view)-> | 
| 692         GetAutocompleteResultWithId(args[0]->Uint32Value()); | 702         GetAutocompleteResultWithId(args[0]->Uint32Value()); | 
| 693     if (result) { | 703     if (result) { | 
| 694       destination_url = GURL(result->destination_url); | 704       destination_url = GURL(result->destination_url); | 
| 695       transition = result->transition; | 705       transition = result->transition; | 
| 696     } | 706     } | 
| 697   } else { | 707   } else { | 
| 698     destination_url = GURL(V8ValueToUTF16(args[0])); | 708     destination_url = GURL(V8ValueToUTF16(args[0])); | 
| 699   } | 709   } | 
| 700 | 710 | 
|  | 711   DVLOG(1) << render_view << " NavigateSearchBox: " << destination_url; | 
|  | 712 | 
| 701   // Navigate the main frame. | 713   // Navigate the main frame. | 
| 702   if (destination_url.is_valid()) { | 714   if (destination_url.is_valid()) { | 
| 703     WindowOpenDisposition disposition = CURRENT_TAB; | 715     WindowOpenDisposition disposition = CURRENT_TAB; | 
|  | 716     if (args[1]->Uint32Value() == 2) | 
|  | 717       disposition = NEW_BACKGROUND_TAB; | 
|  | 718     SearchBox::Get(render_view)->NavigateToURL( | 
|  | 719         destination_url, transition, disposition); | 
|  | 720   } | 
|  | 721   return v8::Undefined(); | 
|  | 722 } | 
|  | 723 | 
|  | 724 // static | 
|  | 725 v8::Handle<v8::Value> SearchBoxExtensionWrapper::NavigateNewTabPage( | 
|  | 726     const v8::Arguments& args) { | 
|  | 727   content::RenderView* render_view = GetRenderView(); | 
|  | 728   if (!render_view || !args.Length()) return v8::Undefined(); | 
|  | 729 | 
|  | 730   GURL destination_url; | 
|  | 731   content::PageTransition transition = content::PAGE_TRANSITION_TYPED; | 
|  | 732   if (args[0]->IsNumber()) { | 
|  | 733     destination_url = GURL(SearchBox::Get(render_view)->RestrictedIdToURL( | 
|  | 734         args[0]->Uint32Value())); | 
|  | 735   } else { | 
|  | 736     destination_url = GURL(V8ValueToUTF16(args[0])); | 
|  | 737   } | 
|  | 738 | 
|  | 739   DVLOG(1) << render_view << " NavigateNewTabPage: " << destination_url; | 
|  | 740 | 
|  | 741   // Navigate the main frame. | 
|  | 742   if (destination_url.is_valid()) { | 
|  | 743     WindowOpenDisposition disposition = CURRENT_TAB; | 
| 704     if (args[1]->Uint32Value() == 2) | 744     if (args[1]->Uint32Value() == 2) | 
| 705       disposition = NEW_BACKGROUND_TAB; | 745       disposition = NEW_BACKGROUND_TAB; | 
| 706     SearchBox::Get(render_view)->NavigateToURL( | 746     SearchBox::Get(render_view)->NavigateToURL( | 
| 707         destination_url, transition, disposition); | 747         destination_url, transition, disposition); | 
| 708   } | 748   } | 
| 709   return v8::Undefined(); | 749   return v8::Undefined(); | 
| 710 } | 750 } | 
| 711 | 751 | 
| 712 // static | 752 // static | 
|  | 753 v8::Handle<v8::Value> SearchBoxExtensionWrapper::NavigateContentWindow( | 
|  | 754     const v8::Arguments& args) { | 
|  | 755   content::RenderView* render_view = GetRenderView(); | 
|  | 756   if (!render_view || !args.Length()) return v8::Undefined(); | 
|  | 757 | 
|  | 758   DVLOG(1) << render_view << " NavigateContentWindow; query=" | 
|  | 759            << SearchBox::Get(render_view)->query(); | 
|  | 760 | 
|  | 761   // If the query is blank and verbatim is false, this must be the NTP. If the | 
|  | 762   // user were clicking on an autocomplete suggestion, either the query would | 
|  | 763   // be non-blank, or it would be blank due to SetQueryFromAutocompleteResult() | 
|  | 764   // but verbatim would be true. | 
|  | 765   if (SearchBox::Get(render_view)->query().empty() && | 
|  | 766       !SearchBox::Get(render_view)->verbatim()) | 
|  | 767     return NavigateNewTabPage(args); | 
|  | 768   return NavigateSearchBox(args); | 
|  | 769 } | 
|  | 770 | 
|  | 771 // static | 
| 713 v8::Handle<v8::Value> SearchBoxExtensionWrapper::SetSuggestions( | 772 v8::Handle<v8::Value> SearchBoxExtensionWrapper::SetSuggestions( | 
| 714     const v8::Arguments& args) { | 773     const v8::Arguments& args) { | 
| 715   content::RenderView* render_view = GetRenderView(); | 774   content::RenderView* render_view = GetRenderView(); | 
| 716   if (!render_view || !args.Length()) return v8::Undefined(); | 775   if (!render_view || !args.Length()) return v8::Undefined(); | 
| 717 | 776 | 
| 718   DVLOG(1) << render_view << " SetSuggestions"; | 777   DVLOG(1) << render_view << " SetSuggestions"; | 
| 719   v8::Handle<v8::Object> suggestion_json = args[0]->ToObject(); | 778   v8::Handle<v8::Object> suggestion_json = args[0]->ToObject(); | 
| 720 | 779 | 
| 721   InstantCompleteBehavior behavior = INSTANT_COMPLETE_NOW; | 780   InstantCompleteBehavior behavior = INSTANT_COMPLETE_NOW; | 
| 722   InstantSuggestionType type = INSTANT_SUGGESTION_SEARCH; | 781   InstantSuggestionType type = INSTANT_SUGGESTION_SEARCH; | 
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 876 | 935 | 
| 877   DVLOG(1) << render_view << " GetMostVisitedItems"; | 936   DVLOG(1) << render_view << " GetMostVisitedItems"; | 
| 878 | 937 | 
| 879   const std::vector<MostVisitedItem>& items = | 938   const std::vector<MostVisitedItem>& items = | 
| 880       SearchBox::Get(render_view)->GetMostVisitedItems(); | 939       SearchBox::Get(render_view)->GetMostVisitedItems(); | 
| 881   v8::Handle<v8::Array> items_array = v8::Array::New(items.size()); | 940   v8::Handle<v8::Array> items_array = v8::Array::New(items.size()); | 
| 882   for (size_t i = 0; i < items.size(); ++i) { | 941   for (size_t i = 0; i < items.size(); ++i) { | 
| 883 | 942 | 
| 884     const string16 url = UTF8ToUTF16(items[i].url.spec()); | 943     const string16 url = UTF8ToUTF16(items[i].url.spec()); | 
| 885     const string16 host = UTF8ToUTF16(items[i].url.host()); | 944     const string16 host = UTF8ToUTF16(items[i].url.host()); | 
| 886     int restrict_id = | 945     int most_visited_item_id = | 
| 887         SearchBox::Get(render_view)->UrlToRestrictedId(url); | 946         SearchBox::Get(render_view)->UrlToRestrictedId(url); | 
| 888 | 947 | 
| 889     // We set the "dir" attribute of the title, so that in RTL locales, a LTR | 948     // We set the "dir" attribute of the title, so that in RTL locales, a LTR | 
| 890     // title is rendered left-to-right and truncated from the right. For | 949     // title is rendered left-to-right and truncated from the right. For | 
| 891     // example, the title of http://msdn.microsoft.com/en-us/default.aspx is | 950     // example, the title of http://msdn.microsoft.com/en-us/default.aspx is | 
| 892     // "MSDN: Microsoft developer network". In RTL locales, in the New Tab | 951     // "MSDN: Microsoft developer network". In RTL locales, in the New Tab | 
| 893     // page, if the "dir" of this title is not specified, it takes Chrome UI's | 952     // page, if the "dir" of this title is not specified, it takes Chrome UI's | 
| 894     // directionality. So the title will be truncated as "soft developer | 953     // directionality. So the title will be truncated as "soft developer | 
| 895     // network". Setting the "dir" attribute as "ltr" renders the truncated | 954     // network". Setting the "dir" attribute as "ltr" renders the truncated | 
| 896     // title as "MSDN: Microsoft D...". As another example, the title of | 955     // title as "MSDN: Microsoft D...". As another example, the title of | 
| 897     // http://yahoo.com is "Yahoo!". In RTL locales, in the New Tab page, the | 956     // http://yahoo.com is "Yahoo!". In RTL locales, in the New Tab page, the | 
| 898     // title will be rendered as "!Yahoo" if its "dir" attribute is not set to | 957     // title will be rendered as "!Yahoo" if its "dir" attribute is not set to | 
| 899     // "ltr". | 958     // "ltr". | 
| 900     std::string direction; | 959     std::string direction; | 
| 901     if (base::i18n::StringContainsStrongRTLChars(items[i].title)) | 960     if (base::i18n::StringContainsStrongRTLChars(items[i].title)) | 
| 902       direction = kRTLHtmlTextDirection; | 961       direction = kRTLHtmlTextDirection; | 
| 903     else | 962     else | 
| 904       direction = kLTRHtmlTextDirection; | 963       direction = kLTRHtmlTextDirection; | 
| 905 | 964 | 
| 906     string16 title = items[i].title; | 965     string16 title = items[i].title; | 
| 907     if (title.empty()) | 966     if (title.empty()) | 
| 908       title = url; | 967       title = url; | 
| 909 | 968 | 
| 910     v8::Handle<v8::Object> item = v8::Object::New(); | 969     v8::Handle<v8::Object> item = v8::Object::New(); | 
| 911     item->Set(v8::String::New("rid"), | 970     item->Set(v8::String::New("rid"), | 
| 912               v8::Int32::New(restrict_id)); | 971               v8::Int32::New(most_visited_item_id)); | 
| 913     item->Set(v8::String::New("thumbnailUrl"), | 972     item->Set(v8::String::New("thumbnailUrl"), | 
| 914               UTF16ToV8String(SearchBox::Get(render_view)-> | 973               UTF16ToV8String(SearchBox::Get(render_view)-> | 
| 915                               GenerateThumbnailUrl(restrict_id))); | 974                               GenerateThumbnailUrl(most_visited_item_id))); | 
| 916     item->Set(v8::String::New("faviconUrl"), | 975     item->Set(v8::String::New("faviconUrl"), | 
| 917               UTF16ToV8String(SearchBox::Get(render_view)-> | 976               UTF16ToV8String(SearchBox::Get(render_view)-> | 
| 918                               GenerateFaviconUrl(restrict_id))); | 977                               GenerateFaviconUrl(most_visited_item_id))); | 
| 919     item->Set(v8::String::New("title"), | 978     item->Set(v8::String::New("title"), | 
| 920               UTF16ToV8String(title)); | 979               UTF16ToV8String(title)); | 
| 921     item->Set(v8::String::New("domain"), UTF16ToV8String(host)); | 980     item->Set(v8::String::New("domain"), UTF16ToV8String(host)); | 
| 922     item->Set(v8::String::New("direction"), | 981     item->Set(v8::String::New("direction"), | 
| 923               UTF8ToV8String(direction)); | 982               UTF8ToV8String(direction)); | 
| 924 | 983 | 
| 925     items_array->Set(i, item); | 984     items_array->Set(i, item); | 
| 926   } | 985   } | 
| 927   return items_array; | 986   return items_array; | 
| 928 } | 987 } | 
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 1067 void SearchBoxExtension::DispatchThemeChange(WebKit::WebFrame* frame) { | 1126 void SearchBoxExtension::DispatchThemeChange(WebKit::WebFrame* frame) { | 
| 1068   Dispatch(frame, kDispatchThemeChangeEventScript); | 1127   Dispatch(frame, kDispatchThemeChangeEventScript); | 
| 1069 } | 1128 } | 
| 1070 | 1129 | 
| 1071 // static | 1130 // static | 
| 1072 void SearchBoxExtension::DispatchMostVisitedChanged( | 1131 void SearchBoxExtension::DispatchMostVisitedChanged( | 
| 1073     WebKit::WebFrame* frame) { | 1132     WebKit::WebFrame* frame) { | 
| 1074   Dispatch(frame, kDispatchMostVisitedChangedScript); | 1133   Dispatch(frame, kDispatchMostVisitedChangedScript); | 
| 1075 } | 1134 } | 
| 1076 }  // namespace extensions_v8 | 1135 }  // namespace extensions_v8 | 
| OLD | NEW | 
|---|