File: global_constants.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (194 lines) | stat: -rw-r--r-- 6,407 bytes parent folder | download | duplicates (2)
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
// Copyright 2024 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/enterprise_companion/global_constants.h"

#include <memory>
#include <optional>
#include <string>

#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/json/json_file_value_serializer.h"
#include "base/json/json_reader.h"
#include "base/logging.h"
#include "base/no_destructor.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "base/values.h"
#include "build/build_config.h"
#include "chrome/enterprise_companion/enterprise_companion_branding.h"
#include "chrome/enterprise_companion/installer_paths.h"
#include "url/gurl.h"

#if BUILDFLAG(IS_WIN)
#include "base/strings/utf_string_conversions.h"
#endif

namespace enterprise_companion {

const char kCompanionAppId[] = ENTERPRISE_COMPANION_APPID;

// Overrides JSON keys.
const char kCrashUploadUrlKey[] = "crash_upload_url";
const char kDMEncryptedReportingUrlKey[] = "dm_encrypted_reporting_url";
const char kDMRealtimeReportingUrlKey[] = "dm_realtime_reporting_url";
const char kDMServerUrlKey[] = "dm_server_url";
const char kEventLoggingUrlKey[] = "event_logging_url";
extern const char kEventLoggerMinTimeoutSecKey[] =
    "event-logger-min-timeout-sec";
#if BUILDFLAG(IS_WIN)
const char kNamedPipeSecurityDescriptorKey[] = "named-pipe-security-descriptor";
#endif

namespace {

class GlobalConstantsImpl : public GlobalConstants {
 public:
  GlobalConstantsImpl() {
#ifdef ENTERPRISE_COMPANION_TEST_ONLY
    ApplyOverrides();
#endif
  }

  GURL CrashUploadURL() const override { return crash_upload_url_; }

  GURL DeviceManagementEncryptedReportingURL() const override {
    return device_management_encrypted_reporting_url_;
  }

  GURL DeviceManagementRealtimeReportingURL() const override {
    return device_management_realtime_reporting_url_;
  }

  GURL DeviceManagementServerURL() const override {
    return device_management_server_url_;
  }

  GURL EnterpriseCompanionEventLoggingURL() const override {
    return enterprise_companion_event_logging_url_;
  }

  base::TimeDelta EventLoggerMinTimeout() const override {
    return event_logger_min_timeout_;
  }

#if BUILDFLAG(IS_WIN)
  std::wstring NamedPipeSecurityDescriptor() const override {
    return named_pipe_security_descriptor_;
  }
#endif

 private:
  GURL crash_upload_url_ = GURL(CRASH_UPLOAD_URL);
  GURL device_management_encrypted_reporting_url_ =
      GURL(DEVICE_MANAGEMENT_ENCRYPTED_REPORTING_URL);
  GURL device_management_realtime_reporting_url_ =
      GURL(DEVICE_MANAGEMENT_REALTIME_REPORTING_URL);
  GURL device_management_server_url_ = GURL(DEVICE_MANAGEMENT_SERVER_URL);
  GURL enterprise_companion_event_logging_url_ =
      GURL(ENTERPRISE_COMPANION_EVENT_LOGGING_URL);
  base::TimeDelta event_logger_min_timeout_ = base::Minutes(15);

#if BUILDFLAG(IS_WIN)
  // By default allow access from the local system account only.
  std::wstring named_pipe_security_descriptor_ = L"D:(A;;GA;;;SY)";
#endif

#ifdef ENTERPRISE_COMPANION_TEST_ONLY
  void ApplyOverrides() {
    std::optional<base::FilePath> overrides_json_path = GetOverridesFilePath();
    if (!overrides_json_path) {
      VLOG(1) << "Failed to apply overrides: can't get overrides file path.";
      return;
    }

    JSONFileValueDeserializer parser(*overrides_json_path,
                                     base::JSON_ALLOW_TRAILING_COMMAS);
    int error_code = 0;
    std::string error_message;
    std::unique_ptr<base::Value> parsed_value(
        parser.Deserialize(&error_code, &error_message));
    if (error_code || !parsed_value) {
      VLOG(1) << "Could not parse " << *overrides_json_path << ": error "
              << error_code << ": " << error_message;
      return;
    }

    if (!parsed_value->is_dict()) {
      VLOG(1) << "Invalid data in " << *overrides_json_path << ": not a dict.";
      return;
    }

    const base::Value::Dict& overrides = parsed_value->GetDict();

    ApplyOverride(overrides, kCrashUploadUrlKey, crash_upload_url_);
    ApplyOverride(overrides, kDMEncryptedReportingUrlKey,
                  device_management_encrypted_reporting_url_);
    ApplyOverride(overrides, kDMRealtimeReportingUrlKey,
                  device_management_realtime_reporting_url_);
    ApplyOverride(overrides, kDMServerUrlKey, device_management_server_url_);
    ApplyOverride(overrides, kEventLoggingUrlKey,
                  enterprise_companion_event_logging_url_);
    ApplyOverride(overrides, kEventLoggerMinTimeoutSecKey,
                  event_logger_min_timeout_);

#if BUILDFLAG(IS_WIN)
    ApplyOverride(overrides, kNamedPipeSecurityDescriptorKey,
                  named_pipe_security_descriptor_);
#endif
  }

  void ApplyOverride(const base::Value::Dict& overrides,
                     const std::string& key,
                     GURL& value) {
    const std::string* str = overrides.FindString(key);
    if (str) {
      VLOG(2) << __func__ << ": " << key << " = " << *str;
      value = GURL(*str);
    }
  }

  void ApplyOverride(const base::Value::Dict& overrides,
                     const std::string& key,
                     base::TimeDelta& value) {
    std::optional<int> override_val = overrides.FindInt(key);
    if (override_val) {
      VLOG(2) << __func__ << ": " << key << " = " << *override_val;
      value = base::Seconds(*override_val);
    }
  }

#if BUILDFLAG(IS_WIN)
  void ApplyOverride(const base::Value::Dict& overrides,
                     const std::string& key,
                     std::wstring& value) {
    const std::string* str = overrides.FindString(key);
    if (str) {
      VLOG(2) << __func__ << ": " << key << " = " << *str;
      value = base::UTF8ToWide(*str);
    }
  }
#endif
#endif  // ENTERPRISE_COMPANION_TEST_ONLY
};

}  // namespace

std::optional<base::FilePath> GetOverridesFilePath() {
  std::optional<base::FilePath> install_dir = GetInstallDirectory();
  if (!install_dir) {
    VLOG(1) << "Can't get overrides file path: can't get install dir.";
    return std::nullopt;
  }
  return install_dir->Append(FILE_PATH_LITERAL("overrides.json"));
}

const GlobalConstants* GetGlobalConstants() {
  static const base::NoDestructor<GlobalConstantsImpl> global_constants;
  return global_constants.get();
}

}  // namespace enterprise_companion