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 #include <utility> | 10 #include <utility> |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 void Shutdown(); | 154 void Shutdown(); |
155 | 155 |
156 // Schedules the commands for a window close. | 156 // Schedules the commands for a window close. |
157 void ScheduleCommandsForWindow(const Window& window); | 157 void ScheduleCommandsForWindow(const Window& window); |
158 | 158 |
159 // Schedules the commands for a tab close. |selected_index| gives the index of | 159 // Schedules the commands for a tab close. |selected_index| gives the index of |
160 // the selected navigation. | 160 // the selected navigation. |
161 void ScheduleCommandsForTab(const Tab& tab, int selected_index); | 161 void ScheduleCommandsForTab(const Tab& tab, int selected_index); |
162 | 162 |
163 // Creates a window close command. | 163 // Creates a window close command. |
164 static scoped_ptr<SessionCommand> CreateWindowCommand(SessionID::id_type id, | 164 static std::unique_ptr<SessionCommand> CreateWindowCommand( |
165 int selected_tab_index, | 165 SessionID::id_type id, |
166 int num_tabs, | 166 int selected_tab_index, |
167 base::Time timestamp); | 167 int num_tabs, |
| 168 base::Time timestamp); |
168 | 169 |
169 // Creates a tab close command. | 170 // Creates a tab close command. |
170 static scoped_ptr<SessionCommand> CreateSelectedNavigationInTabCommand( | 171 static std::unique_ptr<SessionCommand> CreateSelectedNavigationInTabCommand( |
171 SessionID::id_type tab_id, | 172 SessionID::id_type tab_id, |
172 int32_t index, | 173 int32_t index, |
173 base::Time timestamp); | 174 base::Time timestamp); |
174 | 175 |
175 // Creates a restore command. | 176 // Creates a restore command. |
176 static scoped_ptr<SessionCommand> CreateRestoredEntryCommand( | 177 static std::unique_ptr<SessionCommand> CreateRestoredEntryCommand( |
177 SessionID::id_type entry_id); | 178 SessionID::id_type entry_id); |
178 | 179 |
179 // Returns the index to persist as the selected index. This is the same as | 180 // Returns the index to persist as the selected index. This is the same as |
180 // |tab.current_navigation_index| unless the entry at | 181 // |tab.current_navigation_index| unless the entry at |
181 // |tab.current_navigation_index| shouldn't be persisted. Returns -1 if no | 182 // |tab.current_navigation_index| shouldn't be persisted. Returns -1 if no |
182 // valid navigation to persist. | 183 // valid navigation to persist. |
183 int GetSelectedNavigationIndexToPersist(const Tab& tab); | 184 int GetSelectedNavigationIndexToPersist(const Tab& tab); |
184 | 185 |
185 // Invoked when we've loaded the session commands that identify the previously | 186 // Invoked when we've loaded the session commands that identify the previously |
186 // closed tabs. This creates entries, adds them to staging_entries_, and | 187 // closed tabs. This creates entries, adds them to staging_entries_, and |
(...skipping 29 matching lines...) Expand all Loading... |
216 // deleted and removed from both |id_to_entry| and |entries|. This is used | 217 // deleted and removed from both |id_to_entry| and |entries|. This is used |
217 // when creating entries from the backend file. | 218 // when creating entries from the backend file. |
218 void RemoveEntryByID(SessionID::id_type id, | 219 void RemoveEntryByID(SessionID::id_type id, |
219 IDToEntry* id_to_entry, | 220 IDToEntry* id_to_entry, |
220 std::vector<TabRestoreService::Entry*>* entries); | 221 std::vector<TabRestoreService::Entry*>* entries); |
221 | 222 |
222 private: | 223 private: |
223 // The associated client. | 224 // The associated client. |
224 TabRestoreServiceClient* client_; | 225 TabRestoreServiceClient* client_; |
225 | 226 |
226 scoped_ptr<BaseSessionService> base_session_service_; | 227 std::unique_ptr<BaseSessionService> base_session_service_; |
227 | 228 |
228 TabRestoreServiceHelper* tab_restore_service_helper_; | 229 TabRestoreServiceHelper* tab_restore_service_helper_; |
229 | 230 |
230 // The number of entries to write. | 231 // The number of entries to write. |
231 int entries_to_write_; | 232 int entries_to_write_; |
232 | 233 |
233 // Number of entries we've written. | 234 // Number of entries we've written. |
234 int entries_written_; | 235 int entries_written_; |
235 | 236 |
236 // Whether we've loaded the last session. | 237 // Whether we've loaded the last session. |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
374 | 375 |
375 bool PersistentTabRestoreService::Delegate::IsLoaded() const { | 376 bool PersistentTabRestoreService::Delegate::IsLoaded() const { |
376 return !(load_state_ & (NOT_LOADED | LOADING)); | 377 return !(load_state_ & (NOT_LOADED | LOADING)); |
377 } | 378 } |
378 | 379 |
379 // static | 380 // static |
380 void PersistentTabRestoreService::Delegate::CreateEntriesFromWindows( | 381 void PersistentTabRestoreService::Delegate::CreateEntriesFromWindows( |
381 std::vector<SessionWindow*>* windows, | 382 std::vector<SessionWindow*>* windows, |
382 std::vector<Entry*>* entries) { | 383 std::vector<Entry*>* entries) { |
383 for (size_t i = 0; i < windows->size(); ++i) { | 384 for (size_t i = 0; i < windows->size(); ++i) { |
384 scoped_ptr<Window> window(new Window()); | 385 std::unique_ptr<Window> window(new Window()); |
385 if (ConvertSessionWindowToWindow((*windows)[i], window.get())) | 386 if (ConvertSessionWindowToWindow((*windows)[i], window.get())) |
386 entries->push_back(window.release()); | 387 entries->push_back(window.release()); |
387 } | 388 } |
388 } | 389 } |
389 | 390 |
390 void PersistentTabRestoreService::Delegate::Shutdown() { | 391 void PersistentTabRestoreService::Delegate::Shutdown() { |
391 base_session_service_->Save(); | 392 base_session_service_->Save(); |
392 } | 393 } |
393 | 394 |
394 void PersistentTabRestoreService::Delegate::ScheduleCommandsForWindow( | 395 void PersistentTabRestoreService::Delegate::ScheduleCommandsForWindow( |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
440 valid_count_before_selected++; | 441 valid_count_before_selected++; |
441 } | 442 } |
442 } | 443 } |
443 | 444 |
444 // Write the command that identifies the selected tab. | 445 // Write the command that identifies the selected tab. |
445 base_session_service_->ScheduleCommand(CreateSelectedNavigationInTabCommand( | 446 base_session_service_->ScheduleCommand(CreateSelectedNavigationInTabCommand( |
446 tab.id, valid_count_before_selected, tab.timestamp)); | 447 tab.id, valid_count_before_selected, tab.timestamp)); |
447 | 448 |
448 if (tab.pinned) { | 449 if (tab.pinned) { |
449 PinnedStatePayload payload = true; | 450 PinnedStatePayload payload = true; |
450 scoped_ptr<SessionCommand> command( | 451 std::unique_ptr<SessionCommand> command( |
451 new SessionCommand(kCommandPinnedState, sizeof(payload))); | 452 new SessionCommand(kCommandPinnedState, sizeof(payload))); |
452 memcpy(command->contents(), &payload, sizeof(payload)); | 453 memcpy(command->contents(), &payload, sizeof(payload)); |
453 base_session_service_->ScheduleCommand(std::move(command)); | 454 base_session_service_->ScheduleCommand(std::move(command)); |
454 } | 455 } |
455 | 456 |
456 if (!tab.extension_app_id.empty()) { | 457 if (!tab.extension_app_id.empty()) { |
457 base_session_service_->ScheduleCommand(CreateSetTabExtensionAppIDCommand( | 458 base_session_service_->ScheduleCommand(CreateSetTabExtensionAppIDCommand( |
458 kCommandSetExtensionAppID, tab.id, tab.extension_app_id)); | 459 kCommandSetExtensionAppID, tab.id, tab.extension_app_id)); |
459 } | 460 } |
460 | 461 |
461 if (!tab.user_agent_override.empty()) { | 462 if (!tab.user_agent_override.empty()) { |
462 base_session_service_->ScheduleCommand(CreateSetTabUserAgentOverrideCommand( | 463 base_session_service_->ScheduleCommand(CreateSetTabUserAgentOverrideCommand( |
463 kCommandSetTabUserAgentOverride, tab.id, tab.user_agent_override)); | 464 kCommandSetTabUserAgentOverride, tab.id, tab.user_agent_override)); |
464 } | 465 } |
465 | 466 |
466 // Then write the navigations. | 467 // Then write the navigations. |
467 for (int i = first_index_to_persist, wrote_count = 0; | 468 for (int i = first_index_to_persist, wrote_count = 0; |
468 wrote_count < 2 * gMaxPersistNavigationCount && i < max_index; ++i) { | 469 wrote_count < 2 * gMaxPersistNavigationCount && i < max_index; ++i) { |
469 if (client_->ShouldTrackURLForRestore(navigations[i].virtual_url())) { | 470 if (client_->ShouldTrackURLForRestore(navigations[i].virtual_url())) { |
470 base_session_service_->ScheduleCommand( | 471 base_session_service_->ScheduleCommand( |
471 CreateUpdateTabNavigationCommand(kCommandUpdateTabNavigation, | 472 CreateUpdateTabNavigationCommand(kCommandUpdateTabNavigation, |
472 tab.id, | 473 tab.id, |
473 navigations[i])); | 474 navigations[i])); |
474 } | 475 } |
475 } | 476 } |
476 } | 477 } |
477 | 478 |
478 // static | 479 // static |
479 scoped_ptr<SessionCommand> | 480 std::unique_ptr<SessionCommand> |
480 PersistentTabRestoreService::Delegate::CreateWindowCommand( | 481 PersistentTabRestoreService::Delegate::CreateWindowCommand( |
481 SessionID::id_type id, | 482 SessionID::id_type id, |
482 int selected_tab_index, | 483 int selected_tab_index, |
483 int num_tabs, | 484 int num_tabs, |
484 base::Time timestamp) { | 485 base::Time timestamp) { |
485 WindowPayload2 payload; | 486 WindowPayload2 payload; |
486 // |timestamp| is aligned on a 16 byte boundary, leaving 4 bytes of | 487 // |timestamp| is aligned on a 16 byte boundary, leaving 4 bytes of |
487 // uninitialized memory in the struct. | 488 // uninitialized memory in the struct. |
488 memset(&payload, 0, sizeof(payload)); | 489 memset(&payload, 0, sizeof(payload)); |
489 payload.window_id = id; | 490 payload.window_id = id; |
490 payload.selected_tab_index = selected_tab_index; | 491 payload.selected_tab_index = selected_tab_index; |
491 payload.num_tabs = num_tabs; | 492 payload.num_tabs = num_tabs; |
492 payload.timestamp = timestamp.ToInternalValue(); | 493 payload.timestamp = timestamp.ToInternalValue(); |
493 | 494 |
494 scoped_ptr<SessionCommand> command( | 495 std::unique_ptr<SessionCommand> command( |
495 new SessionCommand(kCommandWindow, sizeof(payload))); | 496 new SessionCommand(kCommandWindow, sizeof(payload))); |
496 memcpy(command->contents(), &payload, sizeof(payload)); | 497 memcpy(command->contents(), &payload, sizeof(payload)); |
497 return command; | 498 return command; |
498 } | 499 } |
499 | 500 |
500 // static | 501 // static |
501 scoped_ptr<SessionCommand> | 502 std::unique_ptr<SessionCommand> |
502 PersistentTabRestoreService::Delegate::CreateSelectedNavigationInTabCommand( | 503 PersistentTabRestoreService::Delegate::CreateSelectedNavigationInTabCommand( |
503 SessionID::id_type tab_id, | 504 SessionID::id_type tab_id, |
504 int32_t index, | 505 int32_t index, |
505 base::Time timestamp) { | 506 base::Time timestamp) { |
506 SelectedNavigationInTabPayload2 payload; | 507 SelectedNavigationInTabPayload2 payload; |
507 payload.id = tab_id; | 508 payload.id = tab_id; |
508 payload.index = index; | 509 payload.index = index; |
509 payload.timestamp = timestamp.ToInternalValue(); | 510 payload.timestamp = timestamp.ToInternalValue(); |
510 scoped_ptr<SessionCommand> command( | 511 std::unique_ptr<SessionCommand> command( |
511 new SessionCommand(kCommandSelectedNavigationInTab, sizeof(payload))); | 512 new SessionCommand(kCommandSelectedNavigationInTab, sizeof(payload))); |
512 memcpy(command->contents(), &payload, sizeof(payload)); | 513 memcpy(command->contents(), &payload, sizeof(payload)); |
513 return command; | 514 return command; |
514 } | 515 } |
515 | 516 |
516 // static | 517 // static |
517 scoped_ptr<SessionCommand> | 518 std::unique_ptr<SessionCommand> |
518 PersistentTabRestoreService::Delegate::CreateRestoredEntryCommand( | 519 PersistentTabRestoreService::Delegate::CreateRestoredEntryCommand( |
519 SessionID::id_type entry_id) { | 520 SessionID::id_type entry_id) { |
520 RestoredEntryPayload payload = entry_id; | 521 RestoredEntryPayload payload = entry_id; |
521 scoped_ptr<SessionCommand> command( | 522 std::unique_ptr<SessionCommand> command( |
522 new SessionCommand(kCommandRestoredEntry, sizeof(payload))); | 523 new SessionCommand(kCommandRestoredEntry, sizeof(payload))); |
523 memcpy(command->contents(), &payload, sizeof(payload)); | 524 memcpy(command->contents(), &payload, sizeof(payload)); |
524 return command; | 525 return command; |
525 } | 526 } |
526 | 527 |
527 int PersistentTabRestoreService::Delegate::GetSelectedNavigationIndexToPersist( | 528 int PersistentTabRestoreService::Delegate::GetSelectedNavigationIndexToPersist( |
528 const Tab& tab) { | 529 const Tab& tab) { |
529 const std::vector<SerializedNavigationEntry>& navigations = tab.navigations; | 530 const std::vector<SerializedNavigationEntry>& navigations = tab.navigations; |
530 int selected_index = tab.current_navigation_index; | 531 int selected_index = tab.current_navigation_index; |
531 int max_index = static_cast<int>(navigations.size()); | 532 int max_index = static_cast<int>(navigations.size()); |
(...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
899 return; | 900 return; |
900 } | 901 } |
901 } | 902 } |
902 } | 903 } |
903 } | 904 } |
904 } | 905 } |
905 | 906 |
906 // PersistentTabRestoreService ------------------------------------------------- | 907 // PersistentTabRestoreService ------------------------------------------------- |
907 | 908 |
908 PersistentTabRestoreService::PersistentTabRestoreService( | 909 PersistentTabRestoreService::PersistentTabRestoreService( |
909 scoped_ptr<TabRestoreServiceClient> client, | 910 std::unique_ptr<TabRestoreServiceClient> client, |
910 TimeFactory* time_factory) | 911 TimeFactory* time_factory) |
911 : client_(std::move(client)), | 912 : client_(std::move(client)), |
912 delegate_(new Delegate(client_.get())), | 913 delegate_(new Delegate(client_.get())), |
913 helper_(this, delegate_.get(), client_.get(), time_factory) { | 914 helper_(this, delegate_.get(), client_.get(), time_factory) { |
914 delegate_->set_tab_restore_service_helper(&helper_); | 915 delegate_->set_tab_restore_service_helper(&helper_); |
915 } | 916 } |
916 | 917 |
917 PersistentTabRestoreService::~PersistentTabRestoreService() {} | 918 PersistentTabRestoreService::~PersistentTabRestoreService() {} |
918 | 919 |
919 void PersistentTabRestoreService::AddObserver( | 920 void PersistentTabRestoreService::AddObserver( |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
982 | 983 |
983 TabRestoreService::Entries* PersistentTabRestoreService::mutable_entries() { | 984 TabRestoreService::Entries* PersistentTabRestoreService::mutable_entries() { |
984 return &helper_.entries_; | 985 return &helper_.entries_; |
985 } | 986 } |
986 | 987 |
987 void PersistentTabRestoreService::PruneEntries() { | 988 void PersistentTabRestoreService::PruneEntries() { |
988 helper_.PruneEntries(); | 989 helper_.PruneEntries(); |
989 } | 990 } |
990 | 991 |
991 } // namespace sessions | 992 } // namespace sessions |
OLD | NEW |