Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / first_run / first_run.cc
blob0e88b9fb6fbcab8446c6578009906c8ec06debac
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 // Suppress the first run bubble if 'make chrome metro' flow is showing.
380 if (contents->GetURL().host() == chrome::kChromeUIMetroFlowHost)
381 return;
383 // Suppress the first run bubble if the NTP sync promo bubble is showing
384 // or if sign in is in progress.
385 if (contents->GetURL().host() == chrome::kChromeUINewTabHost) {
386 Profile* profile =
387 Profile::FromBrowserContext(contents->GetBrowserContext());
388 SigninManagerBase* manager =
389 SigninManagerFactory::GetForProfile(profile);
390 bool signin_in_progress = manager && manager->AuthInProgress();
391 bool is_promo_bubble_visible =
392 profile->GetPrefs()->GetBoolean(prefs::kSignInPromoShowNTPBubble);
394 if (is_promo_bubble_visible || signin_in_progress)
395 return;
399 // Suppress the first run bubble if a global error bubble is pending.
400 GlobalErrorService* global_error_service =
401 GlobalErrorServiceFactory::GetForProfile(browser->profile());
402 if (global_error_service->GetFirstGlobalErrorWithBubbleView() != NULL)
403 return;
405 // Reset the preference and notifications to avoid showing the bubble again.
406 prefs->SetInteger(prefs::kShowFirstRunBubbleOption,
407 first_run::FIRST_RUN_BUBBLE_DONT_SHOW);
409 // Show the bubble now and destroy this bubble launcher.
410 browser->ShowFirstRunBubble();
411 delete this;
414 static base::LazyInstance<base::FilePath> master_prefs_path_for_testing
415 = LAZY_INSTANCE_INITIALIZER;
417 // Loads master preferences from the master preference file into the installer
418 // master preferences. Returns the pointer to installer::MasterPreferences
419 // object if successful; otherwise, returns NULL.
420 installer::MasterPreferences* LoadMasterPrefs() {
421 base::FilePath master_prefs_path;
422 if (!master_prefs_path_for_testing.Get().empty())
423 master_prefs_path = master_prefs_path_for_testing.Get();
424 else
425 master_prefs_path = base::FilePath(first_run::internal::MasterPrefsPath());
426 if (master_prefs_path.empty())
427 return NULL;
428 installer::MasterPreferences* install_prefs =
429 new installer::MasterPreferences(master_prefs_path);
430 if (!install_prefs->read_from_file()) {
431 delete install_prefs;
432 return NULL;
435 return install_prefs;
438 // Makes chrome the user's default browser according to policy or
439 // |make_chrome_default_for_user| if no policy is set.
440 void ProcessDefaultBrowserPolicy(bool make_chrome_default_for_user) {
441 // Only proceed if chrome can be made default unattended. The interactive case
442 // (Windows 8+) is handled by the first run default browser prompt.
443 if (ShellIntegration::CanSetAsDefaultBrowser() ==
444 ShellIntegration::SET_DEFAULT_UNATTENDED) {
445 // The policy has precedence over the user's choice.
446 if (g_browser_process->local_state()->IsManagedPreference(
447 prefs::kDefaultBrowserSettingEnabled)) {
448 if (g_browser_process->local_state()->GetBoolean(
449 prefs::kDefaultBrowserSettingEnabled)) {
450 ShellIntegration::SetAsDefaultBrowser();
452 } else if (make_chrome_default_for_user) {
453 ShellIntegration::SetAsDefaultBrowser();
458 } // namespace
460 namespace first_run {
461 namespace internal {
463 FirstRunState g_first_run = FIRST_RUN_UNKNOWN;
465 void SetupMasterPrefsFromInstallPrefs(
466 const installer::MasterPreferences& install_prefs,
467 MasterPrefs* out_prefs) {
468 ConvertStringVectorToGURLVector(
469 install_prefs.GetFirstRunTabs(), &out_prefs->new_tabs);
471 install_prefs.GetInt(installer::master_preferences::kDistroPingDelay,
472 &out_prefs->ping_delay);
474 bool value = false;
475 if (install_prefs.GetBool(
476 installer::master_preferences::kDistroImportSearchPref, &value)) {
477 if (value) {
478 out_prefs->do_import_items |= importer::SEARCH_ENGINES;
479 } else {
480 out_prefs->dont_import_items |= importer::SEARCH_ENGINES;
484 // If we're suppressing the first-run bubble, set that preference now.
485 // Otherwise, wait until the user has completed first run to set it, so the
486 // user is guaranteed to see the bubble iff he or she has completed the first
487 // run process.
488 if (install_prefs.GetBool(
489 installer::master_preferences::kDistroSuppressFirstRunBubble,
490 &value) && value)
491 SetShowFirstRunBubblePref(FIRST_RUN_BUBBLE_SUPPRESS);
493 if (install_prefs.GetBool(
494 installer::master_preferences::kDistroImportHistoryPref,
495 &value)) {
496 if (value) {
497 out_prefs->do_import_items |= importer::HISTORY;
498 } else {
499 out_prefs->dont_import_items |= importer::HISTORY;
503 std::string not_used;
504 out_prefs->homepage_defined = install_prefs.GetString(
505 prefs::kHomePage, &not_used);
507 if (install_prefs.GetBool(
508 installer::master_preferences::kDistroImportHomePagePref,
509 &value)) {
510 if (value) {
511 out_prefs->do_import_items |= importer::HOME_PAGE;
512 } else {
513 out_prefs->dont_import_items |= importer::HOME_PAGE;
517 // Bookmarks are never imported unless specifically turned on.
518 if (install_prefs.GetBool(
519 installer::master_preferences::kDistroImportBookmarksPref,
520 &value)) {
521 if (value)
522 out_prefs->do_import_items |= importer::FAVORITES;
523 else
524 out_prefs->dont_import_items |= importer::FAVORITES;
527 if (install_prefs.GetBool(
528 installer::master_preferences::kMakeChromeDefaultForUser,
529 &value) && value) {
530 out_prefs->make_chrome_default_for_user = true;
533 if (install_prefs.GetBool(
534 installer::master_preferences::kSuppressFirstRunDefaultBrowserPrompt,
535 &value) && value) {
536 out_prefs->suppress_first_run_default_browser_prompt = true;
539 install_prefs.GetString(
540 installer::master_preferences::kDistroImportBookmarksFromFilePref,
541 &out_prefs->import_bookmarks_path);
543 out_prefs->compressed_variations_seed =
544 install_prefs.GetCompressedVariationsSeed();
545 out_prefs->variations_seed = install_prefs.GetVariationsSeed();
546 out_prefs->variations_seed_signature =
547 install_prefs.GetVariationsSeedSignature();
549 install_prefs.GetString(
550 installer::master_preferences::kDistroSuppressDefaultBrowserPromptPref,
551 &out_prefs->suppress_default_browser_prompt_for_version);
554 bool GetFirstRunSentinelFilePath(base::FilePath* path) {
555 base::FilePath user_data_dir;
556 if (!PathService::Get(chrome::DIR_USER_DATA, &user_data_dir))
557 return false;
558 *path = user_data_dir.Append(chrome::kFirstRunSentinel);
559 return true;
562 bool CreateSentinel() {
563 base::FilePath first_run_sentinel;
564 return GetFirstRunSentinelFilePath(&first_run_sentinel) &&
565 base::WriteFile(first_run_sentinel, "", 0) != -1;
568 // -- Platform-specific functions --
570 #if !defined(OS_LINUX) && !defined(OS_BSD)
571 bool IsOrganicFirstRun() {
572 std::string brand;
573 google_brand::GetBrand(&brand);
574 return google_brand::IsOrganicFirstRun(brand);
576 #endif
578 } // namespace internal
580 MasterPrefs::MasterPrefs()
581 : ping_delay(0),
582 homepage_defined(false),
583 do_import_items(0),
584 dont_import_items(0),
585 make_chrome_default_for_user(false),
586 suppress_first_run_default_browser_prompt(false) {
589 MasterPrefs::~MasterPrefs() {}
591 bool IsChromeFirstRun() {
592 if (internal::g_first_run == internal::FIRST_RUN_UNKNOWN) {
593 internal::g_first_run = internal::FIRST_RUN_FALSE;
594 const base::CommandLine* command_line =
595 base::CommandLine::ForCurrentProcess();
596 if (command_line->HasSwitch(switches::kForceFirstRun) ||
597 (!command_line->HasSwitch(switches::kNoFirstRun) &&
598 !internal::IsFirstRunSentinelPresent())) {
599 internal::g_first_run = internal::FIRST_RUN_TRUE;
602 return internal::g_first_run == internal::FIRST_RUN_TRUE;
605 #if defined(OS_MACOSX)
606 bool IsFirstRunSuppressed(const base::CommandLine& command_line) {
607 return command_line.HasSwitch(switches::kNoFirstRun);
609 #endif
611 void CreateSentinelIfNeeded() {
612 if (IsChromeFirstRun())
613 internal::CreateSentinel();
616 std::string GetPingDelayPrefName() {
617 return base::StringPrintf("%s.%s",
618 installer::master_preferences::kDistroDict,
619 installer::master_preferences::kDistroPingDelay);
622 void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
623 registry->RegisterIntegerPref(
624 GetPingDelayPrefName().c_str(),
626 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
629 bool SetShowFirstRunBubblePref(FirstRunBubbleOptions show_bubble_option) {
630 PrefService* local_state = g_browser_process->local_state();
631 if (!local_state)
632 return false;
633 if (local_state->GetInteger(
634 prefs::kShowFirstRunBubbleOption) != FIRST_RUN_BUBBLE_SUPPRESS) {
635 // Set the new state as long as the bubble wasn't explicitly suppressed
636 // already.
637 local_state->SetInteger(prefs::kShowFirstRunBubbleOption,
638 show_bubble_option);
640 return true;
643 void SetShouldShowWelcomePage() {
644 g_should_show_welcome_page = true;
647 bool ShouldShowWelcomePage() {
648 bool retval = g_should_show_welcome_page;
649 g_should_show_welcome_page = false;
650 return retval;
653 void SetShouldDoPersonalDataManagerFirstRun() {
654 g_should_do_autofill_personal_data_manager_first_run = true;
657 bool ShouldDoPersonalDataManagerFirstRun() {
658 bool retval = g_should_do_autofill_personal_data_manager_first_run;
659 g_should_do_autofill_personal_data_manager_first_run = false;
660 return retval;
663 void LogFirstRunMetric(FirstRunBubbleMetric metric) {
664 UMA_HISTOGRAM_ENUMERATION("FirstRun.SearchEngineBubble", metric,
665 NUM_FIRST_RUN_BUBBLE_METRICS);
668 void SetMasterPrefsPathForTesting(const base::FilePath& master_prefs) {
669 master_prefs_path_for_testing.Get() = master_prefs;
672 ProcessMasterPreferencesResult ProcessMasterPreferences(
673 const base::FilePath& user_data_dir,
674 MasterPrefs* out_prefs) {
675 DCHECK(!user_data_dir.empty());
677 scoped_ptr<installer::MasterPreferences> install_prefs(LoadMasterPrefs());
679 // Default value in case master preferences is missing or corrupt, or
680 // ping_delay is missing.
681 out_prefs->ping_delay = 90;
682 if (install_prefs.get()) {
683 if (!internal::ShowPostInstallEULAIfNeeded(install_prefs.get()))
684 return EULA_EXIT_NOW;
686 if (!chrome_prefs::InitializePrefsFromMasterPrefs(
687 profiles::GetDefaultProfileDir(user_data_dir),
688 install_prefs->master_dictionary())) {
689 DLOG(ERROR) << "Failed to initialize from master_preferences.";
692 DoDelayedInstallExtensionsIfNeeded(install_prefs.get());
694 internal::SetupMasterPrefsFromInstallPrefs(*install_prefs, out_prefs);
697 return FIRST_RUN_PROCEED;
700 void AutoImport(
701 Profile* profile,
702 bool homepage_defined,
703 int import_items,
704 int dont_import_items,
705 const std::string& import_bookmarks_path) {
706 base::FilePath local_state_path;
707 PathService::Get(chrome::FILE_LOCAL_STATE, &local_state_path);
708 bool local_state_file_exists = base::PathExists(local_state_path);
710 // It may be possible to do the if block below asynchronously. In which case,
711 // get rid of this RunLoop. http://crbug.com/366116.
712 base::RunLoop run_loop;
713 scoped_ptr<ImporterList> importer_list(new ImporterList());
714 importer_list->DetectSourceProfiles(
715 g_browser_process->GetApplicationLocale(),
716 false, // include_interactive_profiles?
717 run_loop.QuitClosure());
718 run_loop.Run();
720 // Do import if there is an available profile for us to import.
721 if (importer_list->count() > 0) {
722 if (internal::IsOrganicFirstRun()) {
723 // Home page is imported in organic builds only unless turned off or
724 // defined in master_preferences.
725 if (homepage_defined) {
726 dont_import_items |= importer::HOME_PAGE;
727 if (import_items & importer::HOME_PAGE)
728 import_items &= ~importer::HOME_PAGE;
730 // Search engines are not imported automatically in organic builds if the
731 // user already has a user preferences directory.
732 if (local_state_file_exists) {
733 dont_import_items |= importer::SEARCH_ENGINES;
734 if (import_items & importer::SEARCH_ENGINES)
735 import_items &= ~importer::SEARCH_ENGINES;
739 PrefService* user_prefs = profile->GetPrefs();
740 int items = 0;
742 SetImportItem(user_prefs,
743 prefs::kImportHistory,
744 import_items,
745 dont_import_items,
746 importer::HISTORY,
747 &items);
748 SetImportItem(user_prefs,
749 prefs::kImportHomepage,
750 import_items,
751 dont_import_items,
752 importer::HOME_PAGE,
753 &items);
754 SetImportItem(user_prefs,
755 prefs::kImportSearchEngine,
756 import_items,
757 dont_import_items,
758 importer::SEARCH_ENGINES,
759 &items);
760 SetImportItem(user_prefs,
761 prefs::kImportBookmarks,
762 import_items,
763 dont_import_items,
764 importer::FAVORITES,
765 &items);
767 // Deletes itself.
768 ExternalProcessImporterHost* importer_host =
769 new ExternalProcessImporterHost;
771 // Don't show the warning dialog if import fails.
772 importer_host->set_headless();
774 importer::LogImporterUseToMetrics(
775 "AutoImport", importer_list->GetSourceProfileAt(0).importer_type);
777 ImportSettings(profile, importer_host, importer_list.Pass(), items);
780 if (!import_bookmarks_path.empty()) {
781 // Deletes itself.
782 ExternalProcessImporterHost* file_importer_host =
783 new ExternalProcessImporterHost;
784 file_importer_host->set_headless();
786 ImportFromFile(profile, file_importer_host, import_bookmarks_path);
789 content::RecordAction(UserMetricsAction("FirstRunDef_Accept"));
791 g_auto_import_state |= AUTO_IMPORT_CALLED;
794 void DoPostImportTasks(Profile* profile, bool make_chrome_default_for_user) {
795 // Only set default browser after import as auto import relies on the current
796 // default browser to know what to import from.
797 ProcessDefaultBrowserPolicy(make_chrome_default_for_user);
799 // Display the first run bubble if there is a default search provider.
800 TemplateURLService* template_url =
801 TemplateURLServiceFactory::GetForProfile(profile);
802 if (template_url && template_url->GetDefaultSearchProvider())
803 FirstRunBubbleLauncher::ShowFirstRunBubbleSoon();
804 SetShouldShowWelcomePage();
805 SetShouldDoPersonalDataManagerFirstRun();
807 internal::DoPostImportPlatformSpecificTasks(profile);
810 uint16 auto_import_state() {
811 return g_auto_import_state;
814 } // namespace first_run