| 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 45 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|    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 most_visited_item_id) { | 
 |    67   return UTF8ToV8String( | 
 |    68       StringPrintf("chrome-search://thumb/%s", | 
 |    69                    base::Uint64ToString(most_visited_item_id).c_str())); | 
 |    70 } | 
 |    71  | 
 |    72 v8::Handle<v8::String> GenerateFaviconURL(uint64 most_visited_item_id) { | 
 |    73   return UTF8ToV8String( | 
 |    74       StringPrintf("chrome-search://favicon/%s", | 
 |    75                    base::Uint64ToString(most_visited_item_id).c_str())); | 
 |    76 } | 
 |    77  | 
 |    78 const GURL MostVisitedItemIDToURL( | 
 |    79     const std::vector<InstantMostVisitedItem>& most_visited_items, | 
 |    80     uint64 most_visited_item_id) { | 
 |    81   for (size_t i = 0; i < most_visited_items.size(); ++i) { | 
 |    82     if (most_visited_items[i].most_visited_item_id == most_visited_item_id) | 
 |    83       return most_visited_items[i].url; | 
 |    84   } | 
 |    85   return GURL(); | 
 |    86 } | 
 |    87  | 
|    66 }  // namespace |    88 }  // namespace | 
|    67  |    89  | 
|    68 namespace extensions_v8 { |    90 namespace extensions_v8 { | 
|    69  |    91  | 
|    70 static const char kSearchBoxExtensionName[] = "v8/EmbeddedSearch"; |    92 static const char kSearchBoxExtensionName[] = "v8/EmbeddedSearch"; | 
|    71  |    93  | 
|    72 static const char kDispatchChangeEventScript[] = |    94 static const char kDispatchChangeEventScript[] = | 
|    73     "if (window.chrome &&" |    95     "if (window.chrome &&" | 
|    74     "    window.chrome.embeddedSearch &&" |    96     "    window.chrome.embeddedSearch &&" | 
|    75     "    window.chrome.embeddedSearch.searchBox &&" |    97     "    window.chrome.embeddedSearch.searchBox &&" | 
| (...skipping 647 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|   723  |   745  | 
|   724 // static |   746 // static | 
|   725 v8::Handle<v8::Value> SearchBoxExtensionWrapper::NavigateNewTabPage( |   747 v8::Handle<v8::Value> SearchBoxExtensionWrapper::NavigateNewTabPage( | 
|   726     const v8::Arguments& args) { |   748     const v8::Arguments& args) { | 
|   727   content::RenderView* render_view = GetRenderView(); |   749   content::RenderView* render_view = GetRenderView(); | 
|   728   if (!render_view || !args.Length()) return v8::Undefined(); |   750   if (!render_view || !args.Length()) return v8::Undefined(); | 
|   729  |   751  | 
|   730   GURL destination_url; |   752   GURL destination_url; | 
|   731   content::PageTransition transition = content::PAGE_TRANSITION_TYPED; |   753   content::PageTransition transition = content::PAGE_TRANSITION_TYPED; | 
|   732   if (args[0]->IsNumber()) { |   754   if (args[0]->IsNumber()) { | 
|   733     destination_url = GURL(SearchBox::Get(render_view)->MostVisitedItemIDToURL( |   755     destination_url = MostVisitedItemIDToURL( | 
|   734         args[0]->Uint32Value())); |   756         SearchBox::Get(render_view)->GetMostVisitedItems(), | 
 |   757         args[0]->Uint32Value()); | 
|   735   } else { |   758   } else { | 
|   736     destination_url = GURL(V8ValueToUTF16(args[0])); |   759     destination_url = GURL(V8ValueToUTF16(args[0])); | 
|   737   } |   760   } | 
|   738  |   761  | 
|   739   DVLOG(1) << render_view << " NavigateNewTabPage: " << destination_url; |   762   DVLOG(1) << render_view << " NavigateNewTabPage: " << destination_url; | 
|   740  |   763  | 
|   741   // Navigate the main frame. |   764   // Navigate the main frame. | 
|   742   if (destination_url.is_valid()) { |   765   if (destination_url.is_valid()) { | 
|   743     WindowOpenDisposition disposition = CURRENT_TAB; |   766     WindowOpenDisposition disposition = CURRENT_TAB; | 
|   744     if (args[1]->Uint32Value() == 2) |   767     if (args[1]->Uint32Value() == 2) | 
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|   924  |   947  | 
|   925   SearchBox::Get(render_view)->ShowInstantOverlay(reason, height, units); |   948   SearchBox::Get(render_view)->ShowInstantOverlay(reason, height, units); | 
|   926  |   949  | 
|   927   return v8::Undefined(); |   950   return v8::Undefined(); | 
|   928 } |   951 } | 
|   929  |   952  | 
|   930 // static |   953 // static | 
|   931 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetMostVisitedItems( |   954 v8::Handle<v8::Value> SearchBoxExtensionWrapper::GetMostVisitedItems( | 
|   932     const v8::Arguments& args) { |   955     const v8::Arguments& args) { | 
|   933   content::RenderView* render_view = GetRenderView(); |   956   content::RenderView* render_view = GetRenderView(); | 
|   934   if (!render_view) return v8::Undefined(); |   957   if (!render_view) | 
|   935  |   958     return v8::Undefined(); | 
|   936   DVLOG(1) << render_view << " GetMostVisitedItems"; |   959   DVLOG(1) << render_view << " GetMostVisitedItems"; | 
|   937  |   960  | 
|   938   const std::vector<MostVisitedItem>& items = |   961   const SearchBox* search_box = SearchBox::Get(render_view); | 
|   939       SearchBox::Get(render_view)->GetMostVisitedItems(); |  | 
|   940   v8::Handle<v8::Array> items_array = v8::Array::New(items.size()); |  | 
|   941   for (size_t i = 0; i < items.size(); ++i) { |  | 
|   942  |   962  | 
|   943     const string16 url = UTF8ToUTF16(items[i].url.spec()); |   963   const std::vector<InstantMostVisitedItem>& instant_mv_items = | 
|   944     const string16 host = UTF8ToUTF16(items[i].url.host()); |   964       search_box->GetMostVisitedItems(); | 
|   945     int most_visited_item_id = |   965   v8::Handle<v8::Array> v8_mv_items = v8::Array::New(instant_mv_items.size()); | 
|   946         SearchBox::Get(render_view)->URLToMostVisitedItemID(url); |   966   for (size_t i = 0; i < instant_mv_items.size(); ++i) { | 
|   947  |  | 
|   948     // We set the "dir" attribute of the title, so that in RTL locales, a LTR |   967     // We set the "dir" attribute of the title, so that in RTL locales, a LTR | 
|   949     // title is rendered left-to-right and truncated from the right. For |   968     // title is rendered left-to-right and truncated from the right. For | 
|   950     // example, the title of http://msdn.microsoft.com/en-us/default.aspx is |   969     // example, the title of http://msdn.microsoft.com/en-us/default.aspx is | 
|   951     // "MSDN: Microsoft developer network". In RTL locales, in the New Tab |   970     // "MSDN: Microsoft developer network". In RTL locales, in the New Tab | 
|   952     // page, if the "dir" of this title is not specified, it takes Chrome UI's |   971     // page, if the "dir" of this title is not specified, it takes Chrome UI's | 
|   953     // directionality. So the title will be truncated as "soft developer |   972     // directionality. So the title will be truncated as "soft developer | 
|   954     // network". Setting the "dir" attribute as "ltr" renders the truncated |   973     // network". Setting the "dir" attribute as "ltr" renders the truncated | 
|   955     // title as "MSDN: Microsoft D...". As another example, the title of |   974     // title as "MSDN: Microsoft D...". As another example, the title of | 
|   956     // http://yahoo.com is "Yahoo!". In RTL locales, in the New Tab page, the |   975     // http://yahoo.com is "Yahoo!". In RTL locales, in the New Tab page, the | 
|   957     // title will be rendered as "!Yahoo" if its "dir" attribute is not set to |   976     // title will be rendered as "!Yahoo" if its "dir" attribute is not set to | 
|   958     // "ltr". |   977     // "ltr". | 
|   959     std::string direction; |   978     std::string direction; | 
|   960     if (base::i18n::StringContainsStrongRTLChars(items[i].title)) |   979     if (base::i18n::StringContainsStrongRTLChars(instant_mv_items[i].title)) | 
|   961       direction = kRTLHtmlTextDirection; |   980       direction = kRTLHtmlTextDirection; | 
|   962     else |   981     else | 
|   963       direction = kLTRHtmlTextDirection; |   982       direction = kLTRHtmlTextDirection; | 
|   964  |   983  | 
|   965     string16 title = items[i].title; |   984     string16 title = instant_mv_items[i].title; | 
|   966     if (title.empty()) |   985     if (title.empty()) | 
|   967       title = url; |   986       title = UTF8ToUTF16(instant_mv_items[i].url.spec()); | 
|   968  |   987  | 
|   969     v8::Handle<v8::Object> item = v8::Object::New(); |   988     v8::Handle<v8::Object> item = v8::Object::New(); | 
|   970     item->Set(v8::String::New("rid"), |   989     item->Set(v8::String::New("rid"), | 
|   971               v8::Int32::New(most_visited_item_id)); |   990               v8::Int32::New(instant_mv_items[i].most_visited_item_id)); | 
|   972     item->Set(v8::String::New("thumbnailUrl"), |   991     item->Set(v8::String::New("thumbnailUrl"), | 
|   973               UTF16ToV8String(SearchBox::Get(render_view)-> |   992               GenerateThumbnailURL(instant_mv_items[i].most_visited_item_id)); | 
|   974                               GenerateThumbnailUrl(most_visited_item_id))); |  | 
|   975     item->Set(v8::String::New("faviconUrl"), |   993     item->Set(v8::String::New("faviconUrl"), | 
|   976               UTF16ToV8String(SearchBox::Get(render_view)-> |   994               GenerateFaviconURL(instant_mv_items[i].most_visited_item_id)); | 
|   977                               GenerateFaviconUrl(most_visited_item_id))); |  | 
|   978     item->Set(v8::String::New("title"), |   995     item->Set(v8::String::New("title"), | 
|   979               UTF16ToV8String(title)); |   996               UTF16ToV8String(title)); | 
|   980     item->Set(v8::String::New("domain"), UTF16ToV8String(host)); |   997     item->Set(v8::String::New("domain"), | 
|   981     item->Set(v8::String::New("direction"), |   998               UTF8ToV8String(instant_mv_items[i].url.host())); | 
|   982               UTF8ToV8String(direction)); |   999     item->Set(v8::String::New("direction"), UTF8ToV8String(direction)); | 
|   983  |  1000  | 
|   984     items_array->Set(i, item); |  1001     v8_mv_items->Set(i, item); | 
|   985   } |  1002   } | 
|   986   return items_array; |  1003   return v8_mv_items; | 
|   987 } |  1004 } | 
|   988  |  1005  | 
|   989 // static |  1006 // static | 
|   990 v8::Handle<v8::Value> SearchBoxExtensionWrapper::DeleteMostVisitedItem( |  1007 v8::Handle<v8::Value> SearchBoxExtensionWrapper::DeleteMostVisitedItem( | 
|   991     const v8::Arguments& args) { |  1008     const v8::Arguments& args) { | 
|   992   content::RenderView* render_view = GetRenderView(); |  1009   content::RenderView* render_view = GetRenderView(); | 
|   993   if (!render_view || !args.Length()) return v8::Undefined(); |  1010   if (!render_view || !args.Length()) return v8::Undefined(); | 
|   994  |  1011  | 
|   995   DVLOG(1) << render_view << " DeleteMostVisitedItem"; |  1012   DVLOG(1) << render_view << " DeleteMostVisitedItem"; | 
|   996   SearchBox::Get(render_view)->DeleteMostVisitedItem(args[0]->IntegerValue()); |  1013   SearchBox::Get(render_view)->DeleteMostVisitedItem(args[0]->IntegerValue()); | 
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  1126 void SearchBoxExtension::DispatchThemeChange(WebKit::WebFrame* frame) { |  1143 void SearchBoxExtension::DispatchThemeChange(WebKit::WebFrame* frame) { | 
|  1127   Dispatch(frame, kDispatchThemeChangeEventScript); |  1144   Dispatch(frame, kDispatchThemeChangeEventScript); | 
|  1128 } |  1145 } | 
|  1129  |  1146  | 
|  1130 // static |  1147 // static | 
|  1131 void SearchBoxExtension::DispatchMostVisitedChanged( |  1148 void SearchBoxExtension::DispatchMostVisitedChanged( | 
|  1132     WebKit::WebFrame* frame) { |  1149     WebKit::WebFrame* frame) { | 
|  1133   Dispatch(frame, kDispatchMostVisitedChangedScript); |  1150   Dispatch(frame, kDispatchMostVisitedChangedScript); | 
|  1134 } |  1151 } | 
|  1135 }  // namespace extensions_v8 |  1152 }  // namespace extensions_v8 | 
| OLD | NEW |