Commit 5992a9c1 authored by thestig@chromium.org's avatar thestig@chromium.org

Media Galleries: Access MTP devices by file ids rather than file paths.

File paths can be very slow to access because finding a file by path requires
O(N) metadata access to figure out which file id corresponds to a given name.

BUG=385307

Review URL: https://codereview.chromium.org/377383002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@283732 0039d316-1c4b-4281-b951-d872f2087c98
parent 4fbf991d
......@@ -7,11 +7,10 @@
#include "net/base/io_buffer.h"
MTPDeviceAsyncDelegate::ReadBytesRequest::ReadBytesRequest(
const std::string& device_file_relative_path,
net::IOBuffer* buf, int64 offset, int buf_len,
uint32 file_id, net::IOBuffer* buf, int64 offset, int buf_len,
const ReadBytesSuccessCallback& success_callback,
const ErrorCallback& error_callback)
: device_file_relative_path(device_file_relative_path),
: file_id(file_id),
buf(buf),
offset(offset),
buf_len(buf_len),
......
......@@ -49,13 +49,13 @@ class MTPDeviceAsyncDelegate {
int bytes_read)> ReadBytesSuccessCallback;
struct ReadBytesRequest {
ReadBytesRequest(const std::string& device_file_relative_path,
ReadBytesRequest(uint32 file_id,
net::IOBuffer* buf, int64 offset, int buf_len,
const ReadBytesSuccessCallback& success_callback,
const ErrorCallback& error_callback);
~ReadBytesRequest();
std::string device_file_relative_path;
uint32 file_id;
scoped_refptr<net::IOBuffer> buf;
int64 offset;
int buf_len;
......
......@@ -5,16 +5,19 @@
#include "chrome/browser/media_galleries/linux/mtp_device_delegate_impl_linux.h"
#include <algorithm>
#include <set>
#include <vector>
#include "base/bind.h"
#include "base/files/file_path.h"
#include "base/numerics/safe_conversions.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "chrome/browser/media_galleries/linux/mtp_device_task_helper.h"
#include "chrome/browser/media_galleries/linux/mtp_device_task_helper_map_service.h"
#include "chrome/browser/media_galleries/linux/snapshot_file_details.h"
#include "content/public/browser/browser_thread.h"
#include "net/base/io_buffer.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
namespace {
......@@ -80,7 +83,7 @@ void OpenStorageOnUIThread(
task_helper->OpenStorage(storage_name, reply_callback);
}
// Enumerates the |root| directory file entries.
// Enumerates the |dir_id| directory file entries.
//
// Called on the UI thread to dispatch the request to the
// MediaTransferProtocolManager.
......@@ -91,7 +94,7 @@ void OpenStorageOnUIThread(
// |success_callback| and |error_callback| runs on the IO thread.
void ReadDirectoryOnUIThread(
const std::string& storage_name,
const std::string& root,
uint32 dir_id,
const base::Callback<
void(const fileapi::AsyncFileUtil::EntryList&)>& success_callback,
const base::Callback<void(base::File::Error)>& error_callback) {
......@@ -100,7 +103,7 @@ void ReadDirectoryOnUIThread(
GetDeviceTaskHelperForStorage(storage_name);
if (!task_helper)
return;
task_helper->ReadDirectoryByPath(root, success_callback, error_callback);
task_helper->ReadDirectoryById(dir_id, success_callback, error_callback);
}
// Gets the |file_path| details.
......@@ -114,15 +117,15 @@ void ReadDirectoryOnUIThread(
// |success_callback| and |error_callback| runs on the IO thread.
void GetFileInfoOnUIThread(
const std::string& storage_name,
const std::string& file_path,
const base::Callback<void(const base::File::Info&)>& success_callback,
const base::Callback<void(base::File::Error)>& error_callback) {
uint32 file_id,
const MTPDeviceAsyncDelegate::GetFileInfoSuccessCallback& success_callback,
const MTPDeviceAsyncDelegate::ErrorCallback& error_callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
MTPDeviceTaskHelper* task_helper =
GetDeviceTaskHelperForStorage(storage_name);
if (!task_helper)
return;
task_helper->GetFileInfoByPath(file_path, success_callback, error_callback);
task_helper->GetFileInfoById(file_id, success_callback, error_callback);
}
// Copies the contents of |device_file_path| to |snapshot_file_path|.
......@@ -188,20 +191,128 @@ void CloseStorageAndDestroyTaskHelperOnUIThread(
} // namespace
MTPDeviceDelegateImplLinux::PendingTaskInfo::PendingTaskInfo(
const base::FilePath& path,
content::BrowserThread::ID thread_id,
const tracked_objects::Location& location,
const base::Closure& task)
: location(location),
: path(path),
thread_id(thread_id),
location(location),
task(task) {
}
MTPDeviceDelegateImplLinux::PendingTaskInfo::~PendingTaskInfo() {
}
// Represents a file on the MTP device.
// Lives on the IO thread.
class MTPDeviceDelegateImplLinux::MTPFileNode {
public:
MTPFileNode(uint32 file_id,
MTPFileNode* parent,
FileIdToMTPFileNodeMap* file_id_to_node_map);
~MTPFileNode();
const MTPFileNode* GetChild(const std::string& name) const;
void EnsureChildExists(const std::string& name, uint32 id);
// Clears all the children, except those in |children_to_keep|.
void ClearNonexistentChildren(
const std::set<std::string>& children_to_keep);
bool DeleteChild(uint32 file_id);
uint32 file_id() const { return file_id_; }
MTPFileNode* parent() { return parent_; }
private:
// Container for holding a node's children.
typedef base::ScopedPtrHashMap<std::string, MTPFileNode> ChildNodes;
const uint32 file_id_;
ChildNodes children_;
MTPFileNode* const parent_;
FileIdToMTPFileNodeMap* file_id_to_node_map_;
DISALLOW_COPY_AND_ASSIGN(MTPFileNode);
};
MTPDeviceDelegateImplLinux::MTPFileNode::MTPFileNode(
uint32 file_id,
MTPFileNode* parent,
FileIdToMTPFileNodeMap* file_id_to_node_map)
: file_id_(file_id),
parent_(parent),
file_id_to_node_map_(file_id_to_node_map) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
DCHECK(file_id_to_node_map_);
DCHECK(!ContainsKey(*file_id_to_node_map_, file_id_));
(*file_id_to_node_map_)[file_id_] = this;
}
MTPDeviceDelegateImplLinux::MTPFileNode::~MTPFileNode() {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
size_t erased = file_id_to_node_map_->erase(file_id_);
DCHECK_EQ(1U, erased);
}
const MTPDeviceDelegateImplLinux::MTPFileNode*
MTPDeviceDelegateImplLinux::MTPFileNode::GetChild(
const std::string& name) const {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
return children_.get(name);
}
void MTPDeviceDelegateImplLinux::MTPFileNode::EnsureChildExists(
const std::string& name,
uint32 id) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
const MTPFileNode* child = GetChild(name);
if (child && child->file_id() == id)
return;
children_.set(
name,
make_scoped_ptr(new MTPFileNode(id, this, file_id_to_node_map_)));
}
void MTPDeviceDelegateImplLinux::MTPFileNode::ClearNonexistentChildren(
const std::set<std::string>& children_to_keep) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
std::set<std::string> children_to_erase;
for (ChildNodes::const_iterator it = children_.begin();
it != children_.end(); ++it) {
if (ContainsKey(children_to_keep, it->first))
continue;
children_to_erase.insert(it->first);
}
for (std::set<std::string>::iterator it = children_to_erase.begin();
it != children_to_erase.end(); ++it) {
children_.take_and_erase(*it);
}
}
bool MTPDeviceDelegateImplLinux::MTPFileNode::DeleteChild(uint32 file_id) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
for (ChildNodes::iterator it = children_.begin();
it != children_.end(); ++it) {
if (it->second->file_id() == file_id) {
children_.erase(it);
return true;
}
}
return false;
}
MTPDeviceDelegateImplLinux::MTPDeviceDelegateImplLinux(
const std::string& device_location)
: init_state_(UNINITIALIZED),
task_in_progress_(false),
device_path_(device_location),
root_node_(new MTPFileNode(mtpd::kRootFileId,
NULL,
&file_id_to_node_map_)),
weak_ptr_factory_(this) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
DCHECK(!device_path_.empty());
......@@ -219,17 +330,16 @@ void MTPDeviceDelegateImplLinux::GetFileInfo(
const ErrorCallback& error_callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
DCHECK(!file_path.empty());
base::Closure call_closure =
base::Bind(&GetFileInfoOnUIThread,
storage_name_,
GetDeviceRelativePath(device_path_, file_path),
base::Bind(&MTPDeviceDelegateImplLinux::OnDidGetFileInfo,
base::Closure closure =
base::Bind(&MTPDeviceDelegateImplLinux::GetFileInfoInternal,
weak_ptr_factory_.GetWeakPtr(),
success_callback),
base::Bind(&MTPDeviceDelegateImplLinux::HandleDeviceFileError,
weak_ptr_factory_.GetWeakPtr(),
error_callback));
EnsureInitAndRunTask(PendingTaskInfo(FROM_HERE, call_closure));
file_path,
success_callback,
error_callback);
EnsureInitAndRunTask(PendingTaskInfo(file_path,
content::BrowserThread::IO,
FROM_HERE,
closure));
}
void MTPDeviceDelegateImplLinux::ReadDirectory(
......@@ -238,23 +348,16 @@ void MTPDeviceDelegateImplLinux::ReadDirectory(
const ErrorCallback& error_callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
DCHECK(!root.empty());
std::string device_file_relative_path = GetDeviceRelativePath(device_path_,
root);
base::Closure call_closure =
base::Bind(
&GetFileInfoOnUIThread,
storage_name_,
device_file_relative_path,
base::Bind(
&MTPDeviceDelegateImplLinux::OnDidGetFileInfoToReadDirectory,
base::Closure closure =
base::Bind(&MTPDeviceDelegateImplLinux::ReadDirectoryInternal,
weak_ptr_factory_.GetWeakPtr(),
device_file_relative_path,
root,
success_callback,
error_callback),
base::Bind(&MTPDeviceDelegateImplLinux::HandleDeviceFileError,
weak_ptr_factory_.GetWeakPtr(),
error_callback));
EnsureInitAndRunTask(PendingTaskInfo(FROM_HERE, call_closure));
error_callback);
EnsureInitAndRunTask(PendingTaskInfo(root,
content::BrowserThread::IO,
FROM_HERE,
closure));
}
void MTPDeviceDelegateImplLinux::CreateSnapshotFile(
......@@ -265,26 +368,17 @@ void MTPDeviceDelegateImplLinux::CreateSnapshotFile(
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
DCHECK(!device_file_path.empty());
DCHECK(!local_path.empty());
std::string device_file_relative_path =
GetDeviceRelativePath(device_path_, device_file_path);
scoped_ptr<SnapshotRequestInfo> request_info(
new SnapshotRequestInfo(device_file_relative_path,
base::Closure closure =
base::Bind(&MTPDeviceDelegateImplLinux::CreateSnapshotFileInternal,
weak_ptr_factory_.GetWeakPtr(),
device_file_path,
local_path,
success_callback,
error_callback));
base::Closure call_closure =
base::Bind(
&GetFileInfoOnUIThread,
storage_name_,
device_file_relative_path,
base::Bind(
&MTPDeviceDelegateImplLinux::OnDidGetFileInfoToCreateSnapshotFile,
weak_ptr_factory_.GetWeakPtr(),
base::Passed(&request_info)),
base::Bind(&MTPDeviceDelegateImplLinux::HandleDeviceFileError,
weak_ptr_factory_.GetWeakPtr(),
error_callback));
EnsureInitAndRunTask(PendingTaskInfo(FROM_HERE, call_closure));
error_callback);
EnsureInitAndRunTask(PendingTaskInfo(device_file_path,
content::BrowserThread::IO,
FROM_HERE,
closure));
}
bool MTPDeviceDelegateImplLinux::IsStreaming() {
......@@ -298,20 +392,19 @@ void MTPDeviceDelegateImplLinux::ReadBytes(
const ErrorCallback& error_callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
DCHECK(!device_file_path.empty());
std::string device_file_relative_path =
GetDeviceRelativePath(device_path_, device_file_path);
ReadBytesRequest request(
device_file_relative_path, buf, offset, buf_len,
base::Bind(&MTPDeviceDelegateImplLinux::OnDidReadBytes,
weak_ptr_factory_.GetWeakPtr(), success_callback),
base::Bind(&MTPDeviceDelegateImplLinux::HandleDeviceFileError,
weak_ptr_factory_.GetWeakPtr(), error_callback));
base::Closure call_closure =
base::Bind(base::Bind(&ReadBytesOnUIThread, storage_name_, request));
EnsureInitAndRunTask(PendingTaskInfo(FROM_HERE, call_closure));
base::Closure closure =
base::Bind(&MTPDeviceDelegateImplLinux::ReadBytesInternal,
weak_ptr_factory_.GetWeakPtr(),
device_file_path,
make_scoped_refptr(buf),
offset,
buf_len,
success_callback,
error_callback);
EnsureInitAndRunTask(PendingTaskInfo(device_file_path,
content::BrowserThread::IO,
FROM_HERE,
closure));
}
void MTPDeviceDelegateImplLinux::CancelPendingTasksAndDeleteDelegate() {
......@@ -324,17 +417,159 @@ void MTPDeviceDelegateImplLinux::CancelPendingTasksAndDeleteDelegate() {
delete this;
}
void MTPDeviceDelegateImplLinux::GetFileInfoInternal(
const base::FilePath& file_path,
const GetFileInfoSuccessCallback& success_callback,
const ErrorCallback& error_callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
uint32 file_id;
if (CachedPathToId(file_path, &file_id)) {
GetFileInfoSuccessCallback success_callback_wrapper =
base::Bind(&MTPDeviceDelegateImplLinux::OnDidGetFileInfo,
weak_ptr_factory_.GetWeakPtr(),
success_callback);
ErrorCallback error_callback_wrapper =
base::Bind(&MTPDeviceDelegateImplLinux::HandleDeviceFileError,
weak_ptr_factory_.GetWeakPtr(),
error_callback,
file_id);
base::Closure closure = base::Bind(&GetFileInfoOnUIThread,
storage_name_,
file_id,
success_callback_wrapper,
error_callback_wrapper);
EnsureInitAndRunTask(PendingTaskInfo(base::FilePath(),
content::BrowserThread::UI,
FROM_HERE,
closure));
} else {
error_callback.Run(base::File::FILE_ERROR_NOT_FOUND);
}
PendingRequestDone();
}
void MTPDeviceDelegateImplLinux::ReadDirectoryInternal(
const base::FilePath& root,
const ReadDirectorySuccessCallback& success_callback,
const ErrorCallback& error_callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
uint32 dir_id;
if (CachedPathToId(root, &dir_id)) {
GetFileInfoSuccessCallback success_callback_wrapper =
base::Bind(&MTPDeviceDelegateImplLinux::OnDidGetFileInfoToReadDirectory,
weak_ptr_factory_.GetWeakPtr(),
dir_id,
success_callback,
error_callback);
ErrorCallback error_callback_wrapper =
base::Bind(&MTPDeviceDelegateImplLinux::HandleDeviceFileError,
weak_ptr_factory_.GetWeakPtr(),
error_callback,
dir_id);
base::Closure closure = base::Bind(&GetFileInfoOnUIThread,
storage_name_,
dir_id,
success_callback_wrapper,
error_callback_wrapper);
EnsureInitAndRunTask(PendingTaskInfo(base::FilePath(),
content::BrowserThread::UI,
FROM_HERE,
closure));
} else {
error_callback.Run(base::File::FILE_ERROR_NOT_FOUND);
}
PendingRequestDone();
}
void MTPDeviceDelegateImplLinux::CreateSnapshotFileInternal(
const base::FilePath& device_file_path,
const base::FilePath& local_path,
const CreateSnapshotFileSuccessCallback& success_callback,
const ErrorCallback& error_callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
uint32 file_id;
if (CachedPathToId(device_file_path, &file_id)) {
scoped_ptr<SnapshotRequestInfo> request_info(
new SnapshotRequestInfo(file_id,
local_path,
success_callback,
error_callback));
GetFileInfoSuccessCallback success_callback_wrapper =
base::Bind(
&MTPDeviceDelegateImplLinux::OnDidGetFileInfoToCreateSnapshotFile,
weak_ptr_factory_.GetWeakPtr(),
base::Passed(&request_info));
ErrorCallback error_callback_wrapper =
base::Bind(&MTPDeviceDelegateImplLinux::HandleDeviceFileError,
weak_ptr_factory_.GetWeakPtr(),
error_callback,
file_id);
base::Closure closure = base::Bind(&GetFileInfoOnUIThread,
storage_name_,
file_id,
success_callback_wrapper,
error_callback_wrapper);
EnsureInitAndRunTask(PendingTaskInfo(base::FilePath(),
content::BrowserThread::UI,
FROM_HERE,
closure));
} else {
error_callback.Run(base::File::FILE_ERROR_NOT_FOUND);
}
PendingRequestDone();
}
void MTPDeviceDelegateImplLinux::ReadBytesInternal(
const base::FilePath& device_file_path,
net::IOBuffer* buf, int64 offset, int buf_len,
const ReadBytesSuccessCallback& success_callback,
const ErrorCallback& error_callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
uint32 file_id;
if (CachedPathToId(device_file_path, &file_id)) {
ReadBytesRequest request(
file_id, buf, offset, buf_len,
base::Bind(&MTPDeviceDelegateImplLinux::OnDidReadBytes,
weak_ptr_factory_.GetWeakPtr(),
success_callback),
base::Bind(&MTPDeviceDelegateImplLinux::HandleDeviceFileError,
weak_ptr_factory_.GetWeakPtr(),
error_callback,
file_id));
base::Closure closure =
base::Bind(base::Bind(&ReadBytesOnUIThread, storage_name_, request));
EnsureInitAndRunTask(PendingTaskInfo(base::FilePath(),
content::BrowserThread::UI,
FROM_HERE,
closure));
} else {
error_callback.Run(base::File::FILE_ERROR_NOT_FOUND);
}
PendingRequestDone();
}
void MTPDeviceDelegateImplLinux::EnsureInitAndRunTask(
const PendingTaskInfo& task_info) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
if ((init_state_ == INITIALIZED) && !task_in_progress_) {
task_in_progress_ = true;
content::BrowserThread::PostTask(content::BrowserThread::UI,
task_info.location,
task_info.task);
RunTask(task_info);
return;
}
pending_tasks_.push(task_info);
// Only *Internal functions have empty paths. Since they are the continuation
// of the current running task, they get to cut in line.
if (task_info.path.empty())
pending_tasks_.push_front(task_info);
else
pending_tasks_.push_back(task_info);
if (init_state_ == UNINITIALIZED) {
init_state_ = PENDING_INIT;
task_in_progress_ = true;
......@@ -348,6 +583,29 @@ void MTPDeviceDelegateImplLinux::EnsureInitAndRunTask(
}
}
void MTPDeviceDelegateImplLinux::RunTask(const PendingTaskInfo& task_info) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
DCHECK_EQ(INITIALIZED, init_state_);
DCHECK(!task_in_progress_);
task_in_progress_ = true;
bool need_to_check_cache = !task_info.path.empty();
if (need_to_check_cache) {
base::FilePath uncached_path =
NextUncachedPathComponent(task_info.path, task_info.cached_path);
if (!uncached_path.empty()) {
// Save the current task and do a cache lookup first.
pending_tasks_.push_front(task_info);
FillFileCache(uncached_path);
return;
}
}
content::BrowserThread::PostTask(task_info.thread_id,
task_info.location,
task_info.task);
}
void MTPDeviceDelegateImplLinux::WriteDataIntoSnapshotFile(
const base::File::Info& file_info) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
......@@ -355,7 +613,7 @@ void MTPDeviceDelegateImplLinux::WriteDataIntoSnapshotFile(
DCHECK_GT(file_info.size, 0);
DCHECK(task_in_progress_);
SnapshotRequestInfo request_info(
current_snapshot_request_info_->device_file_path,
current_snapshot_request_info_->file_id,
current_snapshot_request_info_->snapshot_file_path,
base::Bind(
&MTPDeviceDelegateImplLinux::OnDidWriteDataIntoSnapshotFile,
......@@ -385,12 +643,9 @@ void MTPDeviceDelegateImplLinux::ProcessNextPendingRequest() {
if (pending_tasks_.empty())
return;
task_in_progress_ = true;
const PendingTaskInfo& task_info = pending_tasks_.front();
content::BrowserThread::PostTask(content::BrowserThread::UI,
task_info.location,
task_info.task);
pending_tasks_.pop();
PendingTaskInfo task_info = pending_tasks_.front();
pending_tasks_.pop_front();
RunTask(task_info);
}
void MTPDeviceDelegateImplLinux::OnInitCompleted(bool succeeded) {
......@@ -408,7 +663,7 @@ void MTPDeviceDelegateImplLinux::OnDidGetFileInfo(
}
void MTPDeviceDelegateImplLinux::OnDidGetFileInfoToReadDirectory(
const std::string& root,
uint32 dir_id,
const ReadDirectorySuccessCallback& success_callback,
const ErrorCallback& error_callback,
const base::File::Info& file_info) {
......@@ -416,19 +671,22 @@ void MTPDeviceDelegateImplLinux::OnDidGetFileInfoToReadDirectory(
DCHECK(task_in_progress_);
if (!file_info.is_directory) {
return HandleDeviceFileError(error_callback,
dir_id,
base::File::FILE_ERROR_NOT_A_DIRECTORY);
}
base::Closure task_closure =
base::Bind(&ReadDirectoryOnUIThread,
storage_name_,
root,
dir_id,
base::Bind(&MTPDeviceDelegateImplLinux::OnDidReadDirectory,
weak_ptr_factory_.GetWeakPtr(),
dir_id,
success_callback),
base::Bind(&MTPDeviceDelegateImplLinux::HandleDeviceFileError,
weak_ptr_factory_.GetWeakPtr(),
error_callback));
error_callback,
dir_id));
content::BrowserThread::PostTask(content::BrowserThread::UI,
FROM_HERE,
task_closure);
......@@ -448,7 +706,9 @@ void MTPDeviceDelegateImplLinux::OnDidGetFileInfoToCreateSnapshotFile(
error = base::File::FILE_ERROR_FAILED;
if (error != base::File::FILE_OK)
return HandleDeviceFileError(snapshot_request_info->error_callback, error);
return HandleDeviceFileError(snapshot_request_info->error_callback,
snapshot_request_info->file_id,
error);
base::File::Info snapshot_file_info(file_info);
// Modify the last modified time to null. This prevents the time stamp
......@@ -465,10 +725,38 @@ void MTPDeviceDelegateImplLinux::OnDidGetFileInfoToCreateSnapshotFile(
}
void MTPDeviceDelegateImplLinux::OnDidReadDirectory(
uint32 dir_id,
const ReadDirectorySuccessCallback& success_callback,
const fileapi::AsyncFileUtil::EntryList& file_list) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
success_callback.Run(file_list, false /*no more entries*/);
FileIdToMTPFileNodeMap::iterator it = file_id_to_node_map_.find(dir_id);
DCHECK(it != file_id_to_node_map_.end());
MTPFileNode* dir_node = it->second;
std::set<std::string> children_to_keep;
fileapi::AsyncFileUtil::EntryList normalized_file_list;
for (size_t i = 0; i < file_list.size(); ++i) {
normalized_file_list.push_back(file_list[i]);
fileapi::DirectoryEntry& entry = normalized_file_list.back();
// |entry.name| has the file id encoded in it. Decode here.
size_t separator_idx = entry.name.find_last_of(',');
DCHECK_NE(std::string::npos, separator_idx);
std::string file_id_str = entry.name.substr(separator_idx);
file_id_str = file_id_str.substr(1); // Get rid of the comma.
uint32 file_id = 0;
bool ret = base::StringToUint(file_id_str, &file_id);
DCHECK(ret);
entry.name = entry.name.substr(0, separator_idx);
// Refresh the in memory tree.
dir_node->EnsureChildExists(entry.name, file_id);
children_to_keep.insert(entry.name);
}
dir_node->ClearNonexistentChildren(children_to_keep);
success_callback.Run(normalized_file_list, false /*no more entries*/);
PendingRequestDone();
}
......@@ -500,14 +788,111 @@ void MTPDeviceDelegateImplLinux::OnDidReadBytes(
PendingRequestDone();
}
void MTPDeviceDelegateImplLinux::OnDidFillFileCache(
const base::FilePath& path,
const fileapi::AsyncFileUtil::EntryList& /* file_list */,
bool /* has_more */) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
DCHECK(path.IsParent(pending_tasks_.front().path));
pending_tasks_.front().cached_path = path;
}
void MTPDeviceDelegateImplLinux::OnFillFileCacheFailed(
base::File::Error /* error */) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
// When filling the cache fails for the task at the front of the queue, clear
// the path of the task so it will not try to do any more caching. Instead,
// the task will just run and fail the CachedPathToId() lookup.
pending_tasks_.front().path.clear();
}
void MTPDeviceDelegateImplLinux::HandleDeviceFileError(
const ErrorCallback& error_callback,
uint32 file_id,
base::File::Error error) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
FileIdToMTPFileNodeMap::iterator it = file_id_to_node_map_.find(file_id);
if (it != file_id_to_node_map_.end()) {
MTPFileNode* parent = it->second->parent();
if (parent) {
bool ret = parent->DeleteChild(file_id);
DCHECK(ret);
}
}
error_callback.Run(error);
PendingRequestDone();
}
base::FilePath MTPDeviceDelegateImplLinux::NextUncachedPathComponent(
const base::FilePath& path,
const base::FilePath& cached_path) const {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
DCHECK(cached_path.empty() || cached_path.IsParent(path));
base::FilePath uncached_path;
std::string device_relpath = GetDeviceRelativePath(device_path_, path);
if (!device_relpath.empty() && device_relpath != kRootPath) {
uncached_path = device_path_;
std::vector<std::string> device_relpath_components;
base::SplitString(device_relpath, '/', &device_relpath_components);
DCHECK(!device_relpath_components.empty());
bool all_components_cached = true;
const MTPFileNode* current_node = root_node_.get();
for (size_t i = 0; i < device_relpath_components.size(); ++i) {
current_node = current_node->GetChild(device_relpath_components[i]);
if (!current_node) {
// With a cache miss, check if it is a genuine failure. If so, pretend
// the entire |path| is cached, so there is no further attempt to do
// more caching. The actual operation will then fail.
all_components_cached =
!cached_path.empty() && (uncached_path == cached_path);
break;
}
uncached_path = uncached_path.Append(device_relpath_components[i]);
}
if (all_components_cached)
uncached_path.clear();
}
return uncached_path;
}
void MTPDeviceDelegateImplLinux::FillFileCache(
const base::FilePath& uncached_path) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
DCHECK(task_in_progress_);
ReadDirectorySuccessCallback success_callback =
base::Bind(&MTPDeviceDelegateImplLinux::OnDidFillFileCache,
weak_ptr_factory_.GetWeakPtr(),
uncached_path);
ErrorCallback error_callback =
base::Bind(&MTPDeviceDelegateImplLinux::OnFillFileCacheFailed,
weak_ptr_factory_.GetWeakPtr());
ReadDirectoryInternal(uncached_path, success_callback, error_callback);
}
bool MTPDeviceDelegateImplLinux::CachedPathToId(const base::FilePath& path,
uint32* id) const {
DCHECK(id);
std::string device_relpath = GetDeviceRelativePath(device_path_, path);
if (device_relpath.empty())
return false;
std::vector<std::string> device_relpath_components;
if (device_relpath != kRootPath)
base::SplitString(device_relpath, '/', &device_relpath_components);
const MTPFileNode* current_node = root_node_.get();
for (size_t i = 0; i < device_relpath_components.size(); ++i) {
current_node = current_node->GetChild(device_relpath_components[i]);
if (!current_node)
return false;
}
*id = current_node->file_id();
return true;
}
void CreateMTPDeviceAsyncDelegate(
const std::string& device_location,
const CreateMTPDeviceAsyncDelegateCallback& callback) {
......
......@@ -5,19 +5,20 @@
#ifndef CHROME_BROWSER_MEDIA_GALLERIES_LINUX_MTP_DEVICE_DELEGATE_IMPL_LINUX_H_
#define CHROME_BROWSER_MEDIA_GALLERIES_LINUX_MTP_DEVICE_DELEGATE_IMPL_LINUX_H_
#include <queue>
#include <deque>
#include <map>
#include <string>
#include "base/callback.h"
#include "base/containers/scoped_ptr_hash_map.h"
#include "base/files/file_path.h"
#include "base/location.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/media_galleries/fileapi/mtp_device_async_delegate.h"
#include "content/public/browser/browser_thread.h"
#include "webkit/browser/fileapi/async_file_util.h"
namespace base {
class FilePath;
}
struct SnapshotRequestInfo;
// MTPDeviceDelegateImplLinux communicates with the media transfer protocol
......@@ -40,14 +41,24 @@ class MTPDeviceDelegateImplLinux : public MTPDeviceAsyncDelegate {
// Used to represent pending task details.
struct PendingTaskInfo {
PendingTaskInfo(const tracked_objects::Location& location,
PendingTaskInfo(const base::FilePath& path,
content::BrowserThread::ID thread_id,
const tracked_objects::Location& location,
const base::Closure& task);
~PendingTaskInfo();
base::FilePath path;
base::FilePath cached_path;
const content::BrowserThread::ID thread_id;
const tracked_objects::Location location;
const base::Closure task;
};
class MTPFileNode;
// Maps file ids to file nodes.
typedef std::map<uint32, MTPFileNode*> FileIdToMTPFileNodeMap;
// Should only be called by CreateMTPDeviceAsyncDelegate() factory call.
// Defer the device initializations until the first file operation request.
// Do all the initializations in EnsureInitAndRunTask() function.
......@@ -77,17 +88,41 @@ class MTPDeviceDelegateImplLinux : public MTPDeviceAsyncDelegate {
const ErrorCallback& error_callback) OVERRIDE;
virtual void CancelPendingTasksAndDeleteDelegate() OVERRIDE;
// Ensures the device is initialized for communication by doing a
// call-and-reply to the UI thread. |task_info.task| runs on the UI thread.
//
// If the device is already initialized, post the |task_info.task| immediately
// on the UI thread.
// The internal methods correspond to the similarly named methods above.
// The |root_node_| cache should be filled at this point.
virtual void GetFileInfoInternal(
const base::FilePath& file_path,
const GetFileInfoSuccessCallback& success_callback,
const ErrorCallback& error_callback);
virtual void ReadDirectoryInternal(
const base::FilePath& root,
const ReadDirectorySuccessCallback& success_callback,
const ErrorCallback& error_callback);
virtual void CreateSnapshotFileInternal(
const base::FilePath& device_file_path,
const base::FilePath& local_path,
const CreateSnapshotFileSuccessCallback& success_callback,
const ErrorCallback& error_callback);
virtual void ReadBytesInternal(
const base::FilePath& device_file_path,
net::IOBuffer* buf, int64 offset, int buf_len,
const ReadBytesSuccessCallback& success_callback,
const ErrorCallback& error_callback);
// Ensures the device is initialized for communication.
// If the device is already initialized, call RunTask().
//
// If the device is uninitialized, store the |task_info| in a pending task
// list and runs all the pending tasks once the device is successfully
// initialized.
// queue and runs the pending tasks in the queue once the device is
// successfully initialized.
void EnsureInitAndRunTask(const PendingTaskInfo& task_info);
// Runs a task. If |task_info.path| is empty, or if the path is cached, runs
// the task immediately.
// Otherwise, fills the cache first before running the task.
// |task_info.task| runs on the UI thread.
void RunTask(const PendingTaskInfo& task_info);
// Writes data from the device to the snapshot file path based on the
// parameters in |current_snapshot_request_info_| by doing a call-and-reply to
// the UI thread.
......@@ -114,16 +149,16 @@ class MTPDeviceDelegateImplLinux : public MTPDeviceAsyncDelegate {
const base::File::Info& file_info);
// Called when GetFileInfo() succeeds. GetFileInfo() is invoked to
// get the |root| directory metadata details. |file_info| specifies the |root|
// directory details.
// get the |dir_id| directory metadata details. |file_info| specifies the
// |dir_id| directory details.
//
// If |root| is a directory, post a task on the UI thread to read the |root|
// directory file entries.
// If |dir_id| is a directory, post a task on the UI thread to read the
// |dir_id| directory file entries.
//
// If |root| is not a directory, |error_callback| is invoked to notify the
// If |dir_id| is not a directory, |error_callback| is invoked to notify the
// caller about the file error and process the next pending request.
void OnDidGetFileInfoToReadDirectory(
const std::string& root,
uint32 dir_id,
const ReadDirectorySuccessCallback& success_callback,
const ErrorCallback& error_callback,
const base::File::Info& file_info);
......@@ -140,10 +175,12 @@ class MTPDeviceDelegateImplLinux : public MTPDeviceAsyncDelegate {
// Called when ReadDirectory() succeeds.
//
// |file_list| contains the directory file entries.
// |dir_id| is the directory read.
// |file_list| contains the directory file entries with their file ids.
// |success_callback| is invoked to notify the caller about the directory
// file entries.
void OnDidReadDirectory(const ReadDirectorySuccessCallback& success_callback,
void OnDidReadDirectory(uint32 dir_id,
const ReadDirectorySuccessCallback& success_callback,
const fileapi::AsyncFileUtil::EntryList& file_list);
// Called when WriteDataIntoSnapshotFile() succeeds.
......@@ -171,11 +208,34 @@ class MTPDeviceDelegateImplLinux : public MTPDeviceAsyncDelegate {
void OnDidReadBytes(const ReadBytesSuccessCallback& success_callback,
const base::File::Info& file_info, int bytes_read);
// Handles the device file |error|. |error_callback| is invoked to notify the
// caller about the file error.
// Called when FillFileCache() succeeds.
void OnDidFillFileCache(const base::FilePath& path,
const fileapi::AsyncFileUtil::EntryList& file_list,
bool has_more);
// Called when FillFileCache() fails.
void OnFillFileCacheFailed(base::File::Error error);
// Handles the device file |error| while operating on |file_id|.
// |error_callback| is invoked to notify the caller about the file error.
void HandleDeviceFileError(const ErrorCallback& error_callback,
uint32 file_id,
base::File::Error error);
// Given a full path, returns a non-empty sub-path that needs to be read into
// the cache if such a uncached path exists.
// |cached_path| is the portion of |path| that has had cache lookup attempts.
base::FilePath NextUncachedPathComponent(
const base::FilePath& path,
const base::FilePath& cached_path) const;
// Fills the file cache using the results from NextUncachedPathComponent().
void FillFileCache(const base::FilePath& uncached_path);
// Given a full path, if it exists in the cache, writes the file's id to |id|
// and return true.
bool CachedPathToId(const base::FilePath& path, uint32* id) const;
// MTP device initialization state.
InitializationState init_state_;
......@@ -193,7 +253,7 @@ class MTPDeviceDelegateImplLinux : public MTPDeviceAsyncDelegate {
// A list of pending tasks that needs to be run when the device is
// initialized or when the current task in progress is complete.
std::queue<PendingTaskInfo> pending_tasks_;
std::deque<PendingTaskInfo> pending_tasks_;
// Used to track the current snapshot file request. A snapshot file is created
// incrementally. CreateSnapshotFile request reads the device file and writes
......@@ -202,6 +262,15 @@ class MTPDeviceDelegateImplLinux : public MTPDeviceAsyncDelegate {
// request at any time.
scoped_ptr<SnapshotRequestInfo> current_snapshot_request_info_;
// A mapping for quick lookups into the |root_node_| tree structure. Since
// |root_node_| contains pointers to this map, it must be declared after this
// so destruction happens in the right order.
FileIdToMTPFileNodeMap file_id_to_node_map_;
// The root node of a tree-structure that caches the directory structure of
// the MTP device.
scoped_ptr<MTPFileNode> root_node_;
// For callbacks that may run after destruction.
base::WeakPtrFactory<MTPDeviceDelegateImplLinux> weak_ptr_factory_;
......
......@@ -8,6 +8,7 @@
#include "base/logging.h"
#include "base/numerics/safe_conversions.h"
#include "base/strings/string_number_conversions.h"
#include "chrome/browser/media_galleries/linux/mtp_device_object_enumerator.h"
#include "chrome/browser/media_galleries/linux/mtp_read_file_worker.h"
#include "chrome/browser/media_galleries/linux/snapshot_file_details.h"
......@@ -74,33 +75,33 @@ void MTPDeviceTaskHelper::OpenStorage(const std::string& storage_name,
callback));
}
void MTPDeviceTaskHelper::GetFileInfoByPath(
const std::string& file_path,
void MTPDeviceTaskHelper::GetFileInfoById(
uint32 file_id,
const GetFileInfoSuccessCallback& success_callback,
const ErrorCallback& error_callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (device_handle_.empty())
return HandleDeviceError(error_callback, base::File::FILE_ERROR_FAILED);
GetMediaTransferProtocolManager()->GetFileInfoByPath(
device_handle_, file_path,
GetMediaTransferProtocolManager()->GetFileInfoById(
device_handle_, file_id,
base::Bind(&MTPDeviceTaskHelper::OnGetFileInfo,
weak_ptr_factory_.GetWeakPtr(),
success_callback,
error_callback));
}
void MTPDeviceTaskHelper::ReadDirectoryByPath(
const std::string& dir_path,
void MTPDeviceTaskHelper::ReadDirectoryById(
uint32 dir_id,
const ReadDirectorySuccessCallback& success_callback,
const ErrorCallback& error_callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (device_handle_.empty())
return HandleDeviceError(error_callback, base::File::FILE_ERROR_FAILED);
GetMediaTransferProtocolManager()->ReadDirectoryByPath(
device_handle_, dir_path,
base::Bind(&MTPDeviceTaskHelper::OnDidReadDirectoryByPath,
GetMediaTransferProtocolManager()->ReadDirectoryById(
device_handle_, dir_id,
base::Bind(&MTPDeviceTaskHelper::OnDidReadDirectoryById,
weak_ptr_factory_.GetWeakPtr(),
success_callback,
error_callback));
......@@ -129,8 +130,8 @@ void MTPDeviceTaskHelper::ReadBytes(
base::File::FILE_ERROR_FAILED);
}
GetMediaTransferProtocolManager()->GetFileInfoByPath(
device_handle_, request.device_file_relative_path,
GetMediaTransferProtocolManager()->GetFileInfoById(
device_handle_, request.file_id,
base::Bind(&MTPDeviceTaskHelper::OnGetFileInfoToReadBytes,
weak_ptr_factory_.GetWeakPtr(), request));
}
......@@ -171,7 +172,7 @@ void MTPDeviceTaskHelper::OnGetFileInfo(
base::Bind(success_callback, FileInfoFromMTPFileEntry(file_entry)));
}
void MTPDeviceTaskHelper::OnDidReadDirectoryByPath(
void MTPDeviceTaskHelper::OnDidReadDirectoryById(
const ReadDirectorySuccessCallback& success_callback,
const ErrorCallback& error_callback,
const std::vector<MtpFileEntry>& file_entries,
......@@ -186,6 +187,11 @@ void MTPDeviceTaskHelper::OnDidReadDirectoryByPath(
while (!(current = file_enum.Next()).empty()) {
fileapi::DirectoryEntry entry;
entry.name = fileapi::VirtualPath::BaseName(current).value();
uint32 file_id = 0;
bool ret = file_enum.GetEntryId(&file_id);
DCHECK(ret);
entry.name.push_back(',');
entry.name += base::UintToString(file_id);
entry.is_directory = file_enum.IsDirectory();
entry.size = file_enum.Size();
entry.last_modified_time = file_enum.LastModifiedTime();
......@@ -202,7 +208,7 @@ void MTPDeviceTaskHelper::OnGetFileInfoToReadBytes(
bool error) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
DCHECK(request.buf);
DCHECK(request.buf_len >= 0);
DCHECK_GE(request.buf_len, 0);
DCHECK_GE(request.offset, 0);
if (error) {
return HandleDeviceError(request.error_callback,
......@@ -229,9 +235,9 @@ void MTPDeviceTaskHelper::OnGetFileInfoToReadBytes(
base::checked_cast<uint32>(request.buf_len),
base::saturated_cast<uint32>(file_info.size - request.offset));
GetMediaTransferProtocolManager()->ReadFileChunkByPath(
GetMediaTransferProtocolManager()->ReadFileChunkById(
device_handle_,
request.device_file_relative_path,
request.file_id,
base::checked_cast<uint32>(request.offset),
bytes_to_read,
base::Bind(&MTPDeviceTaskHelper::OnDidReadBytes,
......
......@@ -31,6 +31,8 @@ class MTPDeviceTaskHelper {
typedef MTPDeviceAsyncDelegate::GetFileInfoSuccessCallback
GetFileInfoSuccessCallback;
// NOTE: The file names in the entry list have their file id appended at the
// end. e.g. foo.jpg with file id 45 becomes foo.jpg,45.
typedef base::Callback<void(const fileapi::AsyncFileUtil::EntryList&)>
ReadDirectorySuccessCallback;
......@@ -48,32 +50,34 @@ class MTPDeviceTaskHelper {
void OpenStorage(const std::string& storage_name,
const OpenStorageCallback& callback);
// Dispatches the GetFileInfoByPath request to the
// Dispatches the GetFileInfoById request to the
// MediaTransferProtocolManager.
//
// |file_path| specifies the relative of the file whose details are requested.
// |file_id| specifies the id of the file whose details are requested.
//
// If the file details are fetched successfully, |success_callback| is invoked
// on the IO thread to notify the caller about the file details.
//
// If there is an error, |error_callback| is invoked on the IO thread to
// notify the caller about the file error.
void GetFileInfoByPath(
const std::string& file_path,
void GetFileInfoById(
uint32 file_id,
const GetFileInfoSuccessCallback& success_callback,
const ErrorCallback& error_callback);
// Dispatches the read directory request to the MediaTransferProtocolManager.
//
// |dir_path| specifies the directory file path.
// |dir_id| specifies the directory id.
//
// If the directory file entries are enumerated successfully,
// |success_callback| is invoked on the IO thread to notify the caller about
// the directory file entries.
// the directory file entries. Please see the note in the
// ReadDirectorySuccessCallback typedef regarding the special treatment of
// file names.
//
// If there is an error, |error_callback| is invoked on the IO thread to
// notify the caller about the file error.
void ReadDirectoryByPath(const std::string& dir_path,
void ReadDirectoryById(uint32 dir_id,
const ReadDirectorySuccessCallback& success_callback,
const ErrorCallback& error_callback);
......@@ -122,7 +126,7 @@ class MTPDeviceTaskHelper {
const MtpFileEntry& file_entry,
bool error) const;
// Query callback for ReadDirectoryByPath().
// Query callback for ReadDirectoryById().
//
// If there is no error, |error| is set to false, |file_entries| has the
// directory file entries and |success_callback| is invoked on the IO thread
......@@ -130,7 +134,7 @@ class MTPDeviceTaskHelper {
//
// If there is an error, |error| is set to true, |file_entries| is empty
// and |error_callback| is invoked on the IO thread to notify the caller.
void OnDidReadDirectoryByPath(
void OnDidReadDirectoryById(
const ReadDirectorySuccessCallback& success_callback,
const ErrorCallback& error_callback,
const std::vector<MtpFileEntry>& file_entries,
......
......@@ -66,9 +66,9 @@ void MTPReadFileWorker::ReadDataChunkFromDeviceFile(
device::MediaTransferProtocolManager* mtp_device_manager =
StorageMonitor::GetInstance()->media_transfer_protocol_manager();
mtp_device_manager->ReadFileChunkByPath(
mtp_device_manager->ReadFileChunkById(
device_handle_,
snapshot_file_details_ptr->device_file_path(),
snapshot_file_details_ptr->file_id(),
snapshot_file_details_ptr->bytes_written(),
snapshot_file_details_ptr->BytesToRead(),
base::Bind(&MTPReadFileWorker::OnDidReadDataChunkFromDeviceFile,
......
......@@ -11,12 +11,12 @@
////////////////////////////////////////////////////////////////////////////////
SnapshotRequestInfo::SnapshotRequestInfo(
const std::string& device_file_path,
uint32 file_id,
const base::FilePath& snapshot_file_path,
const MTPDeviceAsyncDelegate::CreateSnapshotFileSuccessCallback&
success_callback,
const MTPDeviceAsyncDelegate::ErrorCallback& error_callback)
: device_file_path(device_file_path),
: file_id(file_id),
snapshot_file_path(snapshot_file_path),
success_callback(success_callback),
error_callback(error_callback) {
......
......@@ -16,15 +16,15 @@
// Used to represent snapshot file request params.
struct SnapshotRequestInfo {
SnapshotRequestInfo(
const std::string& device_file_path,
uint32 file_id,
const base::FilePath& snapshot_file_path,
const MTPDeviceAsyncDelegate::CreateSnapshotFileSuccessCallback&
success_callback,
const MTPDeviceAsyncDelegate::ErrorCallback& error_callback);
~SnapshotRequestInfo();
// MTP device file path.
const std::string device_file_path;
// MTP device file id.
const uint32 file_id;
// Local platform path of the snapshot file.
const base::FilePath snapshot_file_path;
......@@ -47,8 +47,8 @@ class SnapshotFileDetails {
~SnapshotFileDetails();
std::string device_file_path() const {
return request_info_.device_file_path;
uint32 file_id() const {
return request_info_.file_id;
}
base::FilePath snapshot_file_path() const {
......
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