summaryrefslogtreecommitdiffstats
path: root/chrome/browser/profile_manager.h
blob: 92ee707d6285637962d798a8b3c0b5c1bc6cd3d9 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// 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 keeps track of the currently-active profiles in the runtime.

#ifndef CHROME_BROWSER_PROFILE_MANAGER_H__
#define CHROME_BROWSER_PROFILE_MANAGER_H__

#include <map>
#include <string>
#include <vector>

#include "base/basictypes.h"
#include "base/values.h"
#include "chrome/browser/profile.h"

// This is a small storage class that simply represents some metadata about
// profiles that are available in the current user data directory.
// These are cached in local state so profiles don't need to be scanned
// for their metadata on every launch.
class AvailableProfile {
 public:
  AvailableProfile(const std::wstring& name,
                   const std::wstring& id,
                   const std::wstring& directory)
      : name_(name), id_(id), directory_(directory) {}

  // Decodes a DictionaryValue into an AvailableProfile
  static AvailableProfile* FromValue(DictionaryValue* value) {
    DCHECK(value);
    std::wstring name, id, directory;
    value->GetString(L"name", &name);
    value->GetString(L"id", &id);
    value->GetString(L"directory", &directory);
    return new AvailableProfile(name, id, directory);
  }

  // Encodes this AvailableProfile into a new DictionaryValue
  DictionaryValue* ToValue() {
    DictionaryValue* value = new DictionaryValue;
    value->SetString(L"name", name_);
    value->SetString(L"id", id_);
    value->SetString(L"directory", directory_);
    return value;
  }

  std::wstring name() const { return name_; }
  std::wstring id() const { return id_; }
  std::wstring directory() const { return directory_; }

 private:
  std::wstring name_;       // User-visible profile name
  std::wstring id_;         // Profile identifier
  std::wstring directory_;  // Subdirectory containing profile (not full path)

  DISALLOW_EVIL_CONSTRUCTORS(AvailableProfile);
};

class ProfileManager {
 public:
  ProfileManager();
  virtual ~ProfileManager();

  // ProfileManager prefs are loaded as soon as the profile is created.
  static void RegisterUserPrefs(PrefService* prefs);

  // Invokes ShutdownSessionService() on all profiles.
  static void ShutdownSessionServices();

  // Returns the default profile.  This adds the profile to the
  // ProfileManager if it doesn't already exist.  This method returns NULL if
  // the profile doesn't exist and we can't create it.
  Profile* GetDefaultProfile(const std::wstring& user_data_dir);

  // If a profile with the given path is currently managed by this object,
  // return a pointer to the corresponding Profile object;
  // otherwise return NULL.
  Profile* GetProfileByPath(const std::wstring& path) const;

  // If a profile with the given ID is currently managed by this object,
  // return a pointer to the corresponding Profile object;
  // otherwise returns NULL.
  Profile* GetProfileByID(const std::wstring& id) const;

  // Adds a profile to the set of currently-loaded profiles.  Returns a
  // pointer to a Profile object corresponding to the given path.
  Profile* AddProfileByPath(const std::wstring& path);

  // Adds a profile to the set of currently-loaded profiles.  Returns a
  // pointer to a Profile object corresponding to the given profile ID.
  // If no profile with the given ID is known, returns NULL.
  Profile* AddProfileByID(const std::wstring& id);

  // Adds a pre-existing Profile object to the set managed by this
  // ProfileManager.  This ProfileManager takes ownership of the Profile.
  // The Profile should not already be managed by this ProfileManager.
  // Returns true if the profile was added, false otherwise.
  bool AddProfile(Profile* profile);

  // Removes a profile from the set of currently-loaded profiles. The path must
  // be exactly the same (including case) as when GetProfileByPath was called.
  void RemoveProfileByPath(const std::wstring& path);

  // Removes a profile from the set of currently-loaded profiles.
  // (Does not delete the profile object.)
  void RemoveProfile(Profile* profile);

  // These allow iteration through the current list of profiles.
  typedef std::vector<Profile*> ProfileVector;
  typedef ProfileVector::iterator iterator;
  typedef ProfileVector::const_iterator const_iterator;

  const_iterator begin() { return profiles_.begin(); }
  const_iterator end() { return profiles_.end(); }

  typedef std::vector<AvailableProfile*> AvailableProfileVector;
  const AvailableProfileVector& available_profiles() const {
    return available_profiles_;
  }

  // Creates a new window with the given profile.
  void NewWindowWithProfile(Profile* profile);

  // ------------------ static utility functions -------------------

  // Tries to determine whether the given path represents a profile
  // directory, and returns true if it thinks it does.
  static bool IsProfile(const std::wstring& path);

  // Tries to copy profile data from the source path to the destination path,
  // returning true if successful.
  static bool CopyProfileData(const std::wstring& source_path,
                              const std::wstring& destination_path);

  // Creates a new profile at the specified path with the given name and ID.
  // |name| is the full-length human-readable name for the profile
  // |nickname| is a shorter name for the profile--can be empty string
  // This method should always return a valid Profile (i.e., should never
  // return NULL).
  static Profile* CreateProfile(const std::wstring& path,
                                const std::wstring& name,
                                const std::wstring& nickname,
                                const std::wstring& id);

  // Returns the canonical form of the given ID string.
  static std::wstring CanonicalizeID(const std::wstring& id);

 private:
  // Returns the AvailableProfile entry associated with the given ID,
  // or NULL if no match is found.
  AvailableProfile* GetAvailableProfileByID(const std::wstring& id);

  // We keep a simple vector of profiles rather than something fancier
  // because we expect there to be a small number of profiles active.
  ProfileVector profiles_;

  AvailableProfileVector available_profiles_;

  DISALLOW_EVIL_CONSTRUCTORS(ProfileManager);
};

#endif  // CHROME_BROWSER_PROFILE_MANAGER_H__