File: policy_util.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 (114 lines) | stat: -rw-r--r-- 4,031 bytes parent folder | download | duplicates (5)
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
// Copyright 2022 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/apps/app_service/policy_util.h"

#include <functional>
#include <string_view>
#include <utility>

#include "base/containers/contains.h"
#include "base/containers/flat_map.h"
#include "base/strings/string_util.h"
#include "chrome/browser/apps/app_service/app_service_proxy.h"
#include "chrome/browser/apps/app_service/app_service_proxy_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "components/crx_file/id_util.h"
#include "components/services/app_service/public/cpp/app_update.h"
#include "components/services/app_service/public/cpp/types_util.h"
#include "components/web_package/signed_web_bundles/signed_web_bundle_id.h"

#if BUILDFLAG(IS_CHROMEOS)
#include "ash/webui/system_apps/public/system_web_app_type.h"
#include "base/containers/map_util.h"
#include "base/types/optional_util.h"
#include "chrome/browser/ash/file_manager/office_file_tasks.h"
#include "chrome/browser/ash/file_manager/virtual_tasks/id_constants.h"
#endif  // BUILDFLAG(IS_CHROMEOS)

namespace apps_util {

namespace {

#if BUILDFLAG(IS_CHROMEOS)

namespace fm_tasks = file_manager::file_tasks;

// These virtual task identifiers are supposed to be a subset of tasks listed in
// chrome/browser/ash/file_manager/virtual_file_tasks.cc
constexpr auto kVirtualFileTasksMapping =
    base::MakeFixedFlatMap<std::string_view, std::string_view>(
        {{"install-isolated-web-app", fm_tasks::kActionIdInstallIsolatedWebApp},
         {"microsoft-office", fm_tasks::kActionIdOpenInOffice},
         {"google-docs", fm_tasks::kActionIdWebDriveOfficeWord},
         {"google-spreadsheets", fm_tasks::kActionIdWebDriveOfficeExcel},
         {"google-slides", fm_tasks::kActionIdWebDriveOfficePowerPoint}});

#endif  // BUILDFLAG(IS_CHROMEOS)

}  // namespace

bool IsIsolatedWebAppPolicyId(std::string_view policy_id) {
  return web_package::SignedWebBundleId::Create(policy_id).has_value();
}

#if BUILDFLAG(IS_CHROMEOS)
bool IsFileManagerVirtualTaskPolicyId(std::string_view policy_id) {
  return GetVirtualTaskIdFromPolicyId(policy_id).has_value();
}

std::optional<std::string_view> GetVirtualTaskIdFromPolicyId(
    std::string_view policy_id) {
  if (!base::StartsWith(policy_id, kVirtualTaskPrefix)) {
    return std::nullopt;
  }
  static constexpr size_t kOffset =
      std::char_traits<char>::length(kVirtualTaskPrefix);
  return base::OptionalFromPtr(
      base::FindOrNull(kVirtualFileTasksMapping, policy_id.substr(kOffset)));
}
#endif  // BUILDFLAG(IS_CHROMEOS)

std::string TransformRawPolicyId(const std::string& raw_policy_id) {
  if (const GURL raw_policy_id_gurl{raw_policy_id};
      raw_policy_id_gurl.is_valid()) {
    return raw_policy_id_gurl.spec();
  }

  return raw_policy_id;
}

std::vector<std::string> GetAppIdsFromPolicyId(Profile* profile,
                                               const std::string& policy_id) {
  if (!apps::AppServiceProxyFactory::IsAppServiceAvailableForProfile(profile)) {
    return {};
  }
  std::vector<std::string> app_ids;
  apps::AppServiceProxyFactory::GetForProfile(profile)
      ->AppRegistryCache()
      .ForEachApp([&policy_id, &app_ids](const apps::AppUpdate& update) {
        if (IsInstalled(update.Readiness()) &&
            base::Contains(update.PolicyIds(), policy_id)) {
          app_ids.push_back(update.AppId());
        }
      });
  return app_ids;
}

std::optional<std::vector<std::string>> GetPolicyIdsFromAppId(
    Profile* profile,
    const std::string& app_id) {
  if (!apps::AppServiceProxyFactory::IsAppServiceAvailableForProfile(profile)) {
    return std::nullopt;
  }
  std::optional<std::vector<std::string>> policy_ids;
  apps::AppServiceProxyFactory::GetForProfile(profile)
      ->AppRegistryCache()
      .ForOneApp(app_id, [&policy_ids](const apps::AppUpdate& update) {
        policy_ids = update.PolicyIds();
      });
  return policy_ids;
}

}  // namespace apps_util