blob: fbd85fed6d72795d4ada06950858cc0cd0eb3f03 [file] [log] [blame]
[email protected]86c6b9e32011-10-25 17:09:101// Copyright (c) 2011 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.
4
5#include "chrome/browser/extensions/default_apps.h"
6
7#include "base/command_line.h"
8#include "base/metrics/field_trial.h"
9#include "chrome/browser/browser_process.h"
10#include "chrome/browser/extensions/default_apps_trial.h"
11#include "chrome/browser/extensions/extension_service.h"
12#include "chrome/browser/profiles/profile.h"
13#include "chrome/common/chrome_switches.h"
14#include "chrome/common/pref_names.h"
15#include "ui/base/l10n/l10n_util.h"
16
[email protected]d190cef2011-11-09 02:09:2417static bool ShouldInstallInProfile(Profile* profile) {
[email protected]86c6b9e32011-10-25 17:09:1018 // We decide to install or not install default apps based on the following
19 // criteria, from highest priority to lowest priority:
20 //
21 // - If this instance of chrome is participating in the default apps
22 // field trial, then install apps based on the group.
23 // - The command line option. Tests use this option to disable installation
24 // of default apps in some cases.
25 // - If the locale is not compatible with the defaults, don't install them.
26 // - If the profile says to either always install or never install default
27 // apps, obey.
28 // - The kDefaultApps preferences value in the profile. This value is
29 // usually set in the master_preferences file.
30 bool install_apps =
31 profile->GetPrefs()->GetString(prefs::kDefaultApps) == "install";
32
[email protected]d190cef2011-11-09 02:09:2433 default_apps::InstallState state =
34 static_cast<default_apps::InstallState>(profile->GetPrefs()->GetInteger(
35 prefs::kDefaultAppsInstallState));
[email protected]86c6b9e32011-10-25 17:09:1036 switch (state) {
[email protected]d190cef2011-11-09 02:09:2437 case default_apps::kUnknown: {
[email protected]86c6b9e32011-10-25 17:09:1038 // We get here for either new profile, or profiles created before the
39 // default apps feature was implemented. In the former case, we always
40 // want to install default apps. In the latter case, we don't want to
41 // disturb a user that has already installed and possibly curated a list
42 // of favourite apps, so we only install if there are no apps in the
43 // profile. We can check for both these cases by looking to see if
44 // any apps already exist.
45 ExtensionService* extension_service = profile->GetExtensionService();
46 if (extension_service && extension_service->HasApps())
47 install_apps = false;
48 break;
49 }
[email protected]d190cef2011-11-09 02:09:2450 case default_apps::kAlwaysProvideDefaultApps:
[email protected]86c6b9e32011-10-25 17:09:1051 install_apps = true;
52 break;
[email protected]d190cef2011-11-09 02:09:2453 case default_apps::kNeverProvideDefaultApps:
[email protected]86c6b9e32011-10-25 17:09:1054 install_apps = false;
55 break;
56 default:
57 NOTREACHED();
58 }
59
60 if (install_apps) {
61 // Don't bother installing default apps in locales where it is known that
62 // they don't work.
63 // TODO(rogerta): Do this check dynamically once the webstore can expose
64 // an API. See http://crbug.com/101357
65 const std::string& locale = g_browser_process->GetApplicationLocale();
66 static const char* unsupported_locales[] = {"CN", "TR", "IR"};
67 for (size_t i = 0; i < arraysize(unsupported_locales); ++i) {
68 if (EndsWith(locale, unsupported_locales[i], false)) {
69 install_apps = false;
70 break;
71 }
72 }
73 }
74
75 if (CommandLine::ForCurrentProcess()->HasSwitch(
76 switches::kDisableDefaultApps)) {
77 install_apps = false;
78 }
79
80 if (base::FieldTrialList::TrialExists(kDefaultAppsTrial_Name)) {
81 install_apps = base::FieldTrialList::Find(
82 kDefaultAppsTrial_Name)->group_name() != kDefaultAppsTrial_NoAppsGroup;
83 }
84
85 // Save the state if needed.
[email protected]d190cef2011-11-09 02:09:2486 if (state == default_apps::kUnknown) {
[email protected]86c6b9e32011-10-25 17:09:1087 if (install_apps) {
88 profile->GetPrefs()->SetInteger(prefs::kDefaultAppsInstallState,
[email protected]d190cef2011-11-09 02:09:2489 default_apps::kAlwaysProvideDefaultApps);
[email protected]86c6b9e32011-10-25 17:09:1090 } else {
91 profile->GetPrefs()->SetInteger(prefs::kDefaultAppsInstallState,
[email protected]d190cef2011-11-09 02:09:2492 default_apps::kNeverProvideDefaultApps);
[email protected]86c6b9e32011-10-25 17:09:1093 }
94 profile->GetPrefs()->ScheduleSavePersistentPrefs();
95 }
96
97 return install_apps;
98}
99
[email protected]d190cef2011-11-09 02:09:24100namespace default_apps {
101
102void RegisterUserPrefs(PrefService* prefs) {
103 prefs->RegisterIntegerPref(prefs::kDefaultAppsInstallState, kUnknown,
104 PrefService::UNSYNCABLE_PREF);
105}
106
107Provider::Provider(Profile* profile,
108 VisitorInterface* service,
109 ExternalExtensionLoader* loader,
110 Extension::Location crx_location,
111 Extension::Location download_location,
112 int creation_flags)
113 : ExternalExtensionProviderImpl(service, loader, crx_location,
114 download_location, creation_flags),
115 profile_(profile) {
116 DCHECK(profile);
117}
118
119void Provider::VisitRegisteredExtension() {
120 if (!profile_ || !ShouldInstallInProfile(profile_)) {
121 base::DictionaryValue* prefs = new base::DictionaryValue;
122 SetPrefs(prefs);
123 return;
124 }
125
126 ExternalExtensionProviderImpl::VisitRegisteredExtension();
127}
128
[email protected]86c6b9e32011-10-25 17:09:10129} // namespace default_apps