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 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 size_stat.SetString("usedSize", ui::FormatBytes(used_size)); | 159 size_stat.SetString("usedSize", ui::FormatBytes(used_size)); |
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 FireWebUIListener("storage-size-stat-changed", size_stat); |
170 base::Value("storage-size-stat-changed"), size_stat); | |
171 } | 170 } |
172 | 171 |
173 void StorageHandler::UpdateDownloadsSize() { | 172 void StorageHandler::UpdateDownloadsSize() { |
174 if (updating_downloads_size_) | 173 if (updating_downloads_size_) |
175 return; | 174 return; |
176 updating_downloads_size_ = true; | 175 updating_downloads_size_ = true; |
177 | 176 |
178 Profile* const profile = Profile::FromWebUI(web_ui()); | 177 Profile* const profile = Profile::FromWebUI(web_ui()); |
179 const base::FilePath downloads_path = | 178 const base::FilePath downloads_path = |
180 file_manager::util::GetDownloadsFolderForProfile(profile); | 179 file_manager::util::GetDownloadsFolderForProfile(profile); |
181 | 180 |
182 base::PostTaskWithTraitsAndReplyWithResult( | 181 base::PostTaskWithTraitsAndReplyWithResult( |
183 FROM_HERE, {base::MayBlock(), base::TaskPriority::BACKGROUND}, | 182 FROM_HERE, {base::MayBlock(), base::TaskPriority::BACKGROUND}, |
184 base::Bind(&base::ComputeDirectorySize, downloads_path), | 183 base::Bind(&base::ComputeDirectorySize, downloads_path), |
185 base::Bind(&StorageHandler::OnGetDownloadsSize, | 184 base::Bind(&StorageHandler::OnGetDownloadsSize, |
186 weak_ptr_factory_.GetWeakPtr())); | 185 weak_ptr_factory_.GetWeakPtr())); |
187 } | 186 } |
188 | 187 |
189 void StorageHandler::OnGetDownloadsSize(int64_t size) { | 188 void StorageHandler::OnGetDownloadsSize(int64_t size) { |
190 updating_downloads_size_ = false; | 189 updating_downloads_size_ = false; |
191 CallJavascriptFunction("cr.webUIListenerCallback", | 190 FireWebUIListener("storage-downloads-size-changed", |
192 base::Value("storage-downloads-size-changed"), | 191 base::Value(ui::FormatBytes(size))); |
193 base::Value(ui::FormatBytes(size))); | |
194 } | 192 } |
195 | 193 |
196 void StorageHandler::UpdateDriveCacheSize() { | 194 void StorageHandler::UpdateDriveCacheSize() { |
197 if (updating_drive_cache_size_) | 195 if (updating_drive_cache_size_) |
198 return; | 196 return; |
199 | 197 |
200 drive::FileSystemInterface* const file_system = | 198 drive::FileSystemInterface* const file_system = |
201 drive::util::GetFileSystemByProfile(Profile::FromWebUI(web_ui())); | 199 drive::util::GetFileSystemByProfile(Profile::FromWebUI(web_ui())); |
202 if (!file_system) | 200 if (!file_system) |
203 return; | 201 return; |
204 | 202 |
205 // Shows the item "Offline cache" and start calculating size. | 203 // Shows the item "Offline cache" and start calculating size. |
206 CallJavascriptFunction("cr.webUIListenerCallback", | 204 FireWebUIListener("storage-drive-enabled-changed", base::Value(true)); |
207 base::Value("storage-drive-enabled-changed"), | |
208 base::Value(true)); | |
209 updating_drive_cache_size_ = true; | 205 updating_drive_cache_size_ = true; |
210 file_system->CalculateCacheSize(base::Bind( | 206 file_system->CalculateCacheSize(base::Bind( |
211 &StorageHandler::OnGetDriveCacheSize, weak_ptr_factory_.GetWeakPtr())); | 207 &StorageHandler::OnGetDriveCacheSize, weak_ptr_factory_.GetWeakPtr())); |
212 } | 208 } |
213 | 209 |
214 void StorageHandler::OnGetDriveCacheSize(int64_t size) { | 210 void StorageHandler::OnGetDriveCacheSize(int64_t size) { |
215 updating_drive_cache_size_ = false; | 211 updating_drive_cache_size_ = false; |
216 CallJavascriptFunction("cr.webUIListenerCallback", | 212 FireWebUIListener("storage-drive-cache-size-changed", |
217 base::Value("storage-drive-cache-size-changed"), | 213 base::Value(ui::FormatBytes(size)), base::Value(size > 0)); |
218 base::Value(ui::FormatBytes(size)), | |
219 base::Value(size > 0)); | |
220 } | 214 } |
221 | 215 |
222 void StorageHandler::UpdateBrowsingDataSize() { | 216 void StorageHandler::UpdateBrowsingDataSize() { |
223 if (updating_browsing_data_size_) | 217 if (updating_browsing_data_size_) |
224 return; | 218 return; |
225 updating_browsing_data_size_ = true; | 219 updating_browsing_data_size_ = true; |
226 | 220 |
227 has_browser_cache_size_ = false; | 221 has_browser_cache_size_ = false; |
228 has_browser_site_data_size_ = false; | 222 has_browser_site_data_size_ = false; |
229 Profile* const profile = Profile::FromWebUI(web_ui()); | 223 Profile* const profile = Profile::FromWebUI(web_ui()); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 if (has_browser_cache_size_ && has_browser_site_data_size_) { | 271 if (has_browser_cache_size_ && has_browser_site_data_size_) { |
278 base::string16 size_string; | 272 base::string16 size_string; |
279 if (browser_cache_size_ >= 0 && browser_site_data_size_ >= 0) { | 273 if (browser_cache_size_ >= 0 && browser_site_data_size_ >= 0) { |
280 size_string = ui::FormatBytes( | 274 size_string = ui::FormatBytes( |
281 browser_site_data_size_ + browser_cache_size_); | 275 browser_site_data_size_ + browser_cache_size_); |
282 } else { | 276 } else { |
283 size_string = l10n_util::GetStringUTF16( | 277 size_string = l10n_util::GetStringUTF16( |
284 IDS_OPTIONS_SETTINGS_STORAGE_SIZE_UNKNOWN); | 278 IDS_OPTIONS_SETTINGS_STORAGE_SIZE_UNKNOWN); |
285 } | 279 } |
286 updating_browsing_data_size_ = false; | 280 updating_browsing_data_size_ = false; |
287 CallJavascriptFunction("cr.webUIListenerCallback", | 281 FireWebUIListener("storage-browsing-data-size-changed", |
288 base::Value("storage-browsing-data-size-changed"), | 282 base::Value(size_string)); |
289 base::Value(size_string)); | |
290 } | 283 } |
291 } | 284 } |
292 | 285 |
293 void StorageHandler::UpdateOtherUsersSize() { | 286 void StorageHandler::UpdateOtherUsersSize() { |
294 if (updating_other_users_size_) | 287 if (updating_other_users_size_) |
295 return; | 288 return; |
296 updating_other_users_size_ = true; | 289 updating_other_users_size_ = true; |
297 | 290 |
298 other_users_.clear(); | 291 other_users_.clear(); |
299 user_sizes_.clear(); | 292 user_sizes_.clear(); |
300 const user_manager::UserList& users = | 293 const user_manager::UserList& users = |
301 user_manager::UserManager::Get()->GetUsers(); | 294 user_manager::UserManager::Get()->GetUsers(); |
302 for (auto* user : users) { | 295 for (auto* user : users) { |
303 if (user->is_active()) | 296 if (user->is_active()) |
304 continue; | 297 continue; |
305 other_users_.push_back(user); | 298 other_users_.push_back(user); |
306 cryptohome::HomedirMethods::GetInstance()->GetAccountDiskUsage( | 299 cryptohome::HomedirMethods::GetInstance()->GetAccountDiskUsage( |
307 cryptohome::Identification(user->GetAccountId()), | 300 cryptohome::Identification(user->GetAccountId()), |
308 base::Bind(&StorageHandler::OnGetOtherUserSize, | 301 base::Bind(&StorageHandler::OnGetOtherUserSize, |
309 weak_ptr_factory_.GetWeakPtr())); | 302 weak_ptr_factory_.GetWeakPtr())); |
310 } | 303 } |
311 // We should show "0 B" if there is no other user. | 304 // We should show "0 B" if there is no other user. |
312 if (other_users_.empty()) { | 305 if (other_users_.empty()) { |
313 updating_other_users_size_ = false; | 306 updating_other_users_size_ = false; |
314 CallJavascriptFunction("cr.webUIListenerCallback", | 307 FireWebUIListener("storage-other-users-size-changed", |
315 base::Value("storage-other-users-size-changed"), | 308 base::Value(ui::FormatBytes(0))); |
316 base::Value(ui::FormatBytes(0))); | |
317 } | 309 } |
318 } | 310 } |
319 | 311 |
320 void StorageHandler::OnGetOtherUserSize(bool success, int64_t size) { | 312 void StorageHandler::OnGetOtherUserSize(bool success, int64_t size) { |
321 user_sizes_.push_back(success ? size : -1); | 313 user_sizes_.push_back(success ? size : -1); |
322 if (user_sizes_.size() == other_users_.size()) { | 314 if (user_sizes_.size() == other_users_.size()) { |
323 base::string16 size_string; | 315 base::string16 size_string; |
324 // If all the requests succeed, shows the total bytes in the UI. | 316 // If all the requests succeed, shows the total bytes in the UI. |
325 if (std::count(user_sizes_.begin(), user_sizes_.end(), -1) == 0) { | 317 if (std::count(user_sizes_.begin(), user_sizes_.end(), -1) == 0) { |
326 size_string = ui::FormatBytes( | 318 size_string = ui::FormatBytes( |
327 std::accumulate(user_sizes_.begin(), user_sizes_.end(), 0LL)); | 319 std::accumulate(user_sizes_.begin(), user_sizes_.end(), 0LL)); |
328 } else { | 320 } else { |
329 size_string = l10n_util::GetStringUTF16( | 321 size_string = l10n_util::GetStringUTF16( |
330 IDS_OPTIONS_SETTINGS_STORAGE_SIZE_UNKNOWN); | 322 IDS_OPTIONS_SETTINGS_STORAGE_SIZE_UNKNOWN); |
331 } | 323 } |
332 updating_other_users_size_ = false; | 324 updating_other_users_size_ = false; |
333 CallJavascriptFunction("cr.webUIListenerCallback", | 325 FireWebUIListener("storage-other-users-size-changed", |
334 base::Value("storage-other-users-size-changed"), | 326 base::Value(size_string)); |
335 base::Value(size_string)); | |
336 } | 327 } |
337 } | 328 } |
338 | 329 |
339 void StorageHandler::UpdateAndroidSize() { | 330 void StorageHandler::UpdateAndroidSize() { |
340 if (updating_android_size_) | 331 if (updating_android_size_) |
341 return; | 332 return; |
342 updating_android_size_ = true; | 333 updating_android_size_ = true; |
343 | 334 |
344 Profile* const profile = Profile::FromWebUI(web_ui()); | 335 Profile* const profile = Profile::FromWebUI(web_ui()); |
345 if (!arc::IsArcPlayStoreEnabledForProfile(profile) || | 336 if (!arc::IsArcPlayStoreEnabledForProfile(profile) || |
346 arc::IsArcOptInVerificationDisabled()) { | 337 arc::IsArcOptInVerificationDisabled()) { |
347 return; | 338 return; |
348 } | 339 } |
349 | 340 |
350 // Shows the item "Android apps and cache" and start calculating size. | 341 // Shows the item "Android apps and cache" and start calculating size. |
351 CallJavascriptFunction("cr.webUIListenerCallback", | 342 FireWebUIListener("storage-android-enabled-changed", base::Value(true)); |
352 base::Value("storage-android-enabled-changed"), | |
353 base::Value(true)); | |
354 bool success = arc::ArcStorageManager::Get()->GetApplicationsSize(base::Bind( | 343 bool success = arc::ArcStorageManager::Get()->GetApplicationsSize(base::Bind( |
355 &StorageHandler::OnGetAndroidSize, weak_ptr_factory_.GetWeakPtr())); | 344 &StorageHandler::OnGetAndroidSize, weak_ptr_factory_.GetWeakPtr())); |
356 if (!success) | 345 if (!success) |
357 updating_android_size_ = false; | 346 updating_android_size_ = false; |
358 } | 347 } |
359 | 348 |
360 void StorageHandler::OnGetAndroidSize(bool succeeded, | 349 void StorageHandler::OnGetAndroidSize(bool succeeded, |
361 arc::mojom::ApplicationsSizePtr size) { | 350 arc::mojom::ApplicationsSizePtr size) { |
362 base::string16 size_string; | 351 base::string16 size_string; |
363 if (succeeded) { | 352 if (succeeded) { |
364 uint64_t total_bytes = size->total_code_bytes + | 353 uint64_t total_bytes = size->total_code_bytes + |
365 size->total_data_bytes + | 354 size->total_data_bytes + |
366 size->total_cache_bytes; | 355 size->total_cache_bytes; |
367 size_string = ui::FormatBytes(total_bytes); | 356 size_string = ui::FormatBytes(total_bytes); |
368 } else { | 357 } else { |
369 size_string = l10n_util::GetStringUTF16( | 358 size_string = l10n_util::GetStringUTF16( |
370 IDS_OPTIONS_SETTINGS_STORAGE_SIZE_UNKNOWN); | 359 IDS_OPTIONS_SETTINGS_STORAGE_SIZE_UNKNOWN); |
371 } | 360 } |
372 updating_android_size_ = false; | 361 updating_android_size_ = false; |
373 CallJavascriptFunction("cr.webUIListenerCallback", | 362 FireWebUIListener("storage-android-size-changed", base::Value(size_string)); |
374 base::Value("storage-android-size-changed"), | |
375 base::Value(size_string)); | |
376 } | 363 } |
377 | 364 |
378 void StorageHandler::OnClearDriveCacheDone(bool success) { | 365 void StorageHandler::OnClearDriveCacheDone(bool success) { |
379 UpdateDriveCacheSize(); | 366 UpdateDriveCacheSize(); |
380 } | 367 } |
381 | 368 |
382 } // namespace settings | 369 } // namespace settings |
383 } // namespace chromeos | 370 } // namespace chromeos |
OLD | NEW |