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
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_PROFILES_PROFILE_SHORTCUT_MANAGER_WIN_H_
#define CHROME_BROWSER_PROFILES_PROFILE_SHORTCUT_MANAGER_WIN_H_
#include <set>
#include <string>
#include <string_view>
#include "base/files/file_path.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "chrome/browser/profiles/profile_attributes_storage.h"
#include "chrome/browser/profiles/profile_manager_observer.h"
#include "chrome/browser/profiles/profile_shortcut_manager.h"
// Internal free-standing functions that are exported here for testing.
namespace profiles {
// Name of the badged icon file generated for a given profile.
extern const base::FilePath::StringViewType kProfileIconFileName;
namespace internal {
// Returns the full path to the profile icon file.
base::FilePath GetProfileIconPath(const base::FilePath& profile_path);
// Returns the default shortcut filename for the given profile name. Returns a
// filename appropriate for a single-user installation if |profile_name| is
// empty.
std::wstring GetShortcutFilenameForProfile(const std::u16string& profile_name);
// The same as GetShortcutFilenameForProfile but uniqueness is guaranteed.
// Makes an unique filename among |excludes|.
std::wstring GetUniqueShortcutFilenameForProfile(
const std::u16string& profile_name,
const std::set<base::FilePath>& excludes);
// Looks through the various Windows directories that could have pinned
// shortcuts and returns a vector of shortcuts with profile `profile_path`.
const std::vector<base::FilePath> GetPinnedShortCutsForProfile(
const base::FilePath& profile_path);
// This class checks that shortcut filename matches certain profile.
class ShortcutFilenameMatcher {
public:
explicit ShortcutFilenameMatcher(const std::u16string& profile_name);
ShortcutFilenameMatcher(const ShortcutFilenameMatcher&) = delete;
ShortcutFilenameMatcher& operator=(const ShortcutFilenameMatcher&) = delete;
// Check that shortcut filename has a name given by us (by
// GetShortcutFilenameForProfile or GetUniqueShortcutFilenameForProfile).
bool IsCanonical(const std::wstring& filename) const;
private:
const std::wstring profile_shortcut_filename_;
const std::wstring_view lnk_ext_;
std::wstring_view profile_shortcut_name_;
};
// Returns the command-line flags to launch Chrome with the given profile.
std::wstring CreateProfileShortcutFlags(const base::FilePath& profile_path,
const bool incognito = false);
} // namespace internal
} // namespace profiles
class ProfileShortcutManagerWin : public ProfileShortcutManager,
public ProfileAttributesStorage::Observer,
public ProfileManagerObserver {
public:
// Specifies whether only the existing shortcut should be updated, a new
// shortcut should be created if none exist, or only the icon for this profile
// should be created in the profile directory.
enum CreateOrUpdateMode {
UPDATE_EXISTING_ONLY,
CREATE_WHEN_NONE_FOUND,
CREATE_OR_UPDATE_ICON_ONLY,
};
// Specifies whether non-profile shortcuts should be updated. This also
// includes default profile shortcuts, which point at the default
// profile, but don't have a profile name in their filename.
enum NonProfileShortcutAction {
IGNORE_NON_PROFILE_SHORTCUTS,
UPDATE_NON_PROFILE_SHORTCUTS,
};
// Unit tests can't launch other processes.
static void DisableUnpinningForTests();
static void DisableOutOfProcessShortcutOpsForTests();
explicit ProfileShortcutManagerWin(ProfileManager* manager);
ProfileShortcutManagerWin(const ProfileShortcutManagerWin&) = delete;
ProfileShortcutManagerWin& operator=(const ProfileShortcutManagerWin&) =
delete;
~ProfileShortcutManagerWin() override;
// ProfileShortcutManager implementation:
void CreateOrUpdateProfileIcon(const base::FilePath& profile_path) override;
void CreateProfileShortcut(const base::FilePath& profile_path) override;
void RemoveProfileShortcuts(const base::FilePath& profile_path) override;
void HasProfileShortcuts(const base::FilePath& profile_path,
base::OnceCallback<void(bool)> callback) override;
void GetShortcutProperties(const base::FilePath& profile_path,
base::CommandLine* command_line,
std::wstring* name,
base::FilePath* icon_path) override;
// ProfileAttributesStorage::Observer implementation:
void OnProfileAdded(const base::FilePath& profile_path) override;
void OnProfileWasRemoved(const base::FilePath& profile_path,
const std::u16string& profile_name) override;
void OnProfileNameChanged(const base::FilePath& profile_path,
const std::u16string& old_profile_name) override;
void OnProfileAvatarChanged(const base::FilePath& profile_path) override;
void OnProfileHighResAvatarLoaded(
const base::FilePath& profile_path) override;
// ProfileManagerObserver:
void OnProfileAdded(Profile* profile) override;
private:
// Gives the profile path of an alternate profile than |profile_path|.
// Must only be called when the number profiles is 2.
base::FilePath GetOtherProfilePath(const base::FilePath& profile_path);
// Creates or updates shortcuts for the profile at |profile_path| according
// to the specified |create_mode|, |action|, and |incognito|. This will always
// involve creating or updating the icon file for this profile.
void CreateOrUpdateShortcutsForProfileAtPath(
const base::FilePath& profile_path,
CreateOrUpdateMode create_mode,
NonProfileShortcutAction action,
bool incognito);
raw_ptr<ProfileManager> profile_manager_;
// The profile icon of these profiles needs to be updated when an avatar image
// is loaded.
std::set<base::FilePath> profiles_with_pending_avatar_load_;
};
#endif // CHROME_BROWSER_PROFILES_PROFILE_SHORTCUT_MANAGER_WIN_H_
|