Commit 0856cf84 authored by Aga Wronska's avatar Aga Wronska Committed by Commit Bot

Return fixed storage from StorageMonitor::GetAllAvailableStorages(). (2/2)

Consume fixed storage disk events in StorageMonitorChromeos.
Only observe stateful partition, because it is relevant for users.

Bug: 420633
Change-Id: Ia235b8f60b94c396c90da856bc68e3a61b2292dd
Reviewed-on: https://chromium-review.googlesource.com/786649
Commit-Queue: Aga Wronska <agawronska@chromium.org>
Reviewed-by: default avatarSteven Bennetts <stevenjb@chromium.org>
Reviewed-by: default avatarLei Zhang <thestig@chromium.org>
Reviewed-by: default avatarToni Barzic <tbarzic@chromium.org>
Cr-Commit-Position: refs/heads/master@{#521057}
parent 576868c5
......@@ -25,10 +25,10 @@ namespace disks {
namespace {
const char kDefaultFormattedDeviceName[] = "UNTITLED";
const char kDefaultFormatVFAT[] = "vfat";
const char kDeviceNotFound[] = "Device could not be found";
constexpr char kDefaultFormattedDeviceName[] = "UNTITLED";
constexpr char kDefaultFormatVFAT[] = "vfat";
constexpr char kDeviceNotFound[] = "Device could not be found";
constexpr char kStatefulPartition[] = "/mnt/stateful_partition";
DiskMountManager* g_disk_mount_manager = NULL;
// The DiskMountManager implementation.
......@@ -915,6 +915,10 @@ bool DiskMountManager::Disk::IsAutoMountable() const {
return !on_boot_device_;
};
bool DiskMountManager::Disk::IsStatefulPartition() const {
return mount_path_ == kStatefulPartition;
}
bool DiskMountManager::AddDiskForTest(std::unique_ptr<Disk> disk) {
return false;
}
......
......@@ -185,6 +185,8 @@ class CHROMEOS_EXPORT DiskMountManager {
bool IsAutoMountable() const;
bool IsStatefulPartition() const;
private:
std::string device_path_;
std::string mount_path_;
......
......@@ -49,8 +49,8 @@ std::string MakeDeviceUniqueId(const DiskMountManager::Disk& disk) {
return kVendorModelSerialPrefix + vendor + ":" + product + ":";
}
// Returns true if the requested device is valid, else false. On success, fills
// in |info|.
// Returns whether the requested device is valid. On success |info| will contain
// device information.
bool GetDeviceInfo(const DiskMountManager::MountPointInfo& mount_info,
bool has_dcim,
StorageInfo* info) {
......@@ -78,6 +78,24 @@ bool GetDeviceInfo(const DiskMountManager::MountPointInfo& mount_info,
return true;
}
// Returns whether the requested device is valid. On success |info| will contain
// fixed storage device information.
bool GetFixedStorageInfo(const DiskMountManager::Disk& disk,
StorageInfo* info) {
DCHECK(info);
std::string unique_id = MakeDeviceUniqueId(disk);
if (unique_id.empty())
return false;
*info = StorageInfo(
StorageInfo::MakeDeviceId(StorageInfo::FIXED_MASS_STORAGE, unique_id),
disk.mount_path(), base::UTF8ToUTF16(disk.device_label()),
base::UTF8ToUTF16(disk.vendor_name()),
base::UTF8ToUTF16(disk.product_name()), disk.total_size_in_bytes());
return true;
}
} // namespace
StorageMonitorCros::StorageMonitorCros() : weak_ptr_factory_(this) {}
......@@ -107,6 +125,13 @@ void StorageMonitorCros::Init() {
void StorageMonitorCros::CheckExistingMountPoints() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
for (const auto& it : DiskMountManager::GetInstance()->disks()) {
if (it.second->IsStatefulPartition()) {
AddFixedStorageDisk(*it.second);
break;
}
}
scoped_refptr<base::SequencedTaskRunner> blocking_task_runner =
base::CreateSequencedTaskRunnerWithTraits(
{base::MayBlock(), base::TaskPriority::BACKGROUND});
......@@ -138,7 +163,28 @@ void StorageMonitorCros::OnAutoMountableDiskEvent(
void StorageMonitorCros::OnBootDeviceDiskEvent(
DiskMountManager::DiskEvent event,
const DiskMountManager::Disk& disk) {}
const DiskMountManager::Disk& disk) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (!disk.IsStatefulPartition())
return;
switch (event) {
case DiskMountManager::DiskEvent::DISK_ADDED: {
AddFixedStorageDisk(disk);
break;
}
case DiskMountManager::DiskEvent::DISK_REMOVED: {
RemoveFixedStorageDisk(disk);
break;
}
case DiskMountManager::DiskEvent::DISK_CHANGED: {
NOTREACHED() << "DiskMountManager::DiskEvent::DISK_CHANGED should not "
"occur for disks on boot device";
break;
}
}
}
void StorageMonitorCros::OnDeviceEvent(DiskMountManager::DeviceEvent event,
const std::string& device_path) {}
......@@ -302,6 +348,38 @@ void StorageMonitorCros::AddMountedPath(
receiver()->ProcessAttach(info);
}
void StorageMonitorCros::AddFixedStorageDisk(
const DiskMountManager::Disk& disk) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
DCHECK(disk.IsStatefulPartition());
StorageInfo info;
if (!GetFixedStorageInfo(disk, &info))
return;
if (base::ContainsKey(mount_map_, disk.mount_path()))
return;
mount_map_.insert(std::make_pair(disk.mount_path(), info));
receiver()->ProcessAttach(info);
}
void StorageMonitorCros::RemoveFixedStorageDisk(
const DiskMountManager::Disk& disk) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
DCHECK(disk.IsStatefulPartition());
StorageInfo info;
if (!GetFixedStorageInfo(disk, &info))
return;
size_t erased_count = mount_map_.erase(disk.mount_path());
if (!erased_count)
return;
receiver()->ProcessDetach((info.device_id()));
}
StorageMonitor* StorageMonitor::CreateInternal() {
return new StorageMonitorCros();
}
......
......@@ -87,6 +87,15 @@ class StorageMonitorCros : public StorageMonitor,
const chromeos::disks::DiskMountManager::MountPointInfo& mount_info,
bool has_dcim);
// Adds the mount point in |disk| to |mount_map_| and send a device
// attach notification.
void AddFixedStorageDisk(const chromeos::disks::DiskMountManager::Disk& disk);
// Removes the mount point in |disk| from |mount_map_| and send a device
// detach notification.
void RemoveFixedStorageDisk(
const chromeos::disks::DiskMountManager::Disk& disk);
// Mapping of relevant mount points and their corresponding mount devices.
MountMap mount_map_;
......
......@@ -80,6 +80,12 @@ class TestStorageMonitorCros : public StorageMonitorCros {
StorageMonitorCros::OnMountEvent(event, error_code, mount_info);
}
void OnBootDeviceDiskEvent(
DiskMountManager::DiskEvent event,
const chromeos::disks::DiskMountManager::Disk& disk) override {
StorageMonitorCros::OnBootDeviceDiskEvent(event, disk);
}
bool GetStorageInfoForPath(const base::FilePath& path,
StorageInfo* device_info) const override {
return StorageMonitorCros::GetStorageInfoForPath(path, device_info);
......@@ -533,6 +539,42 @@ TEST_F(StorageMonitorCrosTest, EjectTest) {
EXPECT_EQ(StorageMonitor::EJECT_OK, status_);
}
TEST_F(StorageMonitorCrosTest, FixedStroageTest) {
const std::string uuid = "fixed1-uuid";
const std::string mount_point = "/mnt/stateful_partition";
// Fixed storage (stateful partition) added.
const std::string label = "fixed1";
const chromeos::disks::DiskMountManager::Disk disk(
"", mount_point, false, "", "", label, "", "", "", "", "", uuid, "",
chromeos::DEVICE_TYPE_UNKNOWN, 0, false, false, false, false, false,
false, "", "");
monitor_->OnBootDeviceDiskEvent(DiskMountManager::DiskEvent::DISK_ADDED,
disk);
std::vector<StorageInfo> disks = monitor_->GetAllAvailableStorages();
ASSERT_EQ(1U, disks.size());
EXPECT_EQ(mount_point, disks[0].location());
EXPECT_EQ(base::ASCIIToUTF16(label), disks[0].storage_label());
// Fixed storage (not stateful partition) added - ignore.
const chromeos::disks::DiskMountManager::Disk ignored_disk(
"", "usr/share/OEM", false, "", "", "fixed2", "", "", "", "", "",
"fixed2-uuid", "", chromeos::DEVICE_TYPE_UNKNOWN, 0, false, false, false,
false, false, false, "", "");
monitor_->OnBootDeviceDiskEvent(DiskMountManager::DiskEvent::DISK_ADDED,
ignored_disk);
disks = monitor_->GetAllAvailableStorages();
ASSERT_EQ(1U, disks.size());
EXPECT_EQ(mount_point, disks[0].location());
EXPECT_EQ(base::ASCIIToUTF16(label), disks[0].storage_label());
// Fixed storage (stateful partition) removed.
monitor_->OnBootDeviceDiskEvent(DiskMountManager::DiskEvent::DISK_REMOVED,
disk);
disks = monitor_->GetAllAvailableStorages();
EXPECT_EQ(0U, disks.size());
}
} // namespace
} // namespace storage_monitor
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