| 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/sessions/persistent_tab_restore_service.h" | 5 #include "chrome/browser/sessions/persistent_tab_restore_service.h" |
| 6 | 6 |
| 7 #include <cstring> // memcpy | 7 #include <cstring> // memcpy |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 16 #include "base/memory/scoped_vector.h" | 16 #include "base/memory/scoped_vector.h" |
| 17 #include "base/stl_util.h" | 17 #include "base/stl_util.h" |
| 18 #include "base/task/cancelable_task_tracker.h" | 18 #include "base/task/cancelable_task_tracker.h" |
| 19 #include "base/time/time.h" | 19 #include "base/time/time.h" |
| 20 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
| 21 #include "chrome/browser/sessions/base_session_service.h" | 21 #include "chrome/browser/sessions/base_session_service.h" |
| 22 #include "chrome/browser/sessions/base_session_service_commands.h" |
| 22 #include "chrome/browser/sessions/base_session_service_delegate_impl.h" | 23 #include "chrome/browser/sessions/base_session_service_delegate_impl.h" |
| 23 #include "chrome/browser/sessions/session_command.h" | 24 #include "chrome/browser/sessions/session_command.h" |
| 24 #include "chrome/browser/sessions/session_service.h" | 25 #include "chrome/browser/sessions/session_service.h" |
| 25 #include "chrome/browser/sessions/session_service_factory.h" | 26 #include "chrome/browser/sessions/session_service_factory.h" |
| 26 #include "chrome/browser/sessions/tab_restore_service_factory.h" | 27 #include "chrome/browser/sessions/tab_restore_service_factory.h" |
| 27 #include "content/public/browser/session_storage_namespace.h" | 28 #include "content/public/browser/session_storage_namespace.h" |
| 28 | 29 |
| 29 namespace { | 30 namespace { |
| 30 | 31 |
| 31 // Only written if the tab is pinned. | 32 // Only written if the tab is pinned. |
| (...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 406 if (valid_tab_count == 0) | 407 if (valid_tab_count == 0) |
| 407 return; // No tabs to persist. | 408 return; // No tabs to persist. |
| 408 | 409 |
| 409 ScheduleCommand( | 410 ScheduleCommand( |
| 410 CreateWindowCommand(window.id, | 411 CreateWindowCommand(window.id, |
| 411 std::min(real_selected_tab, valid_tab_count - 1), | 412 std::min(real_selected_tab, valid_tab_count - 1), |
| 412 valid_tab_count, | 413 valid_tab_count, |
| 413 window.timestamp)); | 414 window.timestamp)); |
| 414 | 415 |
| 415 if (!window.app_name.empty()) { | 416 if (!window.app_name.empty()) { |
| 416 ScheduleCommand( | 417 ScheduleCommand(CreateSetWindowAppNameCommand(kCommandSetWindowAppName, |
| 417 CreateSetWindowAppNameCommand(kCommandSetWindowAppName, | 418 window.id, |
| 418 window.id, | 419 window.app_name)); |
| 419 window.app_name)); | |
| 420 } | 420 } |
| 421 | 421 |
| 422 for (size_t i = 0; i < window.tabs.size(); ++i) { | 422 for (size_t i = 0; i < window.tabs.size(); ++i) { |
| 423 int selected_index = GetSelectedNavigationIndexToPersist(window.tabs[i]); | 423 int selected_index = GetSelectedNavigationIndexToPersist(window.tabs[i]); |
| 424 if (selected_index != -1) | 424 if (selected_index != -1) |
| 425 ScheduleCommandsForTab(window.tabs[i], selected_index); | 425 ScheduleCommandsForTab(window.tabs[i], selected_index); |
| 426 } | 426 } |
| 427 } | 427 } |
| 428 | 428 |
| 429 void PersistentTabRestoreService::Delegate::ScheduleCommandsForTab( | 429 void PersistentTabRestoreService::Delegate::ScheduleCommandsForTab( |
| (...skipping 22 matching lines...) Expand all Loading... |
| 452 | 452 |
| 453 if (tab.pinned) { | 453 if (tab.pinned) { |
| 454 PinnedStatePayload payload = true; | 454 PinnedStatePayload payload = true; |
| 455 SessionCommand* command = | 455 SessionCommand* command = |
| 456 new SessionCommand(kCommandPinnedState, sizeof(payload)); | 456 new SessionCommand(kCommandPinnedState, sizeof(payload)); |
| 457 memcpy(command->contents(), &payload, sizeof(payload)); | 457 memcpy(command->contents(), &payload, sizeof(payload)); |
| 458 ScheduleCommand(command); | 458 ScheduleCommand(command); |
| 459 } | 459 } |
| 460 | 460 |
| 461 if (!tab.extension_app_id.empty()) { | 461 if (!tab.extension_app_id.empty()) { |
| 462 ScheduleCommand( | 462 ScheduleCommand(CreateSetTabExtensionAppIDCommand( |
| 463 CreateSetTabExtensionAppIDCommand(kCommandSetExtensionAppID, tab.id, | 463 kCommandSetExtensionAppID, |
| 464 tab.extension_app_id)); | 464 tab.id, |
| 465 tab.extension_app_id)); |
| 465 } | 466 } |
| 466 | 467 |
| 467 if (!tab.user_agent_override.empty()) { | 468 if (!tab.user_agent_override.empty()) { |
| 468 ScheduleCommand( | 469 ScheduleCommand(CreateSetTabUserAgentOverrideCommand( |
| 469 CreateSetTabUserAgentOverrideCommand(kCommandSetTabUserAgentOverride, | 470 kCommandSetTabUserAgentOverride, |
| 470 tab.id, tab.user_agent_override)); | 471 tab.id, |
| 472 tab.user_agent_override)); |
| 471 } | 473 } |
| 472 | 474 |
| 473 // Then write the navigations. | 475 // Then write the navigations. |
| 474 for (int i = first_index_to_persist, wrote_count = 0; | 476 for (int i = first_index_to_persist, wrote_count = 0; |
| 475 i < max_index && wrote_count < 2 * max_persist_navigation_count; ++i) { | 477 i < max_index && wrote_count < 2 * max_persist_navigation_count; ++i) { |
| 476 if (ShouldTrackEntry(navigations[i].virtual_url())) { | 478 if (ShouldTrackEntry(navigations[i].virtual_url())) { |
| 477 ScheduleCommand( | 479 ScheduleCommand(CreateUpdateTabNavigationCommand( |
| 478 CreateUpdateTabNavigationCommand(kCommandUpdateTabNavigation, tab.id, | 480 kCommandUpdateTabNavigation, |
| 479 navigations[i])); | 481 tab.id, |
| 482 navigations[i])); |
| 480 } | 483 } |
| 481 } | 484 } |
| 482 } | 485 } |
| 483 | 486 |
| 484 // static | 487 // static |
| 485 SessionCommand* PersistentTabRestoreService::Delegate::CreateWindowCommand( | 488 SessionCommand* PersistentTabRestoreService::Delegate::CreateWindowCommand( |
| 486 SessionID::id_type id, | 489 SessionID::id_type id, |
| 487 int selected_tab_index, | 490 int selected_tab_index, |
| 488 int num_tabs, | 491 int num_tabs, |
| 489 base::Time timestamp) { | 492 base::Time timestamp) { |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 679 break; | 682 break; |
| 680 } | 683 } |
| 681 | 684 |
| 682 case kCommandUpdateTabNavigation: { | 685 case kCommandUpdateTabNavigation: { |
| 683 if (!current_tab) { | 686 if (!current_tab) { |
| 684 // Should be in a tab when we get this. | 687 // Should be in a tab when we get this. |
| 685 return; | 688 return; |
| 686 } | 689 } |
| 687 current_tab->navigations.resize(current_tab->navigations.size() + 1); | 690 current_tab->navigations.resize(current_tab->navigations.size() + 1); |
| 688 SessionID::id_type tab_id; | 691 SessionID::id_type tab_id; |
| 689 if (!RestoreUpdateTabNavigationCommand( | 692 if (!RestoreUpdateTabNavigationCommand(command, |
| 690 command, ¤t_tab->navigations.back(), &tab_id)) { | 693 ¤t_tab->navigations.back(), |
| 694 &tab_id)) { |
| 691 return; | 695 return; |
| 692 } | 696 } |
| 693 break; | 697 break; |
| 694 } | 698 } |
| 695 | 699 |
| 696 case kCommandPinnedState: { | 700 case kCommandPinnedState: { |
| 697 if (!current_tab) { | 701 if (!current_tab) { |
| 698 // Should be in a tab when we get this. | 702 // Should be in a tab when we get this. |
| 699 return; | 703 return; |
| 700 } | 704 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 720 break; | 724 break; |
| 721 } | 725 } |
| 722 | 726 |
| 723 case kCommandSetExtensionAppID: { | 727 case kCommandSetExtensionAppID: { |
| 724 if (!current_tab) { | 728 if (!current_tab) { |
| 725 // Should be in a tab when we get this. | 729 // Should be in a tab when we get this. |
| 726 return; | 730 return; |
| 727 } | 731 } |
| 728 SessionID::id_type tab_id; | 732 SessionID::id_type tab_id; |
| 729 std::string extension_app_id; | 733 std::string extension_app_id; |
| 730 if (!RestoreSetTabExtensionAppIDCommand(command, &tab_id, | 734 if (!RestoreSetTabExtensionAppIDCommand(command, |
| 735 &tab_id, |
| 731 &extension_app_id)) { | 736 &extension_app_id)) { |
| 732 return; | 737 return; |
| 733 } | 738 } |
| 734 current_tab->extension_app_id.swap(extension_app_id); | 739 current_tab->extension_app_id.swap(extension_app_id); |
| 735 break; | 740 break; |
| 736 } | 741 } |
| 737 | 742 |
| 738 case kCommandSetTabUserAgentOverride: { | 743 case kCommandSetTabUserAgentOverride: { |
| 739 if (!current_tab) { | 744 if (!current_tab) { |
| 740 // Should be in a tab when we get this. | 745 // Should be in a tab when we get this. |
| 741 return; | 746 return; |
| 742 } | 747 } |
| 743 SessionID::id_type tab_id; | 748 SessionID::id_type tab_id; |
| 744 std::string user_agent_override; | 749 std::string user_agent_override; |
| 745 if (!RestoreSetTabUserAgentOverrideCommand(command, &tab_id, | 750 if (!RestoreSetTabUserAgentOverrideCommand(command, |
| 751 &tab_id, |
| 746 &user_agent_override)) { | 752 &user_agent_override)) { |
| 747 return; | 753 return; |
| 748 } | 754 } |
| 749 current_tab->user_agent_override.swap(user_agent_override); | 755 current_tab->user_agent_override.swap(user_agent_override); |
| 750 break; | 756 break; |
| 751 } | 757 } |
| 752 | 758 |
| 753 default: | 759 default: |
| 754 // Unknown type, usually indicates corruption of file. Ignore it. | 760 // Unknown type, usually indicates corruption of file. Ignore it. |
| 755 return; | 761 return; |
| (...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 992 } | 998 } |
| 993 | 999 |
| 994 void PersistentTabRestoreService::PruneEntries() { | 1000 void PersistentTabRestoreService::PruneEntries() { |
| 995 helper_.PruneEntries(); | 1001 helper_.PruneEntries(); |
| 996 } | 1002 } |
| 997 | 1003 |
| 998 KeyedService* TabRestoreServiceFactory::BuildServiceInstanceFor( | 1004 KeyedService* TabRestoreServiceFactory::BuildServiceInstanceFor( |
| 999 content::BrowserContext* profile) const { | 1005 content::BrowserContext* profile) const { |
| 1000 return new PersistentTabRestoreService(static_cast<Profile*>(profile), NULL); | 1006 return new PersistentTabRestoreService(static_cast<Profile*>(profile), NULL); |
| 1001 } | 1007 } |
| OLD | NEW |