Index: components/sessions/core/persistent_tab_restore_service.cc |
diff --git a/components/sessions/core/persistent_tab_restore_service.cc b/components/sessions/core/persistent_tab_restore_service.cc |
index d9016ee0369073fcc59ca8e04381f34fff8eb73c..6d90d0924fd19ca328e118a987ab410732e76fbe 100644 |
--- a/components/sessions/core/persistent_tab_restore_service.cc |
+++ b/components/sessions/core/persistent_tab_restore_service.cc |
@@ -45,15 +45,16 @@ struct SelectedNavigationInTabPayload { |
// Payload used for the start of a window close. This is the old struct that is |
// used for backwards compat when it comes to reading the session files. This |
// struct must be POD, because we memset the contents. |
-struct WindowPayload { |
+struct WindowPayloadObsolete { |
SessionID::id_type window_id; |
int32_t selected_tab_index; |
int32_t num_tabs; |
}; |
-// Payload used for the start of a window close. This struct must be POD, |
-// because we memset the contents. |
-struct WindowPayload2 : WindowPayload { |
+// Payload used for the start of a window close. This struct must be POD, |
+// because we memset the contents. This is an older version of the struct that |
+// is used for backwards compat when it comes to reading the session files. |
+struct WindowPayloadObsolete2 : WindowPayloadObsolete { |
int64_t timestamp; |
}; |
@@ -135,6 +136,170 @@ void RemoveEntryByID( |
} |
} |
+// Converts a window show state to an integer. This function needs to be kept in |
+// sync with IntToWindowShowState below. |
+int WindowShowStateToInt(ui::WindowShowState show_state) { |
sky
2017/07/11 20:28:09
optional: Maybe WindowShowStateToSerializedValue a
chrisha
2017/08/14 14:43:21
Done.
|
+ switch (show_state) { |
+ case ui::SHOW_STATE_DEFAULT: |
+ return 0; |
sky
2017/07/11 20:28:09
Use a local enum with explicit values and a warnin
chrisha
2017/08/14 14:43:21
Done.
|
+ case ui::SHOW_STATE_NORMAL: |
+ return 1; |
+ case ui::SHOW_STATE_MINIMIZED: |
+ return 2; |
+ case ui::SHOW_STATE_MAXIMIZED: |
+ return 3; |
+ case ui::SHOW_STATE_INACTIVE: |
+ return 4; |
+ case ui::SHOW_STATE_FULLSCREEN: |
+ return 5; |
+ case ui::SHOW_STATE_END: |
+ // This should never happen. |
+ NOTREACHED(); |
+ return 6; |
+ } |
+} |
+ |
+// Converts an integer to a window show state. Returns true on success, false |
+// otherwise. |
+bool IntToWindowShowState(int show_state_int, ui::WindowShowState* show_state) { |
+ switch (show_state_int) { |
+ case 0: |
+ *show_state = ui::SHOW_STATE_DEFAULT; |
+ return true; |
+ case 1: |
+ *show_state = ui::SHOW_STATE_NORMAL; |
+ return true; |
+ case 2: |
+ *show_state = ui::SHOW_STATE_MINIMIZED; |
+ return true; |
+ case 3: |
+ *show_state = ui::SHOW_STATE_MAXIMIZED; |
+ return true; |
+ case 4: |
+ *show_state = ui::SHOW_STATE_INACTIVE; |
+ return true; |
+ case 5: |
+ *show_state = ui::SHOW_STATE_FULLSCREEN; |
+ return true; |
+ default: |
+ break; |
+ } |
+ return false; |
+} |
+ |
+// Superset of WindowPayloadObsolete/WindowPayloadObsolete2 and the other fields |
+// that can appear in the Pickle version of a Window command. This is used as a |
+// convenient destination for parsing the various fields in a WindowCommand. |
+struct WindowCommandFields { |
+ // Fields in WindowPayloadObsolete/WindowPayloadObsolete2/Pickle: |
+ int window_id = 0; |
+ int selected_tab_index = 0; |
+ int num_tabs = 0; |
+ |
+ // Fields in WindowPayloadObsolete2/Pickle: |
+ int64_t timestamp = 0; |
+ |
+ // Fields in Pickle: |
+ // Completely zeroed position/dimensions indicates that defaults should be |
+ // used. |
+ int window_x = 0; |
+ int window_y = 0; |
+ int window_width = 0; |
+ int window_height = 0; |
+ int window_show_state = 0; |
+ std::string workspace; |
+}; |
+ |
+std::unique_ptr<sessions::TabRestoreService::Window> |
+CreateWindowEntryFromCommand(const SessionCommand* command, |
+ SessionID::id_type* window_id, |
+ int32_t* num_tabs) { |
+ bool parsed = false; |
+ |
+ WindowCommandFields fields; |
+ ui::WindowShowState show_state = ui::SHOW_STATE_DEFAULT; |
+ |
+ // First try to parse the command as a pickle (the most modern version). |
+ std::unique_ptr<base::Pickle> pickle(command->PayloadAsPickle()); |
sky
2017/07/11 20:28:09
I think this code would be less error prone if you
chrisha
2017/08/14 14:43:21
That's a good point. Done.
|
+ if (pickle.get()) { |
+ base::PickleIterator it(*pickle); |
+ WindowCommandFields parsed_fields; |
+ |
+ // The first version of the pickle contains all of the following fields, so |
+ // they should all successfully parse if the command is in fact a pickle. |
+ if (it.ReadInt(&parsed_fields.window_id) && |
+ it.ReadInt(&parsed_fields.selected_tab_index) && |
+ it.ReadInt(&parsed_fields.num_tabs) && |
+ it.ReadInt64(&parsed_fields.timestamp) && |
+ it.ReadInt(&parsed_fields.window_x) && |
+ it.ReadInt(&parsed_fields.window_y) && |
+ it.ReadInt(&parsed_fields.window_width) && |
sky
2017/07/11 20:28:09
Make sure width/height are non-negative.
chrisha
2017/08/14 14:43:21
Done.
|
+ it.ReadInt(&parsed_fields.window_height) && |
+ it.ReadInt(&parsed_fields.window_show_state) && |
+ it.ReadString(&parsed_fields.workspace)) { |
+ // Validate the window_show_state. If the entire pickle parsed but the |
+ // show state is invalid then assume corruption. |
+ if (!IntToWindowShowState(parsed_fields.window_show_state, &show_state)) |
+ return nullptr; |
+ |
+ // New fields added to the pickle in later versions would be parsed here. |
+ |
+ // Copy the parsed data. |
+ fields = parsed_fields; |
+ parsed = true; |
+ } |
+ } |
+ |
+ // Next try to parse the command as a WindowPayloadObsolete2. |
+ if (!parsed) { |
+ WindowPayloadObsolete2 payload2; |
+ if (command->GetPayload(&payload2, sizeof(payload2))) { |
+ fields.window_id = payload2.window_id; |
+ fields.selected_tab_index = payload2.selected_tab_index; |
+ fields.num_tabs = payload2.num_tabs; |
+ fields.timestamp = payload2.timestamp; |
+ parsed = true; |
+ } |
+ } |
+ |
+ // Finally, try the oldest WindowPayloadObsolete type. |
+ if (!parsed) { |
+ WindowPayloadObsolete payload; |
+ if (command->GetPayload(&payload, sizeof(payload))) { |
+ fields.window_id = payload.window_id; |
+ fields.selected_tab_index = payload.selected_tab_index; |
+ fields.num_tabs = payload.num_tabs; |
+ parsed = true; |
+ } |
+ } |
+ |
+ if (!parsed) |
+ return nullptr; |
+ |
+ // Create the Window entry. |
+ std::unique_ptr<sessions::TabRestoreService::Window> window = |
+ base::MakeUnique<sessions::TabRestoreService::Window>(); |
+ window->selected_tab_index = fields.selected_tab_index; |
+ window->timestamp = base::Time::FromInternalValue(fields.timestamp); |
+ window->selected_tab_index = fields.selected_tab_index; |
+ window->timestamp = base::Time::FromInternalValue(fields.timestamp); |
+ *window_id = static_cast<SessionID::id_type>(fields.window_id); |
+ *num_tabs = fields.num_tabs; |
+ |
+ // Set the bounds, show state and workspace if valid ones have been provided. |
+ if (!(fields.window_x == 0 && fields.window_y == 0 && |
+ fields.window_width == 0 && fields.window_height == 0)) { |
+ window->bounds.SetRect(fields.window_x, fields.window_y, |
+ fields.window_width, fields.window_height); |
+ // |show_state| was converted from window->show_state earlier during |
+ // validation. |
+ window->show_state = show_state; |
+ window->workspace = std::move(fields.workspace); |
+ } |
+ |
+ return window; |
+} |
+ |
} // namespace |
// PersistentTabRestoreService::Delegate --------------------------------------- |
@@ -187,9 +352,12 @@ class PersistentTabRestoreService::Delegate |
// Creates a window close command. |
static std::unique_ptr<SessionCommand> CreateWindowCommand( |
- SessionID::id_type id, |
+ SessionID::id_type window_id, |
int selected_tab_index, |
int num_tabs, |
+ const gfx::Rect& bounds, |
+ ui::WindowShowState show_state, |
+ const std::string& workspace, |
base::Time timestamp); |
// Creates a tab close command. |
@@ -435,7 +603,8 @@ void PersistentTabRestoreService::Delegate::ScheduleCommandsForWindow( |
base_session_service_->ScheduleCommand(CreateWindowCommand( |
window.id, std::min(real_selected_tab, valid_tab_count - 1), |
- valid_tab_count, window.timestamp)); |
+ valid_tab_count, window.bounds, window.show_state, window.workspace, |
+ window.timestamp)); |
if (!window.app_name.empty()) { |
base_session_service_->ScheduleCommand(CreateSetWindowAppNameCommand( |
@@ -504,22 +673,38 @@ void PersistentTabRestoreService::Delegate::ScheduleCommandsForTab( |
// static |
std::unique_ptr<SessionCommand> |
PersistentTabRestoreService::Delegate::CreateWindowCommand( |
- SessionID::id_type id, |
+ SessionID::id_type window_id, |
int selected_tab_index, |
int num_tabs, |
+ const gfx::Rect& bounds, |
+ ui::WindowShowState show_state, |
+ const std::string& workspace, |
base::Time timestamp) { |
- WindowPayload2 payload; |
- // |timestamp| is aligned on a 16 byte boundary, leaving 4 bytes of |
- // uninitialized memory in the struct. |
- memset(&payload, 0, sizeof(payload)); |
- payload.window_id = id; |
- payload.selected_tab_index = selected_tab_index; |
- payload.num_tabs = num_tabs; |
- payload.timestamp = timestamp.ToInternalValue(); |
+ static_assert(sizeof(SessionID::id_type) == sizeof(int), |
+ "SessionID::id_type has changed size."); |
+ |
+ // Use a pickle to handle marshaling as this command contains variable-length |
+ // content. |
+ base::Pickle pickle; |
+ pickle.WriteInt(static_cast<int>(window_id)); |
+ pickle.WriteInt(selected_tab_index); |
+ pickle.WriteInt(num_tabs); |
+ pickle.WriteInt64(timestamp.ToInternalValue()); |
+ pickle.WriteInt(bounds.x()); |
+ pickle.WriteInt(bounds.y()); |
+ pickle.WriteInt(bounds.width()); |
+ pickle.WriteInt(bounds.height()); |
+ pickle.WriteInt(WindowShowStateToInt(show_state)); |
+ |
+ // Enforce a maximum length on workspace names. A common size is 32 bytes for |
+ // GUIDs. |
+ if (workspace.size() <= 128) |
+ pickle.WriteString(workspace); |
+ else |
+ pickle.WriteString(std::string()); |
std::unique_ptr<SessionCommand> command( |
- new SessionCommand(kCommandWindow, sizeof(payload))); |
- memcpy(command->contents(), &payload, sizeof(payload)); |
+ new SessionCommand(kCommandWindow, pickle)); |
return command; |
} |
@@ -625,42 +810,27 @@ void PersistentTabRestoreService::Delegate::CreateEntriesFromCommands( |
} |
case kCommandWindow: { |
- WindowPayload2 payload; |
- if (pending_window_tabs > 0) { |
- // Should never receive a window command while waiting for all the |
- // tabs in a window. |
+ // Should never receive a window command while waiting for all the |
+ // tabs in a window. |
+ if (pending_window_tabs > 0) |
return; |
- } |
- // Try the new payload first |
- if (!command.GetPayload(&payload, sizeof(payload))) { |
- // then the old payload |
- WindowPayload old_payload; |
- if (!command.GetPayload(&old_payload, sizeof(old_payload))) |
- return; |
- |
- // Copy the old payload data to the new payload. |
- payload.window_id = old_payload.window_id; |
- payload.selected_tab_index = old_payload.selected_tab_index; |
- payload.num_tabs = old_payload.num_tabs; |
- // Since we don't have a time use time 0 which is used to mark as an |
- // unknown timestamp. |
- payload.timestamp = 0; |
- } |
- |
- pending_window_tabs = payload.num_tabs; |
- if (pending_window_tabs <= 0) { |
- // Should always have at least 1 tab. Likely indicates corruption. |
+ // Try to parse the command, and silently skip if it fails. |
+ int32_t num_tabs = 0; |
+ SessionID::id_type window_id = 0; |
+ std::unique_ptr<Window> window = |
+ CreateWindowEntryFromCommand(&command, &window_id, &num_tabs); |
+ if (!window) |
return; |
- } |
- RemoveEntryByID(payload.window_id, &entries); |
+ // Should always have at least 1 tab. Likely indicates corruption. |
+ pending_window_tabs = num_tabs; |
+ if (pending_window_tabs <= 0) |
+ return; |
- entries.push_back(base::MakeUnique<Window>()); |
- current_window = static_cast<Window*>(entries.back().get()); |
- current_window->selected_tab_index = payload.selected_tab_index; |
- current_window->timestamp = |
- base::Time::FromInternalValue(payload.timestamp); |
+ RemoveEntryByID(window_id, &entries); |
+ current_window = window.get(); |
+ entries.push_back(std::move(window)); |
break; |
} |
@@ -780,7 +950,6 @@ void PersistentTabRestoreService::Delegate::CreateEntriesFromCommands( |
// If there was corruption some of the entries won't be valid. |
ValidateAndDeleteEmptyEntries(&entries); |
- |
loaded_entries->swap(entries); |
} |
@@ -833,6 +1002,9 @@ bool PersistentTabRestoreService::Delegate::ConvertSessionWindowToWindow( |
std::min(session_window->selected_tab_index, |
static_cast<int>(window->tabs.size() - 1)); |
window->timestamp = base::Time(); |
+ window->bounds = session_window->bounds; |
+ window->show_state = session_window->show_state; |
+ window->workspace = session_window->workspace; |
return true; |
} |