Home | History | Annotate | Download | only in base
      1 // Copyright (c) 2012 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/test/base/testing_profile_manager.h"
      6 
      7 #include "base/memory/ref_counted.h"
      8 #include "base/strings/utf_string_conversions.h"
      9 #include "chrome/browser/extensions/extension_special_storage_policy.h"
     10 #include "chrome/browser/prefs/pref_service_syncable.h"
     11 #include "chrome/browser/profiles/profile_info_cache.h"
     12 #include "chrome/browser/profiles/profile_manager.h"
     13 #include "chrome/test/base/testing_browser_process.h"
     14 #include "testing/gtest/include/gtest/gtest.h"
     15 
     16 const std::string kGuestProfileName = "Guest";
     17 
     18 namespace testing {
     19 
     20 class ProfileManager : public ::ProfileManagerWithoutInit {
     21  public:
     22   explicit ProfileManager(const base::FilePath& user_data_dir)
     23       : ::ProfileManagerWithoutInit(user_data_dir) {}
     24 
     25  protected:
     26   virtual Profile* CreateProfileHelper(
     27       const base::FilePath& file_path) OVERRIDE {
     28     return new TestingProfile(file_path);
     29   }
     30 };
     31 
     32 }  // namespace testing
     33 
     34 TestingProfileManager::TestingProfileManager(TestingBrowserProcess* process)
     35     : called_set_up_(false),
     36       browser_process_(process),
     37       local_state_(process) {
     38 }
     39 
     40 TestingProfileManager::~TestingProfileManager() {
     41   // Destroying this class also destroys the LocalState, so make sure the
     42   // associated ProfileManager is also destroyed.
     43   browser_process_->SetProfileManager(NULL);
     44 }
     45 
     46 bool TestingProfileManager::SetUp() {
     47   SetUpInternal();
     48   return called_set_up_;
     49 }
     50 
     51 TestingProfile* TestingProfileManager::CreateTestingProfile(
     52     const std::string& profile_name,
     53     scoped_ptr<PrefServiceSyncable> prefs,
     54     const base::string16& user_name,
     55     int avatar_id,
     56     const std::string& supervised_user_id,
     57     const TestingProfile::TestingFactories& factories) {
     58   DCHECK(called_set_up_);
     59 
     60   // Create a path for the profile based on the name.
     61   base::FilePath profile_path(profiles_dir_.path());
     62   profile_path = profile_path.AppendASCII(profile_name);
     63 
     64   // Create the profile and register it.
     65   TestingProfile::Builder builder;
     66   builder.SetPath(profile_path);
     67   builder.SetPrefService(prefs.Pass());
     68   builder.SetSupervisedUserId(supervised_user_id);
     69 
     70   for (TestingProfile::TestingFactories::const_iterator it = factories.begin();
     71        it != factories.end(); ++it) {
     72     builder.AddTestingFactory(it->first, it->second);
     73   }
     74 
     75   TestingProfile* profile = builder.Build().release();
     76   profile->set_profile_name(profile_name);
     77   profile_manager_->AddProfile(profile);  // Takes ownership.
     78 
     79   // Update the user metadata.
     80   ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache();
     81   size_t index = cache.GetIndexOfProfileWithPath(profile_path);
     82   cache.SetAvatarIconOfProfileAtIndex(index, avatar_id);
     83   cache.SetSupervisedUserIdOfProfileAtIndex(index, supervised_user_id);
     84   // SetNameOfProfileAtIndex may reshuffle the list of profiles, so we do it
     85   // last.
     86   cache.SetNameOfProfileAtIndex(index, user_name);
     87 
     88   testing_profiles_.insert(std::make_pair(profile_name, profile));
     89 
     90   return profile;
     91 }
     92 
     93 TestingProfile* TestingProfileManager::CreateTestingProfile(
     94     const std::string& name) {
     95   DCHECK(called_set_up_);
     96   return CreateTestingProfile(name, scoped_ptr<PrefServiceSyncable>(),
     97                               base::UTF8ToUTF16(name), 0, std::string(),
     98                               TestingProfile::TestingFactories());
     99 }
    100 
    101 TestingProfile* TestingProfileManager::CreateGuestProfile() {
    102   DCHECK(called_set_up_);
    103 
    104   // Set up a profile with an off the record profile.
    105   TestingProfile::Builder otr_builder;
    106   otr_builder.SetIncognito();
    107   scoped_ptr<TestingProfile> otr_profile(otr_builder.Build());
    108 
    109   // Create the profile and register it.
    110   TestingProfile::Builder builder;
    111   builder.SetGuestSession();
    112   builder.SetPath(ProfileManager::GetGuestProfilePath());
    113 
    114   // Add the guest profile to the profile manager, but not to the info cache.
    115   TestingProfile* profile = builder.Build().release();
    116   profile->set_profile_name(kGuestProfileName);
    117 
    118   otr_profile->SetOriginalProfile(profile);
    119   profile->SetOffTheRecordProfile(otr_profile.PassAs<Profile>());
    120   profile_manager_->AddProfile(profile);  // Takes ownership.
    121   profile_manager_->SetGuestProfilePrefs(profile);
    122 
    123   testing_profiles_.insert(std::make_pair(kGuestProfileName, profile));
    124 
    125   return profile;
    126 }
    127 
    128 void TestingProfileManager::DeleteTestingProfile(const std::string& name) {
    129   DCHECK(called_set_up_);
    130 
    131   TestingProfilesMap::iterator it = testing_profiles_.find(name);
    132   DCHECK(it != testing_profiles_.end());
    133 
    134   TestingProfile* profile = it->second;
    135 
    136   ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache();
    137   cache.DeleteProfileFromCache(profile->GetPath());
    138 
    139   profile_manager_->profiles_info_.erase(profile->GetPath());
    140 }
    141 
    142 void TestingProfileManager::DeleteGuestProfile() {
    143   DCHECK(called_set_up_);
    144 
    145   TestingProfilesMap::iterator it = testing_profiles_.find(kGuestProfileName);
    146   DCHECK(it != testing_profiles_.end());
    147 
    148   profile_manager_->profiles_info_.erase(ProfileManager::GetGuestProfilePath());
    149 }
    150 
    151 void TestingProfileManager::DeleteProfileInfoCache() {
    152   profile_manager_->profile_info_cache_.reset(NULL);
    153 }
    154 
    155 void TestingProfileManager::SetLoggedIn(bool logged_in) {
    156   profile_manager_->logged_in_ = logged_in;
    157 }
    158 
    159 const base::FilePath& TestingProfileManager::profiles_dir() {
    160   DCHECK(called_set_up_);
    161   return profiles_dir_.path();
    162 }
    163 
    164 ProfileManager* TestingProfileManager::profile_manager() {
    165   DCHECK(called_set_up_);
    166   return profile_manager_;
    167 }
    168 
    169 ProfileInfoCache* TestingProfileManager::profile_info_cache() {
    170   DCHECK(called_set_up_);
    171   return &profile_manager_->GetProfileInfoCache();
    172 }
    173 
    174 void TestingProfileManager::SetUpInternal() {
    175   ASSERT_FALSE(browser_process_->profile_manager())
    176       << "ProfileManager already exists";
    177 
    178   // Set up the directory for profiles.
    179   ASSERT_TRUE(profiles_dir_.CreateUniqueTempDir());
    180 
    181   profile_manager_ = new testing::ProfileManager(profiles_dir_.path());
    182   browser_process_->SetProfileManager(profile_manager_);  // Takes ownership.
    183 
    184   called_set_up_ = true;
    185 }
    186