| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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/browser/ui/webui/foreign_session_handler.h" | 5 #include "chrome/browser/ui/webui/foreign_session_handler.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 scoped_ptr<base::DictionaryValue> dictionary(new base::DictionaryValue()); | 63 scoped_ptr<base::DictionaryValue> dictionary(new base::DictionaryValue()); |
| 64 NewTabUI::SetUrlTitleAndDirection(dictionary.get(), | 64 NewTabUI::SetUrlTitleAndDirection(dictionary.get(), |
| 65 current_navigation.title(), tab_url); | 65 current_navigation.title(), tab_url); |
| 66 dictionary->SetString("type", "tab"); | 66 dictionary->SetString("type", "tab"); |
| 67 dictionary->SetDouble("timestamp", | 67 dictionary->SetDouble("timestamp", |
| 68 static_cast<double>(tab.timestamp.ToInternalValue())); | 68 static_cast<double>(tab.timestamp.ToInternalValue())); |
| 69 // TODO(jeremycho): This should probably be renamed to tabId to avoid | 69 // TODO(jeremycho): This should probably be renamed to tabId to avoid |
| 70 // confusion with the ID corresponding to a session. Investigate all the | 70 // confusion with the ID corresponding to a session. Investigate all the |
| 71 // places (C++ and JS) where this is being used. (http://crbug.com/154865). | 71 // places (C++ and JS) where this is being used. (http://crbug.com/154865). |
| 72 dictionary->SetInteger("sessionId", tab.tab_id.id()); | 72 dictionary->SetInteger("sessionId", tab.tab_id.id()); |
| 73 return dictionary.Pass(); | 73 return dictionary; |
| 74 } | 74 } |
| 75 | 75 |
| 76 // Helper for initializing a boilerplate SessionWindow JSON compatible object. | 76 // Helper for initializing a boilerplate SessionWindow JSON compatible object. |
| 77 scoped_ptr<base::DictionaryValue> BuildWindowData( | 77 scoped_ptr<base::DictionaryValue> BuildWindowData( |
| 78 base::Time modification_time, | 78 base::Time modification_time, |
| 79 SessionID::id_type window_id) { | 79 SessionID::id_type window_id) { |
| 80 scoped_ptr<base::DictionaryValue> dictionary(new base::DictionaryValue()); | 80 scoped_ptr<base::DictionaryValue> dictionary(new base::DictionaryValue()); |
| 81 // The items which are to be written into |dictionary| are also described in | 81 // The items which are to be written into |dictionary| are also described in |
| 82 // chrome/browser/resources/ntp4/other_sessions.js in @typedef for WindowData. | 82 // chrome/browser/resources/ntp4/other_sessions.js in @typedef for WindowData. |
| 83 // Please update it whenever you add or remove any keys here. | 83 // Please update it whenever you add or remove any keys here. |
| 84 dictionary->SetString("type", "window"); | 84 dictionary->SetString("type", "window"); |
| 85 dictionary->SetDouble("timestamp", modification_time.ToInternalValue()); | 85 dictionary->SetDouble("timestamp", modification_time.ToInternalValue()); |
| 86 const base::TimeDelta last_synced = base::Time::Now() - modification_time; | 86 const base::TimeDelta last_synced = base::Time::Now() - modification_time; |
| 87 // If clock skew leads to a future time, or we last synced less than a minute | 87 // If clock skew leads to a future time, or we last synced less than a minute |
| 88 // ago, output "Just now". | 88 // ago, output "Just now". |
| 89 dictionary->SetString( | 89 dictionary->SetString( |
| 90 "userVisibleTimestamp", | 90 "userVisibleTimestamp", |
| 91 last_synced < base::TimeDelta::FromMinutes(1) | 91 last_synced < base::TimeDelta::FromMinutes(1) |
| 92 ? l10n_util::GetStringUTF16(IDS_SYNC_TIME_JUST_NOW) | 92 ? l10n_util::GetStringUTF16(IDS_SYNC_TIME_JUST_NOW) |
| 93 : ui::TimeFormat::Simple(ui::TimeFormat::FORMAT_ELAPSED, | 93 : ui::TimeFormat::Simple(ui::TimeFormat::FORMAT_ELAPSED, |
| 94 ui::TimeFormat::LENGTH_SHORT, last_synced)); | 94 ui::TimeFormat::LENGTH_SHORT, last_synced)); |
| 95 | 95 |
| 96 dictionary->SetInteger("sessionId", window_id); | 96 dictionary->SetInteger("sessionId", window_id); |
| 97 return dictionary.Pass(); | 97 return dictionary; |
| 98 } | 98 } |
| 99 | 99 |
| 100 // Helper method to create JSON compatible objects from SessionWindow objects. | 100 // Helper method to create JSON compatible objects from SessionWindow objects. |
| 101 scoped_ptr<base::DictionaryValue> SessionWindowToValue( | 101 scoped_ptr<base::DictionaryValue> SessionWindowToValue( |
| 102 const ::sessions::SessionWindow& window) { | 102 const ::sessions::SessionWindow& window) { |
| 103 if (window.tabs.empty()) | 103 if (window.tabs.empty()) |
| 104 return nullptr; | 104 return nullptr; |
| 105 scoped_ptr<base::ListValue> tab_values(new base::ListValue()); | 105 scoped_ptr<base::ListValue> tab_values(new base::ListValue()); |
| 106 // Calculate the last |modification_time| for all entries within a window. | 106 // Calculate the last |modification_time| for all entries within a window. |
| 107 base::Time modification_time = window.timestamp; | 107 base::Time modification_time = window.timestamp; |
| 108 for (const ::sessions::SessionTab* tab : window.tabs) { | 108 for (const ::sessions::SessionTab* tab : window.tabs) { |
| 109 scoped_ptr<base::DictionaryValue> tab_value(SessionTabToValue(*tab)); | 109 scoped_ptr<base::DictionaryValue> tab_value(SessionTabToValue(*tab)); |
| 110 if (tab_value.get()) { | 110 if (tab_value.get()) { |
| 111 modification_time = std::max(modification_time, | 111 modification_time = std::max(modification_time, |
| 112 tab->timestamp); | 112 tab->timestamp); |
| 113 tab_values->Append(tab_value.release()); | 113 tab_values->Append(tab_value.release()); |
| 114 } | 114 } |
| 115 } | 115 } |
| 116 if (tab_values->GetSize() == 0) | 116 if (tab_values->GetSize() == 0) |
| 117 return nullptr; | 117 return nullptr; |
| 118 scoped_ptr<base::DictionaryValue> dictionary( | 118 scoped_ptr<base::DictionaryValue> dictionary( |
| 119 BuildWindowData(window.timestamp, window.window_id.id())); | 119 BuildWindowData(window.timestamp, window.window_id.id())); |
| 120 dictionary->Set("tabs", tab_values.release()); | 120 dictionary->Set("tabs", tab_values.release()); |
| 121 return dictionary.Pass(); | 121 return dictionary; |
| 122 } | 122 } |
| 123 | 123 |
| 124 } // namespace | 124 } // namespace |
| 125 | 125 |
| 126 ForeignSessionHandler::ForeignSessionHandler() : scoped_observer_(this) { | 126 ForeignSessionHandler::ForeignSessionHandler() : scoped_observer_(this) { |
| 127 load_attempt_time_ = base::TimeTicks::Now(); | 127 load_attempt_time_ = base::TimeTicks::Now(); |
| 128 } | 128 } |
| 129 | 129 |
| 130 ForeignSessionHandler::~ForeignSessionHandler() {} | 130 ForeignSessionHandler::~ForeignSessionHandler() {} |
| 131 | 131 |
| (...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 430 // collapsed state persists. | 430 // collapsed state persists. |
| 431 PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs(); | 431 PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs(); |
| 432 DictionaryPrefUpdate update(prefs, prefs::kNtpCollapsedForeignSessions); | 432 DictionaryPrefUpdate update(prefs, prefs::kNtpCollapsedForeignSessions); |
| 433 if (is_collapsed) | 433 if (is_collapsed) |
| 434 update.Get()->SetBoolean(session_tag, true); | 434 update.Get()->SetBoolean(session_tag, true); |
| 435 else | 435 else |
| 436 update.Get()->Remove(session_tag, NULL); | 436 update.Get()->Remove(session_tag, NULL); |
| 437 } | 437 } |
| 438 | 438 |
| 439 } // namespace browser_sync | 439 } // namespace browser_sync |
| OLD | NEW |