| 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/options/chromeos/storage_manager_handler.h" | 5 #include "chrome/browser/ui/webui/options/chromeos/storage_manager_handler.h" |
| 6 | 6 |
| 7 #include <sys/statvfs.h> |
| 8 |
| 9 #include "base/files/file_util.h" |
| 10 #include "base/posix/eintr_wrapper.h" |
| 11 #include "chrome/browser/browser_process.h" |
| 12 #include "chrome/browser/chromeos/file_manager/path_util.h" |
| 13 #include "chrome/browser/platform_util.h" |
| 14 #include "chrome/browser/profiles/profile.h" |
| 7 #include "chrome/grit/generated_resources.h" | 15 #include "chrome/grit/generated_resources.h" |
| 16 #include "content/public/browser/browser_thread.h" |
| 17 #include "ui/base/l10n/l10n_util.h" |
| 18 #include "ui/base/text/bytes_formatting.h" |
| 8 | 19 |
| 9 namespace chromeos { | 20 namespace chromeos { |
| 10 namespace options { | 21 namespace options { |
| 22 namespace { |
| 11 | 23 |
| 12 StorageManagerHandler::StorageManagerHandler() { | 24 void GetSizeStatOnBlockingPool(const std::string& mount_path, |
| 25 int64_t* total_size, |
| 26 int64_t* available_size) { |
| 27 struct statvfs stat = {}; |
| 28 if (HANDLE_EINTR(statvfs(mount_path.c_str(), &stat)) == 0) { |
| 29 *total_size = static_cast<int64_t>(stat.f_blocks) * stat.f_frsize; |
| 30 *available_size = static_cast<int64_t>(stat.f_bavail) * stat.f_frsize; |
| 31 } |
| 32 } |
| 33 |
| 34 } // namespace |
| 35 |
| 36 StorageManagerHandler::StorageManagerHandler() : weak_ptr_factory_(this) { |
| 13 } | 37 } |
| 14 | 38 |
| 15 StorageManagerHandler::~StorageManagerHandler() { | 39 StorageManagerHandler::~StorageManagerHandler() { |
| 16 } | 40 } |
| 17 | 41 |
| 18 void StorageManagerHandler::GetLocalizedValues( | 42 void StorageManagerHandler::GetLocalizedValues( |
| 19 base::DictionaryValue* localized_strings) { | 43 base::DictionaryValue* localized_strings) { |
| 20 DCHECK(localized_strings); | 44 DCHECK(localized_strings); |
| 21 | 45 |
| 22 RegisterTitle(localized_strings, "storageManagerPage", | 46 RegisterTitle(localized_strings, "storageManagerPage", |
| 23 IDS_OPTIONS_SETTINGS_STORAGE_MANAGER_TAB_TITLE); | 47 IDS_OPTIONS_SETTINGS_STORAGE_MANAGER_TAB_TITLE); |
| 48 |
| 49 localized_strings->SetString( |
| 50 "storageItemLabelCapacity", l10n_util::GetStringUTF16( |
| 51 IDS_OPTIONS_SETTINGS_STORAGE_ITEM_LABEL_CAPACITY)); |
| 52 localized_strings->SetString( |
| 53 "storageItemLabelInUse", l10n_util::GetStringUTF16( |
| 54 IDS_OPTIONS_SETTINGS_STORAGE_ITEM_LABEL_IN_USE)); |
| 55 localized_strings->SetString( |
| 56 "storageItemLabelAvailable", l10n_util::GetStringUTF16( |
| 57 IDS_OPTIONS_SETTINGS_STORAGE_ITEM_LABEL_AVAILABLE)); |
| 58 localized_strings->SetString( |
| 59 "storageSubitemLabelDownloads", l10n_util::GetStringUTF16( |
| 60 IDS_OPTIONS_SETTINGS_STORAGE_SUBITEM_LABEL_DOWNLOADS)); |
| 24 } | 61 } |
| 25 | 62 |
| 26 void StorageManagerHandler::InitializePage() { | 63 void StorageManagerHandler::InitializePage() { |
| 27 DCHECK(web_ui()); | 64 DCHECK(web_ui()); |
| 28 } | 65 } |
| 29 | 66 |
| 30 void StorageManagerHandler::RegisterMessages() { | 67 void StorageManagerHandler::RegisterMessages() { |
| 31 DCHECK(web_ui()); | 68 DCHECK(web_ui()); |
| 69 |
| 70 web_ui()->RegisterMessageCallback( |
| 71 "updateStorageInfo", |
| 72 base::Bind(&StorageManagerHandler::HandleUpdateStorageInfo, |
| 73 base::Unretained(this))); |
| 74 web_ui()->RegisterMessageCallback( |
| 75 "openDownloads", |
| 76 base::Bind(&StorageManagerHandler::HandleOpenDownloads, |
| 77 base::Unretained(this))); |
| 78 } |
| 79 |
| 80 void StorageManagerHandler::HandleUpdateStorageInfo( |
| 81 const base::ListValue* unused_args) { |
| 82 UpdateSizeStat(); |
| 83 UpdateDownloadsSize(); |
| 84 } |
| 85 |
| 86 void StorageManagerHandler::HandleOpenDownloads( |
| 87 const base::ListValue* unused_args) { |
| 88 Profile* const profile = Profile::FromWebUI(web_ui()); |
| 89 const base::FilePath downloads_path = |
| 90 file_manager::util::GetDownloadsFolderForProfile(profile); |
| 91 platform_util::OpenItem( |
| 92 profile, |
| 93 downloads_path, |
| 94 platform_util::OPEN_FOLDER, |
| 95 platform_util::OpenOperationCallback()); |
| 96 } |
| 97 |
| 98 void StorageManagerHandler::UpdateSizeStat() { |
| 99 Profile* const profile = Profile::FromWebUI(web_ui()); |
| 100 const base::FilePath downloads_path = |
| 101 file_manager::util::GetDownloadsFolderForProfile(profile); |
| 102 |
| 103 int64_t* total_size = new int64_t(0); |
| 104 int64_t* available_size = new int64_t(0); |
| 105 content::BrowserThread::PostBlockingPoolTaskAndReply( |
| 106 FROM_HERE, |
| 107 base::Bind(&GetSizeStatOnBlockingPool, |
| 108 downloads_path.value(), |
| 109 total_size, |
| 110 available_size), |
| 111 base::Bind(&StorageManagerHandler::OnGetSizeStat, |
| 112 weak_ptr_factory_.GetWeakPtr(), |
| 113 base::Owned(total_size), |
| 114 base::Owned(available_size))); |
| 115 } |
| 116 |
| 117 void StorageManagerHandler::OnGetSizeStat(int64_t* total_size, |
| 118 int64_t* available_size) { |
| 119 int64_t used_size = *total_size - *available_size; |
| 120 base::DictionaryValue size_stat; |
| 121 size_stat.SetString("totalSize", ui::FormatBytes(*total_size)); |
| 122 size_stat.SetString("availableSize", ui::FormatBytes(*available_size)); |
| 123 size_stat.SetString("usedSize", ui::FormatBytes(used_size)); |
| 124 size_stat.SetDouble("usedRatio", |
| 125 static_cast<double>(used_size) / *total_size); |
| 126 web_ui()->CallJavascriptFunctionUnsafe( |
| 127 "options.StorageManager.setSizeStat", size_stat); |
| 128 } |
| 129 |
| 130 void StorageManagerHandler::UpdateDownloadsSize() { |
| 131 Profile* const profile = Profile::FromWebUI(web_ui()); |
| 132 const base::FilePath downloads_path = |
| 133 file_manager::util::GetDownloadsFolderForProfile(profile); |
| 134 |
| 135 base::PostTaskAndReplyWithResult( |
| 136 content::BrowserThread::GetBlockingPool(), |
| 137 FROM_HERE, |
| 138 base::Bind(&base::ComputeDirectorySize, downloads_path), |
| 139 base::Bind(&StorageManagerHandler::OnGetDownloadsSize, |
| 140 weak_ptr_factory_.GetWeakPtr())); |
| 141 } |
| 142 |
| 143 void StorageManagerHandler::OnGetDownloadsSize(int64_t size) { |
| 144 web_ui()->CallJavascriptFunctionUnsafe( |
| 145 "options.StorageManager.setDownloadsSize", |
| 146 base::StringValue(ui::FormatBytes(size))); |
| 32 } | 147 } |
| 33 | 148 |
| 34 } // namespace options | 149 } // namespace options |
| 35 } // namespace chromeos | 150 } // namespace chromeos |
| OLD | NEW |