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/ntp/foreign_session_handler.h" | 5 #include "chrome/browser/ui/webui/ntp/foreign_session_handler.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 64 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
65 } | 65 } |
66 | 66 |
67 // static | 67 // static |
68 void ForeignSessionHandler::OpenForeignSessionTab( | 68 void ForeignSessionHandler::OpenForeignSessionTab( |
69 content::WebUI* web_ui, | 69 content::WebUI* web_ui, |
70 const std::string& session_string_value, | 70 const std::string& session_string_value, |
71 SessionID::id_type window_num, | 71 SessionID::id_type window_num, |
72 SessionID::id_type tab_id, | 72 SessionID::id_type tab_id, |
73 const WindowOpenDisposition& disposition) { | 73 const WindowOpenDisposition& disposition) { |
74 SessionModelAssociator* associator = GetModelAssociator(web_ui); | 74 OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(web_ui); |
75 if (!associator) | 75 if (!open_tabs) |
76 return; | 76 return; |
77 | 77 |
78 // We don't actually care about |window_num|, this is just a sanity check. | 78 // We don't actually care about |window_num|, this is just a sanity check. |
79 DCHECK_LT(kInvalidId, window_num); | 79 DCHECK_LT(kInvalidId, window_num); |
80 const SessionTab* tab; | 80 const SessionTab* tab; |
81 if (!associator->GetForeignTab(session_string_value, tab_id, &tab)) { | 81 if (!open_tabs->GetForeignTab(session_string_value, tab_id, &tab)) { |
82 LOG(ERROR) << "Failed to load foreign tab."; | 82 LOG(ERROR) << "Failed to load foreign tab."; |
83 return; | 83 return; |
84 } | 84 } |
85 if (tab->navigations.empty()) { | 85 if (tab->navigations.empty()) { |
86 LOG(ERROR) << "Foreign tab no longer has valid navigations."; | 86 LOG(ERROR) << "Foreign tab no longer has valid navigations."; |
87 return; | 87 return; |
88 } | 88 } |
89 SessionRestore::RestoreForeignSessionTab( | 89 SessionRestore::RestoreForeignSessionTab( |
90 web_ui->GetWebContents(), *tab, disposition); | 90 web_ui->GetWebContents(), *tab, disposition); |
91 } | 91 } |
92 | 92 |
93 // static | 93 // static |
94 void ForeignSessionHandler::OpenForeignSessionWindows( | 94 void ForeignSessionHandler::OpenForeignSessionWindows( |
95 content::WebUI* web_ui, | 95 content::WebUI* web_ui, |
96 const std::string& session_string_value, | 96 const std::string& session_string_value, |
97 SessionID::id_type window_num) { | 97 SessionID::id_type window_num) { |
98 SessionModelAssociator* associator = GetModelAssociator(web_ui); | 98 OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(web_ui); |
99 if (!associator) | 99 if (!open_tabs) |
100 return; | 100 return; |
101 | 101 |
102 std::vector<const SessionWindow*> windows; | 102 std::vector<const SessionWindow*> windows; |
103 // Note: we don't own the ForeignSessions themselves. | 103 // Note: we don't own the ForeignSessions themselves. |
104 if (!associator->GetForeignSession(session_string_value, &windows)) { | 104 if (!open_tabs->GetForeignSession(session_string_value, &windows)) { |
105 LOG(ERROR) << "ForeignSessionHandler failed to get session data from" | 105 LOG(ERROR) << "ForeignSessionHandler failed to get session data from" |
106 "SessionModelAssociator."; | 106 "OpenTabsUIDelegate."; |
107 return; | 107 return; |
108 } | 108 } |
109 std::vector<const SessionWindow*>::const_iterator iter_begin = | 109 std::vector<const SessionWindow*>::const_iterator iter_begin = |
110 windows.begin() + (window_num == kInvalidId ? 0 : window_num); | 110 windows.begin() + (window_num == kInvalidId ? 0 : window_num); |
111 std::vector<const SessionWindow*>::const_iterator iter_end = | 111 std::vector<const SessionWindow*>::const_iterator iter_end = |
112 window_num == kInvalidId ? | 112 window_num == kInvalidId ? |
113 std::vector<const SessionWindow*>::const_iterator(windows.end()) : | 113 std::vector<const SessionWindow*>::const_iterator(windows.end()) : |
114 iter_begin + 1; | 114 iter_begin + 1; |
115 chrome::HostDesktopType host_desktop_type = | 115 chrome::HostDesktopType host_desktop_type = |
116 chrome::GetHostDesktopTypeForNativeView( | 116 chrome::GetHostDesktopTypeForNativeView( |
(...skipping 23 matching lines...) Expand all Loading... |
140 dictionary->SetDouble("timestamp", | 140 dictionary->SetDouble("timestamp", |
141 static_cast<double>(tab.timestamp.ToInternalValue())); | 141 static_cast<double>(tab.timestamp.ToInternalValue())); |
142 // TODO(jeremycho): This should probably be renamed to tabId to avoid | 142 // TODO(jeremycho): This should probably be renamed to tabId to avoid |
143 // confusion with the ID corresponding to a session. Investigate all the | 143 // confusion with the ID corresponding to a session. Investigate all the |
144 // places (C++ and JS) where this is being used. (http://crbug.com/154865). | 144 // places (C++ and JS) where this is being used. (http://crbug.com/154865). |
145 dictionary->SetInteger("sessionId", tab.tab_id.id()); | 145 dictionary->SetInteger("sessionId", tab.tab_id.id()); |
146 return true; | 146 return true; |
147 } | 147 } |
148 | 148 |
149 // static | 149 // static |
150 SessionModelAssociator* ForeignSessionHandler::GetModelAssociator( | 150 OpenTabsUIDelegate* ForeignSessionHandler::GetOpenTabsUIDelegate( |
151 content::WebUI* web_ui) { | 151 content::WebUI* web_ui) { |
152 Profile* profile = Profile::FromWebUI(web_ui); | 152 Profile* profile = Profile::FromWebUI(web_ui); |
153 ProfileSyncService* service = | 153 ProfileSyncService* service = |
154 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile); | 154 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile); |
155 | 155 |
156 // Only return the associator if it exists and it is done syncing sessions. | 156 // Only return the delegate if it exists and it is done syncing sessions. |
157 if (service && service->ShouldPushChanges()) | 157 if (service && service->ShouldPushChanges()) |
158 return service->GetSessionModelAssociator(); | 158 return service->GetOpenTabsUIDelegate(); |
159 | 159 |
160 return NULL; | 160 return NULL; |
161 } | 161 } |
162 | 162 |
163 void ForeignSessionHandler::RegisterMessages() { | 163 void ForeignSessionHandler::RegisterMessages() { |
164 Init(); | 164 Init(); |
165 web_ui()->RegisterMessageCallback("deleteForeignSession", | 165 web_ui()->RegisterMessageCallback("deleteForeignSession", |
166 base::Bind(&ForeignSessionHandler::HandleDeleteForeignSession, | 166 base::Bind(&ForeignSessionHandler::HandleDeleteForeignSession, |
167 base::Unretained(this))); | 167 base::Unretained(this))); |
168 web_ui()->RegisterMessageCallback("getForeignSessions", | 168 web_ui()->RegisterMessageCallback("getForeignSessions", |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 | 219 |
220 string16 ForeignSessionHandler::FormatSessionTime(const base::Time& time) { | 220 string16 ForeignSessionHandler::FormatSessionTime(const base::Time& time) { |
221 // Return a time like "1 hour ago", "2 days ago", etc. | 221 // Return a time like "1 hour ago", "2 days ago", etc. |
222 base::Time now = base::Time::Now(); | 222 base::Time now = base::Time::Now(); |
223 // TimeElapsed does not support negative TimeDelta values, so then we use 0. | 223 // TimeElapsed does not support negative TimeDelta values, so then we use 0. |
224 return ui::TimeFormat::TimeElapsed( | 224 return ui::TimeFormat::TimeElapsed( |
225 now < time ? base::TimeDelta() : now - time); | 225 now < time ? base::TimeDelta() : now - time); |
226 } | 226 } |
227 | 227 |
228 void ForeignSessionHandler::HandleGetForeignSessions(const ListValue* args) { | 228 void ForeignSessionHandler::HandleGetForeignSessions(const ListValue* args) { |
229 SessionModelAssociator* associator = GetModelAssociator(web_ui()); | 229 OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(web_ui()); |
230 std::vector<const SyncedSession*> sessions; | 230 std::vector<const SyncedSession*> sessions; |
231 | 231 |
232 ListValue session_list; | 232 ListValue session_list; |
233 if (associator && associator->GetAllForeignSessions(&sessions)) { | 233 if (open_tabs && open_tabs->GetAllForeignSessions(&sessions)) { |
234 // Sort sessions from most recent to least recent. | 234 // Sort sessions from most recent to least recent. |
235 std::sort(sessions.begin(), sessions.end(), SortSessionsByRecency); | 235 std::sort(sessions.begin(), sessions.end(), SortSessionsByRecency); |
236 | 236 |
237 // Use a pref to keep track of sessions that were collapsed by the user. | 237 // Use a pref to keep track of sessions that were collapsed by the user. |
238 // To prevent the pref from accumulating stale sessions, clear it each time | 238 // To prevent the pref from accumulating stale sessions, clear it each time |
239 // and only add back sessions that are still current. | 239 // and only add back sessions that are still current. |
240 DictionaryPrefUpdate pref_update(Profile::FromWebUI(web_ui())->GetPrefs(), | 240 DictionaryPrefUpdate pref_update(Profile::FromWebUI(web_ui())->GetPrefs(), |
241 prefs::kNtpCollapsedForeignSessions); | 241 prefs::kNtpCollapsedForeignSessions); |
242 DictionaryValue* current_collapsed_sessions = pref_update.Get(); | 242 DictionaryValue* current_collapsed_sessions = pref_update.Get(); |
243 scoped_ptr<DictionaryValue> collapsed_sessions( | 243 scoped_ptr<DictionaryValue> collapsed_sessions( |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
332 return; | 332 return; |
333 } | 333 } |
334 | 334 |
335 // Get the session tag argument (required). | 335 // Get the session tag argument (required). |
336 std::string session_tag; | 336 std::string session_tag; |
337 if (!args->GetString(0, &session_tag)) { | 337 if (!args->GetString(0, &session_tag)) { |
338 LOG(ERROR) << "Unable to extract session tag"; | 338 LOG(ERROR) << "Unable to extract session tag"; |
339 return; | 339 return; |
340 } | 340 } |
341 | 341 |
342 SessionModelAssociator* associator = GetModelAssociator(web_ui()); | 342 OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(web_ui()); |
343 if (associator) | 343 if (open_tabs) |
344 associator->DeleteForeignSession(session_tag); | 344 open_tabs->DeleteForeignSession(session_tag); |
345 } | 345 } |
346 | 346 |
347 void ForeignSessionHandler::HandleSetForeignSessionCollapsed( | 347 void ForeignSessionHandler::HandleSetForeignSessionCollapsed( |
348 const ListValue* args) { | 348 const ListValue* args) { |
349 if (args->GetSize() != 2U) { | 349 if (args->GetSize() != 2U) { |
350 LOG(ERROR) << "Wrong number of args to setForeignSessionCollapsed"; | 350 LOG(ERROR) << "Wrong number of args to setForeignSessionCollapsed"; |
351 return; | 351 return; |
352 } | 352 } |
353 | 353 |
354 // Get the session tag argument (required). | 354 // Get the session tag argument (required). |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
402 dictionary->SetString("userVisibleTimestamp", | 402 dictionary->SetString("userVisibleTimestamp", |
403 last_synced < base::TimeDelta::FromMinutes(1) ? | 403 last_synced < base::TimeDelta::FromMinutes(1) ? |
404 l10n_util::GetStringUTF16(IDS_SYNC_TIME_JUST_NOW) : | 404 l10n_util::GetStringUTF16(IDS_SYNC_TIME_JUST_NOW) : |
405 ui::TimeFormat::TimeElapsed(last_synced)); | 405 ui::TimeFormat::TimeElapsed(last_synced)); |
406 dictionary->SetInteger("sessionId", window.window_id.id()); | 406 dictionary->SetInteger("sessionId", window.window_id.id()); |
407 dictionary->Set("tabs", tab_values.release()); | 407 dictionary->Set("tabs", tab_values.release()); |
408 return true; | 408 return true; |
409 } | 409 } |
410 | 410 |
411 } // namespace browser_sync | 411 } // namespace browser_sync |
OLD | NEW |