- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / app_list / profile_loader.cc
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 #include "chrome/browser/ui/app_list/profile_loader.h"
6
7 #include "base/bind.h"
8 #include "base/files/file_path.h"
9 #include "base/memory/weak_ptr.h"
10 #include "chrome/browser/lifetime/application_lifetime.h"
11 #include "chrome/browser/profiles/profile_manager.h"
12
13 ProfileLoader::ProfileLoader(ProfileStore* profile_store,
14                              scoped_ptr<KeepAliveService> keep_alive_service)
15   : profile_store_(profile_store),
16     keep_alive_service_(keep_alive_service.Pass()),
17     profile_load_sequence_id_(0),
18     pending_profile_loads_(0),
19     weak_factory_(this) {
20 }
21
22 ProfileLoader::~ProfileLoader() {
23 }
24
25 bool ProfileLoader::IsAnyProfileLoading() const {
26   return pending_profile_loads_ > 0;
27 }
28
29 void ProfileLoader::InvalidatePendingProfileLoads() {
30   ++profile_load_sequence_id_;
31 }
32
33 void ProfileLoader::LoadProfileInvalidatingOtherLoads(
34     const base::FilePath& profile_file_path,
35     base::Callback<void(Profile*)> callback) {
36   InvalidatePendingProfileLoads();
37
38   Profile* profile = profile_store_->GetProfileByPath(profile_file_path);
39   if (profile) {
40     callback.Run(profile);
41     return;
42   }
43
44   IncrementPendingProfileLoads();
45   profile_store_->LoadProfileAsync(
46       profile_file_path,
47       base::Bind(&ProfileLoader::OnProfileLoaded,
48                  weak_factory_.GetWeakPtr(),
49                  profile_load_sequence_id_,
50                  callback));
51 }
52
53 void ProfileLoader::OnProfileLoaded(int profile_load_sequence_id,
54                                     base::Callback<void(Profile*)> callback,
55                                     Profile* profile) {
56   DecrementPendingProfileLoads();
57   if (profile_load_sequence_id == profile_load_sequence_id_)
58     callback.Run(profile);
59 }
60
61 void ProfileLoader::IncrementPendingProfileLoads() {
62   pending_profile_loads_++;
63   if (pending_profile_loads_ == 1)
64     keep_alive_service_->EnsureKeepAlive();
65 }
66
67 void ProfileLoader::DecrementPendingProfileLoads() {
68   pending_profile_loads_--;
69   if (pending_profile_loads_ == 0)
70     keep_alive_service_->FreeKeepAlive();
71 }