Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(64)

Side by Side Diff: chrome/browser/first_run/first_run.cc

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

Powered by Google App Engine
This is Rietveld 408576698