File: install_event_logger_base.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 (116 lines) | stat: -rw-r--r-- 4,222 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
// 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_REPORTING_INSTALL_EVENT_LOGGER_BASE_H_
#define CHROME_BROWSER_ASH_POLICY_REPORTING_INSTALL_EVENT_LOGGER_BASE_H_

#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/system/sys_info.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "chrome/browser/profiles/profile.h"
#include "chromeos/ash/components/disks/disk.h"
#include "chromeos/ash/components/disks/disk_mount_manager.h"
#include "chromeos/ash/experiences/arc/arc_prefs.h"
#include "chromeos/dbus/power/power_manager_client.h"
#include "components/prefs/pref_service.h"
#include "services/network/public/cpp/network_connection_tracker.h"

namespace policy {

// Contains shared functionality to manage logs of install events
// (of type Event) for apps keyed by AppId.
// The term |app| in this class refers to ARC++ apps as well as extensions.
template <class Event, class EventType, class AppId>
class InstallEventLoggerBase {
 public:
  explicit InstallEventLoggerBase(Profile* profile) : profile_(profile) {
    stateful_path_ = ash::disks::GetStatefulPartitionPath();
  }

  std::unique_ptr<Event> CreateEvent(EventType type) {
    std::unique_ptr<Event> event = std::make_unique<Event>();
    EnsureTimestampSet(event.get());
    event->set_event_type(type);
    return event;
  }

  void EnsureTimestampSet(Event* event) {
    if (!event->has_timestamp()) {
      event->set_timestamp(
          (base::Time::Now() - base::Time::UnixEpoch()).InMicroseconds());
    }
  }

  // Return all elements that are members of |first| but not |second|.
  std::set<AppId> GetDifference(const std::set<AppId>& first,
                                const std::set<AppId>& second) {
    std::set<AppId> difference;
    std::set_difference(first.begin(), first.end(), second.begin(),
                        second.end(),
                        std::inserter(difference, difference.end()));
    return difference;
  }

  void AddForSetOfAppsWithDiskSpaceInfo(const std::set<AppId>& ids,
                                        std::unique_ptr<Event> event) {
    base::ThreadPool::PostTaskAndReplyWithResult(
        FROM_HERE, {base::MayBlock()},
        base::BindOnce(&AddDiskSpaceInfoToEvent, std::move(event),
                       stateful_path_),
        base::BindOnce(&InstallEventLoggerBase::AddForSetOfApps,
                       weak_factory_.GetWeakPtr(), ids));
  }

  static std::unique_ptr<Event> AddDiskSpaceInfoToEvent(
      std::unique_ptr<Event> event,
      const base::FilePath& stateful_path) {
    const int64_t stateful_total =
        base::SysInfo::AmountOfTotalDiskSpace(stateful_path);
    if (stateful_total >= 0) {
      event->set_stateful_total(stateful_total);
    }
    const int64_t stateful_free =
        base::SysInfo::AmountOfFreeDiskSpace(stateful_path);
    if (stateful_free >= 0) {
      event->set_stateful_free(stateful_free);
    }
    return event;
  }

  // Set stateful partition path for unit tests.
  void SetStatefulPathForTesting(const base::FilePath& path) {
    stateful_path_ = path;
  }

 protected:
  // The profile whose install requests to log.
  // This applies both to ARC++ apps and extensions.
  const raw_ptr<Profile> profile_;

  // Adds |event| to the log for all apps in |ids|.
  virtual void AddForSetOfApps(const std::set<AppId>& ids,
                               std::unique_ptr<Event> event) = 0;

  void AddEvent(const AppId& appId,
                bool gather_disk_space_info,
                std::unique_ptr<Event>& event) {
    EnsureTimestampSet(event.get());
    if (gather_disk_space_info) {
      AddForSetOfAppsWithDiskSpaceInfo({appId}, std::move(event));
    } else {
      AddForSetOfApps({appId}, std::move(event));
    }
  }

 private:
  // Path for stateful partition.
  base::FilePath stateful_path_;

  // Weak factory used to reference |this| from background tasks.
  base::WeakPtrFactory<InstallEventLoggerBase> weak_factory_{this};
};
}  // namespace policy
#endif  // CHROME_BROWSER_ASH_POLICY_REPORTING_INSTALL_EVENT_LOGGER_BASE_H_