Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(144)

Unified Diff: chrome/browser/sessions/persistent_tab_restore_service.cc

Issue 672083002: Refactoring of SessionService to get componentized. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: One more self nit Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/sessions/persistent_tab_restore_service.cc
diff --git a/chrome/browser/sessions/persistent_tab_restore_service.cc b/chrome/browser/sessions/persistent_tab_restore_service.cc
index f0be5620b3c17c876b15f9e72351441ba5563451..9ef3ac16c8736b95fae9e2d96ab0a2588c52d8af 100644
--- a/chrome/browser/sessions/persistent_tab_restore_service.cc
+++ b/chrome/browser/sessions/persistent_tab_restore_service.cc
@@ -19,6 +19,7 @@
#include "base/time/time.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sessions/base_session_service.h"
+#include "chrome/browser/sessions/base_session_service_commands.h"
#include "chrome/browser/sessions/base_session_service_delegate_impl.h"
#include "chrome/browser/sessions/session_command.h"
#include "chrome/browser/sessions/session_service.h"
@@ -113,17 +114,18 @@ const size_t kMaxEntries = TabRestoreServiceHelper::kMaxEntries;
// PersistentTabRestoreService::Delegate ---------------------------------------
-// Implements the link between the tab restore service and the session backend.
+// Implements the link between the tab restore service and BaseSessionService.
class PersistentTabRestoreService::Delegate
- : public BaseSessionService,
+ : public BaseSessionServiceDelegateImpl,
public TabRestoreServiceHelper::Observer {
public:
explicit Delegate(Profile* profile);
~Delegate() override;
- // BaseSessionService:
- void Save() override;
+ // BaseSessionServiceDelegateImpl:
+ void OnWillSaveCommands() override;
+ void OnSavedCommands() override;
// TabRestoreServiceHelper::Observer:
void OnClearEntries() override;
@@ -138,6 +140,8 @@ class PersistentTabRestoreService::Delegate
void LoadTabsFromLastSession();
+ void DeleteLastSession();
+
bool IsLoaded() const;
// Creates and add entries to |entries| for each of the windows in |windows|.
@@ -154,19 +158,19 @@ class PersistentTabRestoreService::Delegate
void ScheduleCommandsForTab(const Tab& tab, int selected_index);
// Creates a window close command.
- static SessionCommand* CreateWindowCommand(SessionID::id_type id,
- int selected_tab_index,
- int num_tabs,
- base::Time timestamp);
+ static scoped_ptr<SessionCommand> CreateWindowCommand(SessionID::id_type id,
+ int selected_tab_index,
+ int num_tabs,
+ base::Time timestamp);
// Creates a tab close command.
- static SessionCommand* CreateSelectedNavigationInTabCommand(
+ static scoped_ptr<SessionCommand> CreateSelectedNavigationInTabCommand(
SessionID::id_type tab_id,
int32 index,
base::Time timestamp);
// Creates a restore command.
- static SessionCommand* CreateRestoredEntryCommand(
+ static scoped_ptr<SessionCommand> CreateRestoredEntryCommand(
SessionID::id_type entry_id);
// Returns the index to persist as the selected index. This is the same as
@@ -213,6 +217,8 @@ class PersistentTabRestoreService::Delegate
std::vector<TabRestoreService::Entry*>* entries);
private:
+ scoped_ptr<BaseSessionService> base_session_service_;
+
// The associated profile.
Profile* profile_;
@@ -239,11 +245,11 @@ class PersistentTabRestoreService::Delegate
};
PersistentTabRestoreService::Delegate::Delegate(Profile* profile)
- : BaseSessionService(
- BaseSessionService::TAB_RESTORE,
- profile->GetPath(),
- scoped_ptr<BaseSessionServiceDelegate>(
- new BaseSessionServiceDelegateImpl(true))),
+ : BaseSessionServiceDelegateImpl(true),
+ base_session_service_(
+ new BaseSessionService(BaseSessionService::TAB_RESTORE,
+ profile->GetPath(),
+ this)),
profile_(profile),
tab_restore_service_helper_(NULL),
entries_to_write_(0),
@@ -255,14 +261,14 @@ PersistentTabRestoreService::Delegate::Delegate(Profile* profile)
PersistentTabRestoreService::Delegate::~Delegate() {}
-void PersistentTabRestoreService::Delegate::Save() {
+void PersistentTabRestoreService::Delegate::OnWillSaveCommands() {
const Entries& entries = tab_restore_service_helper_->entries();
int to_write_count = std::min(entries_to_write_,
static_cast<int>(entries.size()));
entries_to_write_ = 0;
if (entries_written_ + to_write_count > kEntriesPerReset) {
to_write_count = entries.size();
- set_pending_reset(true);
+ base_session_service_->set_pending_reset(true);
}
if (to_write_count) {
// Write the to_write_count most recently added entries out. The most
@@ -284,25 +290,27 @@ void PersistentTabRestoreService::Delegate::Save() {
entries_written_++;
}
}
- if (pending_reset())
+ if (base_session_service_->pending_reset())
entries_written_ = 0;
- BaseSessionService::Save();
}
+void PersistentTabRestoreService::Delegate::OnSavedCommands() {}
+
void PersistentTabRestoreService::Delegate::OnClearEntries() {
// Mark all the tabs as closed so that we don't attempt to restore them.
const Entries& entries = tab_restore_service_helper_->entries();
for (Entries::const_iterator i = entries.begin(); i != entries.end(); ++i)
- ScheduleCommand(CreateRestoredEntryCommand((*i)->id));
+ base_session_service_->ScheduleCommand(
+ CreateRestoredEntryCommand((*i)->id).Pass());
entries_to_write_ = 0;
// Schedule a pending reset so that we nuke the file on next write.
- set_pending_reset(true);
+ base_session_service_->set_pending_reset(true);
// Schedule a command, otherwise if there are no pending commands Save does
// nothing.
- ScheduleCommand(CreateRestoredEntryCommand(1));
+ base_session_service_->ScheduleCommand(CreateRestoredEntryCommand(1).Pass());
}
void PersistentTabRestoreService::Delegate::OnRestoreEntryById(
@@ -316,12 +324,12 @@ void PersistentTabRestoreService::Delegate::OnRestoreEntryById(
if (static_cast<int>(index) < entries_to_write_)
entries_to_write_--;
- ScheduleCommand(CreateRestoredEntryCommand(id));
+ base_session_service_->ScheduleCommand(CreateRestoredEntryCommand(id).Pass());
}
void PersistentTabRestoreService::Delegate::OnAddEntry() {
// Start the save timer, when it fires we'll generate the commands.
- StartSaveTimer();
+ base_session_service_->StartSaveTimer();
entries_to_write_++;
}
@@ -365,11 +373,15 @@ void PersistentTabRestoreService::Delegate::LoadTabsFromLastSession() {
// Request the tabs closed in the last session. If the last session crashed,
// this won't contain the tabs/window that were open at the point of the
// crash (the call to GetLastSession above requests those).
- ScheduleGetLastSessionCommands(
+ base_session_service_->ScheduleGetLastSessionCommands(
base::Bind(&Delegate::OnGotLastSessionCommands, base::Unretained(this)),
&cancelable_task_tracker_);
}
+void PersistentTabRestoreService::Delegate::DeleteLastSession() {
+ base_session_service_->DeleteLastSession();
+}
+
bool PersistentTabRestoreService::Delegate::IsLoaded() const {
return !(load_state_ & (NOT_LOADED | LOADING));
}
@@ -386,8 +398,7 @@ void PersistentTabRestoreService::Delegate::CreateEntriesFromWindows(
}
void PersistentTabRestoreService::Delegate::Shutdown() {
- if (backend())
- Save();
+ base_session_service_->SaveNow();
}
void PersistentTabRestoreService::Delegate::ScheduleCommandsForWindow(
@@ -406,17 +417,18 @@ void PersistentTabRestoreService::Delegate::ScheduleCommandsForWindow(
if (valid_tab_count == 0)
return; // No tabs to persist.
- ScheduleCommand(
+ base_session_service_->ScheduleCommand(
CreateWindowCommand(window.id,
std::min(real_selected_tab, valid_tab_count - 1),
valid_tab_count,
- window.timestamp));
+ window.timestamp).Pass());
if (!window.app_name.empty()) {
- ScheduleCommand(
- CreateSetWindowAppNameCommand(kCommandSetWindowAppName,
- window.id,
- window.app_name));
+ base_session_service_->ScheduleCommand(
+ BaseSessionServiceCommands::CreateSetWindowAppNameCommand(
+ kCommandSetWindowAppName,
+ window.id,
+ window.app_name).Pass());
}
for (size_t i = 0; i < window.tabs.size(); ++i) {
@@ -437,7 +449,8 @@ void PersistentTabRestoreService::Delegate::ScheduleCommandsForTab(
int valid_count_before_selected = 0;
int first_index_to_persist = selected_index;
for (int i = selected_index - 1; i >= 0 &&
- valid_count_before_selected < max_persist_navigation_count; --i) {
+ valid_count_before_selected <
+ BaseSessionService::max_persist_navigation_count; --i) {
if (ShouldTrackEntry(navigations[i].virtual_url())) {
first_index_to_persist = i;
valid_count_before_selected++;
@@ -445,44 +458,52 @@ void PersistentTabRestoreService::Delegate::ScheduleCommandsForTab(
}
// Write the command that identifies the selected tab.
- ScheduleCommand(
+ base_session_service_->ScheduleCommand(
CreateSelectedNavigationInTabCommand(tab.id,
valid_count_before_selected,
- tab.timestamp));
+ tab.timestamp).Pass());
if (tab.pinned) {
PinnedStatePayload payload = true;
- SessionCommand* command =
- new SessionCommand(kCommandPinnedState, sizeof(payload));
+ scoped_ptr<SessionCommand> command(
+ new SessionCommand(kCommandPinnedState, sizeof(payload)));
memcpy(command->contents(), &payload, sizeof(payload));
- ScheduleCommand(command);
+ base_session_service_->ScheduleCommand(command.Pass());
}
if (!tab.extension_app_id.empty()) {
- ScheduleCommand(
- CreateSetTabExtensionAppIDCommand(kCommandSetExtensionAppID, tab.id,
- tab.extension_app_id));
+ base_session_service_->ScheduleCommand(
+ BaseSessionServiceCommands::CreateSetTabExtensionAppIDCommand(
+ kCommandSetExtensionAppID,
+ tab.id,
+ tab.extension_app_id).Pass());
}
if (!tab.user_agent_override.empty()) {
- ScheduleCommand(
- CreateSetTabUserAgentOverrideCommand(kCommandSetTabUserAgentOverride,
- tab.id, tab.user_agent_override));
+ base_session_service_->ScheduleCommand(
+ BaseSessionServiceCommands::CreateSetTabUserAgentOverrideCommand(
+ kCommandSetTabUserAgentOverride,
+ tab.id,
+ tab.user_agent_override).Pass());
}
// Then write the navigations.
for (int i = first_index_to_persist, wrote_count = 0;
- i < max_index && wrote_count < 2 * max_persist_navigation_count; ++i) {
+ wrote_count < 2 * BaseSessionService::max_persist_navigation_count &&
+ i < max_index; ++i) {
if (ShouldTrackEntry(navigations[i].virtual_url())) {
- ScheduleCommand(
- CreateUpdateTabNavigationCommand(kCommandUpdateTabNavigation, tab.id,
- navigations[i]));
+ base_session_service_->ScheduleCommand(
+ BaseSessionServiceCommands::CreateUpdateTabNavigationCommand(
+ kCommandUpdateTabNavigation,
+ tab.id,
+ navigations[i]));
}
}
}
// static
-SessionCommand* PersistentTabRestoreService::Delegate::CreateWindowCommand(
+scoped_ptr<SessionCommand>
+PersistentTabRestoreService::Delegate::CreateWindowCommand(
SessionID::id_type id,
int selected_tab_index,
int num_tabs,
@@ -496,14 +517,14 @@ SessionCommand* PersistentTabRestoreService::Delegate::CreateWindowCommand(
payload.num_tabs = num_tabs;
payload.timestamp = timestamp.ToInternalValue();
- SessionCommand* command =
- new SessionCommand(kCommandWindow, sizeof(payload));
+ scoped_ptr<SessionCommand> command(
+ new SessionCommand(kCommandWindow, sizeof(payload)));
memcpy(command->contents(), &payload, sizeof(payload));
- return command;
+ return command.Pass();
}
// static
-SessionCommand*
+scoped_ptr<SessionCommand>
PersistentTabRestoreService::Delegate::CreateSelectedNavigationInTabCommand(
SessionID::id_type tab_id,
int32 index,
@@ -512,21 +533,21 @@ PersistentTabRestoreService::Delegate::CreateSelectedNavigationInTabCommand(
payload.id = tab_id;
payload.index = index;
payload.timestamp = timestamp.ToInternalValue();
- SessionCommand* command =
- new SessionCommand(kCommandSelectedNavigationInTab, sizeof(payload));
+ scoped_ptr<SessionCommand> command(
+ new SessionCommand(kCommandSelectedNavigationInTab, sizeof(payload)));
memcpy(command->contents(), &payload, sizeof(payload));
- return command;
+ return command.Pass();
}
// static
-SessionCommand*
+scoped_ptr<SessionCommand>
PersistentTabRestoreService::Delegate::CreateRestoredEntryCommand(
SessionID::id_type entry_id) {
RestoredEntryPayload payload = entry_id;
- SessionCommand* command =
- new SessionCommand(kCommandRestoredEntry, sizeof(payload));
+ scoped_ptr<SessionCommand> command(
+ new SessionCommand(kCommandRestoredEntry, sizeof(payload)));
memcpy(command->contents(), &payload, sizeof(payload));
- return command;
+ return command.Pass();
}
int PersistentTabRestoreService::Delegate::GetSelectedNavigationIndexToPersist(
@@ -686,8 +707,10 @@ void PersistentTabRestoreService::Delegate::CreateEntriesFromCommands(
}
current_tab->navigations.resize(current_tab->navigations.size() + 1);
SessionID::id_type tab_id;
- if (!RestoreUpdateTabNavigationCommand(
- command, &current_tab->navigations.back(), &tab_id)) {
+ if (!BaseSessionServiceCommands::RestoreUpdateTabNavigationCommand(
+ command,
+ &current_tab->navigations.back(),
+ &tab_id)) {
return;
}
break;
@@ -713,8 +736,12 @@ void PersistentTabRestoreService::Delegate::CreateEntriesFromCommands(
SessionID::id_type window_id;
std::string app_name;
- if (!RestoreSetWindowAppNameCommand(command, &window_id, &app_name))
+ if (!BaseSessionServiceCommands::RestoreSetWindowAppNameCommand(
+ command,
+ &window_id,
+ &app_name)) {
return;
+ }
current_window->app_name.swap(app_name);
break;
@@ -727,8 +754,10 @@ void PersistentTabRestoreService::Delegate::CreateEntriesFromCommands(
}
SessionID::id_type tab_id;
std::string extension_app_id;
- if (!RestoreSetTabExtensionAppIDCommand(command, &tab_id,
- &extension_app_id)) {
+ if (!BaseSessionServiceCommands::RestoreSetTabExtensionAppIDCommand(
+ command,
+ &tab_id,
+ &extension_app_id)) {
return;
}
current_tab->extension_app_id.swap(extension_app_id);
@@ -742,8 +771,10 @@ void PersistentTabRestoreService::Delegate::CreateEntriesFromCommands(
}
SessionID::id_type tab_id;
std::string user_agent_override;
- if (!RestoreSetTabUserAgentOverrideCommand(command, &tab_id,
- &user_agent_override)) {
+ if (!BaseSessionServiceCommands::RestoreSetTabUserAgentOverrideCommand(
+ command,
+ &tab_id,
+ &user_agent_override)) {
return;
}
current_tab->user_agent_override.swap(user_agent_override);

Powered by Google App Engine
This is Rietveld 408576698