Owen Min | 0d9183b1 | 2023-08-16 19:37:50 | [diff] [blame] | 1 | // 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 Silva | e165eeb | 2025-03-18 16:44:17 | [diff] [blame] | 7 | #include <variant> |
| 8 | |
Owen Min | 0d9183b1 | 2023-08-16 19:37:50 | [diff] [blame] | 9 | #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 | |
| 15 | namespace enterprise_commands { |
| 16 | namespace { |
| 17 | |
| 18 | const char kProfilePathField[] = "profile_path"; |
| 19 | |
| 20 | } // namespace |
| 21 | |
| 22 | JobProfilePicker::JobProfilePicker(Profile* profile) |
| 23 | : profile_or_profile_manager_(profile) {} |
| 24 | JobProfilePicker::JobProfilePicker(ProfileManager* profile_manager) |
| 25 | : profile_or_profile_manager_(profile_manager) {} |
| 26 | |
| 27 | JobProfilePicker::~JobProfilePicker() = default; |
| 28 | |
| 29 | bool JobProfilePicker::ParseCommandPayload( |
| 30 | const base::Value::Dict& command_payload) { |
Victor Hugo Vianna Silva | e165eeb | 2025-03-18 16:44:17 | [diff] [blame] | 31 | if (std::holds_alternative<raw_ptr<Profile>>(profile_or_profile_manager_)) { |
Owen Min | 0d9183b1 | 2023-08-16 19:37:50 | [diff] [blame] | 32 | 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 Silva | e165eeb | 2025-03-18 16:44:17 | [diff] [blame] | 60 | std::get<raw_ptr<ProfileManager>>(profile_or_profile_manager_) |
Owen Min | 0d9183b1 | 2023-08-16 19:37:50 | [diff] [blame] | 61 | ->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 | |
| 75 | Profile* JobProfilePicker::GetProfile() { |
Victor Hugo Vianna Silva | e165eeb | 2025-03-18 16:44:17 | [diff] [blame] | 76 | if (std::holds_alternative<raw_ptr<Profile>>(profile_or_profile_manager_)) { |
| 77 | return std::get<raw_ptr<Profile>>(profile_or_profile_manager_); |
Owen Min | 0d9183b1 | 2023-08-16 19:37:50 | [diff] [blame] | 78 | } |
Victor Hugo Vianna Silva | e165eeb | 2025-03-18 16:44:17 | [diff] [blame] | 79 | return std::get<raw_ptr<ProfileManager>>(profile_or_profile_manager_) |
Owen Min | 0d9183b1 | 2023-08-16 19:37:50 | [diff] [blame] | 80 | ->GetProfileByPath(profile_path_); |
| 81 | } |
| 82 | |
| 83 | } // namespace enterprise_commands |