Chromium Code Reviews| Index: device/media_transfer_protocol/media_transfer_protocol_manager.cc |
| diff --git a/device/media_transfer_protocol/media_transfer_protocol_manager.cc b/device/media_transfer_protocol/media_transfer_protocol_manager.cc |
| index dba7d184328447b77256732ae8add753de3a4e92..3c06c469376dd9d8109464c971dc56c0e49119ea 100644 |
| --- a/device/media_transfer_protocol/media_transfer_protocol_manager.cc |
| +++ b/device/media_transfer_protocol/media_transfer_protocol_manager.cc |
| @@ -12,10 +12,11 @@ |
| #include "base/bind.h" |
| #include "base/command_line.h" |
| #include "base/memory/weak_ptr.h" |
| +#include "base/message_loop_proxy.h" |
| #include "base/observer_list.h" |
| #include "base/stl_util.h" |
| +#include "base/threading/thread_checker.h" |
| #include "chrome/common/chrome_switches.h" |
| -#include "content/public/browser/browser_thread.h" |
| #include "device/media_transfer_protocol/media_transfer_protocol_daemon_client.h" |
| #include "device/media_transfer_protocol/mtp_file_entry.pb.h" |
| #include "device/media_transfer_protocol/mtp_storage_info.pb.h" |
| @@ -26,8 +27,6 @@ |
| #include "dbus/bus.h" |
| #endif |
| -using content::BrowserThread; |
| - |
| namespace device { |
| namespace { |
| @@ -37,23 +36,26 @@ MediaTransferProtocolManager* g_media_transfer_protocol_manager = NULL; |
| // The MediaTransferProtocolManager implementation. |
| class MediaTransferProtocolManagerImpl : public MediaTransferProtocolManager { |
| public: |
| - MediaTransferProtocolManagerImpl() : weak_ptr_factory_(this) { |
| + MediaTransferProtocolManagerImpl( |
| + scoped_refptr<base::MessageLoopProxy> loop_proxy) |
| + : weak_ptr_factory_(this) { |
| if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType)) |
| return; |
| dbus::Bus* bus = NULL; |
| #if defined(OS_CHROMEOS) |
| + DCHECK(!loop_proxy.get()); |
| chromeos::DBusThreadManager* dbus_thread_manager = |
| chromeos::DBusThreadManager::Get(); |
| bus = dbus_thread_manager->GetSystemBus(); |
| if (!bus) |
| return; |
| #else |
| + DCHECK(loop_proxy.get()); |
| dbus::Bus::Options options; |
| options.bus_type = dbus::Bus::SYSTEM; |
| options.connection_type = dbus::Bus::PRIVATE; |
| - options.dbus_thread_message_loop_proxy = |
| - BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE); |
| + options.dbus_thread_message_loop_proxy = loop_proxy; |
| session_bus_ = new dbus::Bus(options); |
| bus = session_bus_.get(); |
| #endif |
| @@ -87,7 +89,7 @@ class MediaTransferProtocolManagerImpl : public MediaTransferProtocolManager { |
| // MediaTransferProtocolManager override. |
| const std::vector<std::string> GetStorages() const OVERRIDE { |
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| std::vector<std::string> storages; |
| for (StorageInfoMap::const_iterator it = storage_info_map_.begin(); |
| it != storage_info_map_.end(); |
| @@ -100,7 +102,7 @@ class MediaTransferProtocolManagerImpl : public MediaTransferProtocolManager { |
| // MediaTransferProtocolManager override. |
| virtual const MtpStorageInfo* GetStorageInfo( |
| const std::string& storage_name) const OVERRIDE { |
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| StorageInfoMap::const_iterator it = storage_info_map_.find(storage_name); |
| if (it == storage_info_map_.end()) |
| return NULL; |
| @@ -111,7 +113,7 @@ class MediaTransferProtocolManagerImpl : public MediaTransferProtocolManager { |
| virtual void OpenStorage(const std::string& storage_name, |
| const std::string& mode, |
| const OpenStorageCallback& callback) OVERRIDE { |
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| if (!ContainsKey(storage_info_map_, storage_name)) { |
| callback.Run("", true); |
| return; |
| @@ -129,7 +131,7 @@ class MediaTransferProtocolManagerImpl : public MediaTransferProtocolManager { |
| // MediaTransferProtocolManager override. |
| virtual void CloseStorage(const std::string& storage_handle, |
| const CloseStorageCallback& callback) OVERRIDE { |
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| if (!ContainsKey(handles_, storage_handle)) { |
| callback.Run(true); |
| return; |
| @@ -148,7 +150,7 @@ class MediaTransferProtocolManagerImpl : public MediaTransferProtocolManager { |
| const std::string& storage_handle, |
| const std::string& path, |
| const ReadDirectoryCallback& callback) OVERRIDE { |
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| if (!ContainsKey(handles_, storage_handle)) { |
| callback.Run(std::vector<MtpFileEntry>(), true); |
| return; |
| @@ -168,7 +170,7 @@ class MediaTransferProtocolManagerImpl : public MediaTransferProtocolManager { |
| const std::string& storage_handle, |
| uint32 file_id, |
| const ReadDirectoryCallback& callback) OVERRIDE { |
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| if (!ContainsKey(handles_, storage_handle)) { |
| callback.Run(std::vector<MtpFileEntry>(), true); |
| return; |
| @@ -189,7 +191,7 @@ class MediaTransferProtocolManagerImpl : public MediaTransferProtocolManager { |
| uint32 offset, |
| uint32 count, |
| const ReadFileCallback& callback) OVERRIDE { |
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| if (!ContainsKey(handles_, storage_handle)) { |
| callback.Run(std::string(), true); |
| return; |
| @@ -209,7 +211,7 @@ class MediaTransferProtocolManagerImpl : public MediaTransferProtocolManager { |
| uint32 offset, |
| uint32 count, |
| const ReadFileCallback& callback) OVERRIDE { |
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| if (!ContainsKey(handles_, storage_handle)) { |
| callback.Run(std::string(), true); |
| return; |
| @@ -226,7 +228,7 @@ class MediaTransferProtocolManagerImpl : public MediaTransferProtocolManager { |
| virtual void GetFileInfoByPath(const std::string& storage_handle, |
| const std::string& path, |
| const GetFileInfoCallback& callback) OVERRIDE { |
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| if (!ContainsKey(handles_, storage_handle)) { |
| callback.Run(MtpFileEntry(), true); |
| return; |
| @@ -244,7 +246,7 @@ class MediaTransferProtocolManagerImpl : public MediaTransferProtocolManager { |
| virtual void GetFileInfoById(const std::string& storage_handle, |
| uint32 file_id, |
| const GetFileInfoCallback& callback) OVERRIDE { |
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| if (!ContainsKey(handles_, storage_handle)) { |
| callback.Run(MtpFileEntry(), true); |
| return; |
| @@ -273,7 +275,7 @@ class MediaTransferProtocolManagerImpl : public MediaTransferProtocolManager { |
| typedef std::queue<GetFileInfoCallback> GetFileInfoCallbackQueue; |
| void OnStorageChanged(bool is_attach, const std::string& storage_name) { |
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| if (is_attach) { |
| mtp_client_->GetStorageInfo( |
| storage_name, |
| @@ -297,6 +299,7 @@ class MediaTransferProtocolManagerImpl : public MediaTransferProtocolManager { |
| } |
| void OnEnumerateStorages(const std::vector<std::string>& storage_names) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| for (size_t i = 0; i < storage_names.size(); ++i) { |
| mtp_client_->GetStorageInfo( |
| storage_names[i], |
| @@ -307,7 +310,7 @@ class MediaTransferProtocolManagerImpl : public MediaTransferProtocolManager { |
| } |
| void OnGetStorageInfo(const MtpStorageInfo& storage_info) { |
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| const std::string& storage_name = storage_info.storage_name(); |
| if (ContainsKey(storage_info_map_, storage_name)) { |
| // This should not happen, since MediaTransferProtocolManagerImpl should |
| @@ -328,6 +331,7 @@ class MediaTransferProtocolManagerImpl : public MediaTransferProtocolManager { |
| } |
| void OnOpenStorage(const std::string& handle) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| if (!ContainsKey(handles_, handle)) { |
| handles_.insert(handle); |
| open_storage_callbacks_.front().Run(handle, false); |
| @@ -344,6 +348,7 @@ class MediaTransferProtocolManagerImpl : public MediaTransferProtocolManager { |
| } |
| void OnCloseStorage() { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| const std::string& handle = close_storage_callbacks_.front().second; |
| if (ContainsKey(handles_, handle)) { |
| handles_.erase(handle); |
| @@ -356,36 +361,43 @@ class MediaTransferProtocolManagerImpl : public MediaTransferProtocolManager { |
| } |
| void OnCloseStorageError() { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| close_storage_callbacks_.front().first.Run(true); |
| close_storage_callbacks_.pop(); |
| } |
| void OnReadDirectory(const std::vector<MtpFileEntry>& file_entries) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| read_directory_callbacks_.front().Run(file_entries, false); |
| read_directory_callbacks_.pop(); |
| } |
| void OnReadDirectoryError() { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| read_directory_callbacks_.front().Run(std::vector<MtpFileEntry>(), true); |
| read_directory_callbacks_.pop(); |
| } |
| void OnReadFile(const std::string& data) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| read_file_callbacks_.front().Run(data, false); |
| read_file_callbacks_.pop(); |
| } |
| void OnReadFileError() { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| read_file_callbacks_.front().Run(std::string(), true); |
| read_file_callbacks_.pop(); |
| } |
| void OnGetFileInfo(const MtpFileEntry& entry) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| get_file_info_callbacks_.front().Run(entry, false); |
| get_file_info_callbacks_.pop(); |
| } |
| void OnGetFileInfoError() { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| get_file_info_callbacks_.front().Run(MtpFileEntry(), true); |
| get_file_info_callbacks_.pop(); |
| } |
| @@ -418,18 +430,22 @@ class MediaTransferProtocolManagerImpl : public MediaTransferProtocolManager { |
| ReadFileCallbackQueue read_file_callbacks_; |
| GetFileInfoCallbackQueue get_file_info_callbacks_; |
| + base::ThreadChecker thread_checker_; |
| + |
| DISALLOW_COPY_AND_ASSIGN(MediaTransferProtocolManagerImpl); |
| }; |
| } // namespace |
| // static |
| -void MediaTransferProtocolManager::Initialize() { |
| +void MediaTransferProtocolManager::Initialize( |
| + scoped_refptr<base::MessageLoopProxy> loop_proxy) { |
| if (g_media_transfer_protocol_manager) { |
| LOG(WARNING) << "MediaTransferProtocolManager was already initialized"; |
| return; |
| } |
| - g_media_transfer_protocol_manager = new MediaTransferProtocolManagerImpl(); |
| + g_media_transfer_protocol_manager = new MediaTransferProtocolManagerImpl( |
|
Lei Zhang
2013/01/31 04:01:31
nit: I find it a bit more readable if you wrap the
kmadhusu
2013/01/31 18:12:38
Done.
|
| + loop_proxy); |
| VLOG(1) << "MediaTransferProtocolManager initialized"; |
| } |