File: app_info_generator.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (141 lines) | stat: -rw-r--r-- 5,003 bytes parent folder | download | duplicates (6)
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
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_ASH_POLICY_STATUS_COLLECTOR_APP_INFO_GENERATOR_H_
#define CHROME_BROWSER_ASH_POLICY_STATUS_COLLECTOR_APP_INFO_GENERATOR_H_

#include <map>
#include <memory>
#include <optional>
#include <string>
#include <vector>

#include "base/memory/raw_ref.h"
#include "base/time/default_clock.h"
#include "base/time/time.h"
#include "base/unguessable_token.h"
#include "chrome/browser/apps/app_service/app_service_proxy_forward.h"
#include "chrome/browser/ash/policy/status_collector/activity_storage.h"
#include "chrome/browser/ash/policy/status_collector/managed_session_service.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/services/app_service/public/cpp/instance_registry.h"

class Profile;

namespace apps {
class AppUpdate;
}

namespace enterprise_management {
class AppInfo;
}  // namespace enterprise_management

namespace policy {

// A class that is responsible for collecting application inventory and usage
// information.
class AppInfoGenerator : public apps::InstanceRegistry::Observer,
                         public ManagedSessionService::Observer {
 public:
  using Result = std::optional<std::vector<enterprise_management::AppInfo>>;

  explicit AppInfoGenerator(
      ManagedSessionService* managed_session_service,
      base::TimeDelta max_stored_past_activity_interval,
      base::Clock* clock = base::DefaultClock::GetInstance());
  AppInfoGenerator(const AppInfoGenerator&) = delete;
  AppInfoGenerator& operator=(const AppInfoGenerator&) = delete;
  ~AppInfoGenerator() override;

  // If reporting is enabled and there is an active affiliated user session,
  // generates an app info report with usage per app for up to the last
  // |max_stored_past_activity_interval| days for the current user, not
  // including the current day, otherwise returns a null optional.
  const Result Generate() const;

  // When not reporting usage is no longer recorded and app information is not
  // reported when generating the report.
  void OnReportingChanged(bool should_report);

  // Occurs when usage has been reported successfully. This removes reported
  // usage before |report_time| so it will not be reported again.
  void OnReportedSuccessfully(base::Time report_time);

  // Occurs when usage will be reported, which records active usage in the pref
  // up until the current time, so it may be reported.
  void OnWillReport();

  // ManagedSessionService::Observer
  void OnLogin(Profile* profile) override;
  void OnLogout(Profile* profile) override;
  void OnLocked() override;
  void OnUnlocked() override;
  void OnResumeActive(base::Time suspend_time) override;

  // InstanceRegistry::Observer
  void OnInstanceUpdate(const apps::InstanceUpdate& update) override;
  void OnInstanceRegistryWillBeDestroyed(
      apps::InstanceRegistry* cache) override;

  static void RegisterProfilePrefs(PrefRegistrySimple* registry);

 private:
  struct AppInstances {
    explicit AppInstances(const base::Time start_time_);
    AppInstances(const AppInstances&) = delete;
    AppInstances& operator=(const AppInstances&) = delete;
    ~AppInstances();

    const base::Time start_time;
    std::unordered_set<base::UnguessableToken, base::UnguessableTokenHash>
        running_instances;
  };
  struct AppInfoProvider {
    explicit AppInfoProvider(Profile* profile);
    AppInfoProvider(const AppInfoProvider&) = delete;
    AppInfoProvider& operator=(const AppInfoProvider&) = delete;
    ~AppInfoProvider();

    ActivityStorage activity_storage;
    const raw_ref<apps::AppServiceProxy> app_service_proxy;
  };

  const enterprise_management::AppInfo ConvertToAppInfo(
      const apps::AppUpdate& update,
      const std::vector<enterprise_management::TimePeriod>& app_activity) const;

  void SetOpenDurationsToClosed(base::Time end_time);

  void SetIdleDurationsToOpen();

  void OpenUsageInterval(const std::string& app_id,
                         const base::UnguessableToken& instance_key,
                         const base::Time start_time);

  void CloseUsageInterval(const std::string& app_id,
                          const base::UnguessableToken& instance_key,
                          const base::Time end_time);

  std::unique_ptr<AppInfoProvider> provider_;

  bool should_report_ = false;

  bool device_locked_ = false;

  std::map<std::string, std::unique_ptr<AppInstances>> app_instances_by_id_;

  // The timeout in the past to store activity.
  // This is kept in case status uploads fail for a number of days.
  base::TimeDelta max_stored_past_activity_interval_;

  const raw_ref<const base::Clock> clock_;

  base::ScopedObservation<ManagedSessionService,
                          ManagedSessionService::Observer>
      managed_session_observation_{this};
};

}  // namespace policy

#endif  // CHROME_BROWSER_ASH_POLICY_STATUS_COLLECTOR_APP_INFO_GENERATOR_H_