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/message_loop/message_loop.h"
16 #include "base/metrics/histogram.h"
17 #include "base/path_service.h"
18 #include "base/prefs/pref_service.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/profiles_state.h"
35 #include "chrome/browser/search_engines/template_url_service.h"
36 #include "chrome/browser/search_engines/template_url_service_factory.h"
37 #include "chrome/browser/shell_integration.h"
38 #include "chrome/browser/signin/signin_manager.h"
39 #include "chrome/browser/signin/signin_manager_factory.h"
40 #include "chrome/browser/signin/signin_promo.h"
41 #include "chrome/browser/signin/signin_tracker.h"
42 #include "chrome/browser/ui/browser.h"
43 #include "chrome/browser/ui/browser_finder.h"
44 #include "chrome/browser/ui/global_error/global_error_service.h"
45 #include "chrome/browser/ui/global_error/global_error_service_factory.h"
46 #include "chrome/browser/ui/tabs/tab_strip_model.h"
47 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h"
48 #include "chrome/common/chrome_paths.h"
49 #include "chrome/common/chrome_switches.h"
50 #include "chrome/common/pref_names.h"
51 #include "chrome/common/url_constants.h"
52 #include "chrome/installer/util/master_preferences.h"
53 #include "chrome/installer/util/master_preferences_constants.h"
54 #include "chrome/installer/util/util_constants.h"
55 #include "components/user_prefs/pref_registry_syncable.h"
56 #include "content/public/browser/notification_observer.h"
57 #include "content/public/browser/notification_registrar.h"
58 #include "content/public/browser/notification_service.h"
59 #include "content/public/browser/notification_types.h"
60 #include "content/public/browser/user_metrics.h"
61 #include "content/public/browser/web_contents.h"
62 #include "google_apis/gaia/gaia_auth_util.h"
65 using base::UserMetricsAction;
69 // A bitfield formed from values in AutoImportState to record the state of
70 // AutoImport. This is used in testing to verify import startup actions that
71 // occur before an observer can be registered in the test.
72 uint16 g_auto_import_state = first_run::AUTO_IMPORT_NONE;
74 // Flags for functions of similar name.
75 bool g_should_show_welcome_page = false;
76 bool g_should_do_autofill_personal_data_manager_first_run = false;
78 // This class acts as an observer for the ImporterProgressObserver::ImportEnded
79 // callback. When the import process is started, certain errors may cause
80 // ImportEnded() to be called synchronously, but the typical case is that
81 // ImportEnded() is called asynchronously. Thus we have to handle both cases.
82 class ImportEndedObserver : public importer::ImporterProgressObserver {
84 ImportEndedObserver() : ended_(false),
85 should_quit_message_loop_(false) {}
86 virtual ~ImportEndedObserver() {}
88 // importer::ImporterProgressObserver:
89 virtual void ImportStarted() OVERRIDE {}
90 virtual void ImportItemStarted(importer::ImportItem item) OVERRIDE {}
91 virtual void ImportItemEnded(importer::ImportItem item) OVERRIDE {}
92 virtual void ImportEnded() OVERRIDE {
94 if (should_quit_message_loop_)
95 base::MessageLoop::current()->Quit();
98 void set_should_quit_message_loop() {
99 should_quit_message_loop_ = true;
107 // Set if the import has ended.
110 bool should_quit_message_loop_;
113 // Helper class that performs delayed first-run tasks that need more of the
114 // chrome infrastructure to be up and running before they can be attempted.
115 class FirstRunDelayedTasks : public content::NotificationObserver {
122 explicit FirstRunDelayedTasks(Tasks task) {
123 if (task == INSTALL_EXTENSIONS) {
124 registrar_.Add(this, chrome::NOTIFICATION_EXTENSIONS_READY,
125 content::NotificationService::AllSources());
127 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_CLOSED,
128 content::NotificationService::AllSources());
131 virtual void Observe(int type,
132 const content::NotificationSource& source,
133 const content::NotificationDetails& details) OVERRIDE {
134 // After processing the notification we always delete ourselves.
135 if (type == chrome::NOTIFICATION_EXTENSIONS_READY) {
137 content::Source<Profile>(source).ptr()->GetExtensionService());
143 // Private ctor forces it to be created only in the heap.
144 virtual ~FirstRunDelayedTasks() {}
146 // The extension work is to basically trigger an extension update check.
147 // If the extension specified in the master pref is older than the live
148 // extension it will get updated which is the same as get it installed.
149 void DoExtensionWork(ExtensionService* service) {
151 service->updater()->CheckNow(extensions::ExtensionUpdater::CheckParams());
154 content::NotificationRegistrar registrar_;
157 // Installs a task to do an extensions update check once the extensions system
159 void DoDelayedInstallExtensions() {
160 new FirstRunDelayedTasks(FirstRunDelayedTasks::INSTALL_EXTENSIONS);
163 void DoDelayedInstallExtensionsIfNeeded(
164 installer::MasterPreferences* install_prefs) {
165 base::DictionaryValue* extensions = 0;
166 if (install_prefs->GetExtensionsBlock(&extensions)) {
167 VLOG(1) << "Extensions block found in master preferences";
168 DoDelayedInstallExtensions();
172 // Sets the |items| bitfield according to whether the import data specified by
173 // |import_type| should be be auto imported or not.
174 void SetImportItem(PrefService* user_prefs,
175 const char* pref_path,
177 int dont_import_items,
178 importer::ImportItem import_type,
180 // Work out whether an item is to be imported according to what is specified
181 // in master preferences.
182 bool should_import = false;
183 bool master_pref_set =
184 ((import_items | dont_import_items) & import_type) != 0;
185 bool master_pref = ((import_items & ~dont_import_items) & import_type) != 0;
187 if (import_type == importer::HISTORY ||
188 (import_type != importer::FAVORITES &&
189 first_run::internal::IsOrganicFirstRun())) {
190 // History is always imported unless turned off in master_preferences.
191 // Search engines and home page are imported in organic builds only
192 // unless turned off in master_preferences.
193 should_import = !master_pref_set || master_pref;
195 // Bookmarks are never imported, unless turned on in master_preferences.
196 // Search engine and home page import behaviour is similar in non organic
198 should_import = master_pref_set && master_pref;
201 // If an import policy is set, import items according to policy. If no master
202 // preference is set, but a corresponding recommended policy is set, import
203 // item according to recommended policy. If both a master preference and a
204 // recommended policy is set, the master preference wins. If neither
205 // recommended nor managed policies are set, import item according to what we
208 user_prefs->SetBoolean(pref_path, should_import);
210 if (!user_prefs->FindPreference(pref_path)->IsDefaultValue()) {
211 if (user_prefs->GetBoolean(pref_path))
212 *items |= import_type;
213 } else { // no policy (recommended or managed) is set
215 *items |= import_type;
218 user_prefs->ClearPref(pref_path);
221 // Launches the import, via |importer_host|, from |source_profile| into
222 // |target_profile| for the items specified in the |items_to_import| bitfield.
223 // This may be done in a separate process depending on the platform, but it will
224 // always block until done.
225 void ImportFromSourceProfile(ExternalProcessImporterHost* importer_host,
226 const importer::SourceProfile& source_profile,
227 Profile* target_profile,
228 uint16 items_to_import) {
229 ImportEndedObserver observer;
230 importer_host->set_observer(&observer);
231 importer_host->StartImportSettings(source_profile,
234 new ProfileWriter(target_profile));
235 // If the import process has not errored out, block on it.
236 if (!observer.ended()) {
237 observer.set_should_quit_message_loop();
238 base::MessageLoop::current()->Run();
242 // Imports bookmarks from an html file whose path is provided by
243 // |import_bookmarks_path|.
244 void ImportFromFile(Profile* profile,
245 ExternalProcessImporterHost* file_importer_host,
246 const std::string& import_bookmarks_path) {
247 importer::SourceProfile source_profile;
248 source_profile.importer_type = importer::TYPE_BOOKMARKS_FILE;
250 const base::FilePath::StringType& import_bookmarks_path_str =
252 base::UTF8ToUTF16(import_bookmarks_path);
254 import_bookmarks_path;
256 source_profile.source_path = base::FilePath(import_bookmarks_path_str);
258 ImportFromSourceProfile(file_importer_host, source_profile, profile,
259 importer::FAVORITES);
260 g_auto_import_state |= first_run::AUTO_IMPORT_BOOKMARKS_FILE_IMPORTED;
263 // Imports settings from the first profile in |importer_list|.
264 void ImportSettings(Profile* profile,
265 ExternalProcessImporterHost* importer_host,
266 scoped_refptr<ImporterList> importer_list,
267 int items_to_import) {
268 const importer::SourceProfile& source_profile =
269 importer_list->GetSourceProfileAt(0);
271 // Ensure that importers aren't requested to import items that they do not
272 // support. If there is no overlap, skip.
273 items_to_import &= source_profile.services_supported;
274 if (items_to_import == 0)
277 ImportFromSourceProfile(importer_host, source_profile, profile,
279 g_auto_import_state |= first_run::AUTO_IMPORT_PROFILE_IMPORTED;
282 GURL UrlFromString(const std::string& in) {
286 void ConvertStringVectorToGURLVector(
287 const std::vector<std::string>& src,
288 std::vector<GURL>* ret) {
289 ret->resize(src.size());
290 std::transform(src.begin(), src.end(), ret->begin(), &UrlFromString);
293 // Show the first run search engine bubble at the first appropriate opportunity.
294 // This bubble may be delayed by other UI, like global errors and sync promos.
295 class FirstRunBubbleLauncher : public content::NotificationObserver {
297 // Show the bubble at the first appropriate opportunity. This function
298 // instantiates a FirstRunBubbleLauncher, which manages its own lifetime.
299 static void ShowFirstRunBubbleSoon();
302 FirstRunBubbleLauncher();
303 virtual ~FirstRunBubbleLauncher();
305 // content::NotificationObserver:
306 virtual void Observe(int type,
307 const content::NotificationSource& source,
308 const content::NotificationDetails& details) OVERRIDE;
310 content::NotificationRegistrar registrar_;
312 DISALLOW_COPY_AND_ASSIGN(FirstRunBubbleLauncher);
316 void FirstRunBubbleLauncher::ShowFirstRunBubbleSoon() {
317 SetShowFirstRunBubblePref(first_run::FIRST_RUN_BUBBLE_SHOW);
318 // This FirstRunBubbleLauncher instance will manage its own lifetime.
319 new FirstRunBubbleLauncher();
322 FirstRunBubbleLauncher::FirstRunBubbleLauncher() {
323 registrar_.Add(this, content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
324 content::NotificationService::AllSources());
326 // This notification is required to observe the switch between the sync setup
327 // page and the general settings page.
328 registrar_.Add(this, chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED,
329 content::NotificationService::AllSources());
332 FirstRunBubbleLauncher::~FirstRunBubbleLauncher() {}
334 void FirstRunBubbleLauncher::Observe(
336 const content::NotificationSource& source,
337 const content::NotificationDetails& details) {
338 DCHECK(type == content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME ||
339 type == chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED);
341 Browser* browser = chrome::FindBrowserWithWebContents(
342 content::Source<content::WebContents>(source).ptr());
343 if (!browser || !browser->is_type_tabbed())
346 // Check the preference to determine if the bubble should be shown.
347 PrefService* prefs = g_browser_process->local_state();
348 if (!prefs || prefs->GetInteger(prefs::kShowFirstRunBubbleOption) !=
349 first_run::FIRST_RUN_BUBBLE_SHOW) {
354 content::WebContents* contents =
355 browser->tab_strip_model()->GetActiveWebContents();
357 // Suppress the first run bubble if a Gaia sign in page, the continue
358 // URL for the sign in page or the sync setup page is showing.
360 (contents->GetURL().GetOrigin().spec() ==
361 chrome::kChromeUIChromeSigninURL ||
362 gaia::IsGaiaSignonRealm(contents->GetURL().GetOrigin()) ||
363 signin::IsContinueUrlForWebBasedSigninFlow(contents->GetURL()) ||
364 contents->GetURL() == GURL(std::string(chrome::kChromeUISettingsURL) +
365 chrome::kSyncSetupSubPage))) {
369 if (contents && contents->GetURL().SchemeIs(content::kChromeUIScheme)) {
370 // Suppress the first run bubble if 'make chrome metro' flow is showing.
371 if (contents->GetURL().host() == chrome::kChromeUIMetroFlowHost)
374 // Suppress the first run bubble if the NTP sync promo bubble is showing
375 // or if sign in is in progress.
376 if (contents->GetURL().host() == chrome::kChromeUINewTabHost) {
378 Profile::FromBrowserContext(contents->GetBrowserContext());
379 SigninManagerBase* manager =
380 SigninManagerFactory::GetForProfile(profile);
381 bool signin_in_progress = manager && manager->AuthInProgress();
382 bool is_promo_bubble_visible =
383 profile->GetPrefs()->GetBoolean(prefs::kSignInPromoShowNTPBubble);
385 if (is_promo_bubble_visible || signin_in_progress)
390 // Suppress the first run bubble if a global error bubble is pending.
391 GlobalErrorService* global_error_service =
392 GlobalErrorServiceFactory::GetForProfile(browser->profile());
393 if (global_error_service->GetFirstGlobalErrorWithBubbleView() != NULL)
396 // Reset the preference and notifications to avoid showing the bubble again.
397 prefs->SetInteger(prefs::kShowFirstRunBubbleOption,
398 first_run::FIRST_RUN_BUBBLE_DONT_SHOW);
400 // Show the bubble now and destroy this bubble launcher.
401 browser->ShowFirstRunBubble();
405 static base::LazyInstance<base::FilePath> master_prefs_path_for_testing
406 = LAZY_INSTANCE_INITIALIZER;
408 // Loads master preferences from the master preference file into the installer
409 // master preferences. Returns the pointer to installer::MasterPreferences
410 // object if successful; otherwise, returns NULL.
411 installer::MasterPreferences* LoadMasterPrefs() {
412 base::FilePath master_prefs_path;
413 if (!master_prefs_path_for_testing.Get().empty())
414 master_prefs_path = master_prefs_path_for_testing.Get();
416 master_prefs_path = base::FilePath(first_run::internal::MasterPrefsPath());
417 if (master_prefs_path.empty())
419 installer::MasterPreferences* install_prefs =
420 new installer::MasterPreferences(master_prefs_path);
421 if (!install_prefs->read_from_file()) {
422 delete install_prefs;
426 return install_prefs;
429 // Makes chrome the user's default browser according to policy or
430 // |make_chrome_default_for_user| if no policy is set.
431 void ProcessDefaultBrowserPolicy(bool make_chrome_default_for_user) {
432 // Only proceed if chrome can be made default unattended. The interactive case
433 // (Windows 8+) is handled by the first run default browser prompt.
434 if (ShellIntegration::CanSetAsDefaultBrowser() ==
435 ShellIntegration::SET_DEFAULT_UNATTENDED) {
436 // The policy has precedence over the user's choice.
437 if (g_browser_process->local_state()->IsManagedPreference(
438 prefs::kDefaultBrowserSettingEnabled)) {
439 if (g_browser_process->local_state()->GetBoolean(
440 prefs::kDefaultBrowserSettingEnabled)) {
441 ShellIntegration::SetAsDefaultBrowser();
443 } else if (make_chrome_default_for_user) {
444 ShellIntegration::SetAsDefaultBrowser();
451 namespace first_run {
454 FirstRunState first_run_ = FIRST_RUN_UNKNOWN;
456 void SetupMasterPrefsFromInstallPrefs(
457 const installer::MasterPreferences& install_prefs,
458 MasterPrefs* out_prefs) {
459 ConvertStringVectorToGURLVector(
460 install_prefs.GetFirstRunTabs(), &out_prefs->new_tabs);
462 install_prefs.GetInt(installer::master_preferences::kDistroPingDelay,
463 &out_prefs->ping_delay);
466 if (install_prefs.GetBool(
467 installer::master_preferences::kDistroImportSearchPref, &value)) {
469 out_prefs->do_import_items |= importer::SEARCH_ENGINES;
471 out_prefs->dont_import_items |= importer::SEARCH_ENGINES;
475 // If we're suppressing the first-run bubble, set that preference now.
476 // Otherwise, wait until the user has completed first run to set it, so the
477 // user is guaranteed to see the bubble iff he or she has completed the first
479 if (install_prefs.GetBool(
480 installer::master_preferences::kDistroSuppressFirstRunBubble,
482 SetShowFirstRunBubblePref(FIRST_RUN_BUBBLE_SUPPRESS);
484 if (install_prefs.GetBool(
485 installer::master_preferences::kDistroImportHistoryPref,
488 out_prefs->do_import_items |= importer::HISTORY;
490 out_prefs->dont_import_items |= importer::HISTORY;
494 std::string not_used;
495 out_prefs->homepage_defined = install_prefs.GetString(
496 prefs::kHomePage, ¬_used);
498 if (install_prefs.GetBool(
499 installer::master_preferences::kDistroImportHomePagePref,
502 out_prefs->do_import_items |= importer::HOME_PAGE;
504 out_prefs->dont_import_items |= importer::HOME_PAGE;
508 // Bookmarks are never imported unless specifically turned on.
509 if (install_prefs.GetBool(
510 installer::master_preferences::kDistroImportBookmarksPref,
513 out_prefs->do_import_items |= importer::FAVORITES;
515 out_prefs->dont_import_items |= importer::FAVORITES;
518 if (install_prefs.GetBool(
519 installer::master_preferences::kMakeChromeDefaultForUser,
521 out_prefs->make_chrome_default_for_user = true;
524 if (install_prefs.GetBool(
525 installer::master_preferences::kSuppressFirstRunDefaultBrowserPrompt,
527 out_prefs->suppress_first_run_default_browser_prompt = true;
530 install_prefs.GetString(
531 installer::master_preferences::kDistroImportBookmarksFromFilePref,
532 &out_prefs->import_bookmarks_path);
534 out_prefs->variations_seed = install_prefs.GetVariationsSeed();
535 out_prefs->variations_seed_signature =
536 install_prefs.GetVariationsSeedSignature();
538 install_prefs.GetString(
539 installer::master_preferences::kDistroSuppressDefaultBrowserPromptPref,
540 &out_prefs->suppress_default_browser_prompt_for_version);
543 bool CreateSentinel() {
544 base::FilePath first_run_sentinel;
545 if (!internal::GetFirstRunSentinelFilePath(&first_run_sentinel))
547 return file_util::WriteFile(first_run_sentinel, "", 0) != -1;
550 // -- Platform-specific functions --
552 #if !defined(OS_LINUX) && !defined(OS_BSD)
553 bool IsOrganicFirstRun() {
555 google_util::GetBrand(&brand);
556 return google_util::IsOrganicFirstRun(brand);
560 } // namespace internal
562 MasterPrefs::MasterPrefs()
564 homepage_defined(false),
566 dont_import_items(0),
567 make_chrome_default_for_user(false),
568 suppress_first_run_default_browser_prompt(false) {
571 MasterPrefs::~MasterPrefs() {}
573 bool IsChromeFirstRun() {
574 if (internal::first_run_ != internal::FIRST_RUN_UNKNOWN)
575 return internal::first_run_ == internal::FIRST_RUN_TRUE;
577 internal::first_run_ = internal::FIRST_RUN_FALSE;
579 base::FilePath first_run_sentinel;
580 const CommandLine* command_line = CommandLine::ForCurrentProcess();
581 if (command_line->HasSwitch(switches::kForceFirstRun)) {
582 internal::first_run_ = internal::FIRST_RUN_TRUE;
583 } else if (command_line->HasSwitch(switches::kCancelFirstRun)) {
584 internal::first_run_ = internal::FIRST_RUN_CANCEL;
585 } else if (!command_line->HasSwitch(switches::kNoFirstRun) &&
586 internal::GetFirstRunSentinelFilePath(&first_run_sentinel) &&
587 !base::PathExists(first_run_sentinel)) {
588 internal::first_run_ = internal::FIRST_RUN_TRUE;
591 return internal::first_run_ == internal::FIRST_RUN_TRUE;
594 bool IsFirstRunSuppressed(const CommandLine& command_line) {
595 return command_line.HasSwitch(switches::kCancelFirstRun) ||
596 command_line.HasSwitch(switches::kNoFirstRun);
599 void CreateSentinelIfNeeded() {
600 if (IsChromeFirstRun() ||
601 internal::first_run_ == internal::FIRST_RUN_CANCEL) {
602 internal::CreateSentinel();
606 std::string GetPingDelayPrefName() {
607 return base::StringPrintf("%s.%s",
608 installer::master_preferences::kDistroDict,
609 installer::master_preferences::kDistroPingDelay);
612 void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
613 registry->RegisterIntegerPref(
614 GetPingDelayPrefName().c_str(),
616 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
619 bool RemoveSentinel() {
620 base::FilePath first_run_sentinel;
621 if (!internal::GetFirstRunSentinelFilePath(&first_run_sentinel))
623 return base::DeleteFile(first_run_sentinel, false);
626 bool SetShowFirstRunBubblePref(FirstRunBubbleOptions show_bubble_option) {
627 PrefService* local_state = g_browser_process->local_state();
630 if (local_state->GetInteger(
631 prefs::kShowFirstRunBubbleOption) != FIRST_RUN_BUBBLE_SUPPRESS) {
632 // Set the new state as long as the bubble wasn't explicitly suppressed
634 local_state->SetInteger(prefs::kShowFirstRunBubbleOption,
640 void SetShouldShowWelcomePage() {
641 g_should_show_welcome_page = true;
644 bool ShouldShowWelcomePage() {
645 bool retval = g_should_show_welcome_page;
646 g_should_show_welcome_page = false;
650 void SetShouldDoPersonalDataManagerFirstRun() {
651 g_should_do_autofill_personal_data_manager_first_run = true;
654 bool ShouldDoPersonalDataManagerFirstRun() {
655 bool retval = g_should_do_autofill_personal_data_manager_first_run;
656 g_should_do_autofill_personal_data_manager_first_run = false;
660 void LogFirstRunMetric(FirstRunBubbleMetric metric) {
661 UMA_HISTOGRAM_ENUMERATION("FirstRun.SearchEngineBubble", metric,
662 NUM_FIRST_RUN_BUBBLE_METRICS);
665 void SetMasterPrefsPathForTesting(const base::FilePath& master_prefs) {
666 master_prefs_path_for_testing.Get() = master_prefs;
669 ProcessMasterPreferencesResult ProcessMasterPreferences(
670 const base::FilePath& user_data_dir,
671 MasterPrefs* out_prefs) {
672 DCHECK(!user_data_dir.empty());
674 scoped_ptr<installer::MasterPreferences> install_prefs(LoadMasterPrefs());
676 // Default value in case master preferences is missing or corrupt, or
677 // ping_delay is missing.
678 out_prefs->ping_delay = 90;
679 if (install_prefs.get()) {
680 if (!internal::ShowPostInstallEULAIfNeeded(install_prefs.get()))
681 return EULA_EXIT_NOW;
683 if (!chrome_prefs::InitializePrefsFromMasterPrefs(
684 profiles::GetDefaultProfileDir(user_data_dir),
685 install_prefs->master_dictionary())) {
686 DLOG(ERROR) << "Failed to initialize from master_preferences.";
689 DoDelayedInstallExtensionsIfNeeded(install_prefs.get());
691 internal::SetupMasterPrefsFromInstallPrefs(*install_prefs, out_prefs);
694 return FIRST_RUN_PROCEED;
699 bool homepage_defined,
701 int dont_import_items,
702 const std::string& import_bookmarks_path) {
703 base::FilePath local_state_path;
704 PathService::Get(chrome::FILE_LOCAL_STATE, &local_state_path);
705 bool local_state_file_exists = base::PathExists(local_state_path);
707 scoped_refptr<ImporterList> importer_list(new ImporterList());
708 importer_list->DetectSourceProfilesHack(
709 g_browser_process->GetApplicationLocale(), false);
711 // Do import if there is an available profile for us to import.
712 if (importer_list->count() > 0) {
713 if (internal::IsOrganicFirstRun()) {
714 // Home page is imported in organic builds only unless turned off or
715 // defined in master_preferences.
716 if (homepage_defined) {
717 dont_import_items |= importer::HOME_PAGE;
718 if (import_items & importer::HOME_PAGE)
719 import_items &= ~importer::HOME_PAGE;
721 // Search engines are not imported automatically in organic builds if the
722 // user already has a user preferences directory.
723 if (local_state_file_exists) {
724 dont_import_items |= importer::SEARCH_ENGINES;
725 if (import_items & importer::SEARCH_ENGINES)
726 import_items &= ~importer::SEARCH_ENGINES;
730 PrefService* user_prefs = profile->GetPrefs();
733 SetImportItem(user_prefs,
734 prefs::kImportHistory,
739 SetImportItem(user_prefs,
740 prefs::kImportHomepage,
745 SetImportItem(user_prefs,
746 prefs::kImportSearchEngine,
749 importer::SEARCH_ENGINES,
751 SetImportItem(user_prefs,
752 prefs::kImportBookmarks,
759 ExternalProcessImporterHost* importer_host =
760 new ExternalProcessImporterHost;
762 // Don't show the warning dialog if import fails.
763 importer_host->set_headless();
765 importer::LogImporterUseToMetrics(
766 "AutoImport", importer_list->GetSourceProfileAt(0).importer_type);
768 ImportSettings(profile, importer_host, importer_list, items);
771 if (!import_bookmarks_path.empty()) {
773 ExternalProcessImporterHost* file_importer_host =
774 new ExternalProcessImporterHost;
775 file_importer_host->set_headless();
777 ImportFromFile(profile, file_importer_host, import_bookmarks_path);
780 content::RecordAction(UserMetricsAction("FirstRunDef_Accept"));
782 g_auto_import_state |= AUTO_IMPORT_CALLED;
785 void DoPostImportTasks(Profile* profile, bool make_chrome_default_for_user) {
786 // Only set default browser after import as auto import relies on the current
787 // default browser to know what to import from.
788 ProcessDefaultBrowserPolicy(make_chrome_default_for_user);
790 // Display the first run bubble if there is a default search provider.
791 TemplateURLService* template_url =
792 TemplateURLServiceFactory::GetForProfile(profile);
793 if (template_url && template_url->GetDefaultSearchProvider())
794 FirstRunBubbleLauncher::ShowFirstRunBubbleSoon();
795 SetShouldShowWelcomePage();
796 SetShouldDoPersonalDataManagerFirstRun();
798 internal::DoPostImportPlatformSpecificTasks(profile);
801 uint16 auto_import_state() {
802 return g_auto_import_state;
805 } // namespace first_run