File: arc_app_install_event_logger.cc

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 (282 lines) | stat: -rw-r--r-- 9,835 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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/policy/reporting/arc_app_install_event_logger.h"

#include <stdint.h>

#include <algorithm>
#include <iterator>

#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "base/time/time.h"
#include "base/values.h"
#include "chrome/browser/ash/arc/arc_util.h"
#include "chrome/browser/ash/arc/policy/arc_policy_util.h"
#include "chrome/browser/ash/policy/reporting/install_event_logger_base.h"
#include "chrome/browser/policy/profile_policy_connector.h"
#include "chrome/browser/profiles/profile.h"
#include "chromeos/ash/experiences/arc/arc_prefs.h"
#include "components/policy/core/common/policy_map.h"
#include "components/policy/core/common/policy_namespace.h"
#include "components/policy/policy_constants.h"
#include "components/policy/proto/device_management_backend.pb.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/pref_service.h"

namespace policy {

namespace {

namespace em = ::enterprise_management;

constexpr int kNonComplianceReasonAppNotInstalled = 5;

std::set<std::string> GetRequestedPackagesFromPolicy(const PolicyMap& policy) {
  const base::Value* const arc_enabled =
      policy.GetValue(key::kArcEnabled, base::Value::Type::BOOLEAN);
  if (!arc_enabled || !arc_enabled->GetBool())
    return {};

  const base::Value* const arc_policy =
      policy.GetValue(key::kArcPolicy, base::Value::Type::STRING);
  if (!arc_policy)
    return {};

  return arc::policy_util::GetRequestedPackagesFromArcPolicy(
      arc_policy->GetString());
}

}  // namespace

ArcAppInstallEventLogger::ArcAppInstallEventLogger(Delegate* delegate,
                                                   Profile* profile)
    : InstallEventLoggerBase(profile), delegate_(delegate) {
  if (!arc::IsArcAllowedForProfile(profile_)) {
    AddForSetOfApps(GetPackagesFromPref(arc::prefs::kArcPushInstallAppsPending),
                    CreateEvent(em::AppInstallReportLogEvent::CANCELED));
    Clear(profile_);
    return;
  }

  PolicyService* const policy_service =
      profile_->GetProfilePolicyConnector()->policy_service();
  EvaluatePolicy(policy_service->GetPolicies(
                     PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())),
                 true /* initial */);

  observing_ = true;
  arc::ArcPolicyBridge* bridge =
      arc::ArcPolicyBridge::GetForBrowserContext(profile_);
  bridge->AddObserver(this);
  policy_service->AddObserver(POLICY_DOMAIN_CHROME, this);
}

ArcAppInstallEventLogger::~ArcAppInstallEventLogger() {
  if (log_collector_) {
    log_collector_->OnLogout();
  }
  if (observing_) {
    arc::ArcPolicyBridge::GetForBrowserContext(profile_)->RemoveObserver(this);
    profile_->GetProfilePolicyConnector()->policy_service()->RemoveObserver(
        POLICY_DOMAIN_CHROME, this);
  }
}

// static
void ArcAppInstallEventLogger::RegisterProfilePrefs(
    user_prefs::PrefRegistrySyncable* registry) {
  registry->RegisterListPref(arc::prefs::kArcPushInstallAppsRequested);
  registry->RegisterListPref(arc::prefs::kArcPushInstallAppsPending);
}

// static
void ArcAppInstallEventLogger::Clear(Profile* profile) {
  profile->GetPrefs()->ClearPref(arc::prefs::kArcPushInstallAppsRequested);
  profile->GetPrefs()->ClearPref(arc::prefs::kArcPushInstallAppsPending);
}

void ArcAppInstallEventLogger::AddForAllPackages(
    std::unique_ptr<em::AppInstallReportLogEvent> event) {
  EnsureTimestampSet(event.get());
  AddForSetOfApps(GetPackagesFromPref(arc::prefs::kArcPushInstallAppsPending),
                  std::move(event));
}

void ArcAppInstallEventLogger::Add(
    const std::string& package,
    bool gather_disk_space_info,
    std::unique_ptr<em::AppInstallReportLogEvent> event) {
  AddEvent(package, gather_disk_space_info, event);
}

void ArcAppInstallEventLogger::UpdatePolicySuccessRate(
    const std::string& package,
    bool success) {
  policy_data_helper_.UpdatePolicySuccessRate(package, success);
}

void ArcAppInstallEventLogger::OnPolicyUpdated(const PolicyNamespace& ns,
                                               const PolicyMap& previous,
                                               const PolicyMap& current) {
  EvaluatePolicy(current, false /* initial */);
}

void ArcAppInstallEventLogger::OnPolicySent(const std::string& policy) {
  requested_in_arc_ =
      arc::policy_util::GetRequestedPackagesFromArcPolicy(policy);
}

