This source file includes following definitions.
- activated_profile_
- Observe
- HasBeenLaunched
- AddLaunched
- Clear
- activated_profile
- set_profile_to_activate
- MaybeActivateProfile
- ActivateProfile
- DumpBrowserHistograms
- show_main_browser_window_
- AddFirstRunTab
- InSynchronousProfileLaunch
- LaunchBrowser
- WasRestarted
- GetSessionStartupPref
- ClearLaunchedProfilesForTesting
- GetURLsFromCommandLine
- ProcessCmdLineImpl
- ProcessCommandLineOnProfileCreated
- ProcessCommandLineAlreadyRunning
- ActivatedProfile
- HasPendingUncleanExit
- GetStartupProfilePath
#include "chrome/browser/ui/startup/startup_browser_creator.h"
#include <algorithm>
#include <set>
#include "apps/app_load_service.h"
#include "apps/switches.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/command_line.h"
#include "base/compiler_specific.h"
#include "base/environment.h"
#include "base/file_util.h"
#include "base/files/file_path.h"
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/metrics/histogram.h"
#include "base/metrics/statistics_recorder.h"
#include "base/path_service.h"
#include "base/prefs/pref_service.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread_restrictions.h"
#include "chrome/browser/app_mode/app_mode_utils.h"
#include "chrome/browser/auto_launch_trial.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/custom_handlers/protocol_handler_registry.h"
#include "chrome/browser/extensions/startup_helper.h"
#include "chrome/browser/extensions/unpacked_installer.h"
#include "chrome/browser/first_run/first_run.h"
#include "chrome/browser/google/google_util.h"
#include "chrome/browser/notifications/desktop_notification_service.h"
#include "chrome/browser/prefs/incognito_mode_prefs.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/profiles/profiles_state.h"
#include "chrome/browser/search_engines/util.h"
#include "chrome/browser/ui/app_list/app_list_service.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_dialogs.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/startup/startup_browser_creator_impl.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_result_codes.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/chrome_version_info.h"
#include "chrome/common/net/url_fixer_upper.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/profile_management_switches.h"
#include "chrome/common/url_constants.h"
#include "chrome/installer/util/browser_distribution.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/child_process_security_policy.h"
#include "content/public/browser/navigation_controller.h"
#include "grit/locale_settings.h"
#include "net/base/net_util.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#if defined(USE_ASH)
#include "ash/shell.h"
#endif
#if defined(OS_CHROMEOS)
#include "chrome/browser/chromeos/app_mode/app_launch_utils.h"
#include "chrome/browser/chromeos/kiosk_mode/kiosk_mode_settings.h"
#include "chrome/browser/chromeos/login/demo_mode/demo_app_launcher.h"
#include "chrome/browser/chromeos/login/user_manager.h"
#include "chrome/browser/chromeos/profiles/profile_helper.h"
#include "chrome/browser/lifetime/application_lifetime.h"
#include "chromeos/chromeos_switches.h"
#endif
#if defined(TOOLKIT_VIEWS) && defined(OS_LINUX)
#include "ui/events/x/touch_factory_x11.h"
#endif
#if defined(OS_WIN)
#include "chrome/browser/ui/startup/startup_browser_creator_win.h"
#endif
#if defined(ENABLE_FULL_PRINTING)
#include "chrome/browser/printing/cloud_print/cloud_print_proxy_service.h"
#include "chrome/browser/printing/cloud_print/cloud_print_proxy_service_factory.h"
#include "chrome/browser/printing/print_dialog_cloud.h"
#endif
using content::BrowserThread;
using content::ChildProcessSecurityPolicy;
namespace {
class ProfileLaunchObserver : public content::NotificationObserver {
public:
ProfileLaunchObserver()
: profile_to_activate_(NULL),
activated_profile_(false) {
registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED,
content::NotificationService::AllSources());
registrar_.Add(this, chrome::NOTIFICATION_BROWSER_WINDOW_READY,
content::NotificationService::AllSources());
}
virtual ~ProfileLaunchObserver() {}
virtual void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) OVERRIDE {
switch (type) {
case chrome::NOTIFICATION_PROFILE_DESTROYED: {
Profile* profile = content::Source<Profile>(source).ptr();
launched_profiles_.erase(profile);
opened_profiles_.erase(profile);
if (profile == profile_to_activate_)
profile_to_activate_ = NULL;
MaybeActivateProfile();
break;
}
case chrome::NOTIFICATION_BROWSER_WINDOW_READY: {
Browser* browser = content::Source<Browser>(source).ptr();
DCHECK(browser);
opened_profiles_.insert(browser->profile());
MaybeActivateProfile();
break;
}
default:
NOTREACHED();
}
}
bool HasBeenLaunched(const Profile* profile) const {
return launched_profiles_.find(profile) != launched_profiles_.end();
}
void AddLaunched(Profile* profile) {
launched_profiles_.insert(profile);
if (chrome::FindBrowserWithProfile(profile,
chrome::HOST_DESKTOP_TYPE_NATIVE)) {
opened_profiles_.insert(profile);
}
}
void Clear() {
launched_profiles_.clear();
opened_profiles_.clear();
}
bool activated_profile() { return activated_profile_; }
void set_profile_to_activate(Profile* profile) {
profile_to_activate_ = profile;
MaybeActivateProfile();
}
private:
void MaybeActivateProfile() {
if (!profile_to_activate_)
return;
std::set<const Profile*>::const_iterator i = launched_profiles_.begin();
for (; i != launched_profiles_.end(); ++i) {
if (opened_profiles_.find(*i) == opened_profiles_.end())
return;
}
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(&ProfileLaunchObserver::ActivateProfile,
base::Unretained(this)));
registrar_.Remove(this, chrome::NOTIFICATION_BROWSER_WINDOW_READY,
content::NotificationService::AllSources());
registrar_.Remove(this, chrome::NOTIFICATION_PROFILE_DESTROYED,
content::NotificationService::AllSources());
}
void ActivateProfile() {
if (profile_to_activate_) {
Browser* browser = chrome::FindBrowserWithProfile(
profile_to_activate_, chrome::HOST_DESKTOP_TYPE_NATIVE);
if (browser)
browser->window()->Activate();
profile_to_activate_ = NULL;
}
activated_profile_ = true;
}
std::set<const Profile*> launched_profiles_;
std::set<const Profile*> opened_profiles_;
content::NotificationRegistrar registrar_;
Profile* profile_to_activate_;
bool activated_profile_;
DISALLOW_COPY_AND_ASSIGN(ProfileLaunchObserver);
};
base::LazyInstance<ProfileLaunchObserver> profile_launch_observer =
LAZY_INSTANCE_INITIALIZER;
void DumpBrowserHistograms(const base::FilePath& output_file) {
base::ThreadRestrictions::AssertIOAllowed();
std::string output_string(base::StatisticsRecorder::ToJSON(std::string()));
base::WriteFile(output_file, output_string.data(),
static_cast<int>(output_string.size()));
}
}
StartupBrowserCreator::StartupBrowserCreator()
: is_default_browser_dialog_suppressed_(false),
show_main_browser_window_(true) {
}
StartupBrowserCreator::~StartupBrowserCreator() {}
bool StartupBrowserCreator::was_restarted_read_ = false;
bool StartupBrowserCreator::in_synchronous_profile_launch_ = false;
void StartupBrowserCreator::AddFirstRunTab(const GURL& url) {
first_run_tabs_.push_back(url);
}
bool StartupBrowserCreator::InSynchronousProfileLaunch() {
return in_synchronous_profile_launch_;
}
bool StartupBrowserCreator::LaunchBrowser(
const CommandLine& command_line,
Profile* profile,
const base::FilePath& cur_dir,
chrome::startup::IsProcessStartup process_startup,
chrome::startup::IsFirstRun is_first_run,
int* return_code) {
in_synchronous_profile_launch_ =
process_startup == chrome::startup::IS_PROCESS_STARTUP;
DCHECK(profile);
if (IncognitoModePrefs::ShouldLaunchIncognito(command_line,
profile->GetPrefs())) {
profile = profile->GetOffTheRecordProfile();
} else if (command_line.HasSwitch(switches::kIncognito)) {
LOG(WARNING) << "Incognito mode disabled by policy, launching a normal "
<< "browser session.";
}
const bool silent_launch = command_line.HasSwitch(switches::kSilentLaunch);
if (!silent_launch) {
StartupBrowserCreatorImpl lwp(cur_dir, command_line, this, is_first_run);
const std::vector<GURL> urls_to_launch =
GetURLsFromCommandLine(command_line, cur_dir, profile);
chrome::HostDesktopType host_desktop_type =
chrome::HOST_DESKTOP_TYPE_NATIVE;
#if defined(OS_WIN) && defined(USE_ASH)
if (ash::Shell::HasInstance())
host_desktop_type = chrome::HOST_DESKTOP_TYPE_ASH;
#endif
const bool launched = lwp.Launch(profile, urls_to_launch,
in_synchronous_profile_launch_,
host_desktop_type);
in_synchronous_profile_launch_ = false;
if (!launched) {
LOG(ERROR) << "launch error";
if (return_code)
*return_code = chrome::RESULT_CODE_INVALID_CMDLINE_URL;
return false;
}
} else {
in_synchronous_profile_launch_ = false;
}
profile_launch_observer.Get().AddLaunched(profile);
#if defined(OS_CHROMEOS)
g_browser_process->platform_part()->profile_helper()->ProfileStartup(
profile,
process_startup);
#endif
return true;
}
bool StartupBrowserCreator::WasRestarted() {
static bool was_restarted = false;
if (!was_restarted_read_) {
PrefService* pref_service = g_browser_process->local_state();
was_restarted = pref_service->GetBoolean(prefs::kWasRestarted);
pref_service->SetBoolean(prefs::kWasRestarted, false);
was_restarted_read_ = true;
}
return was_restarted;
}
SessionStartupPref StartupBrowserCreator::GetSessionStartupPref(
const CommandLine& command_line,
Profile* profile) {
DCHECK(profile);
PrefService* prefs = profile->GetPrefs();
SessionStartupPref pref = SessionStartupPref::GetStartupPref(prefs);
#if defined(OS_CHROMEOS)
const bool is_first_run = chromeos::UserManager::Get()->IsCurrentUserNew();
#else
const bool is_first_run = first_run::IsChromeFirstRun();
#endif
if (is_first_run && SessionStartupPref::TypeIsDefault(prefs))
pref.type = SessionStartupPref::DEFAULT;
if ((command_line.HasSwitch(switches::kRestoreLastSession) ||
StartupBrowserCreator::WasRestarted()) &&
!profile->IsNewProfile()) {
pref.type = SessionStartupPref::LAST;
}
if (pref.type == SessionStartupPref::LAST &&
IncognitoModePrefs::ShouldLaunchIncognito(command_line, prefs)) {
pref.type = SessionStartupPref::DEFAULT;
}
return pref;
}
void StartupBrowserCreator::ClearLaunchedProfilesForTesting() {
profile_launch_observer.Get().Clear();
}
std::vector<GURL> StartupBrowserCreator::GetURLsFromCommandLine(
const CommandLine& command_line,
const base::FilePath& cur_dir,
Profile* profile) {
std::vector<GURL> urls;
const CommandLine::StringVector& params = command_line.GetArgs();
for (size_t i = 0; i < params.size(); ++i) {
base::FilePath param = base::FilePath(params[i]);
if ((param.value().size() > 2) && (param.value()[0] == '?') &&
(param.value()[1] == ' ')) {
GURL url(GetDefaultSearchURLForSearchTerms(
profile, param.LossyDisplayName().substr(2)));
if (url.is_valid()) {
urls.push_back(url);
continue;
}
}
GURL url;
{
base::ThreadRestrictions::ScopedAllowIO allow_io;
url = URLFixerUpper::FixupRelativeFile(cur_dir, param);
}
if (url.is_valid()) {
ChildProcessSecurityPolicy* policy =
ChildProcessSecurityPolicy::GetInstance();
if (policy->IsWebSafeScheme(url.scheme()) ||
url.SchemeIs(content::kFileScheme) ||
#if defined(OS_CHROMEOS)
(url.spec().find(chrome::kChromeUISettingsURL) == 0) ||
#endif
(url.spec().compare(content::kAboutBlankURL) == 0)) {
urls.push_back(url);
}
}
}
#if defined(OS_WIN)
if (urls.empty()) {
GURL url(chrome::GetURLToOpen(profile));
if (url.is_valid())
urls.push_back(url);
}
#endif
return urls;
}
bool StartupBrowserCreator::ProcessCmdLineImpl(
const CommandLine& command_line,
const base::FilePath& cur_dir,
bool process_startup,
Profile* last_used_profile,
const Profiles& last_opened_profiles,
int* return_code,
StartupBrowserCreator* browser_creator) {
DCHECK(last_used_profile);
if (process_startup) {
if (command_line.HasSwitch(switches::kDisablePromptOnRepost))
content::NavigationController::DisablePromptOnRepost();
}
bool silent_launch = false;
#if defined(ENABLE_FULL_PRINTING)
if (command_line.HasSwitch(switches::kCloudPrintFile) &&
print_dialog_cloud::CreatePrintDialogFromCommandLine(last_used_profile,
command_line)) {
silent_launch = true;
}
if (command_line.HasSwitch(switches::kCheckCloudPrintConnectorPolicy)) {
silent_launch = true;
if (CloudPrintProxyServiceFactory::GetForProfile(last_used_profile)->
EnforceCloudPrintConnectorPolicyAndQuit())
return false;
}
#endif
if (command_line.HasSwitch(switches::kExplicitlyAllowedPorts)) {
std::string allowed_ports =
command_line.GetSwitchValueASCII(switches::kExplicitlyAllowedPorts);
net::SetExplicitlyAllowedPorts(allowed_ports);
}
if (command_line.HasSwitch(switches::kInstallFromWebstore)) {
extensions::StartupHelper helper;
helper.InstallFromWebstore(command_line, last_used_profile);
return false;
}
if (command_line.HasSwitch(switches::kValidateCrx)) {
if (!process_startup) {
LOG(ERROR) << "chrome is already running; you must close all running "
<< "instances before running with the --"
<< switches::kValidateCrx << " flag";
return false;
}
extensions::StartupHelper helper;
std::string message;
std::string error;
if (helper.ValidateCrx(command_line, &error))
message = std::string("ValidateCrx Success");
else
message = std::string("ValidateCrx Failure: ") + error;
printf("%s\n", message.c_str());
return false;
}
if (command_line.HasSwitch(switches::kLimitedInstallFromWebstore)) {
extensions::StartupHelper helper;
helper.LimitedInstallFromWebstore(command_line, last_used_profile,
base::Bind(&base::DoNothing));
}
#if defined(OS_CHROMEOS)
if (command_line.HasSwitch(chromeos::switches::kLoginManager) ||
command_line.HasSwitch(chromeos::switches::kLoginPassword)) {
silent_launch = true;
}
if (chrome::IsRunningInAppMode() &&
command_line.HasSwitch(switches::kAppId)) {
chromeos::LaunchAppOrDie(
last_used_profile,
command_line.GetSwitchValueASCII(switches::kAppId));
silent_launch = true;
}
if (chromeos::DemoAppLauncher::IsDemoAppSession(
command_line.GetSwitchValueASCII(chromeos::switches::kLoginUser))) {
chrome::AttemptUserExit();
return false;
}
#endif
#if defined(TOOLKIT_VIEWS) && defined(USE_X11)
ui::TouchFactory::SetTouchDeviceListFromCommandLine();
#endif
if (!process_startup &&
command_line.HasSwitch(switches::kDumpBrowserHistograms)) {
base::FilePath output_file(
command_line.GetSwitchValuePath(switches::kDumpBrowserHistograms));
if (!output_file.empty()) {
BrowserThread::PostBlockingPoolTask(
FROM_HERE,
base::Bind(&DumpBrowserHistograms, output_file));
}
silent_launch = true;
}
if (silent_launch)
return true;
if (command_line.HasSwitch(apps::kLoadAndLaunchApp) &&
!IncognitoModePrefs::ShouldLaunchIncognito(
command_line, last_used_profile->GetPrefs())) {
CommandLine::StringType path = command_line.GetSwitchValueNative(
apps::kLoadAndLaunchApp);
if (!apps::AppLoadService::Get(last_used_profile)->LoadAndLaunch(
base::FilePath(path), command_line, cur_dir)) {
return false;
}
if (chrome::GetTotalBrowserCountForProfile(last_used_profile) != 0)
return true;
}
chrome::startup::IsProcessStartup is_process_startup = process_startup ?
chrome::startup::IS_PROCESS_STARTUP :
chrome::startup::IS_NOT_PROCESS_STARTUP;
chrome::startup::IsFirstRun is_first_run = first_run::IsChromeFirstRun() ?
chrome::startup::IS_FIRST_RUN : chrome::startup::IS_NOT_FIRST_RUN;
if (last_opened_profiles.empty()) {
if (switches::IsNewProfileManagement()) {
ProfileInfoCache& profile_info =
g_browser_process->profile_manager()->GetProfileInfoCache();
size_t profile_index = profile_info.GetIndexOfProfileWithPath(
last_used_profile->GetPath());
bool signin_required = profile_index != std::string::npos &&
profile_info.ProfileIsSigninRequiredAtIndex(profile_index);
if (signin_required || last_used_profile->IsGuestSession()) {
chrome::ShowUserManager(base::FilePath());
return true;
}
}
if (!browser_creator->LaunchBrowser(command_line, last_used_profile,
cur_dir, is_process_startup,
is_first_run, return_code)) {
return false;
}
} else {
CommandLine command_line_without_urls(command_line.GetProgram());
const CommandLine::SwitchMap& switches = command_line.GetSwitches();
for (CommandLine::SwitchMap::const_iterator switch_it = switches.begin();
switch_it != switches.end(); ++switch_it) {
command_line_without_urls.AppendSwitchNative(switch_it->first,
switch_it->second);
}
for (Profiles::const_iterator it = last_opened_profiles.begin();
it != last_opened_profiles.end(); ++it) {
SessionStartupPref startup_pref =
GetSessionStartupPref(command_line, *it);
if (*it != last_used_profile &&
startup_pref.type == SessionStartupPref::DEFAULT &&
!HasPendingUncleanExit(*it))
continue;
if (switches::IsNewProfileManagement() &&
(*it)->IsGuestSession())
continue;
if (!browser_creator->LaunchBrowser((*it == last_used_profile) ?
command_line : command_line_without_urls, *it, cur_dir,
is_process_startup, is_first_run, return_code))
return false;
is_process_startup = chrome::startup::IS_NOT_PROCESS_STARTUP;
}
if (!switches::IsNewProfileManagement() &&
!last_used_profile->IsGuestSession())
profile_launch_observer.Get().set_profile_to_activate(last_used_profile);
}
return true;
}
void StartupBrowserCreator::ProcessCommandLineOnProfileCreated(
const CommandLine& command_line,
const base::FilePath& cur_dir,
Profile* profile,
Profile::CreateStatus status) {
if (status == Profile::CREATE_STATUS_INITIALIZED)
ProcessCmdLineImpl(command_line, cur_dir, false, profile, Profiles(), NULL,
NULL);
}
void StartupBrowserCreator::ProcessCommandLineAlreadyRunning(
const CommandLine& command_line,
const base::FilePath& cur_dir,
const base::FilePath& profile_path) {
ProfileManager* profile_manager = g_browser_process->profile_manager();
Profile* profile = profile_manager->GetProfileByPath(profile_path);
if (!profile) {
profile_manager->CreateProfileAsync(profile_path,
base::Bind(&StartupBrowserCreator::ProcessCommandLineOnProfileCreated,
command_line, cur_dir), base::string16(), base::string16(),
std::string());
return;
}
ProcessCmdLineImpl(command_line, cur_dir, false, profile, Profiles(), NULL,
NULL);
}
bool StartupBrowserCreator::ActivatedProfile() {
return profile_launch_observer.Get().activated_profile();
}
bool HasPendingUncleanExit(Profile* profile) {
return profile->GetLastSessionExitType() == Profile::EXIT_CRASHED &&
!profile_launch_observer.Get().HasBeenLaunched(profile);
}
base::FilePath GetStartupProfilePath(const base::FilePath& user_data_dir,
const CommandLine& command_line) {
if (command_line.HasSwitch(switches::kShowAppList)) {
return AppListService::Get(chrome::HOST_DESKTOP_TYPE_NATIVE)->
GetProfilePath(user_data_dir);
}
if (command_line.HasSwitch(switches::kProfileDirectory)) {
return user_data_dir.Append(
command_line.GetSwitchValuePath(switches::kProfileDirectory));
}
return g_browser_process->profile_manager()->GetLastUsedProfileDir(
user_data_dir);
}