Chromium Code Reviews| 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 "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_delegate_impl.h" | 21 #include "chrome/browser/sessions/base_session_service_delegate_impl.h" |
| 22 #include "chrome/browser/sessions/session_service_factory.h" | |
| 23 #include "chrome/browser/sessions/tab_restore_service_factory.h" | |
| 24 #include "components/sessions/base_session_service.h" | 22 #include "components/sessions/base_session_service.h" |
| 25 #include "components/sessions/base_session_service_commands.h" | 23 #include "components/sessions/base_session_service_commands.h" |
| 26 #include "components/sessions/session_command.h" | 24 #include "components/sessions/session_command.h" |
| 27 #include "content/public/browser/session_storage_namespace.h" | 25 #include "content/public/browser/session_storage_namespace.h" |
| 28 | 26 |
| 29 namespace { | 27 namespace { |
| 30 | 28 |
| 31 // Only written if the tab is pinned. | 29 // Only written if the tab is pinned. |
| 32 typedef bool PinnedStatePayload; | 30 typedef bool PinnedStatePayload; |
| 33 | 31 |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 112 } // namespace | 110 } // namespace |
| 113 | 111 |
| 114 // PersistentTabRestoreService::Delegate --------------------------------------- | 112 // PersistentTabRestoreService::Delegate --------------------------------------- |
| 115 | 113 |
| 116 // This restore service will create and own a BaseSessionService and implement | 114 // This restore service will create and own a BaseSessionService and implement |
| 117 // the required BaseSessionServiceDelegateImpl. | 115 // the required BaseSessionServiceDelegateImpl. |
| 118 class PersistentTabRestoreService::Delegate | 116 class PersistentTabRestoreService::Delegate |
| 119 : public BaseSessionServiceDelegateImpl, | 117 : public BaseSessionServiceDelegateImpl, |
| 120 public TabRestoreServiceHelper::Observer { | 118 public TabRestoreServiceHelper::Observer { |
| 121 public: | 119 public: |
| 122 explicit Delegate(Profile* profile); | 120 explicit Delegate(Profile* profile, |
|
sky
2015/09/08 23:44:59
nit: remove explicit.
blundell
2015/09/09 10:27:10
Done.
| |
| 121 sessions::TabRestoreServiceClient* client); | |
| 123 | 122 |
| 124 ~Delegate() override; | 123 ~Delegate() override; |
| 125 | 124 |
| 126 // BaseSessionServiceDelegateImpl: | 125 // BaseSessionServiceDelegateImpl: |
| 127 void OnWillSaveCommands() override; | 126 void OnWillSaveCommands() override; |
| 128 | 127 |
| 129 // TabRestoreServiceHelper::Observer: | 128 // TabRestoreServiceHelper::Observer: |
| 130 void OnClearEntries() override; | 129 void OnClearEntries() override; |
| 131 void OnRestoreEntryById(SessionID::id_type id, | 130 void OnRestoreEntryById(SessionID::id_type id, |
| 132 Entries::const_iterator entry_iterator) override; | 131 Entries::const_iterator entry_iterator) override; |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 220 void RemoveEntryByID(SessionID::id_type id, | 219 void RemoveEntryByID(SessionID::id_type id, |
| 221 IDToEntry* id_to_entry, | 220 IDToEntry* id_to_entry, |
| 222 std::vector<TabRestoreService::Entry*>* entries); | 221 std::vector<TabRestoreService::Entry*>* entries); |
| 223 | 222 |
| 224 private: | 223 private: |
| 225 scoped_ptr<sessions::BaseSessionService> base_session_service_; | 224 scoped_ptr<sessions::BaseSessionService> base_session_service_; |
| 226 | 225 |
| 227 // The associated profile. | 226 // The associated profile. |
| 228 Profile* profile_; | 227 Profile* profile_; |
| 229 | 228 |
| 229 // The associated client. | |
| 230 sessions::TabRestoreServiceClient* client_; | |
| 231 | |
| 230 TabRestoreServiceHelper* tab_restore_service_helper_; | 232 TabRestoreServiceHelper* tab_restore_service_helper_; |
| 231 | 233 |
| 232 // The number of entries to write. | 234 // The number of entries to write. |
| 233 int entries_to_write_; | 235 int entries_to_write_; |
| 234 | 236 |
| 235 // Number of entries we've written. | 237 // Number of entries we've written. |
| 236 int entries_written_; | 238 int entries_written_; |
| 237 | 239 |
| 238 // Whether we've loaded the last session. | 240 // Whether we've loaded the last session. |
| 239 int load_state_; | 241 int load_state_; |
| 240 | 242 |
| 241 // Results from previously closed tabs/sessions is first added here. When the | 243 // Results from previously closed tabs/sessions is first added here. When the |
| 242 // results from both us and the session restore service have finished loading | 244 // results from both us and the session restore service have finished loading |
| 243 // LoadStateChanged is invoked, which adds these entries to entries_. | 245 // LoadStateChanged is invoked, which adds these entries to entries_. |
| 244 ScopedVector<Entry> staging_entries_; | 246 ScopedVector<Entry> staging_entries_; |
| 245 | 247 |
| 246 // Used when loading previous tabs/session and open tabs/session. | 248 // Used when loading previous tabs/session and open tabs/session. |
| 247 base::CancelableTaskTracker cancelable_task_tracker_; | 249 base::CancelableTaskTracker cancelable_task_tracker_; |
| 248 | 250 |
| 249 DISALLOW_COPY_AND_ASSIGN(Delegate); | 251 DISALLOW_COPY_AND_ASSIGN(Delegate); |
| 250 }; | 252 }; |
| 251 | 253 |
| 252 PersistentTabRestoreService::Delegate::Delegate(Profile* profile) | 254 PersistentTabRestoreService::Delegate::Delegate( |
| 255 Profile* profile, | |
| 256 sessions::TabRestoreServiceClient* client) | |
| 253 : BaseSessionServiceDelegateImpl(true), | 257 : BaseSessionServiceDelegateImpl(true), |
| 254 base_session_service_( | 258 base_session_service_(new sessions::BaseSessionService( |
| 255 new sessions::BaseSessionService( | 259 sessions::BaseSessionService::TAB_RESTORE, |
| 256 sessions::BaseSessionService::TAB_RESTORE, | 260 profile->GetPath(), |
| 257 profile->GetPath(), | 261 this)), |
| 258 this)), | |
| 259 profile_(profile), | 262 profile_(profile), |
| 263 client_(client), | |
| 260 tab_restore_service_helper_(NULL), | 264 tab_restore_service_helper_(NULL), |
| 261 entries_to_write_(0), | 265 entries_to_write_(0), |
| 262 entries_written_(0), | 266 entries_written_(0), |
| 263 load_state_(NOT_LOADED) { | 267 load_state_(NOT_LOADED) { |
| 264 // We should never be created when incognito. | 268 // We should never be created when incognito. |
| 265 DCHECK(!profile->IsOffTheRecord()); | 269 DCHECK(!profile->IsOffTheRecord()); |
| 266 } | 270 } |
| 267 | 271 |
| 268 PersistentTabRestoreService::Delegate::~Delegate() {} | 272 PersistentTabRestoreService::Delegate::~Delegate() {} |
| 269 | 273 |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 342 return; | 346 return; |
| 343 | 347 |
| 344 if (tab_restore_service_helper_->entries().size() == kMaxEntries) { | 348 if (tab_restore_service_helper_->entries().size() == kMaxEntries) { |
| 345 // We already have the max number of entries we can take. There is no point | 349 // We already have the max number of entries we can take. There is no point |
| 346 // in attempting to load since we'll just drop the results. Skip to loaded. | 350 // in attempting to load since we'll just drop the results. Skip to loaded. |
| 347 load_state_ = (LOADING | LOADED_LAST_SESSION | LOADED_LAST_TABS); | 351 load_state_ = (LOADING | LOADED_LAST_SESSION | LOADED_LAST_TABS); |
| 348 LoadStateChanged(); | 352 LoadStateChanged(); |
| 349 return; | 353 return; |
| 350 } | 354 } |
| 351 | 355 |
| 352 #if !defined(ENABLE_SESSION_SERVICE) | |
| 353 // If sessions are not stored in the SessionService, default to | |
| 354 // |LOADED_LAST_SESSION| state. | |
| 355 load_state_ = LOADING | LOADED_LAST_SESSION; | |
| 356 #else | |
| 357 load_state_ = LOADING; | 356 load_state_ = LOADING; |
| 358 | 357 if (client_->HasLastSession()) { |
| 359 SessionService* session_service = | 358 client_->GetLastSession( |
| 360 SessionServiceFactory::GetForProfile(profile_); | |
| 361 Profile::ExitType exit_type = profile_->GetLastSessionExitType(); | |
| 362 if (!profile_->restored_last_session() && session_service && | |
| 363 (exit_type == Profile::EXIT_CRASHED || | |
| 364 exit_type == Profile::EXIT_SESSION_ENDED)) { | |
| 365 // The previous session crashed and wasn't restored, or was a forced | |
| 366 // shutdown. Both of which won't have notified us of the browser close so | |
| 367 // that we need to load the windows from session service (which will have | |
| 368 // saved them). | |
| 369 session_service->GetLastSession( | |
| 370 base::Bind(&Delegate::OnGotPreviousSession, base::Unretained(this)), | 359 base::Bind(&Delegate::OnGotPreviousSession, base::Unretained(this)), |
| 371 &cancelable_task_tracker_); | 360 &cancelable_task_tracker_); |
| 372 } else { | 361 } else { |
| 373 load_state_ |= LOADED_LAST_SESSION; | 362 load_state_ |= LOADED_LAST_SESSION; |
| 374 } | 363 } |
| 375 #endif | |
| 376 | 364 |
| 377 // Request the tabs closed in the last session. If the last session crashed, | 365 // Request the tabs closed in the last session. If the last session crashed, |
| 378 // this won't contain the tabs/window that were open at the point of the | 366 // this won't contain the tabs/window that were open at the point of the |
| 379 // crash (the call to GetLastSession above requests those). | 367 // crash (the call to GetLastSession above requests those). |
| 380 base_session_service_->ScheduleGetLastSessionCommands( | 368 base_session_service_->ScheduleGetLastSessionCommands( |
| 381 base::Bind(&Delegate::OnGotLastSessionCommands, base::Unretained(this)), | 369 base::Bind(&Delegate::OnGotLastSessionCommands, base::Unretained(this)), |
| 382 &cancelable_task_tracker_); | 370 &cancelable_task_tracker_); |
| 383 } | 371 } |
| 384 | 372 |
| 385 void PersistentTabRestoreService::Delegate::DeleteLastSession() { | 373 void PersistentTabRestoreService::Delegate::DeleteLastSession() { |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 444 void PersistentTabRestoreService::Delegate::ScheduleCommandsForTab( | 432 void PersistentTabRestoreService::Delegate::ScheduleCommandsForTab( |
| 445 const Tab& tab, | 433 const Tab& tab, |
| 446 int selected_index) { | 434 int selected_index) { |
| 447 const std::vector<sessions::SerializedNavigationEntry>& navigations = | 435 const std::vector<sessions::SerializedNavigationEntry>& navigations = |
| 448 tab.navigations; | 436 tab.navigations; |
| 449 int max_index = static_cast<int>(navigations.size()); | 437 int max_index = static_cast<int>(navigations.size()); |
| 450 | 438 |
| 451 // Determine the first navigation we'll persist. | 439 // Determine the first navigation we'll persist. |
| 452 int valid_count_before_selected = 0; | 440 int valid_count_before_selected = 0; |
| 453 int first_index_to_persist = selected_index; | 441 int first_index_to_persist = selected_index; |
| 454 for (int i = selected_index - 1; i >= 0 && | 442 for (int i = selected_index - 1; |
| 455 valid_count_before_selected < gMaxPersistNavigationCount; --i) { | 443 i >= 0 && |
| 444 valid_count_before_selected < client_->GetMaxPersistNavigationCount(); | |
| 445 --i) { | |
| 456 if (ShouldTrackEntry(navigations[i].virtual_url())) { | 446 if (ShouldTrackEntry(navigations[i].virtual_url())) { |
| 457 first_index_to_persist = i; | 447 first_index_to_persist = i; |
| 458 valid_count_before_selected++; | 448 valid_count_before_selected++; |
| 459 } | 449 } |
| 460 } | 450 } |
| 461 | 451 |
| 462 // Write the command that identifies the selected tab. | 452 // Write the command that identifies the selected tab. |
| 463 base_session_service_->ScheduleCommand( | 453 base_session_service_->ScheduleCommand( |
| 464 CreateSelectedNavigationInTabCommand(tab.id, | 454 CreateSelectedNavigationInTabCommand(tab.id, |
| 465 valid_count_before_selected, | 455 valid_count_before_selected, |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 484 if (!tab.user_agent_override.empty()) { | 474 if (!tab.user_agent_override.empty()) { |
| 485 base_session_service_->ScheduleCommand( | 475 base_session_service_->ScheduleCommand( |
| 486 sessions::CreateSetTabUserAgentOverrideCommand( | 476 sessions::CreateSetTabUserAgentOverrideCommand( |
| 487 kCommandSetTabUserAgentOverride, | 477 kCommandSetTabUserAgentOverride, |
| 488 tab.id, | 478 tab.id, |
| 489 tab.user_agent_override).Pass()); | 479 tab.user_agent_override).Pass()); |
| 490 } | 480 } |
| 491 | 481 |
| 492 // Then write the navigations. | 482 // Then write the navigations. |
| 493 for (int i = first_index_to_persist, wrote_count = 0; | 483 for (int i = first_index_to_persist, wrote_count = 0; |
| 494 wrote_count < 2 * gMaxPersistNavigationCount && i < max_index; ++i) { | 484 wrote_count < 2 * client_->GetMaxPersistNavigationCount() && |
| 485 i < max_index; | |
| 486 ++i) { | |
| 495 if (ShouldTrackEntry(navigations[i].virtual_url())) { | 487 if (ShouldTrackEntry(navigations[i].virtual_url())) { |
| 496 base_session_service_->ScheduleCommand( | 488 base_session_service_->ScheduleCommand( |
| 497 CreateUpdateTabNavigationCommand(kCommandUpdateTabNavigation, | 489 CreateUpdateTabNavigationCommand(kCommandUpdateTabNavigation, |
| 498 tab.id, | 490 tab.id, |
| 499 navigations[i])); | 491 navigations[i])); |
| 500 } | 492 } |
| 501 } | 493 } |
| 502 } | 494 } |
| 503 | 495 |
| 504 // static | 496 // static |
| (...skipping 421 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 926 } | 918 } |
| 927 } | 919 } |
| 928 } | 920 } |
| 929 } | 921 } |
| 930 } | 922 } |
| 931 | 923 |
| 932 // PersistentTabRestoreService ------------------------------------------------- | 924 // PersistentTabRestoreService ------------------------------------------------- |
| 933 | 925 |
| 934 PersistentTabRestoreService::PersistentTabRestoreService( | 926 PersistentTabRestoreService::PersistentTabRestoreService( |
| 935 Profile* profile, | 927 Profile* profile, |
| 928 scoped_ptr<sessions::TabRestoreServiceClient> client, | |
| 936 TimeFactory* time_factory) | 929 TimeFactory* time_factory) |
| 937 : delegate_(new Delegate(profile)), | 930 : client_(client.Pass()), |
| 931 delegate_(new Delegate(profile, client_.get())), | |
| 938 helper_(this, delegate_.get(), profile, time_factory) { | 932 helper_(this, delegate_.get(), profile, time_factory) { |
| 939 delegate_->set_tab_restore_service_helper(&helper_); | 933 delegate_->set_tab_restore_service_helper(&helper_); |
| 940 } | 934 } |
| 941 | 935 |
| 942 PersistentTabRestoreService::~PersistentTabRestoreService() {} | 936 PersistentTabRestoreService::~PersistentTabRestoreService() {} |
| 943 | 937 |
| 944 void PersistentTabRestoreService::AddObserver( | 938 void PersistentTabRestoreService::AddObserver( |
| 945 TabRestoreServiceObserver* observer) { | 939 TabRestoreServiceObserver* observer) { |
| 946 helper_.AddObserver(observer); | 940 helper_.AddObserver(observer); |
| 947 } | 941 } |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1012 delegate_->LoadTabsFromLastSession(); | 1006 delegate_->LoadTabsFromLastSession(); |
| 1013 } | 1007 } |
| 1014 | 1008 |
| 1015 TabRestoreService::Entries* PersistentTabRestoreService::mutable_entries() { | 1009 TabRestoreService::Entries* PersistentTabRestoreService::mutable_entries() { |
| 1016 return &helper_.entries_; | 1010 return &helper_.entries_; |
| 1017 } | 1011 } |
| 1018 | 1012 |
| 1019 void PersistentTabRestoreService::PruneEntries() { | 1013 void PersistentTabRestoreService::PruneEntries() { |
| 1020 helper_.PruneEntries(); | 1014 helper_.PruneEntries(); |
| 1021 } | 1015 } |
| 1022 | |
| 1023 KeyedService* TabRestoreServiceFactory::BuildServiceInstanceFor( | |
| 1024 content::BrowserContext* profile) const { | |
| 1025 return new PersistentTabRestoreService(static_cast<Profile*>(profile), NULL); | |
| 1026 } | |
| OLD | NEW |