Upstream version 7.36.149.0
[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.h"
13 #include "chrome/browser/chromeos/login/user_manager.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/profiles/profile_manager.h"
16 #include "chrome/common/chrome_constants.h"
17 #include "chrome/common/chrome_switches.h"
18 #include "chromeos/chromeos_switches.h"
19
20 namespace chromeos {
21
22 namespace {
23
24 bool ShouldAddProfileDirPrefix(const std::string& user_id_hash) {
25   // Do not add profile dir prefix for legacy profile dir and test
26   // user profile. The reason of not adding prefix for test user profile
27   // is to keep the promise that TestingProfile::kTestUserProfileDir and
28   // chrome::kTestUserProfileDir are always in sync. Otherwise,
29   // TestingProfile::kTestUserProfileDir needs to be dynamically calculated
30   // based on whether multi profile is enabled or not.
31   return user_id_hash != chrome::kLegacyProfileDir &&
32       user_id_hash != chrome::kTestUserProfileDir;
33 }
34
35 }  // anonymous namespace
36
37 ////////////////////////////////////////////////////////////////////////////////
38 // ProfileHelper, public
39
40 ProfileHelper::ProfileHelper()
41   : signin_profile_clear_requested_(false) {
42 }
43
44 ProfileHelper::~ProfileHelper() {
45   // Checking whether UserManager is initialized covers case
46   // when ScopedTestUserManager is used.
47   if (UserManager::IsInitialized())
48     UserManager::Get()->RemoveSessionStateObserver(this);
49 }
50
51 // static
52 Profile* ProfileHelper::GetProfileByUserIdHash(
53     const std::string& user_id_hash) {
54   ProfileManager* profile_manager = g_browser_process->profile_manager();
55   return profile_manager->GetProfile(GetProfilePathByUserIdHash(user_id_hash));
56 }
57
58 // static
59 base::FilePath ProfileHelper::GetProfilePathByUserIdHash(
60     const std::string& user_id_hash) {
61   ProfileManager* profile_manager = g_browser_process->profile_manager();
62   base::FilePath profile_path = profile_manager->user_data_dir();
63
64   return profile_path.Append(GetUserProfileDir(user_id_hash));
65 }
66
67 // static
68 base::FilePath ProfileHelper::GetProfileDirByLegacyLoginProfileSwitch() {
69   const std::string login_profile_value =
70       CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
71           chromeos::switches::kLoginProfile);
72   return ProfileHelper::GetUserProfileDir(login_profile_value);
73 }
74
75 // static
76 base::FilePath ProfileHelper::GetSigninProfileDir() {
77   ProfileManager* profile_manager = g_browser_process->profile_manager();
78   base::FilePath user_data_dir = profile_manager->user_data_dir();
79   return user_data_dir.AppendASCII(chrome::kInitialProfile);
80 }
81
82 // static
83 Profile* ProfileHelper::GetSigninProfile() {
84   ProfileManager* profile_manager = g_browser_process->profile_manager();
85   return profile_manager->GetProfile(GetSigninProfileDir())->
86       GetOffTheRecordProfile();
87 }
88
89 // static
90 std::string ProfileHelper::GetUserIdHashFromProfile(Profile* profile) {
91   if (!profile)
92     return std::string();
93
94   std::string profile_dir = profile->GetPath().BaseName().value();
95
96   // Don't strip prefix if the dir is not supposed to be prefixed.
97   if (!ShouldAddProfileDirPrefix(profile_dir))
98     return profile_dir;
99
100   // Check that profile directory starts with the correct prefix.
101   std::string prefix(chrome::kProfileDirPrefix);
102   if (profile_dir.find(prefix) != 0) {
103     // This happens when creating a TestingProfile in browser tests.
104     return std::string();
105   }
106
107   return profile_dir.substr(prefix.length(),
108                             profile_dir.length() - prefix.length());
109 }
110
111 // static
112 base::FilePath ProfileHelper::GetUserProfileDir(
113     const std::string& user_id_hash) {
114   DCHECK(!user_id_hash.empty());
115   return ShouldAddProfileDirPrefix(user_id_hash)
116              ? base::FilePath(chrome::kProfileDirPrefix + user_id_hash)
117              : base::FilePath(user_id_hash);
118 }
119
120 // static
121 bool ProfileHelper::IsSigninProfile(Profile* profile) {
122   return profile->GetPath().BaseName().value() == chrome::kInitialProfile;
123 }
124
125 // static
126 bool ProfileHelper::IsOwnerProfile(Profile* profile) {
127   if (!profile)
128     return false;
129   chromeos::UserManager* manager = chromeos::UserManager::Get();
130   chromeos::User* user = manager->GetUserByProfile(profile);
131   if (!user)
132     return false;
133   return user->email() == manager->GetOwnerEmail();
134 }
135
136 void ProfileHelper::ProfileStartup(Profile* profile, bool process_startup) {
137   // Initialize Chrome OS preferences like touch pad sensitivity. For the
138   // preferences to work in the guest mode, the initialization has to be
139   // done after |profile| is switched to the incognito profile (which
140   // is actually GuestSessionProfile in the guest mode). See the
141   // GetOffTheRecordProfile() call above.
142   profile->InitChromeOSPreferences();
143
144   // Add observer so we can see when the first profile's session restore is
145   // completed. After that, we won't need the default profile anymore.
146   if (!IsSigninProfile(profile) &&
147       UserManager::Get()->IsLoggedInAsRegularUser() &&
148       !UserManager::Get()->IsLoggedInAsStub()) {
149     chromeos::OAuth2LoginManager* login_manager =
150         chromeos::OAuth2LoginManagerFactory::GetInstance()->GetForProfile(
151             profile);
152     if (login_manager)
153       login_manager->AddObserver(this);
154   }
155 }
156
157 base::FilePath ProfileHelper::GetActiveUserProfileDir() {
158   return ProfileHelper::GetUserProfileDir(active_user_id_hash_);
159 }
160
161 void ProfileHelper::Initialize() {
162   UserManager::Get()->AddSessionStateObserver(this);
163 }
164
165 void ProfileHelper::ClearSigninProfile(const base::Closure& on_clear_callback) {
166   on_clear_callbacks_.push_back(on_clear_callback);
167   if (signin_profile_clear_requested_)
168     return;
169   ProfileManager* profile_manager = g_browser_process->profile_manager();
170   // Check if signin profile was loaded.
171   if (!profile_manager->GetProfileByPath(GetSigninProfileDir())) {
172     OnBrowsingDataRemoverDone();
173     return;
174   }
175   signin_profile_clear_requested_ = true;
176   BrowsingDataRemover* remover =
177       BrowsingDataRemover::CreateForUnboundedRange(GetSigninProfile());
178   remover->AddObserver(this);
179   remover->Remove(BrowsingDataRemover::REMOVE_SITE_DATA,
180                   BrowsingDataHelper::ALL);
181 }
182
183 ////////////////////////////////////////////////////////////////////////////////
184 // ProfileHelper, BrowsingDataRemover::Observer implementation:
185
186 void ProfileHelper::OnBrowsingDataRemoverDone() {
187   signin_profile_clear_requested_ = false;
188   for (size_t i = 0; i < on_clear_callbacks_.size(); ++i) {
189     if (!on_clear_callbacks_[i].is_null())
190       on_clear_callbacks_[i].Run();
191   }
192   on_clear_callbacks_.clear();
193 }
194
195 ////////////////////////////////////////////////////////////////////////////////
196 // ProfileHelper, OAuth2LoginManager::Observer implementation:
197
198 void ProfileHelper::OnSessionRestoreStateChanged(
199     Profile* user_profile,
200     OAuth2LoginManager::SessionRestoreState state) {
201   if (state == OAuth2LoginManager::SESSION_RESTORE_DONE ||
202       state == OAuth2LoginManager::SESSION_RESTORE_FAILED ||
203       state == OAuth2LoginManager::SESSION_RESTORE_CONNECTION_FAILED) {
204     chromeos::OAuth2LoginManager* login_manager =
205         chromeos::OAuth2LoginManagerFactory::GetInstance()->
206             GetForProfile(user_profile);
207     login_manager->RemoveObserver(this);
208     ClearSigninProfile(base::Closure());
209   }
210 }
211
212 ////////////////////////////////////////////////////////////////////////////////
213 // ProfileHelper, UserManager::UserSessionStateObserver implementation:
214
215 void ProfileHelper::ActiveUserHashChanged(const std::string& hash) {
216   active_user_id_hash_ = hash;
217   base::FilePath profile_path = GetProfilePathByUserIdHash(hash);
218   VLOG(1) << "Switching to profile path: " << profile_path.value();
219 }
220
221 }  // namespace chromeos