first_run.cc revision 0f1bc08d4cfcc34181b0b5cbf065c40f687bf740
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.
4
5#include "chrome/browser/first_run/first_run.h"
6
7#include <algorithm>
8
9#include "base/command_line.h"
10#include "base/compiler_specific.h"
11#include "base/file_util.h"
12#include "base/files/file_path.h"
13#include "base/json/json_file_value_serializer.h"
14#include "base/lazy_instance.h"
15#include "base/memory/ref_counted.h"
16#include "base/message_loop/message_loop.h"
17#include "base/metrics/histogram.h"
18#include "base/path_service.h"
19#include "base/prefs/pref_service.h"
20#include "base/strings/stringprintf.h"
21#include "base/strings/utf_string_conversions.h"
22#include "build/build_config.h"
23#include "chrome/browser/browser_process.h"
24#include "chrome/browser/chrome_notification_types.h"
25#include "chrome/browser/extensions/extension_service.h"
26#include "chrome/browser/extensions/updater/extension_updater.h"
27#include "chrome/browser/first_run/first_run_internal.h"
28#include "chrome/browser/google/google_util.h"
29#include "chrome/browser/importer/external_process_importer_host.h"
30#include "chrome/browser/importer/importer_list.h"
31#include "chrome/browser/importer/importer_progress_observer.h"
32#include "chrome/browser/importer/importer_uma.h"
33#include "chrome/browser/importer/profile_writer.h"
34#include "chrome/browser/profiles/profiles_state.h"
35#include "chrome/browser/search_engines/template_url_service.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.h"
39#include "chrome/browser/signin/signin_manager_factory.h"
40#include "chrome/browser/signin/signin_promo.h"
41#include "chrome/browser/signin/signin_tracker.h"
42#include "chrome/browser/ui/browser.h"
43#include "chrome/browser/ui/browser_finder.h"
44#include "chrome/browser/ui/global_error/global_error_service.h"
45#include "chrome/browser/ui/global_error/global_error_service_factory.h"
46#include "chrome/browser/ui/tabs/tab_strip_model.h"
47#include "chrome/browser/ui/webui/ntp/new_tab_ui.h"
48#include "chrome/common/chrome_paths.h"
49#include "chrome/common/chrome_switches.h"
50#include "chrome/common/pref_names.h"
51#include "chrome/common/url_constants.h"
52#include "chrome/installer/util/master_preferences.h"
53#include "chrome/installer/util/master_preferences_constants.h"
54#include "chrome/installer/util/util_constants.h"
55#include "components/user_prefs/pref_registry_syncable.h"
56#include "content/public/browser/notification_observer.h"
57#include "content/public/browser/notification_registrar.h"
58#include "content/public/browser/notification_service.h"
59#include "content/public/browser/notification_types.h"
60#include "content/public/browser/user_metrics.h"
61#include "content/public/browser/web_contents.h"
62#include "google_apis/gaia/gaia_auth_util.h"
63#include "url/gurl.h"
64
65using content::UserMetricsAction;
66
67namespace {
68
69// A bitfield formed from values in AutoImportState to record the state of
70// AutoImport. This is used in testing to verify import startup actions that
71// occur before an observer can be registered in the test.
72uint16 g_auto_import_state = first_run::AUTO_IMPORT_NONE;
73
74// Flags for functions of similar name.
75bool g_should_show_welcome_page = false;
76bool g_should_do_autofill_personal_data_manager_first_run = false;
77
78// This class acts as an observer for the ImporterProgressObserver::ImportEnded
79// callback. When the import process is started, certain errors may cause
80// ImportEnded() to be called synchronously, but the typical case is that
81// ImportEnded() is called asynchronously. Thus we have to handle both cases.
82class ImportEndedObserver : public importer::ImporterProgressObserver {
83 public:
84  ImportEndedObserver() : ended_(false),
85                          should_quit_message_loop_(false) {}
86  virtual ~ImportEndedObserver() {}
87
88  // importer::ImporterProgressObserver:
89  virtual void ImportStarted() OVERRIDE {}
90  virtual void ImportItemStarted(importer::ImportItem item) OVERRIDE {}
91  virtual void ImportItemEnded(importer::ImportItem item) OVERRIDE {}
92  virtual void ImportEnded() OVERRIDE {
93    ended_ = true;
94    if (should_quit_message_loop_)
95      base::MessageLoop::current()->Quit();
96  }
97
98  void set_should_quit_message_loop() {
99    should_quit_message_loop_ = true;
100  }
101
102  bool ended() const {
103    return ended_;
104  }
105
106 private:
107  // Set if the import has ended.
108  bool ended_;
109
110  bool should_quit_message_loop_;
111};
112
113// Helper class that performs delayed first-run tasks that need more of the
114// chrome infrastructure to be up and running before they can be attempted.
115class FirstRunDelayedTasks : public content::NotificationObserver {
116 public:
117  enum Tasks {
118    NO_TASK,
119    INSTALL_EXTENSIONS
120  };
121
122  explicit FirstRunDelayedTasks(Tasks task) {
123    if (task == INSTALL_EXTENSIONS) {
124      registrar_.Add(this, chrome::NOTIFICATION_EXTENSIONS_READY,
125                     content::NotificationService::AllSources());
126    }
127    registrar_.Add(this, chrome::NOTIFICATION_BROWSER_CLOSED,
128                   content::NotificationService::AllSources());
129  }
130
131  virtual void Observe(int type,
132                       const content::NotificationSource& source,
133                       const content::NotificationDetails& details) OVERRIDE {
134    // After processing the notification we always delete ourselves.
135    if (type == chrome::NOTIFICATION_EXTENSIONS_READY) {
136      DoExtensionWork(
137          content::Source<Profile>(source).ptr()->GetExtensionService());
138    }
139    delete this;
140  }
141
142 private:
143  // Private ctor forces it to be created only in the heap.
144  virtual ~FirstRunDelayedTasks() {}
145
146  // The extension work is to basically trigger an extension update check.
147  // If the extension specified in the master pref is older than the live
148  // extension it will get updated which is the same as get it installed.
149  void DoExtensionWork(ExtensionService* service) {
150    if (service)
151      service->updater()->CheckNow(extensions::ExtensionUpdater::CheckParams());
152  }
153
154  content::NotificationRegistrar registrar_;
155};
156
157// Installs a task to do an extensions update check once the extensions system
158// is running.
159void DoDelayedInstallExtensions() {
160  new FirstRunDelayedTasks(FirstRunDelayedTasks::INSTALL_EXTENSIONS);
161}
162
163void DoDelayedInstallExtensionsIfNeeded(
164    installer::MasterPreferences* install_prefs) {
165  DictionaryValue* extensions = 0;
166  if (install_prefs->GetExtensionsBlock(&extensions)) {
167    VLOG(1) << "Extensions block found in master preferences";
168    DoDelayedInstallExtensions();
169  }
170}
171
172base::FilePath GetDefaultPrefFilePath(bool create_profile_dir,
173                                      const base::FilePath& user_data_dir) {
174  base::FilePath default_pref_dir =
175      profiles::GetDefaultProfileDir(user_data_dir);
176  if (create_profile_dir) {
177    if (!base::PathExists(default_pref_dir)) {
178      if (!file_util::CreateDirectory(default_pref_dir))
179        return base::FilePath();
180    }
181  }
182  return profiles::GetProfilePrefsPath(default_pref_dir);
183}
184
185// Sets the |items| bitfield according to whether the import data specified by
186// |import_type| should be be auto imported or not.
187void SetImportItem(PrefService* user_prefs,
188                   const char* pref_path,
189                   int import_items,
190                   int dont_import_items,
191                   importer::ImportItem import_type,
192                   int* items) {
193  // Work out whether an item is to be imported according to what is specified
194  // in master preferences.
195  bool should_import = false;
196  bool master_pref_set =
197      ((import_items | dont_import_items) & import_type) != 0;
198  bool master_pref = ((import_items & ~dont_import_items) & import_type) != 0;
199
200  if (import_type == importer::HISTORY ||
201      (import_type != importer::FAVORITES &&
202       first_run::internal::IsOrganicFirstRun())) {
203    // History is always imported unless turned off in master_preferences.
204    // Search engines and home page are imported in organic builds only
205    // unless turned off in master_preferences.
206    should_import = !master_pref_set || master_pref;
207  } else {
208    // Bookmarks are never imported, unless turned on in master_preferences.
209    // Search engine and home page import behaviour is similar in non organic
210    // builds.
211    should_import = master_pref_set && master_pref;
212  }
213
214  // If an import policy is set, import items according to policy. If no master
215  // preference is set, but a corresponding recommended policy is set, import
216  // item according to recommended policy. If both a master preference and a
217  // recommended policy is set, the master preference wins. If neither
218  // recommended nor managed policies are set, import item according to what we
219  // worked out above.
220  if (master_pref_set)
221    user_prefs->SetBoolean(pref_path, should_import);
222
223  if (!user_prefs->FindPreference(pref_path)->IsDefaultValue()) {
224    if (user_prefs->GetBoolean(pref_path))
225      *items |= import_type;
226  } else { // no policy (recommended or managed) is set
227    if (should_import)
228      *items |= import_type;
229  }
230
231  user_prefs->ClearPref(pref_path);
232}
233
234// Launches the import, via |importer_host|, from |source_profile| into
235// |target_profile| for the items specified in the |items_to_import| bitfield.
236// This may be done in a separate process depending on the platform, but it will
237// always block until done.
238void ImportFromSourceProfile(ExternalProcessImporterHost* importer_host,
239                             const importer::SourceProfile& source_profile,
240                             Profile* target_profile,
241                             uint16 items_to_import) {
242  ImportEndedObserver observer;
243  importer_host->set_observer(&observer);
244  importer_host->StartImportSettings(source_profile,
245                                     target_profile,
246                                     items_to_import,
247                                     new ProfileWriter(target_profile));
248  // If the import process has not errored out, block on it.
249  if (!observer.ended()) {
250    observer.set_should_quit_message_loop();
251    base::MessageLoop::current()->Run();
252  }
253}
254
255// Imports bookmarks from an html file whose path is provided by
256// |import_bookmarks_path|.
257void ImportFromFile(Profile* profile,
258                    ExternalProcessImporterHost* file_importer_host,
259                    const std::string& import_bookmarks_path) {
260  importer::SourceProfile source_profile;
261  source_profile.importer_type = importer::TYPE_BOOKMARKS_FILE;
262
263  const base::FilePath::StringType& import_bookmarks_path_str =
264#if defined(OS_WIN)
265      UTF8ToUTF16(import_bookmarks_path);
266#else
267      import_bookmarks_path;
268#endif
269  source_profile.source_path = base::FilePath(import_bookmarks_path_str);
270
271  ImportFromSourceProfile(file_importer_host, source_profile, profile,
272                          importer::FAVORITES);
273  g_auto_import_state |= first_run::AUTO_IMPORT_BOOKMARKS_FILE_IMPORTED;
274}
275
276// Imports settings from the first profile in |importer_list|.
277void ImportSettings(Profile* profile,
278                    ExternalProcessImporterHost* importer_host,
279                    scoped_refptr<ImporterList> importer_list,
280                    int items_to_import) {
281  const importer::SourceProfile& source_profile =
282      importer_list->GetSourceProfileAt(0);
283
284  // Ensure that importers aren't requested to import items that they do not
285  // support. If there is no overlap, skip.
286  items_to_import &= source_profile.services_supported;
287  if (items_to_import == 0)
288    return;
289
290  ImportFromSourceProfile(importer_host, source_profile, profile,
291                          items_to_import);
292  g_auto_import_state |= first_run::AUTO_IMPORT_PROFILE_IMPORTED;
293}
294
295GURL UrlFromString(const std::string& in) {
296  return GURL(in);
297}
298
299void ConvertStringVectorToGURLVector(
300    const std::vector<std::string>& src,
301    std::vector<GURL>* ret) {
302  ret->resize(src.size());
303  std::transform(src.begin(), src.end(), ret->begin(), &UrlFromString);
304}
305
306// Show the first run search engine bubble at the first appropriate opportunity.
307// This bubble may be delayed by other UI, like global errors and sync promos.
308class FirstRunBubbleLauncher : public content::NotificationObserver {
309 public:
310  // Show the bubble at the first appropriate opportunity. This function
311  // instantiates a FirstRunBubbleLauncher, which manages its own lifetime.
312  static void ShowFirstRunBubbleSoon();
313
314 private:
315  FirstRunBubbleLauncher();
316  virtual ~FirstRunBubbleLauncher();
317
318  // content::NotificationObserver:
319  virtual void Observe(int type,
320                       const content::NotificationSource& source,
321                       const content::NotificationDetails& details) OVERRIDE;
322
323  content::NotificationRegistrar registrar_;
324
325  DISALLOW_COPY_AND_ASSIGN(FirstRunBubbleLauncher);
326};
327
328// static
329void FirstRunBubbleLauncher::ShowFirstRunBubbleSoon() {
330  SetShowFirstRunBubblePref(first_run::FIRST_RUN_BUBBLE_SHOW);
331  // This FirstRunBubbleLauncher instance will manage its own lifetime.
332  new FirstRunBubbleLauncher();
333}
334
335FirstRunBubbleLauncher::FirstRunBubbleLauncher() {
336  registrar_.Add(this, content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
337                 content::NotificationService::AllSources());
338
339  // This notification is required to observe the switch between the sync setup
340  // page and the general settings page.
341  registrar_.Add(this, chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED,
342                 content::NotificationService::AllSources());
343}
344
345FirstRunBubbleLauncher::~FirstRunBubbleLauncher() {}
346
347void FirstRunBubbleLauncher::Observe(
348    int type,
349    const content::NotificationSource& source,
350    const content::NotificationDetails& details) {
351  DCHECK(type == content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME ||
352         type == chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED);
353
354  Browser* browser = chrome::FindBrowserWithWebContents(
355      content::Source<content::WebContents>(source).ptr());
356  if (!browser || !browser->is_type_tabbed())
357    return;
358
359  // Check the preference to determine if the bubble should be shown.
360  PrefService* prefs = g_browser_process->local_state();
361  if (!prefs || prefs->GetInteger(prefs::kShowFirstRunBubbleOption) !=
362      first_run::FIRST_RUN_BUBBLE_SHOW) {
363    delete this;
364    return;
365  }
366
367  content::WebContents* contents =
368      browser->tab_strip_model()->GetActiveWebContents();
369
370  // Suppress the first run bubble if a Gaia sign in page, the continue
371  // URL for the sign in page or the sync setup page is showing.
372  if (contents &&
373      (gaia::IsGaiaSignonRealm(contents->GetURL().GetOrigin()) ||
374       signin::IsContinueUrlForWebBasedSigninFlow(contents->GetURL()) ||
375       contents->GetURL() == GURL(std::string(chrome::kChromeUISettingsURL) +
376                                  chrome::kSyncSetupSubPage))) {
377    return;
378  }
379
380  if (contents && contents->GetURL().SchemeIs(chrome::kChromeUIScheme)) {
381    // Suppress the first run bubble if 'make chrome metro' flow is showing.
382    if (contents->GetURL().host() == chrome::kChromeUIMetroFlowHost)
383      return;
384
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);
395
396      if (is_promo_bubble_visible || signin_in_progress)
397        return;
398    }
399  }
400
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;
406
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);
410
411  // Show the bubble now and destroy this bubble launcher.
412  browser->ShowFirstRunBubble();
413  delete this;
414}
415
416static base::LazyInstance<base::FilePath> master_prefs_path_for_testing
417    = LAZY_INSTANCE_INITIALIZER;
418
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.
422installer::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;
435  }
436
437  return install_prefs;
438}
439
440}  // namespace
441
442namespace first_run {
443namespace internal {
444
445FirstRunState first_run_ = FIRST_RUN_UNKNOWN;
446
447bool GeneratePrefFile(const base::FilePath& user_data_dir,
448                      const installer::MasterPreferences& master_prefs) {
449  base::FilePath user_prefs = GetDefaultPrefFilePath(true, user_data_dir);
450  if (user_prefs.empty())
451    return false;
452
453  const base::DictionaryValue& master_prefs_dict =
454      master_prefs.master_dictionary();
455
456  JSONFileValueSerializer serializer(user_prefs);
457
458  // Call Serialize (which does IO) on the main thread, which would _normally_
459  // be verboten. In this case however, we require this IO to synchronously
460  // complete before Chrome can start (as master preferences seed the Local
461  // State and Preferences files). This won't trip ThreadIORestrictions as they
462  // won't have kicked in yet on the main thread.
463  return serializer.Serialize(master_prefs_dict);
464}
465
466void SetupMasterPrefsFromInstallPrefs(
467    const installer::MasterPreferences& install_prefs,
468    MasterPrefs* out_prefs) {
469  ConvertStringVectorToGURLVector(
470      install_prefs.GetFirstRunTabs(), &out_prefs->new_tabs);
471
472  install_prefs.GetInt(installer::master_preferences::kDistroPingDelay,
473                       &out_prefs->ping_delay);
474
475  bool value = false;
476  if (install_prefs.GetBool(
477          installer::master_preferences::kDistroImportSearchPref, &value)) {
478    if (value) {
479      out_prefs->do_import_items |= importer::SEARCH_ENGINES;
480    } else {
481      out_prefs->dont_import_items |= importer::SEARCH_ENGINES;
482    }
483  }
484
485  // If we're suppressing the first-run bubble, set that preference now.
486  // Otherwise, wait until the user has completed first run to set it, so the
487  // user is guaranteed to see the bubble iff he or she has completed the first
488  // run process.
489  if (install_prefs.GetBool(
490          installer::master_preferences::kDistroSuppressFirstRunBubble,
491          &value) && value)
492    SetShowFirstRunBubblePref(FIRST_RUN_BUBBLE_SUPPRESS);
493
494  if (install_prefs.GetBool(
495          installer::master_preferences::kDistroImportHistoryPref,
496          &value)) {
497    if (value) {
498      out_prefs->do_import_items |= importer::HISTORY;
499    } else {
500      out_prefs->dont_import_items |= importer::HISTORY;
501    }
502  }
503
504  std::string not_used;
505  out_prefs->homepage_defined = install_prefs.GetString(
506      prefs::kHomePage, &not_used);
507
508  if (install_prefs.GetBool(
509          installer::master_preferences::kDistroImportHomePagePref,
510          &value)) {
511    if (value) {
512      out_prefs->do_import_items |= importer::HOME_PAGE;
513    } else {
514      out_prefs->dont_import_items |= importer::HOME_PAGE;
515    }
516  }
517
518  // Bookmarks are never imported unless specifically turned on.
519  if (install_prefs.GetBool(
520          installer::master_preferences::kDistroImportBookmarksPref,
521          &value)) {
522    if (value)
523      out_prefs->do_import_items |= importer::FAVORITES;
524    else
525      out_prefs->dont_import_items |= importer::FAVORITES;
526  }
527
528  if (install_prefs.GetBool(
529          installer::master_preferences::kMakeChromeDefaultForUser,
530          &value) && value) {
531    out_prefs->make_chrome_default = true;
532  }
533
534  if (install_prefs.GetBool(
535          installer::master_preferences::kSuppressFirstRunDefaultBrowserPrompt,
536          &value) && value) {
537    out_prefs->suppress_first_run_default_browser_prompt = true;
538  }
539
540  install_prefs.GetString(
541      installer::master_preferences::kDistroImportBookmarksFromFilePref,
542      &out_prefs->import_bookmarks_path);
543
544  out_prefs->variations_seed = install_prefs.GetVariationsSeed();
545
546  install_prefs.GetString(
547      installer::master_preferences::kDistroSuppressDefaultBrowserPromptPref,
548      &out_prefs->suppress_default_browser_prompt_for_version);
549}
550
551void SetDefaultBrowser(installer::MasterPreferences* install_prefs){
552  // Even on the first run we only allow for the user choice to take effect if
553  // no policy has been set by the admin.
554  if (!g_browser_process->local_state()->IsManagedPreference(
555          prefs::kDefaultBrowserSettingEnabled)) {
556    bool value = false;
557    if (install_prefs->GetBool(
558            installer::master_preferences::kMakeChromeDefaultForUser,
559            &value) && value) {
560      ShellIntegration::SetAsDefaultBrowser();
561    }
562  } else {
563    if (g_browser_process->local_state()->GetBoolean(
564            prefs::kDefaultBrowserSettingEnabled)) {
565      ShellIntegration::SetAsDefaultBrowser();
566    }
567  }
568}
569
570bool CreateSentinel() {
571  base::FilePath first_run_sentinel;
572  if (!internal::GetFirstRunSentinelFilePath(&first_run_sentinel))
573    return false;
574  return file_util::WriteFile(first_run_sentinel, "", 0) != -1;
575}
576
577// -- Platform-specific functions --
578
579#if !defined(OS_LINUX) && !defined(OS_BSD)
580bool IsOrganicFirstRun() {
581  std::string brand;
582  google_util::GetBrand(&brand);
583  return google_util::IsOrganicFirstRun(brand);
584}
585#endif
586
587}  // namespace internal
588
589MasterPrefs::MasterPrefs()
590    : ping_delay(0),
591      homepage_defined(false),
592      do_import_items(0),
593      dont_import_items(0),
594      make_chrome_default(false),
595      suppress_first_run_default_browser_prompt(false) {
596}
597
598MasterPrefs::~MasterPrefs() {}
599
600bool IsChromeFirstRun() {
601  if (internal::first_run_ != internal::FIRST_RUN_UNKNOWN)
602    return internal::first_run_ == internal::FIRST_RUN_TRUE;
603
604  internal::first_run_ = internal::FIRST_RUN_FALSE;
605
606  base::FilePath first_run_sentinel;
607  const CommandLine* command_line = CommandLine::ForCurrentProcess();
608  if (command_line->HasSwitch(switches::kForceFirstRun)) {
609    internal::first_run_ = internal::FIRST_RUN_TRUE;
610  } else if (command_line->HasSwitch(switches::kCancelFirstRun)) {
611    internal::first_run_ = internal::FIRST_RUN_CANCEL;
612  } else if (!command_line->HasSwitch(switches::kNoFirstRun) &&
613             internal::GetFirstRunSentinelFilePath(&first_run_sentinel) &&
614             !base::PathExists(first_run_sentinel)) {
615    internal::first_run_ = internal::FIRST_RUN_TRUE;
616  }
617
618  return internal::first_run_ == internal::FIRST_RUN_TRUE;
619}
620
621bool IsFirstRunSuppressed(const CommandLine& command_line) {
622  return command_line.HasSwitch(switches::kCancelFirstRun) ||
623      command_line.HasSwitch(switches::kNoFirstRun);
624}
625
626void CreateSentinelIfNeeded() {
627  if (IsChromeFirstRun() ||
628      internal::first_run_ == internal::FIRST_RUN_CANCEL) {
629    internal::CreateSentinel();
630  }
631}
632
633std::string GetPingDelayPrefName() {
634  return base::StringPrintf("%s.%s",
635                            installer::master_preferences::kDistroDict,
636                            installer::master_preferences::kDistroPingDelay);
637}
638
639void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
640  registry->RegisterIntegerPref(
641      GetPingDelayPrefName().c_str(),
642      0,
643      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
644}
645
646bool RemoveSentinel() {
647  base::FilePath first_run_sentinel;
648  if (!internal::GetFirstRunSentinelFilePath(&first_run_sentinel))
649    return false;
650  return base::DeleteFile(first_run_sentinel, false);
651}
652
653bool SetShowFirstRunBubblePref(FirstRunBubbleOptions show_bubble_option) {
654  PrefService* local_state = g_browser_process->local_state();
655  if (!local_state)
656    return false;
657  if (local_state->GetInteger(
658          prefs::kShowFirstRunBubbleOption) != FIRST_RUN_BUBBLE_SUPPRESS) {
659    // Set the new state as long as the bubble wasn't explicitly suppressed
660    // already.
661    local_state->SetInteger(prefs::kShowFirstRunBubbleOption,
662                            show_bubble_option);
663  }
664  return true;
665}
666
667void SetShouldShowWelcomePage() {
668  g_should_show_welcome_page = true;
669}
670
671bool ShouldShowWelcomePage() {
672  bool retval = g_should_show_welcome_page;
673  g_should_show_welcome_page = false;
674  return retval;
675}
676
677void SetShouldDoPersonalDataManagerFirstRun() {
678  g_should_do_autofill_personal_data_manager_first_run = true;
679}
680
681bool ShouldDoPersonalDataManagerFirstRun() {
682  bool retval = g_should_do_autofill_personal_data_manager_first_run;
683  g_should_do_autofill_personal_data_manager_first_run = false;
684  return retval;
685}
686
687void LogFirstRunMetric(FirstRunBubbleMetric metric) {
688  UMA_HISTOGRAM_ENUMERATION("FirstRun.SearchEngineBubble", metric,
689                            NUM_FIRST_RUN_BUBBLE_METRICS);
690}
691
692void SetMasterPrefsPathForTesting(const base::FilePath& master_prefs) {
693  master_prefs_path_for_testing.Get() = master_prefs;
694}
695
696ProcessMasterPreferencesResult ProcessMasterPreferences(
697    const base::FilePath& user_data_dir,
698    MasterPrefs* out_prefs) {
699  DCHECK(!user_data_dir.empty());
700
701  scoped_ptr<installer::MasterPreferences> install_prefs(LoadMasterPrefs());
702
703  // Default value in case master preferences is missing or corrupt, or
704  // ping_delay is missing.
705  out_prefs->ping_delay = 90;
706  if (install_prefs.get()) {
707    if (!internal::ShowPostInstallEULAIfNeeded(install_prefs.get()))
708      return EULA_EXIT_NOW;
709
710    if (!internal::GeneratePrefFile(user_data_dir, *install_prefs.get()))
711      DLOG(ERROR) << "Failed to generate master_preferences in user data dir.";
712
713    DoDelayedInstallExtensionsIfNeeded(install_prefs.get());
714
715    internal::SetupMasterPrefsFromInstallPrefs(*install_prefs, out_prefs);
716
717    internal::SetDefaultBrowser(install_prefs.get());
718  }
719
720  return FIRST_RUN_PROCEED;
721}
722
723void AutoImport(
724    Profile* profile,
725    bool homepage_defined,
726    int import_items,
727    int dont_import_items,
728    const std::string& import_bookmarks_path) {
729  base::FilePath local_state_path;
730  PathService::Get(chrome::FILE_LOCAL_STATE, &local_state_path);
731  bool local_state_file_exists = base::PathExists(local_state_path);
732
733  scoped_refptr<ImporterList> importer_list(new ImporterList());
734  importer_list->DetectSourceProfilesHack(
735      g_browser_process->GetApplicationLocale(), false);
736
737  // Do import if there is an available profile for us to import.
738  if (importer_list->count() > 0) {
739    if (internal::IsOrganicFirstRun()) {
740      // Home page is imported in organic builds only unless turned off or
741      // defined in master_preferences.
742      if (homepage_defined) {
743        dont_import_items |= importer::HOME_PAGE;
744        if (import_items & importer::HOME_PAGE)
745          import_items &= ~importer::HOME_PAGE;
746      }
747      // Search engines are not imported automatically in organic builds if the
748      // user already has a user preferences directory.
749      if (local_state_file_exists) {
750        dont_import_items |= importer::SEARCH_ENGINES;
751        if (import_items & importer::SEARCH_ENGINES)
752          import_items &= ~importer::SEARCH_ENGINES;
753      }
754    }
755
756    PrefService* user_prefs = profile->GetPrefs();
757    int items = 0;
758
759    SetImportItem(user_prefs,
760                  prefs::kImportHistory,
761                  import_items,
762                  dont_import_items,
763                  importer::HISTORY,
764                  &items);
765    SetImportItem(user_prefs,
766                  prefs::kImportHomepage,
767                  import_items,
768                  dont_import_items,
769                  importer::HOME_PAGE,
770                  &items);
771    SetImportItem(user_prefs,
772                  prefs::kImportSearchEngine,
773                  import_items,
774                  dont_import_items,
775                  importer::SEARCH_ENGINES,
776                  &items);
777    SetImportItem(user_prefs,
778                  prefs::kImportBookmarks,
779                  import_items,
780                  dont_import_items,
781                  importer::FAVORITES,
782                  &items);
783
784    // Deletes itself.
785    ExternalProcessImporterHost* importer_host =
786        new ExternalProcessImporterHost;
787
788    // Don't show the warning dialog if import fails.
789    importer_host->set_headless();
790
791    importer::LogImporterUseToMetrics(
792        "AutoImport", importer_list->GetSourceProfileAt(0).importer_type);
793
794    ImportSettings(profile, importer_host, importer_list, items);
795  }
796
797  if (!import_bookmarks_path.empty()) {
798    // Deletes itself.
799    ExternalProcessImporterHost* file_importer_host =
800        new ExternalProcessImporterHost;
801    file_importer_host->set_headless();
802
803    ImportFromFile(profile, file_importer_host, import_bookmarks_path);
804  }
805
806  content::RecordAction(UserMetricsAction("FirstRunDef_Accept"));
807
808  g_auto_import_state |= AUTO_IMPORT_CALLED;
809}
810
811void DoPostImportTasks(Profile* profile, bool make_chrome_default) {
812  if (make_chrome_default &&
813      ShellIntegration::CanSetAsDefaultBrowser() ==
814          ShellIntegration::SET_DEFAULT_UNATTENDED) {
815    ShellIntegration::SetAsDefaultBrowser();
816  }
817
818  // Display the first run bubble if there is a default search provider.
819  TemplateURLService* template_url =
820      TemplateURLServiceFactory::GetForProfile(profile);
821  if (template_url && template_url->GetDefaultSearchProvider())
822    FirstRunBubbleLauncher::ShowFirstRunBubbleSoon();
823  SetShouldShowWelcomePage();
824  SetShouldDoPersonalDataManagerFirstRun();
825
826  internal::DoPostImportPlatformSpecificTasks(profile);
827}
828
829uint16 auto_import_state() {
830  return g_auto_import_state;
831}
832
833}  // namespace first_run
834