15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This class keeps track of the currently-active profiles in the runtime. 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef CHROME_BROWSER_PROFILES_PROFILE_MANAGER_H_ 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHROME_BROWSER_PROFILES_PROFILE_MANAGER_H_ 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <list> 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector> 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h" 147d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/containers/hash_tables.h" 152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/files/file_path.h" 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/gtest_prod_util.h" 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/linked_ptr.h" 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h" 199ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "base/message_loop/message_loop.h" 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/threading/non_thread_safe.h" 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/profiles/profile.h" 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/profiles/profile_shortcut_manager.h" 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/browser_list_observer.h" 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/notification_observer.h" 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/notification_registrar.h" 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class NewProfileLauncher; 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ProfileInfoCache; 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ProfileManager : public base::NonThreadSafe, 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public content::NotificationObserver, 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public Profile::Delegate { 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) typedef base::Callback<void(Profile*, Profile::CreateStatus)> CreateCallback; 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) explicit ProfileManager(const base::FilePath& user_data_dir); 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual ~ProfileManager(); 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(ENABLE_SESSION_SERVICE) 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Invokes SessionServiceFactory::ShutdownForProfile() for all profiles. 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static void ShutdownSessionServices(); 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Physically remove deleted profile directories from disk. 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static void NukeDeletedProfilesFromDisk(); 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Same as instance method but provides the default user_data_dir as well. 485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // If the Profile is going to be used to open a new window then consider using 495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // GetLastUsedProfileAllowedByPolicy() instead. 505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) static Profile* GetLastUsedProfile(); 515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Same as GetLastUsedProfile() but returns the incognito Profile if 535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // incognito mode is forced. This should be used if the last used Profile 545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // will be used to open new browser windows. 555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) static Profile* GetLastUsedProfileAllowedByPolicy(); 565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Same as instance method but provides the default user_data_dir as well. 585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) static std::vector<Profile*> GetLastOpenedProfiles(); 59a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 60a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) // Get the profile for the user which created the current session. 615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Note that in case of a guest account this will return a 'suitable' profile. 625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // This function is temporary and will soon be moved to ash. As such avoid 635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // using it at all cost. 645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // TODO(skuhne): Move into ash's new user management function. 65a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) static Profile* GetPrimaryUserProfile(); 66a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 67a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) // Get the profile for the currently active user. 685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Note that in case of a guest account this will return a 'suitable' profile. 695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // This function is temporary and will soon be moved to ash. As such avoid 705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // using it at all cost. 715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // TODO(skuhne): Move into ash's new user management function. 72a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) static Profile* GetActiveUserProfile(); 73a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns a profile for a specific profile directory within the user data 755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // dir. This will return an existing profile it had already been created, 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // otherwise it will create and manage it. 772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) Profile* GetProfile(const base::FilePath& profile_dir); 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns total number of profiles available on this machine. 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) size_t GetNumberOfProfiles(); 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Explicit asynchronous creation of a profile located at |profile_path|. 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // If the profile has already been created then callback is called 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // immediately. Should be called on the UI thread. 852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) void CreateProfileAsync(const base::FilePath& profile_path, 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const CreateCallback& callback, 87a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) const base::string16& name, 88a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) const base::string16& icon_url, 89f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) const std::string& supervised_user_id); 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns true if the profile pointer is known to point to an existing 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // profile. 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool IsValidProfile(Profile* profile); 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns the directory where the first created profile is stored, 96e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch // relative to the user data directory currently in use. 972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::FilePath GetInitialProfileDir(); 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Get the Profile last used (the Profile to which owns the most recently 1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // focused window) with this Chrome build. If no signed profile has been 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // stored in Local State, hand back the Default profile. 1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) Profile* GetLastUsedProfile(const base::FilePath& user_data_dir); 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Get the path of the last used profile, or if that's undefined, the default 1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // profile. 1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::FilePath GetLastUsedProfileDir(const base::FilePath& user_data_dir); 1072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Get the Profiles which are currently open, i.e., have open browsers, or 1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // were open the last time Chrome was running. The Profiles appear in the 1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // order they were opened. The last used profile will be on the list, but its 1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // index on the list will depend on when it was opened (it is not necessarily 1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // the last one). 1132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) std::vector<Profile*> GetLastOpenedProfiles( 1142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const base::FilePath& user_data_dir); 1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns created profiles. Note, profiles order is NOT guaranteed to be 1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // related with the creation order. 1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::vector<Profile*> GetLoadedProfiles() const; 1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // If a profile with the given path is currently managed by this object, 1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // return a pointer to the corresponding Profile object; 1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // otherwise return NULL. 1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) Profile* GetProfileByPath(const base::FilePath& path) const; 1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Creates a new profile in the next available multiprofile directory. 1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Directories are named "profile_1", "profile_2", etc., in sequence of 1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // creation. (Because directories can be removed, however, it may be the case 1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // that at some point the list of numbered profiles is not continuous.) 1292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // |callback| may be invoked multiple times (for CREATE_STATUS_INITIALIZED 1302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // and CREATE_STATUS_CREATED) so binding parameters with bind::Passed() is 131868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // prohibited. Returns the file path to the profile that will be created 132868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // asynchronously. 133868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) static base::FilePath CreateMultiProfileAsync( 134a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) const base::string16& name, 135a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) const base::string16& icon_url, 1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const CreateCallback& callback, 137f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) const std::string& supervised_user_id); 1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1397dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch // Returns the full path to be used for guest profiles. 1407dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch static base::FilePath GetGuestProfilePath(); 1417dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch 14268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) // Get the path of the next profile directory and increment the internal 14368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) // count. 14468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) // Lack of side effects: 14568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) // This function doesn't actually create the directory or touch the file 14668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) // system. 14768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) base::FilePath GenerateNextProfileDirectoryPath(); 14868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) 1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns a ProfileInfoCache object which can be used to get information 1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // about profiles without having to load them from disk. 1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ProfileInfoCache& GetProfileInfoCache(); 1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns a ProfileShortcut Manager that enables the caller to create 1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // profile specfic desktop shortcuts. 1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ProfileShortcutManager* profile_shortcut_manager(); 1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 157868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Schedules the profile at the given path to be deleted on shutdown. If we're 158868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // deleting the last profile, a new one will be created in its place, and in 159868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // that case the callback will be called when profile creation is complete. 1602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) void ScheduleProfileForDeletion(const base::FilePath& profile_dir, 161868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const CreateCallback& callback); 1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 163f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) // Called on start-up if there are any stale ephemeral profiles to be deleted. 164f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) // This can be the case if the browser has crashed and the clean-up code had 165f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) // no chance to run then. 166f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) static void CleanUpStaleProfiles( 167f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) const std::vector<base::FilePath>& profile_paths); 168f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Autoloads profiles if they are running background apps. 1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void AutoloadProfiles(); 1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1728bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) // Initializes user prefs of |profile|. This includes profile name and 1738bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) // avatar values. 1748bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) void InitProfileUserPrefs(Profile* profile); 1758bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Register and add testing profile to the ProfileManager. Use ONLY in tests. 1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // This allows the creation of Profiles outside of the standard creation path 178c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // for testing. If |addToCache|, adds to ProfileInfoCache as well. 179c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // If |start_deferred_task_runners|, starts the deferred task runners. 180c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // Use ONLY in tests. 181c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) void RegisterTestingProfile(Profile* profile, 182c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) bool addToCache, 183c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) bool start_deferred_task_runners); 1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const base::FilePath& user_data_dir() const { return user_data_dir_; } 1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 187c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // For ChromeOS, determines if the user has logged in to a real profile. 188c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) bool IsLoggedIn() const { return logged_in_; } 189c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 1905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // content::NotificationObserver implementation. 1915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) virtual void Observe(int type, 1925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const content::NotificationSource& source, 1935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const content::NotificationDetails& details) OVERRIDE; 1945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Profile::Delegate implementation: 1965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) virtual void OnProfileCreated(Profile* profile, 1975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) bool success, 1985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) bool is_new_profile) OVERRIDE; 1995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected: 2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Does final initial actions. 2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void DoFinalInit(Profile* profile, bool go_off_the_record); 2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void DoFinalInitForServices(Profile* profile, bool go_off_the_record); 2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void DoFinalInitLogging(Profile* profile); 2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Creates a new profile by calling into the profile's profile creation 2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // method. Virtual so that unittests can return a TestingProfile instead 2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // of the Profile's result. 2092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual Profile* CreateProfileHelper(const base::FilePath& path); 2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Creates a new profile asynchronously by calling into the profile's 2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // asynchronous profile creation method. Virtual so that unittests can return 2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // a TestingProfile instead of the Profile's result. 2142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual Profile* CreateProfileAsyncHelper(const base::FilePath& path, 2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Delegate* delegate); 2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private: 2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) friend class TestingProfileManager; 2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) FRIEND_TEST_ALL_PREFIXES(ProfileManagerBrowserTest, DeleteAllProfiles); 220ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch FRIEND_TEST_ALL_PREFIXES(ProfileManagerBrowserTest, SwitchToProfile); 2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // This struct contains information about profiles which are being loaded or 2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // were loaded. 2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) struct ProfileInfo { 2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ProfileInfo(Profile* profile, bool created); 2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ~ProfileInfo(); 2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) scoped_ptr<Profile> profile; 2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Whether profile has been fully loaded (created and initialized). 2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool created; 2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Whether or not this profile should have a shortcut. 2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool create_shortcut; 2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // List of callbacks to run when profile initialization is done. Note, when 2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // profile is fully loaded this vector will be empty. 2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::vector<CreateCallback> callbacks; 2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private: 2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(ProfileInfo); 2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) }; 2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Returns the profile of the active user and / or the off the record profile 2435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // if needed. This adds the profile to the ProfileManager if it doesn't 2445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // already exist. The method will return NULL if the profile doesn't exist 2455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // and we can't create it. 2465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // The profile used can be overridden by using --login-profile on cros. 2475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) Profile* GetActiveUserOrOffTheRecordProfileFromPath( 2485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const base::FilePath& user_data_dir); 2495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Adds a pre-existing Profile object to the set managed by this 2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // ProfileManager. This ProfileManager takes ownership of the Profile. 2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // The Profile should not already be managed by this ProfileManager. 2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns true if the profile was added, false otherwise. 2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool AddProfile(Profile* profile); 2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 256eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // Schedules the profile at the given path to be deleted on shutdown. 257eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch void FinishDeletingProfile(const base::FilePath& profile_dir); 258eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Registers profile with given info. Returns pointer to created ProfileInfo 2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // entry. 2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ProfileInfo* RegisterProfile(Profile* profile, bool created); 2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Returns ProfileInfo associated with given |path|, registred earlier with 2642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // RegisterProfile. 2652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ProfileInfo* GetProfileInfoByPath(const base::FilePath& path) const; 2662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Adds |profile| to the profile info cache if it hasn't been added yet. 2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void AddProfileToCache(Profile* profile); 2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 270d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // Apply settings for (desktop) Guest User profile. 271d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) void SetGuestProfilePrefs(Profile* profile); 272d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // For ChromeOS, determines if profile should be otr. 274868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) bool ShouldGoOffTheRecord(Profile* profile); 2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void RunCallbacks(const std::vector<CreateCallback>& callbacks, 2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Profile* profile, 2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Profile::CreateStatus status); 2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#if !defined(OS_ANDROID) && !defined(OS_IOS) 2811320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // Updates the last active user of the current session. 2821320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // On Chrome OS updating this user will have no effect since when browser is 2831320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // restored after crash there's another preference that is taken into account. 2841320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // See kLastActiveUser in UserManagerBase. 2851320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci void UpdateLastUser(Profile* last_active); 2861320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 2875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) class BrowserListObserver : public chrome::BrowserListObserver { 2885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public: 2895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) explicit BrowserListObserver(ProfileManager* manager); 2905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) virtual ~BrowserListObserver(); 2915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 2925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // chrome::BrowserListObserver implementation. 2935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) virtual void OnBrowserAdded(Browser* browser) OVERRIDE; 2945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) virtual void OnBrowserRemoved(Browser* browser) OVERRIDE; 2955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) virtual void OnBrowserSetLastActive(Browser* browser) OVERRIDE; 2965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 2975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) private: 2985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ProfileManager* profile_manager_; 2995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(BrowserListObserver); 3005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) }; 3015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif // !defined(OS_ANDROID) && !defined(OS_IOS) 3025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 3035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#if defined(OS_MACOSX) 3045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // If the |loaded_profile| has been loaded successfully (according to 3055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // |status|) and isn't already scheduled for deletion, then finishes adding 306eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // |profile_to_delete_dir| to the queue of profiles to be deleted, and updates 307f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) // the kProfileLastUsed preference based on 308f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) // |last_non_supervised_profile_path|. 309eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch void OnNewActiveProfileLoaded( 310eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch const base::FilePath& profile_to_delete_path, 311f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) const base::FilePath& last_non_supervised_profile_path, 312eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch const CreateCallback& original_callback, 313eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch Profile* loaded_profile, 314eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch Profile::CreateStatus status); 3155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif 316eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) content::NotificationRegistrar registrar_; 3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // The path to the user data directory (DIR_USER_DATA). 3202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const base::FilePath user_data_dir_; 3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Indicates that a user has logged in and that the profile specified 3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // in the --login-profile command line argument should be used as the 3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // default. 3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool logged_in_; 3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#if !defined(OS_ANDROID) && !defined(OS_IOS) 3285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) BrowserListObserver browser_list_observer_; 3295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif // !defined(OS_ANDROID) && !defined(OS_IOS) 3305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Maps profile path to ProfileInfo (if profile has been created). Use 3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // RegisterProfile() to add into this map. This map owns all loaded profile 3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // objects in a running instance of Chrome. 3342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) typedef std::map<base::FilePath, linked_ptr<ProfileInfo> > ProfilesInfoMap; 3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ProfilesInfoMap profiles_info_; 3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Object to cache various information about profiles. Contains information 3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // about every profile which has been created for this instance of Chrome, 3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // if it has not been explicitly deleted. 3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) scoped_ptr<ProfileInfoCache> profile_info_cache_; 3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Manages the process of creating, deleteing and updating Desktop shortcuts. 3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) scoped_ptr<ProfileShortcutManager> profile_shortcut_manager_; 3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // For keeping track of the last active profiles. 3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::map<Profile*, int> browser_counts_; 3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // On startup we launch the active profiles in the order they became active 3485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // during the last run. This is why they are kept in a list, not in a set. 3495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::vector<Profile*> active_profiles_; 3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool closing_all_browsers_; 3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(ProfileManager); 3535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 3545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Same as the ProfileManager, but doesn't initialize some services of the 3565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// profile. This one is useful in unittests. 3575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ProfileManagerWithoutInit : public ProfileManager { 3585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 3592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) explicit ProfileManagerWithoutInit(const base::FilePath& user_data_dir); 3605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected: 3625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void DoFinalInitForServices(Profile*, bool) OVERRIDE {} 3635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void DoFinalInitLogging(Profile*) OVERRIDE {} 3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 3655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif // CHROME_BROWSER_PROFILES_PROFILE_MANAGER_H_ 367