Commit 0726efb0 authored by thestig@chromium.org's avatar thestig@chromium.org

Storage Monitor: Remove...

Storage Monitor: Remove MediaTransferProtocolDeviceObserverLinux::GetInstance() and access it only from StorageMonitor.

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@192686 0039d316-1c4b-4281-b951-d872f2087c98
parent 80ab663f
...@@ -17,10 +17,6 @@ ...@@ -17,10 +17,6 @@
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "ui/base/text/bytes_formatting.h" #include "ui/base/text/bytes_formatting.h"
#if defined(OS_LINUX) // Implies OS_CHROMEOS
#include "chrome/browser/storage_monitor/media_transfer_protocol_device_observer_linux.h"
#endif
using content::BrowserThread; using content::BrowserThread;
namespace chrome { namespace chrome {
...@@ -301,20 +297,9 @@ bool MediaStorageUtil::GetDeviceInfoFromPath(const base::FilePath& path, ...@@ -301,20 +297,9 @@ bool MediaStorageUtil::GetDeviceInfoFromPath(const base::FilePath& path,
if (!path.IsAbsolute()) if (!path.IsAbsolute())
return false; return false;
bool found_device = false;
StorageInfo info; StorageInfo info;
StorageMonitor* monitor = StorageMonitor::GetInstance(); StorageMonitor* monitor = StorageMonitor::GetInstance();
found_device = monitor->GetStorageInfoForPath(path, &info); bool found_device = monitor->GetStorageInfoForPath(path, &info);
// TODO(gbillock): Move this upstream into the RemovableStorageNotifications
// implementation to handle in its GetDeviceInfoForPath call.
#if defined(OS_LINUX)
if (!found_device) {
MediaTransferProtocolDeviceObserverLinux* mtp_manager =
MediaTransferProtocolDeviceObserverLinux::GetInstance();
found_device = mtp_manager->GetStorageInfoForPath(path, &info);
}
#endif
if (found_device && IsRemovableDevice(info.device_id)) { if (found_device && IsRemovableDevice(info.device_id)) {
base::FilePath sub_folder_path; base::FilePath sub_folder_path;
...@@ -346,7 +331,7 @@ bool MediaStorageUtil::GetDeviceInfoFromPath(const base::FilePath& path, ...@@ -346,7 +331,7 @@ bool MediaStorageUtil::GetDeviceInfoFromPath(const base::FilePath& path,
#endif #endif
// Handle non-removable devices. Note: this is just overwriting // Handle non-removable devices. Note: this is just overwriting
// good values from RemovableStorageNotifications. // good values from StorageMonitor.
// TODO(gbillock): Make sure return values from that class are definitive, // TODO(gbillock): Make sure return values from that class are definitive,
// and don't do this here. // and don't do this here.
info.device_id = MakeDeviceId(FIXED_MASS_STORAGE, path.AsUTF8Unsafe()); info.device_id = MakeDeviceId(FIXED_MASS_STORAGE, path.AsUTF8Unsafe());
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#include "chrome/browser/storage_monitor/media_transfer_protocol_device_observer_linux.h" #include "chrome/browser/storage_monitor/media_transfer_protocol_device_observer_linux.h"
#include <vector>
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/stl_util.h" #include "base/stl_util.h"
#include "base/strings/string_number_conversions.h" #include "base/strings/string_number_conversions.h"
...@@ -120,8 +122,9 @@ void GetStorageInfo(const std::string& storage_name, ...@@ -120,8 +122,9 @@ void GetStorageInfo(const std::string& storage_name,
} // namespace } // namespace
MediaTransferProtocolDeviceObserverLinux:: MediaTransferProtocolDeviceObserverLinux::
MediaTransferProtocolDeviceObserverLinux() MediaTransferProtocolDeviceObserverLinux(StorageMonitor::Receiver* receiver)
: get_storage_info_func_(&GetStorageInfo) { : get_storage_info_func_(&GetStorageInfo),
notifications_(receiver) {
DCHECK(!g_mtp_device_observer); DCHECK(!g_mtp_device_observer);
g_mtp_device_observer = this; g_mtp_device_observer = this;
...@@ -135,9 +138,10 @@ MediaTransferProtocolDeviceObserverLinux() ...@@ -135,9 +138,10 @@ MediaTransferProtocolDeviceObserverLinux()
// This constructor is only used by unit tests. // This constructor is only used by unit tests.
MediaTransferProtocolDeviceObserverLinux:: MediaTransferProtocolDeviceObserverLinux::
MediaTransferProtocolDeviceObserverLinux( MediaTransferProtocolDeviceObserverLinux(
StorageMonitor::Receiver* receiver,
GetStorageInfoFunc get_storage_info_func) GetStorageInfoFunc get_storage_info_func)
: get_storage_info_func_(get_storage_info_func), : get_storage_info_func_(get_storage_info_func),
notifications_(NULL) { notifications_(receiver) {
DCHECK(!g_mtp_device_observer); DCHECK(!g_mtp_device_observer);
g_mtp_device_observer = this; g_mtp_device_observer = this;
} }
...@@ -153,13 +157,6 @@ MediaTransferProtocolDeviceObserverLinux:: ...@@ -153,13 +157,6 @@ MediaTransferProtocolDeviceObserverLinux::
mtp_manager->RemoveObserver(this); mtp_manager->RemoveObserver(this);
} }
// static
MediaTransferProtocolDeviceObserverLinux*
MediaTransferProtocolDeviceObserverLinux::GetInstance() {
DCHECK(g_mtp_device_observer != NULL);
return g_mtp_device_observer;
}
bool MediaTransferProtocolDeviceObserverLinux::GetStorageInfoForPath( bool MediaTransferProtocolDeviceObserverLinux::GetStorageInfoForPath(
const base::FilePath& path, const base::FilePath& path,
StorageInfo* storage_info) const { StorageInfo* storage_info) const {
...@@ -184,11 +181,6 @@ bool MediaTransferProtocolDeviceObserverLinux::GetStorageInfoForPath( ...@@ -184,11 +181,6 @@ bool MediaTransferProtocolDeviceObserverLinux::GetStorageInfoForPath(
return true; return true;
} }
void MediaTransferProtocolDeviceObserverLinux::SetNotifications(
StorageMonitor::Receiver* notifications) {
notifications_ = notifications;
}
// device::MediaTransferProtocolManager::Observer override. // device::MediaTransferProtocolManager::Observer override.
void MediaTransferProtocolDeviceObserverLinux::StorageChanged( void MediaTransferProtocolDeviceObserverLinux::StorageChanged(
bool is_attached, bool is_attached,
......
...@@ -30,24 +30,19 @@ typedef void (*GetStorageInfoFunc)(const std::string& storage_name, ...@@ -30,24 +30,19 @@ typedef void (*GetStorageInfoFunc)(const std::string& storage_name,
class MediaTransferProtocolDeviceObserverLinux class MediaTransferProtocolDeviceObserverLinux
: public device::MediaTransferProtocolManager::Observer { : public device::MediaTransferProtocolManager::Observer {
public: public:
// Should only be called by browser start up code. Use GetInstance() instead. explicit MediaTransferProtocolDeviceObserverLinux(
MediaTransferProtocolDeviceObserverLinux(); StorageMonitor::Receiver* receiver);
virtual ~MediaTransferProtocolDeviceObserverLinux(); virtual ~MediaTransferProtocolDeviceObserverLinux();
static MediaTransferProtocolDeviceObserverLinux* GetInstance();
// Finds the storage that contains |path| and populates |storage_info|. // Finds the storage that contains |path| and populates |storage_info|.
// Returns false if unable to find the storage. // Returns false if unable to find the storage.
bool GetStorageInfoForPath(const base::FilePath& path, bool GetStorageInfoForPath(const base::FilePath& path,
StorageInfo* storage_info) const; StorageInfo* storage_info) const;
// Set the volume notifications object to be used when new
// MTP devices are found.
void SetNotifications(StorageMonitor::Receiver* notifications);
protected: protected:
// Only used in unit tests. // Only used in unit tests.
explicit MediaTransferProtocolDeviceObserverLinux( MediaTransferProtocolDeviceObserverLinux(
StorageMonitor::Receiver* receiver,
GetStorageInfoFunc get_storage_info_func); GetStorageInfoFunc get_storage_info_func);
// device::MediaTransferProtocolManager::Observer implementation. // device::MediaTransferProtocolManager::Observer implementation.
...@@ -71,9 +66,7 @@ class MediaTransferProtocolDeviceObserverLinux ...@@ -71,9 +66,7 @@ class MediaTransferProtocolDeviceObserverLinux
// The notifications object to use to signal newly attached devices. // The notifications object to use to signal newly attached devices.
// Guaranteed to outlive this class. // Guaranteed to outlive this class.
// TODO(gbillock): Edit this when this class is owned by a StorageMonitor::Receiver* const notifications_;
// StorageMonitor subclass.
StorageMonitor::Receiver* notifications_;
DISALLOW_COPY_AND_ASSIGN(MediaTransferProtocolDeviceObserverLinux); DISALLOW_COPY_AND_ASSIGN(MediaTransferProtocolDeviceObserverLinux);
}; };
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "base/message_loop.h" #include "base/message_loop.h"
#include "base/run_loop.h"
#include "base/utf_string_conversions.h" #include "base/utf_string_conversions.h"
#include "chrome/browser/storage_monitor/media_storage_util.h" #include "chrome/browser/storage_monitor/media_storage_util.h"
#include "chrome/browser/storage_monitor/mock_removable_storage_observer.h" #include "chrome/browser/storage_monitor/mock_removable_storage_observer.h"
...@@ -54,17 +55,40 @@ void GetStorageInfo(const std::string& storage_name, ...@@ -54,17 +55,40 @@ void GetStorageInfo(const std::string& storage_name,
*location = kStorageLocation; *location = kStorageLocation;
} }
class TestMediaTransferProtocolDeviceObserverLinux
: public MediaTransferProtocolDeviceObserverLinux {
public:
TestMediaTransferProtocolDeviceObserverLinux(
StorageMonitor::Receiver* receiver)
: MediaTransferProtocolDeviceObserverLinux(receiver, &GetStorageInfo) {
}
// Notifies MediaTransferProtocolDeviceObserverLinux about the attachment of
// mtp storage device given the |storage_name|.
void MtpStorageAttached(const std::string& storage_name) {
StorageChanged(true, storage_name);
base::RunLoop().RunUntilIdle();
}
// Notifies MediaTransferProtocolDeviceObserverLinux about the detachment of
// mtp storage device given the |storage_name|.
void MtpStorageDetached(const std::string& storage_name) {
StorageChanged(false, storage_name);
base::RunLoop().RunUntilIdle();
}
private:
DISALLOW_COPY_AND_ASSIGN(TestMediaTransferProtocolDeviceObserverLinux);
};
} // namespace } // namespace
// A class to test the functionality of MediaTransferProtocolDeviceObserverLinux // A class to test the functionality of MediaTransferProtocolDeviceObserverLinux
// member functions. // member functions.
class MediaTransferProtocolDeviceObserverLinuxTest class MediaTransferProtocolDeviceObserverLinuxTest : public testing::Test {
: public testing::Test,
public MediaTransferProtocolDeviceObserverLinux {
public: public:
MediaTransferProtocolDeviceObserverLinuxTest() MediaTransferProtocolDeviceObserverLinuxTest()
: MediaTransferProtocolDeviceObserverLinux(&GetStorageInfo), : message_loop_(MessageLoop::TYPE_IO),
message_loop_(MessageLoop::TYPE_IO),
file_thread_(content::BrowserThread::FILE, &message_loop_) {} file_thread_(content::BrowserThread::FILE, &message_loop_) {}
virtual ~MediaTransferProtocolDeviceObserverLinuxTest() {} virtual ~MediaTransferProtocolDeviceObserverLinuxTest() {}
...@@ -72,12 +96,14 @@ class MediaTransferProtocolDeviceObserverLinuxTest ...@@ -72,12 +96,14 @@ class MediaTransferProtocolDeviceObserverLinuxTest
protected: protected:
virtual void SetUp() OVERRIDE { virtual void SetUp() OVERRIDE {
mock_storage_observer_.reset(new MockRemovableStorageObserver); mock_storage_observer_.reset(new MockRemovableStorageObserver);
mtp_device_observer_.reset(
new TestMediaTransferProtocolDeviceObserverLinux(monitor_.receiver()));
monitor_.AddObserver(mock_storage_observer_.get()); monitor_.AddObserver(mock_storage_observer_.get());
SetNotifications(monitor_.receiver());
} }
virtual void TearDown() OVERRIDE { virtual void TearDown() OVERRIDE {
monitor_.RemoveObserver(mock_storage_observer_.get()); monitor_.RemoveObserver(mock_storage_observer_.get());
mtp_device_observer_.reset();
} }
// Returns the device changed observer object. // Returns the device changed observer object.
...@@ -85,20 +111,8 @@ class MediaTransferProtocolDeviceObserverLinuxTest ...@@ -85,20 +111,8 @@ class MediaTransferProtocolDeviceObserverLinuxTest
return *mock_storage_observer_; return *mock_storage_observer_;
} }
// Notifies MediaTransferProtocolDeviceObserverLinux about the attachment of TestMediaTransferProtocolDeviceObserverLinux* mtp_device_observer() {
// mtp storage device given the |storage_name|. return mtp_device_observer_.get();
void MtpStorageAttached(const std::string& storage_name) {
MediaTransferProtocolDeviceObserverLinux::StorageChanged(true,
storage_name);
message_loop_.RunUntilIdle();
}
// Notifies MediaTransferProtocolDeviceObserverLinux about the detachment of
// mtp storage device given the |storage_name|.
void MtpStorageDetached(const std::string& storage_name) {
MediaTransferProtocolDeviceObserverLinux::StorageChanged(false,
storage_name);
message_loop_.RunUntilIdle();
} }
private: private:
...@@ -106,6 +120,7 @@ class MediaTransferProtocolDeviceObserverLinuxTest ...@@ -106,6 +120,7 @@ class MediaTransferProtocolDeviceObserverLinuxTest
content::TestBrowserThread file_thread_; content::TestBrowserThread file_thread_;
chrome::test::TestStorageMonitor monitor_; chrome::test::TestStorageMonitor monitor_;
scoped_ptr<TestMediaTransferProtocolDeviceObserverLinux> mtp_device_observer_;
scoped_ptr<MockRemovableStorageObserver> mock_storage_observer_; scoped_ptr<MockRemovableStorageObserver> mock_storage_observer_;
DISALLOW_COPY_AND_ASSIGN(MediaTransferProtocolDeviceObserverLinuxTest); DISALLOW_COPY_AND_ASSIGN(MediaTransferProtocolDeviceObserverLinuxTest);
...@@ -116,7 +131,7 @@ TEST_F(MediaTransferProtocolDeviceObserverLinuxTest, BasicAttachDetach) { ...@@ -116,7 +131,7 @@ TEST_F(MediaTransferProtocolDeviceObserverLinuxTest, BasicAttachDetach) {
std::string device_id = GetMtpDeviceId(kStorageUniqueId); std::string device_id = GetMtpDeviceId(kStorageUniqueId);
// Attach a mtp storage. // Attach a mtp storage.
MtpStorageAttached(kStorageWithValidInfo); mtp_device_observer()->MtpStorageAttached(kStorageWithValidInfo);
EXPECT_EQ(1, observer().attach_calls()); EXPECT_EQ(1, observer().attach_calls());
EXPECT_EQ(0, observer().detach_calls()); EXPECT_EQ(0, observer().detach_calls());
...@@ -125,7 +140,7 @@ TEST_F(MediaTransferProtocolDeviceObserverLinuxTest, BasicAttachDetach) { ...@@ -125,7 +140,7 @@ TEST_F(MediaTransferProtocolDeviceObserverLinuxTest, BasicAttachDetach) {
EXPECT_EQ(kStorageLocation, observer().last_attached().location); EXPECT_EQ(kStorageLocation, observer().last_attached().location);
// Detach the attached storage. // Detach the attached storage.
MtpStorageDetached(kStorageWithValidInfo); mtp_device_observer()->MtpStorageDetached(kStorageWithValidInfo);
EXPECT_EQ(1, observer().attach_calls()); EXPECT_EQ(1, observer().attach_calls());
EXPECT_EQ(1, observer().detach_calls()); EXPECT_EQ(1, observer().detach_calls());
...@@ -137,10 +152,10 @@ TEST_F(MediaTransferProtocolDeviceObserverLinuxTest, BasicAttachDetach) { ...@@ -137,10 +152,10 @@ TEST_F(MediaTransferProtocolDeviceObserverLinuxTest, BasicAttachDetach) {
// notifications. // notifications.
TEST_F(MediaTransferProtocolDeviceObserverLinuxTest, StorageWithInvalidInfo) { TEST_F(MediaTransferProtocolDeviceObserverLinuxTest, StorageWithInvalidInfo) {
// Attach the mtp storage with invalid storage info. // Attach the mtp storage with invalid storage info.
MtpStorageAttached(kStorageWithInvalidInfo); mtp_device_observer()->MtpStorageAttached(kStorageWithInvalidInfo);
// Detach the attached storage. // Detach the attached storage.
MtpStorageDetached(kStorageWithInvalidInfo); mtp_device_observer()->MtpStorageDetached(kStorageWithInvalidInfo);
EXPECT_EQ(0, observer().attach_calls()); EXPECT_EQ(0, observer().attach_calls());
EXPECT_EQ(0, observer().detach_calls()); EXPECT_EQ(0, observer().detach_calls());
......
...@@ -131,8 +131,7 @@ void StorageMonitorCros::Init() { ...@@ -131,8 +131,7 @@ void StorageMonitorCros::Init() {
device::MediaTransferProtocolManager::Initialize(loop_proxy); device::MediaTransferProtocolManager::Initialize(loop_proxy);
media_transfer_protocol_device_observer_.reset( media_transfer_protocol_device_observer_.reset(
new chrome::MediaTransferProtocolDeviceObserverLinux()); new chrome::MediaTransferProtocolDeviceObserverLinux(receiver()));
media_transfer_protocol_device_observer_->SetNotifications(receiver());
} }
} }
...@@ -207,6 +206,14 @@ void StorageMonitorCros::OnFormatEvent( ...@@ -207,6 +206,14 @@ void StorageMonitorCros::OnFormatEvent(
bool StorageMonitorCros::GetStorageInfoForPath( bool StorageMonitorCros::GetStorageInfoForPath(
const base::FilePath& path, const base::FilePath& path,
StorageInfo* device_info) const { StorageInfo* device_info) const {
// TODO(thestig) |media_transfer_protocol_device_observer_| should always be
// valid.
if (media_transfer_protocol_device_observer_ &&
media_transfer_protocol_device_observer_->GetStorageInfoForPath(
path, device_info)) {
return true;
}
if (!path.IsAbsolute()) if (!path.IsAbsolute())
return false; return false;
......
...@@ -258,8 +258,7 @@ void StorageMonitorLinux::Init() { ...@@ -258,8 +258,7 @@ void StorageMonitorLinux::Init() {
device::MediaTransferProtocolManager::Initialize(loop_proxy); device::MediaTransferProtocolManager::Initialize(loop_proxy);
media_transfer_protocol_device_observer_.reset( media_transfer_protocol_device_observer_.reset(
new MediaTransferProtocolDeviceObserverLinux()); new MediaTransferProtocolDeviceObserverLinux(receiver()));
media_transfer_protocol_device_observer_->SetNotifications(receiver());
} }
} }
...@@ -268,6 +267,14 @@ bool StorageMonitorLinux::GetStorageInfoForPath( ...@@ -268,6 +267,14 @@ bool StorageMonitorLinux::GetStorageInfoForPath(
StorageInfo* device_info) const { StorageInfo* device_info) const {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
// TODO(thestig) |media_transfer_protocol_device_observer_| should always be
// valid.
if (media_transfer_protocol_device_observer_ &&
media_transfer_protocol_device_observer_->GetStorageInfoForPath(
path, device_info)) {
return true;
}
if (!path.IsAbsolute()) if (!path.IsAbsolute())
return false; return false;
......
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