| OLD | NEW |
| 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/ui/startup/startup_browser_creator_impl.h" | 5 #include "chrome/browser/ui/startup/startup_browser_creator_impl.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "apps/app_restore_service.h" | 10 #include "apps/app_restore_service.h" |
| (...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 348 } | 348 } |
| 349 } | 349 } |
| 350 | 350 |
| 351 // Open the required browser windows and tabs. First, see if | 351 // Open the required browser windows and tabs. First, see if |
| 352 // we're being run as an application window. If so, the user | 352 // we're being run as an application window. If so, the user |
| 353 // opened an app shortcut. Don't restore tabs or open initial | 353 // opened an app shortcut. Don't restore tabs or open initial |
| 354 // URLs in that case. The user should see the window as an app, | 354 // URLs in that case. The user should see the window as an app, |
| 355 // not as chrome. | 355 // not as chrome. |
| 356 // Special case is when app switches are passed but we do want to restore | 356 // Special case is when app switches are passed but we do want to restore |
| 357 // session. In that case open app window + focus it after session is restored. | 357 // session. In that case open app window + focus it after session is restored. |
| 358 content::WebContents* app_contents = NULL; | 358 content::WebContents* app_contents = nullptr; |
| 359 Browser* browser = nullptr; |
| 359 if (OpenApplicationWindow(profile, &app_contents)) { | 360 if (OpenApplicationWindow(profile, &app_contents)) { |
| 360 RecordLaunchModeHistogram(LM_AS_WEBAPP); | 361 RecordLaunchModeHistogram(LM_AS_WEBAPP); |
| 361 } else { | 362 } else { |
| 362 RecordLaunchModeHistogram(urls_to_open.empty() ? | 363 RecordLaunchModeHistogram(urls_to_open.empty() ? |
| 363 LM_TO_BE_DECIDED : LM_WITH_URLS); | 364 LM_TO_BE_DECIDED : LM_WITH_URLS); |
| 364 | 365 |
| 365 ProcessLaunchURLs(process_startup, urls_to_open, desktop_type); | 366 browser = ProcessLaunchURLs(process_startup, urls_to_open, desktop_type); |
| 366 | 367 |
| 367 if (command_line_.HasSwitch(switches::kInstallChromeApp)) { | 368 if (command_line_.HasSwitch(switches::kInstallChromeApp)) { |
| 368 install_chrome_app::InstallChromeApp( | 369 install_chrome_app::InstallChromeApp( |
| 369 command_line_.GetSwitchValueASCII(switches::kInstallChromeApp)); | 370 command_line_.GetSwitchValueASCII(switches::kInstallChromeApp)); |
| 370 } | 371 } |
| 371 | 372 |
| 372 // If this is an app launch, but we didn't open an app window, it may | 373 // If this is an app launch, but we didn't open an app window, it may |
| 373 // be an app tab. | 374 // be an app tab. |
| 374 OpenApplicationTab(profile); | 375 OpenApplicationTab(profile); |
| 375 | 376 |
| 376 #if defined(OS_MACOSX) | 377 #if defined(OS_MACOSX) |
| 377 if (process_startup) { | 378 if (process_startup) { |
| 378 // Check whether the auto-update system needs to be promoted from user | 379 // Check whether the auto-update system needs to be promoted from user |
| 379 // to system. | 380 // to system. |
| 380 KeystoneInfoBar::PromotionInfoBar(profile); | 381 KeystoneInfoBar::PromotionInfoBar(profile); |
| 381 } | 382 } |
| 382 #endif | 383 #endif |
| 383 } | 384 } |
| 384 | 385 |
| 385 // In kiosk mode, we want to always be fullscreen, so switch to that now. | 386 // In kiosk mode, we want to always be fullscreen, so switch to that now. |
| 386 if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kKioskMode) || | 387 if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kKioskMode) || |
| 387 base::CommandLine::ForCurrentProcess()->HasSwitch( | 388 base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 388 switches::kStartFullscreen)) { | 389 switches::kStartFullscreen)) { |
| 389 // It's possible for there to be no browser window, e.g. if someone | 390 // It's possible for there to be no browser window, e.g. if someone |
| 390 // specified a non-sensical combination of options | 391 // specified a non-sensical combination of options |
| 391 // ("--kiosk --no_startup_window"); do nothing in that case. | 392 // ("--kiosk --no_startup_window"); do nothing in that case. |
| 392 Browser* browser = BrowserList::GetInstance(desktop_type)->GetLastActive(); | |
| 393 if (browser) | 393 if (browser) |
| 394 chrome::ToggleFullscreenMode(browser); | 394 chrome::ToggleFullscreenMode(browser); |
| 395 } | 395 } |
| 396 | 396 |
| 397 #if defined(OS_WIN) | 397 #if defined(OS_WIN) |
| 398 if (process_startup) | 398 if (process_startup) |
| 399 ShellIntegration::MigrateChromiumShortcuts(); | 399 ShellIntegration::MigrateChromiumShortcuts(); |
| 400 #endif // defined(OS_WIN) | 400 #endif // defined(OS_WIN) |
| 401 | 401 |
| 402 return true; | 402 return true; |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 515 | 515 |
| 516 if (out_app_contents) | 516 if (out_app_contents) |
| 517 *out_app_contents = app_tab; | 517 *out_app_contents = app_tab; |
| 518 | 518 |
| 519 return (app_tab != NULL); | 519 return (app_tab != NULL); |
| 520 } | 520 } |
| 521 } | 521 } |
| 522 return false; | 522 return false; |
| 523 } | 523 } |
| 524 | 524 |
| 525 void StartupBrowserCreatorImpl::ProcessLaunchURLs( | 525 Browser* StartupBrowserCreatorImpl::ProcessLaunchURLs( |
| 526 bool process_startup, | 526 bool process_startup, |
| 527 const std::vector<GURL>& urls_to_open, | 527 const std::vector<GURL>& urls_to_open, |
| 528 chrome::HostDesktopType desktop_type) { | 528 chrome::HostDesktopType desktop_type) { |
| 529 // If we're starting up in "background mode" (no open browser window) then | 529 // If we're starting up in "background mode" (no open browser window) then |
| 530 // don't open any browser windows, unless kAutoLaunchAtStartup is also | 530 // don't open any browser windows, unless kAutoLaunchAtStartup is also |
| 531 // specified. | 531 // specified. |
| 532 if (process_startup && | 532 if (process_startup && |
| 533 command_line_.HasSwitch(switches::kNoStartupWindow) && | 533 command_line_.HasSwitch(switches::kNoStartupWindow) && |
| 534 !command_line_.HasSwitch(switches::kAutoLaunchAtStartup)) { | 534 !command_line_.HasSwitch(switches::kAutoLaunchAtStartup)) { |
| 535 return; | 535 return nullptr; |
| 536 } | 536 } |
| 537 | 537 |
| 538 // TODO(tapted): Move this to startup_browser_creator_win.cc after refactor. | 538 // TODO(tapted): Move this to startup_browser_creator_win.cc after refactor. |
| 539 #if defined(OS_WIN) | 539 #if defined(OS_WIN) |
| 540 if (base::win::GetVersion() >= base::win::VERSION_WIN8) { | 540 if (base::win::GetVersion() >= base::win::VERSION_WIN8) { |
| 541 // See if there are apps for this profile that should be launched on startup | 541 // See if there are apps for this profile that should be launched on startup |
| 542 // due to a switch from Metro mode. | 542 // due to a switch from Metro mode. |
| 543 app_metro_launch::HandleAppLaunchForMetroRestart(profile_); | 543 app_metro_launch::HandleAppLaunchForMetroRestart(profile_); |
| 544 } | 544 } |
| 545 #endif | 545 #endif |
| 546 | 546 |
| 547 if (process_startup && ProcessStartupURLs(urls_to_open, desktop_type)) { | 547 if (process_startup) { |
| 548 // ProcessStartupURLs processed the urls, nothing else to do. | 548 Browser* browser = ProcessStartupURLs(urls_to_open, desktop_type); |
| 549 return; | 549 if (browser) { |
| 550 // ProcessStartupURLs processed the urls, nothing else to do. |
| 551 return browser; |
| 552 } |
| 550 } | 553 } |
| 551 | 554 |
| 552 chrome::startup::IsProcessStartup is_process_startup = process_startup ? | 555 chrome::startup::IsProcessStartup is_process_startup = process_startup ? |
| 553 chrome::startup::IS_PROCESS_STARTUP : | 556 chrome::startup::IS_PROCESS_STARTUP : |
| 554 chrome::startup::IS_NOT_PROCESS_STARTUP; | 557 chrome::startup::IS_NOT_PROCESS_STARTUP; |
| 555 if (!process_startup) { | 558 if (!process_startup) { |
| 556 // Even if we're not starting a new process, this may conceptually be | 559 // Even if we're not starting a new process, this may conceptually be |
| 557 // "startup" for the user and so should be handled in a similar way. Eg., | 560 // "startup" for the user and so should be handled in a similar way. Eg., |
| 558 // Chrome may have been running in the background due to an app with a | 561 // Chrome may have been running in the background due to an app with a |
| 559 // background page being installed, or running with only an app window | 562 // background page being installed, or running with only an app window |
| 560 // displayed. | 563 // displayed. |
| 561 SessionService* service = | 564 SessionService* service = |
| 562 SessionServiceFactory::GetForProfileForSessionRestore(profile_); | 565 SessionServiceFactory::GetForProfileForSessionRestore(profile_); |
| 563 if (service && service->ShouldNewWindowStartSession()) { | 566 if (service && service->ShouldNewWindowStartSession()) { |
| 564 // Restore the last session if any. | 567 // Restore the last session if any. |
| 565 if (!HasPendingUncleanExit(profile_) && | 568 if (!HasPendingUncleanExit(profile_) && |
| 566 service->RestoreIfNecessary(urls_to_open)) { | 569 service->RestoreIfNecessary(urls_to_open)) { |
| 567 return; | 570 return nullptr; |
| 568 } | 571 } |
| 569 // Open user-specified URLs like pinned tabs and startup tabs. | 572 // Open user-specified URLs like pinned tabs and startup tabs. |
| 570 Browser* browser = ProcessSpecifiedURLs(urls_to_open, desktop_type); | 573 Browser* browser = ProcessSpecifiedURLs(urls_to_open, desktop_type); |
| 571 if (browser) { | 574 if (browser) { |
| 572 AddInfoBarsIfNecessary(browser, is_process_startup); | 575 AddInfoBarsIfNecessary(browser, is_process_startup); |
| 573 return; | 576 return browser; |
| 574 } | 577 } |
| 575 } | 578 } |
| 576 } | 579 } |
| 577 | 580 |
| 578 // Session startup didn't occur, open the urls. | 581 // Session startup didn't occur, open the urls. |
| 579 Browser* browser = NULL; | 582 Browser* browser = nullptr; |
| 580 std::vector<GURL> adjust_urls = urls_to_open; | 583 std::vector<GURL> adjust_urls = urls_to_open; |
| 581 if (adjust_urls.empty()) { | 584 if (adjust_urls.empty()) { |
| 582 AddStartupURLs(&adjust_urls); | 585 AddStartupURLs(&adjust_urls); |
| 583 } else if (!command_line_.HasSwitch(switches::kOpenInNewWindow)) { | 586 } else if (!command_line_.HasSwitch(switches::kOpenInNewWindow)) { |
| 584 // Always open a list of urls in a window on the native desktop. | 587 // Always open a list of urls in a window on the native desktop. |
| 585 browser = chrome::FindTabbedBrowser(profile_, false, | 588 browser = chrome::FindTabbedBrowser(profile_, false, |
| 586 chrome::HOST_DESKTOP_TYPE_NATIVE); | 589 chrome::HOST_DESKTOP_TYPE_NATIVE); |
| 587 } | 590 } |
| 588 // This will launch a browser; prevent session restore. | 591 // This will launch a browser; prevent session restore. |
| 589 StartupBrowserCreator::in_synchronous_profile_launch_ = true; | 592 StartupBrowserCreator::in_synchronous_profile_launch_ = true; |
| 590 browser = OpenURLsInBrowser(browser, process_startup, adjust_urls, | 593 browser = OpenURLsInBrowser(browser, process_startup, adjust_urls, |
| 591 desktop_type); | 594 desktop_type); |
| 592 StartupBrowserCreator::in_synchronous_profile_launch_ = false; | 595 StartupBrowserCreator::in_synchronous_profile_launch_ = false; |
| 593 AddInfoBarsIfNecessary(browser, is_process_startup); | 596 AddInfoBarsIfNecessary(browser, is_process_startup); |
| 597 return browser; |
| 594 } | 598 } |
| 595 | 599 |
| 596 bool StartupBrowserCreatorImpl::ProcessStartupURLs( | 600 Browser* StartupBrowserCreatorImpl::ProcessStartupURLs( |
| 597 const std::vector<GURL>& urls_to_open, | 601 const std::vector<GURL>& urls_to_open, |
| 598 chrome::HostDesktopType desktop_type) { | 602 chrome::HostDesktopType desktop_type) { |
| 599 VLOG(1) << "StartupBrowserCreatorImpl::ProcessStartupURLs"; | 603 VLOG(1) << "StartupBrowserCreatorImpl::ProcessStartupURLs"; |
| 600 SessionStartupPref pref = | 604 SessionStartupPref pref = |
| 601 StartupBrowserCreator::GetSessionStartupPref(command_line_, profile_); | 605 StartupBrowserCreator::GetSessionStartupPref(command_line_, profile_); |
| 602 if (pref.type == SessionStartupPref::LAST) | 606 if (pref.type == SessionStartupPref::LAST) |
| 603 VLOG(1) << "Pref: last"; | 607 VLOG(1) << "Pref: last"; |
| 604 else if (pref.type == SessionStartupPref::URLS) | 608 else if (pref.type == SessionStartupPref::URLS) |
| 605 VLOG(1) << "Pref: urls"; | 609 VLOG(1) << "Pref: urls"; |
| 606 else if (pref.type == SessionStartupPref::DEFAULT) | 610 else if (pref.type == SessionStartupPref::DEFAULT) |
| 607 VLOG(1) << "Pref: default"; | 611 VLOG(1) << "Pref: default"; |
| 608 | 612 |
| 609 apps::AppRestoreService* restore_service = | 613 apps::AppRestoreService* restore_service = |
| 610 apps::AppRestoreServiceFactory::GetForProfile(profile_); | 614 apps::AppRestoreServiceFactory::GetForProfile(profile_); |
| 611 // NULL in incognito mode. | 615 // NULL in incognito mode. |
| 612 if (restore_service) { | 616 if (restore_service) { |
| 613 restore_service->HandleStartup(apps::AppRestoreService::ShouldRestoreApps( | 617 restore_service->HandleStartup(apps::AppRestoreService::ShouldRestoreApps( |
| 614 StartupBrowserCreator::WasRestarted())); | 618 StartupBrowserCreator::WasRestarted())); |
| 615 } | 619 } |
| 616 | 620 |
| 617 if (pref.type == SessionStartupPref::LAST) { | 621 if (pref.type == SessionStartupPref::LAST) { |
| 618 if (profile_->GetLastSessionExitType() == Profile::EXIT_CRASHED && | 622 if (profile_->GetLastSessionExitType() == Profile::EXIT_CRASHED && |
| 619 !command_line_.HasSwitch(switches::kRestoreLastSession)) { | 623 !command_line_.HasSwitch(switches::kRestoreLastSession)) { |
| 620 // The last session crashed. It's possible automatically loading the | 624 // The last session crashed. It's possible automatically loading the |
| 621 // page will trigger another crash, locking the user out of chrome. | 625 // page will trigger another crash, locking the user out of chrome. |
| 622 // To avoid this, don't restore on startup but instead show the crashed | 626 // To avoid this, don't restore on startup but instead show the crashed |
| 623 // infobar. | 627 // infobar. |
| 624 VLOG(1) << "Unclean exit; not processing"; | 628 VLOG(1) << "Unclean exit; not processing"; |
| 625 return false; | 629 return nullptr; |
| 626 } | 630 } |
| 627 | 631 |
| 628 uint32 restore_behavior = SessionRestore::SYNCHRONOUS; | 632 uint32 restore_behavior = SessionRestore::SYNCHRONOUS; |
| 629 if (browser_defaults::kAlwaysCreateTabbedBrowserOnSessionRestore || | 633 if (browser_defaults::kAlwaysCreateTabbedBrowserOnSessionRestore || |
| 630 base::CommandLine::ForCurrentProcess()->HasSwitch( | 634 base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 631 switches::kCreateBrowserOnStartupForTests)) { | 635 switches::kCreateBrowserOnStartupForTests)) { |
| 632 restore_behavior |= SessionRestore::ALWAYS_CREATE_TABBED_BROWSER; | 636 restore_behavior |= SessionRestore::ALWAYS_CREATE_TABBED_BROWSER; |
| 633 } | 637 } |
| 634 | 638 |
| 635 #if defined(OS_MACOSX) | 639 #if defined(OS_MACOSX) |
| 636 // On Mac, when restoring a session with no windows, suppress the creation | 640 // On Mac, when restoring a session with no windows, suppress the creation |
| 637 // of a new window in the case where the system is launching Chrome via a | 641 // of a new window in the case where the system is launching Chrome via a |
| 638 // login item or Lion's resume feature. | 642 // login item or Lion's resume feature. |
| 639 if (base::mac::WasLaunchedAsLoginOrResumeItem()) { | 643 if (base::mac::WasLaunchedAsLoginOrResumeItem()) { |
| 640 restore_behavior = restore_behavior & | 644 restore_behavior = restore_behavior & |
| 641 ~SessionRestore::ALWAYS_CREATE_TABBED_BROWSER; | 645 ~SessionRestore::ALWAYS_CREATE_TABBED_BROWSER; |
| 642 } | 646 } |
| 643 #endif | 647 #endif |
| 644 | 648 |
| 645 // The startup code only executes for browsers launched in desktop mode. | 649 // The startup code only executes for browsers launched in desktop mode. |
| 646 // i.e. HOST_DESKTOP_TYPE_NATIVE. Ash should never get here. | 650 // i.e. HOST_DESKTOP_TYPE_NATIVE. Ash should never get here. |
| 647 Browser* browser = SessionRestore::RestoreSession( | 651 Browser* browser = SessionRestore::RestoreSession( |
| 648 profile_, NULL, desktop_type, restore_behavior, | 652 profile_, NULL, desktop_type, restore_behavior, |
| 649 urls_to_open); | 653 urls_to_open); |
| 650 | 654 |
| 651 AddInfoBarsIfNecessary(browser, chrome::startup::IS_PROCESS_STARTUP); | 655 AddInfoBarsIfNecessary(browser, chrome::startup::IS_PROCESS_STARTUP); |
| 652 return true; | 656 return browser; |
| 653 } | 657 } |
| 654 | 658 |
| 655 Browser* browser = ProcessSpecifiedURLs(urls_to_open, desktop_type); | 659 Browser* browser = ProcessSpecifiedURLs(urls_to_open, desktop_type); |
| 656 if (!browser) | 660 if (!browser) |
| 657 return false; | 661 return nullptr; |
| 658 | 662 |
| 659 AddInfoBarsIfNecessary(browser, chrome::startup::IS_PROCESS_STARTUP); | 663 AddInfoBarsIfNecessary(browser, chrome::startup::IS_PROCESS_STARTUP); |
| 660 | 664 |
| 661 // Session restore may occur if the startup preference is "last" or if the | 665 // Session restore may occur if the startup preference is "last" or if the |
| 662 // crash infobar is displayed. Otherwise, it's safe for the DOM storage system | 666 // crash infobar is displayed. Otherwise, it's safe for the DOM storage system |
| 663 // to start deleting leftover data. | 667 // to start deleting leftover data. |
| 664 if (pref.type != SessionStartupPref::LAST && | 668 if (pref.type != SessionStartupPref::LAST && |
| 665 !HasPendingUncleanExit(profile_)) { | 669 !HasPendingUncleanExit(profile_)) { |
| 666 content::BrowserContext::GetDefaultStoragePartition(profile_)-> | 670 content::BrowserContext::GetDefaultStoragePartition(profile_)-> |
| 667 GetDOMStorageContext()->StartScavengingUnusedSessionStorage(); | 671 GetDOMStorageContext()->StartScavengingUnusedSessionStorage(); |
| 668 } | 672 } |
| 669 | 673 |
| 670 return true; | 674 return browser; |
| 671 } | 675 } |
| 672 | 676 |
| 673 Browser* StartupBrowserCreatorImpl::ProcessSpecifiedURLs( | 677 Browser* StartupBrowserCreatorImpl::ProcessSpecifiedURLs( |
| 674 const std::vector<GURL>& urls_to_open, | 678 const std::vector<GURL>& urls_to_open, |
| 675 chrome::HostDesktopType desktop_type) { | 679 chrome::HostDesktopType desktop_type) { |
| 676 SessionStartupPref pref = | 680 SessionStartupPref pref = |
| 677 StartupBrowserCreator::GetSessionStartupPref(command_line_, profile_); | 681 StartupBrowserCreator::GetSessionStartupPref(command_line_, profile_); |
| 678 StartupTabs tabs; | 682 StartupTabs tabs; |
| 679 // Pinned tabs should not be displayed when chrome is launched in incognito | 683 // Pinned tabs should not be displayed when chrome is launched in incognito |
| 680 // mode. Also, no pages should be opened automatically if the session | 684 // mode. Also, no pages should be opened automatically if the session |
| (...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 911 // behavior is desired because completing or skipping the sync promo | 915 // behavior is desired because completing or skipping the sync promo |
| 912 // causes a redirect to the NTP. | 916 // causes a redirect to the NTP. |
| 913 if (!startup_urls->empty() && | 917 if (!startup_urls->empty() && |
| 914 startup_urls->at(0) == GURL(chrome::kChromeUINewTabURL)) | 918 startup_urls->at(0) == GURL(chrome::kChromeUINewTabURL)) |
| 915 startup_urls->at(0) = sync_promo_url; | 919 startup_urls->at(0) = sync_promo_url; |
| 916 else | 920 else |
| 917 startup_urls->insert(startup_urls->begin(), sync_promo_url); | 921 startup_urls->insert(startup_urls->begin(), sync_promo_url); |
| 918 } | 922 } |
| 919 } | 923 } |
| 920 } | 924 } |
| OLD | NEW |