Commit 1717bcb7 authored by fukino's avatar fukino Committed by Commit bot

Storage manager: Show the disk usage of other users.

This CL adds an item "Other users" on the storage manager, which shows the total
disk size used by other users.
Cryptohome API was added by https://chromium-review.googlesource.com/#/c/356641

BUG=591958
TEST=manually tested on link/minnie
CQ_INCLUDE_TRYBOTS=tryserver.chromium.linux:closure_compilation

Review-Url: https://codereview.chromium.org/2111043003
Cr-Commit-Position: refs/heads/master@{#403393}
parent ac799c2f
......@@ -2704,6 +2704,9 @@ Press any key to continue exploring.
<message name="IDS_OPTIONS_SETTINGS_STORAGE_SUBITEM_LABEL_BROWSING_DATA" desc="In storage manager overlay, label for the size of browsing data.">
Browsing data
</message>
<message name="IDS_OPTIONS_SETTINGS_STORAGE_SUBITEM_LABEL_OTHER_USERS" desc="In storage manager overlay, label for the total size of other users' data directories.">
Other users
</message>
<message name="IDS_OPTIONS_SETTINGS_STORAGE_SUBITEM_LABEL_ARC" desc="In storage manager overlay, label for the total size size of Android apps and cache.">
Android storage
</message>
......
......@@ -69,6 +69,14 @@
class="storage-manager-item-size"
i18n-content="storageSizeCalculating"></span>
</div>
<div class="storage-manager-subitem">
<a is="action-link" disabled
id="storage-manager-label-other-users"
i18n-content="storageSubitemLabelOtherUsers"></a>
<span id="storage-manager-size-other-users"
class="storage-manager-item-size"
i18n-content="storageSizeCalculating"></span>
</div>
</div>
<div id="storage-manager-section-available" class="storage-manager-section">
<div class="storage-manager-item">
......
......@@ -110,13 +110,22 @@ cr.define('options', function() {
/**
* Updates the size of browsing data.
* @param {string} size Formatted string of the size of Downloads.
* @param {string} size Formatted string of the size of browsing data.
* @private
*/
setBrowsingDataSize_: function(size) {
$('storage-manager-size-browsing-data').textContent = size;
},
/**
* Updates the size of other users.
* @param {string} size Formatted string of the size of other users.
* @private
*/
setOtherUsersSize_: function(size) {
$('storage-manager-size-other-users').textContent = size;
},
/**
* Updates the total size of Android apps and cache.
* @param {string} size Formatted string of the size of Android apps and
......@@ -143,6 +152,7 @@ cr.define('options', function() {
'setBrowsingDataSize',
'setDownloadsSize',
'setDriveCacheSize',
'setOtherUsersSize',
'setSizeStat',
'showArcItem',
]);
......
......@@ -4,6 +4,8 @@
#include "chrome/browser/ui/webui/options/chromeos/storage_manager_handler.h"
#include <algorithm>
#include <numeric>
#include <string>
#include "base/files/file_util.h"
......@@ -15,8 +17,10 @@
#include "chrome/browser/platform_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/grit/generated_resources.h"
#include "chromeos/cryptohome/homedir_methods.h"
#include "components/browsing_data/storage_partition_http_cache_data_remover.h"
#include "components/drive/chromeos/file_system_interface.h"
#include "components/user_manager/user_manager.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
#include "ui/base/l10n/l10n_util.h"
......@@ -81,6 +85,9 @@ void StorageManagerHandler::GetLocalizedValues(
localized_strings->SetString(
"storageSubitemLabelBrowsingData", l10n_util::GetStringUTF16(
IDS_OPTIONS_SETTINGS_STORAGE_SUBITEM_LABEL_BROWSING_DATA));
localized_strings->SetString(
"storageSubitemLabelOtherUsers", l10n_util::GetStringUTF16(
IDS_OPTIONS_SETTINGS_STORAGE_SUBITEM_LABEL_OTHER_USERS));
localized_strings->SetString(
"storageSubitemLabelArc", l10n_util::GetStringUTF16(
IDS_OPTIONS_SETTINGS_STORAGE_SUBITEM_LABEL_ARC));
......@@ -147,6 +154,7 @@ void StorageManagerHandler::HandleUpdateStorageInfo(
UpdateDownloadsSize();
UpdateDriveCacheSize();
UpdateBrowsingDataSize();
UpdateOtherUsersSize();
UpdateArcSize();
}
......@@ -276,6 +284,37 @@ void StorageManagerHandler::OnGetBrowsingDataSize(bool is_site_data,
browser_cache_size_ + browser_site_data_size_))));
}
void StorageManagerHandler::UpdateOtherUsersSize() {
other_users_.clear();
user_sizes_.clear();
const user_manager::UserList& users =
user_manager::UserManager::Get()->GetUsers();
for (const auto& user : users) {
if (user->is_active())
continue;
other_users_.push_back(user);
cryptohome::HomedirMethods::GetInstance()->GetAccountDiskUsage(
cryptohome::Identification(user->GetAccountId()),
base::Bind(&StorageManagerHandler::OnGetOtherUserSize,
weak_ptr_factory_.GetWeakPtr()));
}
}
void StorageManagerHandler::OnGetOtherUserSize(bool success, int64_t size) {
user_sizes_.push_back(success ? size : -1);
if (user_sizes_.size() == other_users_.size()) {
base::StringValue other_users_size(l10n_util::GetStringUTF16(
IDS_OPTIONS_SETTINGS_STORAGE_SIZE_UNKNOWN));
// If all the requests succeed, shows the total bytes in the UI.
if (std::count(user_sizes_.begin(), user_sizes_.end(), -1) == 0) {
other_users_size = base::StringValue(ui::FormatBytes(
std::accumulate(user_sizes_.begin(), user_sizes_.end(), 0LL)));
}
web_ui()->CallJavascriptFunctionUnsafe(
"options.StorageManager.setOtherUsersSize", other_users_size);
}
}
void StorageManagerHandler::UpdateArcSize() {
Profile* const profile = Profile::FromWebUI(web_ui());
if (!arc::ArcAuthService::IsAllowedForProfile(profile) ||
......
......@@ -5,10 +5,15 @@
#ifndef CHROME_BROWSER_UI_WEBUI_OPTIONS_CHROMEOS_STORAGE_MANAGER_HANDLER_H_
#define CHROME_BROWSER_UI_WEBUI_OPTIONS_CHROMEOS_STORAGE_MANAGER_HANDLER_H_
#include <stdint.h>
#include <vector>
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/ui/webui/options/options_ui.h"
#include "components/arc/storage_manager/arc_storage_manager.h"
#include "components/user_manager/user.h"
namespace chromeos {
namespace options {
......@@ -65,6 +70,12 @@ class StorageManagerHandler : public ::options::OptionsPageUIHandler {
// Callback to update the UI about the size of browsing data.
void OnGetBrowsingDataSize(bool is_site_data, int64_t size);
// Requests updating the total size of other users' data.
void UpdateOtherUsersSize();
// Callback to save the fetched user sizes and update the UI.
void OnGetOtherUserSize(bool success, int64_t size);
// Requests updating the space size used by Android apps and cache.
void UpdateArcSize();
......@@ -80,6 +91,13 @@ class StorageManagerHandler : public ::options::OptionsPageUIHandler {
// Total size of site data in browsing data.
int64_t browser_site_data_size_;
// The list of other users whose directory sizes will be accumulated as the
// size of "Other users".
user_manager::UserList other_users_;
// Fetched sizes of user directories.
std::vector<int64_t> user_sizes_;
base::WeakPtrFactory<StorageManagerHandler> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(StorageManagerHandler);
......
......@@ -286,6 +286,14 @@ class HomedirMethodsImpl : public HomedirMethods {
weak_ptr_factory_.GetWeakPtr(), callback));
}
void GetAccountDiskUsage(
const Identification& id,
const GetAccountDiskUsageCallback& callback) override {
DBusThreadManager::Get()->GetCryptohomeClient()->GetAccountDiskUsage(
id, base::Bind(&HomedirMethodsImpl::OnGetAccountDiskUsageCallback,
weak_ptr_factory_.GetWeakPtr(), callback));
}
private:
void OnGetKeyDataExCallback(const GetKeyDataCallback& callback,
chromeos::DBusMethodCallStatus call_status,
......@@ -406,6 +414,30 @@ class HomedirMethodsImpl : public HomedirMethods {
callback.Run(true, MOUNT_ERROR_NONE, mount_hash);
}
void OnGetAccountDiskUsageCallback(
const GetAccountDiskUsageCallback& callback,
chromeos::DBusMethodCallStatus call_status,
bool result,
const BaseReply& reply) {
if (call_status != chromeos::DBUS_METHOD_CALL_SUCCESS) {
callback.Run(false, -1);
return;
}
if (reply.has_error()) {
if (reply.error() != CRYPTOHOME_ERROR_NOT_SET) {
callback.Run(false, -1);
return;
}
}
if (!reply.HasExtension(GetAccountDiskUsageReply::reply)) {
callback.Run(false, -1);
return;
}
int64_t size = reply.GetExtension(GetAccountDiskUsageReply::reply).size();
callback.Run(true, size);
}
void OnBaseReplyCallback(const Callback& callback,
chromeos::DBusMethodCallStatus call_status,
bool result,
......
......@@ -5,6 +5,8 @@
#ifndef CHROMEOS_CRYPTOHOME_HOMEDIR_METHODS_H_
#define CHROMEOS_CRYPTOHOME_HOMEDIR_METHODS_H_
#include <stdint.h>
#include <string>
#include <vector>
......@@ -30,6 +32,8 @@ class CHROMEOS_EXPORT HomedirMethods {
typedef base::Callback<
void(bool success, MountError return_code, const std::string& mount_hash)>
MountCallback;
typedef base::Callback<void(bool success, int64_t size)>
GetAccountDiskUsageCallback;
virtual ~HomedirMethods() {}
......@@ -95,6 +99,12 @@ class CHROMEOS_EXPORT HomedirMethods {
const Identification& id_to,
const Callback& callback) = 0;
// Asks cryptohomed to compute the size of cryptohome for user identified by
// |id|.
virtual void GetAccountDiskUsage(
const Identification& id,
const GetAccountDiskUsageCallback& callback) = 0;
// Creates the global HomedirMethods instance.
static void Initialize();
......
......@@ -55,6 +55,9 @@ class CHROMEOS_EXPORT MockHomedirMethods : public HomedirMethods {
void(const Identification& id_from,
const Identification& id_to,
const Callback& callback));
MOCK_METHOD2(GetAccountDiskUsage,
void(const Identification& id,
const GetAccountDiskUsageCallback& callback));
private:
bool success_;
......
......@@ -152,6 +152,21 @@ class CryptohomeClientImpl : public CryptohomeClient {
weak_ptr_factory_.GetWeakPtr(), callback));
}
// CryptohomeClient override.
void GetAccountDiskUsage(const cryptohome::Identification& account_id,
const ProtobufMethodCallback& callback) override {
dbus::MethodCall method_call(cryptohome::kCryptohomeInterface,
cryptohome::kCryptohomeGetAccountDiskUsage);
cryptohome::AccountIdentifier id;
FillIdentificationProtobuf(account_id, &id);
dbus::MessageWriter writer(&method_call);
writer.AppendProtoAsArrayOfBytes(id);
proxy_->CallMethod(&method_call, kTpmDBusTimeoutMs,
base::Bind(&CryptohomeClientImpl::OnBaseReplyMethod,
weak_ptr_factory_.GetWeakPtr(), callback));
}
// CryptohomeClient override.
void GetSystemSalt(const GetSystemSaltCallback& callback) override {
dbus::MethodCall method_call(cryptohome::kCryptohomeInterface,
......
......@@ -152,6 +152,11 @@ class CHROMEOS_EXPORT CryptohomeClient : public DBusClient {
const cryptohome::Identification& cryptohome_id_to,
const ProtobufMethodCallback& callback) = 0;
// Calls GetAccountDiskUsage method. |callback| is called after the method
// call succeeds
virtual void GetAccountDiskUsage(const cryptohome::Identification& account_id,
const ProtobufMethodCallback& callback) = 0;
// Calls GetSystemSalt method. |callback| is called after the method call
// succeeds.
virtual void GetSystemSalt(const GetSystemSaltCallback& callback) = 0;
......
......@@ -105,6 +105,17 @@ void FakeCryptohomeClient::RenameCryptohome(
ReturnProtobufMethodCallback(reply, callback);
}
void FakeCryptohomeClient::GetAccountDiskUsage(
const cryptohome::Identification& account_id,
const ProtobufMethodCallback& callback) {
cryptohome::BaseReply reply;
cryptohome::GetAccountDiskUsageReply* get_account_disk_usage_reply =
reply.MutableExtension(cryptohome::GetAccountDiskUsageReply::reply);
// Sets 100 MB as a fake usage.
get_account_disk_usage_reply->set_size(100 * 1024 * 1024);
ReturnProtobufMethodCallback(reply, callback);
}
void FakeCryptohomeClient::GetSystemSalt(
const GetSystemSaltCallback& callback) {
base::ThreadTaskRunnerHandle::Get()->PostTask(
......
......@@ -42,6 +42,8 @@ class CHROMEOS_EXPORT FakeCryptohomeClient : public CryptohomeClient {
void RenameCryptohome(const cryptohome::Identification& cryptohome_id_from,
const cryptohome::Identification& cryptohome_id_to,
const ProtobufMethodCallback& callback) override;
void GetAccountDiskUsage(const cryptohome::Identification& account_id,
const ProtobufMethodCallback& callback) override;
void GetSystemSalt(const GetSystemSaltCallback& callback) override;
void GetSanitizedUsername(const cryptohome::Identification& cryptohome_id,
const StringDBusMethodCallback& callback) override;
......
......@@ -48,6 +48,9 @@ class MockCryptohomeClient : public CryptohomeClient {
void(const cryptohome::Identification& id_from,
const cryptohome::Identification& id_to,
const ProtobufMethodCallback& callback));
MOCK_METHOD2(GetAccountDiskUsage,
void(const cryptohome::Identification& account_id,
const ProtobufMethodCallback& callback));
MOCK_METHOD1(GetSystemSalt, void(const GetSystemSaltCallback& callback));
MOCK_METHOD2(GetSanitizedUsername,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment