Commit 68df59a3 authored by gbillock@chromium.org's avatar gbillock@chromium.org

[Media Galleries] Switch Mac MTP delegate to async interface.

BUG=None


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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@192631 0039d316-1c4b-4281-b951-d872f2087c98
parent cb75116a
...@@ -5,21 +5,15 @@ ...@@ -5,21 +5,15 @@
#ifndef CHROME_BROWSER_MEDIA_GALLERIES_MTP_DEVICE_DELEGATE_IMPL_MAC_H_ #ifndef CHROME_BROWSER_MEDIA_GALLERIES_MTP_DEVICE_DELEGATE_IMPL_MAC_H_
#define CHROME_BROWSER_MEDIA_GALLERIES_MTP_DEVICE_DELEGATE_IMPL_MAC_H_ #define CHROME_BROWSER_MEDIA_GALLERIES_MTP_DEVICE_DELEGATE_IMPL_MAC_H_
#include <list>
#include <map>
#include <vector> #include <vector>
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/hash_tables.h" #include "base/hash_tables.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "base/platform_file.h" #include "base/platform_file.h"
#include "base/sequenced_task_runner_helpers.h" #include "webkit/fileapi/media/mtp_device_async_delegate.h"
#include "base/synchronization/lock.h"
#include "base/synchronization/waitable_event.h"
#include "webkit/fileapi/file_system_file_util.h"
#include "webkit/fileapi/media/mtp_device_delegate.h"
namespace base {
class SequencedTaskRunner;
}
namespace chrome { namespace chrome {
...@@ -28,94 +22,89 @@ namespace chrome { ...@@ -28,94 +22,89 @@ namespace chrome {
// and names of all files notified through the ItemAdded call will be // and names of all files notified through the ItemAdded call will be
// all appear as children of that directory. (ItemAdded calls with directories // all appear as children of that directory. (ItemAdded calls with directories
// will be ignored.) // will be ignored.)
class MTPDeviceDelegateImplMac : public fileapi::MTPDeviceDelegate { // Note on thread management: This class is thread-compatible: it can be created
// on any thread, but then mutates all state on the UI thread. The async
// delegate interface can be invoked on any thread, as it simply forwards calls
// to the UI thread.
class MTPDeviceDelegateImplMac : public fileapi::MTPDeviceAsyncDelegate {
public: public:
MTPDeviceDelegateImplMac(const std::string& device_id, MTPDeviceDelegateImplMac(const std::string& device_id,
const base::FilePath::StringType& synthetic_path, const base::FilePath::StringType& synthetic_path);
base::SequencedTaskRunner* media_task_runner);
// MTPDeviceDelegate: // MTPDeviceAsyncDelegate implementation. These functions are called on the
virtual base::PlatformFileError GetFileInfo( // IO thread by the async filesystem file util. They forward to
// similarly-named methods on the UI thread.
virtual void GetFileInfo(
const base::FilePath& file_path, const base::FilePath& file_path,
base::PlatformFileInfo* file_info) OVERRIDE; const GetFileInfoSuccessCallback& success_callback,
virtual scoped_ptr<fileapi::FileSystemFileUtil::AbstractFileEnumerator> const ErrorCallback& error_callback) OVERRIDE;
CreateFileEnumerator(const base::FilePath& root, bool recursive) OVERRIDE; virtual void ReadDirectory(
virtual base::PlatformFileError CreateSnapshotFile( const base::FilePath& root,
const ReadDirectorySuccessCallback& success_callback,
const ErrorCallback& error_callback) OVERRIDE;
virtual void CreateSnapshotFile(
const base::FilePath& device_file_path, const base::FilePath& device_file_path,
const base::FilePath& local_path, const base::FilePath& local_path,
base::PlatformFileInfo* file_info) OVERRIDE; const CreateSnapshotFileSuccessCallback& success_callback,
const ErrorCallback& error_callback) OVERRIDE;
virtual void CancelPendingTasksAndDeleteDelegate() OVERRIDE; virtual void CancelPendingTasksAndDeleteDelegate() OVERRIDE;
// Forward delegates for ImageCaptureDeviceListener // Forward delegates for ImageCaptureDeviceListener. These are
// invoked in callbacks of the ImageCapture library on the UI thread.
virtual void ItemAdded(const std::string& name, virtual void ItemAdded(const std::string& name,
const base::PlatformFileInfo& info); const base::PlatformFileInfo& info);
virtual void NoMoreItems(); virtual void NoMoreItems();
virtual void DownloadedFile(const std::string& name, virtual void DownloadedFile(const std::string& name,
base::PlatformFileError error); base::PlatformFileError error);
// Implementation returned by |CreateFileEnumerator()|. Must be deleted // Scheduled when early directory reads are requested. The
// before CancelPendingTasksAndDeleteDelegate is called. // timeout will signal an ABORT error to the caller if the
class Enumerator : // device metadata cannot be read.
public fileapi::FileSystemFileUtil::AbstractFileEnumerator { void ReadDirectoryTimeout(const base::FilePath& root);
public:
explicit Enumerator(MTPDeviceDelegateImplMac* delegate);
virtual ~Enumerator();
virtual base::FilePath Next() OVERRIDE;
virtual int64 Size() OVERRIDE;
virtual base::Time LastModifiedTime() OVERRIDE;
virtual bool IsDirectory() OVERRIDE;
// Called by the delegate to signal any waiters that the received items
// list has changed state.
void ItemsChanged();
private:
MTPDeviceDelegateImplMac* delegate_;
size_t position_;
base::WaitableEvent wait_for_items_;
};
// GetFile and HasAllFiles called by enumerators.
base::FilePath GetFile(size_t index);
bool ReceivedAllFiles();
void RemoveEnumerator(Enumerator* enumerator);
private: private:
friend class base::DeleteHelper<MTPDeviceDelegateImplMac>;
class DeviceListener; class DeviceListener;
virtual ~MTPDeviceDelegateImplMac(); virtual ~MTPDeviceDelegateImplMac();
std::string device_id_; // Delegate for GetFileInfo, called on the UI thread.
base::FilePath root_path_; void GetFileInfoImpl(const base::FilePath& file_path,
base::PlatformFileInfo* file_info,
base::PlatformFileError* error);
// Stores a reference to worker pool thread. All requests and response of file // Delegate for ReadDirectory, called on the UI thread.
// operations are posted on |media_task_runner_|. All callbacks from the void ReadDirectoryImpl(
// camera will come through this task runner as well. const base::FilePath& root,
scoped_refptr<base::SequencedTaskRunner> media_task_runner_; const ReadDirectorySuccessCallback& success_callback,
const ErrorCallback& error_callback);
// Interface object for the camera underlying this MTP session. // Delegate for CreateSnapshotFile, called on the UI thread.
scoped_ptr<DeviceListener> camera_interface_; void DownloadFile(
const base::FilePath& device_file_path,
const base::FilePath& local_path,
const CreateSnapshotFileSuccessCallback& success_callback,
const ErrorCallback& error_callback);
// This lock protects all subsequent state. While calling into the delegate // Public for closures; should not be called except by
// from the FileSystem API happens in sequence, these calls may be // CancelTasksAndDeleteDelegate.
// interleaved with calls on other threads in the sequenced task runner void CancelAndDelete();
// forwarded from the device.
base::Lock mutex_;
// Weak pointer to the enumerator which may be listening for files to come in. // Cancels any outstanding downloads.
Enumerator* enumerator_; void CancelDownloads();
// Stores a map from filename to file metadata received from the camera. // If necessary, notifies the ReadDirectory callback that all data
base::hash_map<base::FilePath::StringType, base::PlatformFileInfo> file_info_; // has been read.
void NotifyReadDir();
std::string device_id_;
base::FilePath root_path_;
// Notification for pending download. // Interface object for the camera underlying this MTP session.
base::WaitableEvent* file_download_event_; scoped_ptr<DeviceListener> camera_interface_;
// Resulting error code for pending download. // Stores a map from filename to file metadata received from the camera.
base::PlatformFileError file_download_error_; base::hash_map<base::FilePath::StringType,
base::PlatformFileInfo> file_info_;
// List of files received from the camera. // List of files received from the camera.
std::vector<base::FilePath> file_paths_; std::vector<base::FilePath> file_paths_;
...@@ -123,6 +112,28 @@ class MTPDeviceDelegateImplMac : public fileapi::MTPDeviceDelegate { ...@@ -123,6 +112,28 @@ class MTPDeviceDelegateImplMac : public fileapi::MTPDeviceDelegate {
// Set to true when all file metadata has been received from the camera. // Set to true when all file metadata has been received from the camera.
bool received_all_files_; bool received_all_files_;
typedef std::map<std::string,
std::pair<CreateSnapshotFileSuccessCallback,
ErrorCallback> > ReadFileTransactionMap;
struct ReadDirectoryRequest {
ReadDirectoryRequest(const base::FilePath& dir,
ReadDirectorySuccessCallback success_cb,
ErrorCallback error_cb);
~ReadDirectoryRequest();
base::FilePath directory;
ReadDirectorySuccessCallback success_callback;
ErrorCallback error_callback;
};
typedef std::list<ReadDirectoryRequest> ReadDirTransactionList;
ReadFileTransactionMap read_file_transactions_;
ReadDirTransactionList read_dir_transactions_;
base::WeakPtrFactory<MTPDeviceDelegateImplMac> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(MTPDeviceDelegateImplMac); DISALLOW_COPY_AND_ASSIGN(MTPDeviceDelegateImplMac);
}; };
......
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
// Use asynchronous MTP device delegate API. // Use asynchronous MTP device delegate API.
// TODO(kmadhusu): remove this define and make this default. // TODO(kmadhusu): remove this define and make this default.
// Note that OS_LINUX implies OS_CHROMEOS // Note that OS_LINUX implies OS_CHROMEOS
#if defined(OS_WIN) || defined(OS_LINUX) #if defined(OS_WIN) || defined(OS_LINUX) || defined(OS_MACOSX)
#define USE_MTP_DEVICE_ASYNC_DELEGATE #define USE_MTP_DEVICE_ASYNC_DELEGATE
#endif #endif
......
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