void ArcAppInstallEventLogger::OnComplianceReportReceived(
    const base::Value* compliance_report) {
  const base::Value::List* const details =
      compliance_report->GetDict().FindList("nonComplianceDetails");
  if (!details) {
    return;
  }

  const std::set<std::string> all_force_install_apps_in_policy =
      GetPackagesFromPref(arc::prefs::kArcPushInstallAppsRequested);
  const std::set<std::string> previous_pending =
      GetPackagesFromPref(arc::prefs::kArcPushInstallAppsPending);

  std::set<std::string> noncompliant_apps_in_report;
  for (const auto& detail : *details) {
    const base::Value::Dict& details_dict = detail.GetDict();
    const std::optional<int> reason =
        details_dict.FindInt("nonComplianceReason");
    if (!reason || *reason != kNonComplianceReasonAppNotInstalled) {
      continue;
    }
    const std::string* const app_name = details_dict.FindString("packageName");
    if (!app_name || app_name->empty()) {
      continue;
    }
    noncompliant_apps_in_report.insert(*app_name);
  }
  const std::set<std::string> all_installed_apps = GetDifference(
      all_force_install_apps_in_policy, noncompliant_apps_in_report);

  std::set<std::string> newly_installed_apps;
  std::set_intersection(
      previous_pending.begin(), previous_pending.end(),
      all_installed_apps.begin(), all_installed_apps.end(),
      std::inserter(newly_installed_apps, newly_installed_apps.end()));

  AddForSetOfAppsWithDiskSpaceInfo(
      newly_installed_apps, CreateEvent(em::AppInstallReportLogEvent::SUCCESS));

  if (newly_installed_apps.empty()) {
    return;
  }

  SetPref(arc::prefs::kArcPushInstallAppsPending, noncompliant_apps_in_report);

  if (!noncompliant_apps_in_report.empty()) {
    UpdateCollector(noncompliant_apps_in_report);
  } else {
    StopCollector();
  }
}

std::set<std::string> ArcAppInstallEventLogger::GetPackagesFromPref(
    const std::string& pref_name) const {
  std::set<std::string> packages;
  for (const auto& package : profile_->GetPrefs()->GetList(pref_name)) {
    if (!package.is_string()) {
      continue;
    }
    packages.insert(package.GetString());
  }
  return packages;
}

void ArcAppInstallEventLogger::SetPref(const std::string& pref_name,
                                       const std::set<std::string>& packages) {
  base::Value::List value;
  for (const std::string& package : packages) {
    value.Append(package);
  }
  profile_->GetPrefs()->SetList(pref_name, std::move(value));
}

void ArcAppInstallEventLogger::UpdateCollector(
    const std::set<std::string>& pending) {
  if (!log_collector_) {
    log_collector_ = std::make_unique<ArcAppInstallEventLogCollector>(
        this, profile_, pending);
  } else {
    log_collector_->OnPendingPackagesChanged(pending);
  }
}

void ArcAppInstallEventLogger::StopCollector() {
  log_collector_.reset();
}

void ArcAppInstallEventLogger::EvaluatePolicy(const PolicyMap& policy,
                                              bool initial) {
  const std::set<std::string> previous_requested =
      GetPackagesFromPref(arc::prefs::kArcPushInstallAppsRequested);
  const std::set<std::string> previous_pending =
      GetPackagesFromPref(arc::prefs::kArcPushInstallAppsPending);

  const std::set<std::string> current_requested =
      GetRequestedPackagesFromPolicy(policy);

  const std::set<std::string> added =
      GetDifference(current_requested, previous_requested);
  const std::set<std::string> removed =
      GetDifference(previous_pending, current_requested);
  AddForSetOfAppsWithDiskSpaceInfo(
      added, CreateEvent(em::AppInstallReportLogEvent::SERVER_REQUEST));
  AddForSetOfApps(removed, CreateEvent(em::AppInstallReportLogEvent::CANCELED));
  // Consider canceled packages as successful since they are not needed
  policy_data_helper_.UpdatePolicySuccessRateForPackages(removed,
                                                         /* success */ true);

  const std::set<std::string> previously_installed =
      GetDifference(previous_requested, previous_pending);
  const std::set<std::string> current_pending =
      GetDifference(current_requested, previously_installed);
  SetPref(arc::prefs::kArcPushInstallAppsRequested, current_requested);
  SetPref(arc::prefs::kArcPushInstallAppsPending, current_pending);

  policy_data_helper_.AddPolicyData(current_pending,
                                    previously_installed.size());

  if (!current_pending.empty()) {
    UpdateCollector(current_pending);
    if (initial) {
      log_collector_->OnLogin();
    }
  } else {
    StopCollector();
  }
}

void ArcAppInstallEventLogger::AddForSetOfApps(
    const std::set<std::string>& packages,
    std::unique_ptr<em::AppInstallReportLogEvent> event) {
  delegate_->GetAndroidId(
      base::BindOnce(&ArcAppInstallEventLogger::OnGetAndroidId,
                     weak_factory_.GetWeakPtr(), packages, std::move(event)));
}

void ArcAppInstallEventLogger::OnGetAndroidId(
    const std::set<std::string>& packages,
    std::unique_ptr<em::AppInstallReportLogEvent> event,
    bool ok,
    int64_t android_id) {
  if (ok) {
    event->set_android_id(android_id);
  }
  delegate_->Add(packages, *event);
}

}  // namespace policy