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_UI_APP_LIST_PROFILE_LOADER_H_
6#define CHROME_BROWSER_UI_APP_LIST_PROFILE_LOADER_H_
7
8#include "base/callback.h"
9#include "base/memory/weak_ptr.h"
10
11namespace base {
12class FilePath;
13}
14
15class Profile;
16class ProfileLoaderUnittest;
17class ProfileStore;
18class ScopedKeepAlive;
19
20// This class loads profiles asynchronously and calls the provided callback once
21// the profile is ready. Only the callback for the most recent load request is
22// called, and only if the load was successful.
23//
24// This is useful for loading profiles in response to user interaction where
25// only the latest requested profile is required.
26// TODO(koz): Merge this into AppListServiceImpl.
27class ProfileLoader {
28 public:
29  explicit ProfileLoader(ProfileStore* profile_store);
30  ~ProfileLoader();
31
32  bool IsAnyProfileLoading() const;
33  void InvalidatePendingProfileLoads();
34  void LoadProfileInvalidatingOtherLoads(
35      const base::FilePath& profile_file_path,
36      base::Callback<void(Profile*)> callback);
37
38 private:
39  friend class ::ProfileLoaderUnittest;
40
41  void OnProfileLoaded(int profile_load_sequence_id,
42                       base::Callback<void(Profile*)> callback,
43                       Profile* profile);
44
45  void IncrementPendingProfileLoads();
46  void DecrementPendingProfileLoads();
47
48  ProfileStore* profile_store_;
49  scoped_ptr<ScopedKeepAlive> keep_alive_;
50  int profile_load_sequence_id_;
51  int pending_profile_loads_;
52
53  base::WeakPtrFactory<ProfileLoader> weak_factory_;
54
55  DISALLOW_COPY_AND_ASSIGN(ProfileLoader);
56};
57
58#endif  // CHROME_BROWSER_UI_APP_LIST_PROFILE_LOADER_H_
59