OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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 "components/sessions/core/persistent_tab_restore_service.h" | 5 #include "components/sessions/core/persistent_tab_restore_service.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 #include <string.h> | 9 #include <string.h> |
10 | 10 #include <utility> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
15 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
16 #include "base/logging.h" | 16 #include "base/logging.h" |
17 #include "base/macros.h" | 17 #include "base/macros.h" |
18 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
19 #include "base/memory/scoped_vector.h" | 19 #include "base/memory/scoped_vector.h" |
20 #include "base/stl_util.h" | 20 #include "base/stl_util.h" |
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
300 } | 300 } |
301 if (base_session_service_->pending_reset()) | 301 if (base_session_service_->pending_reset()) |
302 entries_written_ = 0; | 302 entries_written_ = 0; |
303 } | 303 } |
304 | 304 |
305 void PersistentTabRestoreService::Delegate::OnClearEntries() { | 305 void PersistentTabRestoreService::Delegate::OnClearEntries() { |
306 // Mark all the tabs as closed so that we don't attempt to restore them. | 306 // Mark all the tabs as closed so that we don't attempt to restore them. |
307 const Entries& entries = tab_restore_service_helper_->entries(); | 307 const Entries& entries = tab_restore_service_helper_->entries(); |
308 for (Entries::const_iterator i = entries.begin(); i != entries.end(); ++i) | 308 for (Entries::const_iterator i = entries.begin(); i != entries.end(); ++i) |
309 base_session_service_->ScheduleCommand( | 309 base_session_service_->ScheduleCommand( |
310 CreateRestoredEntryCommand((*i)->id).Pass()); | 310 CreateRestoredEntryCommand((*i)->id)); |
311 | 311 |
312 entries_to_write_ = 0; | 312 entries_to_write_ = 0; |
313 | 313 |
314 // Schedule a pending reset so that we nuke the file on next write. | 314 // Schedule a pending reset so that we nuke the file on next write. |
315 base_session_service_->set_pending_reset(true); | 315 base_session_service_->set_pending_reset(true); |
316 | 316 |
317 // Schedule a command, otherwise if there are no pending commands Save does | 317 // Schedule a command, otherwise if there are no pending commands Save does |
318 // nothing. | 318 // nothing. |
319 base_session_service_->ScheduleCommand(CreateRestoredEntryCommand(1).Pass()); | 319 base_session_service_->ScheduleCommand(CreateRestoredEntryCommand(1)); |
320 } | 320 } |
321 | 321 |
322 void PersistentTabRestoreService::Delegate::OnRestoreEntryById( | 322 void PersistentTabRestoreService::Delegate::OnRestoreEntryById( |
323 SessionID::id_type id, | 323 SessionID::id_type id, |
324 Entries::const_iterator entry_iterator) { | 324 Entries::const_iterator entry_iterator) { |
325 size_t index = 0; | 325 size_t index = 0; |
326 const Entries& entries = tab_restore_service_helper_->entries(); | 326 const Entries& entries = tab_restore_service_helper_->entries(); |
327 for (Entries::const_iterator j = entries.begin(); | 327 for (Entries::const_iterator j = entries.begin(); |
328 j != entry_iterator && j != entries.end(); | 328 j != entry_iterator && j != entries.end(); |
329 ++j, ++index) {} | 329 ++j, ++index) {} |
330 if (static_cast<int>(index) < entries_to_write_) | 330 if (static_cast<int>(index) < entries_to_write_) |
331 entries_to_write_--; | 331 entries_to_write_--; |
332 | 332 |
333 base_session_service_->ScheduleCommand(CreateRestoredEntryCommand(id).Pass()); | 333 base_session_service_->ScheduleCommand(CreateRestoredEntryCommand(id)); |
334 } | 334 } |
335 | 335 |
336 void PersistentTabRestoreService::Delegate::OnAddEntry() { | 336 void PersistentTabRestoreService::Delegate::OnAddEntry() { |
337 // Start the save timer, when it fires we'll generate the commands. | 337 // Start the save timer, when it fires we'll generate the commands. |
338 base_session_service_->StartSaveTimer(); | 338 base_session_service_->StartSaveTimer(); |
339 entries_to_write_++; | 339 entries_to_write_++; |
340 } | 340 } |
341 | 341 |
342 void PersistentTabRestoreService::Delegate::LoadTabsFromLastSession() { | 342 void PersistentTabRestoreService::Delegate::LoadTabsFromLastSession() { |
343 if (load_state_ != NOT_LOADED) | 343 if (load_state_ != NOT_LOADED) |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
400 for (size_t i = 0; i < window.tabs.size(); ++i) { | 400 for (size_t i = 0; i < window.tabs.size(); ++i) { |
401 if (GetSelectedNavigationIndexToPersist(window.tabs[i]) != -1) { | 401 if (GetSelectedNavigationIndexToPersist(window.tabs[i]) != -1) { |
402 valid_tab_count++; | 402 valid_tab_count++; |
403 } else if (static_cast<int>(i) < selected_tab) { | 403 } else if (static_cast<int>(i) < selected_tab) { |
404 real_selected_tab--; | 404 real_selected_tab--; |
405 } | 405 } |
406 } | 406 } |
407 if (valid_tab_count == 0) | 407 if (valid_tab_count == 0) |
408 return; // No tabs to persist. | 408 return; // No tabs to persist. |
409 | 409 |
410 base_session_service_->ScheduleCommand( | 410 base_session_service_->ScheduleCommand(CreateWindowCommand( |
411 CreateWindowCommand(window.id, | 411 window.id, std::min(real_selected_tab, valid_tab_count - 1), |
412 std::min(real_selected_tab, valid_tab_count - 1), | 412 valid_tab_count, window.timestamp)); |
413 valid_tab_count, | |
414 window.timestamp).Pass()); | |
415 | 413 |
416 if (!window.app_name.empty()) { | 414 if (!window.app_name.empty()) { |
417 base_session_service_->ScheduleCommand( | 415 base_session_service_->ScheduleCommand(CreateSetWindowAppNameCommand( |
418 CreateSetWindowAppNameCommand(kCommandSetWindowAppName, window.id, | 416 kCommandSetWindowAppName, window.id, window.app_name)); |
419 window.app_name) | |
420 .Pass()); | |
421 } | 417 } |
422 | 418 |
423 for (size_t i = 0; i < window.tabs.size(); ++i) { | 419 for (size_t i = 0; i < window.tabs.size(); ++i) { |
424 int selected_index = GetSelectedNavigationIndexToPersist(window.tabs[i]); | 420 int selected_index = GetSelectedNavigationIndexToPersist(window.tabs[i]); |
425 if (selected_index != -1) | 421 if (selected_index != -1) |
426 ScheduleCommandsForTab(window.tabs[i], selected_index); | 422 ScheduleCommandsForTab(window.tabs[i], selected_index); |
427 } | 423 } |
428 } | 424 } |
429 | 425 |
430 void PersistentTabRestoreService::Delegate::ScheduleCommandsForTab( | 426 void PersistentTabRestoreService::Delegate::ScheduleCommandsForTab( |
431 const Tab& tab, | 427 const Tab& tab, |
432 int selected_index) { | 428 int selected_index) { |
433 const std::vector<SerializedNavigationEntry>& navigations = tab.navigations; | 429 const std::vector<SerializedNavigationEntry>& navigations = tab.navigations; |
434 int max_index = static_cast<int>(navigations.size()); | 430 int max_index = static_cast<int>(navigations.size()); |
435 | 431 |
436 // Determine the first navigation we'll persist. | 432 // Determine the first navigation we'll persist. |
437 int valid_count_before_selected = 0; | 433 int valid_count_before_selected = 0; |
438 int first_index_to_persist = selected_index; | 434 int first_index_to_persist = selected_index; |
439 for (int i = selected_index - 1; | 435 for (int i = selected_index - 1; |
440 i >= 0 && valid_count_before_selected < gMaxPersistNavigationCount; | 436 i >= 0 && valid_count_before_selected < gMaxPersistNavigationCount; |
441 --i) { | 437 --i) { |
442 if (client_->ShouldTrackURLForRestore(navigations[i].virtual_url())) { | 438 if (client_->ShouldTrackURLForRestore(navigations[i].virtual_url())) { |
443 first_index_to_persist = i; | 439 first_index_to_persist = i; |
444 valid_count_before_selected++; | 440 valid_count_before_selected++; |
445 } | 441 } |
446 } | 442 } |
447 | 443 |
448 // Write the command that identifies the selected tab. | 444 // Write the command that identifies the selected tab. |
449 base_session_service_->ScheduleCommand( | 445 base_session_service_->ScheduleCommand(CreateSelectedNavigationInTabCommand( |
450 CreateSelectedNavigationInTabCommand(tab.id, | 446 tab.id, valid_count_before_selected, tab.timestamp)); |
451 valid_count_before_selected, | |
452 tab.timestamp).Pass()); | |
453 | 447 |
454 if (tab.pinned) { | 448 if (tab.pinned) { |
455 PinnedStatePayload payload = true; | 449 PinnedStatePayload payload = true; |
456 scoped_ptr<SessionCommand> command( | 450 scoped_ptr<SessionCommand> command( |
457 new SessionCommand(kCommandPinnedState, sizeof(payload))); | 451 new SessionCommand(kCommandPinnedState, sizeof(payload))); |
458 memcpy(command->contents(), &payload, sizeof(payload)); | 452 memcpy(command->contents(), &payload, sizeof(payload)); |
459 base_session_service_->ScheduleCommand(command.Pass()); | 453 base_session_service_->ScheduleCommand(std::move(command)); |
460 } | 454 } |
461 | 455 |
462 if (!tab.extension_app_id.empty()) { | 456 if (!tab.extension_app_id.empty()) { |
463 base_session_service_->ScheduleCommand( | 457 base_session_service_->ScheduleCommand(CreateSetTabExtensionAppIDCommand( |
464 CreateSetTabExtensionAppIDCommand(kCommandSetExtensionAppID, tab.id, | 458 kCommandSetExtensionAppID, tab.id, tab.extension_app_id)); |
465 tab.extension_app_id) | |
466 .Pass()); | |
467 } | 459 } |
468 | 460 |
469 if (!tab.user_agent_override.empty()) { | 461 if (!tab.user_agent_override.empty()) { |
470 base_session_service_->ScheduleCommand( | 462 base_session_service_->ScheduleCommand(CreateSetTabUserAgentOverrideCommand( |
471 CreateSetTabUserAgentOverrideCommand(kCommandSetTabUserAgentOverride, | 463 kCommandSetTabUserAgentOverride, tab.id, tab.user_agent_override)); |
472 tab.id, tab.user_agent_override) | |
473 .Pass()); | |
474 } | 464 } |
475 | 465 |
476 // Then write the navigations. | 466 // Then write the navigations. |
477 for (int i = first_index_to_persist, wrote_count = 0; | 467 for (int i = first_index_to_persist, wrote_count = 0; |
478 wrote_count < 2 * gMaxPersistNavigationCount && i < max_index; ++i) { | 468 wrote_count < 2 * gMaxPersistNavigationCount && i < max_index; ++i) { |
479 if (client_->ShouldTrackURLForRestore(navigations[i].virtual_url())) { | 469 if (client_->ShouldTrackURLForRestore(navigations[i].virtual_url())) { |
480 base_session_service_->ScheduleCommand( | 470 base_session_service_->ScheduleCommand( |
481 CreateUpdateTabNavigationCommand(kCommandUpdateTabNavigation, | 471 CreateUpdateTabNavigationCommand(kCommandUpdateTabNavigation, |
482 tab.id, | 472 tab.id, |
483 navigations[i])); | 473 navigations[i])); |
(...skipping 427 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
911 } | 901 } |
912 } | 902 } |
913 } | 903 } |
914 } | 904 } |
915 | 905 |
916 // PersistentTabRestoreService ------------------------------------------------- | 906 // PersistentTabRestoreService ------------------------------------------------- |
917 | 907 |
918 PersistentTabRestoreService::PersistentTabRestoreService( | 908 PersistentTabRestoreService::PersistentTabRestoreService( |
919 scoped_ptr<TabRestoreServiceClient> client, | 909 scoped_ptr<TabRestoreServiceClient> client, |
920 TimeFactory* time_factory) | 910 TimeFactory* time_factory) |
921 : client_(client.Pass()), | 911 : client_(std::move(client)), |
922 delegate_(new Delegate(client_.get())), | 912 delegate_(new Delegate(client_.get())), |
923 helper_(this, delegate_.get(), client_.get(), time_factory) { | 913 helper_(this, delegate_.get(), client_.get(), time_factory) { |
924 delegate_->set_tab_restore_service_helper(&helper_); | 914 delegate_->set_tab_restore_service_helper(&helper_); |
925 } | 915 } |
926 | 916 |
927 PersistentTabRestoreService::~PersistentTabRestoreService() {} | 917 PersistentTabRestoreService::~PersistentTabRestoreService() {} |
928 | 918 |
929 void PersistentTabRestoreService::AddObserver( | 919 void PersistentTabRestoreService::AddObserver( |
930 TabRestoreServiceObserver* observer) { | 920 TabRestoreServiceObserver* observer) { |
931 helper_.AddObserver(observer); | 921 helper_.AddObserver(observer); |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
994 | 984 |
995 TabRestoreService::Entries* PersistentTabRestoreService::mutable_entries() { | 985 TabRestoreService::Entries* PersistentTabRestoreService::mutable_entries() { |
996 return &helper_.entries_; | 986 return &helper_.entries_; |
997 } | 987 } |
998 | 988 |
999 void PersistentTabRestoreService::PruneEntries() { | 989 void PersistentTabRestoreService::PruneEntries() { |
1000 helper_.PruneEntries(); | 990 helper_.PruneEntries(); |
1001 } | 991 } |
1002 | 992 |
1003 } // namespace sessions | 993 } // namespace sessions |
OLD | NEW |