Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / extensions / default_apps.cc
blob2cc32289435e48d1243285e0de259e8ff594ec24
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
3 // found in the LICENSE file.
5 #include "chrome/browser/extensions/default_apps.h"
7 #include <set>
8 #include <string>
10 #include "base/command_line.h"
11 #include "base/prefs/pref_service.h"
12 #include "base/strings/string_util.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/first_run/first_run.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/common/chrome_switches.h"
17 #include "chrome/common/chrome_version_info.h"
18 #include "chrome/common/extensions/extension_constants.h"
19 #include "chrome/common/pref_names.h"
20 #include "components/pref_registry/pref_registry_syncable.h"
21 #include "extensions/common/extension.h"
23 namespace {
25 // Returns true if the app was a default app in Chrome 22
26 bool IsOldDefaultApp(const std::string& extension_id) {
27 return extension_id == extension_misc::kGmailAppId ||
28 extension_id == extension_misc::kGoogleSearchAppId ||
29 extension_id == extension_misc::kYoutubeAppId;
32 bool IsLocaleSupported() {
33 // Don't bother installing default apps in locales where it is known that
34 // they don't work.
35 // TODO(rogerta): Do this check dynamically once the webstore can expose
36 // an API. See http://crbug.com/101357
37 const std::string& locale = g_browser_process->GetApplicationLocale();
38 static const char* const unsupported_locales[] = {"CN", "TR", "IR"};
39 for (size_t i = 0; i < arraysize(unsupported_locales); ++i) {
40 if (base::EndsWith(locale, unsupported_locales[i],
41 base::CompareCase::INSENSITIVE_ASCII)) {
42 return false;
45 return true;
48 } // namespace
50 namespace default_apps {
52 void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
53 registry->RegisterIntegerPref(prefs::kDefaultAppsInstallState, kUnknown);
56 bool Provider::ShouldInstallInProfile() {
57 // We decide to install or not install default apps based on the following
58 // criteria, from highest priority to lowest priority:
60 // - The command line option. Tests use this option to disable installation
61 // of default apps in some cases.
62 // - If the locale is not compatible with the defaults, don't install them.
63 // - The kDefaultApps preferences value in the profile. This value is
64 // usually set in the master_preferences file.
65 bool install_apps =
66 profile_->GetPrefs()->GetString(prefs::kDefaultApps) == "install";
68 InstallState state =
69 static_cast<InstallState>(profile_->GetPrefs()->GetInteger(
70 prefs::kDefaultAppsInstallState));
72 is_migration_ = (state == kProvideLegacyDefaultApps);
74 switch (state) {
75 case kUnknown: {
76 // Only new installations and profiles get default apps. In theory the
77 // new profile checks should catch new installations, but that is not
78 // always the case (http:/crbug.com/145351).
79 chrome::VersionInfo version_info;
80 bool is_new_profile =
81 profile_->WasCreatedByVersionOrLater(version_info.Version().c_str());
82 bool is_first_run = first_run::IsChromeFirstRun();
83 if (!is_first_run && !is_new_profile)
84 install_apps = false;
85 break;
88 // The old default apps were provided as external extensions and were
89 // installed everytime Chrome was run. Thus, changing the list of default
90 // apps affected all users. Migrate old default apps to new mechanism where
91 // they are installed only once as INTERNAL.
92 // TODO(grv) : remove after Q1-2013.
93 case kProvideLegacyDefaultApps:
94 profile_->GetPrefs()->SetInteger(
95 prefs::kDefaultAppsInstallState,
96 kAlreadyInstalledDefaultApps);
97 break;
99 case kAlreadyInstalledDefaultApps:
100 case kNeverInstallDefaultApps:
101 install_apps = false;
102 break;
103 default:
104 NOTREACHED();
107 if (install_apps && !IsLocaleSupported())
108 install_apps = false;
110 // Default apps are only installed on profile creation or a new chrome
111 // download.
112 if (state == kUnknown) {
113 if (install_apps) {
114 profile_->GetPrefs()->SetInteger(prefs::kDefaultAppsInstallState,
115 kAlreadyInstalledDefaultApps);
116 } else {
117 profile_->GetPrefs()->SetInteger(prefs::kDefaultAppsInstallState,
118 kNeverInstallDefaultApps);
122 return install_apps;
125 Provider::Provider(Profile* profile,
126 VisitorInterface* service,
127 extensions::ExternalLoader* loader,
128 extensions::Manifest::Location crx_location,
129 extensions::Manifest::Location download_location,
130 int creation_flags)
131 : extensions::ExternalProviderImpl(service, loader, profile, crx_location,
132 download_location, creation_flags),
133 profile_(profile),
134 is_migration_(false) {
135 DCHECK(profile);
136 set_auto_acknowledge(true);
139 void Provider::VisitRegisteredExtension() {
140 if (!profile_ || !ShouldInstallInProfile()) {
141 base::DictionaryValue* prefs = new base::DictionaryValue;
142 SetPrefs(prefs);
143 return;
146 extensions::ExternalProviderImpl::VisitRegisteredExtension();
149 void Provider::SetPrefs(base::DictionaryValue* prefs) {
150 if (is_migration_) {
151 std::set<std::string> new_default_apps;
152 for (base::DictionaryValue::Iterator i(*prefs); !i.IsAtEnd(); i.Advance()) {
153 if (!IsOldDefaultApp(i.key()))
154 new_default_apps.insert(i.key());
156 // Filter out the new default apps for migrating users.
157 for (std::set<std::string>::iterator it = new_default_apps.begin();
158 it != new_default_apps.end(); ++it) {
159 prefs->Remove(*it, NULL);
163 ExternalProviderImpl::SetPrefs(prefs);
166 } // namespace default_apps