| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/first_run/first_run.h" | 5 #include "chrome/browser/first_run/first_run.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 | 193 |
| 194 void DoDelayedInstallExtensionsIfNeeded( | 194 void DoDelayedInstallExtensionsIfNeeded( |
| 195 installer::MasterPreferences* install_prefs) { | 195 installer::MasterPreferences* install_prefs) { |
| 196 base::DictionaryValue* extensions = 0; | 196 base::DictionaryValue* extensions = 0; |
| 197 if (install_prefs->GetExtensionsBlock(&extensions)) { | 197 if (install_prefs->GetExtensionsBlock(&extensions)) { |
| 198 DVLOG(1) << "Extensions block found in master preferences"; | 198 DVLOG(1) << "Extensions block found in master preferences"; |
| 199 DoDelayedInstallExtensions(); | 199 DoDelayedInstallExtensions(); |
| 200 } | 200 } |
| 201 } | 201 } |
| 202 | 202 |
| 203 // Sets the |items| bitfield according to whether the import data specified by | |
| 204 // |import_type| should be be auto imported or not. | |
| 205 void SetImportItem(PrefService* user_prefs, | |
| 206 const char* pref_path, | |
| 207 int import_items, | |
| 208 int dont_import_items, | |
| 209 importer::ImportItem import_type, | |
| 210 int* items) { | |
| 211 // Work out whether an item is to be imported according to what is specified | |
| 212 // in master preferences. | |
| 213 bool should_import = false; | |
| 214 bool master_pref_set = | |
| 215 ((import_items | dont_import_items) & import_type) != 0; | |
| 216 bool master_pref = ((import_items & ~dont_import_items) & import_type) != 0; | |
| 217 | |
| 218 if (import_type == importer::HISTORY || | |
| 219 (import_type != importer::FAVORITES && | |
| 220 first_run::internal::IsOrganicFirstRun())) { | |
| 221 // History is always imported unless turned off in master_preferences. | |
| 222 // Search engines and home page are imported in organic builds only | |
| 223 // unless turned off in master_preferences. | |
| 224 should_import = !master_pref_set || master_pref; | |
| 225 } else { | |
| 226 // Bookmarks are never imported, unless turned on in master_preferences. | |
| 227 // Search engine and home page import behaviour is similar in non organic | |
| 228 // builds. | |
| 229 should_import = master_pref_set && master_pref; | |
| 230 } | |
| 231 | |
| 232 // If an import policy is set, import items according to policy. If no master | |
| 233 // preference is set, but a corresponding recommended policy is set, import | |
| 234 // item according to recommended policy. If both a master preference and a | |
| 235 // recommended policy is set, the master preference wins. If neither | |
| 236 // recommended nor managed policies are set, import item according to what we | |
| 237 // worked out above. | |
| 238 if (master_pref_set) | |
| 239 user_prefs->SetBoolean(pref_path, should_import); | |
| 240 | |
| 241 if (!user_prefs->FindPreference(pref_path)->IsDefaultValue()) { | |
| 242 if (user_prefs->GetBoolean(pref_path)) | |
| 243 *items |= import_type; | |
| 244 } else { | |
| 245 // no policy (recommended or managed) is set | |
| 246 if (should_import) | |
| 247 *items |= import_type; | |
| 248 } | |
| 249 | |
| 250 user_prefs->ClearPref(pref_path); | |
| 251 } | |
| 252 | |
| 253 // Launches the import, via |importer_host|, from |source_profile| into | 203 // Launches the import, via |importer_host|, from |source_profile| into |
| 254 // |target_profile| for the items specified in the |items_to_import| bitfield. | 204 // |target_profile| for the items specified in the |items_to_import| bitfield. |
| 255 // This may be done in a separate process depending on the platform, but it will | 205 // This may be done in a separate process depending on the platform, but it will |
| 256 // always block until done. | 206 // always block until done. |
| 257 void ImportFromSourceProfile(const importer::SourceProfile& source_profile, | 207 void ImportFromSourceProfile(const importer::SourceProfile& source_profile, |
| 258 Profile* target_profile, | 208 Profile* target_profile, |
| 259 uint16_t items_to_import) { | 209 uint16_t items_to_import) { |
| 260 // Deletes itself. | 210 // Deletes itself. |
| 261 ExternalProcessImporterHost* importer_host = | 211 ExternalProcessImporterHost* importer_host = |
| 262 new ExternalProcessImporterHost; | 212 new ExternalProcessImporterHost; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 293 #endif | 243 #endif |
| 294 source_profile.source_path = base::FilePath(import_bookmarks_path_str); | 244 source_profile.source_path = base::FilePath(import_bookmarks_path_str); |
| 295 | 245 |
| 296 ImportFromSourceProfile(source_profile, profile, importer::FAVORITES); | 246 ImportFromSourceProfile(source_profile, profile, importer::FAVORITES); |
| 297 g_auto_import_state |= first_run::AUTO_IMPORT_BOOKMARKS_FILE_IMPORTED; | 247 g_auto_import_state |= first_run::AUTO_IMPORT_BOOKMARKS_FILE_IMPORTED; |
| 298 } | 248 } |
| 299 | 249 |
| 300 // Imports settings from the first profile in |importer_list|. | 250 // Imports settings from the first profile in |importer_list|. |
| 301 void ImportSettings(Profile* profile, | 251 void ImportSettings(Profile* profile, |
| 302 std::unique_ptr<ImporterList> importer_list, | 252 std::unique_ptr<ImporterList> importer_list, |
| 303 int items_to_import) { | 253 uint16_t items_to_import) { |
| 254 DCHECK(items_to_import); |
| 304 const importer::SourceProfile& source_profile = | 255 const importer::SourceProfile& source_profile = |
| 305 importer_list->GetSourceProfileAt(0); | 256 importer_list->GetSourceProfileAt(0); |
| 306 // If no items to import then skip entirely. | |
| 307 if (!items_to_import) | |
| 308 return; | |
| 309 | 257 |
| 310 // Ensure that importers aren't requested to import items that they do not | 258 // Ensure that importers aren't requested to import items that they do not |
| 311 // support. If there is no overlap, skip. | 259 // support. If there is no overlap, skip. |
| 312 items_to_import &= source_profile.services_supported; | 260 items_to_import &= source_profile.services_supported; |
| 313 if (items_to_import) { | 261 if (items_to_import) |
| 314 ImportFromSourceProfile(source_profile, profile, items_to_import); | 262 ImportFromSourceProfile(source_profile, profile, items_to_import); |
| 315 } | |
| 316 | 263 |
| 317 g_auto_import_state |= first_run::AUTO_IMPORT_PROFILE_IMPORTED; | 264 g_auto_import_state |= first_run::AUTO_IMPORT_PROFILE_IMPORTED; |
| 318 } | 265 } |
| 319 | 266 |
| 320 GURL UrlFromString(const std::string& in) { | 267 GURL UrlFromString(const std::string& in) { |
| 321 return GURL(in); | 268 return GURL(in); |
| 322 } | 269 } |
| 323 | 270 |
| 324 void ConvertStringVectorToGURLVector( | 271 void ConvertStringVectorToGURLVector( |
| 325 const std::vector<std::string>& src, | 272 const std::vector<std::string>& src, |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 489 | 436 |
| 490 namespace first_run { | 437 namespace first_run { |
| 491 namespace internal { | 438 namespace internal { |
| 492 | 439 |
| 493 void SetupMasterPrefsFromInstallPrefs( | 440 void SetupMasterPrefsFromInstallPrefs( |
| 494 const installer::MasterPreferences& install_prefs, | 441 const installer::MasterPreferences& install_prefs, |
| 495 MasterPrefs* out_prefs) { | 442 MasterPrefs* out_prefs) { |
| 496 ConvertStringVectorToGURLVector( | 443 ConvertStringVectorToGURLVector( |
| 497 install_prefs.GetFirstRunTabs(), &out_prefs->new_tabs); | 444 install_prefs.GetFirstRunTabs(), &out_prefs->new_tabs); |
| 498 | 445 |
| 499 bool value = false; | |
| 500 if (install_prefs.GetBool( | |
| 501 installer::master_preferences::kDistroImportSearchPref, &value)) { | |
| 502 if (value) { | |
| 503 out_prefs->do_import_items |= importer::SEARCH_ENGINES; | |
| 504 } else { | |
| 505 out_prefs->dont_import_items |= importer::SEARCH_ENGINES; | |
| 506 } | |
| 507 } | |
| 508 | |
| 509 // If we're suppressing the first-run bubble, set that preference now. | 446 // If we're suppressing the first-run bubble, set that preference now. |
| 510 // Otherwise, wait until the user has completed first run to set it, so the | 447 // Otherwise, wait until the user has completed first run to set it, so the |
| 511 // user is guaranteed to see the bubble iff they have completed the first run | 448 // user is guaranteed to see the bubble iff they have completed the first run |
| 512 // process. | 449 // process. |
| 450 bool value = false; |
| 513 if (install_prefs.GetBool( | 451 if (install_prefs.GetBool( |
| 514 installer::master_preferences::kDistroSuppressFirstRunBubble, | 452 installer::master_preferences::kDistroSuppressFirstRunBubble, |
| 515 &value) && value) | 453 &value) && |
| 454 value) { |
| 516 SetShowFirstRunBubblePref(FIRST_RUN_BUBBLE_SUPPRESS); | 455 SetShowFirstRunBubblePref(FIRST_RUN_BUBBLE_SUPPRESS); |
| 517 | |
| 518 if (install_prefs.GetBool( | |
| 519 installer::master_preferences::kDistroImportHistoryPref, | |
| 520 &value)) { | |
| 521 if (value) { | |
| 522 out_prefs->do_import_items |= importer::HISTORY; | |
| 523 } else { | |
| 524 out_prefs->dont_import_items |= importer::HISTORY; | |
| 525 } | |
| 526 } | |
| 527 | |
| 528 std::string not_used; | |
| 529 out_prefs->homepage_defined = install_prefs.GetString( | |
| 530 prefs::kHomePage, ¬_used); | |
| 531 | |
| 532 if (install_prefs.GetBool( | |
| 533 installer::master_preferences::kDistroImportHomePagePref, | |
| 534 &value)) { | |
| 535 if (value) { | |
| 536 out_prefs->do_import_items |= importer::HOME_PAGE; | |
| 537 } else { | |
| 538 out_prefs->dont_import_items |= importer::HOME_PAGE; | |
| 539 } | |
| 540 } | |
| 541 | |
| 542 // Bookmarks are never imported unless specifically turned on. | |
| 543 if (install_prefs.GetBool( | |
| 544 installer::master_preferences::kDistroImportBookmarksPref, | |
| 545 &value)) { | |
| 546 if (value) | |
| 547 out_prefs->do_import_items |= importer::FAVORITES; | |
| 548 else | |
| 549 out_prefs->dont_import_items |= importer::FAVORITES; | |
| 550 } | 456 } |
| 551 | 457 |
| 552 if (install_prefs.GetBool( | 458 if (install_prefs.GetBool( |
| 553 installer::master_preferences::kMakeChromeDefaultForUser, | 459 installer::master_preferences::kMakeChromeDefaultForUser, |
| 554 &value) && value) { | 460 &value) && value) { |
| 555 out_prefs->make_chrome_default_for_user = true; | 461 out_prefs->make_chrome_default_for_user = true; |
| 556 } | 462 } |
| 557 | 463 |
| 558 if (install_prefs.GetBool( | 464 if (install_prefs.GetBool( |
| 559 installer::master_preferences::kSuppressFirstRunDefaultBrowserPrompt, | 465 installer::master_preferences::kSuppressFirstRunDefaultBrowserPrompt, |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 610 FirstRunState DetermineFirstRunState(bool has_sentinel, | 516 FirstRunState DetermineFirstRunState(bool has_sentinel, |
| 611 bool force_first_run, | 517 bool force_first_run, |
| 612 bool no_first_run) { | 518 bool no_first_run) { |
| 613 return (force_first_run || (!has_sentinel && !no_first_run)) | 519 return (force_first_run || (!has_sentinel && !no_first_run)) |
| 614 ? FIRST_RUN_TRUE | 520 ? FIRST_RUN_TRUE |
| 615 : FIRST_RUN_FALSE; | 521 : FIRST_RUN_FALSE; |
| 616 } | 522 } |
| 617 | 523 |
| 618 } // namespace internal | 524 } // namespace internal |
| 619 | 525 |
| 620 MasterPrefs::MasterPrefs() | 526 MasterPrefs::MasterPrefs() = default; |
| 621 : homepage_defined(false), | |
| 622 do_import_items(0), | |
| 623 dont_import_items(0), | |
| 624 make_chrome_default_for_user(false), | |
| 625 suppress_first_run_default_browser_prompt(false), | |
| 626 welcome_page_on_os_upgrade_enabled(true) {} | |
| 627 | 527 |
| 628 MasterPrefs::~MasterPrefs() {} | 528 MasterPrefs::~MasterPrefs() = default; |
| 629 | 529 |
| 630 bool IsChromeFirstRun() { | 530 bool IsChromeFirstRun() { |
| 631 if (g_first_run == internal::FIRST_RUN_UNKNOWN) { | 531 if (g_first_run == internal::FIRST_RUN_UNKNOWN) { |
| 632 const base::CommandLine* command_line = | 532 const base::CommandLine* command_line = |
| 633 base::CommandLine::ForCurrentProcess(); | 533 base::CommandLine::ForCurrentProcess(); |
| 634 g_first_run = internal::DetermineFirstRunState( | 534 g_first_run = internal::DetermineFirstRunState( |
| 635 internal::IsFirstRunSentinelPresent(), | 535 internal::IsFirstRunSentinelPresent(), |
| 636 command_line->HasSwitch(switches::kForceFirstRun), | 536 command_line->HasSwitch(switches::kForceFirstRun), |
| 637 command_line->HasSwitch(switches::kNoFirstRun)); | 537 command_line->HasSwitch(switches::kNoFirstRun)); |
| 638 } | 538 } |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 756 DoDelayedInstallExtensionsIfNeeded(install_prefs.get()); | 656 DoDelayedInstallExtensionsIfNeeded(install_prefs.get()); |
| 757 | 657 |
| 758 internal::SetupMasterPrefsFromInstallPrefs(*install_prefs, out_prefs); | 658 internal::SetupMasterPrefsFromInstallPrefs(*install_prefs, out_prefs); |
| 759 } | 659 } |
| 760 | 660 |
| 761 return FIRST_RUN_PROCEED; | 661 return FIRST_RUN_PROCEED; |
| 762 } | 662 } |
| 763 | 663 |
| 764 void AutoImport( | 664 void AutoImport( |
| 765 Profile* profile, | 665 Profile* profile, |
| 766 bool homepage_defined, | |
| 767 int import_items, | |
| 768 int dont_import_items, | |
| 769 const std::string& import_bookmarks_path) { | 666 const std::string& import_bookmarks_path) { |
| 770 base::FilePath local_state_path; | 667 g_auto_import_state |= AUTO_IMPORT_CALLED; |
| 771 PathService::Get(chrome::FILE_LOCAL_STATE, &local_state_path); | |
| 772 bool local_state_file_exists = base::PathExists(local_state_path); | |
| 773 | 668 |
| 774 // It may be possible to do the if block below asynchronously. In which case, | 669 // Use |profile|'s PrefService to determine what to import. It will reflect in |
| 775 // get rid of this RunLoop. http://crbug.com/366116. | 670 // order: |
| 776 base::RunLoop run_loop; | 671 // 1) Policies. |
| 777 std::unique_ptr<ImporterList> importer_list(new ImporterList()); | 672 // 2) Master preferences (used to initialize user prefs in |
| 778 importer_list->DetectSourceProfiles( | 673 // ProcessMasterPreferences()). |
| 779 g_browser_process->GetApplicationLocale(), | 674 // 3) Recommended policies. |
| 780 false, // include_interactive_profiles? | 675 // 4) Registered default. |
| 781 run_loop.QuitClosure()); | 676 PrefService* prefs = profile->GetPrefs(); |
| 782 run_loop.Run(); | 677 uint16_t items_to_import = 0; |
| 678 static constexpr struct { |
| 679 const char* pref_path; |
| 680 importer::ImportItem bit; |
| 681 } kImportItems[] = { |
| 682 {prefs::kImportAutofillFormData, importer::AUTOFILL_FORM_DATA}, |
| 683 {prefs::kImportBookmarks, importer::FAVORITES}, |
| 684 {prefs::kImportHistory, importer::HISTORY}, |
| 685 {prefs::kImportHomepage, importer::HOME_PAGE}, |
| 686 {prefs::kImportSavedPasswords, importer::PASSWORDS}, |
| 687 {prefs::kImportSearchEngine, importer::SEARCH_ENGINES}, |
| 688 }; |
| 783 | 689 |
| 784 // Do import if there is an available profile for us to import. | 690 for (const auto& import_item : kImportItems) { |
| 785 if (importer_list->count() > 0) { | 691 if (prefs->GetBoolean(import_item.pref_path)) |
| 786 if (internal::IsOrganicFirstRun()) { | 692 items_to_import |= import_item.bit; |
| 787 // Home page is imported in organic builds only unless turned off or | |
| 788 // defined in master_preferences. | |
| 789 if (homepage_defined) { | |
| 790 dont_import_items |= importer::HOME_PAGE; | |
| 791 if (import_items & importer::HOME_PAGE) | |
| 792 import_items &= ~importer::HOME_PAGE; | |
| 793 } | |
| 794 // Search engines are not imported automatically in organic builds if the | |
| 795 // user already has a user preferences directory. | |
| 796 if (local_state_file_exists) { | |
| 797 dont_import_items |= importer::SEARCH_ENGINES; | |
| 798 if (import_items & importer::SEARCH_ENGINES) | |
| 799 import_items &= ~importer::SEARCH_ENGINES; | |
| 800 } | |
| 801 } | |
| 802 | |
| 803 PrefService* user_prefs = profile->GetPrefs(); | |
| 804 int items = 0; | |
| 805 | |
| 806 SetImportItem(user_prefs, | |
| 807 prefs::kImportHistory, | |
| 808 import_items, | |
| 809 dont_import_items, | |
| 810 importer::HISTORY, | |
| 811 &items); | |
| 812 SetImportItem(user_prefs, | |
| 813 prefs::kImportHomepage, | |
| 814 import_items, | |
| 815 dont_import_items, | |
| 816 importer::HOME_PAGE, | |
| 817 &items); | |
| 818 SetImportItem(user_prefs, | |
| 819 prefs::kImportSearchEngine, | |
| 820 import_items, | |
| 821 dont_import_items, | |
| 822 importer::SEARCH_ENGINES, | |
| 823 &items); | |
| 824 SetImportItem(user_prefs, | |
| 825 prefs::kImportBookmarks, | |
| 826 import_items, | |
| 827 dont_import_items, | |
| 828 importer::FAVORITES, | |
| 829 &items); | |
| 830 | |
| 831 importer::LogImporterUseToMetrics( | |
| 832 "AutoImport", importer_list->GetSourceProfileAt(0).importer_type); | |
| 833 | |
| 834 ImportSettings(profile, std::move(importer_list), items); | |
| 835 } | 693 } |
| 836 | 694 |
| 837 if (!import_bookmarks_path.empty()) { | 695 if (items_to_import) { |
| 838 ImportFromFile(profile, import_bookmarks_path); | 696 // It may be possible to do the if block below asynchronously. In which |
| 697 // case, get rid of this RunLoop. http://crbug.com/366116. |
| 698 base::RunLoop run_loop; |
| 699 auto importer_list = base::MakeUnique<ImporterList>(); |
| 700 importer_list->DetectSourceProfiles( |
| 701 g_browser_process->GetApplicationLocale(), |
| 702 false, // include_interactive_profiles? |
| 703 run_loop.QuitClosure()); |
| 704 run_loop.Run(); |
| 705 |
| 706 if (importer_list->count() > 0) { |
| 707 importer::LogImporterUseToMetrics( |
| 708 "AutoImport", importer_list->GetSourceProfileAt(0).importer_type); |
| 709 |
| 710 ImportSettings(profile, std::move(importer_list), items_to_import); |
| 711 } |
| 839 } | 712 } |
| 840 | 713 |
| 841 content::RecordAction(UserMetricsAction("FirstRunDef_Accept")); | 714 if (!import_bookmarks_path.empty()) |
| 842 | 715 ImportFromFile(profile, import_bookmarks_path); |
| 843 g_auto_import_state |= AUTO_IMPORT_CALLED; | |
| 844 } | 716 } |
| 845 | 717 |
| 846 void DoPostImportTasks(Profile* profile, bool make_chrome_default_for_user) { | 718 void DoPostImportTasks(Profile* profile, bool make_chrome_default_for_user) { |
| 847 // Only set default browser after import as auto import relies on the current | 719 // Only set default browser after import as auto import relies on the current |
| 848 // default browser to know what to import from. | 720 // default browser to know what to import from. |
| 849 ProcessDefaultBrowserPolicy(make_chrome_default_for_user); | 721 ProcessDefaultBrowserPolicy(make_chrome_default_for_user); |
| 850 | 722 |
| 851 // Display the first run bubble if there is a default search provider. | 723 // Display the first run bubble if there is a default search provider. |
| 852 TemplateURLService* template_url = | 724 TemplateURLService* template_url = |
| 853 TemplateURLServiceFactory::GetForProfile(profile); | 725 TemplateURLServiceFactory::GetForProfile(profile); |
| 854 if (template_url && template_url->GetDefaultSearchProvider()) | 726 if (template_url && template_url->GetDefaultSearchProvider()) |
| 855 FirstRunBubbleLauncher::ShowFirstRunBubbleSoon(); | 727 FirstRunBubbleLauncher::ShowFirstRunBubbleSoon(); |
| 856 SetShouldShowWelcomePage(); | 728 SetShouldShowWelcomePage(); |
| 857 SetShouldDoPersonalDataManagerFirstRun(); | 729 SetShouldDoPersonalDataManagerFirstRun(); |
| 858 | 730 |
| 859 internal::DoPostImportPlatformSpecificTasks(profile); | 731 internal::DoPostImportPlatformSpecificTasks(profile); |
| 860 } | 732 } |
| 861 | 733 |
| 862 uint16_t auto_import_state() { | 734 uint16_t auto_import_state() { |
| 863 return g_auto_import_state; | 735 return g_auto_import_state; |
| 864 } | 736 } |
| 865 | 737 |
| 866 } // namespace first_run | 738 } // namespace first_run |
| OLD | NEW |