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 <memory> | 10 #include <memory> |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 registry->RegisterDictionaryPref(prefs::kNtpCollapsedForeignSessions); | 138 registry->RegisterDictionaryPref(prefs::kNtpCollapsedForeignSessions); |
139 } | 139 } |
140 | 140 |
141 // static | 141 // static |
142 void ForeignSessionHandler::OpenForeignSessionTab( | 142 void ForeignSessionHandler::OpenForeignSessionTab( |
143 content::WebUI* web_ui, | 143 content::WebUI* web_ui, |
144 const std::string& session_string_value, | 144 const std::string& session_string_value, |
145 SessionID::id_type window_num, | 145 SessionID::id_type window_num, |
146 SessionID::id_type tab_id, | 146 SessionID::id_type tab_id, |
147 const WindowOpenDisposition& disposition) { | 147 const WindowOpenDisposition& disposition) { |
148 sync_driver::OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(web_ui); | 148 sync_sessions::OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(web_ui); |
149 if (!open_tabs) | 149 if (!open_tabs) |
150 return; | 150 return; |
151 | 151 |
152 // We don't actually care about |window_num|, this is just a sanity check. | 152 // We don't actually care about |window_num|, this is just a sanity check. |
153 DCHECK_LT(kInvalidId, window_num); | 153 DCHECK_LT(kInvalidId, window_num); |
154 const ::sessions::SessionTab* tab; | 154 const ::sessions::SessionTab* tab; |
155 if (!open_tabs->GetForeignTab(session_string_value, tab_id, &tab)) { | 155 if (!open_tabs->GetForeignTab(session_string_value, tab_id, &tab)) { |
156 LOG(ERROR) << "Failed to load foreign tab."; | 156 LOG(ERROR) << "Failed to load foreign tab."; |
157 return; | 157 return; |
158 } | 158 } |
159 if (tab->navigations.empty()) { | 159 if (tab->navigations.empty()) { |
160 LOG(ERROR) << "Foreign tab no longer has valid navigations."; | 160 LOG(ERROR) << "Foreign tab no longer has valid navigations."; |
161 return; | 161 return; |
162 } | 162 } |
163 SessionRestore::RestoreForeignSessionTab( | 163 SessionRestore::RestoreForeignSessionTab( |
164 web_ui->GetWebContents(), *tab, disposition); | 164 web_ui->GetWebContents(), *tab, disposition); |
165 } | 165 } |
166 | 166 |
167 // static | 167 // static |
168 void ForeignSessionHandler::OpenForeignSessionWindows( | 168 void ForeignSessionHandler::OpenForeignSessionWindows( |
169 content::WebUI* web_ui, | 169 content::WebUI* web_ui, |
170 const std::string& session_string_value, | 170 const std::string& session_string_value, |
171 SessionID::id_type window_num) { | 171 SessionID::id_type window_num) { |
172 sync_driver::OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(web_ui); | 172 sync_sessions::OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(web_ui); |
173 if (!open_tabs) | 173 if (!open_tabs) |
174 return; | 174 return; |
175 | 175 |
176 std::vector<const ::sessions::SessionWindow*> windows; | 176 std::vector<const ::sessions::SessionWindow*> windows; |
177 // Note: we don't own the ForeignSessions themselves. | 177 // Note: we don't own the ForeignSessions themselves. |
178 if (!open_tabs->GetForeignSession(session_string_value, &windows)) { | 178 if (!open_tabs->GetForeignSession(session_string_value, &windows)) { |
179 LOG(ERROR) << "ForeignSessionHandler failed to get session data from" | 179 LOG(ERROR) << "ForeignSessionHandler failed to get session data from" |
180 "OpenTabsUIDelegate."; | 180 "OpenTabsUIDelegate."; |
181 return; | 181 return; |
182 } | 182 } |
183 std::vector<const ::sessions::SessionWindow*>::const_iterator iter_begin = | 183 std::vector<const ::sessions::SessionWindow*>::const_iterator iter_begin = |
184 windows.begin() + (window_num == kInvalidId ? 0 : window_num); | 184 windows.begin() + (window_num == kInvalidId ? 0 : window_num); |
185 std::vector<const ::sessions::SessionWindow*>::const_iterator iter_end = | 185 std::vector<const ::sessions::SessionWindow*>::const_iterator iter_end = |
186 window_num == kInvalidId ? | 186 window_num == kInvalidId ? |
187 std::vector<const ::sessions::SessionWindow*>::const_iterator( | 187 std::vector<const ::sessions::SessionWindow*>::const_iterator( |
188 windows.end()) : iter_begin + 1; | 188 windows.end()) : iter_begin + 1; |
189 SessionRestore::RestoreForeignSessionWindows(Profile::FromWebUI(web_ui), | 189 SessionRestore::RestoreForeignSessionWindows(Profile::FromWebUI(web_ui), |
190 iter_begin, iter_end); | 190 iter_begin, iter_end); |
191 } | 191 } |
192 | 192 |
193 // static | 193 // static |
194 sync_driver::OpenTabsUIDelegate* ForeignSessionHandler::GetOpenTabsUIDelegate( | 194 sync_sessions::OpenTabsUIDelegate* ForeignSessionHandler::GetOpenTabsUIDelegate( |
195 content::WebUI* web_ui) { | 195 content::WebUI* web_ui) { |
196 Profile* profile = Profile::FromWebUI(web_ui); | 196 Profile* profile = Profile::FromWebUI(web_ui); |
197 ProfileSyncService* service = | 197 ProfileSyncService* service = |
198 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile); | 198 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile); |
199 | 199 |
200 // Only return the delegate if it exists and it is done syncing sessions. | 200 // Only return the delegate if it exists and it is done syncing sessions. |
201 if (service && service->IsSyncActive()) | 201 if (service && service->IsSyncActive()) |
202 return service->GetOpenTabsUIDelegate(); | 202 return service->GetOpenTabsUIDelegate(); |
203 | 203 |
204 return NULL; | 204 return NULL; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 // Return a time like "1 hour ago", "2 days ago", etc. | 248 // Return a time like "1 hour ago", "2 days ago", etc. |
249 base::Time now = base::Time::Now(); | 249 base::Time now = base::Time::Now(); |
250 // TimeFormat does not support negative TimeDelta values, so then we use 0. | 250 // TimeFormat does not support negative TimeDelta values, so then we use 0. |
251 return ui::TimeFormat::Simple( | 251 return ui::TimeFormat::Simple( |
252 ui::TimeFormat::FORMAT_ELAPSED, ui::TimeFormat::LENGTH_SHORT, | 252 ui::TimeFormat::FORMAT_ELAPSED, ui::TimeFormat::LENGTH_SHORT, |
253 now < time ? base::TimeDelta() : now - time); | 253 now < time ? base::TimeDelta() : now - time); |
254 } | 254 } |
255 | 255 |
256 void ForeignSessionHandler::HandleGetForeignSessions( | 256 void ForeignSessionHandler::HandleGetForeignSessions( |
257 const base::ListValue* /*args*/) { | 257 const base::ListValue* /*args*/) { |
258 sync_driver::OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(web_ui()); | 258 sync_sessions::OpenTabsUIDelegate* open_tabs = |
259 std::vector<const sync_driver::SyncedSession*> sessions; | 259 GetOpenTabsUIDelegate(web_ui()); |
| 260 std::vector<const sync_sessions::SyncedSession*> sessions; |
260 | 261 |
261 base::ListValue session_list; | 262 base::ListValue session_list; |
262 if (open_tabs && open_tabs->GetAllForeignSessions(&sessions)) { | 263 if (open_tabs && open_tabs->GetAllForeignSessions(&sessions)) { |
263 if (!load_attempt_time_.is_null()) { | 264 if (!load_attempt_time_.is_null()) { |
264 UMA_HISTOGRAM_TIMES("Sync.SessionsRefreshDelay", | 265 UMA_HISTOGRAM_TIMES("Sync.SessionsRefreshDelay", |
265 base::TimeTicks::Now() - load_attempt_time_); | 266 base::TimeTicks::Now() - load_attempt_time_); |
266 load_attempt_time_ = base::TimeTicks(); | 267 load_attempt_time_ = base::TimeTicks(); |
267 } | 268 } |
268 | 269 |
269 // Use a pref to keep track of sessions that were collapsed by the user. | 270 // Use a pref to keep track of sessions that were collapsed by the user. |
270 // To prevent the pref from accumulating stale sessions, clear it each time | 271 // To prevent the pref from accumulating stale sessions, clear it each time |
271 // and only add back sessions that are still current. | 272 // and only add back sessions that are still current. |
272 DictionaryPrefUpdate pref_update(Profile::FromWebUI(web_ui())->GetPrefs(), | 273 DictionaryPrefUpdate pref_update(Profile::FromWebUI(web_ui())->GetPrefs(), |
273 prefs::kNtpCollapsedForeignSessions); | 274 prefs::kNtpCollapsedForeignSessions); |
274 base::DictionaryValue* current_collapsed_sessions = pref_update.Get(); | 275 base::DictionaryValue* current_collapsed_sessions = pref_update.Get(); |
275 std::unique_ptr<base::DictionaryValue> collapsed_sessions( | 276 std::unique_ptr<base::DictionaryValue> collapsed_sessions( |
276 current_collapsed_sessions->DeepCopy()); | 277 current_collapsed_sessions->DeepCopy()); |
277 current_collapsed_sessions->Clear(); | 278 current_collapsed_sessions->Clear(); |
278 | 279 |
279 // Note: we don't own the SyncedSessions themselves. | 280 // Note: we don't own the SyncedSessions themselves. |
280 for (size_t i = 0; i < sessions.size() && i < kMaxSessionsToShow; ++i) { | 281 for (size_t i = 0; i < sessions.size() && i < kMaxSessionsToShow; ++i) { |
281 const sync_driver::SyncedSession* session = sessions[i]; | 282 const sync_sessions::SyncedSession* session = sessions[i]; |
282 const std::string& session_tag = session->session_tag; | 283 const std::string& session_tag = session->session_tag; |
283 std::unique_ptr<base::DictionaryValue> session_data( | 284 std::unique_ptr<base::DictionaryValue> session_data( |
284 new base::DictionaryValue()); | 285 new base::DictionaryValue()); |
285 // The items which are to be written into |session_data| are also | 286 // The items which are to be written into |session_data| are also |
286 // described in chrome/browser/resources/history/externs.js | 287 // described in chrome/browser/resources/history/externs.js |
287 // @typedef for ForeignSession. Please update it whenever you add or | 288 // @typedef for ForeignSession. Please update it whenever you add or |
288 // remove any keys here. | 289 // remove any keys here. |
289 session_data->SetString("tag", session_tag); | 290 session_data->SetString("tag", session_tag); |
290 session_data->SetString("name", session->session_name); | 291 session_data->SetString("name", session->session_name); |
291 session_data->SetString("deviceType", session->DeviceTypeAsString()); | 292 session_data->SetString("deviceType", session->DeviceTypeAsString()); |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
396 return; | 397 return; |
397 } | 398 } |
398 | 399 |
399 // Get the session tag argument (required). | 400 // Get the session tag argument (required). |
400 std::string session_tag; | 401 std::string session_tag; |
401 if (!args->GetString(0, &session_tag)) { | 402 if (!args->GetString(0, &session_tag)) { |
402 LOG(ERROR) << "Unable to extract session tag"; | 403 LOG(ERROR) << "Unable to extract session tag"; |
403 return; | 404 return; |
404 } | 405 } |
405 | 406 |
406 sync_driver::OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(web_ui()); | 407 sync_sessions::OpenTabsUIDelegate* open_tabs = |
| 408 GetOpenTabsUIDelegate(web_ui()); |
407 if (open_tabs) | 409 if (open_tabs) |
408 open_tabs->DeleteForeignSession(session_tag); | 410 open_tabs->DeleteForeignSession(session_tag); |
409 } | 411 } |
410 | 412 |
411 void ForeignSessionHandler::HandleSetForeignSessionCollapsed( | 413 void ForeignSessionHandler::HandleSetForeignSessionCollapsed( |
412 const base::ListValue* args) { | 414 const base::ListValue* args) { |
413 if (args->GetSize() != 2U) { | 415 if (args->GetSize() != 2U) { |
414 LOG(ERROR) << "Wrong number of args to setForeignSessionCollapsed"; | 416 LOG(ERROR) << "Wrong number of args to setForeignSessionCollapsed"; |
415 return; | 417 return; |
416 } | 418 } |
(...skipping 15 matching lines...) Expand all Loading... |
432 // collapsed state persists. | 434 // collapsed state persists. |
433 PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs(); | 435 PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs(); |
434 DictionaryPrefUpdate update(prefs, prefs::kNtpCollapsedForeignSessions); | 436 DictionaryPrefUpdate update(prefs, prefs::kNtpCollapsedForeignSessions); |
435 if (is_collapsed) | 437 if (is_collapsed) |
436 update.Get()->SetBoolean(session_tag, true); | 438 update.Get()->SetBoolean(session_tag, true); |
437 else | 439 else |
438 update.Get()->Remove(session_tag, NULL); | 440 update.Get()->Remove(session_tag, NULL); |
439 } | 441 } |
440 | 442 |
441 } // namespace browser_sync | 443 } // namespace browser_sync |
OLD | NEW |