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/string_util.h" | 8 #include "base/string_util.h" |
9 #include "base/stringprintf.h" | 9 #include "base/stringprintf.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 s->WriteUtf8(WriteInto(&result, len + 1)); | 78 s->WriteUtf8(WriteInto(&result, len + 1)); |
79 } | 79 } |
80 return result; | 80 return result; |
81 } | 81 } |
82 | 82 |
83 void Dispatch(WebKit::WebFrame* frame, const WebKit::WebString& script) { | 83 void Dispatch(WebKit::WebFrame* frame, const WebKit::WebString& script) { |
84 if (!frame) return; | 84 if (!frame) return; |
85 frame->executeScript(WebKit::WebScriptSource(script)); | 85 frame->executeScript(WebKit::WebScriptSource(script)); |
86 } | 86 } |
87 | 87 |
88 v8::Handle<v8::String> GenerateThumbnailURL(uint64 most_visited_item_id) { | 88 v8::Handle<v8::String> GenerateThumbnailURL( |
89 return UTF8ToV8String( | 89 int render_view_id, |
90 base::StringPrintf("chrome-search://thumb/%s", | 90 InstantRestrictedID most_visited_item_id) { |
91 base::Uint64ToString(most_visited_item_id).c_str())); | 91 return UTF8ToV8String(base::StringPrintf("chrome-search://thumb/%d/%d", |
| 92 render_view_id, |
| 93 most_visited_item_id)); |
92 } | 94 } |
93 | 95 |
94 v8::Handle<v8::String> GenerateFaviconURL(uint64 most_visited_item_id) { | 96 v8::Handle<v8::String> GenerateFaviconURL( |
95 return UTF8ToV8String( | 97 int render_view_id, |
96 base::StringPrintf("chrome-search://favicon/%s", | 98 InstantRestrictedID most_visited_item_id) { |
97 base::Uint64ToString(most_visited_item_id).c_str())); | 99 return UTF8ToV8String(base::StringPrintf("chrome-search://favicon/%d/%d", |
| 100 render_view_id, |
| 101 most_visited_item_id)); |
98 } | 102 } |
99 | 103 |
100 // If |url| starts with |prefix|, removes |prefix|. | 104 // If |url| starts with |prefix|, removes |prefix|. |
101 void StripPrefix(string16* url, const string16& prefix) { | 105 void StripPrefix(string16* url, const string16& prefix) { |
102 if (StartsWith(*url, prefix, true)) | 106 if (StartsWith(*url, prefix, true)) |
103 url->erase(0, prefix.length()); | 107 url->erase(0, prefix.length()); |
104 } | 108 } |
105 | 109 |
106 // Removes leading "http://" or "http://www." from |url| unless |user_input| | 110 // Removes leading "http://" or "http://www." from |url| unless |user_input| |
107 // starts with those prefixes. | 111 // starts with those prefixes. |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 v8::Int32::New(result.relevance)); | 168 v8::Int32::New(result.relevance)); |
165 obj->Set(v8::String::New("rankingData"), ranking_data); | 169 obj->Set(v8::String::New("rankingData"), ranking_data); |
166 return obj; | 170 return obj; |
167 } | 171 } |
168 | 172 |
169 // Populates a Javascript MostVisitedItem object from |mv_item|. | 173 // Populates a Javascript MostVisitedItem object from |mv_item|. |
170 // NOTE: Includes "url", "title" and "domain" which are private data, so should | 174 // NOTE: Includes "url", "title" and "domain" which are private data, so should |
171 // not be returned to the Instant page. These should be erased before returning | 175 // not be returned to the Instant page. These should be erased before returning |
172 // the object. See GetMostVisitedItemsWrapper() in searchbox_api.js. | 176 // the object. See GetMostVisitedItemsWrapper() in searchbox_api.js. |
173 v8::Handle<v8::Object> GenerateMostVisitedItem( | 177 v8::Handle<v8::Object> GenerateMostVisitedItem( |
| 178 int render_view_id, |
174 InstantRestrictedID restricted_id, | 179 InstantRestrictedID restricted_id, |
175 const InstantMostVisitedItem &mv_item) { | 180 const InstantMostVisitedItem &mv_item) { |
176 // We set the "dir" attribute of the title, so that in RTL locales, a LTR | 181 // We set the "dir" attribute of the title, so that in RTL locales, a LTR |
177 // title is rendered left-to-right and truncated from the right. For | 182 // title is rendered left-to-right and truncated from the right. For |
178 // example, the title of http://msdn.microsoft.com/en-us/default.aspx is | 183 // example, the title of http://msdn.microsoft.com/en-us/default.aspx is |
179 // "MSDN: Microsoft developer network". In RTL locales, in the New Tab | 184 // "MSDN: Microsoft developer network". In RTL locales, in the New Tab |
180 // page, if the "dir" of this title is not specified, it takes Chrome UI's | 185 // page, if the "dir" of this title is not specified, it takes Chrome UI's |
181 // directionality. So the title will be truncated as "soft developer | 186 // directionality. So the title will be truncated as "soft developer |
182 // network". Setting the "dir" attribute as "ltr" renders the truncated | 187 // network". Setting the "dir" attribute as "ltr" renders the truncated |
183 // title as "MSDN: Microsoft D...". As another example, the title of | 188 // title as "MSDN: Microsoft D...". As another example, the title of |
184 // http://yahoo.com is "Yahoo!". In RTL locales, in the New Tab page, the | 189 // http://yahoo.com is "Yahoo!". In RTL locales, in the New Tab page, the |
185 // title will be rendered as "!Yahoo" if its "dir" attribute is not set to | 190 // title will be rendered as "!Yahoo" if its "dir" attribute is not set to |
186 // "ltr". | 191 // "ltr". |
187 std::string direction; | 192 std::string direction; |
188 if (base::i18n::StringContainsStrongRTLChars(mv_item.title)) | 193 if (base::i18n::StringContainsStrongRTLChars(mv_item.title)) |
189 direction = kRTLHtmlTextDirection; | 194 direction = kRTLHtmlTextDirection; |
190 else | 195 else |
191 direction = kLTRHtmlTextDirection; | 196 direction = kLTRHtmlTextDirection; |
192 | 197 |
193 string16 title = mv_item.title; | 198 string16 title = mv_item.title; |
194 if (title.empty()) | 199 if (title.empty()) |
195 title = UTF8ToUTF16(mv_item.url.spec()); | 200 title = UTF8ToUTF16(mv_item.url.spec()); |
196 | 201 |
197 v8::Handle<v8::Object> obj = v8::Object::New(); | 202 v8::Handle<v8::Object> obj = v8::Object::New(); |
198 obj->Set(v8::String::New("rid"), v8::Int32::New(restricted_id)); | 203 obj->Set(v8::String::New("rid"), v8::Int32::New(restricted_id)); |
199 obj->Set(v8::String::New("thumbnailUrl"), | 204 obj->Set(v8::String::New("thumbnailUrl"), |
200 GenerateThumbnailURL(restricted_id)); | 205 GenerateThumbnailURL(render_view_id, restricted_id)); |
201 obj->Set(v8::String::New("faviconUrl"), | 206 obj->Set(v8::String::New("faviconUrl"), |
202 GenerateFaviconURL(restricted_id)); | 207 GenerateFaviconURL(render_view_id, restricted_id)); |
203 obj->Set(v8::String::New("title"), UTF16ToV8String(title)); | 208 obj->Set(v8::String::New("title"), UTF16ToV8String(title)); |
204 obj->Set(v8::String::New("domain"), UTF8ToV8String(mv_item.url.host())); | 209 obj->Set(v8::String::New("domain"), UTF8ToV8String(mv_item.url.host())); |
205 obj->Set(v8::String::New("direction"), UTF8ToV8String(direction)); | 210 obj->Set(v8::String::New("direction"), UTF8ToV8String(direction)); |
206 obj->Set(v8::String::New("url"), UTF8ToV8String(mv_item.url.spec())); | 211 obj->Set(v8::String::New("url"), UTF8ToV8String(mv_item.url.spec())); |
207 return obj; | 212 return obj; |
208 } | 213 } |
209 | 214 |
210 // Returns the render view for the current JS context if it matches |origin|, | 215 // Returns the render view for the current JS context if it matches |origin|, |
211 // otherwise returns NULL. Used to restrict methods that access suggestions and | 216 // otherwise returns NULL. Used to restrict methods that access suggestions and |
212 // most visited data to pages with origin chrome-search://most-visited and | 217 // most visited data to pages with origin chrome-search://most-visited and |
(...skipping 1039 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1252 if (!render_view) | 1257 if (!render_view) |
1253 return v8::Undefined(); | 1258 return v8::Undefined(); |
1254 DVLOG(1) << render_view << " GetMostVisitedItems"; | 1259 DVLOG(1) << render_view << " GetMostVisitedItems"; |
1255 | 1260 |
1256 const SearchBox* search_box = SearchBox::Get(render_view); | 1261 const SearchBox* search_box = SearchBox::Get(render_view); |
1257 | 1262 |
1258 std::vector<InstantMostVisitedItemIDPair> instant_mv_items; | 1263 std::vector<InstantMostVisitedItemIDPair> instant_mv_items; |
1259 search_box->GetMostVisitedItems(&instant_mv_items); | 1264 search_box->GetMostVisitedItems(&instant_mv_items); |
1260 v8::Handle<v8::Array> v8_mv_items = v8::Array::New(instant_mv_items.size()); | 1265 v8::Handle<v8::Array> v8_mv_items = v8::Array::New(instant_mv_items.size()); |
1261 for (size_t i = 0; i < instant_mv_items.size(); ++i) { | 1266 for (size_t i = 0; i < instant_mv_items.size(); ++i) { |
1262 v8_mv_items->Set(i, GenerateMostVisitedItem(instant_mv_items[i].first, | 1267 v8_mv_items->Set(i, GenerateMostVisitedItem(render_view->GetRoutingID(), |
| 1268 instant_mv_items[i].first, |
1263 instant_mv_items[i].second)); | 1269 instant_mv_items[i].second)); |
1264 } | 1270 } |
1265 return v8_mv_items; | 1271 return v8_mv_items; |
1266 } | 1272 } |
1267 | 1273 |
1268 // static | 1274 // static |
1269 v8::Handle<v8::Value> SearchBoxExtensionWrapper::DeleteMostVisitedItem( | 1275 v8::Handle<v8::Value> SearchBoxExtensionWrapper::DeleteMostVisitedItem( |
1270 const v8::Arguments& args) { | 1276 const v8::Arguments& args) { |
1271 content::RenderView* render_view = GetRenderView(); | 1277 content::RenderView* render_view = GetRenderView(); |
1272 if (!render_view || !args.Length()) return v8::Undefined(); | 1278 if (!render_view || !args.Length()) return v8::Undefined(); |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1401 if (args.Length() < 1 || !args[0]->IsNumber()) | 1407 if (args.Length() < 1 || !args[0]->IsNumber()) |
1402 return v8::Undefined(); | 1408 return v8::Undefined(); |
1403 | 1409 |
1404 DVLOG(1) << render_view << " GetMostVisitedItem"; | 1410 DVLOG(1) << render_view << " GetMostVisitedItem"; |
1405 InstantRestrictedID restricted_id = args[0]->IntegerValue(); | 1411 InstantRestrictedID restricted_id = args[0]->IntegerValue(); |
1406 InstantMostVisitedItem mv_item; | 1412 InstantMostVisitedItem mv_item; |
1407 if (!SearchBox::Get(render_view)->GetMostVisitedItemWithID( | 1413 if (!SearchBox::Get(render_view)->GetMostVisitedItemWithID( |
1408 restricted_id, &mv_item)) { | 1414 restricted_id, &mv_item)) { |
1409 return v8::Undefined(); | 1415 return v8::Undefined(); |
1410 } | 1416 } |
1411 return GenerateMostVisitedItem(restricted_id, mv_item); | 1417 return GenerateMostVisitedItem(render_view->GetRoutingID(), restricted_id, |
| 1418 mv_item); |
1412 } | 1419 } |
1413 | 1420 |
1414 // static | 1421 // static |
1415 v8::Handle<v8::Value> SearchBoxExtensionWrapper::IsFocused( | 1422 v8::Handle<v8::Value> SearchBoxExtensionWrapper::IsFocused( |
1416 const v8::Arguments& args) { | 1423 const v8::Arguments& args) { |
1417 content::RenderView* render_view = GetRenderView(); | 1424 content::RenderView* render_view = GetRenderView(); |
1418 if (!render_view) return v8::Undefined(); | 1425 if (!render_view) return v8::Undefined(); |
1419 | 1426 |
1420 bool is_focused = SearchBox::Get(render_view)->is_focused(); | 1427 bool is_focused = SearchBox::Get(render_view)->is_focused(); |
1421 DVLOG(1) << render_view << " IsFocused: " << is_focused; | 1428 DVLOG(1) << render_view << " IsFocused: " << is_focused; |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1492 Dispatch(frame, kDispatchFocusChangedScript); | 1499 Dispatch(frame, kDispatchFocusChangedScript); |
1493 } | 1500 } |
1494 | 1501 |
1495 // static | 1502 // static |
1496 void SearchBoxExtension::DispatchToggleVoiceSearch( | 1503 void SearchBoxExtension::DispatchToggleVoiceSearch( |
1497 WebKit::WebFrame* frame) { | 1504 WebKit::WebFrame* frame) { |
1498 Dispatch(frame, kDispatchToggleVoiceSearchScript); | 1505 Dispatch(frame, kDispatchToggleVoiceSearchScript); |
1499 } | 1506 } |
1500 | 1507 |
1501 } // namespace extensions_v8 | 1508 } // namespace extensions_v8 |
OLD | NEW |