OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/webui/settings/chromeos/device_storage_handler.h" | 5 #include "chrome/browser/ui/webui/settings/chromeos/device_storage_handler.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <numeric> | 8 #include <numeric> |
9 #include <string> | 9 #include <string> |
10 | 10 |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 size_stat.SetDouble("usedRatio", | 160 size_stat.SetDouble("usedRatio", |
161 static_cast<double>(used_size) / *total_size); | 161 static_cast<double>(used_size) / *total_size); |
162 int storage_space_state = STORAGE_SPACE_NORMAL; | 162 int storage_space_state = STORAGE_SPACE_NORMAL; |
163 if (*available_size < kSpaceCriticallyLowBytes) | 163 if (*available_size < kSpaceCriticallyLowBytes) |
164 storage_space_state = STORAGE_SPACE_CRITICALLY_LOW; | 164 storage_space_state = STORAGE_SPACE_CRITICALLY_LOW; |
165 else if (*available_size < kSpaceLowBytes) | 165 else if (*available_size < kSpaceLowBytes) |
166 storage_space_state = STORAGE_SPACE_LOW; | 166 storage_space_state = STORAGE_SPACE_LOW; |
167 size_stat.SetInteger("spaceState", storage_space_state); | 167 size_stat.SetInteger("spaceState", storage_space_state); |
168 | 168 |
169 CallJavascriptFunction("cr.webUIListenerCallback", | 169 CallJavascriptFunction("cr.webUIListenerCallback", |
170 base::StringValue("storage-size-stat-changed"), | 170 base::Value("storage-size-stat-changed"), size_stat); |
171 size_stat); | |
172 } | 171 } |
173 | 172 |
174 void StorageHandler::UpdateDownloadsSize() { | 173 void StorageHandler::UpdateDownloadsSize() { |
175 if (updating_downloads_size_) | 174 if (updating_downloads_size_) |
176 return; | 175 return; |
177 updating_downloads_size_ = true; | 176 updating_downloads_size_ = true; |
178 | 177 |
179 Profile* const profile = Profile::FromWebUI(web_ui()); | 178 Profile* const profile = Profile::FromWebUI(web_ui()); |
180 const base::FilePath downloads_path = | 179 const base::FilePath downloads_path = |
181 file_manager::util::GetDownloadsFolderForProfile(profile); | 180 file_manager::util::GetDownloadsFolderForProfile(profile); |
182 | 181 |
183 base::PostTaskWithTraitsAndReplyWithResult( | 182 base::PostTaskWithTraitsAndReplyWithResult( |
184 FROM_HERE, base::TaskTraits().MayBlock().WithPriority( | 183 FROM_HERE, base::TaskTraits().MayBlock().WithPriority( |
185 base::TaskPriority::BACKGROUND), | 184 base::TaskPriority::BACKGROUND), |
186 base::Bind(&base::ComputeDirectorySize, downloads_path), | 185 base::Bind(&base::ComputeDirectorySize, downloads_path), |
187 base::Bind(&StorageHandler::OnGetDownloadsSize, base::Unretained(this))); | 186 base::Bind(&StorageHandler::OnGetDownloadsSize, base::Unretained(this))); |
188 } | 187 } |
189 | 188 |
190 void StorageHandler::OnGetDownloadsSize(int64_t size) { | 189 void StorageHandler::OnGetDownloadsSize(int64_t size) { |
191 updating_downloads_size_ = false; | 190 updating_downloads_size_ = false; |
192 CallJavascriptFunction("cr.webUIListenerCallback", | 191 CallJavascriptFunction("cr.webUIListenerCallback", |
193 base::StringValue("storage-downloads-size-changed"), | 192 base::Value("storage-downloads-size-changed"), |
194 base::StringValue(ui::FormatBytes(size))); | 193 base::Value(ui::FormatBytes(size))); |
195 } | 194 } |
196 | 195 |
197 void StorageHandler::UpdateDriveCacheSize() { | 196 void StorageHandler::UpdateDriveCacheSize() { |
198 if (updating_drive_cache_size_) | 197 if (updating_drive_cache_size_) |
199 return; | 198 return; |
200 | 199 |
201 drive::FileSystemInterface* const file_system = | 200 drive::FileSystemInterface* const file_system = |
202 drive::util::GetFileSystemByProfile(Profile::FromWebUI(web_ui())); | 201 drive::util::GetFileSystemByProfile(Profile::FromWebUI(web_ui())); |
203 if (!file_system) | 202 if (!file_system) |
204 return; | 203 return; |
205 | 204 |
206 // Shows the item "Offline cache" and start calculating size. | 205 // Shows the item "Offline cache" and start calculating size. |
207 CallJavascriptFunction("cr.webUIListenerCallback", | 206 CallJavascriptFunction("cr.webUIListenerCallback", |
208 base::StringValue("storage-drive-enabled-changed"), | 207 base::Value("storage-drive-enabled-changed"), |
209 base::Value(true)); | 208 base::Value(true)); |
210 updating_drive_cache_size_ = true; | 209 updating_drive_cache_size_ = true; |
211 file_system->CalculateCacheSize( | 210 file_system->CalculateCacheSize( |
212 base::Bind(&StorageHandler::OnGetDriveCacheSize, base::Unretained(this))); | 211 base::Bind(&StorageHandler::OnGetDriveCacheSize, base::Unretained(this))); |
213 } | 212 } |
214 | 213 |
215 void StorageHandler::OnGetDriveCacheSize(int64_t size) { | 214 void StorageHandler::OnGetDriveCacheSize(int64_t size) { |
216 updating_drive_cache_size_ = false; | 215 updating_drive_cache_size_ = false; |
217 CallJavascriptFunction("cr.webUIListenerCallback", | 216 CallJavascriptFunction("cr.webUIListenerCallback", |
218 base::StringValue("storage-drive-cache-size-changed"), | 217 base::Value("storage-drive-cache-size-changed"), |
219 base::StringValue(ui::FormatBytes(size)), | 218 base::Value(ui::FormatBytes(size)), |
220 base::Value(size > 0)); | 219 base::Value(size > 0)); |
221 } | 220 } |
222 | 221 |
223 void StorageHandler::UpdateBrowsingDataSize() { | 222 void StorageHandler::UpdateBrowsingDataSize() { |
224 if (updating_browsing_data_size_) | 223 if (updating_browsing_data_size_) |
225 return; | 224 return; |
226 updating_browsing_data_size_ = true; | 225 updating_browsing_data_size_ = true; |
227 | 226 |
228 has_browser_cache_size_ = false; | 227 has_browser_cache_size_ = false; |
229 has_browser_site_data_size_ = false; | 228 has_browser_site_data_size_ = false; |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
278 if (has_browser_cache_size_ && has_browser_site_data_size_) { | 277 if (has_browser_cache_size_ && has_browser_site_data_size_) { |
279 base::string16 size_string; | 278 base::string16 size_string; |
280 if (browser_cache_size_ >= 0 && browser_site_data_size_ >= 0) { | 279 if (browser_cache_size_ >= 0 && browser_site_data_size_ >= 0) { |
281 size_string = ui::FormatBytes( | 280 size_string = ui::FormatBytes( |
282 browser_site_data_size_ + browser_cache_size_); | 281 browser_site_data_size_ + browser_cache_size_); |
283 } else { | 282 } else { |
284 size_string = l10n_util::GetStringUTF16( | 283 size_string = l10n_util::GetStringUTF16( |
285 IDS_OPTIONS_SETTINGS_STORAGE_SIZE_UNKNOWN); | 284 IDS_OPTIONS_SETTINGS_STORAGE_SIZE_UNKNOWN); |
286 } | 285 } |
287 updating_browsing_data_size_ = false; | 286 updating_browsing_data_size_ = false; |
288 CallJavascriptFunction( | 287 CallJavascriptFunction("cr.webUIListenerCallback", |
289 "cr.webUIListenerCallback", | 288 base::Value("storage-browsing-data-size-changed"), |
290 base::StringValue("storage-browsing-data-size-changed"), | 289 base::Value(size_string)); |
291 base::StringValue(size_string)); | |
292 } | 290 } |
293 } | 291 } |
294 | 292 |
295 void StorageHandler::UpdateOtherUsersSize() { | 293 void StorageHandler::UpdateOtherUsersSize() { |
296 if (updating_other_users_size_) | 294 if (updating_other_users_size_) |
297 return; | 295 return; |
298 updating_other_users_size_ = true; | 296 updating_other_users_size_ = true; |
299 | 297 |
300 other_users_.clear(); | 298 other_users_.clear(); |
301 user_sizes_.clear(); | 299 user_sizes_.clear(); |
302 const user_manager::UserList& users = | 300 const user_manager::UserList& users = |
303 user_manager::UserManager::Get()->GetUsers(); | 301 user_manager::UserManager::Get()->GetUsers(); |
304 for (auto* user : users) { | 302 for (auto* user : users) { |
305 if (user->is_active()) | 303 if (user->is_active()) |
306 continue; | 304 continue; |
307 other_users_.push_back(user); | 305 other_users_.push_back(user); |
308 cryptohome::HomedirMethods::GetInstance()->GetAccountDiskUsage( | 306 cryptohome::HomedirMethods::GetInstance()->GetAccountDiskUsage( |
309 cryptohome::Identification(user->GetAccountId()), | 307 cryptohome::Identification(user->GetAccountId()), |
310 base::Bind(&StorageHandler::OnGetOtherUserSize, | 308 base::Bind(&StorageHandler::OnGetOtherUserSize, |
311 base::Unretained(this))); | 309 base::Unretained(this))); |
312 } | 310 } |
313 // We should show "0 B" if there is no other user. | 311 // We should show "0 B" if there is no other user. |
314 if (other_users_.empty()) { | 312 if (other_users_.empty()) { |
315 updating_other_users_size_ = false; | 313 updating_other_users_size_ = false; |
316 CallJavascriptFunction( | 314 CallJavascriptFunction("cr.webUIListenerCallback", |
317 "cr.webUIListenerCallback", | 315 base::Value("storage-other-users-size-changed"), |
318 base::StringValue("storage-other-users-size-changed"), | 316 base::Value(ui::FormatBytes(0))); |
319 base::StringValue(ui::FormatBytes(0))); | |
320 } | 317 } |
321 } | 318 } |
322 | 319 |
323 void StorageHandler::OnGetOtherUserSize(bool success, int64_t size) { | 320 void StorageHandler::OnGetOtherUserSize(bool success, int64_t size) { |
324 user_sizes_.push_back(success ? size : -1); | 321 user_sizes_.push_back(success ? size : -1); |
325 if (user_sizes_.size() == other_users_.size()) { | 322 if (user_sizes_.size() == other_users_.size()) { |
326 base::string16 size_string; | 323 base::string16 size_string; |
327 // If all the requests succeed, shows the total bytes in the UI. | 324 // If all the requests succeed, shows the total bytes in the UI. |
328 if (std::count(user_sizes_.begin(), user_sizes_.end(), -1) == 0) { | 325 if (std::count(user_sizes_.begin(), user_sizes_.end(), -1) == 0) { |
329 size_string = ui::FormatBytes( | 326 size_string = ui::FormatBytes( |
330 std::accumulate(user_sizes_.begin(), user_sizes_.end(), 0LL)); | 327 std::accumulate(user_sizes_.begin(), user_sizes_.end(), 0LL)); |
331 } else { | 328 } else { |
332 size_string = l10n_util::GetStringUTF16( | 329 size_string = l10n_util::GetStringUTF16( |
333 IDS_OPTIONS_SETTINGS_STORAGE_SIZE_UNKNOWN); | 330 IDS_OPTIONS_SETTINGS_STORAGE_SIZE_UNKNOWN); |
334 } | 331 } |
335 updating_other_users_size_ = false; | 332 updating_other_users_size_ = false; |
336 CallJavascriptFunction( | 333 CallJavascriptFunction("cr.webUIListenerCallback", |
337 "cr.webUIListenerCallback", | 334 base::Value("storage-other-users-size-changed"), |
338 base::StringValue("storage-other-users-size-changed"), | 335 base::Value(size_string)); |
339 base::StringValue(size_string)); | |
340 } | 336 } |
341 } | 337 } |
342 | 338 |
343 void StorageHandler::UpdateAndroidSize() { | 339 void StorageHandler::UpdateAndroidSize() { |
344 if (updating_android_size_) | 340 if (updating_android_size_) |
345 return; | 341 return; |
346 updating_android_size_ = true; | 342 updating_android_size_ = true; |
347 | 343 |
348 Profile* const profile = Profile::FromWebUI(web_ui()); | 344 Profile* const profile = Profile::FromWebUI(web_ui()); |
349 if (!arc::IsArcPlayStoreEnabledForProfile(profile) || | 345 if (!arc::IsArcPlayStoreEnabledForProfile(profile) || |
350 arc::IsArcOptInVerificationDisabled()) { | 346 arc::IsArcOptInVerificationDisabled()) { |
351 return; | 347 return; |
352 } | 348 } |
353 | 349 |
354 // Shows the item "Android apps and cache" and start calculating size. | 350 // Shows the item "Android apps and cache" and start calculating size. |
355 CallJavascriptFunction("cr.webUIListenerCallback", | 351 CallJavascriptFunction("cr.webUIListenerCallback", |
356 base::StringValue("storage-android-enabled-changed"), | 352 base::Value("storage-android-enabled-changed"), |
357 base::Value(true)); | 353 base::Value(true)); |
358 bool success = arc::ArcStorageManager::Get()->GetApplicationsSize( | 354 bool success = arc::ArcStorageManager::Get()->GetApplicationsSize( |
359 base::Bind(&StorageHandler::OnGetAndroidSize, | 355 base::Bind(&StorageHandler::OnGetAndroidSize, |
360 base::Unretained(this))); | 356 base::Unretained(this))); |
361 if (!success) | 357 if (!success) |
362 updating_android_size_ = false; | 358 updating_android_size_ = false; |
363 } | 359 } |
364 | 360 |
365 void StorageHandler::OnGetAndroidSize(bool succeeded, | 361 void StorageHandler::OnGetAndroidSize(bool succeeded, |
366 arc::mojom::ApplicationsSizePtr size) { | 362 arc::mojom::ApplicationsSizePtr size) { |
367 base::string16 size_string; | 363 base::string16 size_string; |
368 if (succeeded) { | 364 if (succeeded) { |
369 uint64_t total_bytes = size->total_code_bytes + | 365 uint64_t total_bytes = size->total_code_bytes + |
370 size->total_data_bytes + | 366 size->total_data_bytes + |
371 size->total_cache_bytes; | 367 size->total_cache_bytes; |
372 size_string = ui::FormatBytes(total_bytes); | 368 size_string = ui::FormatBytes(total_bytes); |
373 } else { | 369 } else { |
374 size_string = l10n_util::GetStringUTF16( | 370 size_string = l10n_util::GetStringUTF16( |
375 IDS_OPTIONS_SETTINGS_STORAGE_SIZE_UNKNOWN); | 371 IDS_OPTIONS_SETTINGS_STORAGE_SIZE_UNKNOWN); |
376 } | 372 } |
377 updating_android_size_ = false; | 373 updating_android_size_ = false; |
378 CallJavascriptFunction("cr.webUIListenerCallback", | 374 CallJavascriptFunction("cr.webUIListenerCallback", |
379 base::StringValue("storage-android-size-changed"), | 375 base::Value("storage-android-size-changed"), |
380 base::StringValue(size_string)); | 376 base::Value(size_string)); |
381 } | 377 } |
382 | 378 |
383 void StorageHandler::OnClearDriveCacheDone(bool success) { | 379 void StorageHandler::OnClearDriveCacheDone(bool success) { |
384 UpdateDriveCacheSize(); | 380 UpdateDriveCacheSize(); |
385 } | 381 } |
386 | 382 |
387 } // namespace settings | 383 } // namespace settings |
388 } // namespace chromeos | 384 } // namespace chromeos |
OLD | NEW |