This source file includes following definitions.
- BuildStorageUnitInfo
- storage_unit_info_list
- InitializeForTesting
- PrepareQueryOnUIThread
- InitializeProvider
- QueryInfo
- GetAllStoragesIntoInfoList
- GetStorageFreeSpaceFromTransientIdOnFileThread
- Get
#include "chrome/browser/extensions/api/system_storage/storage_info_provider.h"
#include "base/stl_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/sys_info.h"
#include "base/threading/sequenced_worker_pool.h"
#include "components/storage_monitor/storage_info.h"
#include "components/storage_monitor/storage_monitor.h"
#include "content/public/browser/browser_thread.h"
using storage_monitor::StorageInfo;
using storage_monitor::StorageMonitor;
namespace extensions {
using content::BrowserThread;
using api::system_storage::StorageUnitInfo;
using api::system_storage::STORAGE_UNIT_TYPE_FIXED;
using api::system_storage::STORAGE_UNIT_TYPE_REMOVABLE;
namespace systeminfo {
void BuildStorageUnitInfo(const StorageInfo& info,
StorageUnitInfo* unit) {
unit->id = StorageMonitor::GetInstance()->GetTransientIdForDeviceId(
info.device_id());
unit->name = base::UTF16ToUTF8(info.GetDisplayName(false));
unit->type = StorageInfo::IsRemovableDevice(info.device_id()) ?
STORAGE_UNIT_TYPE_REMOVABLE : STORAGE_UNIT_TYPE_FIXED;
unit->capacity = static_cast<double>(info.total_size_in_bytes());
}
}
base::LazyInstance<scoped_refptr<StorageInfoProvider> >
StorageInfoProvider::provider_ = LAZY_INSTANCE_INITIALIZER;
StorageInfoProvider::StorageInfoProvider() {
}
StorageInfoProvider::~StorageInfoProvider() {
}
const StorageUnitInfoList& StorageInfoProvider::storage_unit_info_list() const {
return info_;
}
void StorageInfoProvider::InitializeForTesting(
scoped_refptr<StorageInfoProvider> provider) {
DCHECK(provider.get() != NULL);
provider_.Get() = provider;
}
void StorageInfoProvider::PrepareQueryOnUIThread() {
GetAllStoragesIntoInfoList();
}
void StorageInfoProvider::InitializeProvider(
const base::Closure& do_query_info_callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
StorageMonitor::GetInstance()->EnsureInitialized(do_query_info_callback);
}
bool StorageInfoProvider::QueryInfo() {
DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
return true;
}
void StorageInfoProvider::GetAllStoragesIntoInfoList() {
info_.clear();
std::vector<StorageInfo> storage_list =
StorageMonitor::GetInstance()->GetAllAvailableStorages();
for (std::vector<StorageInfo>::const_iterator it = storage_list.begin();
it != storage_list.end(); ++it) {
linked_ptr<StorageUnitInfo> unit(new StorageUnitInfo());
systeminfo::BuildStorageUnitInfo(*it, unit.get());
info_.push_back(unit);
}
}
double StorageInfoProvider::GetStorageFreeSpaceFromTransientIdOnFileThread(
const std::string& transient_id) {
DCHECK_CURRENTLY_ON(BrowserThread::FILE);
std::vector<StorageInfo> storage_list =
StorageMonitor::GetInstance()->GetAllAvailableStorages();
std::string device_id =
StorageMonitor::GetInstance()->GetDeviceIdForTransientId(
transient_id);
for (std::vector<StorageInfo>::const_iterator it =
storage_list.begin();
it != storage_list.end(); ++it) {
if (device_id == it->device_id())
return static_cast<double>(base::SysInfo::AmountOfFreeDiskSpace(
base::FilePath(it->location())));
}
return -1;
}
StorageInfoProvider* StorageInfoProvider::Get() {
if (provider_.Get().get() == NULL)
provider_.Get() = new StorageInfoProvider();
return provider_.Get();
}
}