OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/extensions/api/sessions/sessions_api.h" | 5 #include "chrome/browser/extensions/api/sessions/sessions_api.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <utility> | 9 #include <utility> |
10 #include <vector> | 10 #include <vector> |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 "There are no recently closed sessions."; | 56 "There are no recently closed sessions."; |
57 const char kInvalidSessionIdError[] = "Invalid session id: \"*\"."; | 57 const char kInvalidSessionIdError[] = "Invalid session id: \"*\"."; |
58 const char kNoBrowserToRestoreSession[] = | 58 const char kNoBrowserToRestoreSession[] = |
59 "There are no browser windows to restore the session."; | 59 "There are no browser windows to restore the session."; |
60 const char kSessionSyncError[] = "Synced sessions are not available."; | 60 const char kSessionSyncError[] = "Synced sessions are not available."; |
61 const char kRestoreInIncognitoError[] = | 61 const char kRestoreInIncognitoError[] = |
62 "Can not restore sessions in incognito mode."; | 62 "Can not restore sessions in incognito mode."; |
63 | 63 |
64 // Comparator function for use with std::sort that will sort sessions by | 64 // Comparator function for use with std::sort that will sort sessions by |
65 // descending modified_time (i.e., most recent first). | 65 // descending modified_time (i.e., most recent first). |
66 bool SortSessionsByRecency(const sync_driver::SyncedSession* s1, | 66 bool SortSessionsByRecency(const sync_sessions::SyncedSession* s1, |
67 const sync_driver::SyncedSession* s2) { | 67 const sync_sessions::SyncedSession* s2) { |
68 return s1->modified_time > s2->modified_time; | 68 return s1->modified_time > s2->modified_time; |
69 } | 69 } |
70 | 70 |
71 // Comparator function for use with std::sort that will sort tabs in a window | 71 // Comparator function for use with std::sort that will sort tabs in a window |
72 // by descending timestamp (i.e., most recent first). | 72 // by descending timestamp (i.e., most recent first). |
73 bool SortTabsByRecency(const sessions::SessionTab* t1, | 73 bool SortTabsByRecency(const sessions::SessionTab* t1, |
74 const sessions::SessionTab* t2) { | 74 const sessions::SessionTab* t2) { |
75 return t1->timestamp > t2->timestamp; | 75 return t1->timestamp > t2->timestamp; |
76 } | 76 } |
77 | 77 |
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 CreateWindowModel(window, session_tag)); | 316 CreateWindowModel(window, session_tag)); |
317 // There is a chance that after pruning uninteresting tabs the window will be | 317 // There is a chance that after pruning uninteresting tabs the window will be |
318 // empty. | 318 // empty. |
319 return !window_model ? std::unique_ptr<api::sessions::Session>() | 319 return !window_model ? std::unique_ptr<api::sessions::Session>() |
320 : CreateSessionModelHelper(window.timestamp.ToTimeT(), | 320 : CreateSessionModelHelper(window.timestamp.ToTimeT(), |
321 std::unique_ptr<tabs::Tab>(), | 321 std::unique_ptr<tabs::Tab>(), |
322 std::move(window_model)); | 322 std::move(window_model)); |
323 } | 323 } |
324 | 324 |
325 api::sessions::Device SessionsGetDevicesFunction::CreateDeviceModel( | 325 api::sessions::Device SessionsGetDevicesFunction::CreateDeviceModel( |
326 const sync_driver::SyncedSession* session) { | 326 const sync_sessions::SyncedSession* session) { |
327 int max_results = api::sessions::MAX_SESSION_RESULTS; | 327 int max_results = api::sessions::MAX_SESSION_RESULTS; |
328 // Already validated in RunAsync(). | 328 // Already validated in RunAsync(). |
329 std::unique_ptr<GetDevices::Params> params( | 329 std::unique_ptr<GetDevices::Params> params( |
330 GetDevices::Params::Create(*args_)); | 330 GetDevices::Params::Create(*args_)); |
331 if (params->filter && params->filter->max_results) | 331 if (params->filter && params->filter->max_results) |
332 max_results = *params->filter->max_results; | 332 max_results = *params->filter->max_results; |
333 | 333 |
334 api::sessions::Device device_struct; | 334 api::sessions::Device device_struct; |
335 device_struct.info = session->session_name; | 335 device_struct.info = session->session_name; |
336 device_struct.device_name = session->session_name; | 336 device_struct.device_name = session->session_name; |
337 | 337 |
338 for (sync_driver::SyncedSession::SyncedWindowMap::const_iterator it = | 338 for (sync_sessions::SyncedSession::SyncedWindowMap::const_iterator it = |
339 session->windows.begin(); | 339 session->windows.begin(); |
340 it != session->windows.end() && | 340 it != session->windows.end() && |
341 static_cast<int>(device_struct.sessions.size()) < max_results; | 341 static_cast<int>(device_struct.sessions.size()) < max_results; |
342 ++it) { | 342 ++it) { |
343 std::unique_ptr<api::sessions::Session> session_model( | 343 std::unique_ptr<api::sessions::Session> session_model( |
344 CreateSessionModel(*it->second, session->session_tag)); | 344 CreateSessionModel(*it->second, session->session_tag)); |
345 if (session_model) | 345 if (session_model) |
346 device_struct.sessions.push_back(std::move(*session_model)); | 346 device_struct.sessions.push_back(std::move(*session_model)); |
347 } | 347 } |
348 return device_struct; | 348 return device_struct; |
349 } | 349 } |
350 | 350 |
351 bool SessionsGetDevicesFunction::RunSync() { | 351 bool SessionsGetDevicesFunction::RunSync() { |
352 ProfileSyncService* service = | 352 ProfileSyncService* service = |
353 ProfileSyncServiceFactory::GetInstance()->GetForProfile(GetProfile()); | 353 ProfileSyncServiceFactory::GetInstance()->GetForProfile(GetProfile()); |
354 if (!(service && service->GetPreferredDataTypes().Has(syncer::SESSIONS))) { | 354 if (!(service && service->GetPreferredDataTypes().Has(syncer::SESSIONS))) { |
355 // Sync not enabled. | 355 // Sync not enabled. |
356 results_ = | 356 results_ = |
357 GetDevices::Results::Create(std::vector<api::sessions::Device>()); | 357 GetDevices::Results::Create(std::vector<api::sessions::Device>()); |
358 return true; | 358 return true; |
359 } | 359 } |
360 | 360 |
361 sync_driver::OpenTabsUIDelegate* open_tabs = service->GetOpenTabsUIDelegate(); | 361 sync_sessions::OpenTabsUIDelegate* open_tabs = |
362 std::vector<const sync_driver::SyncedSession*> sessions; | 362 service->GetOpenTabsUIDelegate(); |
| 363 std::vector<const sync_sessions::SyncedSession*> sessions; |
363 if (!(open_tabs && open_tabs->GetAllForeignSessions(&sessions))) { | 364 if (!(open_tabs && open_tabs->GetAllForeignSessions(&sessions))) { |
364 results_ = | 365 results_ = |
365 GetDevices::Results::Create(std::vector<api::sessions::Device>()); | 366 GetDevices::Results::Create(std::vector<api::sessions::Device>()); |
366 return true; | 367 return true; |
367 } | 368 } |
368 | 369 |
369 std::unique_ptr<GetDevices::Params> params( | 370 std::unique_ptr<GetDevices::Params> params( |
370 GetDevices::Params::Create(*args_)); | 371 GetDevices::Params::Create(*args_)); |
371 EXTENSION_FUNCTION_VALIDATE(params); | 372 EXTENSION_FUNCTION_VALIDATE(params); |
372 if (params->filter && params->filter->max_results) { | 373 if (params->filter && params->filter->max_results) { |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
494 } | 495 } |
495 | 496 |
496 bool SessionsRestoreFunction::RestoreForeignSession(const SessionId& session_id, | 497 bool SessionsRestoreFunction::RestoreForeignSession(const SessionId& session_id, |
497 Browser* browser) { | 498 Browser* browser) { |
498 ProfileSyncService* service = | 499 ProfileSyncService* service = |
499 ProfileSyncServiceFactory::GetInstance()->GetForProfile(GetProfile()); | 500 ProfileSyncServiceFactory::GetInstance()->GetForProfile(GetProfile()); |
500 if (!(service && service->GetPreferredDataTypes().Has(syncer::SESSIONS))) { | 501 if (!(service && service->GetPreferredDataTypes().Has(syncer::SESSIONS))) { |
501 SetError(kSessionSyncError); | 502 SetError(kSessionSyncError); |
502 return false; | 503 return false; |
503 } | 504 } |
504 sync_driver::OpenTabsUIDelegate* open_tabs = service->GetOpenTabsUIDelegate(); | 505 sync_sessions::OpenTabsUIDelegate* open_tabs = |
| 506 service->GetOpenTabsUIDelegate(); |
505 if (!open_tabs) { | 507 if (!open_tabs) { |
506 SetError(kSessionSyncError); | 508 SetError(kSessionSyncError); |
507 return false; | 509 return false; |
508 } | 510 } |
509 | 511 |
510 const sessions::SessionTab* tab = NULL; | 512 const sessions::SessionTab* tab = NULL; |
511 if (open_tabs->GetForeignTab(session_id.session_tag(), | 513 if (open_tabs->GetForeignTab(session_id.session_tag(), |
512 session_id.id(), | 514 session_id.id(), |
513 &tab)) { | 515 &tab)) { |
514 TabStripModel* tab_strip = browser->tab_strip_model(); | 516 TabStripModel* tab_strip = browser->tab_strip_model(); |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
626 return g_factory.Pointer(); | 628 return g_factory.Pointer(); |
627 } | 629 } |
628 | 630 |
629 void SessionsAPI::OnListenerAdded(const EventListenerInfo& details) { | 631 void SessionsAPI::OnListenerAdded(const EventListenerInfo& details) { |
630 sessions_event_router_.reset( | 632 sessions_event_router_.reset( |
631 new SessionsEventRouter(Profile::FromBrowserContext(browser_context_))); | 633 new SessionsEventRouter(Profile::FromBrowserContext(browser_context_))); |
632 EventRouter::Get(browser_context_)->UnregisterObserver(this); | 634 EventRouter::Get(browser_context_)->UnregisterObserver(this); |
633 } | 635 } |
634 | 636 |
635 } // namespace extensions | 637 } // namespace extensions |
OLD | NEW |