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

Side by Side Diff: chrome/browser/sessions/session_service.cc

Issue 694813003: Changing SessionService to have a BaseSessionService, not being one. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed browser tests Created 6 years, 1 month 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 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/session_service.h" 5 #include "chrome/browser/sessions/session_service.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <set> 8 #include <set>
9 #include <utility> 9 #include <utility>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/bind.h" 12 #include "base/bind.h"
13 #include "base/bind_helpers.h" 13 #include "base/bind_helpers.h"
14 #include "base/command_line.h" 14 #include "base/command_line.h"
15 #include "base/message_loop/message_loop.h" 15 #include "base/message_loop/message_loop.h"
16 #include "base/metrics/histogram.h" 16 #include "base/metrics/histogram.h"
17 #include "base/pickle.h" 17 #include "base/pickle.h"
18 #include "base/threading/thread.h" 18 #include "base/threading/thread.h"
19 #include "chrome/browser/background/background_mode_manager.h" 19 #include "chrome/browser/background/background_mode_manager.h"
20 #include "chrome/browser/browser_process.h" 20 #include "chrome/browser/browser_process.h"
21 #include "chrome/browser/chrome_notification_types.h" 21 #include "chrome/browser/chrome_notification_types.h"
22 #include "chrome/browser/defaults.h" 22 #include "chrome/browser/defaults.h"
23 #include "chrome/browser/extensions/tab_helper.h" 23 #include "chrome/browser/extensions/tab_helper.h"
24 #include "chrome/browser/prefs/session_startup_pref.h" 24 #include "chrome/browser/prefs/session_startup_pref.h"
25 #include "chrome/browser/profiles/profile.h" 25 #include "chrome/browser/profiles/profile.h"
26 #include "chrome/browser/profiles/profile_manager.h" 26 #include "chrome/browser/profiles/profile_manager.h"
27 #include "chrome/browser/sessions/base_session_service_delegate_impl.h" 27 #include "chrome/browser/sessions/base_session_service_delegate_impl.h"
28 #include "chrome/browser/sessions/session_backend.h"
29 #include "chrome/browser/sessions/session_command.h" 28 #include "chrome/browser/sessions/session_command.h"
30 #include "chrome/browser/sessions/session_data_deleter.h" 29 #include "chrome/browser/sessions/session_data_deleter.h"
31 #include "chrome/browser/sessions/session_restore.h" 30 #include "chrome/browser/sessions/session_restore.h"
32 #include "chrome/browser/sessions/session_service_utils.h" 31 #include "chrome/browser/sessions/session_service_utils.h"
33 #include "chrome/browser/sessions/session_tab_helper.h" 32 #include "chrome/browser/sessions/session_tab_helper.h"
34 #include "chrome/browser/sessions/session_types.h" 33 #include "chrome/browser/sessions/session_types.h"
35 #include "chrome/browser/ui/browser_iterator.h" 34 #include "chrome/browser/ui/browser_iterator.h"
36 #include "chrome/browser/ui/browser_list.h" 35 #include "chrome/browser/ui/browser_list.h"
37 #include "chrome/browser/ui/browser_tabstrip.h" 36 #include "chrome/browser/ui/browser_tabstrip.h"
38 #include "chrome/browser/ui/browser_window.h" 37 #include "chrome/browser/ui/browser_window.h"
(...skipping 19 matching lines...) Expand all
58 using content::WebContents; 57 using content::WebContents;
59 using sessions::ContentSerializedNavigationBuilder; 58 using sessions::ContentSerializedNavigationBuilder;
60 using sessions::SerializedNavigationEntry; 59 using sessions::SerializedNavigationEntry;
61 60
62 // Every kWritesPerReset commands triggers recreating the file. 61 // Every kWritesPerReset commands triggers recreating the file.
63 static const int kWritesPerReset = 250; 62 static const int kWritesPerReset = 250;
64 63
65 // SessionService ------------------------------------------------------------- 64 // SessionService -------------------------------------------------------------
66 65
67 SessionService::SessionService(Profile* profile) 66 SessionService::SessionService(Profile* profile)
68 : BaseSessionService( 67 : BaseSessionServiceDelegateImpl(true),
69 SESSION_RESTORE,
70 profile->GetPath(),
71 scoped_ptr<BaseSessionServiceDelegate>(
72 new BaseSessionServiceDelegateImpl(true))),
73 profile_(profile), 68 profile_(profile),
69 base_session_service_(
70 new BaseSessionService(BaseSessionService::SESSION_RESTORE,
71 profile->GetPath(),
72 this)),
74 has_open_trackable_browsers_(false), 73 has_open_trackable_browsers_(false),
75 move_on_new_browser_(false), 74 move_on_new_browser_(false),
76 save_delay_in_millis_(base::TimeDelta::FromMilliseconds(2500)), 75 save_delay_in_millis_(base::TimeDelta::FromMilliseconds(2500)),
77 save_delay_in_mins_(base::TimeDelta::FromMinutes(10)), 76 save_delay_in_mins_(base::TimeDelta::FromMinutes(10)),
78 save_delay_in_hrs_(base::TimeDelta::FromHours(8)), 77 save_delay_in_hrs_(base::TimeDelta::FromHours(8)),
79 force_browser_not_alive_with_no_windows_(false), 78 force_browser_not_alive_with_no_windows_(false),
80 weak_factory_(this) { 79 weak_factory_(this) {
81 // We should never be created when incognito. 80 // We should never be created when incognito.
82 DCHECK(!profile->IsOffTheRecord()); 81 DCHECK(!profile->IsOffTheRecord());
83 Init(); 82 Init();
84 } 83 }
85 84
86 SessionService::SessionService(const base::FilePath& save_path) 85 SessionService::SessionService(const base::FilePath& save_path)
87 : BaseSessionService( 86 : BaseSessionServiceDelegateImpl(false),
88 SESSION_RESTORE,
89 save_path,
90 scoped_ptr<BaseSessionServiceDelegate>(
91 new BaseSessionServiceDelegateImpl(false))),
92 profile_(NULL), 87 profile_(NULL),
88 base_session_service_(
89 new BaseSessionService(BaseSessionService::SESSION_RESTORE,
90 save_path,
91 this)),
93 has_open_trackable_browsers_(false), 92 has_open_trackable_browsers_(false),
94 move_on_new_browser_(false), 93 move_on_new_browser_(false),
95 save_delay_in_millis_(base::TimeDelta::FromMilliseconds(2500)), 94 save_delay_in_millis_(base::TimeDelta::FromMilliseconds(2500)),
96 save_delay_in_mins_(base::TimeDelta::FromMinutes(10)), 95 save_delay_in_mins_(base::TimeDelta::FromMinutes(10)),
97 save_delay_in_hrs_(base::TimeDelta::FromHours(8)), 96 save_delay_in_hrs_(base::TimeDelta::FromHours(8)),
98 force_browser_not_alive_with_no_windows_(false), 97 force_browser_not_alive_with_no_windows_(false),
99 weak_factory_(this) { 98 weak_factory_(this) {
100 Init(); 99 Init();
101 } 100 }
102 101
103 SessionService::~SessionService() { 102 SessionService::~SessionService() {
104 // The BrowserList should outlive the SessionService since it's static and 103 // The BrowserList should outlive the SessionService since it's static and
105 // the SessionService is a KeyedService. 104 // the SessionService is a KeyedService.
106 BrowserList::RemoveObserver(this); 105 BrowserList::RemoveObserver(this);
107 Save(); 106 base_session_service_->Save();
108 } 107 }
109 108
110 bool SessionService::ShouldNewWindowStartSession() { 109 bool SessionService::ShouldNewWindowStartSession() {
111 // ChromeOS and OSX have different ideas of application lifetime than 110 // ChromeOS and OSX have different ideas of application lifetime than
112 // the other platforms. 111 // the other platforms.
113 // On ChromeOS opening a new window should never start a new session. 112 // On ChromeOS opening a new window should never start a new session.
114 #if defined(OS_CHROMEOS) 113 #if defined(OS_CHROMEOS)
115 if (!force_browser_not_alive_with_no_windows_) 114 if (!force_browser_not_alive_with_no_windows_)
116 return false; 115 return false;
117 #endif 116 #endif
(...skipping 17 matching lines...) Expand all
135 134
136 void SessionService::ResetFromCurrentBrowsers() { 135 void SessionService::ResetFromCurrentBrowsers() {
137 ScheduleResetCommands(); 136 ScheduleResetCommands();
138 } 137 }
139 138
140 void SessionService::MoveCurrentSessionToLastSession() { 139 void SessionService::MoveCurrentSessionToLastSession() {
141 pending_tab_close_ids_.clear(); 140 pending_tab_close_ids_.clear();
142 window_closing_ids_.clear(); 141 window_closing_ids_.clear();
143 pending_window_close_ids_.clear(); 142 pending_window_close_ids_.clear();
144 143
145 Save(); 144 base_session_service_->MoveCurrentSessionToLastSession();
145 }
146 146
147 RunTaskOnBackendThread( 147 void SessionService::DeleteLastSession() {
148 FROM_HERE, base::Bind(&SessionBackend::MoveCurrentSessionToLastSession, 148 base_session_service_->DeleteLastSession();
149 backend()));
150 } 149 }
151 150
152 void SessionService::SetTabWindow(const SessionID& window_id, 151 void SessionService::SetTabWindow(const SessionID& window_id,
153 const SessionID& tab_id) { 152 const SessionID& tab_id) {
154 if (!ShouldTrackChangesToWindow(window_id)) 153 if (!ShouldTrackChangesToWindow(window_id))
155 return; 154 return;
156 155
157 ScheduleCommand(CreateSetTabWindowCommand(window_id, tab_id).Pass()); 156 ScheduleCommand(CreateSetTabWindowCommand(window_id, tab_id).Pass());
158 } 157 }
159 158
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after
419 range.second = std::max(navigation.index(), range.second); 418 range.second = std::max(navigation.index(), range.second);
420 } 419 }
421 ScheduleCommand(CreateUpdateTabNavigationCommand(tab_id, navigation).Pass()); 420 ScheduleCommand(CreateUpdateTabNavigationCommand(tab_id, navigation).Pass());
422 } 421 }
423 422
424 void SessionService::TabRestored(WebContents* tab, bool pinned) { 423 void SessionService::TabRestored(WebContents* tab, bool pinned) {
425 SessionTabHelper* session_tab_helper = SessionTabHelper::FromWebContents(tab); 424 SessionTabHelper* session_tab_helper = SessionTabHelper::FromWebContents(tab);
426 if (!ShouldTrackChangesToWindow(session_tab_helper->window_id())) 425 if (!ShouldTrackChangesToWindow(session_tab_helper->window_id()))
427 return; 426 return;
428 427
429 BuildCommandsForTab(session_tab_helper->window_id(), 428 BuildCommandsForTab(session_tab_helper->window_id(), tab, -1, pinned, NULL);
430 tab, 429 base_session_service_->StartSaveTimer();
431 -1,
432 pinned,
433 &pending_commands(),
434 NULL);
435 StartSaveTimer();
436 } 430 }
437 431
438 void SessionService::SetSelectedNavigationIndex(const SessionID& window_id, 432 void SessionService::SetSelectedNavigationIndex(const SessionID& window_id,
439 const SessionID& tab_id, 433 const SessionID& tab_id,
440 int index) { 434 int index) {
441 if (!ShouldTrackChangesToWindow(window_id)) 435 if (!ShouldTrackChangesToWindow(window_id))
442 return; 436 return;
443 437
444 if (tab_to_available_range_.find(tab_id.id()) != 438 if (tab_to_available_range_.find(tab_id.id()) !=
445 tab_to_available_range_.end()) { 439 tab_to_available_range_.end()) {
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
483 477
484 ScheduleCommand( 478 ScheduleCommand(
485 CreateSetTabExtensionAppIDCommand(tab_id, extension_app_id).Pass()); 479 CreateSetTabExtensionAppIDCommand(tab_id, extension_app_id).Pass());
486 } 480 }
487 481
488 base::CancelableTaskTracker::TaskId SessionService::GetLastSession( 482 base::CancelableTaskTracker::TaskId SessionService::GetLastSession(
489 const SessionCallback& callback, 483 const SessionCallback& callback,
490 base::CancelableTaskTracker* tracker) { 484 base::CancelableTaskTracker* tracker) {
491 // OnGotSessionCommands maps the SessionCommands to browser state, then run 485 // OnGotSessionCommands maps the SessionCommands to browser state, then run
492 // the callback. 486 // the callback.
493 return ScheduleGetLastSessionCommands( 487 return base_session_service_->ScheduleGetLastSessionCommands(
494 base::Bind(&SessionService::OnGotSessionCommands, 488 base::Bind(&SessionService::OnGotSessionCommands,
495 weak_factory_.GetWeakPtr(), 489 weak_factory_.GetWeakPtr(),
496 callback), 490 callback),
497 tracker); 491 tracker);
498 } 492 }
499 493
500 void SessionService::Save() { 494 void SessionService::OnSavedCommands() {
501 bool had_commands = !pending_commands().empty(); 495 RecordSessionUpdateHistogramData(chrome::NOTIFICATION_SESSION_SERVICE_SAVED,
502 BaseSessionService::Save(); 496 &last_updated_save_time_);
503 if (had_commands) { 497 content::NotificationService::current()->Notify(
504 RecordSessionUpdateHistogramData(chrome::NOTIFICATION_SESSION_SERVICE_SAVED, 498 chrome::NOTIFICATION_SESSION_SERVICE_SAVED,
505 &last_updated_save_time_); 499 content::Source<Profile>(profile()),
506 content::NotificationService::current()->Notify( 500 content::NotificationService::NoDetails());
507 chrome::NOTIFICATION_SESSION_SERVICE_SAVED,
508 content::Source<Profile>(profile()),
509 content::NotificationService::NoDetails());
510 }
511 } 501 }
512 502
513 void SessionService::Init() { 503 void SessionService::Init() {
514 // Register for the notifications we're interested in. 504 // Register for the notifications we're interested in.
515 registrar_.Add(this, content::NOTIFICATION_NAV_LIST_PRUNED, 505 registrar_.Add(this, content::NOTIFICATION_NAV_LIST_PRUNED,
516 content::NotificationService::AllSources()); 506 content::NotificationService::AllSources());
517 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_CHANGED, 507 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_CHANGED,
518 content::NotificationService::AllSources()); 508 content::NotificationService::AllSources());
519 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, 509 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED,
520 content::NotificationService::AllSources()); 510 content::NotificationService::AllSources());
(...skipping 25 matching lines...) Expand all
546 window->app_name.empty() ? TYPE_NORMAL : 536 window->app_name.empty() ? TYPE_NORMAL :
547 TYPE_APP)) { 537 TYPE_APP)) {
548 delete window; 538 delete window;
549 window_list->erase(i++); 539 window_list->erase(i++);
550 } else { 540 } else {
551 ++i; 541 ++i;
552 } 542 }
553 } 543 }
554 } 544 }
555 545
556 bool SessionService::processed_any_commands() {
557 return backend()->inited() || !pending_commands().empty();
558 }
559
560 bool SessionService::RestoreIfNecessary(const std::vector<GURL>& urls_to_open, 546 bool SessionService::RestoreIfNecessary(const std::vector<GURL>& urls_to_open,
561 Browser* browser) { 547 Browser* browser) {
562 if (ShouldNewWindowStartSession()) { 548 if (ShouldNewWindowStartSession()) {
563 // We're going from no tabbed browsers to a tabbed browser (and not in 549 // We're going from no tabbed browsers to a tabbed browser (and not in
564 // process startup), restore the last session. 550 // process startup), restore the last session.
565 if (move_on_new_browser_) { 551 if (move_on_new_browser_) {
566 // Make the current session the last. 552 // Make the current session the last.
567 MoveCurrentSessionToLastSession(); 553 MoveCurrentSessionToLastSession();
568 move_on_new_browser_ = false; 554 move_on_new_browser_ = false;
569 } 555 }
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
701 RestoreSessionFromCommands(commands, &valid_windows.get(), &active_window_id); 687 RestoreSessionFromCommands(commands, &valid_windows.get(), &active_window_id);
702 RemoveUnusedRestoreWindows(&valid_windows.get()); 688 RemoveUnusedRestoreWindows(&valid_windows.get());
703 689
704 callback.Run(valid_windows.Pass(), active_window_id); 690 callback.Run(valid_windows.Pass(), active_window_id);
705 } 691 }
706 692
707 void SessionService::BuildCommandsForTab(const SessionID& window_id, 693 void SessionService::BuildCommandsForTab(const SessionID& window_id,
708 WebContents* tab, 694 WebContents* tab,
709 int index_in_window, 695 int index_in_window,
710 bool is_pinned, 696 bool is_pinned,
711 ScopedVector<SessionCommand>* commands,
712 IdToRange* tab_to_available_range) { 697 IdToRange* tab_to_available_range) {
713 DCHECK(tab && commands && window_id.id()); 698 DCHECK(tab && window_id.id());
714 SessionTabHelper* session_tab_helper = SessionTabHelper::FromWebContents(tab); 699 SessionTabHelper* session_tab_helper = SessionTabHelper::FromWebContents(tab);
715 const SessionID& session_id(session_tab_helper->session_id()); 700 const SessionID& session_id(session_tab_helper->session_id());
716 commands->push_back( 701 base_session_service_->AppendRebuildCommand(
717 CreateSetTabWindowCommand(window_id, session_id).release()); 702 CreateSetTabWindowCommand(window_id, session_id));
718 703
719 const int current_index = tab->GetController().GetCurrentEntryIndex(); 704 const int current_index = tab->GetController().GetCurrentEntryIndex();
720 const int min_index = std::max(0, 705 const int min_index = std::max(current_index - gMaxPersistNavigationCount, 0);
721 current_index - max_persist_navigation_count); 706 const int max_index = std::min(current_index + gMaxPersistNavigationCount,
722 const int max_index = 707 tab->GetController().GetEntryCount());
723 std::min(current_index + max_persist_navigation_count,
724 tab->GetController().GetEntryCount());
725 const int pending_index = tab->GetController().GetPendingEntryIndex(); 708 const int pending_index = tab->GetController().GetPendingEntryIndex();
726 if (tab_to_available_range) { 709 if (tab_to_available_range) {
727 (*tab_to_available_range)[session_id.id()] = 710 (*tab_to_available_range)[session_id.id()] =
728 std::pair<int, int>(min_index, max_index); 711 std::pair<int, int>(min_index, max_index);
729 } 712 }
730 713
731 if (is_pinned) 714 if (is_pinned) {
732 commands->push_back(CreatePinnedStateCommand(session_id, true).release()); 715 base_session_service_->AppendRebuildCommand(
716 CreatePinnedStateCommand(session_id, true));
717 }
733 718
734 extensions::TabHelper* extensions_tab_helper = 719 extensions::TabHelper* extensions_tab_helper =
735 extensions::TabHelper::FromWebContents(tab); 720 extensions::TabHelper::FromWebContents(tab);
736 if (extensions_tab_helper->extension_app()) { 721 if (extensions_tab_helper->extension_app()) {
737 commands->push_back(CreateSetTabExtensionAppIDCommand( 722 base_session_service_->AppendRebuildCommand(
738 session_id, 723 CreateSetTabExtensionAppIDCommand(
739 extensions_tab_helper->extension_app()->id()).release()); 724 session_id,
725 extensions_tab_helper->extension_app()->id()));
740 } 726 }
741 727
742 const std::string& ua_override = tab->GetUserAgentOverride(); 728 const std::string& ua_override = tab->GetUserAgentOverride();
743 if (!ua_override.empty()) { 729 if (!ua_override.empty()) {
744 commands->push_back( 730 base_session_service_->AppendRebuildCommand(
745 CreateSetTabUserAgentOverrideCommand(session_id, ua_override).release()); 731 CreateSetTabUserAgentOverrideCommand(session_id, ua_override));
746 } 732 }
747 733
748 for (int i = min_index; i < max_index; ++i) { 734 for (int i = min_index; i < max_index; ++i) {
749 const NavigationEntry* entry = (i == pending_index) ? 735 const NavigationEntry* entry = (i == pending_index) ?
750 tab->GetController().GetPendingEntry() : 736 tab->GetController().GetPendingEntry() :
751 tab->GetController().GetEntryAtIndex(i); 737 tab->GetController().GetEntryAtIndex(i);
752 DCHECK(entry); 738 DCHECK(entry);
753 if (ShouldTrackEntry(entry->GetVirtualURL())) { 739 if (ShouldTrackEntry(entry->GetVirtualURL())) {
754 const SerializedNavigationEntry navigation = 740 const SerializedNavigationEntry navigation =
755 ContentSerializedNavigationBuilder::FromNavigationEntry(i, *entry); 741 ContentSerializedNavigationBuilder::FromNavigationEntry(i, *entry);
756 commands->push_back( 742 base_session_service_->AppendRebuildCommand(
757 CreateUpdateTabNavigationCommand(session_id, navigation).release()); 743 CreateUpdateTabNavigationCommand(session_id, navigation));
758 } 744 }
759 } 745 }
760 commands->push_back( 746 base_session_service_->AppendRebuildCommand(
761 CreateSetSelectedNavigationIndexCommand(session_id, 747 CreateSetSelectedNavigationIndexCommand(session_id,
762 current_index).release()); 748 current_index));
763 749
764 if (index_in_window != -1) { 750 if (index_in_window != -1) {
765 commands->push_back(CreateSetTabIndexInWindowCommand( 751 base_session_service_->AppendRebuildCommand(
766 session_id, 752 CreateSetTabIndexInWindowCommand(session_id,
767 index_in_window).release()); 753 index_in_window));
768 } 754 }
769 755
770 // Record the association between the sessionStorage namespace and the tab. 756 // Record the association between the sessionStorage namespace and the tab.
771 content::SessionStorageNamespace* session_storage_namespace = 757 content::SessionStorageNamespace* session_storage_namespace =
772 tab->GetController().GetDefaultSessionStorageNamespace(); 758 tab->GetController().GetDefaultSessionStorageNamespace();
773 ScheduleCommand(CreateSessionStorageAssociatedCommand( 759 ScheduleCommand(CreateSessionStorageAssociatedCommand(
774 session_tab_helper->session_id(), 760 session_tab_helper->session_id(),
775 session_storage_namespace->persistent_id()).Pass()); 761 session_storage_namespace->persistent_id()).Pass());
776 } 762 }
777 763
778 void SessionService::BuildCommandsForBrowser( 764 void SessionService::BuildCommandsForBrowser(
779 Browser* browser, 765 Browser* browser,
780 ScopedVector<SessionCommand>* commands,
781 IdToRange* tab_to_available_range, 766 IdToRange* tab_to_available_range,
782 std::set<SessionID::id_type>* windows_to_track) { 767 std::set<SessionID::id_type>* windows_to_track) {
783 DCHECK(browser && commands); 768 DCHECK(browser);
784 DCHECK(browser->session_id().id()); 769 DCHECK(browser->session_id().id());
785 770
786 commands->push_back(CreateSetWindowBoundsCommand( 771 base_session_service_->AppendRebuildCommand(CreateSetWindowBoundsCommand(
787 browser->session_id(), 772 browser->session_id(),
788 browser->window()->GetRestoredBounds(), 773 browser->window()->GetRestoredBounds(),
789 browser->window()->GetRestoredState()).release()); 774 browser->window()->GetRestoredState()));
790 775
791 commands->push_back(CreateSetWindowTypeCommand( 776 base_session_service_->AppendRebuildCommand(CreateSetWindowTypeCommand(
792 browser->session_id(), 777 browser->session_id(),
793 WindowTypeForBrowserType(browser->type())).release()); 778 WindowTypeForBrowserType(browser->type())));
794 779
795 if (!browser->app_name().empty()) { 780 if (!browser->app_name().empty()) {
796 commands->push_back(CreateSetWindowAppNameCommand( 781 base_session_service_->AppendRebuildCommand(CreateSetWindowAppNameCommand(
797 browser->session_id(), 782 browser->session_id(),
798 browser->app_name()).release()); 783 browser->app_name()));
799 } 784 }
800 785
801 windows_to_track->insert(browser->session_id().id()); 786 windows_to_track->insert(browser->session_id().id());
802 TabStripModel* tab_strip = browser->tab_strip_model(); 787 TabStripModel* tab_strip = browser->tab_strip_model();
803 for (int i = 0; i < tab_strip->count(); ++i) { 788 for (int i = 0; i < tab_strip->count(); ++i) {
804 WebContents* tab = tab_strip->GetWebContentsAt(i); 789 WebContents* tab = tab_strip->GetWebContentsAt(i);
805 DCHECK(tab); 790 DCHECK(tab);
806 BuildCommandsForTab(browser->session_id(), tab, i, 791 BuildCommandsForTab(browser->session_id(),
792 tab,
793 i,
807 tab_strip->IsTabPinned(i), 794 tab_strip->IsTabPinned(i),
808 commands, tab_to_available_range); 795 tab_to_available_range);
809 } 796 }
810 797
811 commands->push_back(CreateSetSelectedTabInWindowCommand( 798 base_session_service_->AppendRebuildCommand(
799 CreateSetSelectedTabInWindowCommand(
812 browser->session_id(), 800 browser->session_id(),
813 browser->tab_strip_model()->active_index()).release()); 801 browser->tab_strip_model()->active_index()));
814 } 802 }
815 803
816 void SessionService::BuildCommandsFromBrowsers( 804 void SessionService::BuildCommandsFromBrowsers(
817 ScopedVector<SessionCommand>* commands,
818 IdToRange* tab_to_available_range, 805 IdToRange* tab_to_available_range,
819 std::set<SessionID::id_type>* windows_to_track) { 806 std::set<SessionID::id_type>* windows_to_track) {
820 DCHECK(commands);
821 for (chrome::BrowserIterator it; !it.done(); it.Next()) { 807 for (chrome::BrowserIterator it; !it.done(); it.Next()) {
822 Browser* browser = *it; 808 Browser* browser = *it;
823 // Make sure the browser has tabs and a window. Browser's destructor 809 // Make sure the browser has tabs and a window. Browser's destructor
824 // removes itself from the BrowserList. When a browser is closed the 810 // removes itself from the BrowserList. When a browser is closed the
825 // destructor is not necessarily run immediately. This means it's possible 811 // destructor is not necessarily run immediately. This means it's possible
826 // for us to get a handle to a browser that is about to be removed. If 812 // for us to get a handle to a browser that is about to be removed. If
827 // the tab count is 0 or the window is NULL, the browser is about to be 813 // the tab count is 0 or the window is NULL, the browser is about to be
828 // deleted, so we ignore it. 814 // deleted, so we ignore it.
829 if (ShouldTrackBrowser(browser) && browser->tab_strip_model()->count() && 815 if (ShouldTrackBrowser(browser) && browser->tab_strip_model()->count() &&
830 browser->window()) { 816 browser->window()) {
831 BuildCommandsForBrowser(browser, commands, tab_to_available_range, 817 BuildCommandsForBrowser(browser,
818 tab_to_available_range,
832 windows_to_track); 819 windows_to_track);
833 } 820 }
834 } 821 }
835 } 822 }
836 823
837 void SessionService::ScheduleResetCommands() { 824 void SessionService::ScheduleResetCommands() {
838 set_pending_reset(true); 825 base_session_service_->set_pending_reset(true);
839 pending_commands().clear(); 826 base_session_service_->ClearPendingCommands();
840 tab_to_available_range_.clear(); 827 tab_to_available_range_.clear();
841 windows_tracking_.clear(); 828 windows_tracking_.clear();
842 BuildCommandsFromBrowsers(&pending_commands(), 829 BuildCommandsFromBrowsers(&tab_to_available_range_,
843 &tab_to_available_range_,
844 &windows_tracking_); 830 &windows_tracking_);
845 if (!windows_tracking_.empty()) { 831 if (!windows_tracking_.empty()) {
846 // We're lazily created on startup and won't get an initial batch of 832 // We're lazily created on startup and won't get an initial batch of
847 // SetWindowType messages. Set these here to make sure our state is correct. 833 // SetWindowType messages. Set these here to make sure our state is correct.
848 has_open_trackable_browsers_ = true; 834 has_open_trackable_browsers_ = true;
849 move_on_new_browser_ = true; 835 move_on_new_browser_ = true;
850 } 836 }
851 StartSaveTimer(); 837 base_session_service_->StartSaveTimer();
852 } 838 }
853 839
854 void SessionService::ScheduleCommand(scoped_ptr<SessionCommand> command) { 840 void SessionService::ScheduleCommand(scoped_ptr<SessionCommand> command) {
855 DCHECK(command); 841 DCHECK(command);
856 if (ReplacePendingCommand(pending_commands(), &command)) 842 if (ReplacePendingCommand(base_session_service_.get(), &command))
857 return; 843 return;
858 bool is_closing_command = IsClosingCommand(command.get()); 844 bool is_closing_command = IsClosingCommand(command.get());
859 BaseSessionService::ScheduleCommand(command.Pass()); 845 base_session_service_->ScheduleCommand(command.Pass());
860 // Don't schedule a reset on tab closed/window closed. Otherwise we may 846 // Don't schedule a reset on tab closed/window closed. Otherwise we may
861 // lose tabs/windows we want to restore from if we exit right after this. 847 // lose tabs/windows we want to restore from if we exit right after this.
862 if (!pending_reset() && pending_window_close_ids_.empty() && 848 if (!base_session_service_->pending_reset() &&
863 commands_since_reset() >= kWritesPerReset && is_closing_command) { 849 pending_window_close_ids_.empty() &&
850 base_session_service_->commands_since_reset() >= kWritesPerReset &&
851 !is_closing_command) {
864 ScheduleResetCommands(); 852 ScheduleResetCommands();
865 } 853 }
866 } 854 }
867 855
868 void SessionService::CommitPendingCloses() { 856 void SessionService::CommitPendingCloses() {
869 for (PendingTabCloseIDs::iterator i = pending_tab_close_ids_.begin(); 857 for (PendingTabCloseIDs::iterator i = pending_tab_close_ids_.begin();
870 i != pending_tab_close_ids_.end(); ++i) { 858 i != pending_tab_close_ids_.end(); ++i) {
871 ScheduleCommand(CreateTabClosedCommand(*i).Pass()); 859 ScheduleCommand(CreateTabClosedCommand(*i).Pass());
872 } 860 }
873 pending_tab_close_ids_.clear(); 861 pending_tab_close_ids_.clear();
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after
1085 return; 1073 return;
1086 } 1074 }
1087 1075
1088 // Check for any open windows for the current profile that we aren't tracking. 1076 // Check for any open windows for the current profile that we aren't tracking.
1089 for (chrome::BrowserIterator it; !it.done(); it.Next()) { 1077 for (chrome::BrowserIterator it; !it.done(); it.Next()) {
1090 if ((*it)->profile() == profile()) 1078 if ((*it)->profile() == profile())
1091 return; 1079 return;
1092 } 1080 }
1093 DeleteSessionOnlyData(profile()); 1081 DeleteSessionOnlyData(profile());
1094 } 1082 }
1083
1084 BaseSessionService* SessionService::GetBaseSessionServiceForTest() {
1085 return base_session_service_.get();
1086 }
OLDNEW
« no previous file with comments | « chrome/browser/sessions/session_service.h ('k') | chrome/browser/sessions/session_service_commands.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698