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
3 // found in the LICENSE file.
5 #include "chrome/browser/first_run/first_run.h"
9 #include "base/command_line.h"
10 #include "base/compiler_specific.h"
11 #include "base/file_util.h"
12 #include "base/files/file_path.h"
13 #include "base/lazy_instance.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/metrics/histogram.h"
16 #include "base/path_service.h"
17 #include "base/prefs/pref_service.h"
18 #include "base/run_loop.h"
19 #include "base/strings/stringprintf.h"
20 #include "base/strings/utf_string_conversions.h"
21 #include "build/build_config.h"
22 #include "chrome/browser/browser_process.h"
23 #include "chrome/browser/chrome_notification_types.h"
24 #include "chrome/browser/extensions/extension_service.h"
25 #include "chrome/browser/extensions/updater/extension_updater.h"
26 #include "chrome/browser/first_run/first_run_internal.h"
27 #include "chrome/browser/google/google_util.h"
28 #include "chrome/browser/importer/external_process_importer_host.h"
29 #include "chrome/browser/importer/importer_list.h"
30 #include "chrome/browser/importer/importer_progress_observer.h"
31 #include "chrome/browser/importer/importer_uma.h"
32 #include "chrome/browser/importer/profile_writer.h"
33 #include "chrome/browser/prefs/chrome_pref_service_factory.h"
34 #include "chrome/browser/profiles/profile.h"
35 #include "chrome/browser/profiles/profiles_state.h"
36 #include "chrome/browser/search_engines/template_url_service.h"
37 #include "chrome/browser/search_engines/template_url_service_factory.h"
38 #include "chrome/browser/shell_integration.h"
39 #include "chrome/browser/signin/signin_manager.h"
40 #include "chrome/browser/signin/signin_manager_factory.h"
41 #include "chrome/browser/signin/signin_promo.h"
42 #include "chrome/browser/signin/signin_tracker.h"
43 #include "chrome/browser/ui/browser.h"
44 #include "chrome/browser/ui/browser_finder.h"
45 #include "chrome/browser/ui/chrome_pages.h"
46 #include "chrome/browser/ui/global_error/global_error_service.h"
47 #include "chrome/browser/ui/global_error/global_error_service_factory.h"
48 #include "chrome/browser/ui/tabs/tab_strip_model.h"
49 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h"
50 #include "chrome/common/chrome_constants.h"
51 #include "chrome/common/chrome_paths.h"
52 #include "chrome/common/chrome_switches.h"
53 #include "chrome/common/pref_names.h"
54 #include "chrome/common/url_constants.h"
55 #include "chrome/installer/util/master_preferences.h"
56 #include "chrome/installer/util/master_preferences_constants.h"
57 #include "chrome/installer/util/util_constants.h"
58 #include "components/user_prefs/pref_registry_syncable.h"
59 #include "content/public/browser/notification_observer.h"
60 #include "content/public/browser/notification_registrar.h"
61 #include "content/public/browser/notification_service.h"
62 #include "content/public/browser/notification_types.h"
63 #include "content/public/browser/user_metrics.h"
64 #include "content/public/browser/web_contents.h"
65 #include "google_apis/gaia/gaia_auth_util.h"
68 using base::UserMetricsAction;
72 // A bitfield formed from values in AutoImportState to record the state of
73 // AutoImport. This is used in testing to verify import startup actions that
74 // occur before an observer can be registered in the test.
75 uint16 g_auto_import_state = first_run::AUTO_IMPORT_NONE;
77 // Flags for functions of similar name.
78 bool g_should_show_welcome_page = false;
79 bool g_should_do_autofill_personal_data_manager_first_run = false;
81 // This class acts as an observer for the ImporterProgressObserver::ImportEnded
82 // callback. When the import process is started, certain errors may cause
83 // ImportEnded() to be called synchronously, but the typical case is that
84 // ImportEnded() is called asynchronously. Thus we have to handle both cases.
85 class ImportEndedObserver : public importer::ImporterProgressObserver {
87 ImportEndedObserver() : ended_(false) {}
88 virtual ~ImportEndedObserver() {}
90 // importer::ImporterProgressObserver:
91 virtual void ImportStarted() OVERRIDE {}
92 virtual void ImportItemStarted(importer::ImportItem item) OVERRIDE {}
93 virtual void ImportItemEnded(importer::ImportItem item) OVERRIDE {}
94 virtual void ImportEnded() OVERRIDE {
96 if (!callback_for_import_end_.is_null())
97 callback_for_import_end_.Run();
100 void set_callback_for_import_end(const base::Closure& callback) {
101 callback_for_import_end_ = callback;
109 // Set if the import has ended.
112 base::Closure callback_for_import_end_;
114 DISALLOW_COPY_AND_ASSIGN(ImportEndedObserver);
117 // Helper class that performs delayed first-run tasks that need more of the
118 // chrome infrastructure to be up and running before they can be attempted.
119 class FirstRunDelayedTasks : public content::NotificationObserver {
126 explicit FirstRunDelayedTasks(Tasks task) {
127 if (task == INSTALL_EXTENSIONS) {
128 registrar_.Add(this, chrome::NOTIFICATION_EXTENSIONS_READY,
129 content::NotificationService::AllSources());
131 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_CLOSED,
132 content::NotificationService::AllSources());
135 virtual void Observe(int type,
136 const content::NotificationSource& source,
137 const content::NotificationDetails& details) OVERRIDE {
138 // After processing the notification we always delete ourselves.
139 if (type == chrome::NOTIFICATION_EXTENSIONS_READY) {
141 content::Source<Profile>(source).ptr()->GetExtensionService());
147 // Private ctor forces it to be created only in the heap.
148 virtual ~FirstRunDelayedTasks() {}
150 // The extension work is to basically trigger an extension update check.
151 // If the extension specified in the master pref is older than the live
152 // extension it will get updated which is the same as get it installed.
153 void DoExtensionWork(ExtensionService* service) {
155 service->updater()->CheckNow(extensions::ExtensionUpdater::CheckParams());
158 content::NotificationRegistrar registrar_;
161 // Installs a task to do an extensions update check once the extensions system
163 void DoDelayedInstallExtensions() {
164 new FirstRunDelayedTasks(FirstRunDelayedTasks::INSTALL_EXTENSIONS);
167 void DoDelayedInstallExtensionsIfNeeded(
168 installer::MasterPreferences* install_prefs) {
169 base::DictionaryValue* extensions = 0;
170 if (install_prefs->GetExtensionsBlock(&extensions)) {
171 VLOG(1) << "Extensions block found in master preferences";
172 DoDelayedInstallExtensions();
176 // Sets the |items| bitfield according to whether the import data specified by
177 // |import_type| should be be auto imported or not.
178 void SetImportItem(PrefService* user_prefs,
179 const char* pref_path,
181 int dont_import_items,
182 importer::ImportItem import_type,
184 // Work out whether an item is to be imported according to what is specified
185 // in master preferences.
186 bool should_import = false;
187 bool master_pref_set =
188 ((import_items | dont_import_items) & import_type) != 0;
189 bool master_pref = ((import_items & ~dont_import_items) & import_type) != 0;
191 if (import_type == importer::HISTORY ||
192 (import_type != importer::FAVORITES &&
193 first_run::internal::IsOrganicFirstRun())) {
194 // History is always imported unless turned off in master_preferences.
195 // Search engines and home page are imported in organic builds only
196 // unless turned off in master_preferences.
197 should_import = !master_pref_set || master_pref;
199 // Bookmarks are never imported, unless turned on in master_preferences.
200 // Search engine and home page import behaviour is similar in non organic
202 should_import = master_pref_set && master_pref;
205 // If an import policy is set, import items according to policy. If no master
206 // preference is set, but a corresponding recommended policy is set, import
207 // item according to recommended policy. If both a master preference and a
208 // recommended policy is set, the master preference wins. If neither
209 // recommended nor managed policies are set, import item according to what we
212 user_prefs->SetBoolean(pref_path, should_import);
214 if (!user_prefs->FindPreference(pref_path)->IsDefaultValue()) {
215 if (user_prefs->GetBoolean(pref_path))
216 *items |= import_type;
217 } else { // no policy (recommended or managed) is set
219 *items |= import_type;
222 user_prefs->ClearPref(pref_path);
225 // Launches the import, via |importer_host|, from |source_profile| into
226 // |target_profile| for the items specified in the |items_to_import| bitfield.
227 // This may be done in a separate process depending on the platform, but it will
228 // always block until done.
229 void ImportFromSourceProfile(ExternalProcessImporterHost* importer_host,
230 const importer::SourceProfile& source_profile,
231 Profile* target_profile,
232 uint16 items_to_import) {
233 ImportEndedObserver observer;
234 importer_host->set_observer(&observer);
235 importer_host->StartImportSettings(source_profile,
238 new ProfileWriter(target_profile));
239 // If the import process has not errored out, block on it.
240 if (!observer.ended()) {
242 observer.set_callback_for_import_end(loop.QuitClosure());
244 observer.set_callback_for_import_end(base::Closure());
248 // Imports bookmarks from an html file whose path is provided by
249 // |import_bookmarks_path|.
250 void ImportFromFile(Profile* profile,
251 ExternalProcessImporterHost* file_importer_host,
252 const std::string& import_bookmarks_path) {
253 importer::SourceProfile source_profile;
254 source_profile.importer_type = importer::TYPE_BOOKMARKS_FILE;
256 const base::FilePath::StringType& import_bookmarks_path_str =
258 base::UTF8ToUTF16(import_bookmarks_path);
260 import_bookmarks_path;
262 source_profile.source_path = base::FilePath(import_bookmarks_path_str);
264 ImportFromSourceProfile(file_importer_host, source_profile, profile,
265 importer::FAVORITES);
266 g_auto_import_state |= first_run::AUTO_IMPORT_BOOKMARKS_FILE_IMPORTED;
269 // Imports settings from the first profile in |importer_list|.
270 void ImportSettings(Profile* profile,
271 ExternalProcessImporterHost* importer_host,
272 scoped_refptr<ImporterList> importer_list,
273 int items_to_import) {
274 const importer::SourceProfile& source_profile =
275 importer_list->GetSourceProfileAt(0);
277 // Ensure that importers aren't requested to import items that they do not
278 // support. If there is no overlap, skip.
279 items_to_import &= source_profile.services_supported;
280 if (items_to_import == 0)
283 ImportFromSourceProfile(importer_host, source_profile, profile,
285 g_auto_import_state |= first_run::AUTO_IMPORT_PROFILE_IMPORTED;
288 GURL UrlFromString(const std::string& in) {
292 void ConvertStringVectorToGURLVector(
293 const std::vector<std::string>& src,
294 std::vector<GURL>* ret) {
295 ret->resize(src.size());
296 std::transform(src.begin(), src.end(), ret->begin(), &UrlFromString);
299 // Show the first run search engine bubble at the first appropriate opportunity.
300 // This bubble may be delayed by other UI, like global errors and sync promos.
301 class FirstRunBubbleLauncher : public content::NotificationObserver {
303 // Show the bubble at the first appropriate opportunity. This function
304 // instantiates a FirstRunBubbleLauncher, which manages its own lifetime.
305 static void ShowFirstRunBubbleSoon();
308 FirstRunBubbleLauncher();
309 virtual ~FirstRunBubbleLauncher();
311 // content::NotificationObserver:
312 virtual void Observe(int type,
313 const content::NotificationSource& source,
314 const content::NotificationDetails& details) OVERRIDE;
316 content::NotificationRegistrar registrar_;
318 DISALLOW_COPY_AND_ASSIGN(FirstRunBubbleLauncher);
322 void FirstRunBubbleLauncher::ShowFirstRunBubbleSoon() {
323 SetShowFirstRunBubblePref(first_run::FIRST_RUN_BUBBLE_SHOW);
324 // This FirstRunBubbleLauncher instance will manage its own lifetime.
325 new FirstRunBubbleLauncher();
328 FirstRunBubbleLauncher::FirstRunBubbleLauncher() {
329 registrar_.Add(this, content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
330 content::NotificationService::AllSources());
332 // This notification is required to observe the switch between the sync setup
333 // page and the general settings page.
334 registrar_.Add(this, chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED,
335 content::NotificationService::AllSources());
338 FirstRunBubbleLauncher::~FirstRunBubbleLauncher() {}
340 void FirstRunBubbleLauncher::Observe(
342 const content::NotificationSource& source,
343 const content::NotificationDetails& details) {
344 DCHECK(type == content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME ||
345 type == chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED);
347 Browser* browser = chrome::FindBrowserWithWebContents(
348 content::Source<content::WebContents>(source).ptr());
349 if (!browser || !browser->is_type_tabbed())
352 // Check the preference to determine if the bubble should be shown.
353 PrefService* prefs = g_browser_process->local_state();
354 if (!prefs || prefs->GetInteger(prefs::kShowFirstRunBubbleOption) !=
355 first_run::FIRST_RUN_BUBBLE_SHOW) {
360 content::WebContents* contents =
361 browser->tab_strip_model()->GetActiveWebContents();
363 // Suppress the first run bubble if a Gaia sign in page, the continue
364 // URL for the sign in page or the sync setup page is showing.
366 (contents->GetURL().GetOrigin().spec() ==
367 chrome::kChromeUIChromeSigninURL ||
368 gaia::IsGaiaSignonRealm(contents->GetURL().GetOrigin()) ||
369 signin::IsContinueUrlForWebBasedSigninFlow(contents->GetURL()) ||
370 (contents->GetURL() ==
371 chrome::GetSettingsUrl(chrome::kSyncSetupSubPage)))) {
375 if (contents && contents->GetURL().SchemeIs(content::kChromeUIScheme)) {
376 // Suppress the first run bubble if 'make chrome metro' flow is showing.
377 if (contents->GetURL().host() == chrome::kChromeUIMetroFlowHost)
380 // Suppress the first run bubble if the NTP sync promo bubble is showing
381 // or if sign in is in progress.
382 if (contents->GetURL().host() == chrome::kChromeUINewTabHost) {
384 Profile::FromBrowserContext(contents->GetBrowserContext());
385 SigninManagerBase* manager =
386 SigninManagerFactory::GetForProfile(profile);
387 bool signin_in_progress = manager && manager->AuthInProgress();
388 bool is_promo_bubble_visible =
389 profile->GetPrefs()->GetBoolean(prefs::kSignInPromoShowNTPBubble);
391 if (is_promo_bubble_visible || signin_in_progress)
396 // Suppress the first run bubble if a global error bubble is pending.
397 GlobalErrorService* global_error_service =
398 GlobalErrorServiceFactory::GetForProfile(browser->profile());
399 if (global_error_service->GetFirstGlobalErrorWithBubbleView() != NULL)
402 // Reset the preference and notifications to avoid showing the bubble again.
403 prefs->SetInteger(prefs::kShowFirstRunBubbleOption,
404 first_run::FIRST_RUN_BUBBLE_DONT_SHOW);
406 // Show the bubble now and destroy this bubble launcher.
407 browser->ShowFirstRunBubble();
411 static base::LazyInstance<base::FilePath> master_prefs_path_for_testing
412 = LAZY_INSTANCE_INITIALIZER;
414 // Loads master preferences from the master preference file into the installer
415 // master preferences. Returns the pointer to installer::MasterPreferences
416 // object if successful; otherwise, returns NULL.
417 installer::MasterPreferences* LoadMasterPrefs() {
418 base::FilePath master_prefs_path;
419 if (!master_prefs_path_for_testing.Get().empty())
420 master_prefs_path = master_prefs_path_for_testing.Get();
422 master_prefs_path = base::FilePath(first_run::internal::MasterPrefsPath());
423 if (master_prefs_path.empty())
425 installer::MasterPreferences* install_prefs =
426 new installer::MasterPreferences(master_prefs_path);
427 if (!install_prefs->read_from_file()) {
428 delete install_prefs;
432 return install_prefs;
435 // Makes chrome the user's default browser according to policy or
436 // |make_chrome_default_for_user| if no policy is set.
437 void ProcessDefaultBrowserPolicy(bool make_chrome_default_for_user) {
438 // Only proceed if chrome can be made default unattended. The interactive case
439 // (Windows 8+) is handled by the first run default browser prompt.
440 if (ShellIntegration::CanSetAsDefaultBrowser() ==
441 ShellIntegration::SET_DEFAULT_UNATTENDED) {
442 // The policy has precedence over the user's choice.
443 if (g_browser_process->local_state()->IsManagedPreference(
444 prefs::kDefaultBrowserSettingEnabled)) {
445 if (g_browser_process->local_state()->GetBoolean(
446 prefs::kDefaultBrowserSettingEnabled)) {
447 ShellIntegration::SetAsDefaultBrowser();
449 } else if (make_chrome_default_for_user) {
450 ShellIntegration::SetAsDefaultBrowser();
457 namespace first_run {
460 FirstRunState first_run_ = FIRST_RUN_UNKNOWN;
462 void SetupMasterPrefsFromInstallPrefs(
463 const installer::MasterPreferences& install_prefs,
464 MasterPrefs* out_prefs) {
465 ConvertStringVectorToGURLVector(
466 install_prefs.GetFirstRunTabs(), &out_prefs->new_tabs);
468 install_prefs.GetInt(installer::master_preferences::kDistroPingDelay,
469 &out_prefs->ping_delay);
472 if (install_prefs.GetBool(
473 installer::master_preferences::kDistroImportSearchPref, &value)) {
475 out_prefs->do_import_items |= importer::SEARCH_ENGINES;
477 out_prefs->dont_import_items |= importer::SEARCH_ENGINES;
481 // If we're suppressing the first-run bubble, set that preference now.
482 // Otherwise, wait until the user has completed first run to set it, so the
483 // user is guaranteed to see the bubble iff he or she has completed the first
485 if (install_prefs.GetBool(
486 installer::master_preferences::kDistroSuppressFirstRunBubble,
488 SetShowFirstRunBubblePref(FIRST_RUN_BUBBLE_SUPPRESS);
490 if (install_prefs.GetBool(
491 installer::master_preferences::kDistroImportHistoryPref,
494 out_prefs->do_import_items |= importer::HISTORY;
496 out_prefs->dont_import_items |= importer::HISTORY;
500 std::string not_used;
501 out_prefs->homepage_defined = install_prefs.GetString(
502 prefs::kHomePage, ¬_used);
504 if (install_prefs.GetBool(
505 installer::master_preferences::kDistroImportHomePagePref,
508 out_prefs->do_import_items |= importer::HOME_PAGE;
510 out_prefs->dont_import_items |= importer::HOME_PAGE;
514 // Bookmarks are never imported unless specifically turned on.
515 if (install_prefs.GetBool(
516 installer::master_preferences::kDistroImportBookmarksPref,
519 out_prefs->do_import_items |= importer::FAVORITES;
521 out_prefs->dont_import_items |= importer::FAVORITES;
524 if (install_prefs.GetBool(
525 installer::master_preferences::kMakeChromeDefaultForUser,
527 out_prefs->make_chrome_default_for_user = true;
530 if (install_prefs.GetBool(
531 installer::master_preferences::kSuppressFirstRunDefaultBrowserPrompt,
533 out_prefs->suppress_first_run_default_browser_prompt = true;
536 install_prefs.GetString(
537 installer::master_preferences::kDistroImportBookmarksFromFilePref,
538 &out_prefs->import_bookmarks_path);
540 out_prefs->variations_seed = install_prefs.GetVariationsSeed();
541 out_prefs->variations_seed_signature =
542 install_prefs.GetVariationsSeedSignature();
544 install_prefs.GetString(
545 installer::master_preferences::kDistroSuppressDefaultBrowserPromptPref,
546 &out_prefs->suppress_default_browser_prompt_for_version);
549 bool GetFirstRunSentinelFilePath(base::FilePath* path) {
550 base::FilePath user_data_dir;
551 if (!PathService::Get(chrome::DIR_USER_DATA, &user_data_dir))
553 *path = user_data_dir.Append(chrome::kFirstRunSentinel);
557 bool CreateSentinel() {
558 base::FilePath first_run_sentinel;
559 return GetFirstRunSentinelFilePath(&first_run_sentinel) &&
560 base::WriteFile(first_run_sentinel, "", 0) != -1;
563 // -- Platform-specific functions --
565 #if !defined(OS_LINUX) && !defined(OS_BSD)
566 bool IsOrganicFirstRun() {
568 google_util::GetBrand(&brand);
569 return google_util::IsOrganicFirstRun(brand);
573 } // namespace internal
575 MasterPrefs::MasterPrefs()
577 homepage_defined(false),
579 dont_import_items(0),
580 make_chrome_default_for_user(false),
581 suppress_first_run_default_browser_prompt(false) {
584 MasterPrefs::~MasterPrefs() {}
586 bool IsChromeFirstRun() {
587 if (internal::first_run_ == internal::FIRST_RUN_UNKNOWN) {
588 internal::first_run_ = internal::FIRST_RUN_FALSE;
589 const CommandLine* command_line = CommandLine::ForCurrentProcess();
590 if (command_line->HasSwitch(switches::kForceFirstRun) ||
591 (!command_line->HasSwitch(switches::kNoFirstRun) &&
592 !internal::IsFirstRunSentinelPresent())) {
593 internal::first_run_ = internal::FIRST_RUN_TRUE;
596 return internal::first_run_ == internal::FIRST_RUN_TRUE;
599 bool IsFirstRunSuppressed(const CommandLine& command_line) {
600 return command_line.HasSwitch(switches::kNoFirstRun);
603 void CreateSentinelIfNeeded() {
604 if (IsChromeFirstRun())
605 internal::CreateSentinel();
608 std::string GetPingDelayPrefName() {
609 return base::StringPrintf("%s.%s",
610 installer::master_preferences::kDistroDict,
611 installer::master_preferences::kDistroPingDelay);
614 void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
615 registry->RegisterIntegerPref(
616 GetPingDelayPrefName().c_str(),
618 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
621 bool RemoveSentinel() {
622 base::FilePath first_run_sentinel;
623 return internal::GetFirstRunSentinelFilePath(&first_run_sentinel) &&
624 base::DeleteFile(first_run_sentinel, false);
627 bool SetShowFirstRunBubblePref(FirstRunBubbleOptions show_bubble_option) {
628 PrefService* local_state = g_browser_process->local_state();
631 if (local_state->GetInteger(
632 prefs::kShowFirstRunBubbleOption) != FIRST_RUN_BUBBLE_SUPPRESS) {
633 // Set the new state as long as the bubble wasn't explicitly suppressed
635 local_state->SetInteger(prefs::kShowFirstRunBubbleOption,
641 void SetShouldShowWelcomePage() {
642 g_should_show_welcome_page = true;
645 bool ShouldShowWelcomePage() {
646 bool retval = g_should_show_welcome_page;
647 g_should_show_welcome_page = false;
651 void SetShouldDoPersonalDataManagerFirstRun() {
652 g_should_do_autofill_personal_data_manager_first_run = true;
655 bool ShouldDoPersonalDataManagerFirstRun() {
656 bool retval = g_should_do_autofill_personal_data_manager_first_run;
657 g_should_do_autofill_personal_data_manager_first_run = false;
661 void LogFirstRunMetric(FirstRunBubbleMetric metric) {
662 UMA_HISTOGRAM_ENUMERATION("FirstRun.SearchEngineBubble", metric,
663 NUM_FIRST_RUN_BUBBLE_METRICS);
666 void SetMasterPrefsPathForTesting(const base::FilePath& master_prefs) {
667 master_prefs_path_for_testing.Get() = master_prefs;
670 ProcessMasterPreferencesResult ProcessMasterPreferences(
671 const base::FilePath& user_data_dir,
672 MasterPrefs* out_prefs) {
673 DCHECK(!user_data_dir.empty());
675 scoped_ptr<installer::MasterPreferences> install_prefs(LoadMasterPrefs());
677 // Default value in case master preferences is missing or corrupt, or
678 // ping_delay is missing.
679 out_prefs->ping_delay = 90;
680 if (install_prefs.get()) {
681 if (!internal::ShowPostInstallEULAIfNeeded(install_prefs.get()))
682 return EULA_EXIT_NOW;
684 if (!chrome_prefs::InitializePrefsFromMasterPrefs(
685 profiles::GetDefaultProfileDir(user_data_dir),
686 install_prefs->master_dictionary())) {
687 DLOG(ERROR) << "Failed to initialize from master_preferences.";
690 DoDelayedInstallExtensionsIfNeeded(install_prefs.get());
692 internal::SetupMasterPrefsFromInstallPrefs(*install_prefs, out_prefs);
695 return FIRST_RUN_PROCEED;
700 bool homepage_defined,
702 int dont_import_items,
703 const std::string& import_bookmarks_path) {
704 base::FilePath local_state_path;
705 PathService::Get(chrome::FILE_LOCAL_STATE, &local_state_path);
706 bool local_state_file_exists = base::PathExists(local_state_path);
708 scoped_refptr<ImporterList> importer_list(new ImporterList());
709 importer_list->DetectSourceProfilesHack(
710 g_browser_process->GetApplicationLocale(), false);
712 // Do import if there is an available profile for us to import.
713 if (importer_list->count() > 0) {
714 if (internal::IsOrganicFirstRun()) {
715 // Home page is imported in organic builds only unless turned off or
716 // defined in master_preferences.
717 if (homepage_defined) {
718 dont_import_items |= importer::HOME_PAGE;
719 if (import_items & importer::HOME_PAGE)
720 import_items &= ~importer::HOME_PAGE;
722 // Search engines are not imported automatically in organic builds if the
723 // user already has a user preferences directory.
724 if (local_state_file_exists) {
725 dont_import_items |= importer::SEARCH_ENGINES;
726 if (import_items & importer::SEARCH_ENGINES)
727 import_items &= ~importer::SEARCH_ENGINES;
731 PrefService* user_prefs = profile->GetPrefs();
734 SetImportItem(user_prefs,
735 prefs::kImportHistory,
740 SetImportItem(user_prefs,
741 prefs::kImportHomepage,
746 SetImportItem(user_prefs,
747 prefs::kImportSearchEngine,
750 importer::SEARCH_ENGINES,
752 SetImportItem(user_prefs,
753 prefs::kImportBookmarks,
760 ExternalProcessImporterHost* importer_host =
761 new ExternalProcessImporterHost;
763 // Don't show the warning dialog if import fails.
764 importer_host->set_headless();
766 importer::LogImporterUseToMetrics(
767 "AutoImport", importer_list->GetSourceProfileAt(0).importer_type);
769 ImportSettings(profile, importer_host, importer_list, items);
772 if (!import_bookmarks_path.empty()) {
774 ExternalProcessImporterHost* file_importer_host =
775 new ExternalProcessImporterHost;
776 file_importer_host->set_headless();
778 ImportFromFile(profile, file_importer_host, import_bookmarks_path);
781 content::RecordAction(UserMetricsAction("FirstRunDef_Accept"));
783 g_auto_import_state |= AUTO_IMPORT_CALLED;
786 void DoPostImportTasks(Profile* profile, bool make_chrome_default_for_user) {
787 // Only set default browser after import as auto import relies on the current
788 // default browser to know what to import from.
789 ProcessDefaultBrowserPolicy(make_chrome_default_for_user);
791 // Display the first run bubble if there is a default search provider.
792 TemplateURLService* template_url =
793 TemplateURLServiceFactory::GetForProfile(profile);
794 if (template_url && template_url->GetDefaultSearchProvider())
795 FirstRunBubbleLauncher::ShowFirstRunBubbleSoon();
796 SetShouldShowWelcomePage();
797 SetShouldDoPersonalDataManagerFirstRun();
799 internal::DoPostImportPlatformSpecificTasks(profile);
802 uint16 auto_import_state() {
803 return g_auto_import_state;
806 } // namespace first_run