OLD | NEW |
(Empty) | |
| 1 // Copyright 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 "ios/chrome/browser/browser_state/chrome_browser_state_manager_impl.h" |
| 6 |
| 7 #include <stdint.h> |
| 8 #include <utility> |
| 9 |
| 10 #include "base/files/file_enumerator.h" |
| 11 #include "base/files/file_path.h" |
| 12 #include "base/metrics/histogram_macros.h" |
| 13 #include "base/path_service.h" |
| 14 #include "base/strings/utf_string_conversions.h" |
| 15 #include "components/browser_sync/browser/profile_sync_service.h" |
| 16 #include "components/invalidation/impl/profile_invalidation_provider.h" |
| 17 #include "components/password_manager/core/browser/password_store.h" |
| 18 #include "components/password_manager/sync/browser/password_manager_setting_migr
ator_service.h" |
| 19 #include "components/prefs/pref_service.h" |
| 20 #include "components/signin/core/browser/account_fetcher_service.h" |
| 21 #include "components/signin/core/browser/account_tracker_service.h" |
| 22 #include "components/signin/core/browser/gaia_cookie_manager_service.h" |
| 23 #include "components/signin/core/browser/signin_manager.h" |
| 24 #include "ios/chrome/browser/application_context.h" |
| 25 #include "ios/chrome/browser/browser_state/browser_state_info_cache.h" |
| 26 #include "ios/chrome/browser/browser_state/chrome_browser_state_impl.h" |
| 27 #include "ios/chrome/browser/browser_state/off_the_record_chrome_browser_state_i
mpl.h" |
| 28 #include "ios/chrome/browser/browser_state_metrics/browser_state_metrics.h" |
| 29 #include "ios/chrome/browser/chrome_constants.h" |
| 30 #include "ios/chrome/browser/chrome_paths.h" |
| 31 #include "ios/chrome/browser/data_reduction_proxy/ios_chrome_data_reduction_prox
y_settings.h" |
| 32 #include "ios/chrome/browser/data_reduction_proxy/ios_chrome_data_reduction_prox
y_settings_factory.h" |
| 33 #include "ios/chrome/browser/invalidation/ios_chrome_profile_invalidation_provid
er_factory.h" |
| 34 #include "ios/chrome/browser/passwords/ios_chrome_password_manager_setting_migra
tor_service_factory.h" |
| 35 #include "ios/chrome/browser/pref_names.h" |
| 36 #include "ios/chrome/browser/signin/account_consistency_service_factory.h" |
| 37 #include "ios/chrome/browser/signin/account_fetcher_service_factory.h" |
| 38 #include "ios/chrome/browser/signin/account_reconcilor_factory.h" |
| 39 #include "ios/chrome/browser/signin/account_tracker_service_factory.h" |
| 40 #include "ios/chrome/browser/signin/gaia_cookie_manager_service_factory.h" |
| 41 #include "ios/chrome/browser/signin/signin_manager_factory.h" |
| 42 #include "ios/chrome/browser/sync/ios_chrome_profile_sync_service_factory.h" |
| 43 #include "ios/web/public/active_state_manager.h" |
| 44 #include "ios/web/public/web_thread.h" |
| 45 |
| 46 namespace { |
| 47 |
| 48 int64_t ComputeFilesSize(const base::FilePath& directory, |
| 49 const base::FilePath::StringType& pattern) { |
| 50 int64_t running_size = 0; |
| 51 base::FileEnumerator iter(directory, false, base::FileEnumerator::FILES, |
| 52 pattern); |
| 53 while (!iter.Next().empty()) |
| 54 running_size += iter.GetInfo().GetSize(); |
| 55 return running_size; |
| 56 } |
| 57 |
| 58 // Simple task to log the size of the browser state at |path|. |
| 59 void BrowserStateSizeTask(const base::FilePath& path) { |
| 60 DCHECK_CURRENTLY_ON_WEB_THREAD(web::WebThread::FILE); |
| 61 const int64_t kBytesInOneMB = 1024 * 1024; |
| 62 |
| 63 int64_t size = ComputeFilesSize(path, FILE_PATH_LITERAL("*")); |
| 64 int size_MB = static_cast<int>(size / kBytesInOneMB); |
| 65 UMA_HISTOGRAM_COUNTS_10000("Profile.TotalSize", size_MB); |
| 66 |
| 67 size = ComputeFilesSize(path, FILE_PATH_LITERAL("History")); |
| 68 size_MB = static_cast<int>(size / kBytesInOneMB); |
| 69 UMA_HISTOGRAM_COUNTS_10000("Profile.HistorySize", size_MB); |
| 70 |
| 71 size = ComputeFilesSize(path, FILE_PATH_LITERAL("History*")); |
| 72 size_MB = static_cast<int>(size / kBytesInOneMB); |
| 73 UMA_HISTOGRAM_COUNTS_10000("Profile.TotalHistorySize", size_MB); |
| 74 |
| 75 size = ComputeFilesSize(path, FILE_PATH_LITERAL("Cookies")); |
| 76 size_MB = static_cast<int>(size / kBytesInOneMB); |
| 77 UMA_HISTOGRAM_COUNTS_10000("Profile.CookiesSize", size_MB); |
| 78 |
| 79 size = ComputeFilesSize(path, FILE_PATH_LITERAL("Bookmarks")); |
| 80 size_MB = static_cast<int>(size / kBytesInOneMB); |
| 81 UMA_HISTOGRAM_COUNTS_10000("Profile.BookmarksSize", size_MB); |
| 82 |
| 83 size = ComputeFilesSize(path, FILE_PATH_LITERAL("Favicons")); |
| 84 size_MB = static_cast<int>(size / kBytesInOneMB); |
| 85 UMA_HISTOGRAM_COUNTS_10000("Profile.FaviconsSize", size_MB); |
| 86 |
| 87 size = ComputeFilesSize(path, FILE_PATH_LITERAL("Top Sites")); |
| 88 size_MB = static_cast<int>(size / kBytesInOneMB); |
| 89 UMA_HISTOGRAM_COUNTS_10000("Profile.TopSitesSize", size_MB); |
| 90 |
| 91 size = ComputeFilesSize(path, FILE_PATH_LITERAL("Visited Links")); |
| 92 size_MB = static_cast<int>(size / kBytesInOneMB); |
| 93 UMA_HISTOGRAM_COUNTS_10000("Profile.VisitedLinksSize", size_MB); |
| 94 |
| 95 size = ComputeFilesSize(path, FILE_PATH_LITERAL("Web Data")); |
| 96 size_MB = static_cast<int>(size / kBytesInOneMB); |
| 97 UMA_HISTOGRAM_COUNTS_10000("Profile.WebDataSize", size_MB); |
| 98 |
| 99 size = ComputeFilesSize(path, FILE_PATH_LITERAL("Extension*")); |
| 100 size_MB = static_cast<int>(size / kBytesInOneMB); |
| 101 UMA_HISTOGRAM_COUNTS_10000("Profile.ExtensionSize", size_MB); |
| 102 } |
| 103 |
| 104 // Gets the user data directory. |
| 105 base::FilePath GetUserDataDir() { |
| 106 base::FilePath user_data_dir; |
| 107 bool result = PathService::Get(ios::DIR_USER_DATA, &user_data_dir); |
| 108 DCHECK(result); |
| 109 return user_data_dir; |
| 110 } |
| 111 |
| 112 } // namespace |
| 113 |
| 114 ChromeBrowserStateManagerImpl::ChromeBrowserStateManagerImpl() {} |
| 115 |
| 116 ChromeBrowserStateManagerImpl::~ChromeBrowserStateManagerImpl() { |
| 117 for (const auto& pair : browser_states_) { |
| 118 ChromeBrowserStateImpl* browser_state = pair.second.get(); |
| 119 web::BrowserState::GetActiveStateManager(browser_state)->SetActive(false); |
| 120 if (!browser_state->HasOffTheRecordChromeBrowserState()) |
| 121 continue; |
| 122 |
| 123 web::BrowserState* otr_browser_state = |
| 124 browser_state->GetOffTheRecordChromeBrowserState(); |
| 125 if (!web::BrowserState::HasActiveStateManager(otr_browser_state)) |
| 126 continue; |
| 127 web::BrowserState::GetActiveStateManager(otr_browser_state) |
| 128 ->SetActive(false); |
| 129 } |
| 130 } |
| 131 |
| 132 ios::ChromeBrowserState* |
| 133 ChromeBrowserStateManagerImpl::GetLastUsedBrowserState() { |
| 134 return GetBrowserState(GetLastUsedBrowserStateDir(GetUserDataDir())); |
| 135 } |
| 136 |
| 137 ios::ChromeBrowserState* ChromeBrowserStateManagerImpl::GetBrowserState( |
| 138 const base::FilePath& path) { |
| 139 // If the browser state is already loaded, just return it. |
| 140 auto iter = browser_states_.find(path); |
| 141 if (iter != browser_states_.end()) { |
| 142 DCHECK(iter->second.get()); |
| 143 return iter->second.get(); |
| 144 } |
| 145 |
| 146 scoped_ptr<ChromeBrowserStateImpl> browser_state_impl( |
| 147 new ChromeBrowserStateImpl(path)); |
| 148 DCHECK(!browser_state_impl->IsOffTheRecord()); |
| 149 |
| 150 std::pair<ChromeBrowserStateImplPathMap::iterator, bool> insert_result = |
| 151 browser_states_.insert( |
| 152 std::make_pair(path, std::move(browser_state_impl))); |
| 153 DCHECK(insert_result.second); |
| 154 DCHECK(insert_result.first != browser_states_.end()); |
| 155 |
| 156 DoFinalInit(insert_result.first->second.get()); |
| 157 return insert_result.first->second.get(); |
| 158 } |
| 159 |
| 160 base::FilePath ChromeBrowserStateManagerImpl::GetLastUsedBrowserStateDir( |
| 161 const base::FilePath& user_data_dir) { |
| 162 PrefService* local_state = GetApplicationContext()->GetLocalState(); |
| 163 DCHECK(local_state); |
| 164 std::string last_used_browser_state_name = |
| 165 local_state->GetString(prefs::kBrowserStateLastUsed); |
| 166 if (last_used_browser_state_name.empty()) |
| 167 last_used_browser_state_name = kIOSChromeInitialBrowserState; |
| 168 return user_data_dir.AppendASCII(last_used_browser_state_name); |
| 169 } |
| 170 |
| 171 BrowserStateInfoCache* |
| 172 ChromeBrowserStateManagerImpl::GetBrowserStateInfoCache() { |
| 173 if (!browser_state_info_cache_) { |
| 174 browser_state_info_cache_.reset(new BrowserStateInfoCache( |
| 175 GetApplicationContext()->GetLocalState(), GetUserDataDir())); |
| 176 } |
| 177 return browser_state_info_cache_.get(); |
| 178 } |
| 179 |
| 180 std::vector<ios::ChromeBrowserState*> |
| 181 ChromeBrowserStateManagerImpl::GetLoadedBrowserStates() { |
| 182 std::vector<ios::ChromeBrowserState*> loaded_browser_states; |
| 183 for (const auto& pair : browser_states_) |
| 184 loaded_browser_states.push_back(pair.second.get()); |
| 185 return loaded_browser_states; |
| 186 } |
| 187 |
| 188 void ChromeBrowserStateManagerImpl::DoFinalInit( |
| 189 ios::ChromeBrowserState* browser_state) { |
| 190 DoFinalInitForServices(browser_state); |
| 191 AddBrowserStateToCache(browser_state); |
| 192 |
| 193 // Log the browser state size after a reasonable startup delay. |
| 194 base::FilePath path = |
| 195 browser_state->GetOriginalChromeBrowserState()->GetStatePath(); |
| 196 web::WebThread::PostDelayedTask(web::WebThread::FILE, FROM_HERE, |
| 197 base::Bind(&BrowserStateSizeTask, path), |
| 198 base::TimeDelta::FromSeconds(112)); |
| 199 |
| 200 LogNumberOfBrowserStates( |
| 201 GetApplicationContext()->GetChromeBrowserStateManager()); |
| 202 } |
| 203 |
| 204 void ChromeBrowserStateManagerImpl::DoFinalInitForServices( |
| 205 ios::ChromeBrowserState* browser_state) { |
| 206 // Activate data reduction proxy. This creates a request context and makes a |
| 207 // URL request to check if the data reduction proxy server is reachable. |
| 208 IOSChromeDataReductionProxySettingsFactory::GetForBrowserState(browser_state) |
| 209 ->MaybeActivateDataReductionProxy(true); |
| 210 ios::GaiaCookieManagerServiceFactory::GetForBrowserState(browser_state) |
| 211 ->Init(); |
| 212 ios::AccountConsistencyServiceFactory::GetForBrowserState(browser_state); |
| 213 invalidation::ProfileInvalidationProvider* invalidation_provider = |
| 214 IOSChromeProfileInvalidationProviderFactory::GetForBrowserState( |
| 215 browser_state); |
| 216 invalidation::InvalidationService* invalidation_service = |
| 217 invalidation_provider ? invalidation_provider->GetInvalidationService() |
| 218 : nullptr; |
| 219 ios::AccountFetcherServiceFactory::GetForBrowserState(browser_state) |
| 220 ->SetupInvalidationsOnProfileLoad(invalidation_service); |
| 221 ios::AccountReconcilorFactory::GetForBrowserState(browser_state); |
| 222 |
| 223 // This service is responsible for migration of the legacy password manager |
| 224 // preference which controls behaviour of Chrome to the new preference which |
| 225 // controls password management behaviour on Chrome and Android. After |
| 226 // migration will be performed for all users it's planned to remove the |
| 227 // migration code, rough time estimates are Q1 2016. |
| 228 IOSChromePasswordManagerSettingMigratorServiceFactory::GetForBrowserState( |
| 229 browser_state) |
| 230 ->InitializeMigration( |
| 231 IOSChromeProfileSyncServiceFactory::GetForBrowserState( |
| 232 browser_state)); |
| 233 } |
| 234 |
| 235 void ChromeBrowserStateManagerImpl::AddBrowserStateToCache( |
| 236 ios::ChromeBrowserState* browser_state) { |
| 237 DCHECK(!browser_state->IsOffTheRecord()); |
| 238 BrowserStateInfoCache* cache = GetBrowserStateInfoCache(); |
| 239 if (browser_state->GetStatePath().DirName() != cache->GetUserDataDir()) |
| 240 return; |
| 241 |
| 242 SigninManagerBase* signin_manager = |
| 243 ios::SigninManagerFactory::GetForBrowserState(browser_state); |
| 244 AccountTrackerService* account_tracker = |
| 245 ios::AccountTrackerServiceFactory::GetForBrowserState(browser_state); |
| 246 AccountInfo account_info = account_tracker->GetAccountInfo( |
| 247 signin_manager->GetAuthenticatedAccountId()); |
| 248 base::string16 username = base::UTF8ToUTF16(account_info.email); |
| 249 |
| 250 size_t browser_state_index = |
| 251 cache->GetIndexOfBrowserStateWithPath(browser_state->GetStatePath()); |
| 252 if (browser_state_index != std::string::npos) { |
| 253 // The BrowserStateInfoCache's info must match the Signin Manager. |
| 254 cache->SetAuthInfoOfBrowserStateAtIndex(browser_state_index, |
| 255 account_info.gaia, username); |
| 256 return; |
| 257 } |
| 258 cache->AddBrowserState(browser_state->GetStatePath(), account_info.gaia, |
| 259 username); |
| 260 } |
OLD | NEW |