| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/browser/dom_ui/new_tab_ui.h" | 5 #include "chrome/browser/dom_ui/new_tab_ui.h" |
| 6 | 6 |
| 7 #include "base/histogram.h" | 7 #include "base/histogram.h" |
| 8 #include "base/string_piece.h" | 8 #include "base/string_piece.h" |
| 9 #include "chrome/app/locales/locale_settings.h" | 9 #include "chrome/app/locales/locale_settings.h" |
| 10 #include "chrome/browser/bookmarks/bookmark_utils.h" | 10 #include "chrome/browser/bookmarks/bookmark_utils.h" |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 // The time when we started benchmarking. | 109 // The time when we started benchmarking. |
| 110 base::TimeTicks start_; | 110 base::TimeTicks start_; |
| 111 // The last time we got a paint notification. | 111 // The last time we got a paint notification. |
| 112 base::TimeTicks last_paint_; | 112 base::TimeTicks last_paint_; |
| 113 // Scoping so we can be sure our timeouts don't outlive us. | 113 // Scoping so we can be sure our timeouts don't outlive us. |
| 114 ScopedRunnableMethodFactory<PaintTimer> method_factory_; | 114 ScopedRunnableMethodFactory<PaintTimer> method_factory_; |
| 115 | 115 |
| 116 DISALLOW_COPY_AND_ASSIGN(PaintTimer); | 116 DISALLOW_COPY_AND_ASSIGN(PaintTimer); |
| 117 }; | 117 }; |
| 118 | 118 |
| 119 // Adds "url" and "title" keys on incoming dictionary, setting title | 119 // Adds "url", "title", and "direction" keys on incoming dictionary, setting |
| 120 // as the url as a fallback on empty title. | 120 // title as the url as a fallback on empty title. |
| 121 void SetURLAndTitle(DictionaryValue* dictionary, | 121 void SetURLTitleAndDirection(DictionaryValue* dictionary, |
| 122 const std::wstring& title, | 122 const std::wstring& title, |
| 123 const GURL& gurl) { | 123 const GURL& gurl) { |
| 124 std::wstring wstring_url = UTF8ToWide(gurl.spec()); | 124 std::wstring wstring_url = UTF8ToWide(gurl.spec()); |
| 125 dictionary->SetString(L"url", wstring_url); | 125 dictionary->SetString(L"url", wstring_url); |
| 126 | 126 |
| 127 bool using_url_as_the_title = false; | 127 bool using_url_as_the_title = false; |
| 128 std::wstring title_to_set(title); | 128 std::wstring title_to_set(title); |
| 129 if (title_to_set.empty()) { | 129 if (title_to_set.empty()) { |
| 130 using_url_as_the_title = true; | 130 using_url_as_the_title = true; |
| 131 title_to_set = wstring_url; | 131 title_to_set = wstring_url; |
| 132 } | 132 } |
| 133 | 133 |
| 134 // We set the "dir" attribute of the title, so that in RTL locales, a LTR |
| 135 // title is rendered left-to-right and truncated from the right. For example, |
| 136 // the title of http://msdn.microsoft.com/en-us/default.aspx is "MSDN: |
| 137 // Microsoft developer network". In RTL locales, in the [New Tab] page, if |
| 138 // the "dir" of this title is not specified, it takes Chrome UI's |
| 139 // directionality. So the title will be truncated as "soft developer |
| 140 // network". Setting the "dir" attribute as "ltr" renders the truncated title |
| 141 // as "MSDN: Microsoft D...". As another example, the title of |
| 142 // http://yahoo.com is "Yahoo!". In RTL locales, in the [New Tab] page, the |
| 143 // title will be rendered as "!Yahoo" if its "dir" attribute is not set to |
| 144 // "ltr". |
| 145 // |
| 134 // Since the title can contain BiDi text, we need to mark the text as either | 146 // Since the title can contain BiDi text, we need to mark the text as either |
| 135 // RTL or LTR, depending on the characters in the string. If we use the URL | 147 // RTL or LTR, depending on the characters in the string. If we use the URL |
| 136 // as the title, we mark the title as LTR since URLs are always treated as | 148 // as the title, we mark the title as LTR since URLs are always treated as |
| 137 // left to right strings. | 149 // left to right strings. Simply setting the title's "dir" attribute works |
| 150 // fine for rendering and truncating the title. However, it does not work for |
| 151 // entire title within a tooltip when the mouse is over the title link.. For |
| 152 // example, without LRE-PDF pair, the title "Yahoo!" will be rendered as |
| 153 // "!Yahoo" within the tooltip when the mouse is over the title link. |
| 154 std::wstring direction = kDefaultHtmlTextDirection; |
| 138 if (l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT) { | 155 if (l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT) { |
| 139 if (using_url_as_the_title) { | 156 if (using_url_as_the_title) { |
| 140 l10n_util::WrapStringWithLTRFormatting(&title_to_set); | 157 l10n_util::WrapStringWithLTRFormatting(&title_to_set); |
| 141 } else { | 158 } else { |
| 142 bool success = | 159 if (l10n_util::StringContainsStrongRTLChars(title)) { |
| 143 l10n_util::AdjustStringForLocaleDirection(title, &title_to_set); | 160 l10n_util::WrapStringWithRTLFormatting(&title_to_set); |
| 144 DCHECK(success ? (title != title_to_set) : (title == title_to_set)); | 161 direction = kRTLHtmlTextDirection; |
| 162 } else { |
| 163 l10n_util::WrapStringWithLTRFormatting(&title_to_set); |
| 164 } |
| 145 } | 165 } |
| 146 } | 166 } |
| 147 dictionary->SetString(L"title", title_to_set); | 167 dictionary->SetString(L"title", title_to_set); |
| 168 dictionary->SetString(L"direction", direction); |
| 148 } | 169 } |
| 149 | 170 |
| 150 } // end anonymous namespace | 171 } // end anonymous namespace |
| 151 | 172 |
| 152 /////////////////////////////////////////////////////////////////////////////// | 173 /////////////////////////////////////////////////////////////////////////////// |
| 153 // NewTabHTMLSource | 174 // NewTabHTMLSource |
| 154 | 175 |
| 155 NewTabHTMLSource::NewTabHTMLSource() | 176 NewTabHTMLSource::NewTabHTMLSource() |
| 156 : DataSource("new-tab", MessageLoop::current()) { | 177 : DataSource("new-tab", MessageLoop::current()) { |
| 157 } | 178 } |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 307 void MostVisitedHandler::OnSegmentUsageAvailable( | 328 void MostVisitedHandler::OnSegmentUsageAvailable( |
| 308 CancelableRequestProvider::Handle handle, | 329 CancelableRequestProvider::Handle handle, |
| 309 std::vector<PageUsageData*>* data) { | 330 std::vector<PageUsageData*>* data) { |
| 310 most_visited_urls_.clear(); | 331 most_visited_urls_.clear(); |
| 311 | 332 |
| 312 ListValue pages_value; | 333 ListValue pages_value; |
| 313 const size_t count = std::min<size_t>(kMostVisitedPages, data->size()); | 334 const size_t count = std::min<size_t>(kMostVisitedPages, data->size()); |
| 314 for (size_t i = 0; i < count; ++i) { | 335 for (size_t i = 0; i < count; ++i) { |
| 315 const PageUsageData& page = *(*data)[i]; | 336 const PageUsageData& page = *(*data)[i]; |
| 316 DictionaryValue* page_value = new DictionaryValue; | 337 DictionaryValue* page_value = new DictionaryValue; |
| 317 SetURLAndTitle(page_value, page.GetTitle(), page.GetURL()); | 338 SetURLTitleAndDirection(page_value, page.GetTitle(), page.GetURL()); |
| 318 pages_value.Append(page_value); | 339 pages_value.Append(page_value); |
| 319 most_visited_urls_.push_back(page.GetURL()); | 340 most_visited_urls_.push_back(page.GetURL()); |
| 320 } | 341 } |
| 321 dom_ui_host_->CallJavascriptFunction(L"mostVisitedPages", pages_value); | 342 dom_ui_host_->CallJavascriptFunction(L"mostVisitedPages", pages_value); |
| 322 } | 343 } |
| 323 | 344 |
| 324 void MostVisitedHandler::Observe(NotificationType type, | 345 void MostVisitedHandler::Observe(NotificationType type, |
| 325 const NotificationSource& source, | 346 const NotificationSource& source, |
| 326 const NotificationDetails& details) { | 347 const NotificationDetails& details) { |
| 327 if (type != NotificationType::HISTORY_URLS_DELETED) { | 348 if (type != NotificationType::HISTORY_URLS_DELETED) { |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 493 } | 514 } |
| 494 | 515 |
| 495 void RecentlyBookmarkedHandler::SendBookmarksToPage() { | 516 void RecentlyBookmarkedHandler::SendBookmarksToPage() { |
| 496 std::vector<BookmarkNode*> recently_bookmarked; | 517 std::vector<BookmarkNode*> recently_bookmarked; |
| 497 bookmark_utils::GetMostRecentlyAddedEntries( | 518 bookmark_utils::GetMostRecentlyAddedEntries( |
| 498 model_, kRecentBookmarks, &recently_bookmarked); | 519 model_, kRecentBookmarks, &recently_bookmarked); |
| 499 ListValue list_value; | 520 ListValue list_value; |
| 500 for (size_t i = 0; i < recently_bookmarked.size(); ++i) { | 521 for (size_t i = 0; i < recently_bookmarked.size(); ++i) { |
| 501 BookmarkNode* node = recently_bookmarked[i]; | 522 BookmarkNode* node = recently_bookmarked[i]; |
| 502 DictionaryValue* entry_value = new DictionaryValue; | 523 DictionaryValue* entry_value = new DictionaryValue; |
| 503 SetURLAndTitle(entry_value, node->GetTitle(), node->GetURL()); | 524 SetURLTitleAndDirection(entry_value, node->GetTitle(), node->GetURL()); |
| 504 list_value.Append(entry_value); | 525 list_value.Append(entry_value); |
| 505 } | 526 } |
| 506 dom_ui_host_->CallJavascriptFunction(L"recentlyBookmarked", list_value); | 527 dom_ui_host_->CallJavascriptFunction(L"recentlyBookmarked", list_value); |
| 507 } | 528 } |
| 508 | 529 |
| 509 void RecentlyBookmarkedHandler::Loaded(BookmarkModel* model) { | 530 void RecentlyBookmarkedHandler::Loaded(BookmarkModel* model) { |
| 510 SendBookmarksToPage(); | 531 SendBookmarksToPage(); |
| 511 } | 532 } |
| 512 | 533 |
| 513 void RecentlyBookmarkedHandler::BookmarkNodeAdded(BookmarkModel* model, | 534 void RecentlyBookmarkedHandler::BookmarkNodeAdded(BookmarkModel* model, |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 633 const TabRestoreService::Tab& tab, | 654 const TabRestoreService::Tab& tab, |
| 634 DictionaryValue* dictionary) { | 655 DictionaryValue* dictionary) { |
| 635 if (tab.navigations.empty()) | 656 if (tab.navigations.empty()) |
| 636 return false; | 657 return false; |
| 637 | 658 |
| 638 const TabNavigation& current_navigation = | 659 const TabNavigation& current_navigation = |
| 639 tab.navigations.at(tab.current_navigation_index); | 660 tab.navigations.at(tab.current_navigation_index); |
| 640 if (current_navigation.url() == NewTabUIURL()) | 661 if (current_navigation.url() == NewTabUIURL()) |
| 641 return false; | 662 return false; |
| 642 | 663 |
| 643 SetURLAndTitle(dictionary, current_navigation.title(), | 664 SetURLTitleAndDirection(dictionary, current_navigation.title(), |
| 644 current_navigation.url()); | 665 current_navigation.url()); |
| 645 dictionary->SetString(L"type", L"tab"); | 666 dictionary->SetString(L"type", L"tab"); |
| 646 return true; | 667 return true; |
| 647 } | 668 } |
| 648 | 669 |
| 649 bool RecentlyClosedTabsHandler::WindowToValue( | 670 bool RecentlyClosedTabsHandler::WindowToValue( |
| 650 const TabRestoreService::Window& window, | 671 const TabRestoreService::Window& window, |
| 651 DictionaryValue* dictionary) { | 672 DictionaryValue* dictionary) { |
| 652 if (window.tabs.empty()) { | 673 if (window.tabs.empty()) { |
| 653 NOTREACHED(); | 674 NOTREACHED(); |
| 654 return false; | 675 return false; |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 863 void NewTabUIContents::RequestOpenURL(const GURL& url, | 884 void NewTabUIContents::RequestOpenURL(const GURL& url, |
| 864 const GURL& /*referrer*/, | 885 const GURL& /*referrer*/, |
| 865 WindowOpenDisposition disposition) { | 886 WindowOpenDisposition disposition) { |
| 866 // The user opened a URL on the page (including "open in new window"). | 887 // The user opened a URL on the page (including "open in new window"). |
| 867 // We count all such clicks as AUTO_BOOKMARK, which increments the site's | 888 // We count all such clicks as AUTO_BOOKMARK, which increments the site's |
| 868 // visit count (which is used for ranking the most visited entries). | 889 // visit count (which is used for ranking the most visited entries). |
| 869 // Note this means we're including clicks on not only most visited thumbnails, | 890 // Note this means we're including clicks on not only most visited thumbnails, |
| 870 // but also clicks on recently bookmarked. | 891 // but also clicks on recently bookmarked. |
| 871 OpenURL(url, GURL(), disposition, PageTransition::AUTO_BOOKMARK); | 892 OpenURL(url, GURL(), disposition, PageTransition::AUTO_BOOKMARK); |
| 872 } | 893 } |
| OLD | NEW |