| 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/json/string_escape.h" | 8 #include "base/json/string_escape.h" |
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 const char kCSSBackgroundRepeat[] = "repeat"; | 49 const char kCSSBackgroundRepeat[] = "repeat"; |
| 50 | 50 |
| 51 const char kThemeAttributionFormat[] = "-webkit-image-set(" | 51 const char kThemeAttributionFormat[] = "-webkit-image-set(" |
| 52 "url(chrome-search://theme/IDR_THEME_NTP_ATTRIBUTION?%s) 1x, " | 52 "url(chrome-search://theme/IDR_THEME_NTP_ATTRIBUTION?%s) 1x, " |
| 53 "url(chrome-search://theme/IDR_THEME_NTP_ATTRIBUTION@2x?%s) 2x)"; | 53 "url(chrome-search://theme/IDR_THEME_NTP_ATTRIBUTION@2x?%s) 2x)"; |
| 54 | 54 |
| 55 const char kLTRHtmlTextDirection[] = "ltr"; | 55 const char kLTRHtmlTextDirection[] = "ltr"; |
| 56 const char kRTLHtmlTextDirection[] = "rtl"; | 56 const char kRTLHtmlTextDirection[] = "rtl"; |
| 57 | 57 |
| 58 // Converts a V8 value to a string16. | 58 // Converts a V8 value to a string16. |
| 59 string16 V8ValueToUTF16(v8::Handle<v8::Value> v) { | 59 base::string16 V8ValueToUTF16(v8::Handle<v8::Value> v) { |
| 60 v8::String::Value s(v); | 60 v8::String::Value s(v); |
| 61 return string16(reinterpret_cast<const char16*>(*s), s.length()); | 61 return base::string16(reinterpret_cast<const char16*>(*s), s.length()); |
| 62 } | 62 } |
| 63 | 63 |
| 64 // Converts string16 to V8 String. | 64 // Converts string16 to V8 String. |
| 65 v8::Handle<v8::String> UTF16ToV8String(v8::Isolate* isolate, | 65 v8::Handle<v8::String> UTF16ToV8String(v8::Isolate* isolate, |
| 66 const string16& s) { | 66 const base::string16& s) { |
| 67 return v8::String::NewFromTwoByte(isolate, | 67 return v8::String::NewFromTwoByte(isolate, |
| 68 reinterpret_cast<const uint16_t*>(s.data()), | 68 reinterpret_cast<const uint16_t*>(s.data()), |
| 69 v8::String::kNormalString, | 69 v8::String::kNormalString, |
| 70 s.size()); | 70 s.size()); |
| 71 } | 71 } |
| 72 | 72 |
| 73 // Converts std::string to V8 String. | 73 // Converts std::string to V8 String. |
| 74 v8::Handle<v8::String> UTF8ToV8String(v8::Isolate* isolate, | 74 v8::Handle<v8::String> UTF8ToV8String(v8::Isolate* isolate, |
| 75 const std::string& s) { | 75 const std::string& s) { |
| 76 return v8::String::NewFromUtf8( | 76 return v8::String::NewFromUtf8( |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 // title as "MSDN: Microsoft D...". As another example, the title of | 111 // title as "MSDN: Microsoft D...". As another example, the title of |
| 112 // http://yahoo.com is "Yahoo!". In RTL locales, in the New Tab page, the | 112 // http://yahoo.com is "Yahoo!". In RTL locales, in the New Tab page, the |
| 113 // title will be rendered as "!Yahoo" if its "dir" attribute is not set to | 113 // title will be rendered as "!Yahoo" if its "dir" attribute is not set to |
| 114 // "ltr". | 114 // "ltr". |
| 115 std::string direction; | 115 std::string direction; |
| 116 if (base::i18n::StringContainsStrongRTLChars(mv_item.title)) | 116 if (base::i18n::StringContainsStrongRTLChars(mv_item.title)) |
| 117 direction = kRTLHtmlTextDirection; | 117 direction = kRTLHtmlTextDirection; |
| 118 else | 118 else |
| 119 direction = kLTRHtmlTextDirection; | 119 direction = kLTRHtmlTextDirection; |
| 120 | 120 |
| 121 string16 title = mv_item.title; | 121 base::string16 title = mv_item.title; |
| 122 if (title.empty()) | 122 if (title.empty()) |
| 123 title = UTF8ToUTF16(mv_item.url.spec()); | 123 title = UTF8ToUTF16(mv_item.url.spec()); |
| 124 | 124 |
| 125 v8::Handle<v8::Object> obj = v8::Object::New(); | 125 v8::Handle<v8::Object> obj = v8::Object::New(); |
| 126 obj->Set(v8::String::NewFromUtf8(isolate, "renderViewId"), | 126 obj->Set(v8::String::NewFromUtf8(isolate, "renderViewId"), |
| 127 v8::Int32::New(isolate, render_view_id)); | 127 v8::Int32::New(isolate, render_view_id)); |
| 128 obj->Set(v8::String::NewFromUtf8(isolate, "rid"), | 128 obj->Set(v8::String::NewFromUtf8(isolate, "rid"), |
| 129 v8::Int32::New(isolate, restricted_id)); | 129 v8::Int32::New(isolate, restricted_id)); |
| 130 obj->Set(v8::String::NewFromUtf8(isolate, "thumbnailUrl"), | 130 obj->Set(v8::String::NewFromUtf8(isolate, "thumbnailUrl"), |
| 131 GenerateThumbnailURL(isolate, render_view_id, restricted_id)); | 131 GenerateThumbnailURL(isolate, render_view_id, restricted_id)); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 v8::Handle<v8::Array> color_array = v8::Array::New(isolate, 4); | 178 v8::Handle<v8::Array> color_array = v8::Array::New(isolate, 4); |
| 179 color_array->Set(0, v8::Int32::New(isolate, color.r)); | 179 color_array->Set(0, v8::Int32::New(isolate, color.r)); |
| 180 color_array->Set(1, v8::Int32::New(isolate, color.g)); | 180 color_array->Set(1, v8::Int32::New(isolate, color.g)); |
| 181 color_array->Set(2, v8::Int32::New(isolate, color.b)); | 181 color_array->Set(2, v8::Int32::New(isolate, color.b)); |
| 182 color_array->Set(3, v8::Int32::New(isolate, color.a)); | 182 color_array->Set(3, v8::Int32::New(isolate, color.a)); |
| 183 return color_array; | 183 return color_array; |
| 184 } | 184 } |
| 185 | 185 |
| 186 // Resolves a possibly relative URL using the current URL. | 186 // Resolves a possibly relative URL using the current URL. |
| 187 GURL ResolveURL(const GURL& current_url, | 187 GURL ResolveURL(const GURL& current_url, |
| 188 const string16& possibly_relative_url) { | 188 const base::string16& possibly_relative_url) { |
| 189 if (current_url.is_valid() && !possibly_relative_url.empty()) | 189 if (current_url.is_valid() && !possibly_relative_url.empty()) |
| 190 return current_url.Resolve(possibly_relative_url); | 190 return current_url.Resolve(possibly_relative_url); |
| 191 return GURL(possibly_relative_url); | 191 return GURL(possibly_relative_url); |
| 192 } | 192 } |
| 193 | 193 |
| 194 } // namespace internal | 194 } // namespace internal |
| 195 | 195 |
| 196 namespace extensions_v8 { | 196 namespace extensions_v8 { |
| 197 | 197 |
| 198 static const char kSearchBoxExtensionName[] = "v8/EmbeddedSearch"; | 198 static const char kSearchBoxExtensionName[] = "v8/EmbeddedSearch"; |
| (...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 450 bool SearchBoxExtension::PageSupportsInstant(blink::WebFrame* frame) { | 450 bool SearchBoxExtension::PageSupportsInstant(blink::WebFrame* frame) { |
| 451 if (!frame) return false; | 451 if (!frame) return false; |
| 452 v8::HandleScope handle_scope(v8::Isolate::GetCurrent()); | 452 v8::HandleScope handle_scope(v8::Isolate::GetCurrent()); |
| 453 v8::Handle<v8::Value> v = frame->executeScriptAndReturnValue( | 453 v8::Handle<v8::Value> v = frame->executeScriptAndReturnValue( |
| 454 blink::WebScriptSource(kSupportsInstantScript)); | 454 blink::WebScriptSource(kSupportsInstantScript)); |
| 455 return !v.IsEmpty() && v->BooleanValue(); | 455 return !v.IsEmpty() && v->BooleanValue(); |
| 456 } | 456 } |
| 457 | 457 |
| 458 // static | 458 // static |
| 459 void SearchBoxExtension::DispatchChromeIdentityCheckResult( | 459 void SearchBoxExtension::DispatchChromeIdentityCheckResult( |
| 460 blink::WebFrame* frame, const string16& identity, bool identity_match) { | 460 blink::WebFrame* frame, |
| 461 const base::string16& identity, |
| 462 bool identity_match) { |
| 461 std::string escaped_identity; | 463 std::string escaped_identity; |
| 462 base::JsonDoubleQuote(identity, true, &escaped_identity); | 464 base::JsonDoubleQuote(identity, true, &escaped_identity); |
| 463 blink::WebString script(UTF8ToUTF16(base::StringPrintf( | 465 blink::WebString script(UTF8ToUTF16(base::StringPrintf( |
| 464 kDispatchChromeIdentityCheckResult, | 466 kDispatchChromeIdentityCheckResult, |
| 465 escaped_identity.c_str(), | 467 escaped_identity.c_str(), |
| 466 identity_match ? "true" : "false"))); | 468 identity_match ? "true" : "false"))); |
| 467 Dispatch(frame, script); | 469 Dispatch(frame, script); |
| 468 } | 470 } |
| 469 | 471 |
| 470 // static | 472 // static |
| (...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 680 v8::Isolate* isolate = args.GetIsolate(); | 682 v8::Isolate* isolate = args.GetIsolate(); |
| 681 args.GetReturnValue().Set(GenerateMostVisitedItem( | 683 args.GetReturnValue().Set(GenerateMostVisitedItem( |
| 682 isolate, render_view->GetRoutingID(), restricted_id, mv_item)); | 684 isolate, render_view->GetRoutingID(), restricted_id, mv_item)); |
| 683 } | 685 } |
| 684 | 686 |
| 685 // static | 687 // static |
| 686 void SearchBoxExtensionWrapper::GetQuery( | 688 void SearchBoxExtensionWrapper::GetQuery( |
| 687 const v8::FunctionCallbackInfo<v8::Value>& args) { | 689 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 688 content::RenderView* render_view = GetRenderView(); | 690 content::RenderView* render_view = GetRenderView(); |
| 689 if (!render_view) return; | 691 if (!render_view) return; |
| 690 const string16& query = SearchBox::Get(render_view)->query(); | 692 const base::string16& query = SearchBox::Get(render_view)->query(); |
| 691 DVLOG(1) << render_view << " GetQuery: '" << query << "'"; | 693 DVLOG(1) << render_view << " GetQuery: '" << query << "'"; |
| 692 v8::Isolate* isolate = args.GetIsolate(); | 694 v8::Isolate* isolate = args.GetIsolate(); |
| 693 args.GetReturnValue().Set(UTF16ToV8String(isolate, query)); | 695 args.GetReturnValue().Set(UTF16ToV8String(isolate, query)); |
| 694 } | 696 } |
| 695 | 697 |
| 696 // static | 698 // static |
| 697 void SearchBoxExtensionWrapper::GetRightToLeft( | 699 void SearchBoxExtensionWrapper::GetRightToLeft( |
| 698 const v8::FunctionCallbackInfo<v8::Value>& args) { | 700 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 699 args.GetReturnValue().Set(base::i18n::IsRTL()); | 701 args.GetReturnValue().Set(base::i18n::IsRTL()); |
| 700 } | 702 } |
| (...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 943 // Check if the url is a rid | 945 // Check if the url is a rid |
| 944 if (args[0]->IsNumber()) { | 946 if (args[0]->IsNumber()) { |
| 945 InstantMostVisitedItem item; | 947 InstantMostVisitedItem item; |
| 946 if (SearchBox::Get(render_view)->GetMostVisitedItemWithID( | 948 if (SearchBox::Get(render_view)->GetMostVisitedItemWithID( |
| 947 args[0]->IntegerValue(), &item)) { | 949 args[0]->IntegerValue(), &item)) { |
| 948 destination_url = item.url; | 950 destination_url = item.url; |
| 949 is_most_visited_item_url = true; | 951 is_most_visited_item_url = true; |
| 950 } | 952 } |
| 951 } else { | 953 } else { |
| 952 // Resolve the URL | 954 // Resolve the URL |
| 953 const string16& possibly_relative_url = V8ValueToUTF16(args[0]); | 955 const base::string16& possibly_relative_url = V8ValueToUTF16(args[0]); |
| 954 GURL current_url = GetCurrentURL(render_view); | 956 GURL current_url = GetCurrentURL(render_view); |
| 955 destination_url = internal::ResolveURL(current_url, possibly_relative_url); | 957 destination_url = internal::ResolveURL(current_url, possibly_relative_url); |
| 956 } | 958 } |
| 957 | 959 |
| 958 DVLOG(1) << render_view << " NavigateContentWindow: " << destination_url; | 960 DVLOG(1) << render_view << " NavigateContentWindow: " << destination_url; |
| 959 | 961 |
| 960 // Navigate the main frame. | 962 // Navigate the main frame. |
| 961 if (destination_url.is_valid()) { | 963 if (destination_url.is_valid()) { |
| 962 WindowOpenDisposition disposition = CURRENT_TAB; | 964 WindowOpenDisposition disposition = CURRENT_TAB; |
| 963 if (args[1]->Uint32Value() == 2) | 965 if (args[1]->Uint32Value() == 2) |
| 964 disposition = NEW_BACKGROUND_TAB; | 966 disposition = NEW_BACKGROUND_TAB; |
| 965 SearchBox::Get(render_view)->NavigateToURL(destination_url, disposition, | 967 SearchBox::Get(render_view)->NavigateToURL(destination_url, disposition, |
| 966 is_most_visited_item_url); | 968 is_most_visited_item_url); |
| 967 } | 969 } |
| 968 } | 970 } |
| 969 | 971 |
| 970 // static | 972 // static |
| 971 void SearchBoxExtensionWrapper::Paste( | 973 void SearchBoxExtensionWrapper::Paste( |
| 972 const v8::FunctionCallbackInfo<v8::Value>& args) { | 974 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 973 content::RenderView* render_view = GetRenderView(); | 975 content::RenderView* render_view = GetRenderView(); |
| 974 if (!render_view) return; | 976 if (!render_view) return; |
| 975 | 977 |
| 976 string16 text; | 978 base::string16 text; |
| 977 if (!args[0]->IsUndefined()) | 979 if (!args[0]->IsUndefined()) |
| 978 text = V8ValueToUTF16(args[0]); | 980 text = V8ValueToUTF16(args[0]); |
| 979 | 981 |
| 980 DVLOG(1) << render_view << " Paste: " << text; | 982 DVLOG(1) << render_view << " Paste: " << text; |
| 981 SearchBox::Get(render_view)->Paste(text); | 983 SearchBox::Get(render_view)->Paste(text); |
| 982 } | 984 } |
| 983 | 985 |
| 984 // static | 986 // static |
| 985 void SearchBoxExtensionWrapper::StartCapturingKeyStrokes( | 987 void SearchBoxExtensionWrapper::StartCapturingKeyStrokes( |
| 986 const v8::FunctionCallbackInfo<v8::Value>& args) { | 988 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1038 if (!render_view) return; | 1040 if (!render_view) return; |
| 1039 | 1041 |
| 1040 bool display_instant_results = | 1042 bool display_instant_results = |
| 1041 SearchBox::Get(render_view)->display_instant_results(); | 1043 SearchBox::Get(render_view)->display_instant_results(); |
| 1042 DVLOG(1) << render_view << " GetDisplayInstantResults" << | 1044 DVLOG(1) << render_view << " GetDisplayInstantResults" << |
| 1043 display_instant_results; | 1045 display_instant_results; |
| 1044 args.GetReturnValue().Set(display_instant_results); | 1046 args.GetReturnValue().Set(display_instant_results); |
| 1045 } | 1047 } |
| 1046 | 1048 |
| 1047 } // namespace extensions_v8 | 1049 } // namespace extensions_v8 |
| OLD | NEW |