Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / chrome / browser / first_run / first_run.cc
blob671799c6b63f690b39e5e9dae48132fa14d70025
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"
7 #include <algorithm>
9 #include "base/command_line.h"
10 #include "base/compiler_specific.h"
11 #include "base/files/file_path.h"
12 #include "base/files/file_util.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_brand.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_factory.h"
37 #include "chrome/browser/shell_integration.h"
38 #include "chrome/browser/signin/signin_manager_factory.h"
39 #include "chrome/browser/ui/browser.h"
40 #include "chrome/browser/ui/browser_finder.h"
41 #include "chrome/browser/ui/chrome_pages.h"
42 #include "chrome/browser/ui/global_error/global_error_service.h"
43 #include "chrome/browser/ui/global_error/global_error_service_factory.h"
44 #include "chrome/browser/ui/tabs/tab_strip_model.h"
45 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h"
46 #include "chrome/common/chrome_constants.h"
47 #include "chrome/common/chrome_paths.h"
48 #include "chrome/common/chrome_switches.h"
49 #include "chrome/common/pref_names.h"
50 #include "chrome/common/url_constants.h"
51 #include "chrome/installer/util/master_preferences.h"
52 #include "chrome/installer/util/master_preferences_constants.h"
53 #include "chrome/installer/util/util_constants.h"
54 #include "components/pref_registry/pref_registry_syncable.h"
55 #include "components/search_engines/template_url_service.h"
56 #include "components/signin/core/browser/signin_manager.h"
57 #include "components/signin/core/browser/signin_tracker.h"
58 #include "content/public/browser/notification_observer.h"
59 #include "content/public/browser/notification_registrar.h"
60 #include "content/public/browser/notification_service.h"
61 #include "content/public/browser/notification_types.h"
62 #include "content/public/browser/user_metrics.h"
63 #include "content/public/browser/web_contents.h"
64 #include "extensions/browser/extension_system.h"
65 #include "google_apis/gaia/gaia_auth_util.h"
66 #include "url/gurl.h"
68 using base::UserMetricsAction;
70 namespace {
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 {
86 public:
87 ImportEndedObserver() : ended_(false) {}
88 ~ImportEndedObserver() override {}
90 // importer::ImporterProgressObserver:
91 void ImportStarted() override {}
92 void ImportItemStarted(importer::ImportItem item) override {}
93 void ImportItemEnded(importer::ImportItem item) override {}
94 void ImportEnded() override {
95 ended_ = true;
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;
104 bool ended() const {
105 return ended_;
108 private:
109 // Set if the import has ended.
110 bool 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 {
120 public:
121 enum Tasks {
122 NO_TASK,
123 INSTALL_EXTENSIONS
126 explicit FirstRunDelayedTasks(Tasks task) {
127 if (task == INSTALL_EXTENSIONS) {
128 registrar_.Add(this,
129 extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED,
130 content::NotificationService::AllSources());
132 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_CLOSED,
133 content::NotificationService::AllSources());
136 void Observe(int type,
137 const content::NotificationSource& source,
138 const content::NotificationDetails& details) override {
139 // After processing the notification we always delete ourselves.
140 if (type == extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED) {
141 Profile* profile = content::Source<Profile>(source).ptr();
142 ExtensionService* service =
143 extensions::ExtensionSystem::Get(profile)->extension_service();
144 DoExtensionWork(service);
146 delete this;
149 private:
150 // Private ctor forces it to be created only in the heap.
151 ~FirstRunDelayedTasks() override {}
153 // The extension work is to basically trigger an extension update check.
154 // If the extension specified in the master pref is older than the live
155 // extension it will get updated which is the same as get it installed.
156 void DoExtensionWork(ExtensionService* service) {
157 if (service)
158 service->updater()->CheckNow(extensions::ExtensionUpdater::CheckParams());
161 content::NotificationRegistrar registrar_;
164 // Installs a task to do an extensions update check once the extensions system
165 // is running.
166 void DoDelayedInstallExtensions() {
167 new FirstRunDelayedTasks(FirstRunDelayedTasks::INSTALL_EXTENSIONS);
170 void DoDelayedInstallExtensionsIfNeeded(
171 installer::MasterPreferences* install_prefs) {
172 base::DictionaryValue* extensions = 0;
173 if (install_prefs->GetExtensionsBlock(&extensions)) {
174 DVLOG(1) << "Extensions block found in master preferences";
175 DoDelayedInstallExtensions();
179 // Sets the |items| bitfield according to whether the import data specified by
180 // |import_type| should be be auto imported or not.
181 void SetImportItem(PrefService* user_prefs,
182 const char* pref_path,
183 int import_items,
184 int dont_import_items,
185 importer::ImportItem import_type,
186 int* items) {
187 // Work out whether an item is to be imported according to what is specified
188 // in master preferences.
189 bool should_import = false;
190 bool master_pref_set =
191 ((import_items | dont_import_items) & import_type) != 0;
192 bool master_pref = ((import_items & ~dont_import_items) & import_type) != 0;
194 if (import_type == importer::HISTORY ||
195 (import_type != importer::FAVORITES &&
196 first_run::internal::IsOrganicFirstRun())) {
197 // History is always imported unless turned off in master_preferences.
198 // Search engines and home page are imported in organic builds only
199 // unless turned off in master_preferences.
200 should_import = !master_pref_set || master_pref;
201 } else {
202 // Bookmarks are never imported, unless turned on in master_preferences.
203 // Search engine and home page import behaviour is similar in non organic
204 // builds.
205 should_import = master_pref_set && master_pref;
208 // If an import policy is set, import items according to policy. If no master
209 // preference is set, but a corresponding recommended policy is set, import
210 // item according to recommended policy. If both a master preference and a
211 // recommended policy is set, the master preference wins. If neither
212 // recommended nor managed policies are set, import item according to what we
213 // worked out above.
214 if (master_pref_set)
215 user_prefs->SetBoolean(pref_path, should_import);
217 if (!user_prefs->FindPreference(pref_path)->IsDefaultValue()) {
218 if (user_prefs->GetBoolean(pref_path))
219 *items |= import_type;
220 } else {
221 // no policy (recommended or managed) is set
222 if (should_import)
223 *items |= import_type;
226 user_prefs->ClearPref(pref_path);
229 // Launches the import, via |importer_host|, from |source_profile| into
230 // |target_profile| for the items specified in the |items_to_import| bitfield.
231 // This may be done in a separate process depending on the platform, but it will
232 // always block until done.
233 void ImportFromSourceProfile(ExternalProcessImporterHost* importer_host,
234 const importer::SourceProfile& source_profile,
235 Profile* target_profile,
236 uint16 items_to_import) {
237 ImportEndedObserver observer;
238 importer_host->set_observer(&observer);
239 importer_host->StartImportSettings(source_profile,
240 target_profile,
241 items_to_import,
242 new ProfileWriter(target_profile));
243 // If the import process has not errored out, block on it.
244 if (!observer.ended()) {
245 base::RunLoop loop;
246 observer.set_callback_for_import_end(loop.QuitClosure());
247 loop.Run();
248 observer.set_callback_for_import_end(base::Closure());
252 // Imports bookmarks from an html file whose path is provided by
253 // |import_bookmarks_path|.
254 void ImportFromFile(Profile* profile,
255 ExternalProcessImporterHost* file_importer_host,
256 const std::string& import_bookmarks_path) {
257 importer::SourceProfile source_profile;
258 source_profile.importer_type = importer::TYPE_BOOKMARKS_FILE;
260 const base::FilePath::StringType& import_bookmarks_path_str =
261 #if defined(OS_WIN)
262 base::UTF8ToUTF16(import_bookmarks_path);
263 #else
264 import_bookmarks_path;
265 #endif
266 source_profile.source_path = base::FilePath(import_bookmarks_path_str);
268 ImportFromSourceProfile(file_importer_host, source_profile, profile,
269 importer::FAVORITES);
270 g_auto_import_state |= first_run::AUTO_IMPORT_BOOKMARKS_FILE_IMPORTED;
273 // Imports settings from the first profile in |importer_list|.
274 void ImportSettings(Profile* profile,
275 ExternalProcessImporterHost* importer_host,
276 scoped_ptr<ImporterList> importer_list,
277 int items_to_import) {
278 const importer::SourceProfile& source_profile =
279 importer_list->GetSourceProfileAt(0);
281 // Ensure that importers aren't requested to import items that they do not
282 // support. If there is no overlap, skip.
283 items_to_import &= source_profile.services_supported;
284 if (items_to_import == 0)
285 return;
287 ImportFromSourceProfile(importer_host, source_profile, profile,
288 items_to_import);
289 g_auto_import_state |= first_run::AUTO_IMPORT_PROFILE_IMPORTED;
292 GURL UrlFromString(const std::string& in) {
293 return GURL(in);
296 void ConvertStringVectorToGURLVector(
297 const std::vector<std::string>& src,
298 std::vector<GURL>* ret) {
299 ret->resize(src.size());
300 std::transform(src.begin(), src.end(), ret->begin(), &UrlFromString);
303 // Show the first run search engine bubble at the first appropriate opportunity.
304 // This bubble may be delayed by other UI, like global errors and sync promos.
305 class FirstRunBubbleLauncher : public content::NotificationObserver {
306 public:
307 // Show the bubble at the first appropriate opportunity. This function
308 // instantiates a FirstRunBubbleLauncher, which manages its own lifetime.
309 static void ShowFirstRunBubbleSoon();
311 private:
312 FirstRunBubbleLauncher();
313 ~FirstRunBubbleLauncher() override;
315 // content::NotificationObserver:
316 void Observe(int type,
317 const content::NotificationSource& source,
318 const content::NotificationDetails& details) override;
320 content::NotificationRegistrar registrar_;
322 DISALLOW_COPY_AND_ASSIGN(FirstRunBubbleLauncher);
325 // static
326 void FirstRunBubbleLauncher::ShowFirstRunBubbleSoon() {
327 SetShowFirstRunBubblePref(first_run::FIRST_RUN_BUBBLE_SHOW);
328 // This FirstRunBubbleLauncher instance will manage its own lifetime.
329 new FirstRunBubbleLauncher();
332 FirstRunBubbleLauncher::FirstRunBubbleLauncher() {
333 registrar_.Add(this, content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
334 content::NotificationService::AllSources());
336 // This notification is required to observe the switch between the sync setup
337 // page and the general settings page.
338 registrar_.Add(this, chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED,
339 content::NotificationService::AllSources());
342 FirstRunBubbleLauncher::~FirstRunBubbleLauncher() {}
344 void FirstRunBubbleLauncher::Observe(
345 int type,
346 const content::NotificationSource& source,
347 const content::NotificationDetails& details) {
348 DCHECK(type == content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME ||
349 type == chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED);
351 Browser* browser = chrome::FindBrowserWithWebContents(
352 content::Source<content::WebContents>(source).ptr());
353 if (!browser || !browser->is_type_tabbed())
354 return;
356 // Check the preference to determine if the bubble should be shown.
357 PrefService* prefs = g_browser_process->local_state();
358 if (!prefs || prefs->GetInteger(prefs::kShowFirstRunBubbleOption) !=
359 first_run::FIRST_RUN_BUBBLE_SHOW) {
360 delete this;
361 return;
364 content::WebContents* contents =
365 browser->tab_strip_model()->GetActiveWebContents();
367 // Suppress the first run bubble if a Gaia sign in page or the sync setup
368 // page is showing.
369 if (contents &&
370 (contents->GetURL().GetOrigin().spec() ==
371 chrome::kChromeUIChromeSigninURL ||
372 gaia::IsGaiaSignonRealm(contents->GetURL().GetOrigin()) ||
373 contents->GetURL() ==
374 chrome::GetSettingsUrl(chrome::kSyncSetupSubPage))) {
375 return;
378 if (contents && contents->GetURL().SchemeIs(content::kChromeUIScheme)) {
379 #if defined(OS_WIN)
380 // Suppress the first run bubble if 'make chrome metro' flow is showing.
381 if (contents->GetURL().host() == chrome::kChromeUIMetroFlowHost)
382 return;
383 #endif
385 // Suppress the first run bubble if the NTP sync promo bubble is showing
386 // or if sign in is in progress.
387 if (contents->GetURL().host() == chrome::kChromeUINewTabHost) {
388 Profile* profile =
389 Profile::FromBrowserContext(contents->GetBrowserContext());
390 SigninManagerBase* manager =
391 SigninManagerFactory::GetForProfile(profile);
392 bool signin_in_progress = manager && manager->AuthInProgress();
393 bool is_promo_bubble_visible =
394 profile->GetPrefs()->GetBoolean(prefs::kSignInPromoShowNTPBubble);
396 if (is_promo_bubble_visible || signin_in_progress)
397 return;
401 // Suppress the first run bubble if a global error bubble is pending.
402 GlobalErrorService* global_error_service =
403 GlobalErrorServiceFactory::GetForProfile(browser->profile());
404 if (global_error_service->GetFirstGlobalErrorWithBubbleView() != NULL)
405 return;
407 // Reset the preference and notifications to avoid showing the bubble again.
408 prefs->SetInteger(prefs::kShowFirstRunBubbleOption,
409 first_run::FIRST_RUN_BUBBLE_DONT_SHOW);
411 // Show the bubble now and destroy this bubble launcher.
412 browser->ShowFirstRunBubble();
413 delete this;
416 static base::LazyInstance<base::FilePath> master_prefs_path_for_testing
417 = LAZY_INSTANCE_INITIALIZER;
419 // Loads master preferences from the master preference file into the installer
420 // master preferences. Returns the pointer to installer::MasterPreferences
421 // object if successful; otherwise, returns NULL.
422 installer::MasterPreferences* LoadMasterPrefs() {
423 base::FilePath master_prefs_path;
424 if (!master_prefs_path_for_testing.Get().empty())
425 master_prefs_path = master_prefs_path_for_testing.Get();
426 else
427 master_prefs_path = base::FilePath(first_run::internal::MasterPrefsPath());
428 if (master_prefs_path.empty())
429 return NULL;
430 installer::MasterPreferences* install_prefs =
431 new installer::MasterPreferences(master_prefs_path);
432 if (!install_prefs->read_from_file()) {
433 delete install_prefs;
434 return NULL;
437 return install_prefs;
440 // Makes chrome the user's default browser according to policy or
441 // |make_chrome_default_for_user| if no policy is set.
442 void ProcessDefaultBrowserPolicy(bool make_chrome_default_for_user) {
443 // Only proceed if chrome can be made default unattended. The interactive case
444 // (Windows 8+) is handled by the first run default browser prompt.
445 if (ShellIntegration::CanSetAsDefaultBrowser() ==
446 ShellIntegration::SET_DEFAULT_UNATTENDED) {
447 // The policy has precedence over the user's choice.
448 if (g_browser_process->local_state()->IsManagedPreference(
449 prefs::kDefaultBrowserSettingEnabled)) {
450 if (g_browser_process->local_state()->GetBoolean(
451 prefs::kDefaultBrowserSettingEnabled)) {
452 ShellIntegration::SetAsDefaultBrowser();
454 } else if (make_chrome_default_for_user) {
455 ShellIntegration::SetAsDefaultBrowser();
460 } // namespace
462 namespace first_run {
463 namespace internal {
465 FirstRunState g_first_run = FIRST_RUN_UNKNOWN;
467 void SetupMasterPrefsFromInstallPrefs(
468 const installer::MasterPreferences& install_prefs,
469 MasterPrefs* out_prefs) {
470 ConvertStringVectorToGURLVector(
471 install_prefs.GetFirstRunTabs(), &out_prefs->new_tabs);
473 install_prefs.GetInt(installer::master_preferences::kDistroPingDelay,
474 &out_prefs->ping_delay);
476 bool value = false;
477 if (install_prefs.GetBool(
478 installer::master_preferences::kDistroImportSearchPref, &value)) {
479 if (value) {
480 out_prefs->do_import_items |= importer::SEARCH_ENGINES;
481 } else {
482 out_prefs->dont_import_items |= importer::SEARCH_ENGINES;
486 // If we're suppressing the first-run bubble, set that preference now.
487 // Otherwise, wait until the user has completed first run to set it, so the
488 // user is guaranteed to see the bubble iff he or she has completed the first
489 // run process.
490 if (install_prefs.GetBool(
491 installer::master_preferences::kDistroSuppressFirstRunBubble,
492 &value) && value)
493 SetShowFirstRunBubblePref(FIRST_RUN_BUBBLE_SUPPRESS);
495 if (install_prefs.GetBool(
496 installer::master_preferences::kDistroImportHistoryPref,
497 &value)) {
498 if (value) {
499 out_prefs->do_import_items |= importer::HISTORY;
500 } else {
501 out_prefs->dont_import_items |= importer::HISTORY;
505 std::string not_used;
506 out_prefs->homepage_defined = install_prefs.GetString(
507 prefs::kHomePage, &not_used);
509 if (install_prefs.GetBool(
510 installer::master_preferences::kDistroImportHomePagePref,
511 &value)) {
512 if (value) {
513 out_prefs->do_import_items |= importer::HOME_PAGE;
514 } else {
515 out_prefs->dont_import_items |= importer::HOME_PAGE;
519 // Bookmarks are never imported unless specifically turned on.
520 if (install_prefs.GetBool(
521 installer::master_preferences::kDistroImportBookmarksPref,
522 &value)) {
523 if (value)
524 out_prefs->do_import_items |= importer::FAVORITES;
525 else
526 out_prefs->dont_import_items |= importer::FAVORITES;
529 if (install_prefs.GetBool(
530 installer::master_preferences::kMakeChromeDefaultForUser,
531 &value) && value) {
532 out_prefs->make_chrome_default_for_user = true;
535 if (install_prefs.GetBool(
536 installer::master_preferences::kSuppressFirstRunDefaultBrowserPrompt,
537 &value) && value) {
538 out_prefs->suppress_first_run_default_browser_prompt = true;
541 install_prefs.GetString(
542 installer::master_preferences::kDistroImportBookmarksFromFilePref,
543 &out_prefs->import_bookmarks_path);
545 out_prefs->compressed_variations_seed =
546 install_prefs.GetCompressedVariationsSeed();
547 out_prefs->variations_seed = install_prefs.GetVariationsSeed();
548 out_prefs->variations_seed_signature =
549 install_prefs.GetVariationsSeedSignature();
551 install_prefs.GetString(
552 installer::master_preferences::kDistroSuppressDefaultBrowserPromptPref,
553 &out_prefs->suppress_default_browser_prompt_for_version);
556 bool GetFirstRunSentinelFilePath(base::FilePath* path) {
557 base::FilePath user_data_dir;
558 if (!PathService::Get(chrome::DIR_USER_DATA, &user_data_dir))
559 return false;
560 *path = user_data_dir.Append(chrome::kFirstRunSentinel);
561 return true;
564 bool CreateSentinel() {
565 base::FilePath first_run_sentinel;
566 return GetFirstRunSentinelFilePath(&first_run_sentinel) &&
567 base::WriteFile(first_run_sentinel, "", 0) != -1;
570 // -- Platform-specific functions --
572 #if !defined(OS_LINUX) && !defined(OS_BSD)
573 bool IsOrganicFirstRun() {
574 std::string brand;
575 google_brand::GetBrand(&brand);
576 return google_brand::IsOrganicFirstRun(brand);
578 #endif
580 } // namespace internal
582 MasterPrefs::MasterPrefs()
583 : ping_delay(0),
584 homepage_defined(false),
585 do_import_items(0),
586 dont_import_items(0),
587 make_chrome_default_for_user(false),
588 suppress_first_run_default_browser_prompt(false) {
591 MasterPrefs::~MasterPrefs() {}
593 bool IsChromeFirstRun() {
594 if (internal::g_first_run == internal::FIRST_RUN_UNKNOWN) {
595 internal::g_first_run = internal::FIRST_RUN_FALSE;
596 const base::CommandLine* command_line =
597 base::CommandLine::ForCurrentProcess();
598 if (command_line->HasSwitch(switches::kForceFirstRun) ||
599 (!command_line->HasSwitch(switches::kNoFirstRun) &&
600 !internal::IsFirstRunSentinelPresent())) {
601 internal::g_first_run = internal::FIRST_RUN_TRUE;
604 return internal::g_first_run == internal::FIRST_RUN_TRUE;
607 #if defined(OS_MACOSX)
608 bool IsFirstRunSuppressed(const base::CommandLine& command_line) {
609 return command_line.HasSwitch(switches::kNoFirstRun);
611 #endif
613 void CreateSentinelIfNeeded() {
614 if (IsChromeFirstRun())
615 internal::CreateSentinel();
618 std::string GetPingDelayPrefName() {
619 return base::StringPrintf("%s.%s",
620 installer::master_preferences::kDistroDict,
621 installer::master_preferences::kDistroPingDelay);
624 void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
625 registry->RegisterIntegerPref(GetPingDelayPrefName().c_str(), 0);
628 bool SetShowFirstRunBubblePref(FirstRunBubbleOptions show_bubble_option) {
629 PrefService* local_state = g_browser_process->local_state();
630 if (!local_state)
631 return false;
632 if (local_state->GetInteger(
633 prefs::kShowFirstRunBubbleOption) != FIRST_RUN_BUBBLE_SUPPRESS) {
634 // Set the new state as long as the bubble wasn't explicitly suppressed
635 // already.
636 local_state->SetInteger(prefs::kShowFirstRunBubbleOption,
637 show_bubble_option);
639 return true;
642 void SetShouldShowWelcomePage() {
643 g_should_show_welcome_page = true;
646 bool ShouldShowWelcomePage() {
647 bool retval = g_should_show_welcome_page;
648 g_should_show_welcome_page = false;
649 return retval;
652 void SetShouldDoPersonalDataManagerFirstRun() {
653 g_should_do_autofill_personal_data_manager_first_run = true;
656 bool ShouldDoPersonalDataManagerFirstRun() {
657 bool retval = g_should_do_autofill_personal_data_manager_first_run;
658 g_should_do_autofill_personal_data_manager_first_run = false;
659 return retval;
662 void LogFirstRunMetric(FirstRunBubbleMetric metric) {
663 UMA_HISTOGRAM_ENUMERATION("FirstRun.SearchEngineBubble", metric,
664 NUM_FIRST_RUN_BUBBLE_METRICS);
667 void SetMasterPrefsPathForTesting(const base::FilePath& master_prefs) {
668 master_prefs_path_for_testing.Get() = master_prefs;
671 ProcessMasterPreferencesResult ProcessMasterPreferences(
672 const base::FilePath& user_data_dir,
673 MasterPrefs* out_prefs) {
674 DCHECK(!user_data_dir.empty());
676 scoped_ptr<installer::MasterPreferences> install_prefs(LoadMasterPrefs());
678 // Default value in case master preferences is missing or corrupt, or
679 // ping_delay is missing.
680 out_prefs->ping_delay = 90;
681 if (install_prefs.get()) {
682 if (!internal::ShowPostInstallEULAIfNeeded(install_prefs.get()))
683 return EULA_EXIT_NOW;
685 if (!chrome_prefs::InitializePrefsFromMasterPrefs(
686 profiles::GetDefaultProfileDir(user_data_dir),
687 install_prefs->master_dictionary())) {
688 DLOG(ERROR) << "Failed to initialize from master_preferences.";
691 DoDelayedInstallExtensionsIfNeeded(install_prefs.get());
693 internal::SetupMasterPrefsFromInstallPrefs(*install_prefs, out_prefs);
696 return FIRST_RUN_PROCEED;
699 void AutoImport(
700 Profile* profile,
701 bool homepage_defined,
702 int import_items,
703 int dont_import_items,
704 const std::string& import_bookmarks_path) {
705 base::FilePath local_state_path;
706 PathService::Get(chrome::FILE_LOCAL_STATE, &local_state_path);
707 bool local_state_file_exists = base::PathExists(local_state_path);
709 // It may be possible to do the if block below asynchronously. In which case,
710 // get rid of this RunLoop. http://crbug.com/366116.
711 base::RunLoop run_loop;
712 scoped_ptr<ImporterList> importer_list(new ImporterList());
713 importer_list->DetectSourceProfiles(
714 g_browser_process->GetApplicationLocale(),
715 false, // include_interactive_profiles?
716 run_loop.QuitClosure());
717 run_loop.Run();
719 // Do import if there is an available profile for us to import.
720 if (importer_list->count() > 0) {
721 if (internal::IsOrganicFirstRun()) {
722 // Home page is imported in organic builds only unless turned off or
723 // defined in master_preferences.
724 if (homepage_defined) {
725 dont_import_items |= importer::HOME_PAGE;
726 if (import_items & importer::HOME_PAGE)
727 import_items &= ~importer::HOME_PAGE;
729 // Search engines are not imported automatically in organic builds if the
730 // user already has a user preferences directory.
731 if (local_state_file_exists) {
732 dont_import_items |= importer::SEARCH_ENGINES;
733 if (import_items & importer::SEARCH_ENGINES)
734 import_items &= ~importer::SEARCH_ENGINES;
738 PrefService* user_prefs = profile->GetPrefs();
739 int items = 0;
741 SetImportItem(user_prefs,
742 prefs::kImportHistory,
743 import_items,
744 dont_import_items,
745 importer::HISTORY,
746 &items);
747 SetImportItem(user_prefs,
748 prefs::kImportHomepage,
749 import_items,
750 dont_import_items,
751 importer::HOME_PAGE,
752 &items);
753 SetImportItem(user_prefs,
754 prefs::kImportSearchEngine,
755 import_items,
756 dont_import_items,
757 importer::SEARCH_ENGINES,
758 &items);
759 SetImportItem(user_prefs,
760 prefs::kImportBookmarks,
761 import_items,
762 dont_import_items,
763 importer::FAVORITES,
764 &items);
766 // Deletes itself.
767 ExternalProcessImporterHost* importer_host =
768 new ExternalProcessImporterHost;
770 // Don't show the warning dialog if import fails.
771 importer_host->set_headless();
773 importer::LogImporterUseToMetrics(
774 "AutoImport", importer_list->GetSourceProfileAt(0).importer_type);
776 ImportSettings(profile, importer_host, importer_list.Pass(), items);
779 if (!import_bookmarks_path.empty()) {
780 // Deletes itself.
781 ExternalProcessImporterHost* file_importer_host =
782 new ExternalProcessImporterHost;
783 file_importer_host->set_headless();
785 ImportFromFile(profile, file_importer_host, import_bookmarks_path);
788 content::RecordAction(UserMetricsAction("FirstRunDef_Accept"));
790 g_auto_import_state |= AUTO_IMPORT_CALLED;
793 void DoPostImportTasks(Profile* profile, bool make_chrome_default_for_user) {
794 // Only set default browser after import as auto import relies on the current
795 // default browser to know what to import from.
796 ProcessDefaultBrowserPolicy(make_chrome_default_for_user);
798 // Display the first run bubble if there is a default search provider.
799 TemplateURLService* template_url =
800 TemplateURLServiceFactory::GetForProfile(profile);
801 if (template_url && template_url->GetDefaultSearchProvider())
802 FirstRunBubbleLauncher::ShowFirstRunBubbleSoon();
803 SetShouldShowWelcomePage();
804 SetShouldDoPersonalDataManagerFirstRun();
806 internal::DoPostImportPlatformSpecificTasks(profile);
809 uint16 auto_import_state() {
810 return g_auto_import_state;
813 } // namespace first_run