profile_manager.h revision 3f50c38dc070f4bb515c1b64450dae14f316474e
1// Copyright (c) 2010 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 keeps track of the currently-active profiles in the runtime.
6
7#ifndef CHROME_BROWSER_PROFILES_PROFILE_MANAGER_H_
8#define CHROME_BROWSER_PROFILES_PROFILE_MANAGER_H_
9#pragma once
10
11#include <vector>
12
13#include "app/system_monitor.h"
14#include "base/basictypes.h"
15#include "base/message_loop.h"
16#include "base/threading/non_thread_safe.h"
17#include "chrome/browser/profiles/profile.h"
18#include "chrome/common/notification_observer.h"
19#include "chrome/common/notification_registrar.h"
20
21class FilePath;
22
23class ProfileManager : public base::NonThreadSafe,
24                       public SystemMonitor::PowerObserver,
25                       public NotificationObserver {
26 public:
27  ProfileManager();
28  virtual ~ProfileManager();
29
30  // Invokes ShutdownSessionService() on all profiles.
31  static void ShutdownSessionServices();
32
33  // Returns the default profile.  This adds the profile to the
34  // ProfileManager if it doesn't already exist.  This method returns NULL if
35  // the profile doesn't exist and we can't create it.
36  // The profile used can be overridden by using --login-profile on cros.
37  Profile* GetDefaultProfile(const FilePath& user_data_dir);
38
39  // Same as instance method but provides the default user_data_dir as well.
40  static Profile* GetDefaultProfile();
41
42  // Returns a profile for a specific profile directory within the user data
43  // dir. This will return an existing profile it had already been created,
44  // otherwise it will create and manage it.
45  Profile* GetProfile(const FilePath& profile_dir);
46
47  // Returns a profile for a specific profile directory within the user data
48  // dir with the option of controlling whether extensions are initialized
49  // or not.  This will return an existing profile it had already been created,
50  // otherwise it will create and manage it.
51  // Note that if the profile has already been created, extensions may have
52  // been initialized.  If this matters to you, you should call GetProfileByPath
53  // first to see if the profile already exists.
54  Profile* GetProfile(const FilePath& profile_dir, bool init_extensions);
55
56  // Returns the directory where the currently active profile is
57  // stored, relative to the user data directory currently in use..
58  FilePath GetCurrentProfileDir();
59
60  // These allow iteration through the current list of profiles.
61  typedef std::vector<Profile*> ProfileVector;
62  typedef ProfileVector::iterator iterator;
63  typedef ProfileVector::const_iterator const_iterator;
64
65  iterator begin() { return profiles_.begin(); }
66  const_iterator begin() const { return profiles_.begin(); }
67  iterator end() { return profiles_.end(); }
68  const_iterator end() const { return profiles_.end(); }
69
70  // PowerObserver notifications
71  virtual void OnSuspend();
72  virtual void OnResume();
73
74  // NotificationObserver implementation.
75  virtual void Observe(NotificationType type,
76                       const NotificationSource& source,
77                       const NotificationDetails& details);
78
79  // ------------------ static utility functions -------------------
80
81  // Returns the path to the default profile directory, based on the given
82  // user data directory.
83  static FilePath GetDefaultProfileDir(const FilePath& user_data_dir);
84
85  // Returns the path to the preferences file given the user profile directory.
86  static FilePath GetProfilePrefsPath(const FilePath& profile_dir);
87
88  // Tries to determine whether the given path represents a profile
89  // directory, and returns true if it thinks it does.
90  static bool IsProfile(const FilePath& path);
91
92  // If a profile with the given path is currently managed by this object,
93  // return a pointer to the corresponding Profile object;
94  // otherwise return NULL.
95  Profile* GetProfileByPath(const FilePath& path) const;
96
97  // Creates a new profile at the specified path.
98  // This method should always return a valid Profile (i.e., should never
99  // return NULL).
100  static Profile* CreateProfile(const FilePath& path);
101
102 private:
103  // Hooks to suspend/resume per-profile network traffic.
104  // These must be called on the IO thread.
105  static void SuspendProfile(Profile*);
106  static void ResumeProfile(Profile*);
107
108  // Adds a pre-existing Profile object to the set managed by this
109  // ProfileManager.  This ProfileManager takes ownership of the Profile.
110  // The Profile should not already be managed by this ProfileManager.
111  // Returns true if the profile was added, false otherwise.
112  bool AddProfile(Profile* profile, bool init_extensions);
113
114  // We keep a simple vector of profiles rather than something fancier
115  // because we expect there to be a small number of profiles active.
116  ProfileVector profiles_;
117
118  NotificationRegistrar registrar_;
119
120  // Indicates that a user has logged in and that the profile specified
121  // in the --login-profile command line argument should be used as the
122  // default.
123  bool logged_in_;
124
125  DISALLOW_COPY_AND_ASSIGN(ProfileManager);
126};
127
128#endif  // CHROME_BROWSER_PROFILES_PROFILE_MANAGER_H_
129