- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / profiles / profile_helper.cc
1 // Copyright (c) 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/chromeos/profiles/profile_helper.h"
6
7 #include "base/callback.h"
8 #include "base/command_line.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/browsing_data/browsing_data_helper.h"
11 #include "chrome/browser/chromeos/login/oauth2_login_manager_factory.h"
12 #include "chrome/browser/chromeos/login/user_manager.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/profiles/profile_manager.h"
15 #include "chrome/common/chrome_constants.h"
16 #include "chrome/common/chrome_switches.h"
17
18 namespace chromeos {
19
20 namespace {
21
22 base::FilePath GetSigninProfileDir() {
23   ProfileManager* profile_manager = g_browser_process->profile_manager();
24   base::FilePath user_data_dir = profile_manager->user_data_dir();
25   return user_data_dir.AppendASCII(chrome::kInitialProfile);
26 }
27
28 }  // anonymous namespace
29
30 ////////////////////////////////////////////////////////////////////////////////
31 // ProfileHelper, public
32
33 ProfileHelper::ProfileHelper()
34   : signin_profile_clear_requested_(false) {
35 }
36
37 ProfileHelper::~ProfileHelper() {
38   // Checking whether UserManager is initialized covers case
39   // when ScopedTestUserManager is used.
40   if (UserManager::IsInitialized())
41     UserManager::Get()->RemoveSessionStateObserver(this);
42 }
43
44 // static
45 Profile* ProfileHelper::GetProfileByUserIdHash(
46     const std::string& user_id_hash) {
47   ProfileManager* profile_manager = g_browser_process->profile_manager();
48   return profile_manager->GetProfile(GetProfilePathByUserIdHash(user_id_hash));
49 }
50
51 // static
52 base::FilePath ProfileHelper::GetProfilePathByUserIdHash(
53     const std::string& user_id_hash) {
54   const CommandLine& command_line = *CommandLine::ForCurrentProcess();
55   // Fails for KioskTest.InstallAndLaunchApp test - crbug.com/238985
56   // Will probably fail for Guest session / restart after a crash -
57   // crbug.com/238998
58   // TODO(nkostylev): Remove this check once these bugs are fixed.
59   if (command_line.HasSwitch(switches::kMultiProfiles))
60     DCHECK(!user_id_hash.empty());
61   ProfileManager* profile_manager = g_browser_process->profile_manager();
62   base::FilePath profile_path = profile_manager->user_data_dir();
63   return profile_path.Append(
64       base::FilePath(chrome::kProfileDirPrefix + user_id_hash));
65 }
66
67 // static
68 Profile* ProfileHelper::GetSigninProfile() {
69   ProfileManager* profile_manager = g_browser_process->profile_manager();
70   return profile_manager->GetProfile(GetSigninProfileDir())->
71       GetOffTheRecordProfile();
72 }
73
74 // static
75 std::string ProfileHelper::GetUserIdHashFromProfile(Profile* profile) {
76   if (!profile)
77     return std::string();
78
79   // Check that profile directory starts with the correct prefix.
80   std::string profile_dir = profile->GetPath().BaseName().value();
81   std::string prefix(chrome::kProfileDirPrefix);
82   if (profile_dir.find(prefix) != 0) {
83     NOTREACHED();
84     return std::string();
85   }
86
87   return profile_dir.substr(prefix.length(),
88                             profile_dir.length() - prefix.length());
89 }
90
91 // static
92 bool ProfileHelper::IsSigninProfile(Profile* profile) {
93   return profile->GetPath().BaseName().value() == chrome::kInitialProfile;
94 }
95
96 void ProfileHelper::ProfileStartup(Profile* profile, bool process_startup) {
97   // Initialize Chrome OS preferences like touch pad sensitivity. For the
98   // preferences to work in the guest mode, the initialization has to be
99   // done after |profile| is switched to the incognito profile (which
100   // is actually GuestSessionProfile in the guest mode). See the
101   // GetOffTheRecordProfile() call above.
102   profile->InitChromeOSPreferences();
103
104   // Add observer so we can see when the first profile's session restore is
105   // completed. After that, we won't need the default profile anymore.
106   if (!IsSigninProfile(profile) &&
107       UserManager::Get()->IsLoggedInAsRegularUser() &&
108       !UserManager::Get()->IsLoggedInAsStub()) {
109     chromeos::OAuth2LoginManager* login_manager =
110         chromeos::OAuth2LoginManagerFactory::GetInstance()->GetForProfile(
111             profile);
112     if (login_manager)
113       login_manager->AddObserver(this);
114   }
115 }
116
117 base::FilePath ProfileHelper::GetActiveUserProfileDir() {
118   return GetUserProfileDir(active_user_id_hash_);
119 }
120
121 base::FilePath ProfileHelper::GetUserProfileDir(
122     const std::string& user_id_hash) {
123   DCHECK(!user_id_hash.empty());
124   return base::FilePath(chrome::kProfileDirPrefix + user_id_hash);
125 }
126
127 void ProfileHelper::Initialize() {
128   UserManager::Get()->AddSessionStateObserver(this);
129 }
130
131 void ProfileHelper::ClearSigninProfile(const base::Closure& on_clear_callback) {
132   on_clear_callbacks_.push_back(on_clear_callback);
133   if (signin_profile_clear_requested_)
134     return;
135   ProfileManager* profile_manager = g_browser_process->profile_manager();
136   // Check if signin profile was loaded.
137   if (!profile_manager->GetProfileByPath(GetSigninProfileDir())) {
138     OnBrowsingDataRemoverDone();
139     return;
140   }
141   signin_profile_clear_requested_ = true;
142   BrowsingDataRemover* remover =
143       BrowsingDataRemover::CreateForUnboundedRange(GetSigninProfile());
144   remover->AddObserver(this);
145   remover->Remove(BrowsingDataRemover::REMOVE_SITE_DATA,
146                   BrowsingDataHelper::ALL);
147 }
148
149 ////////////////////////////////////////////////////////////////////////////////
150 // ProfileHelper, BrowsingDataRemover::Observer implementation:
151
152 void ProfileHelper::OnBrowsingDataRemoverDone() {
153   signin_profile_clear_requested_ = false;
154   for (size_t i = 0; i < on_clear_callbacks_.size(); ++i) {
155     if (!on_clear_callbacks_[i].is_null())
156       on_clear_callbacks_[i].Run();
157   }
158   on_clear_callbacks_.clear();
159 }
160
161 ////////////////////////////////////////////////////////////////////////////////
162 // ProfileHelper, OAuth2LoginManager::Observer implementation:
163
164 void ProfileHelper::OnSessionRestoreStateChanged(
165     Profile* user_profile,
166     OAuth2LoginManager::SessionRestoreState state) {
167   if (state == OAuth2LoginManager::SESSION_RESTORE_DONE ||
168       state == OAuth2LoginManager::SESSION_RESTORE_FAILED ||
169       state == OAuth2LoginManager::SESSION_RESTORE_CONNECTION_FAILED) {
170     chromeos::OAuth2LoginManager* login_manager =
171         chromeos::OAuth2LoginManagerFactory::GetInstance()->
172             GetForProfile(user_profile);
173     login_manager->RemoveObserver(this);
174     ClearSigninProfile(base::Closure());
175   }
176 }
177
178 ////////////////////////////////////////////////////////////////////////////////
179 // ProfileHelper, UserManager::UserSessionStateObserver implementation:
180
181 void ProfileHelper::ActiveUserHashChanged(const std::string& hash) {
182   active_user_id_hash_ = hash;
183   base::FilePath profile_path = GetProfilePathByUserIdHash(hash);
184   LOG(INFO) << "Switching to profile path: " << profile_path.value();
185 }
186
187 }  // namespace chromeos