| 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 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 window_struct->focused = false; | 123 window_struct->focused = false; |
| 124 window_struct->type = type; | 124 window_struct->type = type; |
| 125 window_struct->state = state; | 125 window_struct->state = state; |
| 126 return window_struct; | 126 return window_struct; |
| 127 } | 127 } |
| 128 | 128 |
| 129 scoped_ptr<api::sessions::Session> CreateSessionModelHelper( | 129 scoped_ptr<api::sessions::Session> CreateSessionModelHelper( |
| 130 int last_modified, | 130 int last_modified, |
| 131 scoped_ptr<tabs::Tab> tab, | 131 scoped_ptr<tabs::Tab> tab, |
| 132 scoped_ptr<windows::Window> window) { | 132 scoped_ptr<windows::Window> window) { |
| 133 scoped_ptr<api::sessions::Session> session_struct(new api::sessions::Session); | 133 scoped_ptr<api::sessions::Session> session_struct( |
| 134 new api::sessions::Session()); |
| 134 session_struct->last_modified = last_modified; | 135 session_struct->last_modified = last_modified; |
| 135 if (tab) | 136 if (tab) |
| 136 session_struct->tab = std::move(tab); | 137 session_struct->tab = std::move(tab); |
| 137 else if (window) | 138 else if (window) |
| 138 session_struct->window = std::move(window); | 139 session_struct->window = std::move(window); |
| 139 else | 140 else |
| 140 NOTREACHED(); | 141 NOTREACHED(); |
| 141 return session_struct; | 142 return session_struct; |
| 142 } | 143 } |
| 143 | 144 |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 bool SessionsGetRecentlyClosedFunction::RunSync() { | 208 bool SessionsGetRecentlyClosedFunction::RunSync() { |
| 208 scoped_ptr<GetRecentlyClosed::Params> params( | 209 scoped_ptr<GetRecentlyClosed::Params> params( |
| 209 GetRecentlyClosed::Params::Create(*args_)); | 210 GetRecentlyClosed::Params::Create(*args_)); |
| 210 EXTENSION_FUNCTION_VALIDATE(params); | 211 EXTENSION_FUNCTION_VALIDATE(params); |
| 211 int max_results = api::sessions::MAX_SESSION_RESULTS; | 212 int max_results = api::sessions::MAX_SESSION_RESULTS; |
| 212 if (params->filter && params->filter->max_results) | 213 if (params->filter && params->filter->max_results) |
| 213 max_results = *params->filter->max_results; | 214 max_results = *params->filter->max_results; |
| 214 EXTENSION_FUNCTION_VALIDATE(max_results >= 0 && | 215 EXTENSION_FUNCTION_VALIDATE(max_results >= 0 && |
| 215 max_results <= api::sessions::MAX_SESSION_RESULTS); | 216 max_results <= api::sessions::MAX_SESSION_RESULTS); |
| 216 | 217 |
| 217 std::vector<linked_ptr<api::sessions::Session> > result; | 218 std::vector<api::sessions::Session> result; |
| 218 sessions::TabRestoreService* tab_restore_service = | 219 sessions::TabRestoreService* tab_restore_service = |
| 219 TabRestoreServiceFactory::GetForProfile(GetProfile()); | 220 TabRestoreServiceFactory::GetForProfile(GetProfile()); |
| 220 | 221 |
| 221 // TabRestoreServiceFactory::GetForProfile() can return NULL (i.e., when in | 222 // TabRestoreServiceFactory::GetForProfile() can return NULL (i.e., when in |
| 222 // incognito mode) | 223 // incognito mode) |
| 223 if (!tab_restore_service) { | 224 if (!tab_restore_service) { |
| 224 DCHECK_NE(GetProfile(), GetProfile()->GetOriginalProfile()) | 225 DCHECK_NE(GetProfile(), GetProfile()->GetOriginalProfile()) |
| 225 << "sessions::TabRestoreService expected for normal profiles"; | 226 << "sessions::TabRestoreService expected for normal profiles"; |
| 226 results_ = GetRecentlyClosed::Results::Create( | 227 results_ = GetRecentlyClosed::Results::Create(result); |
| 227 std::vector<linked_ptr<api::sessions::Session> >()); | |
| 228 return true; | 228 return true; |
| 229 } | 229 } |
| 230 | 230 |
| 231 // List of entries. They are ordered from most to least recent. | 231 // List of entries. They are ordered from most to least recent. |
| 232 // We prune the list to contain max 25 entries at any time and removes | 232 // We prune the list to contain max 25 entries at any time and removes |
| 233 // uninteresting entries. | 233 // uninteresting entries. |
| 234 sessions::TabRestoreService::Entries entries = tab_restore_service->entries(); | 234 for (const sessions::TabRestoreService::Entry* entry : |
| 235 for (sessions::TabRestoreService::Entries::const_iterator it = | 235 tab_restore_service->entries()) { |
| 236 entries.begin(); | 236 result.push_back(std::move(*CreateSessionModel(entry))); |
| 237 it != entries.end() && static_cast<int>(result.size()) < max_results; | |
| 238 ++it) { | |
| 239 sessions::TabRestoreService::Entry* entry = *it; | |
| 240 result.push_back(make_linked_ptr(CreateSessionModel(entry).release())); | |
| 241 } | 237 } |
| 242 | 238 |
| 243 results_ = GetRecentlyClosed::Results::Create(result); | 239 results_ = GetRecentlyClosed::Results::Create(result); |
| 244 return true; | 240 return true; |
| 245 } | 241 } |
| 246 | 242 |
| 247 scoped_ptr<tabs::Tab> SessionsGetDevicesFunction::CreateTabModel( | 243 scoped_ptr<tabs::Tab> SessionsGetDevicesFunction::CreateTabModel( |
| 248 const std::string& session_tag, | 244 const std::string& session_tag, |
| 249 const sessions::SessionTab& tab, | 245 const sessions::SessionTab& tab, |
| 250 int tab_index, | 246 int tab_index, |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 342 scoped_ptr<windows::Window> window_model( | 338 scoped_ptr<windows::Window> window_model( |
| 343 CreateWindowModel(window, session_tag)); | 339 CreateWindowModel(window, session_tag)); |
| 344 // There is a chance that after pruning uninteresting tabs the window will be | 340 // There is a chance that after pruning uninteresting tabs the window will be |
| 345 // empty. | 341 // empty. |
| 346 return !window_model ? scoped_ptr<api::sessions::Session>() | 342 return !window_model ? scoped_ptr<api::sessions::Session>() |
| 347 : CreateSessionModelHelper(window.timestamp.ToTimeT(), | 343 : CreateSessionModelHelper(window.timestamp.ToTimeT(), |
| 348 scoped_ptr<tabs::Tab>(), | 344 scoped_ptr<tabs::Tab>(), |
| 349 std::move(window_model)); | 345 std::move(window_model)); |
| 350 } | 346 } |
| 351 | 347 |
| 352 scoped_ptr<api::sessions::Device> SessionsGetDevicesFunction::CreateDeviceModel( | 348 api::sessions::Device SessionsGetDevicesFunction::CreateDeviceModel( |
| 353 const sync_driver::SyncedSession* session) { | 349 const sync_driver::SyncedSession* session) { |
| 354 int max_results = api::sessions::MAX_SESSION_RESULTS; | 350 int max_results = api::sessions::MAX_SESSION_RESULTS; |
| 355 // Already validated in RunAsync(). | 351 // Already validated in RunAsync(). |
| 356 scoped_ptr<GetDevices::Params> params(GetDevices::Params::Create(*args_)); | 352 scoped_ptr<GetDevices::Params> params(GetDevices::Params::Create(*args_)); |
| 357 if (params->filter && params->filter->max_results) | 353 if (params->filter && params->filter->max_results) |
| 358 max_results = *params->filter->max_results; | 354 max_results = *params->filter->max_results; |
| 359 | 355 |
| 360 scoped_ptr<api::sessions::Device> device_struct(new api::sessions::Device); | 356 api::sessions::Device device_struct; |
| 361 device_struct->info = session->session_name; | 357 device_struct.info = session->session_name; |
| 362 device_struct->device_name = session->session_name; | 358 device_struct.device_name = session->session_name; |
| 363 | 359 |
| 364 for (sync_driver::SyncedSession::SyncedWindowMap::const_iterator it = | 360 for (sync_driver::SyncedSession::SyncedWindowMap::const_iterator it = |
| 365 session->windows.begin(); | 361 session->windows.begin(); |
| 366 it != session->windows.end() && | 362 it != session->windows.end() && |
| 367 static_cast<int>(device_struct->sessions.size()) < max_results; | 363 static_cast<int>(device_struct.sessions.size()) < max_results; |
| 368 ++it) { | 364 ++it) { |
| 369 scoped_ptr<api::sessions::Session> session_model(CreateSessionModel( | 365 scoped_ptr<api::sessions::Session> session_model(CreateSessionModel( |
| 370 *it->second, session->session_tag)); | 366 *it->second, session->session_tag)); |
| 371 if (session_model) | 367 if (session_model) |
| 372 device_struct->sessions.push_back(make_linked_ptr( | 368 device_struct.sessions.push_back(std::move(*session_model)); |
| 373 session_model.release())); | |
| 374 } | 369 } |
| 375 return device_struct; | 370 return device_struct; |
| 376 } | 371 } |
| 377 | 372 |
| 378 bool SessionsGetDevicesFunction::RunSync() { | 373 bool SessionsGetDevicesFunction::RunSync() { |
| 379 ProfileSyncService* service = | 374 ProfileSyncService* service = |
| 380 ProfileSyncServiceFactory::GetInstance()->GetForProfile(GetProfile()); | 375 ProfileSyncServiceFactory::GetInstance()->GetForProfile(GetProfile()); |
| 381 if (!(service && service->GetPreferredDataTypes().Has(syncer::SESSIONS))) { | 376 if (!(service && service->GetPreferredDataTypes().Has(syncer::SESSIONS))) { |
| 382 // Sync not enabled. | 377 // Sync not enabled. |
| 383 results_ = GetDevices::Results::Create( | 378 results_ = |
| 384 std::vector<linked_ptr<api::sessions::Device> >()); | 379 GetDevices::Results::Create(std::vector<api::sessions::Device>()); |
| 385 return true; | 380 return true; |
| 386 } | 381 } |
| 387 | 382 |
| 388 sync_driver::OpenTabsUIDelegate* open_tabs = service->GetOpenTabsUIDelegate(); | 383 sync_driver::OpenTabsUIDelegate* open_tabs = service->GetOpenTabsUIDelegate(); |
| 389 std::vector<const sync_driver::SyncedSession*> sessions; | 384 std::vector<const sync_driver::SyncedSession*> sessions; |
| 390 if (!(open_tabs && open_tabs->GetAllForeignSessions(&sessions))) { | 385 if (!(open_tabs && open_tabs->GetAllForeignSessions(&sessions))) { |
| 391 results_ = GetDevices::Results::Create( | 386 results_ = |
| 392 std::vector<linked_ptr<api::sessions::Device> >()); | 387 GetDevices::Results::Create(std::vector<api::sessions::Device>()); |
| 393 return true; | 388 return true; |
| 394 } | 389 } |
| 395 | 390 |
| 396 scoped_ptr<GetDevices::Params> params(GetDevices::Params::Create(*args_)); | 391 scoped_ptr<GetDevices::Params> params(GetDevices::Params::Create(*args_)); |
| 397 EXTENSION_FUNCTION_VALIDATE(params); | 392 EXTENSION_FUNCTION_VALIDATE(params); |
| 398 if (params->filter && params->filter->max_results) { | 393 if (params->filter && params->filter->max_results) { |
| 399 EXTENSION_FUNCTION_VALIDATE(*params->filter->max_results >= 0 && | 394 EXTENSION_FUNCTION_VALIDATE(*params->filter->max_results >= 0 && |
| 400 *params->filter->max_results <= api::sessions::MAX_SESSION_RESULTS); | 395 *params->filter->max_results <= api::sessions::MAX_SESSION_RESULTS); |
| 401 } | 396 } |
| 402 | 397 |
| 403 std::vector<linked_ptr<api::sessions::Device> > result; | 398 std::vector<api::sessions::Device> result; |
| 404 // Sort sessions from most recent to least recent. | 399 // Sort sessions from most recent to least recent. |
| 405 std::sort(sessions.begin(), sessions.end(), SortSessionsByRecency); | 400 std::sort(sessions.begin(), sessions.end(), SortSessionsByRecency); |
| 406 for (size_t i = 0; i < sessions.size(); ++i) { | 401 for (size_t i = 0; i < sessions.size(); ++i) |
| 407 result.push_back(make_linked_ptr(CreateDeviceModel(sessions[i]).release())); | 402 result.push_back(CreateDeviceModel(sessions[i])); |
| 408 } | |
| 409 | 403 |
| 410 results_ = GetDevices::Results::Create(result); | 404 results_ = GetDevices::Results::Create(result); |
| 411 return true; | 405 return true; |
| 412 } | 406 } |
| 413 | 407 |
| 414 void SessionsRestoreFunction::SetInvalidIdError(const std::string& invalid_id) { | 408 void SessionsRestoreFunction::SetInvalidIdError(const std::string& invalid_id) { |
| 415 SetError(ErrorUtils::FormatErrorMessage(kInvalidSessionIdError, invalid_id)); | 409 SetError(ErrorUtils::FormatErrorMessage(kInvalidSessionIdError, invalid_id)); |
| 416 } | 410 } |
| 417 | 411 |
| 418 | 412 |
| (...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 650 return g_factory.Pointer(); | 644 return g_factory.Pointer(); |
| 651 } | 645 } |
| 652 | 646 |
| 653 void SessionsAPI::OnListenerAdded(const EventListenerInfo& details) { | 647 void SessionsAPI::OnListenerAdded(const EventListenerInfo& details) { |
| 654 sessions_event_router_.reset( | 648 sessions_event_router_.reset( |
| 655 new SessionsEventRouter(Profile::FromBrowserContext(browser_context_))); | 649 new SessionsEventRouter(Profile::FromBrowserContext(browser_context_))); |
| 656 EventRouter::Get(browser_context_)->UnregisterObserver(this); | 650 EventRouter::Get(browser_context_)->UnregisterObserver(this); |
| 657 } | 651 } |
| 658 | 652 |
| 659 } // namespace extensions | 653 } // namespace extensions |
| OLD | NEW |