1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/sessions/session_service.h"
#include <algorithm>
#include <set>
#include <utility>
#include <vector>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/command_line.h"
#include "base/message_loop/message_loop.h"
#include "base/metrics/histogram.h"
#include "base/pickle.h"
#include "base/threading/thread.h"
#include "chrome/browser/background/background_mode_manager.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/defaults.h"
#include "chrome/browser/extensions/tab_helper.h"
#include "chrome/browser/prefs/session_startup_pref.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/sessions/base_session_service_delegate_impl.h"
#include "chrome/browser/sessions/session_data_deleter.h"
#include "chrome/browser/sessions/session_restore.h"
#include "chrome/browser/sessions/session_service_utils.h"
#include "chrome/browser/sessions/session_tab_helper.h"
#include "chrome/browser/ui/browser_iterator.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_tabstrip.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/host_desktop.h"
#include "chrome/browser/ui/startup/startup_browser_creator.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "components/sessions/content/content_serialized_navigation_builder.h"
#include "components/sessions/session_command.h"
#include "components/sessions/session_types.h"
#include "components/startup_metric_utils/startup_metric_utils.h"
#include "content/public/browser/navigation_details.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/session_storage_namespace.h"
#include "content/public/browser/web_contents.h"
#include "extensions/common/extension.h"
#if defined(OS_MACOSX)
#include "chrome/browser/app_controller_mac.h"
#endif
using base::Time;
using content::NavigationEntry;
using content::WebContents;
using sessions::ContentSerializedNavigationBuilder;
using sessions::SerializedNavigationEntry;
// Every kWritesPerReset commands triggers recreating the file.
static const int kWritesPerReset = 250;
// SessionService -------------------------------------------------------------
SessionService::SessionService(Profile* profile)
: BaseSessionServiceDelegateImpl(true),
profile_(profile),
base_session_service_(
new sessions::BaseSessionService(
sessions::BaseSessionService::SESSION_RESTORE,
profile->GetPath(),
this)),
has_open_trackable_browsers_(false),
move_on_new_browser_(false),
save_delay_in_millis_(base::TimeDelta::FromMilliseconds(2500)),
save_delay_in_mins_(base::TimeDelta::FromMinutes(10)),
save_delay_in_hrs_(base::TimeDelta::FromHours(8)),
force_browser_not_alive_with_no_windows_(false),
weak_factory_(this) {
// We should never be created when incognito.
DCHECK(!profile->IsOffTheRecord());
Init();
}
SessionService::SessionService(const base::FilePath& save_path)
: BaseSessionServiceDelegateImpl(false),
profile_(NULL),
base_session_service_(
new sessions::BaseSessionService(
sessions::BaseSessionService::SESSION_RESTORE,
save_path,
this)),
has_open_trackable_browsers_(false),
move_on_new_browser_(false),
save_delay_in_millis_(base::TimeDelta::FromMilliseconds(2500)),
save_delay_in_mins_(base::TimeDelta::FromMinutes(10)),
save_delay_in_hrs_(base::TimeDelta::FromHours(8)),
force_browser_not_alive_with_no_windows_(false),
weak_factory_(this) {
Init();
}
SessionService::~SessionService() {
// The BrowserList should outlive the SessionService since it's static and
// the SessionService is a KeyedService.
BrowserList::RemoveObserver(this);
base_session_service_->Save();
}
bool SessionService::ShouldNewWindowStartSession() {
// ChromeOS and OSX have different ideas of application lifetime than
// the other platforms.
// On ChromeOS opening a new window should never start a new session.
#if defined(OS_CHROMEOS)
if (!force_browser_not_alive_with_no_windows_)
return false;
#endif
if (!has_open_trackable_browsers_ &&
!StartupBrowserCreator::InSynchronousProfileLaunch() &&
!SessionRestore::IsRestoring(profile())
#if defined(OS_MACOSX)
// On OSX, a new window should not start a new session if it was opened
// from the dock or the menubar.
&& !app_controller_mac::IsOpeningNewWindow()
#endif // OS_MACOSX
) {
return true;
}
return false;
}
bool SessionService::RestoreIfNecessary(const std::vector<GURL>& urls_to_open) {
return RestoreIfNecessary(urls_to_open, NULL);
}
void SessionService::ResetFromCurrentBrowsers() {
ScheduleResetCommands();
}
void SessionService::MoveCurrentSessionToLastSession() {
pending_tab_close_ids_.clear();
window_closing_ids_.clear();
pending_window_close_ids_.clear();
base_session_service_->MoveCurrentSessionToLastSession();
}
void SessionService::DeleteLastSession() {
base_session_service_->DeleteLastSession();
}
void SessionService::SetTabWindow(const SessionID& window_id,
const SessionID& tab_id) {
if (!ShouldTrackChangesToWindow(window_id))
return;
ScheduleCommand(sessions::CreateSetTabWindowCommand(window_id,
tab_id).Pass());
}
void SessionService::SetWindowBounds(const SessionID& window_id,
const gfx::Rect& bounds,
ui::WindowShowState show_state) {
if (!ShouldTrackChangesToWindow(window_id))
return;
ScheduleCommand(sessions::CreateSetWindowBoundsCommand(
window_id, bounds, show_state).Pass());
}
void SessionService::SetTabIndexInWindow(const SessionID& window_id,
const SessionID& tab_id,
int new_index) {
if (!ShouldTrackChangesToWindow(window_id))
return;
ScheduleCommand(sessions::CreateSetTabIndexInWindowCommand(tab_id,
new_index).Pass());
}
void SessionService::SetPinnedState(const SessionID& window_id,
const SessionID& tab_id,
bool is_pinned) {
if (!ShouldTrackChangesToWindow(window_id))
return;
ScheduleCommand(sessions::CreatePinnedStateCommand(tab_id, is_pinned).Pass());
}
void SessionService::TabClosed(const SessionID& window_id,
const SessionID& tab_id,
bool closed_by_user_gesture) {
if (!tab_id.id())
return; // Hapens when the tab is replaced.
if (!ShouldTrackChangesToWindow(window_id))
return;
IdToRange::iterator i = tab_to_available_range_.find(tab_id.id());
if (i != tab_to_available_range_.end())
tab_to_available_range_.erase(i);
if (find(pending_window_close_ids_.begin(), pending_window_close_ids_.end(),
window_id.id()) != pending_window_close_ids_.end()) {
// Tab is in last window. Don't commit it immediately, instead add it to the
// list of tabs to close. If the user creates another window, the close is
// committed.
pending_tab_close_ids_.insert(tab_id.id());
} else if (find(window_closing_ids_.begin(), window_closing_ids_.end(),
window_id.id()) != window_closing_ids_.end() ||
!IsOnlyOneTabLeft() ||
closed_by_user_gesture) {
// Close is the result of one of the following:
// . window close (and it isn't the last window).
// . closing a tab and there are other windows/tabs open.
// . closed by a user gesture.
// In all cases we need to mark the tab as explicitly closed.
ScheduleCommand(sessions::CreateTabClosedCommand(tab_id.id()).Pass());
} else {
// User closed the last tab in the last tabbed browser. Don't mark the
// tab closed.
pending_tab_close_ids_.insert(tab_id.id());
has_open_trackable_browsers_ = false;
}
}
void SessionService::WindowOpened(Browser* browser) {
if (!ShouldTrackBrowser(browser))
return;
RestoreIfNecessary(std::vector<GURL>(), browser);
SetWindowType(browser->session_id(),
browser->type(),
browser->is_app() ? TYPE_APP : TYPE_NORMAL);
SetWindowAppName(browser->session_id(), browser->app_name());
}
void SessionService::WindowClosing(const SessionID& window_id) {
if (!ShouldTrackChangesToWindow(window_id))
return;
// The window is about to close. If there are other tabbed browsers with the
// same original profile commit the close immediately.
//
// NOTE: if the user chooses the exit menu item session service is destroyed
// and this code isn't hit.
if (has_open_trackable_browsers_) {
// Closing a window can never make has_open_trackable_browsers_ go from
// false to true, so only update it if already true.
has_open_trackable_browsers_ = HasOpenTrackableBrowsers(window_id);
}
bool use_pending_close = !has_open_trackable_browsers_;
if (!use_pending_close) {
// Somewhat outside of "normal behavior" is profile locking. In this case
// (when IsSiginRequired has already been set True), we're closing all
// browser windows in turn but want them all to be restored when the user
// unlocks. To accomplish this, we do a "pending close" on all windows
// instead of just the last one (which has no open_trackable_browsers).
// http://crbug.com/356818
//
// Some editions (like iOS) don't have a profile_manager and some tests
// don't supply one so be lenient.
if (g_browser_process) {
ProfileManager* profile_manager = g_browser_process->profile_manager();
if (profile_manager) {
ProfileInfoCache& profile_info =
profile_manager->GetProfileInfoCache();
size_t profile_index = profile_info.GetIndexOfProfileWithPath(
profile()->GetPath());
use_pending_close = profile_index != std::string::npos &&
profile_info.ProfileIsSigninRequiredAtIndex(profile_index);
}
}
}
if (use_pending_close)
pending_window_close_ids_.insert(window_id.id());
else
window_closing_ids_.insert(window_id.id());
}
void SessionService::WindowClosed(const SessionID& window_id) {
if (!ShouldTrackChangesToWindow(window_id)) {
// The last window may be one that is not tracked.
MaybeDeleteSessionOnlyData();
return;
}
windows_tracking_.erase(window_id.id());
if (window_closing_ids_.find(window_id.id()) != window_closing_ids_.end()) {
window_closing_ids_.erase(window_id.id());
ScheduleCommand(sessions::CreateWindowClosedCommand(window_id.id()).Pass());
} else if (pending_window_close_ids_.find(window_id.id()) ==
pending_window_close_ids_.end()) {
// We'll hit this if user closed the last tab in a window.
has_open_trackable_browsers_ = HasOpenTrackableBrowsers(window_id);
if (!has_open_trackable_browsers_)
pending_window_close_ids_.insert(window_id.id());
else
ScheduleCommand(sessions::CreateWindowClosedCommand(
window_id.id()).Pass());
}
MaybeDeleteSessionOnlyData();
}
void SessionService::TabInserted(WebContents* contents) {
SessionTabHelper* session_tab_helper =
SessionTabHelper::FromWebContents(contents);
if (!ShouldTrackChangesToWindow(session_tab_helper->window_id()))
return;
SetTabWindow(session_tab_helper->window_id(),
session_tab_helper->session_id());
extensions::TabHelper* extensions_tab_helper =
extensions::TabHelper::FromWebContents(contents);
if (extensions_tab_helper &&
extensions_tab_helper->extension_app()) {
SetTabExtensionAppID(
session_tab_helper->window_id(),
session_tab_helper->session_id(),
extensions_tab_helper->extension_app()->id());
}
// Record the association between the SessionStorageNamespace and the
// tab.
//
// TODO(ajwong): This should be processing the whole map rather than
// just the default. This in particular will not work for tabs with only
// isolated apps which won't have a default partition.
content::SessionStorageNamespace* session_storage_namespace =
contents->GetController().GetDefaultSessionStorageNamespace();
ScheduleCommand(sessions::CreateSessionStorageAssociatedCommand(
session_tab_helper->session_id(),
session_storage_namespace->persistent_id()).Pass());
session_storage_namespace->SetShouldPersist(true);
}
void SessionService::TabClosing(WebContents* contents) {
// Allow the associated sessionStorage to get deleted; it won't be needed
// in the session restore.
content::SessionStorageNamespace* session_storage_namespace =
contents->GetController().GetDefaultSessionStorageNamespace();
session_storage_namespace->SetShouldPersist(false);
SessionTabHelper* session_tab_helper =
SessionTabHelper::FromWebContents(contents);
TabClosed(session_tab_helper->window_id(),
session_tab_helper->session_id(),
contents->GetClosedByUserGesture());
RecordSessionUpdateHistogramData(content::NOTIFICATION_WEB_CONTENTS_DESTROYED,
&last_updated_tab_closed_time_);
}
void SessionService::SetWindowType(const SessionID& window_id,
Browser::Type type,
AppType app_type) {
sessions::SessionWindow::WindowType window_type =
WindowTypeForBrowserType(type);
if (!ShouldRestoreWindowOfType(window_type, app_type))
return;
windows_tracking_.insert(window_id.id());
// The user created a new tabbed browser with our profile. Commit any
// pending closes.
CommitPendingCloses();
has_open_trackable_browsers_ = true;
move_on_new_browser_ = true;
ScheduleCommand(CreateSetWindowTypeCommand(window_id, window_type).Pass());
}
void SessionService::SetWindowAppName(
const SessionID& window_id,
const std::string& app_name) {
if (!ShouldTrackChangesToWindow(window_id))
return;
ScheduleCommand(sessions::CreateSetWindowAppNameCommand(window_id,
app_name).Pass());
}
void SessionService::TabNavigationPathPrunedFromBack(const SessionID& window_id,
const SessionID& tab_id,
int count) {
if (!ShouldTrackChangesToWindow(window_id))
return;
ScheduleCommand(sessions::CreateTabNavigationPathPrunedFromBackCommand(
tab_id, count).Pass());
}
void SessionService::TabNavigationPathPrunedFromFront(
const SessionID& window_id,
const SessionID& tab_id,
int count) {
if (!ShouldTrackChangesToWindow(window_id))
return;
// Update the range of indices.
if (tab_to_available_range_.find(tab_id.id()) !=
tab_to_available_range_.end()) {
std::pair<int, int>& range = tab_to_available_range_[tab_id.id()];
range.first = std::max(0, range.first - count);
range.second = std::max(0, range.second - count);
}
ScheduleCommand(sessions::CreateTabNavigationPathPrunedFromFrontCommand(
tab_id, count).Pass());
}
void SessionService::UpdateTabNavigation(
const SessionID& window_id,
const SessionID& tab_id,
const SerializedNavigationEntry& navigation) {
if (!ShouldTrackEntry(navigation.virtual_url()) ||
!ShouldTrackChangesToWindow(window_id)) {
return;
}
if (tab_to_available_range_.find(tab_id.id()) !=
tab_to_available_range_.end()) {
std::pair<int, int>& range = tab_to_available_range_[tab_id.id()];
range.first = std::min(navigation.index(), range.first);
range.second = std::max(navigation.index(), range.second);
}
ScheduleCommand(CreateUpdateTabNavigationCommand(tab_id, navigation).Pass());
}
void SessionService::TabRestored(WebContents* tab, bool pinned) {
SessionTabHelper* session_tab_helper = SessionTabHelper::FromWebContents(tab);
if (!ShouldTrackChangesToWindow(session_tab_helper->window_id()))
return;
BuildCommandsForTab(session_tab_helper->window_id(), tab, -1, pinned, NULL);
base_session_service_->StartSaveTimer();
}
void SessionService::SetSelectedNavigationIndex(const SessionID& window_id,
const SessionID& tab_id,
int index) {
if (!ShouldTrackChangesToWindow(window_id))
return;
if (tab_to_available_range_.find(tab_id.id()) !=
tab_to_available_range_.end()) {
if (index < tab_to_available_range_[tab_id.id()].first ||
index > tab_to_available_range_[tab_id.id()].second) {
// The new index is outside the range of what we've archived, schedule
// a reset.
ResetFromCurrentBrowsers();
return;
}
}
ScheduleCommand(
sessions::CreateSetSelectedNavigationIndexCommand(tab_id, index).Pass());
}
void SessionService::SetSelectedTabInWindow(const SessionID& window_id,
int index) {
if (!ShouldTrackChangesToWindow(window_id))
return;
ScheduleCommand(
sessions::CreateSetSelectedTabInWindowCommand(window_id, index).Pass());
}
void SessionService::SetTabUserAgentOverride(
const SessionID& window_id,
const SessionID& tab_id,
const std::string& user_agent_override) {
if (!ShouldTrackChangesToWindow(window_id))
return;
ScheduleCommand(sessions::CreateSetTabUserAgentOverrideCommand(
tab_id, user_agent_override).Pass());
}
void SessionService::SetTabExtensionAppID(
const SessionID& window_id,
const SessionID& tab_id,
const std::string& extension_app_id) {
if (!ShouldTrackChangesToWindow(window_id))
return;
ScheduleCommand(sessions::CreateSetTabExtensionAppIDCommand(
tab_id, extension_app_id).Pass());
}
base::CancelableTaskTracker::TaskId SessionService::GetLastSession(
const SessionCallback& callback,
base::CancelableTaskTracker* tracker) {
// OnGotSessionCommands maps the SessionCommands to browser state, then run
// the callback.
return base_session_service_->ScheduleGetLastSessionCommands(
base::Bind(&SessionService::OnGotSessionCommands,
weak_factory_.GetWeakPtr(),
callback),
tracker);
}
void SessionService::OnSavedCommands() {
RecordSessionUpdateHistogramData(chrome::NOTIFICATION_SESSION_SERVICE_SAVED,
&last_updated_save_time_);
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_SESSION_SERVICE_SAVED,
content::Source<Profile>(profile()),
content::NotificationService::NoDetails());
}
void SessionService::Init() {
// Register for the notifications we're interested in.
registrar_.Add(this, content::NOTIFICATION_NAV_LIST_PRUNED,
content::NotificationService::AllSources());
registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_CHANGED,
content::NotificationService::AllSources());
registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED,
content::NotificationService::AllSources());
registrar_.Add(
this, chrome::NOTIFICATION_TAB_CONTENTS_APPLICATION_EXTENSION_CHANGED,
content::NotificationService::AllSources());
BrowserList::AddObserver(this);
}
bool SessionService::ShouldRestoreWindowOfType(
sessions::SessionWindow::WindowType window_type,
AppType app_type) const {
#if defined(OS_CHROMEOS)
// Restore app popups for ChromeOS alone.
if (window_type == sessions::SessionWindow::TYPE_POPUP &&
app_type == TYPE_APP)
return true;
#endif
return window_type == sessions::SessionWindow::TYPE_TABBED;
}
void SessionService::RemoveUnusedRestoreWindows(
std::vector<sessions::SessionWindow*>* window_list) {
std::vector<sessions::SessionWindow*>::iterator i = window_list->begin();
while (i != window_list->end()) {
sessions::SessionWindow* window = *i;
if (!ShouldRestoreWindowOfType(window->type,
window->app_name.empty() ? TYPE_NORMAL :
TYPE_APP)) {
delete window;
window_list->erase(i++);
} else {
++i;
}
}
}
bool SessionService::RestoreIfNecessary(const std::vector<GURL>& urls_to_open,
Browser* browser) {
if (ShouldNewWindowStartSession()) {
// We're going from no tabbed browsers to a tabbed browser (and not in
// process startup), restore the last session.
if (move_on_new_browser_) {
// Make the current session the last.
MoveCurrentSessionToLastSession();
move_on_new_browser_ = false;
}
SessionStartupPref pref = StartupBrowserCreator::GetSessionStartupPref(
*base::CommandLine::ForCurrentProcess(), profile());
if (pref.type == SessionStartupPref::LAST) {
SessionRestore::RestoreSession(
profile(), browser,
browser ? browser->host_desktop_type() : chrome::GetActiveDesktop(),
browser ? 0 : SessionRestore::ALWAYS_CREATE_TABBED_BROWSER,
urls_to_open);
return true;
}
}
return false;
}
void SessionService::Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
// All of our messages have the NavigationController as the source.
switch (type) {
case content::NOTIFICATION_NAV_LIST_PRUNED: {
WebContents* web_contents =
content::Source<content::NavigationController>(source).ptr()->
GetWebContents();
SessionTabHelper* session_tab_helper =
SessionTabHelper::FromWebContents(web_contents);
if (!session_tab_helper || web_contents->GetBrowserContext() != profile())
return;
content::Details<content::PrunedDetails> pruned_details(details);
if (pruned_details->from_front) {
TabNavigationPathPrunedFromFront(
session_tab_helper->window_id(),
session_tab_helper->session_id(),
pruned_details->count);
} else {
TabNavigationPathPrunedFromBack(
session_tab_helper->window_id(),
session_tab_helper->session_id(),
web_contents->GetController().GetEntryCount());
}
RecordSessionUpdateHistogramData(type,
&last_updated_nav_list_pruned_time_);
break;
}
case content::NOTIFICATION_NAV_ENTRY_CHANGED: {
WebContents* web_contents =
content::Source<content::NavigationController>(source).ptr()->
GetWebContents();
SessionTabHelper* session_tab_helper =
SessionTabHelper::FromWebContents(web_contents);
if (!session_tab_helper || web_contents->GetBrowserContext() != profile())
return;
content::Details<content::EntryChangedDetails> changed(details);
const SerializedNavigationEntry navigation =
ContentSerializedNavigationBuilder::FromNavigationEntry(
changed->index, *changed->changed_entry);
UpdateTabNavigation(session_tab_helper->window_id(),
session_tab_helper->session_id(),
navigation);
break;
}
case content::NOTIFICATION_NAV_ENTRY_COMMITTED: {
WebContents* web_contents =
content::Source<content::NavigationController>(source).ptr()->
GetWebContents();
SessionTabHelper* session_tab_helper =
SessionTabHelper::FromWebContents(web_contents);
if (!session_tab_helper || web_contents->GetBrowserContext() != profile())
return;
int current_entry_index =
web_contents->GetController().GetCurrentEntryIndex();
SetSelectedNavigationIndex(
session_tab_helper->window_id(),
session_tab_helper->session_id(),
current_entry_index);
const SerializedNavigationEntry navigation =
ContentSerializedNavigationBuilder::FromNavigationEntry(
current_entry_index,
*web_contents->GetController().GetEntryAtIndex(
current_entry_index));
UpdateTabNavigation(
session_tab_helper->window_id(),
session_tab_helper->session_id(),
navigation);
content::Details<content::LoadCommittedDetails> changed(details);
if (changed->type == content::NAVIGATION_TYPE_NEW_PAGE ||
changed->type == content::NAVIGATION_TYPE_EXISTING_PAGE) {
RecordSessionUpdateHistogramData(type,
&last_updated_nav_entry_commit_time_);
}
break;
}
case chrome::NOTIFICATION_TAB_CONTENTS_APPLICATION_EXTENSION_CHANGED: {
extensions::TabHelper* extension_tab_helper =
content::Source<extensions::TabHelper>(source).ptr();
if (extension_tab_helper->web_contents()->GetBrowserContext() !=
profile()) {
return;
}
if (extension_tab_helper->extension_app()) {
SessionTabHelper* session_tab_helper =
SessionTabHelper::FromWebContents(
extension_tab_helper->web_contents());
SetTabExtensionAppID(session_tab_helper->window_id(),
session_tab_helper->session_id(),
extension_tab_helper->extension_app()->id());
}
break;
}
default:
NOTREACHED();
}
}
void SessionService::OnBrowserSetLastActive(Browser* browser) {
if (ShouldTrackBrowser(browser))
ScheduleCommand(sessions::CreateSetActiveWindowCommand(
browser->session_id()).Pass());
}
void SessionService::OnGotSessionCommands(
const SessionCallback& callback,
ScopedVector<sessions::SessionCommand> commands) {
ScopedVector<sessions::SessionWindow> valid_windows;
SessionID::id_type active_window_id = 0;
startup_metric_utils::ScopedSlowStartupUMA
scoped_timer("Startup.SlowStartupSessionServiceCreateTabsAndWindows");
sessions::RestoreSessionFromCommands(
commands, &valid_windows.get(), &active_window_id);
RemoveUnusedRestoreWindows(&valid_windows.get());
callback.Run(valid_windows.Pass(), active_window_id);
}
void SessionService::BuildCommandsForTab(const SessionID& window_id,
WebContents* tab,
int index_in_window,
bool is_pinned,
IdToRange* tab_to_available_range) {
DCHECK(tab && window_id.id());
SessionTabHelper* session_tab_helper = SessionTabHelper::FromWebContents(tab);
const SessionID& session_id(session_tab_helper->session_id());
base_session_service_->AppendRebuildCommand(
sessions::CreateSetTabWindowCommand(window_id, session_id));
const int current_index = tab->GetController().GetCurrentEntryIndex();
const int min_index = std::max(current_index - gMaxPersistNavigationCount, 0);
const int max_index = std::min(current_index + gMaxPersistNavigationCount,
tab->GetController().GetEntryCount());
const int pending_index = tab->GetController().GetPendingEntryIndex();
if (tab_to_available_range) {
(*tab_to_available_range)[session_id.id()] =
std::pair<int, int>(min_index, max_index);
}
if (is_pinned) {
base_session_service_->AppendRebuildCommand(
sessions::CreatePinnedStateCommand(session_id, true));
}
extensions::TabHelper* extensions_tab_helper =
extensions::TabHelper::FromWebContents(tab);
if (extensions_tab_helper->extension_app()) {
base_session_service_->AppendRebuildCommand(
sessions::CreateSetTabExtensionAppIDCommand(
session_id,
extensions_tab_helper->extension_app()->id()));
}
const std::string& ua_override = tab->GetUserAgentOverride();
if (!ua_override.empty()) {
base_session_service_->AppendRebuildCommand(
sessions::CreateSetTabUserAgentOverrideCommand(session_id,
ua_override));
}
for (int i = min_index; i < max_index; ++i) {
const NavigationEntry* entry = (i == pending_index) ?
tab->GetController().GetPendingEntry() :
tab->GetController().GetEntryAtIndex(i);
DCHECK(entry);
if (ShouldTrackEntry(entry->GetVirtualURL())) {
const SerializedNavigationEntry navigation =
ContentSerializedNavigationBuilder::FromNavigationEntry(i, *entry);
base_session_service_->AppendRebuildCommand(
CreateUpdateTabNavigationCommand(session_id, navigation));
}
}
base_session_service_->AppendRebuildCommand(
sessions::CreateSetSelectedNavigationIndexCommand(session_id,
current_index));
if (index_in_window != -1) {
base_session_service_->AppendRebuildCommand(
sessions::CreateSetTabIndexInWindowCommand(session_id,
index_in_window));
}
// Record the association between the sessionStorage namespace and the tab.
content::SessionStorageNamespace* session_storage_namespace =
tab->GetController().GetDefaultSessionStorageNamespace();
ScheduleCommand(sessions::CreateSessionStorageAssociatedCommand(
session_tab_helper->session_id(),
session_storage_namespace->persistent_id()).Pass());
}
void SessionService::BuildCommandsForBrowser(
Browser* browser,
IdToRange* tab_to_available_range,
std::set<SessionID::id_type>* windows_to_track) {
DCHECK(browser);
DCHECK(browser->session_id().id());
base_session_service_->AppendRebuildCommand(
sessions::CreateSetWindowBoundsCommand(
browser->session_id(),
browser->window()->GetRestoredBounds(),
browser->window()->GetRestoredState()));
base_session_service_->AppendRebuildCommand(
sessions::CreateSetWindowTypeCommand(
browser->session_id(),
WindowTypeForBrowserType(browser->type())));
if (!browser->app_name().empty()) {
base_session_service_->AppendRebuildCommand(
sessions::CreateSetWindowAppNameCommand(
browser->session_id(),
browser->app_name()));
}
windows_to_track->insert(browser->session_id().id());
TabStripModel* tab_strip = browser->tab_strip_model();
for (int i = 0; i < tab_strip->count(); ++i) {
WebContents* tab = tab_strip->GetWebContentsAt(i);
DCHECK(tab);
BuildCommandsForTab(browser->session_id(),
tab,
i,
tab_strip->IsTabPinned(i),
tab_to_available_range);
}
base_session_service_->AppendRebuildCommand(
sessions::CreateSetSelectedTabInWindowCommand(
browser->session_id(),
browser->tab_strip_model()->active_index()));
}
void SessionService::BuildCommandsFromBrowsers(
IdToRange* tab_to_available_range,
std::set<SessionID::id_type>* windows_to_track) {
for (chrome::BrowserIterator it; !it.done(); it.Next()) {
Browser* browser = *it;
// Make sure the browser has tabs and a window. Browser's destructor
// removes itself from the BrowserList. When a browser is closed the
// destructor is not necessarily run immediately. This means it's possible
// for us to get a handle to a browser that is about to be removed. If
// the tab count is 0 or the window is NULL, the browser is about to be
// deleted, so we ignore it.
if (ShouldTrackBrowser(browser) && browser->tab_strip_model()->count() &&
browser->window()) {
BuildCommandsForBrowser(browser,
tab_to_available_range,
windows_to_track);
}
}
}
void SessionService::ScheduleResetCommands() {
base_session_service_->set_pending_reset(true);
base_session_service_->ClearPendingCommands();
tab_to_available_range_.clear();
windows_tracking_.clear();
BuildCommandsFromBrowsers(&tab_to_available_range_,
&windows_tracking_);
if (!windows_tracking_.empty()) {
// We're lazily created on startup and won't get an initial batch of
// SetWindowType messages. Set these here to make sure our state is correct.
has_open_trackable_browsers_ = true;
move_on_new_browser_ = true;
}
base_session_service_->StartSaveTimer();
}
void SessionService::ScheduleCommand(
scoped_ptr<sessions::SessionCommand> command) {
DCHECK(command);
if (ReplacePendingCommand(base_session_service_.get(), &command))
return;
bool is_closing_command = IsClosingCommand(command.get());
base_session_service_->ScheduleCommand(command.Pass());
// Don't schedule a reset on tab closed/window closed. Otherwise we may
// lose tabs/windows we want to restore from if we exit right after this.
if (!base_session_service_->pending_reset() &&
pending_window_close_ids_.empty() &&
base_session_service_->commands_since_reset() >= kWritesPerReset &&
!is_closing_command) {
ScheduleResetCommands();
}
}
void SessionService::CommitPendingCloses() {
for (PendingTabCloseIDs::iterator i = pending_tab_close_ids_.begin();
i != pending_tab_close_ids_.end(); ++i) {
ScheduleCommand(sessions::CreateTabClosedCommand(*i).Pass());
}
pending_tab_close_ids_.clear();
for (PendingWindowCloseIDs::iterator i = pending_window_close_ids_.begin();
i != pending_window_close_ids_.end(); ++i) {
ScheduleCommand(sessions::CreateWindowClosedCommand(*i).Pass());
}
pending_window_close_ids_.clear();
}
bool SessionService::IsOnlyOneTabLeft() const {
if (!profile() || profile()->AsTestingProfile()) {
// We're testing, always return false.
return false;
}
int window_count = 0;
for (chrome::BrowserIterator it; !it.done(); it.Next()) {
Browser* browser = *it;
const SessionID::id_type window_id = browser->session_id().id();
if (ShouldTrackBrowser(browser) &&
window_closing_ids_.find(window_id) == window_closing_ids_.end()) {
if (++window_count > 1)
return false;
// By the time this is invoked the tab has been removed. As such, we use
// > 0 here rather than > 1.
if (browser->tab_strip_model()->count() > 0)
return false;
}
}
return true;
}
bool SessionService::HasOpenTrackableBrowsers(
const SessionID& window_id) const {
if (!profile() || profile()->AsTestingProfile()) {
// We're testing, always return true.
return true;
}
for (chrome::BrowserIterator it; !it.done(); it.Next()) {
Browser* browser = *it;
const SessionID::id_type browser_id = browser->session_id().id();
if (browser_id != window_id.id() &&
window_closing_ids_.find(browser_id) == window_closing_ids_.end() &&
ShouldTrackBrowser(browser)) {
return true;
}
}
return false;
}
bool SessionService::ShouldTrackChangesToWindow(
const SessionID& window_id) const {
return windows_tracking_.find(window_id.id()) != windows_tracking_.end();
}
bool SessionService::ShouldTrackBrowser(Browser* browser) const {
if (browser->profile() != profile())
return false;
// Never track app popup windows that do not have a trusted source (i.e.
// popup windows spawned by an app). If this logic changes, be sure to also
// change SessionRestoreImpl::CreateRestoredBrowser().
if (browser->is_app() && browser->is_type_popup() &&
!browser->is_trusted_source()) {
return false;
}
return ShouldRestoreWindowOfType(WindowTypeForBrowserType(browser->type()),
browser->is_app() ? TYPE_APP : TYPE_NORMAL);
}
void SessionService::RecordSessionUpdateHistogramData(int type,
base::TimeTicks* last_updated_time) {
if (!last_updated_time->is_null()) {
base::TimeDelta delta = base::TimeTicks::Now() - *last_updated_time;
// We're interested in frequent updates periods longer than
// 10 minutes.
bool use_long_period = false;
if (delta >= save_delay_in_mins_) {
use_long_period = true;
}
switch (type) {
case chrome::NOTIFICATION_SESSION_SERVICE_SAVED :
RecordUpdatedSaveTime(delta, use_long_period);
RecordUpdatedSessionNavigationOrTab(delta, use_long_period);
break;
case content::NOTIFICATION_WEB_CONTENTS_DESTROYED:
RecordUpdatedTabClosed(delta, use_long_period);
RecordUpdatedSessionNavigationOrTab(delta, use_long_period);
break;
case content::NOTIFICATION_NAV_LIST_PRUNED:
RecordUpdatedNavListPruned(delta, use_long_period);
RecordUpdatedSessionNavigationOrTab(delta, use_long_period);
break;
case content::NOTIFICATION_NAV_ENTRY_COMMITTED:
RecordUpdatedNavEntryCommit(delta, use_long_period);
RecordUpdatedSessionNavigationOrTab(delta, use_long_period);
break;
default:
NOTREACHED() << "Bad type sent to RecordSessionUpdateHistogramData";
break;
}
}
(*last_updated_time) = base::TimeTicks::Now();
}
void SessionService::RecordUpdatedTabClosed(base::TimeDelta delta,
bool use_long_period) {
std::string name("SessionRestore.TabClosedPeriod");
UMA_HISTOGRAM_CUSTOM_TIMES(name,
delta,
// 2500ms is the default save delay.
save_delay_in_millis_,
save_delay_in_mins_,
50);
if (use_long_period) {
std::string long_name_("SessionRestore.TabClosedLongPeriod");
UMA_HISTOGRAM_CUSTOM_TIMES(long_name_,
delta,
save_delay_in_mins_,
save_delay_in_hrs_,
50);
}
}
void SessionService::RecordUpdatedNavListPruned(base::TimeDelta delta,
bool use_long_period) {
std::string name("SessionRestore.NavigationListPrunedPeriod");
UMA_HISTOGRAM_CUSTOM_TIMES(name,
delta,
// 2500ms is the default save delay.
save_delay_in_millis_,
save_delay_in_mins_,
50);
if (use_long_period) {
std::string long_name_("SessionRestore.NavigationListPrunedLongPeriod");
UMA_HISTOGRAM_CUSTOM_TIMES(long_name_,
delta,
save_delay_in_mins_,
save_delay_in_hrs_,
50);
}
}
void SessionService::RecordUpdatedNavEntryCommit(base::TimeDelta delta,
bool use_long_period) {
std::string name("SessionRestore.NavEntryCommittedPeriod");
UMA_HISTOGRAM_CUSTOM_TIMES(name,
delta,
// 2500ms is the default save delay.
save_delay_in_millis_,
save_delay_in_mins_,
50);
if (use_long_period) {
std::string long_name_("SessionRestore.NavEntryCommittedLongPeriod");
UMA_HISTOGRAM_CUSTOM_TIMES(long_name_,
delta,
save_delay_in_mins_,
save_delay_in_hrs_,
50);
}
}
void SessionService::RecordUpdatedSaveTime(base::TimeDelta delta,
bool use_long_period) {
std::string name("SessionRestore.SavePeriod");
UMA_HISTOGRAM_CUSTOM_TIMES(name,
delta,
// 2500ms is the default save delay.
save_delay_in_millis_,
save_delay_in_mins_,
50);
if (use_long_period) {
std::string long_name_("SessionRestore.SaveLongPeriod");
UMA_HISTOGRAM_CUSTOM_TIMES(long_name_,
delta,
save_delay_in_mins_,
save_delay_in_hrs_,
50);
}
}
void SessionService::RecordUpdatedSessionNavigationOrTab(base::TimeDelta delta,
bool use_long_period) {
std::string name("SessionRestore.NavOrTabUpdatePeriod");
UMA_HISTOGRAM_CUSTOM_TIMES(name,
delta,
// 2500ms is the default save delay.
save_delay_in_millis_,
save_delay_in_mins_,
50);
if (use_long_period) {
std::string long_name_("SessionRestore.NavOrTabUpdateLongPeriod");
UMA_HISTOGRAM_CUSTOM_TIMES(long_name_,
delta,
save_delay_in_mins_,
save_delay_in_hrs_,
50);
}
}
void SessionService::MaybeDeleteSessionOnlyData() {
// Don't try anything if we're testing. The browser_process is not fully
// created and DeleteSession will crash if we actually attempt it.
if (!profile() || profile()->AsTestingProfile())
return;
// Clear session data if the last window for a profile has been closed and
// closing the last window would normally close Chrome, unless background mode
// is active. Tests don't have a background_mode_manager.
if (has_open_trackable_browsers_ ||
browser_defaults::kBrowserAliveWithNoWindows ||
g_browser_process->background_mode_manager()->IsBackgroundModeActive()) {
return;
}
// Check for any open windows for the current profile that we aren't tracking.
for (chrome::BrowserIterator it; !it.done(); it.Next()) {
if ((*it)->profile() == profile())
return;
}
DeleteSessionOnlyData(profile());
}
sessions::BaseSessionService* SessionService::GetBaseSessionServiceForTest() {
return base_session_service_.get();
}
|