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