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/metrics/field_trial.h" |
9 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
10 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
11 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
12 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
13 #include "chrome/common/instant_types.h" | 14 #include "chrome/common/instant_types.h" |
14 #include "chrome/common/ntp_logging_events.h" | 15 #include "chrome/common/ntp_logging_events.h" |
15 #include "chrome/common/url_constants.h" | 16 #include "chrome/common/url_constants.h" |
16 #include "chrome/grit/renderer_resources.h" | 17 #include "chrome/grit/renderer_resources.h" |
17 #include "chrome/renderer/searchbox/searchbox.h" | 18 #include "chrome/renderer/searchbox/searchbox.h" |
18 #include "components/crx_file/id_util.h" | 19 #include "components/crx_file/id_util.h" |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 | 55 |
55 const char kLTRHtmlTextDirection[] = "ltr"; | 56 const char kLTRHtmlTextDirection[] = "ltr"; |
56 const char kRTLHtmlTextDirection[] = "rtl"; | 57 const char kRTLHtmlTextDirection[] = "rtl"; |
57 | 58 |
58 // Converts a V8 value to a string16. | 59 // Converts a V8 value to a string16. |
59 base::string16 V8ValueToUTF16(v8::Handle<v8::Value> v) { | 60 base::string16 V8ValueToUTF16(v8::Handle<v8::Value> v) { |
60 v8::String::Value s(v); | 61 v8::String::Value s(v); |
61 return base::string16(reinterpret_cast<const base::char16*>(*s), s.length()); | 62 return base::string16(reinterpret_cast<const base::char16*>(*s), s.length()); |
62 } | 63 } |
63 | 64 |
| 65 // Returns whether we should use large icons on NTP. |
| 66 bool IsIconNTPEnabled() { |
| 67 return StartsWithASCII(base::FieldTrialList::FindFullName("IconNTP"), |
| 68 "Enabled", true); |
| 69 } |
| 70 |
64 // Converts string16 to V8 String. | 71 // Converts string16 to V8 String. |
65 v8::Handle<v8::String> UTF16ToV8String(v8::Isolate* isolate, | 72 v8::Handle<v8::String> UTF16ToV8String(v8::Isolate* isolate, |
66 const base::string16& s) { | 73 const base::string16& s) { |
67 return v8::String::NewFromTwoByte(isolate, | 74 return v8::String::NewFromTwoByte(isolate, |
68 reinterpret_cast<const uint16_t*>(s.data()), | 75 reinterpret_cast<const uint16_t*>(s.data()), |
69 v8::String::kNormalString, | 76 v8::String::kNormalString, |
70 s.size()); | 77 s.size()); |
71 } | 78 } |
72 | 79 |
73 // Converts std::string to V8 String. | 80 // Converts std::string to V8 String. |
(...skipping 18 matching lines...) Expand all Loading... |
92 v8::Handle<v8::String> GenerateThumbnailURL( | 99 v8::Handle<v8::String> GenerateThumbnailURL( |
93 v8::Isolate* isolate, | 100 v8::Isolate* isolate, |
94 int render_view_id, | 101 int render_view_id, |
95 InstantRestrictedID most_visited_item_id) { | 102 InstantRestrictedID most_visited_item_id) { |
96 return UTF8ToV8String( | 103 return UTF8ToV8String( |
97 isolate, | 104 isolate, |
98 base::StringPrintf( | 105 base::StringPrintf( |
99 "chrome-search://thumb/%d/%d", render_view_id, most_visited_item_id)); | 106 "chrome-search://thumb/%d/%d", render_view_id, most_visited_item_id)); |
100 } | 107 } |
101 | 108 |
| 109 v8::Handle<v8::String> GenerateLargeIconURL( |
| 110 v8::Isolate* isolate, |
| 111 int render_view_id, |
| 112 InstantRestrictedID most_visited_item_id) { |
| 113 const int kIconSize = 96; // To support high DPI; on screen it's 48 dp. |
| 114 return UTF8ToV8String( |
| 115 isolate, |
| 116 base::StringPrintf("chrome-search://large-icon/%d/%d/%d", |
| 117 kIconSize, render_view_id, most_visited_item_id)); |
| 118 } |
| 119 |
| 120 v8::Handle<v8::String> GenerateFallbackIconURL( |
| 121 v8::Isolate* isolate, |
| 122 int render_view_id, |
| 123 InstantRestrictedID most_visited_item_id) { |
| 124 return UTF8ToV8String( |
| 125 isolate, |
| 126 base::StringPrintf("chrome-search://fallback-icon/,,,,1/%d/%d", |
| 127 render_view_id, most_visited_item_id)); |
| 128 } |
| 129 |
102 // Populates a Javascript MostVisitedItem object from |mv_item|. | 130 // Populates a Javascript MostVisitedItem object from |mv_item|. |
103 // NOTE: Includes "url", "title" and "domain" which are private data, so should | 131 // NOTE: Includes "url", "title" and "domain" which are private data, so should |
104 // not be returned to the Instant page. These should be erased before returning | 132 // not be returned to the Instant page. These should be erased before returning |
105 // the object. See GetMostVisitedItemsWrapper() in searchbox_api.js. | 133 // the object. See GetMostVisitedItemsWrapper() in searchbox_api.js. |
106 v8::Handle<v8::Object> GenerateMostVisitedItem( | 134 v8::Handle<v8::Object> GenerateMostVisitedItem( |
107 v8::Isolate* isolate, | 135 v8::Isolate* isolate, |
108 int render_view_id, | 136 int render_view_id, |
109 InstantRestrictedID restricted_id, | 137 InstantRestrictedID restricted_id, |
110 const InstantMostVisitedItem& mv_item) { | 138 const InstantMostVisitedItem& mv_item) { |
111 // We set the "dir" attribute of the title, so that in RTL locales, a LTR | 139 // We set the "dir" attribute of the title, so that in RTL locales, a LTR |
(...skipping 17 matching lines...) Expand all Loading... |
129 if (title.empty()) | 157 if (title.empty()) |
130 title = base::UTF8ToUTF16(mv_item.url.spec()); | 158 title = base::UTF8ToUTF16(mv_item.url.spec()); |
131 | 159 |
132 v8::Handle<v8::Object> obj = v8::Object::New(isolate); | 160 v8::Handle<v8::Object> obj = v8::Object::New(isolate); |
133 obj->Set(v8::String::NewFromUtf8(isolate, "renderViewId"), | 161 obj->Set(v8::String::NewFromUtf8(isolate, "renderViewId"), |
134 v8::Int32::New(isolate, render_view_id)); | 162 v8::Int32::New(isolate, render_view_id)); |
135 obj->Set(v8::String::NewFromUtf8(isolate, "rid"), | 163 obj->Set(v8::String::NewFromUtf8(isolate, "rid"), |
136 v8::Int32::New(isolate, restricted_id)); | 164 v8::Int32::New(isolate, restricted_id)); |
137 obj->Set(v8::String::NewFromUtf8(isolate, "thumbnailUrl"), | 165 obj->Set(v8::String::NewFromUtf8(isolate, "thumbnailUrl"), |
138 GenerateThumbnailURL(isolate, render_view_id, restricted_id)); | 166 GenerateThumbnailURL(isolate, render_view_id, restricted_id)); |
| 167 if (IsIconNTPEnabled()) { |
| 168 // Update website http://www.chromium.org/embeddedsearch when we make this |
| 169 // permanent. |
| 170 obj->Set(v8::String::NewFromUtf8(isolate, "largeIconUrl"), |
| 171 GenerateLargeIconURL(isolate, render_view_id, restricted_id)); |
| 172 obj->Set(v8::String::NewFromUtf8(isolate, "fallbackIconUrl"), |
| 173 GenerateFallbackIconURL(isolate, render_view_id, restricted_id)); |
| 174 } |
139 obj->Set(v8::String::NewFromUtf8(isolate, "title"), | 175 obj->Set(v8::String::NewFromUtf8(isolate, "title"), |
140 UTF16ToV8String(isolate, title)); | 176 UTF16ToV8String(isolate, title)); |
141 obj->Set(v8::String::NewFromUtf8(isolate, "domain"), | 177 obj->Set(v8::String::NewFromUtf8(isolate, "domain"), |
142 UTF8ToV8String(isolate, mv_item.url.host())); | 178 UTF8ToV8String(isolate, mv_item.url.host())); |
143 obj->Set(v8::String::NewFromUtf8(isolate, "direction"), | 179 obj->Set(v8::String::NewFromUtf8(isolate, "direction"), |
144 UTF8ToV8String(isolate, direction)); | 180 UTF8ToV8String(isolate, direction)); |
145 obj->Set(v8::String::NewFromUtf8(isolate, "url"), | 181 obj->Set(v8::String::NewFromUtf8(isolate, "url"), |
146 UTF8ToV8String(isolate, mv_item.url.spec())); | 182 UTF8ToV8String(isolate, mv_item.url.spec())); |
147 return obj; | 183 return obj; |
148 } | 184 } |
(...skipping 1095 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1244 if (!render_view) return; | 1280 if (!render_view) return; |
1245 | 1281 |
1246 bool display_instant_results = | 1282 bool display_instant_results = |
1247 SearchBox::Get(render_view)->display_instant_results(); | 1283 SearchBox::Get(render_view)->display_instant_results(); |
1248 DVLOG(1) << render_view << " GetDisplayInstantResults" << | 1284 DVLOG(1) << render_view << " GetDisplayInstantResults" << |
1249 display_instant_results; | 1285 display_instant_results; |
1250 args.GetReturnValue().Set(display_instant_results); | 1286 args.GetReturnValue().Set(display_instant_results); |
1251 } | 1287 } |
1252 | 1288 |
1253 } // namespace extensions_v8 | 1289 } // namespace extensions_v8 |
OLD | NEW |