Commit 048048dd authored by thestig@chromium.org's avatar thestig@chromium.org

Cleanup Various bits of Windows StorageMonitor classes.

Review URL: https://chromiumcodereview.appspot.com/16056002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@203114 0039d316-1c4b-4281-b951-d872f2087c98
parent db88c700
...@@ -16,13 +16,6 @@ ...@@ -16,13 +16,6 @@
namespace chrome { namespace chrome {
namespace {
const char16 kWindowClassName[] = L"Chrome_StorageMonitorWindow";
} // namespace
// StorageMonitorWin ------------------------------------------------------- // StorageMonitorWin -------------------------------------------------------
// static // static
...@@ -59,7 +52,7 @@ StorageMonitorWin::~StorageMonitorWin() { ...@@ -59,7 +52,7 @@ StorageMonitorWin::~StorageMonitorWin() {
void StorageMonitorWin::Init() { void StorageMonitorWin::Init() {
WNDCLASSEX window_class; WNDCLASSEX window_class;
base::win::InitializeWindowClass( base::win::InitializeWindowClass(
kWindowClassName, L"Chrome_StorageMonitorWindow",
&base::win::WrappedWindowProc<StorageMonitorWin::WndProcThunk>, &base::win::WrappedWindowProc<StorageMonitorWin::WndProcThunk>,
0, 0, 0, NULL, NULL, NULL, NULL, NULL, 0, 0, 0, NULL, NULL, NULL, NULL, NULL,
&window_class); &window_class);
...@@ -125,8 +118,8 @@ void StorageMonitorWin::EjectDevice( ...@@ -125,8 +118,8 @@ void StorageMonitorWin::EjectDevice(
bool StorageMonitorWin::GetMTPStorageInfoFromDeviceId( bool StorageMonitorWin::GetMTPStorageInfoFromDeviceId(
const std::string& storage_device_id, const std::string& storage_device_id,
string16* device_location, base::string16* device_location,
string16* storage_object_id) const { base::string16* storage_object_id) const {
StorageInfo::Type type; StorageInfo::Type type;
StorageInfo::CrackDeviceId(storage_device_id, &type, NULL); StorageInfo::CrackDeviceId(storage_device_id, &type, NULL);
return ((type == StorageInfo::MTP_OR_PTP) && return ((type == StorageInfo::MTP_OR_PTP) &&
...@@ -159,6 +152,8 @@ LRESULT CALLBACK StorageMonitorWin::WndProc(HWND hwnd, UINT message, ...@@ -159,6 +152,8 @@ LRESULT CALLBACK StorageMonitorWin::WndProc(HWND hwnd, UINT message,
bool StorageMonitorWin::GetDeviceInfo(const base::FilePath& device_path, bool StorageMonitorWin::GetDeviceInfo(const base::FilePath& device_path,
StorageInfo* info) const { StorageInfo* info) const {
DCHECK(info);
// TODO(kmadhusu) Implement PortableDeviceWatcherWin::GetDeviceInfo() // TODO(kmadhusu) Implement PortableDeviceWatcherWin::GetDeviceInfo()
// function when we have the functionality to add a sub directory of // function when we have the functionality to add a sub directory of
// portable device as a media gallery. // portable device as a media gallery.
......
...@@ -39,8 +39,8 @@ class StorageMonitorWin : public StorageMonitor { ...@@ -39,8 +39,8 @@ class StorageMonitorWin : public StorageMonitor {
StorageInfo* device_info) const OVERRIDE; StorageInfo* device_info) const OVERRIDE;
virtual bool GetMTPStorageInfoFromDeviceId( virtual bool GetMTPStorageInfoFromDeviceId(
const std::string& storage_device_id, const std::string& storage_device_id,
string16* device_location, base::string16* device_location,
string16* storage_object_id) const OVERRIDE; base::string16* storage_object_id) const OVERRIDE;
virtual void EjectDevice( virtual void EjectDevice(
const std::string& device_id, const std::string& device_id,
...@@ -57,8 +57,7 @@ class StorageMonitorWin : public StorageMonitor { ...@@ -57,8 +57,7 @@ class StorageMonitorWin : public StorageMonitor {
PortableDeviceWatcherWin* portable_device_watcher); PortableDeviceWatcherWin* portable_device_watcher);
// Gets the removable storage information given a |device_path|. On success, // Gets the removable storage information given a |device_path|. On success,
// returns true and fills in |device_location|, |unique_id|, |name| and // returns true and fills in |info|.
// |removable|, and |total_size_in_bytes|.
bool GetDeviceInfo(const base::FilePath& device_path, bool GetDeviceInfo(const base::FilePath& device_path,
StorageInfo* info) const; StorageInfo* info) const;
......
...@@ -54,14 +54,14 @@ class StorageMonitorWinTest : public testing::Test { ...@@ -54,14 +54,14 @@ class StorageMonitorWinTest : public testing::Test {
// Injects a device attach or detach change (depending on the value of // Injects a device attach or detach change (depending on the value of
// |test_attach|) and tests that the appropriate handler is called. // |test_attach|) and tests that the appropriate handler is called.
void DoMTPDeviceTest(const string16& pnp_device_id, bool test_attach); void DoMTPDeviceTest(const base::string16& pnp_device_id, bool test_attach);
// Gets the MTP details of the storage specified by the |storage_device_id|. // Gets the MTP details of the storage specified by the |storage_device_id|.
// On success, returns true and fills in |pnp_device_id| and // On success, returns true and fills in |pnp_device_id| and
// |storage_object_id|. // |storage_object_id|.
bool GetMTPStorageInfo(const std::string& storage_device_id, bool GetMTPStorageInfo(const std::string& storage_device_id,
string16* pnp_device_id, base::string16* pnp_device_id,
string16* storage_object_id); base::string16* storage_object_id);
scoped_ptr<TestStorageMonitorWin> monitor_; scoped_ptr<TestStorageMonitorWin> monitor_;
...@@ -198,7 +198,7 @@ void StorageMonitorWinTest::DoMassStorageDevicesDetachedTest( ...@@ -198,7 +198,7 @@ void StorageMonitorWinTest::DoMassStorageDevicesDetachedTest(
EXPECT_EQ(expect_detach_calls, observer_.detach_calls()); EXPECT_EQ(expect_detach_calls, observer_.detach_calls());
} }
void StorageMonitorWinTest::DoMTPDeviceTest(const string16& pnp_device_id, void StorageMonitorWinTest::DoMTPDeviceTest(const base::string16& pnp_device_id,
bool test_attach) { bool test_attach) {
GUID guidDevInterface = GUID_NULL; GUID guidDevInterface = GUID_NULL;
HRESULT hr = CLSIDFromString(kWPDDevInterfaceGUID, &guidDevInterface); HRESULT hr = CLSIDFromString(kWPDDevInterfaceGUID, &guidDevInterface);
...@@ -224,8 +224,8 @@ void StorageMonitorWinTest::DoMTPDeviceTest(const string16& pnp_device_id, ...@@ -224,8 +224,8 @@ void StorageMonitorWinTest::DoMTPDeviceTest(const string16& pnp_device_id,
for (PortableDeviceWatcherWin::StorageObjectIDs::const_iterator it = for (PortableDeviceWatcherWin::StorageObjectIDs::const_iterator it =
storage_object_ids.begin(); it != storage_object_ids.end(); ++it) { storage_object_ids.begin(); it != storage_object_ids.end(); ++it) {
std::string unique_id; std::string unique_id;
string16 name; base::string16 name;
string16 location; base::string16 location;
TestPortableDeviceWatcherWin::GetMTPStorageDetails(pnp_device_id, *it, TestPortableDeviceWatcherWin::GetMTPStorageDetails(pnp_device_id, *it,
&location, &unique_id, &location, &unique_id,
&name); &name);
...@@ -246,8 +246,8 @@ void StorageMonitorWinTest::DoMTPDeviceTest(const string16& pnp_device_id, ...@@ -246,8 +246,8 @@ void StorageMonitorWinTest::DoMTPDeviceTest(const string16& pnp_device_id,
bool StorageMonitorWinTest::GetMTPStorageInfo( bool StorageMonitorWinTest::GetMTPStorageInfo(
const std::string& storage_device_id, const std::string& storage_device_id,
string16* pnp_device_id, base::string16* pnp_device_id,
string16* storage_object_id) { base::string16* storage_object_id) {
return monitor_->GetMTPStorageInfoFromDeviceId(storage_device_id, return monitor_->GetMTPStorageInfoFromDeviceId(storage_device_id,
pnp_device_id, pnp_device_id,
storage_object_id); storage_object_id);
...@@ -433,7 +433,7 @@ TEST_F(StorageMonitorWinTest, DuplicateAttachCheckSuppressed) { ...@@ -433,7 +433,7 @@ TEST_F(StorageMonitorWinTest, DuplicateAttachCheckSuppressed) {
volume_mount_watcher_->FlushWorkerPoolForTesting(); volume_mount_watcher_->FlushWorkerPoolForTesting();
RunUntilIdle(); RunUntilIdle();
std::vector<base::FilePath> checked_devices = const std::vector<base::FilePath>& checked_devices =
volume_mount_watcher_->devices_checked(); volume_mount_watcher_->devices_checked();
ASSERT_EQ(1u, checked_devices.size()); ASSERT_EQ(1u, checked_devices.size());
EXPECT_EQ(kAttachedDevicePath, checked_devices[0]); EXPECT_EQ(kAttachedDevicePath, checked_devices[0]);
...@@ -445,7 +445,6 @@ TEST_F(StorageMonitorWinTest, DuplicateAttachCheckSuppressed) { ...@@ -445,7 +445,6 @@ TEST_F(StorageMonitorWinTest, DuplicateAttachCheckSuppressed) {
volume_mount_watcher_->ReleaseDeviceCheck(); volume_mount_watcher_->ReleaseDeviceCheck();
RunUntilIdle(); RunUntilIdle();
checked_devices = volume_mount_watcher_->devices_checked();
ASSERT_EQ(2u, checked_devices.size()); ASSERT_EQ(2u, checked_devices.size());
EXPECT_EQ(kAttachedDevicePath, checked_devices[0]); EXPECT_EQ(kAttachedDevicePath, checked_devices[0]);
EXPECT_EQ(kAttachedDevicePath, checked_devices[1]); EXPECT_EQ(kAttachedDevicePath, checked_devices[1]);
...@@ -529,12 +528,13 @@ TEST_F(StorageMonitorWinTest, GetMTPStorageInfoFromDeviceId) { ...@@ -529,12 +528,13 @@ TEST_F(StorageMonitorWinTest, GetMTPStorageInfoFromDeviceId) {
for (PortableDeviceWatcherWin::StorageObjects::const_iterator it = for (PortableDeviceWatcherWin::StorageObjects::const_iterator it =
storage_objects.begin(); storage_objects.begin();
it != storage_objects.end(); ++it) { it != storage_objects.end(); ++it) {
string16 pnp_device_id; base::string16 pnp_device_id;
string16 storage_object_id; base::string16 storage_object_id;
ASSERT_TRUE(GetMTPStorageInfo(it->object_persistent_id, &pnp_device_id, ASSERT_TRUE(GetMTPStorageInfo(it->object_persistent_id, &pnp_device_id,
&storage_object_id)); &storage_object_id));
EXPECT_EQ(string16(TestPortableDeviceWatcherWin::kMTPDeviceWithValidInfo), base::string16 expected(
pnp_device_id); TestPortableDeviceWatcherWin::kMTPDeviceWithValidInfo);
EXPECT_EQ(expected, pnp_device_id);
EXPECT_EQ(it->object_persistent_id, EXPECT_EQ(it->object_persistent_id,
TestPortableDeviceWatcherWin::GetMTPStorageUniqueId( TestPortableDeviceWatcherWin::GetMTPStorageUniqueId(
pnp_device_id, storage_object_id)); pnp_device_id, storage_object_id));
......
...@@ -76,9 +76,9 @@ bool GetMassStorageDeviceDetails(const base::FilePath& device_path, ...@@ -76,9 +76,9 @@ bool GetMassStorageDeviceDetails(const base::FilePath& device_path,
"\\\\?\\Volume{00000000-0000-0000-0000-000000000000}\\"; "\\\\?\\Volume{00000000-0000-0000-0000-000000000000}\\";
unique_id[11] = device_path.value()[0]; unique_id[11] = device_path.value()[0];
std::string device_id = StorageInfo::MakeDeviceId(type, unique_id); std::string device_id = StorageInfo::MakeDeviceId(type, unique_id);
string16 storage_label = path.Append(L" Drive").LossyDisplayName(); base::string16 storage_label = path.Append(L" Drive").LossyDisplayName();
*info = StorageInfo(device_id, string16(), path.value(), storage_label, *info = StorageInfo(device_id, base::string16(), path.value(), storage_label,
string16(), string16(), 1000000); base::string16(), base::string16(), 1000000);
return true; return true;
} }
...@@ -96,10 +96,11 @@ TestVolumeMountWatcherWin::~TestVolumeMountWatcherWin() { ...@@ -96,10 +96,11 @@ TestVolumeMountWatcherWin::~TestVolumeMountWatcherWin() {
void TestVolumeMountWatcherWin::AddDeviceForTesting( void TestVolumeMountWatcherWin::AddDeviceForTesting(
const base::FilePath& device_path, const base::FilePath& device_path,
const std::string& device_id, const std::string& device_id,
const string16& device_name, const base::string16& device_name,
uint64 total_size_in_bytes) { uint64 total_size_in_bytes) {
StorageInfo info(device_id, device_name, device_path.value(), StorageInfo info(device_id, device_name, device_path.value(),
string16(), string16(), string16(), total_size_in_bytes); base::string16(), base::string16(), base::string16(),
total_size_in_bytes);
HandleDeviceAttachEventOnUIThread(device_path, info); HandleDeviceAttachEventOnUIThread(device_path, info);
} }
...@@ -128,9 +129,10 @@ void TestVolumeMountWatcherWin::ReleaseDeviceCheck() { ...@@ -128,9 +129,10 @@ void TestVolumeMountWatcherWin::ReleaseDeviceCheck() {
device_check_complete_event_->Signal(); device_check_complete_event_->Signal();
} }
// static
bool TestVolumeMountWatcherWin::GetDeviceRemovable( bool TestVolumeMountWatcherWin::GetDeviceRemovable(
const base::FilePath& device_path, const base::FilePath& device_path,
bool* removable) const { bool* removable) {
StorageInfo info; StorageInfo info;
bool success = GetMassStorageDeviceDetails(device_path, &info); bool success = GetMassStorageDeviceDetails(device_path, &info);
*removable = StorageInfo::IsRemovableDevice(info.device_id()); *removable = StorageInfo::IsRemovableDevice(info.device_id());
...@@ -146,7 +148,6 @@ VolumeMountWatcherWin::GetAttachedDevicesCallbackType ...@@ -146,7 +148,6 @@ VolumeMountWatcherWin::GetAttachedDevicesCallbackType
TestVolumeMountWatcherWin::GetAttachedDevicesCallback() const { TestVolumeMountWatcherWin::GetAttachedDevicesCallback() const {
if (attached_devices_fake_) if (attached_devices_fake_)
return base::Bind(&FakeGetAttachedDevices); return base::Bind(&FakeGetAttachedDevices);
return base::Bind(&FakeGetSingleAttachedDevice); return base::Bind(&FakeGetSingleAttachedDevice);
} }
......
...@@ -27,9 +27,12 @@ class TestVolumeMountWatcherWin : public VolumeMountWatcherWin { ...@@ -27,9 +27,12 @@ class TestVolumeMountWatcherWin : public VolumeMountWatcherWin {
TestVolumeMountWatcherWin(); TestVolumeMountWatcherWin();
virtual ~TestVolumeMountWatcherWin(); virtual ~TestVolumeMountWatcherWin();
static bool GetDeviceRemovable(const base::FilePath& device_path,
bool* removable);
void AddDeviceForTesting(const base::FilePath& device_path, void AddDeviceForTesting(const base::FilePath& device_path,
const std::string& device_id, const std::string& device_id,
const string16& device_name, const base::string16& device_name,
uint64 total_size_in_bytes); uint64 total_size_in_bytes);
void SetAttachedDevicesFake(); void SetAttachedDevicesFake();
...@@ -38,7 +41,7 @@ class TestVolumeMountWatcherWin : public VolumeMountWatcherWin { ...@@ -38,7 +41,7 @@ class TestVolumeMountWatcherWin : public VolumeMountWatcherWin {
virtual void DeviceCheckComplete(const base::FilePath& device_path); virtual void DeviceCheckComplete(const base::FilePath& device_path);
std::vector<base::FilePath> devices_checked() const { const std::vector<base::FilePath>& devices_checked() const {
return devices_checked_; return devices_checked_;
} }
...@@ -46,11 +49,7 @@ class TestVolumeMountWatcherWin : public VolumeMountWatcherWin { ...@@ -46,11 +49,7 @@ class TestVolumeMountWatcherWin : public VolumeMountWatcherWin {
void ReleaseDeviceCheck(); void ReleaseDeviceCheck();
bool GetDeviceRemovable(const base::FilePath& device_path,
bool* removable) const;
// VolumeMountWatcherWin: // VolumeMountWatcherWin:
virtual GetAttachedDevicesCallbackType virtual GetAttachedDevicesCallbackType
GetAttachedDevicesCallback() const OVERRIDE; GetAttachedDevicesCallback() const OVERRIDE;
virtual GetDeviceDetailsCallbackType virtual GetDeviceDetailsCallbackType
......
...@@ -27,6 +27,8 @@ ...@@ -27,6 +27,8 @@
using content::BrowserThread; using content::BrowserThread;
namespace chrome {
namespace { namespace {
const DWORD kMaxPathBufLen = MAX_PATH + 1; const DWORD kMaxPathBufLen = MAX_PATH + 1;
...@@ -53,7 +55,7 @@ enum EjectWinLockOutcomes { ...@@ -53,7 +55,7 @@ enum EjectWinLockOutcomes {
// on either floppy or removable volumes. The DRIVE_CDROM type is handled // on either floppy or removable volumes. The DRIVE_CDROM type is handled
// as a floppy, as are DRIVE_UNKNOWN and DRIVE_NO_ROOT_DIR, as there are // as a floppy, as are DRIVE_UNKNOWN and DRIVE_NO_ROOT_DIR, as there are
// reports that some floppy drives don't report as DRIVE_REMOVABLE. // reports that some floppy drives don't report as DRIVE_REMOVABLE.
DeviceType GetDeviceType(const string16& mount_point) { DeviceType GetDeviceType(const base::string16& mount_point) {
UINT drive_type = GetDriveType(mount_point.c_str()); UINT drive_type = GetDriveType(mount_point.c_str());
if (drive_type == DRIVE_FIXED || drive_type == DRIVE_REMOTE || if (drive_type == DRIVE_FIXED || drive_type == DRIVE_REMOTE ||
drive_type == DRIVE_RAMDISK) { drive_type == DRIVE_RAMDISK) {
...@@ -64,22 +66,22 @@ DeviceType GetDeviceType(const string16& mount_point) { ...@@ -64,22 +66,22 @@ DeviceType GetDeviceType(const string16& mount_point) {
// Check device strings of the form "X:" and "\\.\X:" // Check device strings of the form "X:" and "\\.\X:"
// For floppy drives, these will return strings like "/Device/Floppy0" // For floppy drives, these will return strings like "/Device/Floppy0"
string16 device = mount_point; base::string16 device = mount_point;
if (EndsWith(mount_point, L"\\", false)) if (EndsWith(mount_point, L"\\", false))
device = mount_point.substr(0, mount_point.length() - 1); device = mount_point.substr(0, mount_point.length() - 1);
string16 device_path; base::string16 device_path;
string16 device_path_slash; base::string16 device_path_slash;
DWORD dos_device = QueryDosDevice( DWORD dos_device = QueryDosDevice(
device.c_str(), WriteInto(&device_path, kMaxPathBufLen), kMaxPathBufLen); device.c_str(), WriteInto(&device_path, kMaxPathBufLen), kMaxPathBufLen);
string16 device_slash = string16(L"\\\\.\\"); base::string16 device_slash = base::string16(L"\\\\.\\");
device_slash += device; device_slash += device;
DWORD dos_device_slash = QueryDosDevice( DWORD dos_device_slash = QueryDosDevice(
device_slash.c_str(), WriteInto(&device_path_slash, kMaxPathBufLen), device_slash.c_str(), WriteInto(&device_path_slash, kMaxPathBufLen),
kMaxPathBufLen); kMaxPathBufLen);
if (dos_device == 0 && dos_device_slash == 0) if (dos_device == 0 && dos_device_slash == 0)
return FLOPPY; return FLOPPY;
if (device_path.find(L"Floppy") != string16::npos || if (device_path.find(L"Floppy") != base::string16::npos ||
device_path_slash.find(L"Floppy") != string16::npos) { device_path_slash.find(L"Floppy") != base::string16::npos) {
return FLOPPY; return FLOPPY;
} }
...@@ -104,7 +106,7 @@ bool IsLogicalVolumeStructure(LPARAM data) { ...@@ -104,7 +106,7 @@ bool IsLogicalVolumeStructure(LPARAM data) {
} }
// Gets the total volume of the |mount_point| in bytes. // Gets the total volume of the |mount_point| in bytes.
uint64 GetVolumeSize(const string16& mount_point) { uint64 GetVolumeSize(const base::string16& mount_point) {
ULARGE_INTEGER total; ULARGE_INTEGER total;
if (!GetDiskFreeSpaceExW(mount_point.c_str(), NULL, &total, NULL)) if (!GetDiskFreeSpaceExW(mount_point.c_str(), NULL, &total, NULL))
return 0; return 0;
...@@ -116,11 +118,10 @@ uint64 GetVolumeSize(const string16& mount_point) { ...@@ -116,11 +118,10 @@ uint64 GetVolumeSize(const string16& mount_point) {
// The following msdn blog entry is helpful for understanding disk volumes // The following msdn blog entry is helpful for understanding disk volumes
// and how they are treated in Windows: // and how they are treated in Windows:
// http://blogs.msdn.com/b/adioltean/archive/2005/04/16/408947.aspx. // http://blogs.msdn.com/b/adioltean/archive/2005/04/16/408947.aspx.
bool GetDeviceDetails(const base::FilePath& device_path, bool GetDeviceDetails(const base::FilePath& device_path, StorageInfo* info) {
chrome::StorageInfo* info) {
DCHECK(info); DCHECK(info);
string16 mount_point; base::string16 mount_point;
if (!GetVolumePathName(device_path.value().c_str(), if (!GetVolumePathName(device_path.value().c_str(),
WriteInto(&mount_point, kMaxPathBufLen), WriteInto(&mount_point, kMaxPathBufLen),
kMaxPathBufLen)) { kMaxPathBufLen)) {
...@@ -130,7 +131,7 @@ bool GetDeviceDetails(const base::FilePath& device_path, ...@@ -130,7 +131,7 @@ bool GetDeviceDetails(const base::FilePath& device_path,
// Note: experimentally this code does not spin a floppy drive. It // Note: experimentally this code does not spin a floppy drive. It
// returns a GUID associated with the device, not the volume. // returns a GUID associated with the device, not the volume.
string16 guid; base::string16 guid;
if (!GetVolumeNameForVolumeMountPoint(mount_point.c_str(), if (!GetVolumeNameForVolumeMountPoint(mount_point.c_str(),
WriteInto(&guid, kMaxPathBufLen), WriteInto(&guid, kMaxPathBufLen),
kMaxPathBufLen)) { kMaxPathBufLen)) {
...@@ -148,34 +149,33 @@ bool GetDeviceDetails(const base::FilePath& device_path, ...@@ -148,34 +149,33 @@ bool GetDeviceDetails(const base::FilePath& device_path,
// Note: treats FLOPPY as FIXED_MASS_STORAGE. This is intentional. // Note: treats FLOPPY as FIXED_MASS_STORAGE. This is intentional.
DeviceType device_type = GetDeviceType(mount_point); DeviceType device_type = GetDeviceType(mount_point);
if (device_type == FLOPPY) { if (device_type == FLOPPY) {
info->set_device_id(chrome::StorageInfo::MakeDeviceId( info->set_device_id(StorageInfo::MakeDeviceId(
chrome::StorageInfo::FIXED_MASS_STORAGE, UTF16ToUTF8(guid))); StorageInfo::FIXED_MASS_STORAGE, UTF16ToUTF8(guid)));
return true; return true;
} }
chrome::StorageInfo::Type type = chrome::StorageInfo::FIXED_MASS_STORAGE; StorageInfo::Type type = StorageInfo::FIXED_MASS_STORAGE;
if (device_type == REMOVABLE) { if (device_type == REMOVABLE) {
type = chrome::StorageInfo::REMOVABLE_MASS_STORAGE_NO_DCIM; type = StorageInfo::REMOVABLE_MASS_STORAGE_NO_DCIM;
if (chrome::MediaStorageUtil::HasDcim(base::FilePath(mount_point))) if (MediaStorageUtil::HasDcim(base::FilePath(mount_point)))
type = chrome::StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM; type = StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM;
} }
// NOTE: experimentally, this function returns false if there is no volume // NOTE: experimentally, this function returns false if there is no volume
// name set. // name set.
string16 volume_label; base::string16 volume_label;
GetVolumeInformationW(device_path.value().c_str(), GetVolumeInformationW(device_path.value().c_str(),
WriteInto(&volume_label, kMaxPathBufLen), WriteInto(&volume_label, kMaxPathBufLen),
kMaxPathBufLen, NULL, NULL, NULL, NULL, 0); kMaxPathBufLen, NULL, NULL, NULL, NULL, 0);
uint64 total_size_in_bytes = GetVolumeSize(mount_point); uint64 total_size_in_bytes = GetVolumeSize(mount_point);
std::string device_id = std::string device_id = StorageInfo::MakeDeviceId(type, UTF16ToUTF8(guid));
chrome::StorageInfo::MakeDeviceId(type, UTF16ToUTF8(guid));
// TODO(gbillock): if volume_label.empty(), get the vendor/model information // TODO(gbillock): if volume_label.empty(), get the vendor/model information
// for the volume. // for the volume.
*info = chrome::StorageInfo(device_id, string16(), mount_point, *info = StorageInfo(device_id, base::string16(), mount_point,
volume_label, string16(), string16(), volume_label, base::string16(), base::string16(),
total_size_in_bytes); total_size_in_bytes);
return true; return true;
} }
...@@ -183,14 +183,14 @@ bool GetDeviceDetails(const base::FilePath& device_path, ...@@ -183,14 +183,14 @@ bool GetDeviceDetails(const base::FilePath& device_path,
// connected. // connected.
std::vector<base::FilePath> GetAttachedDevices() { std::vector<base::FilePath> GetAttachedDevices() {
std::vector<base::FilePath> result; std::vector<base::FilePath> result;
string16 volume_name; base::string16 volume_name;
HANDLE find_handle = FindFirstVolume(WriteInto(&volume_name, kMaxPathBufLen), HANDLE find_handle = FindFirstVolume(WriteInto(&volume_name, kMaxPathBufLen),
kMaxPathBufLen); kMaxPathBufLen);
if (find_handle == INVALID_HANDLE_VALUE) if (find_handle == INVALID_HANDLE_VALUE)
return result; return result;
while (true) { while (true) {
string16 volume_path; base::string16 volume_path;
DWORD return_count; DWORD return_count;
if (GetVolumePathNamesForVolumeName(volume_name.c_str(), if (GetVolumePathNamesForVolumeName(volume_name.c_str(),
WriteInto(&volume_path, kMaxPathBufLen), WriteInto(&volume_path, kMaxPathBufLen),
...@@ -217,7 +217,7 @@ std::vector<base::FilePath> GetAttachedDevices() { ...@@ -217,7 +217,7 @@ std::vector<base::FilePath> GetAttachedDevices() {
// See http://support.microsoft.com/kb/165721 // See http://support.microsoft.com/kb/165721
void EjectDeviceInThreadPool( void EjectDeviceInThreadPool(
const base::FilePath& device, const base::FilePath& device,
base::Callback<void(chrome::StorageMonitor::EjectStatus)> callback, base::Callback<void(StorageMonitor::EjectStatus)> callback,
scoped_refptr<base::SequencedTaskRunner> task_runner, scoped_refptr<base::SequencedTaskRunner> task_runner,
int iteration) { int iteration) {
base::FilePath::StringType volume_name; base::FilePath::StringType volume_name;
...@@ -227,9 +227,9 @@ void EjectDeviceInThreadPool( ...@@ -227,9 +227,9 @@ void EjectDeviceInThreadPool(
// at not-just-drive-letter paths. // at not-just-drive-letter paths.
if (drive_letter < L'A' || drive_letter > L'Z' || if (drive_letter < L'A' || drive_letter > L'Z' ||
device != device.DirName()) { device != device.DirName()) {
content::BrowserThread::PostTask( BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE, BrowserThread::UI, FROM_HERE,
base::Bind(callback, chrome::StorageMonitor::EJECT_FAILURE)); base::Bind(callback, StorageMonitor::EJECT_FAILURE));
return; return;
} }
base::SStringPrintf(&volume_name, L"\\\\.\\%lc:", drive_letter); base::SStringPrintf(&volume_name, L"\\\\.\\%lc:", drive_letter);
...@@ -240,9 +240,9 @@ void EjectDeviceInThreadPool( ...@@ -240,9 +240,9 @@ void EjectDeviceInThreadPool(
NULL, OPEN_EXISTING, 0, NULL)); NULL, OPEN_EXISTING, 0, NULL));
if (!volume_handle.IsValid()) { if (!volume_handle.IsValid()) {
content::BrowserThread::PostTask( BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE, BrowserThread::UI, FROM_HERE,
base::Bind(callback, chrome::StorageMonitor::EJECT_FAILURE)); base::Bind(callback, StorageMonitor::EJECT_FAILURE));
return; return;
} }
...@@ -267,16 +267,16 @@ void EjectDeviceInThreadPool( ...@@ -267,16 +267,16 @@ void EjectDeviceInThreadPool(
// Try again -- the lock may have been a transient one. This happens on // Try again -- the lock may have been a transient one. This happens on
// things like AV disk lock for some reason, or another process // things like AV disk lock for some reason, or another process
// transient disk lock. // transient disk lock.
task_runner->PostDelayedTask(FROM_HERE, task_runner->PostDelayedTask(
FROM_HERE,
base::Bind(&EjectDeviceInThreadPool, base::Bind(&EjectDeviceInThreadPool,
device, callback, task_runner, iteration + 1), device, callback, task_runner, iteration + 1),
kLockRetryInterval); kLockRetryInterval);
return; return;
} }
content::BrowserThread::PostTask( BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
content::BrowserThread::UI, FROM_HERE, base::Bind(callback, StorageMonitor::EJECT_IN_USE));
base::Bind(callback, chrome::StorageMonitor::EJECT_IN_USE));
return; return;
} }
...@@ -291,9 +291,8 @@ void EjectDeviceInThreadPool( ...@@ -291,9 +291,8 @@ void EjectDeviceInThreadPool(
if (!dismounted) { if (!dismounted) {
DeviceIoControl(volume_handle, FSCTL_UNLOCK_VOLUME, DeviceIoControl(volume_handle, FSCTL_UNLOCK_VOLUME,
NULL, 0, NULL, 0, &bytes_returned, NULL); NULL, 0, NULL, 0, &bytes_returned, NULL);
content::BrowserThread::PostTask( BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
content::BrowserThread::UI, FROM_HERE, base::Bind(callback, StorageMonitor::EJECT_OK));
base::Bind(callback, chrome::StorageMonitor::EJECT_OK));
return; return;
} }
...@@ -303,30 +302,27 @@ void EjectDeviceInThreadPool( ...@@ -303,30 +302,27 @@ void EjectDeviceInThreadPool(
if (!DeviceIoControl(volume_handle, IOCTL_STORAGE_MEDIA_REMOVAL, if (!DeviceIoControl(volume_handle, IOCTL_STORAGE_MEDIA_REMOVAL,
&pmr_buffer, sizeof(PREVENT_MEDIA_REMOVAL), &pmr_buffer, sizeof(PREVENT_MEDIA_REMOVAL),
NULL, 0, &bytes_returned, NULL)) { NULL, 0, &bytes_returned, NULL)) {
content::BrowserThread::PostTask( BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE, BrowserThread::UI, FROM_HERE,
base::Bind(callback, chrome::StorageMonitor::EJECT_FAILURE)); base::Bind(callback, StorageMonitor::EJECT_FAILURE));
return; return;
} }
// Physically eject or soft-eject the device. // Physically eject or soft-eject the device.
if (!DeviceIoControl(volume_handle, IOCTL_STORAGE_EJECT_MEDIA, if (!DeviceIoControl(volume_handle, IOCTL_STORAGE_EJECT_MEDIA,
NULL, 0, NULL, 0, &bytes_returned, NULL)) { NULL, 0, NULL, 0, &bytes_returned, NULL)) {
content::BrowserThread::PostTask( BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE, BrowserThread::UI, FROM_HERE,
base::Bind(callback, chrome::StorageMonitor::EJECT_FAILURE)); base::Bind(callback, StorageMonitor::EJECT_FAILURE));
return; return;
} }
content::BrowserThread::PostTask( BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
content::BrowserThread::UI, FROM_HERE, base::Bind(callback, StorageMonitor::EJECT_OK));
base::Bind(callback, chrome::StorageMonitor::EJECT_OK));
} }
} // namespace } // namespace
namespace chrome {
const int kWorkerPoolNumThreads = 3; const int kWorkerPoolNumThreads = 3;
const char* kWorkerPoolNamePrefix = "DeviceInfoPool"; const char* kWorkerPoolNamePrefix = "DeviceInfoPool";
...@@ -345,7 +341,7 @@ VolumeMountWatcherWin::VolumeMountWatcherWin() ...@@ -345,7 +341,7 @@ VolumeMountWatcherWin::VolumeMountWatcherWin()
base::FilePath VolumeMountWatcherWin::DriveNumberToFilePath(int drive_number) { base::FilePath VolumeMountWatcherWin::DriveNumberToFilePath(int drive_number) {
if (drive_number < 0 || drive_number > 25) if (drive_number < 0 || drive_number > 25)
return base::FilePath(); return base::FilePath();
string16 path(L"_:\\"); base::string16 path(L"_:\\");
path[0] = L'A' + drive_number; path[0] = L'A' + drive_number;
return base::FilePath(path); return base::FilePath(path);
} }
...@@ -377,10 +373,11 @@ void VolumeMountWatcherWin::AddDevicesOnUIThread( ...@@ -377,10 +373,11 @@ void VolumeMountWatcherWin::AddDevicesOnUIThread(
if (ContainsKey(pending_device_checks_, removable_devices[i])) if (ContainsKey(pending_device_checks_, removable_devices[i]))
continue; continue;
pending_device_checks_.insert(removable_devices[i]); pending_device_checks_.insert(removable_devices[i]);
task_runner_->PostTask(FROM_HERE, base::Bind( task_runner_->PostTask(
&VolumeMountWatcherWin::RetrieveInfoForDeviceAndAdd, FROM_HERE,
removable_devices[i], GetDeviceDetailsCallback(), base::Bind(&VolumeMountWatcherWin::RetrieveInfoForDeviceAndAdd,
weak_factory_.GetWeakPtr())); removable_devices[i], GetDeviceDetailsCallback(),
weak_factory_.GetWeakPtr()));
} }
} }
...@@ -388,18 +385,20 @@ void VolumeMountWatcherWin::AddDevicesOnUIThread( ...@@ -388,18 +385,20 @@ void VolumeMountWatcherWin::AddDevicesOnUIThread(
void VolumeMountWatcherWin::RetrieveInfoForDeviceAndAdd( void VolumeMountWatcherWin::RetrieveInfoForDeviceAndAdd(
const base::FilePath& device_path, const base::FilePath& device_path,
const GetDeviceDetailsCallbackType& get_device_details_callback, const GetDeviceDetailsCallbackType& get_device_details_callback,
base::WeakPtr<chrome::VolumeMountWatcherWin> volume_watcher) { base::WeakPtr<VolumeMountWatcherWin> volume_watcher) {
StorageInfo info; StorageInfo info;
if (!get_device_details_callback.Run(device_path, &info)) { if (!get_device_details_callback.Run(device_path, &info)) {
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind( BrowserThread::PostTask(
&chrome::VolumeMountWatcherWin::DeviceCheckComplete, BrowserThread::UI, FROM_HERE,
volume_watcher, device_path)); base::Bind(&VolumeMountWatcherWin::DeviceCheckComplete,
volume_watcher, device_path));
return; return;
} }
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind( BrowserThread::PostTask(
&chrome::VolumeMountWatcherWin::HandleDeviceAttachEventOnUIThread, BrowserThread::UI, FROM_HERE,
volume_watcher, device_path, info)); base::Bind(&VolumeMountWatcherWin::HandleDeviceAttachEventOnUIThread,
volume_watcher, device_path, info));
} }
void VolumeMountWatcherWin::DeviceCheckComplete( void VolumeMountWatcherWin::DeviceCheckComplete(
...@@ -426,6 +425,7 @@ VolumeMountWatcherWin::GetDeviceDetailsCallbackType ...@@ -426,6 +425,7 @@ VolumeMountWatcherWin::GetDeviceDetailsCallbackType
bool VolumeMountWatcherWin::GetDeviceInfo(const base::FilePath& device_path, bool VolumeMountWatcherWin::GetDeviceInfo(const base::FilePath& device_path,
StorageInfo* info) const { StorageInfo* info) const {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(info);
base::FilePath path(device_path); base::FilePath path(device_path);
MountPointDeviceMetadataMap::const_iterator iter = MountPointDeviceMetadataMap::const_iterator iter =
device_metadata_.find(path.value()); device_metadata_.find(path.value());
...@@ -437,9 +437,7 @@ bool VolumeMountWatcherWin::GetDeviceInfo(const base::FilePath& device_path, ...@@ -437,9 +437,7 @@ bool VolumeMountWatcherWin::GetDeviceInfo(const base::FilePath& device_path,
if (iter == device_metadata_.end()) if (iter == device_metadata_.end())
return false; return false;
if (info) *info = iter->second;
*info = iter->second;
return true; return true;
} }
...@@ -501,7 +499,7 @@ void VolumeMountWatcherWin::HandleDeviceAttachEventOnUIThread( ...@@ -501,7 +499,7 @@ void VolumeMountWatcherWin::HandleDeviceAttachEventOnUIThread(
} }
void VolumeMountWatcherWin::HandleDeviceDetachEventOnUIThread( void VolumeMountWatcherWin::HandleDeviceDetachEventOnUIThread(
const string16& device_location) { const base::string16& device_location) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
MountPointDeviceMetadataMap::const_iterator device_info = MountPointDeviceMetadataMap::const_iterator device_info =
...@@ -519,8 +517,7 @@ void VolumeMountWatcherWin::EjectDevice( ...@@ -519,8 +517,7 @@ void VolumeMountWatcherWin::EjectDevice(
const std::string& device_id, const std::string& device_id,
base::Callback<void(StorageMonitor::EjectStatus)> callback) { base::Callback<void(StorageMonitor::EjectStatus)> callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
base::FilePath device = base::FilePath device = MediaStorageUtil::FindDevicePathById(device_id);
chrome::MediaStorageUtil::FindDevicePathById(device_id);
if (device.empty()) { if (device.empty()) {
callback.Run(StorageMonitor::EJECT_FAILURE); callback.Run(StorageMonitor::EJECT_FAILURE);
return; return;
...@@ -530,7 +527,8 @@ void VolumeMountWatcherWin::EjectDevice( ...@@ -530,7 +527,8 @@ void VolumeMountWatcherWin::EjectDevice(
return; return;
} }
task_runner_->PostTask(FROM_HERE, task_runner_->PostTask(
FROM_HERE,
base::Bind(&EjectDeviceInThreadPool, device, callback, task_runner_, 0)); base::Bind(&EjectDeviceInThreadPool, device, callback, task_runner_, 0));
} }
......
...@@ -44,8 +44,8 @@ class VolumeMountWatcherWin { ...@@ -44,8 +44,8 @@ class VolumeMountWatcherWin {
// Gets the information about the device mounted at |device_path|. On success, // Gets the information about the device mounted at |device_path|. On success,
// returns true and fills in |info|. // returns true and fills in |info|.
// Can block during startup while device info is still loading. // Can block during startup while device info is still loading.
virtual bool GetDeviceInfo(const base::FilePath& device_path, bool GetDeviceInfo(const base::FilePath& device_path,
StorageInfo* info) const; StorageInfo* info) const;
// Processes DEV_BROADCAST_VOLUME messages and triggers a // Processes DEV_BROADCAST_VOLUME messages and triggers a
// notification if appropriate. // notification if appropriate.
...@@ -71,7 +71,7 @@ class VolumeMountWatcherWin { ...@@ -71,7 +71,7 @@ class VolumeMountWatcherWin {
const StorageInfo& info); const StorageInfo& info);
// Handles mass storage device detach event on UI thread. // Handles mass storage device detach event on UI thread.
void HandleDeviceDetachEventOnUIThread(const string16& device_location); void HandleDeviceDetachEventOnUIThread(const base::string16& device_location);
// UI thread delegate to set up adding storage devices. // UI thread delegate to set up adding storage devices.
void AddDevicesOnUIThread(std::vector<base::FilePath> removable_devices); void AddDevicesOnUIThread(std::vector<base::FilePath> removable_devices);
...@@ -81,7 +81,7 @@ class VolumeMountWatcherWin { ...@@ -81,7 +81,7 @@ class VolumeMountWatcherWin {
static void RetrieveInfoForDeviceAndAdd( static void RetrieveInfoForDeviceAndAdd(
const base::FilePath& device_path, const base::FilePath& device_path,
const GetDeviceDetailsCallbackType& get_device_details_callback, const GetDeviceDetailsCallbackType& get_device_details_callback,
base::WeakPtr<chrome::VolumeMountWatcherWin> volume_watcher); base::WeakPtr<VolumeMountWatcherWin> volume_watcher);
// Mark that a device we started a metadata check for has completed. // Mark that a device we started a metadata check for has completed.
virtual void DeviceCheckComplete(const base::FilePath& device_path); virtual void DeviceCheckComplete(const base::FilePath& device_path);
...@@ -100,7 +100,7 @@ class VolumeMountWatcherWin { ...@@ -100,7 +100,7 @@ class VolumeMountWatcherWin {
// Key: Mass storage device mount point. // Key: Mass storage device mount point.
// Value: Mass storage device metadata. // Value: Mass storage device metadata.
typedef std::map<string16, StorageInfo> MountPointDeviceMetadataMap; typedef std::map<base::string16, StorageInfo> MountPointDeviceMetadataMap;
// Maintain a set of device attribute check calls in-flight. Only accessed // Maintain a set of device attribute check calls in-flight. Only accessed
// on the UI thread. This is to try and prevent the same device from // on the UI thread. This is to try and prevent the same device from
......
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