No dual_mode on Win10+ shortcuts.
[chromium-blink-merge.git] / chrome / browser / first_run / first_run.cc
blob9c739e9fde61a46c7c1b4aefe40a5f6e4de32582
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);
555 if (install_prefs.GetBool(
556 installer::master_preferences::kDistroWelcomePageOnOSUpgradeEnabled,
557 &value) &&
558 !value) {
559 out_prefs->welcome_page_on_os_upgrade_enabled = false;
563 bool GetFirstRunSentinelFilePath(base::FilePath* path) {
564 base::FilePath user_data_dir;
565 if (!PathService::Get(chrome::DIR_USER_DATA, &user_data_dir))
566 return false;
567 *path = user_data_dir.Append(chrome::kFirstRunSentinel);
568 return true;
571 bool CreateSentinel() {
572 base::FilePath first_run_sentinel;
573 return GetFirstRunSentinelFilePath(&first_run_sentinel) &&
574 base::WriteFile(first_run_sentinel, "", 0) != -1;
577 // -- Platform-specific functions --
579 #if !defined(OS_LINUX) && !defined(OS_BSD)
580 bool IsOrganicFirstRun() {
581 std::string brand;
582 google_brand::GetBrand(&brand);
583 return google_brand::IsOrganicFirstRun(brand);
585 #endif
587 } // namespace internal
589 MasterPrefs::MasterPrefs()
590 : ping_delay(0),
591 homepage_defined(false),
592 do_import_items(0),
593 dont_import_items(0),
594 make_chrome_default_for_user(false),
595 suppress_first_run_default_browser_prompt(false),
596 welcome_page_on_os_upgrade_enabled(true) {
599 MasterPrefs::~MasterPrefs() {}
601 bool IsChromeFirstRun() {
602 if (internal::g_first_run == internal::FIRST_RUN_UNKNOWN) {
603 internal::g_first_run = internal::FIRST_RUN_FALSE;
604 const base::CommandLine* command_line =
605 base::CommandLine::ForCurrentProcess();
606 if (command_line->HasSwitch(switches::kForceFirstRun) ||
607 (!command_line->HasSwitch(switches::kNoFirstRun) &&
608 !internal::IsFirstRunSentinelPresent())) {
609 internal::g_first_run = internal::FIRST_RUN_TRUE;
612 return internal::g_first_run == internal::FIRST_RUN_TRUE;
615 #if defined(OS_MACOSX)
616 bool IsFirstRunSuppressed(const base::CommandLine& command_line) {
617 return command_line.HasSwitch(switches::kNoFirstRun);
619 #endif
621 void CreateSentinelIfNeeded() {
622 if (IsChromeFirstRun())
623 internal::CreateSentinel();
626 std::string GetPingDelayPrefName() {
627 return base::StringPrintf("%s.%s",
628 installer::master_preferences::kDistroDict,
629 installer::master_preferences::kDistroPingDelay);
632 void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
633 registry->RegisterIntegerPref(GetPingDelayPrefName().c_str(), 0);
636 bool SetShowFirstRunBubblePref(FirstRunBubbleOptions show_bubble_option) {
637 PrefService* local_state = g_browser_process->local_state();
638 if (!local_state)
639 return false;
640 if (local_state->GetInteger(
641 prefs::kShowFirstRunBubbleOption) != FIRST_RUN_BUBBLE_SUPPRESS) {
642 // Set the new state as long as the bubble wasn't explicitly suppressed
643 // already.
644 local_state->SetInteger(prefs::kShowFirstRunBubbleOption,
645 show_bubble_option);
647 return true;
650 void SetShouldShowWelcomePage() {
651 g_should_show_welcome_page = true;
654 bool ShouldShowWelcomePage() {
655 bool retval = g_should_show_welcome_page;
656 g_should_show_welcome_page = false;
657 return retval;
660 void SetShouldDoPersonalDataManagerFirstRun() {
661 g_should_do_autofill_personal_data_manager_first_run = true;
664 bool ShouldDoPersonalDataManagerFirstRun() {
665 bool retval = g_should_do_autofill_personal_data_manager_first_run;
666 g_should_do_autofill_personal_data_manager_first_run = false;
667 return retval;
670 void LogFirstRunMetric(FirstRunBubbleMetric metric) {
671 UMA_HISTOGRAM_ENUMERATION("FirstRun.SearchEngineBubble", metric,
672 NUM_FIRST_RUN_BUBBLE_METRICS);
675 void SetMasterPrefsPathForTesting(const base::FilePath& master_prefs) {
676 master_prefs_path_for_testing.Get() = master_prefs;
679 ProcessMasterPreferencesResult ProcessMasterPreferences(
680 const base::FilePath& user_data_dir,
681 MasterPrefs* out_prefs) {
682 DCHECK(!user_data_dir.empty());
684 scoped_ptr<installer::MasterPreferences> install_prefs(LoadMasterPrefs());
686 // Default value in case master preferences is missing or corrupt, or
687 // ping_delay is missing.
688 out_prefs->ping_delay = 90;
689 if (install_prefs.get()) {
690 if (!internal::ShowPostInstallEULAIfNeeded(install_prefs.get()))
691 return EULA_EXIT_NOW;
693 if (!chrome_prefs::InitializePrefsFromMasterPrefs(
694 profiles::GetDefaultProfileDir(user_data_dir),
695 install_prefs->master_dictionary())) {
696 DLOG(ERROR) << "Failed to initialize from master_preferences.";
699 DoDelayedInstallExtensionsIfNeeded(install_prefs.get());
701 internal::SetupMasterPrefsFromInstallPrefs(*install_prefs, out_prefs);
704 return FIRST_RUN_PROCEED;
707 void AutoImport(
708 Profile* profile,
709 bool homepage_defined,
710 int import_items,
711 int dont_import_items,
712 const std::string& import_bookmarks_path) {
713 base::FilePath local_state_path;
714 PathService::Get(chrome::FILE_LOCAL_STATE, &local_state_path);
715 bool local_state_file_exists = base::PathExists(local_state_path);
717 // It may be possible to do the if block below asynchronously. In which case,
718 // get rid of this RunLoop. http://crbug.com/366116.
719 base::RunLoop run_loop;
720 scoped_ptr<ImporterList> importer_list(new ImporterList());
721 importer_list->DetectSourceProfiles(
722 g_browser_process->GetApplicationLocale(),
723 false, // include_interactive_profiles?
724 run_loop.QuitClosure());
725 run_loop.Run();
727 // Do import if there is an available profile for us to import.
728 if (importer_list->count() > 0) {
729 if (internal::IsOrganicFirstRun()) {
730 // Home page is imported in organic builds only unless turned off or
731 // defined in master_preferences.
732 if (homepage_defined) {
733 dont_import_items |= importer::HOME_PAGE;
734 if (import_items & importer::HOME_PAGE)
735 import_items &= ~importer::HOME_PAGE;
737 // Search engines are not imported automatically in organic builds if the
738 // user already has a user preferences directory.
739 if (local_state_file_exists) {
740 dont_import_items |= importer::SEARCH_ENGINES;
741 if (import_items & importer::SEARCH_ENGINES)
742 import_items &= ~importer::SEARCH_ENGINES;
746 PrefService* user_prefs = profile->GetPrefs();
747 int items = 0;
749 SetImportItem(user_prefs,
750 prefs::kImportHistory,
751 import_items,
752 dont_import_items,
753 importer::HISTORY,
754 &items);
755 SetImportItem(user_prefs,
756 prefs::kImportHomepage,
757 import_items,
758 dont_import_items,
759 importer::HOME_PAGE,
760 &items);
761 SetImportItem(user_prefs,
762 prefs::kImportSearchEngine,
763 import_items,
764 dont_import_items,
765 importer::SEARCH_ENGINES,
766 &items);
767 SetImportItem(user_prefs,
768 prefs::kImportBookmarks,
769 import_items,
770 dont_import_items,
771 importer::FAVORITES,
772 &items);
774 // Deletes itself.
775 ExternalProcessImporterHost* importer_host =
776 new ExternalProcessImporterHost;
778 // Don't show the warning dialog if import fails.
779 importer_host->set_headless();
781 importer::LogImporterUseToMetrics(
782 "AutoImport", importer_list->GetSourceProfileAt(0).importer_type);
784 ImportSettings(profile, importer_host, importer_list.Pass(), items);
787 if (!import_bookmarks_path.empty()) {
788 // Deletes itself.
789 ExternalProcessImporterHost* file_importer_host =
790 new ExternalProcessImporterHost;
791 file_importer_host->set_headless();
793 ImportFromFile(profile, file_importer_host, import_bookmarks_path);
796 content::RecordAction(UserMetricsAction("FirstRunDef_Accept"));
798 g_auto_import_state |= AUTO_IMPORT_CALLED;
801 void DoPostImportTasks(Profile* profile, bool make_chrome_default_for_user) {
802 // Only set default browser after import as auto import relies on the current
803 // default browser to know what to import from.
804 ProcessDefaultBrowserPolicy(make_chrome_default_for_user);
806 // Display the first run bubble if there is a default search provider.
807 TemplateURLService* template_url =
808 TemplateURLServiceFactory::GetForProfile(profile);
809 if (template_url && template_url->GetDefaultSearchProvider())
810 FirstRunBubbleLauncher::ShowFirstRunBubbleSoon();
811 SetShouldShowWelcomePage();
812 SetShouldDoPersonalDataManagerFirstRun();
814 internal::DoPostImportPlatformSpecificTasks(profile);
817 uint16 auto_import_state() {
818 return g_auto_import_state;
821 } // namespace first_run