15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/first_run/first_run.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <algorithm>
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/command_line.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/compiler_specific.h"
11c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/files/file_path.h"
121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/files/file_util.h"
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/lazy_instance.h"
14a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#include "base/memory/ref_counted.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/metrics/histogram.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/path_service.h"
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/prefs/pref_service.h"
18c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#include "base/run_loop.h"
19868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/stringprintf.h"
20868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "build/build_config.h"
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/browser_process.h"
237dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "chrome/browser/chrome_notification_types.h"
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/extensions/extension_service.h"
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/extensions/updater/extension_updater.h"
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/first_run/first_run_internal.h"
2746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "chrome/browser/google/google_brand.h"
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/importer/external_process_importer_host.h"
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/importer/importer_list.h"
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/importer/importer_progress_observer.h"
317dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "chrome/browser/importer/importer_uma.h"
3290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "chrome/browser/importer/profile_writer.h"
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/prefs/chrome_pref_service_factory.h"
34effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "chrome/browser/profiles/profile.h"
359ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "chrome/browser/profiles/profiles_state.h"
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/search_engines/template_url_service_factory.h"
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/shell_integration.h"
38c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/browser/signin/signin_manager_factory.h"
39a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#include "chrome/browser/signin/signin_promo.h"
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/browser.h"
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/browser_finder.h"
42effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "chrome/browser/ui/chrome_pages.h"
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/global_error/global_error_service.h"
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/global_error/global_error_service_factory.h"
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/tabs/tab_strip_model.h"
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/webui/ntp/new_tab_ui.h"
47effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "chrome/common/chrome_constants.h"
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/chrome_paths.h"
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/chrome_switches.h"
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/pref_names.h"
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/url_constants.h"
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/installer/util/master_preferences.h"
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/installer/util/master_preferences_constants.h"
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/installer/util/util_constants.h"
55cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/pref_registry/pref_registry_syncable.h"
56116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "components/search_engines/template_url_service.h"
57e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "components/signin/core/browser/signin_manager.h"
58c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#include "components/signin/core/browser/signin_tracker.h"
59a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#include "content/public/browser/notification_observer.h"
60a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#include "content/public/browser/notification_registrar.h"
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/notification_service.h"
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/notification_types.h"
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/user_metrics.h"
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/web_contents.h"
65116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "extensions/browser/extension_system.h"
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "google_apis/gaia/gaia_auth_util.h"
67eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "url/gurl.h"
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)using base::UserMetricsAction;
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// A bitfield formed from values in AutoImportState to record the state of
7490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// AutoImport. This is used in testing to verify import startup actions that
7590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// occur before an observer can be registered in the test.
7690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)uint16 g_auto_import_state = first_run::AUTO_IMPORT_NONE;
7790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Flags for functions of similar name.
79c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool g_should_show_welcome_page = false;
80c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool g_should_do_autofill_personal_data_manager_first_run = false;
812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
8290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// This class acts as an observer for the ImporterProgressObserver::ImportEnded
8390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// callback. When the import process is started, certain errors may cause
8490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// ImportEnded() to be called synchronously, but the typical case is that
8590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// ImportEnded() is called asynchronously. Thus we have to handle both cases.
8690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)class ImportEndedObserver : public importer::ImporterProgressObserver {
8790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) public:
88c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  ImportEndedObserver() : ended_(false) {}
8990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual ~ImportEndedObserver() {}
9090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
9190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // importer::ImporterProgressObserver:
9290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual void ImportStarted() OVERRIDE {}
9390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual void ImportItemStarted(importer::ImportItem item) OVERRIDE {}
9490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual void ImportItemEnded(importer::ImportItem item) OVERRIDE {}
9590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual void ImportEnded() OVERRIDE {
9690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    ended_ = true;
97c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    if (!callback_for_import_end_.is_null())
98c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      callback_for_import_end_.Run();
9990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
10090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
101c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  void set_callback_for_import_end(const base::Closure& callback) {
102c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    callback_for_import_end_ = callback;
10390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
10490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
10590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  bool ended() const {
10690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return ended_;
10790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
10890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
10990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) private:
11090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Set if the import has ended.
11190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  bool ended_;
11290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
113c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  base::Closure callback_for_import_end_;
114c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
115c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  DISALLOW_COPY_AND_ASSIGN(ImportEndedObserver);
11690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)};
1172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Helper class that performs delayed first-run tasks that need more of the
1192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// chrome infrastructure to be up and running before they can be attempted.
1202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class FirstRunDelayedTasks : public content::NotificationObserver {
1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public:
1222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  enum Tasks {
1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    NO_TASK,
1242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    INSTALL_EXTENSIONS
1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  };
1262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  explicit FirstRunDelayedTasks(Tasks task) {
1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (task == INSTALL_EXTENSIONS) {
1295f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      registrar_.Add(this,
1305f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                     extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED,
1312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                     content::NotificationService::AllSources());
1322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
1332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    registrar_.Add(this, chrome::NOTIFICATION_BROWSER_CLOSED,
1342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                   content::NotificationService::AllSources());
1352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void Observe(int type,
1382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                       const content::NotificationSource& source,
1392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                       const content::NotificationDetails& details) OVERRIDE {
1402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // After processing the notification we always delete ourselves.
1415f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    if (type == extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED) {
142116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      Profile* profile = content::Source<Profile>(source).ptr();
143116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      ExtensionService* service =
144116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          extensions::ExtensionSystem::Get(profile)->extension_service();
145116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      DoExtensionWork(service);
1462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
1472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    delete this;
1482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) private:
1512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Private ctor forces it to be created only in the heap.
1522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual ~FirstRunDelayedTasks() {}
1532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The extension work is to basically trigger an extension update check.
1552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If the extension specified in the master pref is older than the live
1562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // extension it will get updated which is the same as get it installed.
1572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void DoExtensionWork(ExtensionService* service) {
1582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (service)
1592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      service->updater()->CheckNow(extensions::ExtensionUpdater::CheckParams());
1602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  content::NotificationRegistrar registrar_;
1632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
1642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Installs a task to do an extensions update check once the extensions system
1662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// is running.
1672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void DoDelayedInstallExtensions() {
1682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  new FirstRunDelayedTasks(FirstRunDelayedTasks::INSTALL_EXTENSIONS);
1692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void DoDelayedInstallExtensionsIfNeeded(
1722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    installer::MasterPreferences* install_prefs) {
1735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::DictionaryValue* extensions = 0;
1742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (install_prefs->GetExtensionsBlock(&extensions)) {
1752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    VLOG(1) << "Extensions block found in master preferences";
1762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DoDelayedInstallExtensions();
1772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Sets the |items| bitfield according to whether the import data specified by
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// |import_type| should be be auto imported or not.
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void SetImportItem(PrefService* user_prefs,
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   const char* pref_path,
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   int import_items,
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   int dont_import_items,
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   importer::ImportItem import_type,
187c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                   int* items) {
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Work out whether an item is to be imported according to what is specified
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // in master preferences.
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool should_import = false;
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool master_pref_set =
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ((import_items | dont_import_items) & import_type) != 0;
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool master_pref = ((import_items & ~dont_import_items) & import_type) != 0;
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (import_type == importer::HISTORY ||
196c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      (import_type != importer::FAVORITES &&
197c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)       first_run::internal::IsOrganicFirstRun())) {
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // History is always imported unless turned off in master_preferences.
199c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // Search engines and home page are imported in organic builds only
200c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // unless turned off in master_preferences.
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    should_import = !master_pref_set || master_pref;
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Bookmarks are never imported, unless turned on in master_preferences.
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Search engine and home page import behaviour is similar in non organic
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // builds.
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    should_import = master_pref_set && master_pref;
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If an import policy is set, import items according to policy. If no master
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // preference is set, but a corresponding recommended policy is set, import
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // item according to recommended policy. If both a master preference and a
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // recommended policy is set, the master preference wins. If neither
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // recommended nor managed policies are set, import item according to what we
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // worked out above.
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (master_pref_set)
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    user_prefs->SetBoolean(pref_path, should_import);
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!user_prefs->FindPreference(pref_path)->IsDefaultValue()) {
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (user_prefs->GetBoolean(pref_path))
220c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      *items |= import_type;
2215c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  } else {
2225c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    // no policy (recommended or managed) is set
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (should_import)
224c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      *items |= import_type;
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  user_prefs->ClearPref(pref_path);
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
23090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Launches the import, via |importer_host|, from |source_profile| into
23190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// |target_profile| for the items specified in the |items_to_import| bitfield.
23290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// This may be done in a separate process depending on the platform, but it will
23390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// always block until done.
234eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid ImportFromSourceProfile(ExternalProcessImporterHost* importer_host,
23590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                             const importer::SourceProfile& source_profile,
23690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                             Profile* target_profile,
23790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                             uint16 items_to_import) {
23890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  ImportEndedObserver observer;
239eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  importer_host->set_observer(&observer);
24090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  importer_host->StartImportSettings(source_profile,
24190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                                     target_profile,
24290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                                     items_to_import,
24390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                                     new ProfileWriter(target_profile));
244c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // If the import process has not errored out, block on it.
245c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!observer.ended()) {
246c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    base::RunLoop loop;
247c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    observer.set_callback_for_import_end(loop.QuitClosure());
248c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    loop.Run();
249c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    observer.set_callback_for_import_end(base::Closure());
250c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
25390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Imports bookmarks from an html file whose path is provided by
25490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// |import_bookmarks_path|.
25590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void ImportFromFile(Profile* profile,
256eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                    ExternalProcessImporterHost* file_importer_host,
25790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                    const std::string& import_bookmarks_path) {
25890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  importer::SourceProfile source_profile;
25990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  source_profile.importer_type = importer::TYPE_BOOKMARKS_FILE;
26090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
26190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  const base::FilePath::StringType& import_bookmarks_path_str =
26290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#if defined(OS_WIN)
2635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::UTF8ToUTF16(import_bookmarks_path);
26490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#else
26590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      import_bookmarks_path;
26690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#endif
26790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  source_profile.source_path = base::FilePath(import_bookmarks_path_str);
26890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
26990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  ImportFromSourceProfile(file_importer_host, source_profile, profile,
27090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                          importer::FAVORITES);
27190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  g_auto_import_state |= first_run::AUTO_IMPORT_BOOKMARKS_FILE_IMPORTED;
27290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
27390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
27490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Imports settings from the first profile in |importer_list|.
27590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void ImportSettings(Profile* profile,
276eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                    ExternalProcessImporterHost* importer_host,
2775c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu                    scoped_ptr<ImporterList> importer_list,
27890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                    int items_to_import) {
27990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  const importer::SourceProfile& source_profile =
28090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      importer_list->GetSourceProfileAt(0);
28190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
28290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Ensure that importers aren't requested to import items that they do not
28390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // support. If there is no overlap, skip.
28490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  items_to_import &= source_profile.services_supported;
28590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (items_to_import == 0)
28690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return;
28790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
28890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  ImportFromSourceProfile(importer_host, source_profile, profile,
28990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                          items_to_import);
29090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  g_auto_import_state |= first_run::AUTO_IMPORT_PROFILE_IMPORTED;
29190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
29290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)GURL UrlFromString(const std::string& in) {
2942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return GURL(in);
2952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void ConvertStringVectorToGURLVector(
2982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::vector<std::string>& src,
2992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    std::vector<GURL>* ret) {
3002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ret->resize(src.size());
3012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::transform(src.begin(), src.end(), ret->begin(), &UrlFromString);
3022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
304a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)// Show the first run search engine bubble at the first appropriate opportunity.
305a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)// This bubble may be delayed by other UI, like global errors and sync promos.
306a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)class FirstRunBubbleLauncher : public content::NotificationObserver {
307a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) public:
308a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Show the bubble at the first appropriate opportunity. This function
309a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // instantiates a FirstRunBubbleLauncher, which manages its own lifetime.
310a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  static void ShowFirstRunBubbleSoon();
311a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
312a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) private:
313a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  FirstRunBubbleLauncher();
314a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  virtual ~FirstRunBubbleLauncher();
315a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
316a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // content::NotificationObserver:
317a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  virtual void Observe(int type,
318a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                       const content::NotificationSource& source,
319a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                       const content::NotificationDetails& details) OVERRIDE;
320a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
321a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  content::NotificationRegistrar registrar_;
322a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
323a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(FirstRunBubbleLauncher);
324a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)};
325a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
326a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)// static
327a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)void FirstRunBubbleLauncher::ShowFirstRunBubbleSoon() {
328a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  SetShowFirstRunBubblePref(first_run::FIRST_RUN_BUBBLE_SHOW);
329a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // This FirstRunBubbleLauncher instance will manage its own lifetime.
330a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  new FirstRunBubbleLauncher();
331a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
332a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
333a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)FirstRunBubbleLauncher::FirstRunBubbleLauncher() {
334a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  registrar_.Add(this, content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
335a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                 content::NotificationService::AllSources());
336a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
337a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // This notification is required to observe the switch between the sync setup
338a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // page and the general settings page.
339a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  registrar_.Add(this, chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED,
340a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                 content::NotificationService::AllSources());
341a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
342a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
343a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)FirstRunBubbleLauncher::~FirstRunBubbleLauncher() {}
344a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
345a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)void FirstRunBubbleLauncher::Observe(
346a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    int type,
347a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    const content::NotificationSource& source,
348a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    const content::NotificationDetails& details) {
349a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  DCHECK(type == content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME ||
350a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)         type == chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED);
351a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
352a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  Browser* browser = chrome::FindBrowserWithWebContents(
353a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      content::Source<content::WebContents>(source).ptr());
354a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  if (!browser || !browser->is_type_tabbed())
355a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    return;
356a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
357a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Check the preference to determine if the bubble should be shown.
358a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  PrefService* prefs = g_browser_process->local_state();
359a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  if (!prefs || prefs->GetInteger(prefs::kShowFirstRunBubbleOption) !=
360a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      first_run::FIRST_RUN_BUBBLE_SHOW) {
361a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    delete this;
362a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    return;
363a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  }
364a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
365a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  content::WebContents* contents =
366a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      browser->tab_strip_model()->GetActiveWebContents();
367a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
368a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Suppress the first run bubble if a Gaia sign in page, the continue
369a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // URL for the sign in page or the sync setup page is showing.
370a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  if (contents &&
3715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      (contents->GetURL().GetOrigin().spec() ==
3725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)           chrome::kChromeUIChromeSigninURL ||
3735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)       gaia::IsGaiaSignonRealm(contents->GetURL().GetOrigin()) ||
374a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)       signin::IsContinueUrlForWebBasedSigninFlow(contents->GetURL()) ||
375effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch       (contents->GetURL() ==
376effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch        chrome::GetSettingsUrl(chrome::kSyncSetupSubPage)))) {
377a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    return;
378a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  }
379a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
3805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (contents && contents->GetURL().SchemeIs(content::kChromeUIScheme)) {
381a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    // Suppress the first run bubble if 'make chrome metro' flow is showing.
382a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    if (contents->GetURL().host() == chrome::kChromeUIMetroFlowHost)
383a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      return;
384a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
385a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    // Suppress the first run bubble if the NTP sync promo bubble is showing
386a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    // or if sign in is in progress.
387a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    if (contents->GetURL().host() == chrome::kChromeUINewTabHost) {
388a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      Profile* profile =
389a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)          Profile::FromBrowserContext(contents->GetBrowserContext());
390a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      SigninManagerBase* manager =
391a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)          SigninManagerFactory::GetForProfile(profile);
39258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      bool signin_in_progress = manager && manager->AuthInProgress();
393a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      bool is_promo_bubble_visible =
394bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch          profile->GetPrefs()->GetBoolean(prefs::kSignInPromoShowNTPBubble);
395a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
396a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      if (is_promo_bubble_visible || signin_in_progress)
397a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)        return;
398a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    }
399a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  }
400a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
401a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Suppress the first run bubble if a global error bubble is pending.
402a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  GlobalErrorService* global_error_service =
403a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      GlobalErrorServiceFactory::GetForProfile(browser->profile());
404a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  if (global_error_service->GetFirstGlobalErrorWithBubbleView() != NULL)
405a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    return;
406a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
407a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Reset the preference and notifications to avoid showing the bubble again.
408a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  prefs->SetInteger(prefs::kShowFirstRunBubbleOption,
409a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                    first_run::FIRST_RUN_BUBBLE_DONT_SHOW);
410a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
411a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Show the bubble now and destroy this bubble launcher.
412a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  browser->ShowFirstRunBubble();
413a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  delete this;
414a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
415a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
4162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)static base::LazyInstance<base::FilePath> master_prefs_path_for_testing
4172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    = LAZY_INSTANCE_INITIALIZER;
4182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4190f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Loads master preferences from the master preference file into the installer
4200f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// master preferences. Returns the pointer to installer::MasterPreferences
4210f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// object if successful; otherwise, returns NULL.
4220f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)installer::MasterPreferences* LoadMasterPrefs() {
4230f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  base::FilePath master_prefs_path;
4242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!master_prefs_path_for_testing.Get().empty())
4250f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    master_prefs_path = master_prefs_path_for_testing.Get();
4262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  else
4270f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    master_prefs_path = base::FilePath(first_run::internal::MasterPrefsPath());
4280f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  if (master_prefs_path.empty())
4295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return NULL;
4305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  installer::MasterPreferences* install_prefs =
4310f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      new installer::MasterPreferences(master_prefs_path);
4325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!install_prefs->read_from_file()) {
4335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    delete install_prefs;
4345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return NULL;
4355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return install_prefs;
4385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
440a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// Makes chrome the user's default browser according to policy or
441a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// |make_chrome_default_for_user| if no policy is set.
442a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)void ProcessDefaultBrowserPolicy(bool make_chrome_default_for_user) {
443a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Only proceed if chrome can be made default unattended. The interactive case
444a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // (Windows 8+) is handled by the first run default browser prompt.
445a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  if (ShellIntegration::CanSetAsDefaultBrowser() ==
446a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)          ShellIntegration::SET_DEFAULT_UNATTENDED) {
447a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    // The policy has precedence over the user's choice.
448a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    if (g_browser_process->local_state()->IsManagedPreference(
449a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)            prefs::kDefaultBrowserSettingEnabled)) {
450a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      if (g_browser_process->local_state()->GetBoolean(
451a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)          prefs::kDefaultBrowserSettingEnabled)) {
452a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)        ShellIntegration::SetAsDefaultBrowser();
453a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      }
454a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    } else if (make_chrome_default_for_user) {
455a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)        ShellIntegration::SetAsDefaultBrowser();
456a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    }
457a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  }
458a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}
459a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
4600f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}  // namespace
4610f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
4620f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)namespace first_run {
4630f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)namespace internal {
4640f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
4655c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo LiuFirstRunState g_first_run = FIRST_RUN_UNKNOWN;
4660f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
4675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void SetupMasterPrefsFromInstallPrefs(
4682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const installer::MasterPreferences& install_prefs,
4692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    MasterPrefs* out_prefs) {
470c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ConvertStringVectorToGURLVector(
471c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      install_prefs.GetFirstRunTabs(), &out_prefs->new_tabs);
472c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
473c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  install_prefs.GetInt(installer::master_preferences::kDistroPingDelay,
474c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                       &out_prefs->ping_delay);
475c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
4765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool value = false;
4772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (install_prefs.GetBool(
4785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          installer::master_preferences::kDistroImportSearchPref, &value)) {
4795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (value) {
4805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      out_prefs->do_import_items |= importer::SEARCH_ENGINES;
4815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    } else {
4825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      out_prefs->dont_import_items |= importer::SEARCH_ENGINES;
4835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
4845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If we're suppressing the first-run bubble, set that preference now.
4875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Otherwise, wait until the user has completed first run to set it, so the
4885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // user is guaranteed to see the bubble iff he or she has completed the first
4895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // run process.
4902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (install_prefs.GetBool(
4915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          installer::master_preferences::kDistroSuppressFirstRunBubble,
4925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          &value) && value)
4932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    SetShowFirstRunBubblePref(FIRST_RUN_BUBBLE_SUPPRESS);
4945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (install_prefs.GetBool(
4965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          installer::master_preferences::kDistroImportHistoryPref,
4975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          &value)) {
4985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (value) {
4995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      out_prefs->do_import_items |= importer::HISTORY;
5005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    } else {
5015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      out_prefs->dont_import_items |= importer::HISTORY;
5025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
5035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
5045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string not_used;
5062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  out_prefs->homepage_defined = install_prefs.GetString(
5075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      prefs::kHomePage, &not_used);
5085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (install_prefs.GetBool(
5105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          installer::master_preferences::kDistroImportHomePagePref,
5115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          &value)) {
5125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (value) {
5135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      out_prefs->do_import_items |= importer::HOME_PAGE;
5145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    } else {
5155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      out_prefs->dont_import_items |= importer::HOME_PAGE;
5165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
5175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
5185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Bookmarks are never imported unless specifically turned on.
5202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (install_prefs.GetBool(
5215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          installer::master_preferences::kDistroImportBookmarksPref,
5225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          &value)) {
5235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (value)
5245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      out_prefs->do_import_items |= importer::FAVORITES;
5255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    else
5265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      out_prefs->dont_import_items |= importer::FAVORITES;
5275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
5285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (install_prefs.GetBool(
5305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          installer::master_preferences::kMakeChromeDefaultForUser,
5315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          &value) && value) {
532a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    out_prefs->make_chrome_default_for_user = true;
5335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
5345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (install_prefs.GetBool(
5365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          installer::master_preferences::kSuppressFirstRunDefaultBrowserPrompt,
5375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          &value) && value) {
5385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    out_prefs->suppress_first_run_default_browser_prompt = true;
5395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
5402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
54190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  install_prefs.GetString(
54290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      installer::master_preferences::kDistroImportBookmarksFromFilePref,
54390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      &out_prefs->import_bookmarks_path);
54490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
5452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  out_prefs->variations_seed = install_prefs.GetVariationsSeed();
5465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  out_prefs->variations_seed_signature =
5475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      install_prefs.GetVariationsSeedSignature();
548c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
549c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  install_prefs.GetString(
55090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      installer::master_preferences::kDistroSuppressDefaultBrowserPromptPref,
55190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      &out_prefs->suppress_default_browser_prompt_for_version);
5525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
554effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochbool GetFirstRunSentinelFilePath(base::FilePath* path) {
555effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  base::FilePath user_data_dir;
556effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (!PathService::Get(chrome::DIR_USER_DATA, &user_data_dir))
557effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    return false;
558effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  *path = user_data_dir.Append(chrome::kFirstRunSentinel);
559effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  return true;
560effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
561effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
562a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)bool CreateSentinel() {
563a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  base::FilePath first_run_sentinel;
564effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  return GetFirstRunSentinelFilePath(&first_run_sentinel) &&
565effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      base::WriteFile(first_run_sentinel, "", 0) != -1;
566a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)}
567a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
5685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// -- Platform-specific functions --
5695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if !defined(OS_LINUX) && !defined(OS_BSD)
5715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool IsOrganicFirstRun() {
5725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string brand;
57346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  google_brand::GetBrand(&brand);
57446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  return google_brand::IsOrganicFirstRun(brand);
5755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
5775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace internal
5795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)MasterPrefs::MasterPrefs()
5815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : ping_delay(0),
5825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      homepage_defined(false),
5835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      do_import_items(0),
5845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      dont_import_items(0),
585a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      make_chrome_default_for_user(false),
5865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      suppress_first_run_default_browser_prompt(false) {
5875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)MasterPrefs::~MasterPrefs() {}
5905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool IsChromeFirstRun() {
5925c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  if (internal::g_first_run == internal::FIRST_RUN_UNKNOWN) {
5935c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    internal::g_first_run = internal::FIRST_RUN_FALSE;
594effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    const CommandLine* command_line = CommandLine::ForCurrentProcess();
595effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    if (command_line->HasSwitch(switches::kForceFirstRun) ||
596effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch        (!command_line->HasSwitch(switches::kNoFirstRun) &&
597effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch         !internal::IsFirstRunSentinelPresent())) {
5985c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      internal::g_first_run = internal::FIRST_RUN_TRUE;
599effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    }
6005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6015c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  return internal::g_first_run == internal::FIRST_RUN_TRUE;
6025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6045c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#if defined(OS_MACOSX)
605ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdochbool IsFirstRunSuppressed(const CommandLine& command_line) {
606a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return command_line.HasSwitch(switches::kNoFirstRun);
607ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch}
6085c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#endif
609ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch
610a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)void CreateSentinelIfNeeded() {
611a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (IsChromeFirstRun())
612a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    internal::CreateSentinel();
6135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)std::string GetPingDelayPrefName() {
6165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return base::StringPrintf("%s.%s",
6175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                            installer::master_preferences::kDistroDict,
6185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                            installer::master_preferences::kDistroPingDelay);
6195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6217dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochvoid RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
622c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  registry->RegisterIntegerPref(
623c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      GetPingDelayPrefName().c_str(),
624c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      0,
625c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
6265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool SetShowFirstRunBubblePref(FirstRunBubbleOptions show_bubble_option) {
6295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PrefService* local_state = g_browser_process->local_state();
6305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!local_state)
6315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
6322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (local_state->GetInteger(
6332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          prefs::kShowFirstRunBubbleOption) != FIRST_RUN_BUBBLE_SUPPRESS) {
6342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Set the new state as long as the bubble wasn't explicitly suppressed
6352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // already.
6362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    local_state->SetInteger(prefs::kShowFirstRunBubbleOption,
6372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                            show_bubble_option);
6382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
6395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
6405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void SetShouldShowWelcomePage() {
643c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  g_should_show_welcome_page = true;
6445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool ShouldShowWelcomePage() {
647c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  bool retval = g_should_show_welcome_page;
648c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  g_should_show_welcome_page = false;
6492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return retval;
6502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
6512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void SetShouldDoPersonalDataManagerFirstRun() {
653c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  g_should_do_autofill_personal_data_manager_first_run = true;
6542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
6552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool ShouldDoPersonalDataManagerFirstRun() {
657c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  bool retval = g_should_do_autofill_personal_data_manager_first_run;
658c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  g_should_do_autofill_personal_data_manager_first_run = false;
6592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return retval;
6605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void LogFirstRunMetric(FirstRunBubbleMetric metric) {
6635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  UMA_HISTOGRAM_ENUMERATION("FirstRun.SearchEngineBubble", metric,
6645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                            NUM_FIRST_RUN_BUBBLE_METRICS);
6655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void SetMasterPrefsPathForTesting(const base::FilePath& master_prefs) {
6680f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  master_prefs_path_for_testing.Get() = master_prefs;
6692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
6702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)ProcessMasterPreferencesResult ProcessMasterPreferences(
6722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath& user_data_dir,
6732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    MasterPrefs* out_prefs) {
6742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(!user_data_dir.empty());
6752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6760f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  scoped_ptr<installer::MasterPreferences> install_prefs(LoadMasterPrefs());
6772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
678c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Default value in case master preferences is missing or corrupt, or
679c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // ping_delay is missing.
680c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  out_prefs->ping_delay = 90;
681c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (install_prefs.get()) {
682c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (!internal::ShowPostInstallEULAIfNeeded(install_prefs.get()))
683c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return EULA_EXIT_NOW;
6842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (!chrome_prefs::InitializePrefsFromMasterPrefs(
6865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            profiles::GetDefaultProfileDir(user_data_dir),
6875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            install_prefs->master_dictionary())) {
6885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      DLOG(ERROR) << "Failed to initialize from master_preferences.";
6895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }
6902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
691c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DoDelayedInstallExtensionsIfNeeded(install_prefs.get());
6922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
693c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    internal::SetupMasterPrefsFromInstallPrefs(*install_prefs, out_prefs);
694c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
6952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
696a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  return FIRST_RUN_PROCEED;
6972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
6982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void AutoImport(
7002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    Profile* profile,
7012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    bool homepage_defined,
7022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    int import_items,
70390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    int dont_import_items,
70490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    const std::string& import_bookmarks_path) {
7052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath local_state_path;
7062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  PathService::Get(chrome::FILE_LOCAL_STATE, &local_state_path);
7077dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  bool local_state_file_exists = base::PathExists(local_state_path);
7082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7095c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  // It may be possible to do the if block below asynchronously. In which case,
7105c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  // get rid of this RunLoop. http://crbug.com/366116.
7115c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  base::RunLoop run_loop;
7125c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  scoped_ptr<ImporterList> importer_list(new ImporterList());
7135c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  importer_list->DetectSourceProfiles(
7145c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      g_browser_process->GetApplicationLocale(),
7155c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      false,  // include_interactive_profiles?
7165c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      run_loop.QuitClosure());
7175c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  run_loop.Run();
7182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Do import if there is an available profile for us to import.
7202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (importer_list->count() > 0) {
7212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (internal::IsOrganicFirstRun()) {
7222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // Home page is imported in organic builds only unless turned off or
7232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // defined in master_preferences.
7242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      if (homepage_defined) {
7252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        dont_import_items |= importer::HOME_PAGE;
7262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        if (import_items & importer::HOME_PAGE)
7272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          import_items &= ~importer::HOME_PAGE;
7282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      }
7292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // Search engines are not imported automatically in organic builds if the
7302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // user already has a user preferences directory.
7312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      if (local_state_file_exists) {
7322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        dont_import_items |= importer::SEARCH_ENGINES;
7332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        if (import_items & importer::SEARCH_ENGINES)
7342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          import_items &= ~importer::SEARCH_ENGINES;
7352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      }
7362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
7372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    PrefService* user_prefs = profile->GetPrefs();
7391e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    int items = 0;
7402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    SetImportItem(user_prefs,
7422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  prefs::kImportHistory,
7432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  import_items,
7442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  dont_import_items,
7452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  importer::HISTORY,
746c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                  &items);
7472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    SetImportItem(user_prefs,
7482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  prefs::kImportHomepage,
7492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  import_items,
7502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  dont_import_items,
7512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  importer::HOME_PAGE,
752c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                  &items);
7532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    SetImportItem(user_prefs,
7542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  prefs::kImportSearchEngine,
7552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  import_items,
7562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  dont_import_items,
7572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  importer::SEARCH_ENGINES,
758c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                  &items);
7592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    SetImportItem(user_prefs,
7602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  prefs::kImportBookmarks,
7612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  import_items,
7622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  dont_import_items,
7632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  importer::FAVORITES,
764c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                  &items);
765c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
7661e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    // Deletes itself.
7671e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    ExternalProcessImporterHost* importer_host =
7681e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        new ExternalProcessImporterHost;
7691e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
7701e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    // Don't show the warning dialog if import fails.
7711e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    importer_host->set_headless();
7721e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
773c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    importer::LogImporterUseToMetrics(
774c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        "AutoImport", importer_list->GetSourceProfileAt(0).importer_type);
7752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7765c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    ImportSettings(profile, importer_host, importer_list.Pass(), items);
77790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
77890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
77990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (!import_bookmarks_path.empty()) {
78090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    // Deletes itself.
781eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    ExternalProcessImporterHost* file_importer_host =
782eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        new ExternalProcessImporterHost;
78390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    file_importer_host->set_headless();
78490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
78590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    ImportFromFile(profile, file_importer_host, import_bookmarks_path);
7862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
7872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  content::RecordAction(UserMetricsAction("FirstRunDef_Accept"));
7892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
79090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  g_auto_import_state |= AUTO_IMPORT_CALLED;
7912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
7922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
793a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)void DoPostImportTasks(Profile* profile, bool make_chrome_default_for_user) {
794a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Only set default browser after import as auto import relies on the current
795a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // default browser to know what to import from.
796a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  ProcessDefaultBrowserPolicy(make_chrome_default_for_user);
7972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Display the first run bubble if there is a default search provider.
7992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  TemplateURLService* template_url =
8002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      TemplateURLServiceFactory::GetForProfile(profile);
8012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (template_url && template_url->GetDefaultSearchProvider())
8022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    FirstRunBubbleLauncher::ShowFirstRunBubbleSoon();
8032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  SetShouldShowWelcomePage();
8042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  SetShouldDoPersonalDataManagerFirstRun();
8052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
806b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  internal::DoPostImportPlatformSpecificTasks(profile);
8072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
8082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
80990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)uint16 auto_import_state() {
81090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return g_auto_import_state;
8112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
8122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
8135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace first_run
814