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/sessions/base_session_service_delegate_impl.h" | |
21 #include "components/sessions/base_session_service.h" | 20 #include "components/sessions/base_session_service.h" |
22 #include "components/sessions/base_session_service_commands.h" | 21 #include "components/sessions/base_session_service_commands.h" |
| 22 #include "components/sessions/base_session_service_delegate.h" |
23 #include "components/sessions/core/session_constants.h" | 23 #include "components/sessions/core/session_constants.h" |
24 #include "components/sessions/session_command.h" | 24 #include "components/sessions/session_command.h" |
25 #include "content/public/browser/session_storage_namespace.h" | 25 #include "content/public/browser/session_storage_namespace.h" |
26 | 26 |
27 namespace { | 27 namespace { |
28 | 28 |
29 // Only written if the tab is pinned. | 29 // Only written if the tab is pinned. |
30 typedef bool PinnedStatePayload; | 30 typedef bool PinnedStatePayload; |
31 | 31 |
32 typedef int32 RestoredEntryPayload; | 32 typedef int32 RestoredEntryPayload; |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 // everything. | 105 // everything. |
106 const int kEntriesPerReset = 40; | 106 const int kEntriesPerReset = 40; |
107 | 107 |
108 const size_t kMaxEntries = TabRestoreServiceHelper::kMaxEntries; | 108 const size_t kMaxEntries = TabRestoreServiceHelper::kMaxEntries; |
109 | 109 |
110 } // namespace | 110 } // namespace |
111 | 111 |
112 // PersistentTabRestoreService::Delegate --------------------------------------- | 112 // PersistentTabRestoreService::Delegate --------------------------------------- |
113 | 113 |
114 // This restore service will create and own a BaseSessionService and implement | 114 // This restore service will create and own a BaseSessionService and implement |
115 // the required BaseSessionServiceDelegateImpl. | 115 // the required sessions::BaseSessionServiceDelegate. |
116 class PersistentTabRestoreService::Delegate | 116 class PersistentTabRestoreService::Delegate |
117 : public BaseSessionServiceDelegateImpl, | 117 : public sessions::BaseSessionServiceDelegate, |
118 public TabRestoreServiceHelper::Observer { | 118 public TabRestoreServiceHelper::Observer { |
119 public: | 119 public: |
120 explicit Delegate(sessions::TabRestoreServiceClient* client); | 120 explicit Delegate(sessions::TabRestoreServiceClient* client); |
121 | 121 |
122 ~Delegate() override; | 122 ~Delegate() override; |
123 | 123 |
124 // BaseSessionServiceDelegateImpl: | 124 // sessions::BaseSessionServiceDelegate: |
| 125 base::SequencedWorkerPool* GetBlockingPool() override; |
| 126 bool ShouldUseDelayedSave() override; |
125 void OnWillSaveCommands() override; | 127 void OnWillSaveCommands() override; |
126 | 128 |
127 // TabRestoreServiceHelper::Observer: | 129 // TabRestoreServiceHelper::Observer: |
128 void OnClearEntries() override; | 130 void OnClearEntries() override; |
129 void OnRestoreEntryById(SessionID::id_type id, | 131 void OnRestoreEntryById(SessionID::id_type id, |
130 Entries::const_iterator entry_iterator) override; | 132 Entries::const_iterator entry_iterator) override; |
131 void OnAddEntry() override; | 133 void OnAddEntry() override; |
132 | 134 |
133 void set_tab_restore_service_helper( | 135 void set_tab_restore_service_helper( |
134 TabRestoreServiceHelper* tab_restore_service_helper) { | 136 TabRestoreServiceHelper* tab_restore_service_helper) { |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 void LoadStateChanged(); | 215 void LoadStateChanged(); |
214 | 216 |
215 // If |id_to_entry| contains an entry for |id| the corresponding entry is | 217 // If |id_to_entry| contains an entry for |id| the corresponding entry is |
216 // deleted and removed from both |id_to_entry| and |entries|. This is used | 218 // deleted and removed from both |id_to_entry| and |entries|. This is used |
217 // when creating entries from the backend file. | 219 // when creating entries from the backend file. |
218 void RemoveEntryByID(SessionID::id_type id, | 220 void RemoveEntryByID(SessionID::id_type id, |
219 IDToEntry* id_to_entry, | 221 IDToEntry* id_to_entry, |
220 std::vector<TabRestoreService::Entry*>* entries); | 222 std::vector<TabRestoreService::Entry*>* entries); |
221 | 223 |
222 private: | 224 private: |
223 scoped_ptr<sessions::BaseSessionService> base_session_service_; | |
224 | |
225 // The associated client. | 225 // The associated client. |
226 sessions::TabRestoreServiceClient* client_; | 226 sessions::TabRestoreServiceClient* client_; |
227 | 227 |
| 228 scoped_ptr<sessions::BaseSessionService> base_session_service_; |
| 229 |
228 TabRestoreServiceHelper* tab_restore_service_helper_; | 230 TabRestoreServiceHelper* tab_restore_service_helper_; |
229 | 231 |
230 // The number of entries to write. | 232 // The number of entries to write. |
231 int entries_to_write_; | 233 int entries_to_write_; |
232 | 234 |
233 // Number of entries we've written. | 235 // Number of entries we've written. |
234 int entries_written_; | 236 int entries_written_; |
235 | 237 |
236 // Whether we've loaded the last session. | 238 // Whether we've loaded the last session. |
237 int load_state_; | 239 int load_state_; |
238 | 240 |
239 // Results from previously closed tabs/sessions is first added here. When the | 241 // Results from previously closed tabs/sessions is first added here. When the |
240 // results from both us and the session restore service have finished loading | 242 // results from both us and the session restore service have finished loading |
241 // LoadStateChanged is invoked, which adds these entries to entries_. | 243 // LoadStateChanged is invoked, which adds these entries to entries_. |
242 ScopedVector<Entry> staging_entries_; | 244 ScopedVector<Entry> staging_entries_; |
243 | 245 |
244 // Used when loading previous tabs/session and open tabs/session. | 246 // Used when loading previous tabs/session and open tabs/session. |
245 base::CancelableTaskTracker cancelable_task_tracker_; | 247 base::CancelableTaskTracker cancelable_task_tracker_; |
246 | 248 |
247 DISALLOW_COPY_AND_ASSIGN(Delegate); | 249 DISALLOW_COPY_AND_ASSIGN(Delegate); |
248 }; | 250 }; |
249 | 251 |
250 PersistentTabRestoreService::Delegate::Delegate( | 252 PersistentTabRestoreService::Delegate::Delegate( |
251 sessions::TabRestoreServiceClient* client) | 253 sessions::TabRestoreServiceClient* client) |
252 : BaseSessionServiceDelegateImpl(true), | 254 : client_(client), |
253 base_session_service_(new sessions::BaseSessionService( | 255 base_session_service_(new sessions::BaseSessionService( |
254 sessions::BaseSessionService::TAB_RESTORE, | 256 sessions::BaseSessionService::TAB_RESTORE, |
255 client->GetPathToSaveTo(), | 257 client_->GetPathToSaveTo(), |
256 this)), | 258 this)), |
257 client_(client), | |
258 tab_restore_service_helper_(NULL), | 259 tab_restore_service_helper_(NULL), |
259 entries_to_write_(0), | 260 entries_to_write_(0), |
260 entries_written_(0), | 261 entries_written_(0), |
261 load_state_(NOT_LOADED) {} | 262 load_state_(NOT_LOADED) {} |
262 | 263 |
263 PersistentTabRestoreService::Delegate::~Delegate() {} | 264 PersistentTabRestoreService::Delegate::~Delegate() {} |
264 | 265 |
| 266 base::SequencedWorkerPool* |
| 267 PersistentTabRestoreService::Delegate::GetBlockingPool() { |
| 268 return client_->GetBlockingPool(); |
| 269 } |
| 270 |
| 271 bool PersistentTabRestoreService::Delegate::ShouldUseDelayedSave() { |
| 272 return true; |
| 273 } |
| 274 |
265 void PersistentTabRestoreService::Delegate::OnWillSaveCommands() { | 275 void PersistentTabRestoreService::Delegate::OnWillSaveCommands() { |
266 const Entries& entries = tab_restore_service_helper_->entries(); | 276 const Entries& entries = tab_restore_service_helper_->entries(); |
267 int to_write_count = std::min(entries_to_write_, | 277 int to_write_count = std::min(entries_to_write_, |
268 static_cast<int>(entries.size())); | 278 static_cast<int>(entries.size())); |
269 entries_to_write_ = 0; | 279 entries_to_write_ = 0; |
270 if (entries_written_ + to_write_count > kEntriesPerReset) { | 280 if (entries_written_ + to_write_count > kEntriesPerReset) { |
271 to_write_count = entries.size(); | 281 to_write_count = entries.size(); |
272 base_session_service_->set_pending_reset(true); | 282 base_session_service_->set_pending_reset(true); |
273 } | 283 } |
274 if (to_write_count) { | 284 if (to_write_count) { |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
427 tab.navigations; | 437 tab.navigations; |
428 int max_index = static_cast<int>(navigations.size()); | 438 int max_index = static_cast<int>(navigations.size()); |
429 | 439 |
430 // Determine the first navigation we'll persist. | 440 // Determine the first navigation we'll persist. |
431 int valid_count_before_selected = 0; | 441 int valid_count_before_selected = 0; |
432 int first_index_to_persist = selected_index; | 442 int first_index_to_persist = selected_index; |
433 for (int i = selected_index - 1; | 443 for (int i = selected_index - 1; |
434 i >= 0 && | 444 i >= 0 && |
435 valid_count_before_selected < sessions::gMaxPersistNavigationCount; | 445 valid_count_before_selected < sessions::gMaxPersistNavigationCount; |
436 --i) { | 446 --i) { |
437 if (ShouldTrackEntry(navigations[i].virtual_url())) { | 447 if (client_->ShouldTrackEntry(navigations[i].virtual_url())) { |
438 first_index_to_persist = i; | 448 first_index_to_persist = i; |
439 valid_count_before_selected++; | 449 valid_count_before_selected++; |
440 } | 450 } |
441 } | 451 } |
442 | 452 |
443 // Write the command that identifies the selected tab. | 453 // Write the command that identifies the selected tab. |
444 base_session_service_->ScheduleCommand( | 454 base_session_service_->ScheduleCommand( |
445 CreateSelectedNavigationInTabCommand(tab.id, | 455 CreateSelectedNavigationInTabCommand(tab.id, |
446 valid_count_before_selected, | 456 valid_count_before_selected, |
447 tab.timestamp).Pass()); | 457 tab.timestamp).Pass()); |
(...skipping 19 matching lines...) Expand all Loading... |
467 sessions::CreateSetTabUserAgentOverrideCommand( | 477 sessions::CreateSetTabUserAgentOverrideCommand( |
468 kCommandSetTabUserAgentOverride, | 478 kCommandSetTabUserAgentOverride, |
469 tab.id, | 479 tab.id, |
470 tab.user_agent_override).Pass()); | 480 tab.user_agent_override).Pass()); |
471 } | 481 } |
472 | 482 |
473 // Then write the navigations. | 483 // Then write the navigations. |
474 for (int i = first_index_to_persist, wrote_count = 0; | 484 for (int i = first_index_to_persist, wrote_count = 0; |
475 wrote_count < 2 * sessions::gMaxPersistNavigationCount && i < max_index; | 485 wrote_count < 2 * sessions::gMaxPersistNavigationCount && i < max_index; |
476 ++i) { | 486 ++i) { |
477 if (ShouldTrackEntry(navigations[i].virtual_url())) { | 487 if (client_->ShouldTrackEntry(navigations[i].virtual_url())) { |
478 base_session_service_->ScheduleCommand( | 488 base_session_service_->ScheduleCommand( |
479 CreateUpdateTabNavigationCommand(kCommandUpdateTabNavigation, | 489 CreateUpdateTabNavigationCommand(kCommandUpdateTabNavigation, |
480 tab.id, | 490 tab.id, |
481 navigations[i])); | 491 navigations[i])); |
482 } | 492 } |
483 } | 493 } |
484 } | 494 } |
485 | 495 |
486 // static | 496 // static |
487 scoped_ptr<sessions::SessionCommand> | 497 scoped_ptr<sessions::SessionCommand> |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
534 } | 544 } |
535 | 545 |
536 int PersistentTabRestoreService::Delegate::GetSelectedNavigationIndexToPersist( | 546 int PersistentTabRestoreService::Delegate::GetSelectedNavigationIndexToPersist( |
537 const Tab& tab) { | 547 const Tab& tab) { |
538 const std::vector<sessions::SerializedNavigationEntry>& navigations = | 548 const std::vector<sessions::SerializedNavigationEntry>& navigations = |
539 tab.navigations; | 549 tab.navigations; |
540 int selected_index = tab.current_navigation_index; | 550 int selected_index = tab.current_navigation_index; |
541 int max_index = static_cast<int>(navigations.size()); | 551 int max_index = static_cast<int>(navigations.size()); |
542 | 552 |
543 // Find the first navigation to persist. We won't persist the selected | 553 // Find the first navigation to persist. We won't persist the selected |
544 // navigation if ShouldTrackEntry returns false. | 554 // navigation if client_->ShouldTrackEntry returns false. |
545 while (selected_index >= 0 && | 555 while ( |
546 !ShouldTrackEntry(navigations[selected_index].virtual_url())) { | 556 selected_index >= 0 && |
| 557 !client_->ShouldTrackEntry(navigations[selected_index].virtual_url())) { |
547 selected_index--; | 558 selected_index--; |
548 } | 559 } |
549 | 560 |
550 if (selected_index != -1) | 561 if (selected_index != -1) |
551 return selected_index; | 562 return selected_index; |
552 | 563 |
553 // Couldn't find a navigation to persist going back, go forward. | 564 // Couldn't find a navigation to persist going back, go forward. |
554 selected_index = tab.current_navigation_index + 1; | 565 selected_index = tab.current_navigation_index + 1; |
555 while (selected_index < max_index && | 566 while ( |
556 !ShouldTrackEntry(navigations[selected_index].virtual_url())) { | 567 selected_index < max_index && |
| 568 !client_->ShouldTrackEntry(navigations[selected_index].virtual_url())) { |
557 selected_index++; | 569 selected_index++; |
558 } | 570 } |
559 | 571 |
560 return (selected_index == max_index) ? -1 : selected_index; | 572 return (selected_index == max_index) ? -1 : selected_index; |
561 } | 573 } |
562 | 574 |
563 void PersistentTabRestoreService::Delegate::OnGotLastSessionCommands( | 575 void PersistentTabRestoreService::Delegate::OnGotLastSessionCommands( |
564 ScopedVector<sessions::SessionCommand> commands) { | 576 ScopedVector<sessions::SessionCommand> commands) { |
565 std::vector<Entry*> entries; | 577 std::vector<Entry*> entries; |
566 CreateEntriesFromCommands(commands.get(), &entries); | 578 CreateEntriesFromCommands(commands.get(), &entries); |
(...skipping 429 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
996 delegate_->LoadTabsFromLastSession(); | 1008 delegate_->LoadTabsFromLastSession(); |
997 } | 1009 } |
998 | 1010 |
999 TabRestoreService::Entries* PersistentTabRestoreService::mutable_entries() { | 1011 TabRestoreService::Entries* PersistentTabRestoreService::mutable_entries() { |
1000 return &helper_.entries_; | 1012 return &helper_.entries_; |
1001 } | 1013 } |
1002 | 1014 |
1003 void PersistentTabRestoreService::PruneEntries() { | 1015 void PersistentTabRestoreService::PruneEntries() { |
1004 helper_.PruneEntries(); | 1016 helper_.PruneEntries(); |
1005 } | 1017 } |
OLD | NEW |