Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(71)

Side by Side Diff: chrome/renderer/searchbox/searchbox_extension.cc

Issue 12732005: Most visited thumbnails and favicons need id-based urls (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Adds proper handling of ThumbnailSource Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 // Converts std::string to V8 String. 56 // Converts std::string to V8 String.
57 v8::Handle<v8::String> UTF8ToV8String(const std::string& s) { 57 v8::Handle<v8::String> UTF8ToV8String(const std::string& s) {
58 return v8::String::New(s.data(), s.size()); 58 return v8::String::New(s.data(), s.size());
59 } 59 }
60 60
61 void Dispatch(WebKit::WebFrame* frame, const WebKit::WebString& script) { 61 void Dispatch(WebKit::WebFrame* frame, const WebKit::WebString& script) {
62 if (!frame) return; 62 if (!frame) return;
63 frame->executeScript(WebKit::WebScriptSource(script)); 63 frame->executeScript(WebKit::WebScriptSource(script));
64 } 64 }
65 65
66 v8::Handle<v8::String> GenerateThumbnailURL(uint64 restricted_id) {
67 std::ostringstream ostr;
68 ostr << "chrome-search://thumb/" << restricted_id;
palmer 2013/03/11 20:42:31 This seems kind of heavy-weight (including having
dhollowa 2013/03/11 23:27:59 Done. I was blindly copy/pasting...
69 GURL url = GURL(ostr.str());
70 return UTF8ToV8String(url.spec());
71 }
72
73 v8::Handle<v8::String> GenerateFaviconURL(uint64 restricted_id) {
74 std::ostringstream ostr;
75 ostr << "chrome-search://favicon/" << restricted_id;
76 GURL url = GURL(ostr.str());
77 return UTF8ToV8String(url.spec());
78 }
79
66 } // namespace 80 } // namespace
67 81
68 namespace extensions_v8 { 82 namespace extensions_v8 {
69 83
70 static const char kSearchBoxExtensionName[] = "v8/EmbeddedSearch"; 84 static const char kSearchBoxExtensionName[] = "v8/EmbeddedSearch";
71 85
72 static const char kDispatchChangeEventScript[] = 86 static const char kDispatchChangeEventScript[] =
73 "if (window.chrome &&" 87 "if (window.chrome &&"
74 " window.chrome.embeddedSearch &&" 88 " window.chrome.embeddedSearch &&"
75 " window.chrome.embeddedSearch.searchBox &&" 89 " window.chrome.embeddedSearch.searchBox &&"
(...skipping 789 matching lines...) Expand 10 before | Expand all | Expand 10 after
865 879
866 SearchBox::Get(render_view)->ShowInstantOverlay(reason, height, units); 880 SearchBox::Get(render_view)->ShowInstantOverlay(reason, height, units);
867 881
868 return v8::Undefined(); 882 return v8::Undefined();
869 } 883 }
870 884
871 // static 885 // static
872 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetMostVisitedItems( 886 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetMostVisitedItems(
873 const v8::Arguments& args) { 887 const v8::Arguments& args) {
874 content::RenderView* render_view = GetRenderView(); 888 content::RenderView* render_view = GetRenderView();
875 if (!render_view) return v8::Undefined(); 889 if (!render_view)
876 890 return v8::Undefined();
877 DVLOG(1) << render_view << " GetMostVisitedItems"; 891 DVLOG(1) << render_view << " GetMostVisitedItems";
878 892
879 const std::vector<MostVisitedItem>& items = 893 SearchBox* search_box = SearchBox::Get(render_view);
palmer 2013/03/11 20:42:31 Can |search_box| be declared const?
dhollowa 2013/03/11 23:27:59 Yes. Done.
880 SearchBox::Get(render_view)->GetMostVisitedItems();
881 v8::Handle<v8::Array> items_array = v8::Array::New(items.size());
882 for (size_t i = 0; i < items.size(); ++i) {
883 894
884 const string16 url = UTF8ToUTF16(items[i].url.spec()); 895 const std::vector<InstantMostVisitedItem>& instant_mv_items =
885 const string16 host = UTF8ToUTF16(items[i].url.host()); 896 search_box->GetMostVisitedItems();
886 int restrict_id = 897 v8::Handle<v8::Array> v8_mv_items = v8::Array::New(instant_mv_items.size());
887 SearchBox::Get(render_view)->UrlToRestrictedId(url); 898 for (size_t i = 0; i < instant_mv_items.size(); ++i) {
888
889 // We set the "dir" attribute of the title, so that in RTL locales, a LTR 899 // 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 900 // 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 901 // 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 902 // "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 903 // 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 904 // directionality. So the title will be truncated as "soft developer
895 // network". Setting the "dir" attribute as "ltr" renders the truncated 905 // network". Setting the "dir" attribute as "ltr" renders the truncated
896 // title as "MSDN: Microsoft D...". As another example, the title of 906 // 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 907 // 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 908 // title will be rendered as "!Yahoo" if its "dir" attribute is not set to
899 // "ltr". 909 // "ltr".
900 std::string direction; 910 std::string direction;
901 if (base::i18n::StringContainsStrongRTLChars(items[i].title)) 911 if (base::i18n::StringContainsStrongRTLChars(instant_mv_items[i].title))
902 direction = kRTLHtmlTextDirection; 912 direction = kRTLHtmlTextDirection;
903 else 913 else
904 direction = kLTRHtmlTextDirection; 914 direction = kLTRHtmlTextDirection;
905 915
906 string16 title = items[i].title; 916 string16 title = instant_mv_items[i].title;
907 if (title.empty()) 917 if (title.empty())
908 title = url; 918 title = UTF8ToUTF16(instant_mv_items[i].url.spec());
909 919
910 v8::Handle<v8::Object> item = v8::Object::New(); 920 v8::Handle<v8::Object> item = v8::Object::New();
911 item->Set(v8::String::New("rid"), 921 item->Set(v8::String::New("rid"),
912 v8::Int32::New(restrict_id)); 922 v8::Int32::New(instant_mv_items[i].restricted_id));
913 item->Set(v8::String::New("thumbnailUrl"), 923 item->Set(v8::String::New("thumbnailUrl"),
914 UTF16ToV8String(SearchBox::Get(render_view)-> 924 GenerateThumbnailURL(instant_mv_items[i].restricted_id));
915 GenerateThumbnailUrl(restrict_id)));
916 item->Set(v8::String::New("faviconUrl"), 925 item->Set(v8::String::New("faviconUrl"),
917 UTF16ToV8String(SearchBox::Get(render_view)-> 926 GenerateFaviconURL(instant_mv_items[i].restricted_id));
918 GenerateFaviconUrl(restrict_id)));
919 item->Set(v8::String::New("title"), 927 item->Set(v8::String::New("title"),
920 UTF16ToV8String(title)); 928 UTF16ToV8String(title));
921 item->Set(v8::String::New("domain"), UTF16ToV8String(host)); 929 item->Set(v8::String::New("domain"),
922 item->Set(v8::String::New("direction"), 930 UTF8ToV8String(instant_mv_items[i].url.host()));
923 UTF8ToV8String(direction)); 931 item->Set(v8::String::New("direction"), UTF8ToV8String(direction));
924 932
925 items_array->Set(i, item); 933 v8_mv_items->Set(i, item);
926 } 934 }
927 return items_array; 935 return v8_mv_items;
928 } 936 }
929 937
930 // static 938 // static
931 v8::Handle<v8::Value> SearchBoxExtensionWrapper::DeleteMostVisitedItem( 939 v8::Handle<v8::Value> SearchBoxExtensionWrapper::DeleteMostVisitedItem(
932 const v8::Arguments& args) { 940 const v8::Arguments& args) {
933 content::RenderView* render_view = GetRenderView(); 941 content::RenderView* render_view = GetRenderView();
934 if (!render_view || !args.Length()) return v8::Undefined(); 942 if (!render_view || !args.Length()) return v8::Undefined();
935 943
936 DVLOG(1) << render_view << " DeleteMostVisitedItem"; 944 DVLOG(1) << render_view << " DeleteMostVisitedItem";
937 SearchBox::Get(render_view)->DeleteMostVisitedItem(args[0]->IntegerValue()); 945 SearchBox::Get(render_view)->DeleteMostVisitedItem(args[0]->IntegerValue());
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
1067 void SearchBoxExtension::DispatchThemeChange(WebKit::WebFrame* frame) { 1075 void SearchBoxExtension::DispatchThemeChange(WebKit::WebFrame* frame) {
1068 Dispatch(frame, kDispatchThemeChangeEventScript); 1076 Dispatch(frame, kDispatchThemeChangeEventScript);
1069 } 1077 }
1070 1078
1071 // static 1079 // static
1072 void SearchBoxExtension::DispatchMostVisitedChanged( 1080 void SearchBoxExtension::DispatchMostVisitedChanged(
1073 WebKit::WebFrame* frame) { 1081 WebKit::WebFrame* frame) {
1074 Dispatch(frame, kDispatchMostVisitedChangedScript); 1082 Dispatch(frame, kDispatchMostVisitedChangedScript);
1075 } 1083 }
1076 } // namespace extensions_v8 1084 } // namespace extensions_v8
OLDNEW
« chrome/browser/ui/webui/ntp/thumbnail_source.cc ('K') | « chrome/renderer/searchbox/searchbox.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698