This source file includes following definitions.
- IsOldDefaultApp
- IsLocaleSupported
- RegisterProfilePrefs
- ShouldInstallInProfile
- is_migration_
- VisitRegisteredExtension
- SetPrefs
#include "chrome/browser/extensions/default_apps.h"
#include <set>
#include <string>
#include "base/command_line.h"
#include "base/prefs/pref_service.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/chrome_version_info.h"
#include "chrome/common/extensions/extension_constants.h"
#include "chrome/common/pref_names.h"
#include "components/user_prefs/pref_registry_syncable.h"
#include "extensions/common/extension.h"
#include "ui/base/l10n/l10n_util.h"
#if !defined(OS_ANDROID)
#include "chrome/browser/first_run/first_run.h"
#endif
namespace {
bool IsOldDefaultApp(const std::string& extension_id) {
return extension_id == extension_misc::kGmailAppId ||
extension_id == extension_misc::kGoogleSearchAppId ||
extension_id == extension_misc::kYoutubeAppId;
}
bool IsLocaleSupported() {
const std::string& locale = g_browser_process->GetApplicationLocale();
static const char* unsupported_locales[] = {"CN", "TR", "IR"};
for (size_t i = 0; i < arraysize(unsupported_locales); ++i) {
if (EndsWith(locale, unsupported_locales[i], false)) {
return false;
}
}
return true;
}
}
namespace default_apps {
void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterIntegerPref(
prefs::kDefaultAppsInstallState,
kUnknown,
user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
}
bool Provider::ShouldInstallInProfile() {
bool install_apps =
profile_->GetPrefs()->GetString(prefs::kDefaultApps) == "install";
InstallState state =
static_cast<InstallState>(profile_->GetPrefs()->GetInteger(
prefs::kDefaultAppsInstallState));
is_migration_ = (state == kProvideLegacyDefaultApps);
switch (state) {
case kUnknown: {
chrome::VersionInfo version_info;
bool is_new_profile =
profile_->WasCreatedByVersionOrLater(version_info.Version().c_str());
#if defined(OS_ANDROID)
bool is_first_run = false;
#else
bool is_first_run = first_run::IsChromeFirstRun();
#endif
if (!is_first_run && !is_new_profile)
install_apps = false;
break;
}
case kProvideLegacyDefaultApps:
profile_->GetPrefs()->SetInteger(
prefs::kDefaultAppsInstallState,
kAlreadyInstalledDefaultApps);
break;
case kAlreadyInstalledDefaultApps:
case kNeverInstallDefaultApps:
install_apps = false;
break;
default:
NOTREACHED();
}
if (install_apps && !IsLocaleSupported())
install_apps = false;
if (state == kUnknown) {
if (install_apps) {
profile_->GetPrefs()->SetInteger(prefs::kDefaultAppsInstallState,
kAlreadyInstalledDefaultApps);
} else {
profile_->GetPrefs()->SetInteger(prefs::kDefaultAppsInstallState,
kNeverInstallDefaultApps);
}
}
return install_apps;
}
Provider::Provider(Profile* profile,
VisitorInterface* service,
extensions::ExternalLoader* loader,
extensions::Manifest::Location crx_location,
extensions::Manifest::Location download_location,
int creation_flags)
: extensions::ExternalProviderImpl(service, loader, profile, crx_location,
download_location, creation_flags),
profile_(profile),
is_migration_(false) {
DCHECK(profile);
set_auto_acknowledge(true);
}
void Provider::VisitRegisteredExtension() {
if (!profile_ || !ShouldInstallInProfile()) {
base::DictionaryValue* prefs = new base::DictionaryValue;
SetPrefs(prefs);
return;
}
extensions::ExternalProviderImpl::VisitRegisteredExtension();
}
void Provider::SetPrefs(base::DictionaryValue* prefs) {
if (is_migration_) {
std::set<std::string> new_default_apps;
for (base::DictionaryValue::Iterator i(*prefs); !i.IsAtEnd(); i.Advance()) {
if (!IsOldDefaultApp(i.key()))
new_default_apps.insert(i.key());
}
for (std::set<std::string>::iterator it = new_default_apps.begin();
it != new_default_apps.end(); ++it) {
prefs->Remove(*it, NULL);
}
}
ExternalProviderImpl::SetPrefs(prefs);
}
}