OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // This class encapsulates the implementation of multiple profiles by using |
| 6 // the user-data-dir functionality. |
| 7 |
| 8 #ifndef CHROME_BROWSER_USER_DATA_MANAGER_H_ |
| 9 #define CHROME_BROWSER_USER_DATA_MANAGER_H_ |
| 10 |
| 11 #include <string> |
| 12 #include <vector> |
| 13 |
| 14 #include "base/basictypes.h" |
| 15 #include "base/ref_counted.h" |
| 16 |
| 17 class MessageLoop; |
| 18 |
| 19 // Provides an abstraction of profiles on top of the user data directory |
| 20 // feature. Given the root of the user data directories, it provides |
| 21 // functionality to enumerate the existing profiles and start Chrome in a |
| 22 // given profile. |
| 23 // Also holds a shared instance of its own for convenience though it's not a |
| 24 // singleton class. The shared instance should be created by the main thread, |
| 25 // then other threads can access and use the shared instance. |
| 26 class UserDataManager { |
| 27 public: |
| 28 // Creates the shared instance of this class. This method is not thread-safe, |
| 29 // so the shared instance should be created on the main thread. |
| 30 static void Create(); |
| 31 |
| 32 // Returns the shared instance. CreateInstance must be called before callling |
| 33 // this method. |
| 34 static UserDataManager* Get(); |
| 35 |
| 36 // Creates a new instance with the given root folder for storing user data |
| 37 // folders. |
| 38 explicit UserDataManager(const std::wstring& user_data_root); |
| 39 |
| 40 ~UserDataManager(); |
| 41 |
| 42 // Returns the name of the current profile. |
| 43 std::wstring current_profile_name() const { return current_profile_name_; } |
| 44 |
| 45 // Returns whether the current profile is the default profile or not. |
| 46 bool is_current_profile_default() const { |
| 47 return is_current_profile_default_; |
| 48 } |
| 49 |
| 50 // Populates the given vector with a list of all the profiles. |
| 51 // This function should be called on the file thread. |
| 52 void GetProfiles(std::vector<std::wstring>* profiles) const; |
| 53 |
| 54 // Creates a desktop shortcut for the given profile name. |
| 55 // Returns false if the shortcut creation fails; true otherwise. |
| 56 bool CreateDesktopShortcutForProfile(const std::wstring& profile_name) const; |
| 57 |
| 58 // Starts a new Chrome instance in the given profile name. |
| 59 void LaunchChromeForProfile(const std::wstring& profile_name) const; |
| 60 |
| 61 // Starts a new Chrome instance in the profile with the given index. The |
| 62 // index is zero based, and refers to the position of the profile in the |
| 63 // list of profile names in alphabetical order. |
| 64 // This method launches Chrome asynchornously since it enumerates profiles |
| 65 // on a separate thread. |
| 66 void LaunchChromeForProfile(int index) const; |
| 67 |
| 68 private: |
| 69 // Gets the name of the profile from the name of the folder. |
| 70 // Returns false if the folder does not correspond to a profile folder, true |
| 71 // otherwise. |
| 72 static bool GetProfileNameFromFolderName(const std::wstring& folder_name, |
| 73 std::wstring* profile_name); |
| 74 |
| 75 // Returns the name of the folder from the name of the profile. |
| 76 static std::wstring GetFolderNameFromProfileName( |
| 77 const std::wstring& profile_name); |
| 78 |
| 79 // Returns the path of the user data folder for the given profile. |
| 80 std::wstring GetUserDataFolderForProfile( |
| 81 const std::wstring& profile_name) const; |
| 82 |
| 83 // Returns the command to start the app in the given profile. |
| 84 std::wstring GetCommandForProfile(const std::wstring& profile_name) const; |
| 85 |
| 86 // Shared instance. |
| 87 static UserDataManager* instance_; |
| 88 |
| 89 // Root folder. |
| 90 std::wstring user_data_root_; |
| 91 |
| 92 // Current user data folder. |
| 93 std::wstring current_folder_name_; |
| 94 |
| 95 // Whether the current profile is the default profile. |
| 96 bool is_current_profile_default_; |
| 97 |
| 98 // Current profile name. |
| 99 std::wstring current_profile_name_; |
| 100 |
| 101 DISALLOW_COPY_AND_ASSIGN(UserDataManager); |
| 102 }; |
| 103 |
| 104 // Helper class to enumerate the profiles asynchronously on the file thread. |
| 105 // It calls the given delegate instance when the enumeration is complete. |
| 106 // USAGE: Create an instance of the helper with a delegate instance, call the |
| 107 // asynchronous method GetProfiles. The delegate instance will be called when |
| 108 // enumerating profiles is done. |
| 109 // IMPORTANT: It's the responsibility of the caller to call OnDelegateDeleted |
| 110 // method when the delegate instance is deleted. Typically OnDelegateDeleted |
| 111 // should be called in the destructor of the delegate. This is the way to |
| 112 // tell the helper to not call the delegate when enumerating profiles is done. |
| 113 class GetProfilesHelper |
| 114 : public base::RefCountedThreadSafe<GetProfilesHelper> { |
| 115 public: |
| 116 // Interface the delegate classes should implement. |
| 117 class Delegate { |
| 118 public: |
| 119 virtual void OnGetProfilesDone( |
| 120 const std::vector<std::wstring>& profiles) = 0; |
| 121 virtual ~Delegate() { } |
| 122 }; |
| 123 |
| 124 explicit GetProfilesHelper(Delegate* delegate); |
| 125 |
| 126 // Asynchronous call to get the list of profiles. Calls the delegate when done |
| 127 // on either the given target loop or the message loop on which this function |
| 128 // is called if target loop is NULL. |
| 129 void GetProfiles(MessageLoop* target_loop); |
| 130 |
| 131 // Records that the delegate is deleted. |
| 132 void OnDelegateDeleted(); |
| 133 |
| 134 private: |
| 135 // Helper to get the profiles from user data manager. |
| 136 void GetProfilesFromManager(); |
| 137 |
| 138 // Helper to invoke the delegate. |
| 139 void InvokeDelegate(std::vector<std::wstring>* profiles); |
| 140 |
| 141 // Delegate to call. |
| 142 Delegate* delegate_; |
| 143 // Message loop to post tasks on completion of loading profiles. |
| 144 MessageLoop* message_loop_; |
| 145 |
| 146 DISALLOW_COPY_AND_ASSIGN(GetProfilesHelper); |
| 147 }; |
| 148 |
| 149 #endif // CHROME_BROWSER_USER_DATA_MANAGER_H_ |
OLD | NEW |