1 // Copyright 2016 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/browsing_data/counters/hosted_apps_counter.h"
6 
7 #include <algorithm>
8 #include <string>
9 
10 #include "chrome/browser/extensions/extension_special_storage_policy.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "components/browsing_data/core/pref_names.h"
13 #include "extensions/browser/extension_registry.h"
14 #include "extensions/common/constants.h"
15 #include "extensions/common/extension.h"
16 
HostedAppsCounter(Profile * profile)17 HostedAppsCounter::HostedAppsCounter(Profile* profile)
18     : profile_(profile) {}
19 
20 HostedAppsCounter::~HostedAppsCounter() = default;
21 
GetPrefName() const22 const char* HostedAppsCounter::GetPrefName() const {
23   return browsing_data::prefs::kDeleteHostedAppsData;
24 }
25 
Count()26 void HostedAppsCounter::Count() {
27   std::vector<std::string> names;
28 
29   std::unique_ptr<extensions::ExtensionSet> extensions =
30       extensions::ExtensionRegistry::Get(profile_)
31           ->GenerateInstalledExtensionsSet();
32   auto* special_storage_policy = profile_->GetExtensionSpecialStoragePolicy();
33 
34   for (const auto& extension : *extensions) {
35     // Exclude kChromeAppId because this is not a proper hosted app. It is just
36     // a shortcut to launch Chrome on Chrome OS.
37     if (special_storage_policy->NeedsProtection(extension.get()) &&
38         extension->id() != extension_misc::kChromeAppId) {
39       names.push_back(extension->short_name());
40     }
41   }
42 
43   int count = names.size();
44 
45   // Give the first two names (alphabetically) as examples.
46   std::sort(names.begin(), names.end());
47   names.resize(std::min<size_t>(2u, names.size()));
48 
49   ReportResult(std::make_unique<HostedAppsResult>(this, count, names));
50 }
51 
52 // HostedAppsCounter::HostedAppsResult -----------------------------------------
53 
HostedAppsResult(const HostedAppsCounter * source,ResultInt num_apps,const std::vector<std::string> & examples)54 HostedAppsCounter::HostedAppsResult::HostedAppsResult(
55     const HostedAppsCounter* source,
56     ResultInt num_apps,
57     const std::vector<std::string>& examples)
58     : FinishedResult(source, num_apps), examples_(examples) {}
59 
~HostedAppsResult()60 HostedAppsCounter::HostedAppsResult::~HostedAppsResult() {}
61