summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/default_apps.cc
blob: cee5c324126db7ea1f7154be64a419eed8f0d3e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/extensions/default_apps.h"

#include "base/command_line.h"
#include "base/metrics/histogram.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"

const int DefaultApps::kAppsPromoCounterMax = 10;

// static
void DefaultApps::RegisterUserPrefs(PrefService* prefs) {
  prefs->RegisterBooleanPref(prefs::kDefaultAppsInstalled, false);
  prefs->RegisterIntegerPref(prefs::kAppsPromoCounter, 0);
}

DefaultApps::DefaultApps(PrefService* prefs,
                         const std::string& application_locale)
    : prefs_(prefs), application_locale_(application_locale) {
  // Poppit, Entanglement
  ids_.insert("mcbkbpnkkkipelfledbfocopglifcfmi");
  ids_.insert("aciahcmjmecflokailenpkdchphgkefd");
}

DefaultApps::~DefaultApps() {}

const ExtensionIdSet& DefaultApps::default_apps() const {
  return ids_;
}

bool DefaultApps::DefaultAppSupported() {
  // On Chrome OS the default apps are installed via a different mechanism.
#if defined(OS_CHROMEOS)
  return false;
#else
  return DefaultAppsSupportedForLanguage();
#endif
}

bool DefaultApps::DefaultAppsSupportedForLanguage() {
  return application_locale_ == "en-US";
}

bool DefaultApps::ShouldInstallDefaultApps(
    const ExtensionIdSet& installed_ids) {
  if (!DefaultAppSupported())
    return false;

  if (GetDefaultAppsInstalled())
    return false;

  // If there are any non-default apps installed, we should never try to install
  // the default apps again, even if the non-default apps are later removed.
  if (NonDefaultAppIsInstalled(installed_ids)) {
    SetDefaultAppsInstalled(true);
    return false;
  }

  return true;
}

bool DefaultApps::ShouldShowAppLauncher(const ExtensionIdSet& installed_ids) {
  // On Chrome OS the default apps are installed via a separate mechanism that
  // is always enabled. Therefore we always show the launcher.
#if defined(OS_CHROME)
  return true;
#else
  // The app store only supports en-us at the moment, so we don't show the apps
  // section by default for users in other locales. But we do show it if a user
  // from a non-supported locale somehow installs an app (eg by navigating
  // directly to the store).
  if (!DefaultAppsSupportedForLanguage())
    return !installed_ids.empty();

  // For supported locales, we need to wait for all the default apps to be
  // installed before showing the apps section. We also show it if any
  // non-default app is installed (eg because the user installed the app in a
  // previous version of Chrome).
  if (GetDefaultAppsInstalled() || NonDefaultAppIsInstalled(installed_ids))
    return true;
  else
    return false;
#endif
}

bool DefaultApps::ShouldShowPromo(const ExtensionIdSet& installed_ids,
                                  bool* just_expired) {
  *just_expired = false;

  if (CommandLine::ForCurrentProcess()->HasSwitch(
          switches::kForceAppsPromoVisible)) {
    return true;
  }

  if (!DefaultAppSupported())
    return false;

  if (!GetDefaultAppsInstalled())
    return false;

  int promo_counter = GetPromoCounter();
  if (promo_counter <= kAppsPromoCounterMax) {
    // If we have the exact set of default apps, show the promo. If we don't
    // have the exact set of default apps, this means that the user manually
    // installed or uninstalled one. The promo doesn't make sense if it shows
    // apps the user manually installed, so expire it immediately in that
    // situation.
    if (ids_ != installed_ids) {
      SetPromoHidden();
      return false;
    }

    if (promo_counter == kAppsPromoCounterMax) {
      *just_expired = true;
      UMA_HISTOGRAM_ENUMERATION(extension_misc::kAppsPromoHistogram,
                                extension_misc::PROMO_EXPIRE,
                                extension_misc::PROMO_BUCKET_BOUNDARY);
      SetPromoCounter(++promo_counter);
      return false;
    } else {
      UMA_HISTOGRAM_ENUMERATION(extension_misc::kAppsPromoHistogram,
                                extension_misc::PROMO_SEEN,
                                extension_misc::PROMO_BUCKET_BOUNDARY);
      SetPromoCounter(++promo_counter);
      return true;
    }
  }

  return false;
}

void DefaultApps::DidInstallApp(const ExtensionIdSet& installed_ids) {
  // If all the default apps have been installed, stop trying to install them.
  // Note that we use std::includes here instead of == because apps might have
  // been manually installed while the the default apps were installing and we
  // wouldn't want to keep trying to install them in that case.
  if (!GetDefaultAppsInstalled() &&
      std::includes(installed_ids.begin(), installed_ids.end(),
                    ids_.begin(), ids_.end())) {
    SetDefaultAppsInstalled(true);
  }
}

bool DefaultApps::NonDefaultAppIsInstalled(
    const ExtensionIdSet& installed_ids) const {
  for (ExtensionIdSet::const_iterator iter = installed_ids.begin();
       iter != installed_ids.end(); ++iter) {
    if (ids_.find(*iter) == ids_.end())
      return true;
  }

  return false;
}

void DefaultApps::SetPromoHidden() {
  SetPromoCounter(kAppsPromoCounterMax + 1);
}

int DefaultApps::GetPromoCounter() const {
  return prefs_->GetInteger(prefs::kAppsPromoCounter);
}

void DefaultApps::SetPromoCounter(int val) {
  prefs_->SetInteger(prefs::kAppsPromoCounter, val);
  prefs_->ScheduleSavePersistentPrefs();
}

bool DefaultApps::GetDefaultAppsInstalled() const {
  return prefs_->GetBoolean(prefs::kDefaultAppsInstalled);
}

void DefaultApps::SetDefaultAppsInstalled(bool val) {
  prefs_->SetBoolean(prefs::kDefaultAppsInstalled, val);
  prefs_->ScheduleSavePersistentPrefs();
}