blob: 14b9faa23507ece2c67bf2931a23cd589e6793bc (
plain)
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
|
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// This class encapsulates the implementation of multiple profiles by using
// the user-data-dir functionality.
#ifndef CHROME_BROWSER_USER_DATA_MANAGER_H_
#define CHROME_BROWSER_USER_DATA_MANAGER_H_
#include <string>
#include <vector>
#include "base/basictypes.h"
#include "base/ref_counted.h"
class MessageLoop;
// Provides an abstraction of profiles on top of the user data directory
// feature. Given the root of the user data directories, it provides
// functionality to enumerate the existing profiles and start Chrome in a
// given profile.
// Also holds a shared instance of its own for convenience though it's not a
// singleton class. The shared instance should be created by the main thread,
// then other threads can access and use the shared instance.
class UserDataManager {
public:
// Creates the shared instance of this class. This method is not thread-safe,
// so the shared instance should be created on the main thread.
static UserDataManager* Create();
// Returns the shared instance. CreateInstance must be called before callling
// this method.
static UserDataManager* Get();
// Creates a new instance with the given root folder for storing user data
// folders.
explicit UserDataManager(const std::wstring& user_data_root);
~UserDataManager();
// Returns the name of the current profile.
std::wstring current_profile_name() const { return current_profile_name_; }
// Returns whether the current profile is the default profile or not.
bool is_current_profile_default() const {
return is_current_profile_default_;
}
// Populates the given vector with a list of all the profiles.
// This function should be called on the file thread.
void GetProfiles(std::vector<std::wstring>* profiles) const;
// Creates a desktop shortcut for the given profile name.
// Returns false if the shortcut creation fails; true otherwise.
bool CreateDesktopShortcutForProfile(const std::wstring& profile_name) const;
// Starts a new Chrome instance in the given profile name.
void LaunchChromeForProfile(const std::wstring& profile_name) const;
// Starts a new Chrome instance in the profile with the given index. The
// index is zero based, and refers to the position of the profile in the
// list of profile names in alphabetical order.
// This method launches Chrome asynchornously since it enumerates profiles
// on a separate thread.
void LaunchChromeForProfile(int index) const;
private:
// Gets the name of the profile from the name of the folder.
// Returns false if the folder does not correspond to a profile folder, true
// otherwise.
static bool GetProfileNameFromFolderName(const std::wstring& folder_name,
std::wstring* profile_name);
// Returns the name of the folder from the name of the profile.
static std::wstring GetFolderNameFromProfileName(
const std::wstring& profile_name);
// Returns the path of the user data folder for the given profile.
std::wstring GetUserDataFolderForProfile(
const std::wstring& profile_name) const;
// Shared instance.
static UserDataManager* instance_;
// Root folder.
std::wstring user_data_root_;
// Current user data folder.
std::wstring current_folder_name_;
// Whether the current profile is the default profile.
bool is_current_profile_default_;
// Current profile name.
std::wstring current_profile_name_;
DISALLOW_COPY_AND_ASSIGN(UserDataManager);
};
// Helper class to enumerate the profiles asynchronously on the file thread.
// It calls the given delegate instance when the enumeration is complete.
// USAGE: Create an instance of the helper with a delegate instance, call the
// asynchronous method GetProfiles. The delegate instance will be called when
// enumerating profiles is done.
// IMPORTANT: It's the responsibility of the caller to call OnDelegateDeleted
// method when the delegate instance is deleted. Typically OnDelegateDeleted
// should be called in the destructor of the delegate. This is the way to
// tell the helper to not call the delegate when enumerating profiles is done.
class GetProfilesHelper
: public base::RefCountedThreadSafe<GetProfilesHelper> {
public:
// Interface the delegate classes should implement.
class Delegate {
public:
virtual void OnGetProfilesDone(
const std::vector<std::wstring>& profiles) = 0;
virtual ~Delegate() { }
};
explicit GetProfilesHelper(Delegate* delegate);
// Asynchronous call to get the list of profiles. Calls the delegate when done
// on either the given target loop or the message loop on which this function
// is called if target loop is NULL.
void GetProfiles(MessageLoop* target_loop);
// Records that the delegate is deleted.
void OnDelegateDeleted();
private:
friend class base::RefCountedThreadSafe<GetProfilesHelper>;
~GetProfilesHelper() {}
// Helper to get the profiles from user data manager.
void GetProfilesFromManager();
// Helper to invoke the delegate.
void InvokeDelegate(std::vector<std::wstring>* profiles);
// Delegate to call.
Delegate* delegate_;
// Message loop to post tasks on completion of loading profiles.
MessageLoop* message_loop_;
DISALLOW_COPY_AND_ASSIGN(GetProfilesHelper);
};
#endif // CHROME_BROWSER_USER_DATA_MANAGER_H_
|