| 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 static bool ShouldInstallInProfile(Profile* profile) { | 20 namespace default_apps { |
| 21 |
| 22 bool ShouldInstallInProfile(Profile* profile) { |
| 21 // 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 |
| 22 // criteria, from highest priority to lowest priority: | 24 // criteria, from highest priority to lowest priority: |
| 23 // | 25 // |
| 24 // - If this instance of chrome is participating in the default apps | 26 // - If this instance of chrome is participating in the default apps |
| 25 // field trial, then install apps based on the group. | 27 // field trial, then install apps based on the group. |
| 26 // - The command line option. Tests use this option to disable installation | 28 // - The command line option. Tests use this option to disable installation |
| 27 // of default apps in some cases. | 29 // of default apps in some cases. |
| 28 // - 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. |
| 29 // - If the profile says to either always install or never install default | |
| 30 // apps, obey. | |
| 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 default_apps::InstallState state = |
| 37 static_cast<default_apps::InstallState>(profile->GetPrefs()->GetInteger( | 37 static_cast<default_apps::InstallState>(profile->GetPrefs()->GetInteger( |
| 38 prefs::kDefaultAppsInstallState)); | 38 prefs::kDefaultAppsInstallState)); |
| 39 |
| 39 switch (state) { | 40 switch (state) { |
| 40 case default_apps::kUnknown: { | 41 case default_apps::kUnknown: { |
| 41 // This is the first time the default apps feature runs on this profile. | 42 // This is the first time the default apps feature runs on this profile. |
| 42 // Determine if we want to install them or not. | 43 // Determine if we want to install them or not. |
| 43 chrome::VersionInfo version_info; | 44 chrome::VersionInfo version_info; |
| 44 if (!profile->WasCreatedByVersionOrLater(version_info.Version().c_str())) | 45 if (!profile->WasCreatedByVersionOrLater(version_info.Version().c_str())) |
| 45 install_apps = false; | 46 install_apps = false; |
| 46 break; | 47 break; |
| 47 } | 48 } |
| 48 case default_apps::kAlwaysProvideDefaultApps: | 49 |
| 49 install_apps = true; | 50 // The old default apps were provided as external extensions and were |
| 51 // installed everytime Chrome was run. Thus, changing the list of default |
| 52 // apps affected all users. Migrate old default apps to new mechanism where |
| 53 // they are installed only once as INTERNAL. |
| 54 // TODO(grv) : remove after Q1-2013. |
| 55 case default_apps::kProvideLegacyDefaultApps: |
| 56 profile->GetPrefs()->SetInteger( |
| 57 prefs::kDefaultAppsInstallState, |
| 58 default_apps::kAlreadyInstalledDefaultApps); |
| 50 break; | 59 break; |
| 51 case default_apps::kNeverProvideDefaultApps: | 60 |
| 61 case default_apps::kAlreadyInstalledDefaultApps: |
| 62 case default_apps::kNeverInstallDefaultApps: |
| 52 install_apps = false; | 63 install_apps = false; |
| 53 break; | 64 break; |
| 54 default: | 65 default: |
| 55 NOTREACHED(); | 66 NOTREACHED(); |
| 56 } | 67 } |
| 57 | 68 |
| 58 if (install_apps) { | 69 if (install_apps && !isLocaleSupported()) { |
| 59 // Don't bother installing default apps in locales where it is known that | 70 install_apps = false; |
| 60 // they don't work. | |
| 61 // TODO(rogerta): Do this check dynamically once the webstore can expose | |
| 62 // an API. See http://crbug.com/101357 | |
| 63 const std::string& locale = g_browser_process->GetApplicationLocale(); | |
| 64 static const char* unsupported_locales[] = {"CN", "TR", "IR"}; | |
| 65 for (size_t i = 0; i < arraysize(unsupported_locales); ++i) { | |
| 66 if (EndsWith(locale, unsupported_locales[i], false)) { | |
| 67 install_apps = false; | |
| 68 break; | |
| 69 } | |
| 70 } | |
| 71 } | 71 } |
| 72 | 72 |
| 73 if (CommandLine::ForCurrentProcess()->HasSwitch( | 73 if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 74 switches::kDisableDefaultApps)) { | 74 switches::kDisableDefaultApps)) { |
| 75 install_apps = false; | 75 install_apps = false; |
| 76 } | 76 } |
| 77 | 77 |
| 78 if (base::FieldTrialList::TrialExists(kDefaultAppsTrialName)) { | 78 if (base::FieldTrialList::TrialExists(kDefaultAppsTrialName)) { |
| 79 install_apps = base::FieldTrialList::Find( | 79 install_apps = base::FieldTrialList::Find( |
| 80 kDefaultAppsTrialName)->group_name() != kDefaultAppsTrialNoAppsGroup; | 80 kDefaultAppsTrialName)->group_name() != kDefaultAppsTrialNoAppsGroup; |
| 81 } | 81 } |
| 82 | 82 |
| 83 // Save the state if needed. Once it is decided whether we are installing | 83 // Default apps are only installed on profile creation or a new chrome |
| 84 // default apps or not, we want to always respond with same value. Therefore | 84 // download. |
| 85 // on first run of this feature (i.e. the current state is kUnknown) the | |
| 86 // state is updated to remember the choice that was made at this time. The | |
| 87 // next time chrome runs it will use the same decision. | |
| 88 // | |
| 89 // The reason for responding with the same value is that once an external | |
| 90 // extenson provider has provided apps for a given profile, it must continue | |
| 91 // to provide those extensions on each subsequent run. Otherwise the | |
| 92 // extension manager will automatically uninstall the apps. The extension | |
| 93 // manager is smart enough to know not to reinstall the apps on all | |
| 94 // subsequent runs of chrome. | |
| 95 if (state == default_apps::kUnknown) { | 85 if (state == default_apps::kUnknown) { |
| 96 if (install_apps) { | 86 if (install_apps) { |
| 97 profile->GetPrefs()->SetInteger(prefs::kDefaultAppsInstallState, | 87 profile->GetPrefs()->SetInteger( |
| 98 default_apps::kAlwaysProvideDefaultApps); | 88 prefs::kDefaultAppsInstallState, |
| 89 default_apps::kAlreadyInstalledDefaultApps); |
| 99 } else { | 90 } else { |
| 100 profile->GetPrefs()->SetInteger(prefs::kDefaultAppsInstallState, | 91 profile->GetPrefs()->SetInteger(prefs::kDefaultAppsInstallState, |
| 101 default_apps::kNeverProvideDefaultApps); | 92 default_apps::kNeverInstallDefaultApps); |
| 102 } | 93 } |
| 103 } | 94 } |
| 104 | 95 |
| 105 return install_apps; | 96 return install_apps; |
| 106 } | 97 } |
| 107 | 98 |
| 108 namespace default_apps { | |
| 109 | |
| 110 void RegisterUserPrefs(PrefService* prefs) { | 99 void RegisterUserPrefs(PrefService* prefs) { |
| 111 prefs->RegisterIntegerPref(prefs::kDefaultAppsInstallState, kUnknown, | 100 prefs->RegisterIntegerPref(prefs::kDefaultAppsInstallState, kUnknown, |
| 112 PrefService::UNSYNCABLE_PREF); | 101 PrefService::UNSYNCABLE_PREF); |
| 113 } | 102 } |
| 114 | 103 |
| 115 Provider::Provider(Profile* profile, | 104 Provider::Provider(Profile* profile, |
| 116 VisitorInterface* service, | 105 VisitorInterface* service, |
| 117 extensions::ExternalLoader* loader, | 106 extensions::ExternalLoader* loader, |
| 118 extensions::Extension::Location crx_location, | 107 extensions::Extension::Location crx_location, |
| 119 extensions::Extension::Location download_location, | 108 extensions::Extension::Location download_location, |
| 120 int creation_flags) | 109 int creation_flags) |
| 121 : extensions::ExternalProviderImpl(service, loader, crx_location, | 110 : extensions::ExternalProviderImpl(service, loader, crx_location, |
| 122 download_location, creation_flags), | 111 download_location, creation_flags), |
| 123 profile_(profile) { | 112 profile_(profile) { |
| 124 DCHECK(profile); | 113 DCHECK(profile); |
| 125 set_auto_acknowledge(true); | 114 set_auto_acknowledge(true); |
| 126 } | 115 } |
| 127 | 116 |
| 128 void Provider::VisitRegisteredExtension() { | 117 void Provider::VisitRegisteredExtension() { |
| 129 if (!profile_ || !ShouldInstallInProfile(profile_)) { | 118 if (!profile_ || !ShouldInstallInProfile(profile_)) { |
| 130 base::DictionaryValue* prefs = new base::DictionaryValue; | 119 base::DictionaryValue* prefs = new base::DictionaryValue; |
| 131 SetPrefs(prefs); | 120 SetPrefs(prefs); |
| 132 return; | 121 return; |
| 133 } | 122 } |
| 134 | 123 |
| 135 extensions::ExternalProviderImpl::VisitRegisteredExtension(); | 124 extensions::ExternalProviderImpl::VisitRegisteredExtension(); |
| 136 } | 125 } |
| 137 | 126 |
| 127 bool isLocaleSupported() { |
| 128 // Don't bother installing default apps in locales where it is known that |
| 129 // they don't work. |
| 130 // TODO(rogerta): Do this check dynamically once the webstore can expose |
| 131 // an API. See http://crbug.com/101357 |
| 132 const std::string& locale = g_browser_process->GetApplicationLocale(); |
| 133 static const char* unsupported_locales[] = {"CN", "TR", "IR"}; |
| 134 for (size_t i = 0; i < arraysize(unsupported_locales); ++i) { |
| 135 if (EndsWith(locale, unsupported_locales[i], false)) { |
| 136 return false; |
| 137 } |
| 138 } |
| 139 return true; |
| 140 } |
| 141 |
| 138 } // namespace default_apps | 142 } // namespace default_apps |
| OLD | NEW |