| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/default_apps.h" | 5 #include "chrome/browser/extensions/default_apps.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/metrics/field_trial.h" | 8 #include "base/metrics/field_trial.h" |
| 9 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
| 10 #include "chrome/browser/first_run/first_run.h" | 10 #include "chrome/browser/first_run/first_run.h" |
| 11 #include "chrome/browser/extensions/default_apps_trial.h" | 11 #include "chrome/browser/extensions/default_apps_trial.h" |
| 12 #include "chrome/browser/prefs/pref_service.h" | 12 #include "chrome/browser/prefs/pref_service.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/common/chrome_switches.h" | 14 #include "chrome/common/chrome_switches.h" |
| 15 #include "chrome/common/chrome_version_info.h" | 15 #include "chrome/common/chrome_version_info.h" |
| 16 #include "chrome/common/extensions/extension.h" | 16 #include "chrome/common/extensions/extension.h" |
| 17 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" |
| 18 #include "ui/base/l10n/l10n_util.h" | 18 #include "ui/base/l10n/l10n_util.h" |
| 19 | 19 |
| 20 namespace default_apps { | 20 namespace default_apps { |
| 21 | 21 |
| 22 bool ShouldInstallInProfile(Profile* profile) { | 22 bool Provider::ShouldInstallInProfile() { |
| 23 // We decide to install or not install default apps based on the following | 23 // We decide to install or not install default apps based on the following |
| 24 // criteria, from highest priority to lowest priority: | 24 // criteria, from highest priority to lowest priority: |
| 25 // | 25 // |
| 26 // - If this instance of chrome is participating in the default apps | 26 // - If this instance of chrome is participating in the default apps |
| 27 // field trial, then install apps based on the group. | 27 // field trial, then install apps based on the group. |
| 28 // - The command line option. Tests use this option to disable installation | 28 // - The command line option. Tests use this option to disable installation |
| 29 // of default apps in some cases. | 29 // of default apps in some cases. |
| 30 // - If the locale is not compatible with the defaults, don't install them. | 30 // - If the locale is not compatible with the defaults, don't install them. |
| 31 // - The kDefaultApps preferences value in the profile. This value is | 31 // - The kDefaultApps preferences value in the profile. This value is |
| 32 // usually set in the master_preferences file. | 32 // usually set in the master_preferences file. |
| 33 bool install_apps = | 33 bool install_apps = |
| 34 profile->GetPrefs()->GetString(prefs::kDefaultApps) == "install"; | 34 profile_->GetPrefs()->GetString(prefs::kDefaultApps) == "install"; |
| 35 | 35 |
| 36 default_apps::InstallState state = | 36 InstallState state = |
| 37 static_cast<default_apps::InstallState>(profile->GetPrefs()->GetInteger( | 37 static_cast<InstallState>(profile_->GetPrefs()->GetInteger( |
| 38 prefs::kDefaultAppsInstallState)); | 38 prefs::kDefaultAppsInstallState)); |
| 39 | 39 |
| 40 isMigration_ = (state == kProvideLegacyDefaultApps); |
| 41 |
| 40 switch (state) { | 42 switch (state) { |
| 41 case default_apps::kUnknown: { | 43 case kUnknown: { |
| 42 // This is the first time the default apps feature runs on this profile. | 44 // This is the first time the default apps feature runs on this profile. |
| 43 // Determine if we want to install them or not. | 45 // Determine if we want to install them or not. |
| 44 chrome::VersionInfo version_info; | 46 chrome::VersionInfo version_info; |
| 45 if (!profile->WasCreatedByVersionOrLater(version_info.Version().c_str())) | 47 if (!profile_->WasCreatedByVersionOrLater(version_info.Version().c_str())) |
| 46 install_apps = false; | 48 install_apps = false; |
| 47 break; | 49 break; |
| 48 } | 50 } |
| 49 | 51 |
| 50 // The old default apps were provided as external extensions and were | 52 // The old default apps were provided as external extensions and were |
| 51 // installed everytime Chrome was run. Thus, changing the list of default | 53 // installed everytime Chrome was run. Thus, changing the list of default |
| 52 // apps affected all users. Migrate old default apps to new mechanism where | 54 // apps affected all users. Migrate old default apps to new mechanism where |
| 53 // they are installed only once as INTERNAL. | 55 // they are installed only once as INTERNAL. |
| 54 // TODO(grv) : remove after Q1-2013. | 56 // TODO(grv) : remove after Q1-2013. |
| 55 case default_apps::kProvideLegacyDefaultApps: | 57 case kProvideLegacyDefaultApps: |
| 56 profile->GetPrefs()->SetInteger( | 58 profile_->GetPrefs()->SetInteger( |
| 57 prefs::kDefaultAppsInstallState, | 59 prefs::kDefaultAppsInstallState, |
| 58 default_apps::kAlreadyInstalledDefaultApps); | 60 kAlreadyInstalledDefaultApps); |
| 59 break; | 61 break; |
| 60 | 62 |
| 61 case default_apps::kAlreadyInstalledDefaultApps: | 63 case kAlreadyInstalledDefaultApps: |
| 62 case default_apps::kNeverInstallDefaultApps: | 64 case kNeverInstallDefaultApps: |
| 63 install_apps = false; | 65 install_apps = false; |
| 64 break; | 66 break; |
| 65 default: | 67 default: |
| 66 NOTREACHED(); | 68 NOTREACHED(); |
| 67 } | 69 } |
| 68 | 70 |
| 69 if (install_apps && !isLocaleSupported()) { | 71 if (install_apps && !isLocaleSupported()) { |
| 70 install_apps = false; | 72 install_apps = false; |
| 71 } | 73 } |
| 72 | 74 |
| 73 if (CommandLine::ForCurrentProcess()->HasSwitch( | 75 if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 74 switches::kDisableDefaultApps)) { | 76 switches::kDisableDefaultApps)) { |
| 75 install_apps = false; | 77 install_apps = false; |
| 76 } | 78 } |
| 77 | 79 |
| 78 if (base::FieldTrialList::TrialExists(kDefaultAppsTrialName)) { | 80 if (base::FieldTrialList::TrialExists(kDefaultAppsTrialName)) { |
| 79 install_apps = base::FieldTrialList::Find( | 81 install_apps = base::FieldTrialList::Find( |
| 80 kDefaultAppsTrialName)->group_name() != kDefaultAppsTrialNoAppsGroup; | 82 kDefaultAppsTrialName)->group_name() != kDefaultAppsTrialNoAppsGroup; |
| 81 } | 83 } |
| 82 | 84 |
| 83 // Default apps are only installed on profile creation or a new chrome | 85 // Default apps are only installed on profile creation or a new chrome |
| 84 // download. | 86 // download. |
| 85 if (state == default_apps::kUnknown) { | 87 if (state == kUnknown) { |
| 86 if (install_apps) { | 88 if (install_apps) { |
| 87 profile->GetPrefs()->SetInteger( | 89 profile_->GetPrefs()->SetInteger( |
| 88 prefs::kDefaultAppsInstallState, | 90 prefs::kDefaultAppsInstallState, |
| 89 default_apps::kAlreadyInstalledDefaultApps); | 91 kAlreadyInstalledDefaultApps); |
| 90 } else { | 92 } else { |
| 91 profile->GetPrefs()->SetInteger(prefs::kDefaultAppsInstallState, | 93 profile_->GetPrefs()->SetInteger(prefs::kDefaultAppsInstallState, |
| 92 default_apps::kNeverInstallDefaultApps); | 94 kNeverInstallDefaultApps); |
| 93 } | 95 } |
| 94 } | 96 } |
| 95 | 97 |
| 96 return install_apps; | 98 return install_apps; |
| 97 } | 99 } |
| 98 | 100 |
| 99 void RegisterUserPrefs(PrefService* prefs) { | 101 void RegisterUserPrefs(PrefService* prefs) { |
| 100 prefs->RegisterIntegerPref(prefs::kDefaultAppsInstallState, kUnknown, | 102 prefs->RegisterIntegerPref(prefs::kDefaultAppsInstallState, kUnknown, |
| 101 PrefService::UNSYNCABLE_PREF); | 103 PrefService::UNSYNCABLE_PREF); |
| 102 } | 104 } |
| 103 | 105 |
| 104 Provider::Provider(Profile* profile, | 106 Provider::Provider(Profile* profile, |
| 105 VisitorInterface* service, | 107 VisitorInterface* service, |
| 106 extensions::ExternalLoader* loader, | 108 extensions::ExternalLoader* loader, |
| 107 extensions::Extension::Location crx_location, | 109 extensions::Extension::Location crx_location, |
| 108 extensions::Extension::Location download_location, | 110 extensions::Extension::Location download_location, |
| 109 int creation_flags) | 111 int creation_flags) |
| 110 : extensions::ExternalProviderImpl(service, loader, crx_location, | 112 : extensions::ExternalProviderImpl(service, loader, crx_location, |
| 111 download_location, creation_flags), | 113 download_location, creation_flags), |
| 112 profile_(profile) { | 114 profile_(profile), isMigration_(false) { |
| 113 DCHECK(profile); | 115 DCHECK(profile); |
| 114 set_auto_acknowledge(true); | 116 set_auto_acknowledge(true); |
| 115 } | 117 } |
| 116 | 118 |
| 117 void Provider::VisitRegisteredExtension() { | 119 void Provider::VisitRegisteredExtension() { |
| 118 if (!profile_ || !ShouldInstallInProfile(profile_)) { | 120 if (!profile_ || !ShouldInstallInProfile()) { |
| 119 base::DictionaryValue* prefs = new base::DictionaryValue; | 121 base::DictionaryValue* prefs = new base::DictionaryValue; |
| 120 SetPrefs(prefs); | 122 SetPrefs(prefs); |
| 121 return; | 123 return; |
| 122 } | 124 } |
| 123 | 125 |
| 124 extensions::ExternalProviderImpl::VisitRegisteredExtension(); | 126 extensions::ExternalProviderImpl::VisitRegisteredExtension(); |
| 125 } | 127 } |
| 126 | 128 |
| 129 void Provider::SetPrefs(base::DictionaryValue* prefs) { |
| 130 if (isMigration_) { |
| 131 std::set<std::string> new_default_apps; |
| 132 for (base::DictionaryValue::key_iterator i = prefs->begin_keys(); |
| 133 i != prefs->end_keys(); ++i) { |
| 134 if (!isOldDefaultApp(*i)) { |
| 135 new_default_apps.insert(*i); |
| 136 } |
| 137 } |
| 138 // Filter out the new default apps for migrating users. |
| 139 for (std::set<std::string>::iterator it = new_default_apps.begin(); |
| 140 it != new_default_apps.end(); ++it) { |
| 141 prefs->Remove(*it, NULL); |
| 142 } |
| 143 } |
| 144 |
| 145 ExternalProviderImpl::SetPrefs(prefs); |
| 146 } |
| 147 |
| 148 bool isOldDefaultApp(const std::string& extension_id) { |
| 149 const std::string gmail_id = "pjkljhegncpnkpknbcohdijeoejaedia"; |
| 150 const std::string search_id = "coobgpohoikkiipiblmjeljniedjpjpf"; |
| 151 const std::string youtube_id = "blpcfgokakmgnkcojhhkbfbldkacnbeo"; |
| 152 return extension_id == gmail_id || extension_id == search_id; |
| 153 // || extension_id == youtube_id; |
| 154 } |
| 155 |
| 127 bool isLocaleSupported() { | 156 bool isLocaleSupported() { |
| 128 // Don't bother installing default apps in locales where it is known that | 157 // Don't bother installing default apps in locales where it is known that |
| 129 // they don't work. | 158 // they don't work. |
| 130 // TODO(rogerta): Do this check dynamically once the webstore can expose | 159 // TODO(rogerta): Do this check dynamically once the webstore can expose |
| 131 // an API. See http://crbug.com/101357 | 160 // an API. See http://crbug.com/101357 |
| 132 const std::string& locale = g_browser_process->GetApplicationLocale(); | 161 const std::string& locale = g_browser_process->GetApplicationLocale(); |
| 133 static const char* unsupported_locales[] = {"CN", "TR", "IR"}; | 162 static const char* unsupported_locales[] = {"CN", "TR", "IR"}; |
| 134 for (size_t i = 0; i < arraysize(unsupported_locales); ++i) { | 163 for (size_t i = 0; i < arraysize(unsupported_locales); ++i) { |
| 135 if (EndsWith(locale, unsupported_locales[i], false)) { | 164 if (EndsWith(locale, unsupported_locales[i], false)) { |
| 136 return false; | 165 return false; |
| 137 } | 166 } |
| 138 } | 167 } |
| 139 return true; | 168 return true; |
| 140 } | 169 } |
| 141 | 170 |
| 142 } // namespace default_apps | 171 } // namespace default_apps |
| OLD | NEW |