blob: 59ebfaff4c5a7352e337b2176fc9293095544cce [file] [log] [blame]
Owen Min0d9183b12023-08-16 19:37:501// Copyright 2023 The Chromium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/enterprise/remote_commands/job_profile_picker.h"
6
Victor Hugo Vianna Silvae165eeb2025-03-18 16:44:177#include <variant>
8
Owen Min0d9183b12023-08-16 19:37:509#include "base/values.h"
10#include "build/build_config.h"
11#include "chrome/browser/profiles/profile.h"
12#include "chrome/browser/profiles/profile_attributes_storage.h"
13#include "chrome/browser/profiles/profile_manager.h"
14
15namespace enterprise_commands {
16namespace {
17
18const char kProfilePathField[] = "profile_path";
19
20} // namespace
21
22JobProfilePicker::JobProfilePicker(Profile* profile)
23 : profile_or_profile_manager_(profile) {}
24JobProfilePicker::JobProfilePicker(ProfileManager* profile_manager)
25 : profile_or_profile_manager_(profile_manager) {}
26
27JobProfilePicker::~JobProfilePicker() = default;
28
29bool JobProfilePicker::ParseCommandPayload(
30 const base::Value::Dict& command_payload) {
Victor Hugo Vianna Silvae165eeb2025-03-18 16:44:1731 if (std::holds_alternative<raw_ptr<Profile>>(profile_or_profile_manager_)) {
Owen Min0d9183b12023-08-16 19:37:5032 return true;
33 }
34
35 const std::string* path = command_payload.FindString(kProfilePathField);
36 if (!path) {
37 return false;
38 }
39
40 // On Windows, file paths are wstring as opposed to string on other platforms.
41 // On POSIX platforms other than MacOS and ChromeOS, the encoding is unknown.
42 //
43 // This path is sent from the server, which obtained it from Chrome in a
44 // previous report, and Chrome casts the path as UTF8 using UTF8Unsafe before
45 // sending it (see BrowserReportGeneratorDesktop::GenerateProfileInfo).
46 // Because of that, the best thing we can do everywhere is try to get the
47 // path from UTF8, and ending up with an invalid path will fail later in
48 // RunImpl when we attempt to get the profile from the path.
49 profile_path_ = base::FilePath::FromUTF8Unsafe(*path);
50#if BUILDFLAG(IS_WIN)
51 // For Windows machines, the path that Chrome reports for the profile is
52 // "Normalized" to all lower-case on the reporting server. This means that
53 // when the server sends the command, the path will be all lower case and
54 // the profile manager won't be able to use it as a key. To avoid this issue,
55 // This code will iterate over all profile paths and find the one that matches
56 // in a case-insensitive comparison. If this doesn't find one, RunImpl will
57 // fail in the same manner as if the profile didn't exist, which is the
58 // expected behavior.
59 ProfileAttributesStorage& storage =
Victor Hugo Vianna Silvae165eeb2025-03-18 16:44:1760 std::get<raw_ptr<ProfileManager>>(profile_or_profile_manager_)
Owen Min0d9183b12023-08-16 19:37:5061 ->GetProfileAttributesStorage();
62 for (ProfileAttributesEntry* entry : storage.GetAllProfilesAttributes()) {
63 base::FilePath entry_path = entry->GetPath();
64
65 if (base::FilePath::CompareEqualIgnoreCase(profile_path_.value(),
66 entry_path.value())) {
67 profile_path_ = entry_path;
68 break;
69 }
70 }
71#endif
72 return true;
73}
74
75Profile* JobProfilePicker::GetProfile() {
Victor Hugo Vianna Silvae165eeb2025-03-18 16:44:1776 if (std::holds_alternative<raw_ptr<Profile>>(profile_or_profile_manager_)) {
77 return std::get<raw_ptr<Profile>>(profile_or_profile_manager_);
Owen Min0d9183b12023-08-16 19:37:5078 }
Victor Hugo Vianna Silvae165eeb2025-03-18 16:44:1779 return std::get<raw_ptr<ProfileManager>>(profile_or_profile_manager_)
Owen Min0d9183b12023-08-16 19:37:5080 ->GetProfileByPath(profile_path_);
81}
82
83} // namespace enterprise_commands