1// Copyright 2013 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#ifndef CHROME_BROWSER_PROFILES_PROFILE_LOADER_H_
6#define CHROME_BROWSER_PROFILES_PROFILE_LOADER_H_
7
8#include "base/callback.h"
9#include "base/memory/weak_ptr.h"
10#include "chrome/browser/profiles/profile.h"
11#include "chrome/browser/profiles/profile_manager.h"
12
13namespace base {
14class FilePath;
15}
16
17class ProfileManager;
18
19// This class loads profiles asynchronously and calls the provided callback once
20// the profile is ready. Only the callback for the most recent load request is
21// called, and only if the load was successful.
22//
23// This is useful for loading profiles in response to user interaction where
24// only the latest requested profile is required.
25class ProfileLoader {
26 public:
27  explicit ProfileLoader(ProfileManager* profile_manager);
28  ~ProfileLoader();
29
30  bool IsAnyProfileLoading() const;
31  void InvalidatePendingProfileLoads();
32  void LoadProfileInvalidatingOtherLoads(
33      const base::FilePath& profile_file_path,
34      base::Callback<void(Profile*)> callback);
35
36 protected:
37  // These just call through to the ProfileManager.
38  // Virtual so they can be mocked out in tests.
39  virtual Profile* GetProfileByPath(const base::FilePath& path);
40  virtual void CreateProfileAsync(
41      const base::FilePath& profile_path,
42      const ProfileManager::CreateCallback& callback,
43      const string16& name,
44      const string16& icon_url,
45      const std::string& managed_user_id);
46
47 private:
48  void OnProfileLoaded(int profile_load_sequence_id,
49                       base::Callback<void(Profile*)> callback,
50                       Profile* profile,
51                       Profile::CreateStatus status);
52
53  void IncrementPendingProfileLoads();
54  void DecrementPendingProfileLoads();
55
56  ProfileManager* profile_manager_;
57  int profile_load_sequence_id_;
58  int pending_profile_loads_;
59
60  base::WeakPtrFactory<ProfileLoader> weak_factory_;
61
62  DISALLOW_COPY_AND_ASSIGN(ProfileLoader);
63};
64
65#endif  // CHROME_BROWSER_PROFILES_PROFILE_LOADER_H_
66