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/prefs/session_startup_pref.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
98bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "base/metrics/histogram.h"
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/prefs/pref_service.h"
111e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "base/prefs/scoped_user_pref_update.h"
128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "base/time/time.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/values.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/version.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/profiles/profile.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/pref_names.h"
17cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/pref_registry/pref_registry_syncable.h"
18f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "components/url_fixer/url_fixer.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_MACOSX)
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/cocoa/window_restore_utils.h"
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
268bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)enum StartupURLsMigrationMetrics {
278bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  STARTUP_URLS_MIGRATION_METRICS_PERFORMED,
288bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  STARTUP_URLS_MIGRATION_METRICS_NOT_PRESENT,
298bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  STARTUP_URLS_MIGRATION_METRICS_RESET,
308bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  STARTUP_URLS_MIGRATION_METRICS_MAX,
318bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)};
328bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Converts a SessionStartupPref::Type to an integer written to prefs.
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int TypeToPrefValue(SessionStartupPref::Type type) {
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  switch (type) {
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case SessionStartupPref::LAST: return SessionStartupPref::kPrefValueLast;
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case SessionStartupPref::URLS: return SessionStartupPref::kPrefValueURLs;
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    default:                       return SessionStartupPref::kPrefValueNewTab;
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void SetNewURLList(PrefService* prefs) {
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (prefs->IsUserModifiablePreference(prefs::kURLsToRestoreOnStartup)) {
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    base::ListValue new_url_pref_list;
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    base::StringValue* home_page =
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        new base::StringValue(prefs->GetString(prefs::kHomePage));
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    new_url_pref_list.Append(home_page);
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    prefs->Set(prefs::kURLsToRestoreOnStartup, new_url_pref_list);
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLListToPref(const base::ListValue* url_list, SessionStartupPref* pref) {
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  pref->urls.clear();
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (size_t i = 0; i < url_list->GetSize(); ++i) {
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::string url_text;
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (url_list->GetString(i, &url_text)) {
57f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      GURL fixed_url = url_fixer::FixupURL(url_text, std::string());
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      pref->urls.push_back(fixed_url);
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
667dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochvoid SessionStartupPref::RegisterProfilePrefs(
67c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    user_prefs::PrefRegistrySyncable* registry) {
68c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  registry->RegisterIntegerPref(
69c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      prefs::kRestoreOnStartup,
70c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      TypeToPrefValue(GetDefaultStartupType()),
71c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  registry->RegisterListPref(prefs::kURLsToRestoreOnStartup,
73c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                             user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
748bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  registry->RegisterListPref(prefs::kURLsToRestoreOnStartupOld,
758bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                             user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
76c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  registry->RegisterBooleanPref(
77c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      prefs::kRestoreOnStartupMigrated,
78c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      false,
79c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
808bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  registry->RegisterInt64Pref(
818bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      prefs::kRestoreStartupURLsMigrationTime,
828bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      false,
838bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)SessionStartupPref::Type SessionStartupPref::GetDefaultStartupType() {
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_CHROMEOS)
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return SessionStartupPref::LAST;
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return SessionStartupPref::DEFAULT;
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void SessionStartupPref::SetStartupPref(
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Profile* profile,
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const SessionStartupPref& pref) {
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(profile);
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SetStartupPref(profile->GetPrefs(), pref);
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void SessionStartupPref::SetStartupPref(PrefService* prefs,
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                        const SessionStartupPref& pref) {
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(prefs);
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!SessionStartupPref::TypeIsManaged(prefs))
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    prefs->SetInteger(prefs::kRestoreOnStartup, TypeToPrefValue(pref.type));
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!SessionStartupPref::URLsAreManaged(prefs)) {
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Always save the URLs, that way the UI can remain consistent even if the
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // user changes the startup type pref.
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Ownership of the ListValue retains with the pref service.
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ListPrefUpdate update(prefs, prefs::kURLsToRestoreOnStartup);
1165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::ListValue* url_pref_list = update.Get();
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DCHECK(url_pref_list);
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    url_pref_list->Clear();
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    for (size_t i = 0; i < pref.urls.size(); ++i) {
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      url_pref_list->Set(static_cast<int>(i),
1215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                         new base::StringValue(pref.urls[i].spec()));
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)SessionStartupPref SessionStartupPref::GetStartupPref(Profile* profile) {
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(profile);
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return GetStartupPref(profile->GetPrefs());
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)SessionStartupPref SessionStartupPref::GetStartupPref(PrefService* prefs) {
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(prefs);
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MigrateIfNecessary(prefs);
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MigrateMacDefaultPrefIfNecessary(prefs);
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SessionStartupPref pref(
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      PrefValueToType(prefs->GetInteger(prefs::kRestoreOnStartup)));
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Always load the urls, even if the pref type isn't URLS. This way the
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // preferences panels can show the user their last choice.
1445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const base::ListValue* url_list =
1455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      prefs->GetList(prefs::kURLsToRestoreOnStartup);
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  URLListToPref(url_list, &pref);
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return pref;
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void SessionStartupPref::MigrateIfNecessary(PrefService* prefs) {
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(prefs);
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1558bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Check if we need to migrate the old version of the startup URLs preference
1568bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // to the new name, and also send metrics about the migration.
1578bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  StartupURLsMigrationMetrics metrics_result =
1588bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      STARTUP_URLS_MIGRATION_METRICS_MAX;
1598bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  const base::ListValue* old_startup_urls =
1608bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      prefs->GetList(prefs::kURLsToRestoreOnStartupOld);
1618bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (!prefs->GetUserPrefValue(prefs::kRestoreStartupURLsMigrationTime)) {
1628bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    // Record the absence of the migration timestamp, this will get overwritten
1638bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    // below if migration occurs now.
1648bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    metrics_result = STARTUP_URLS_MIGRATION_METRICS_NOT_PRESENT;
1658bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1668bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    // Seems like we never migrated, do it if necessary.
1678bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    if (!prefs->GetUserPrefValue(prefs::kURLsToRestoreOnStartup)) {
1688bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      if (old_startup_urls && !old_startup_urls->empty()) {
1698bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        prefs->Set(prefs::kURLsToRestoreOnStartup, *old_startup_urls);
1708bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        prefs->ClearPref(prefs::kURLsToRestoreOnStartupOld);
1718bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      }
1728bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      metrics_result = STARTUP_URLS_MIGRATION_METRICS_PERFORMED;
1738bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    }
1748bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1758bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    prefs->SetInt64(prefs::kRestoreStartupURLsMigrationTime,
1768bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                    base::Time::Now().ToInternalValue());
1778bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  } else if (old_startup_urls && !old_startup_urls->empty()) {
1788bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    // Migration needs to be reset.
1798bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    prefs->ClearPref(prefs::kURLsToRestoreOnStartupOld);
1808bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    base::Time last_migration_time = base::Time::FromInternalValue(
1818bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        prefs->GetInt64(prefs::kRestoreStartupURLsMigrationTime));
1828bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    base::Time now = base::Time::Now();
1838bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    prefs->SetInt64(prefs::kRestoreStartupURLsMigrationTime,
1848bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                    now.ToInternalValue());
1858bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    if (now < last_migration_time)
1868bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      last_migration_time = now;
1870f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    UMA_HISTOGRAM_CUSTOM_TIMES("Settings.StartupURLsResetTime",
1880f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)                               now - last_migration_time,
1890f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)                               base::TimeDelta::FromDays(0),
1900f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)                               base::TimeDelta::FromDays(7),
1910f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)                               50);
1928bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    metrics_result = STARTUP_URLS_MIGRATION_METRICS_RESET;
1938bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
1948bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1958bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Record a metric migration event if something interesting happened.
1968bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (metrics_result != STARTUP_URLS_MIGRATION_METRICS_MAX) {
1978bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    UMA_HISTOGRAM_ENUMERATION(
1988bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)          "Settings.StartupURLsMigration",
1998bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)          metrics_result,
2008bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)          STARTUP_URLS_MIGRATION_METRICS_MAX);
2018bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
2028bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!prefs->GetBoolean(prefs::kRestoreOnStartupMigrated)) {
2048bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    // Read existing values.
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const base::Value* homepage_is_new_tab_page_value =
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        prefs->GetUserPrefValue(prefs::kHomePageIsNewTabPage);
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool homepage_is_new_tab_page = true;
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (homepage_is_new_tab_page_value) {
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (!homepage_is_new_tab_page_value->GetAsBoolean(
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)              &homepage_is_new_tab_page))
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        NOTREACHED();
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const base::Value* restore_on_startup_value =
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        prefs->GetUserPrefValue(prefs::kRestoreOnStartup);
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int restore_on_startup = -1;
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (restore_on_startup_value) {
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (!restore_on_startup_value->GetAsInteger(&restore_on_startup))
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        NOTREACHED();
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // If restore_on_startup has the deprecated value kPrefValueHomePage,
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // migrate it to open the homepage on startup. If 'homepage is NTP' is set,
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // that means just opening the NTP. If not, it means opening a one-item URL
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // list containing the homepage.
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (restore_on_startup == kPrefValueHomePage) {
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (homepage_is_new_tab_page) {
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        prefs->SetInteger(prefs::kRestoreOnStartup, kPrefValueNewTab);
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      } else {
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        prefs->SetInteger(prefs::kRestoreOnStartup, kPrefValueURLs);
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        SetNewURLList(prefs);
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    } else if (!restore_on_startup_value && !homepage_is_new_tab_page &&
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)               GetDefaultStartupType() == DEFAULT) {
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // kRestoreOnStartup was never set by the user, but the homepage was set.
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // Migrate to the list of URLs. (If restore_on_startup was never set,
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // and homepage_is_new_tab_page is true, no action is needed. The new
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // default value is "open the new tab page" which is what we want.)
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      prefs->SetInteger(prefs::kRestoreOnStartup, kPrefValueURLs);
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      SetNewURLList(prefs);
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    prefs->SetBoolean(prefs::kRestoreOnStartupMigrated, true);
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void SessionStartupPref::MigrateMacDefaultPrefIfNecessary(PrefService* prefs) {
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_MACOSX)
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(prefs);
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!restore_utils::IsWindowRestoreEnabled())
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The default startup pref used to be LAST, now it is DEFAULT. Don't change
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the setting for existing profiles (even if the user has never changed it),
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // but make new profiles default to DEFAULT.
2562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool old_profile_version =
2572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      !prefs->FindPreference(
2582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          prefs::kProfileCreatedByVersion)->IsDefaultValue() &&
2592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      Version(prefs->GetString(prefs::kProfileCreatedByVersion)).IsOlderThan(
2602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          "21.0.1180.0");
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (old_profile_version && TypeIsDefault(prefs))
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    prefs->SetInteger(prefs::kRestoreOnStartup, kPrefValueLast);
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool SessionStartupPref::TypeIsManaged(PrefService* prefs) {
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(prefs);
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const PrefService::Preference* pref_restore =
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      prefs->FindPreference(prefs::kRestoreOnStartup);
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(pref_restore);
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return pref_restore->IsManaged();
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool SessionStartupPref::URLsAreManaged(PrefService* prefs) {
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(prefs);
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const PrefService::Preference* pref_urls =
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      prefs->FindPreference(prefs::kURLsToRestoreOnStartup);
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(pref_urls);
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return pref_urls->IsManaged();
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool SessionStartupPref::TypeIsDefault(PrefService* prefs) {
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(prefs);
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const PrefService::Preference* pref_restore =
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      prefs->FindPreference(prefs::kRestoreOnStartup);
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(pref_restore);
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return pref_restore->IsDefaultValue();
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)SessionStartupPref::Type SessionStartupPref::PrefValueToType(int pref_value) {
2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  switch (pref_value) {
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case kPrefValueLast:     return SessionStartupPref::LAST;
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case kPrefValueURLs:     return SessionStartupPref::URLS;
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case kPrefValueHomePage: return SessionStartupPref::HOMEPAGE;
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    default:                 return SessionStartupPref::DEFAULT;
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)SessionStartupPref::SessionStartupPref(Type type) : type(type) {}
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)SessionStartupPref::~SessionStartupPref() {}
306