| Index: chrome/browser/dom_ui/new_tab_ui.cc
|
| ===================================================================
|
| --- chrome/browser/dom_ui/new_tab_ui.cc (revision 61074)
|
| +++ chrome/browser/dom_ui/new_tab_ui.cc (working copy)
|
| @@ -27,6 +27,7 @@
|
| #include "chrome/browser/dom_ui/ntp_resource_cache.h"
|
| #include "chrome/browser/dom_ui/shown_sections_handler.h"
|
| #include "chrome/browser/dom_ui/tips_handler.h"
|
| +#include "chrome/browser/dom_ui/value_helper.h"
|
| #include "chrome/browser/metrics/user_metrics.h"
|
| #include "chrome/browser/themes/browser_theme_provider.h"
|
| #include "chrome/browser/prefs/pref_service.h"
|
| @@ -151,22 +152,6 @@
|
| virtual void TabRestoreServiceDestroyed(TabRestoreService* service);
|
|
|
| private:
|
| - // Converts a closed tab to the value sent down to the NTP. Returns true on
|
| - // success, false if the value shouldn't be sent down.
|
| - bool TabToValue(const TabRestoreService::Tab& tab,
|
| - DictionaryValue* dictionary);
|
| -
|
| - // Converts a closed window to the value sent down to the NTP. Returns true
|
| - // on success, false if the value shouldn't be sent down.
|
| - bool WindowToValue(const TabRestoreService::Window& window,
|
| - DictionaryValue* dictionary);
|
| -
|
| - // Adds tab to unique_items list if it is not present. Returns false if
|
| - // tab was already in the list, true if it was absent. A tab is
|
| - // considered unique if no other tab shares both its title and its url.
|
| - bool EnsureTabIsUnique(const DictionaryValue* tab,
|
| - std::set<std::string>* unique_items);
|
| -
|
| // TabRestoreService that we are observing.
|
| TabRestoreService* tab_restore_service_;
|
|
|
| @@ -221,32 +206,9 @@
|
|
|
| void RecentlyClosedTabsHandler::TabRestoreServiceChanged(
|
| TabRestoreService* service) {
|
| - const TabRestoreService::Entries& entries = service->entries();
|
| ListValue list_value;
|
| - std::set<std::string> unique_items;
|
| - int added_count = 0;
|
| - const int max_count = 10;
|
| + NewTabUI::AddRecentlyClosedEntries(service->entries(), &list_value);
|
|
|
| - // We filter the list of recently closed to only show 'interesting' entries,
|
| - // where an interesting entry is either a closed window or a closed tab
|
| - // whose selected navigation is not the new tab ui.
|
| - for (TabRestoreService::Entries::const_iterator it = entries.begin();
|
| - it != entries.end() && added_count < max_count; ++it) {
|
| - TabRestoreService::Entry* entry = *it;
|
| - DictionaryValue* value = new DictionaryValue();
|
| - if ((entry->type == TabRestoreService::TAB &&
|
| - TabToValue(*static_cast<TabRestoreService::Tab*>(entry), value) &&
|
| - EnsureTabIsUnique(value, &unique_items)) ||
|
| - (entry->type == TabRestoreService::WINDOW &&
|
| - WindowToValue(*static_cast<TabRestoreService::Window*>(entry),
|
| - value))) {
|
| - value->SetInteger("sessionId", entry->id);
|
| - list_value.Append(value);
|
| - added_count++;
|
| - } else {
|
| - delete value;
|
| - }
|
| - }
|
| dom_ui_->CallJavascriptFunction(L"recentlyClosedTabs", list_value);
|
| }
|
|
|
| @@ -255,71 +217,7 @@
|
| tab_restore_service_ = NULL;
|
| }
|
|
|
| -bool RecentlyClosedTabsHandler::TabToValue(
|
| - const TabRestoreService::Tab& tab,
|
| - DictionaryValue* dictionary) {
|
| - if (tab.navigations.empty())
|
| - return false;
|
|
|
| - const TabNavigation& current_navigation =
|
| - tab.navigations.at(tab.current_navigation_index);
|
| - if (current_navigation.virtual_url() == GURL(chrome::kChromeUINewTabURL))
|
| - return false;
|
| -
|
| - NewTabUI::SetURLTitleAndDirection(dictionary, current_navigation.title(),
|
| - current_navigation.virtual_url());
|
| - dictionary->SetString("type", "tab");
|
| - dictionary->SetReal("timestamp", tab.timestamp.ToDoubleT());
|
| - return true;
|
| -}
|
| -
|
| -bool RecentlyClosedTabsHandler::WindowToValue(
|
| - const TabRestoreService::Window& window,
|
| - DictionaryValue* dictionary) {
|
| - if (window.tabs.empty()) {
|
| - NOTREACHED();
|
| - return false;
|
| - }
|
| -
|
| - ListValue* tab_values = new ListValue();
|
| - for (size_t i = 0; i < window.tabs.size(); ++i) {
|
| - DictionaryValue* tab_value = new DictionaryValue();
|
| - if (TabToValue(window.tabs[i], tab_value))
|
| - tab_values->Append(tab_value);
|
| - else
|
| - delete tab_value;
|
| - }
|
| - if (tab_values->GetSize() == 0) {
|
| - delete tab_values;
|
| - return false;
|
| - }
|
| -
|
| - dictionary->SetString("type", "window");
|
| - dictionary->SetReal("timestamp", window.timestamp.ToDoubleT());
|
| - dictionary->Set("tabs", tab_values);
|
| - return true;
|
| -}
|
| -
|
| -bool RecentlyClosedTabsHandler::EnsureTabIsUnique(
|
| - const DictionaryValue* tab,
|
| - std::set<std::string>* unique_items) {
|
| - DCHECK(unique_items);
|
| - std::string title;
|
| - std::string url;
|
| - if (tab->GetString("title", &title) &&
|
| - tab->GetString("url", &url)) {
|
| - // TODO(viettrungluu): this isn't obviously reliable, since different
|
| - // combinations of titles/urls may conceivably yield the same string.
|
| - std::string unique_key = title + url;
|
| - if (unique_items->find(unique_key) != unique_items->end())
|
| - return false;
|
| - else
|
| - unique_items->insert(unique_key);
|
| - }
|
| - return true;
|
| -}
|
| -
|
| -
|
| ///////////////////////////////////////////////////////////////////////////////
|
| // MetricsHandler
|
|
|
| @@ -605,6 +503,57 @@
|
| dictionary->SetString("direction", direction);
|
| }
|
|
|
| +namespace {
|
| +
|
| +bool IsTabUnique(const DictionaryValue* tab,
|
| + std::set<std::string>* unique_items) {
|
| + DCHECK(unique_items);
|
| + std::string title;
|
| + std::string url;
|
| + if (tab->GetString("title", &title) &&
|
| + tab->GetString("url", &url)) {
|
| + // TODO(viettrungluu): this isn't obviously reliable, since different
|
| + // combinations of titles/urls may conceivably yield the same string.
|
| + std::string unique_key = title + url;
|
| + if (unique_items->find(unique_key) != unique_items->end())
|
| + return false;
|
| + else
|
| + unique_items->insert(unique_key);
|
| + }
|
| + return true;
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +// static
|
| +void NewTabUI::AddRecentlyClosedEntries(
|
| + const TabRestoreService::Entries& entries, ListValue* entry_list_value) {
|
| + const int max_count = 10;
|
| + int added_count = 0;
|
| + std::set<std::string> unique_items;
|
| + // We filter the list of recently closed to only show 'interesting' entries,
|
| + // where an interesting entry is either a closed window or a closed tab
|
| + // whose selected navigation is not the new tab ui.
|
| + for (TabRestoreService::Entries::const_iterator it = entries.begin();
|
| + it != entries.end() && added_count < max_count; ++it) {
|
| + TabRestoreService::Entry* entry = *it;
|
| + scoped_ptr<DictionaryValue> entry_dict(new DictionaryValue());
|
| + if ((entry->type == TabRestoreService::TAB &&
|
| + ValueHelper::TabToValue(
|
| + *static_cast<TabRestoreService::Tab*>(entry),
|
| + entry_dict.get()) &&
|
| + IsTabUnique(entry_dict.get(), &unique_items)) ||
|
| + (entry->type == TabRestoreService::WINDOW &&
|
| + ValueHelper::WindowToValue(
|
| + *static_cast<TabRestoreService::Window*>(entry),
|
| + entry_dict.get()))) {
|
| + entry_dict->SetInteger("sessionId", entry->id);
|
| + entry_list_value->Append(entry_dict.release());
|
| + added_count++;
|
| + }
|
| + }
|
| +}
|
| +
|
| ///////////////////////////////////////////////////////////////////////////////
|
| // NewTabHTMLSource
|
|
|
|
|