1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#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 <deque>
#include <map>
#include <set>
#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 "storage/browser/fileapi/async_file_util.h"
struct SnapshotRequestInfo;
// MTPDeviceDelegateImplLinux communicates with the media transfer protocol
// (MTP) device to complete file system operations. These operations are
// performed asynchronously. Instantiate this class per MTP device storage.
// MTPDeviceDelegateImplLinux lives on the IO thread.
// MTPDeviceDelegateImplLinux does a call-and-reply to the UI thread
// to dispatch the requests to MediaTransferProtocolManager.
class MTPDeviceDelegateImplLinux : public MTPDeviceAsyncDelegate {
private:
friend void CreateMTPDeviceAsyncDelegate(
const std::string&,
const CreateMTPDeviceAsyncDelegateCallback&);
enum InitializationState {
UNINITIALIZED = 0,
PENDING_INIT,
INITIALIZED
};
// Used to represent pending task details.
struct PendingTaskInfo {
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;
// Maps file paths to file info.
typedef std::map<base::FilePath, storage::DirectoryEntry> FileInfoCache;
// 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.
explicit MTPDeviceDelegateImplLinux(const std::string& device_location);
// Destructed via CancelPendingTasksAndDeleteDelegate().
~MTPDeviceDelegateImplLinux() override;
// MTPDeviceAsyncDelegate:
void GetFileInfo(const base::FilePath& file_path,
const GetFileInfoSuccessCallback& success_callback,
const ErrorCallback& error_callback) override;
void ReadDirectory(const base::FilePath& root,
const ReadDirectorySuccessCallback& success_callback,
const ErrorCallback& error_callback) override;
void CreateSnapshotFile(
const base::FilePath& device_file_path,
const base::FilePath& local_path,
const CreateSnapshotFileSuccessCallback& success_callback,
const ErrorCallback& error_callback) override;
bool IsStreaming() override;
void ReadBytes(const base::FilePath& device_file_path,
const scoped_refptr<net::IOBuffer>& buf,
int64 offset,
int buf_len,
const ReadBytesSuccessCallback& success_callback,
const ErrorCallback& error_callback) override;
void CancelPendingTasksAndDeleteDelegate() override;
// 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
// 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.
//
// |snapshot_file_info| specifies the metadata details of the snapshot file.
void WriteDataIntoSnapshotFile(const base::File::Info& snapshot_file_info);
// Marks the current request as complete and call ProcessNextPendingRequest().
void PendingRequestDone();
// Processes the next pending request.
void ProcessNextPendingRequest();
// Handles the device initialization event. |succeeded| indicates whether
// device initialization succeeded.
//
// If the device is successfully initialized, runs the next pending task.
void OnInitCompleted(bool succeeded);
// Called when GetFileInfo() succeeds. |file_info| specifies the
// requested file details. |success_callback| is invoked to notify the caller
// about the requested file details.
void OnDidGetFileInfo(const GetFileInfoSuccessCallback& success_callback,
const base::File::Info& file_info);
// Called when GetFileInfo() succeeds. GetFileInfo() is invoked to
// get the |dir_id| directory metadata details. |file_info| specifies the
// |dir_id| directory details.
//
// If |dir_id| is a directory, post a task on the UI thread to read the
// |dir_id| directory file entries.
//
// 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(
uint32 dir_id,
const ReadDirectorySuccessCallback& success_callback,
const ErrorCallback& error_callback,
const base::File::Info& file_info);
// Called when GetFileInfo() succeeds. GetFileInfo() is invoked to
// create the snapshot file of |snapshot_request_info.device_file_path|.
// |file_info| specifies the device file metadata details.
//
// Posts a task on the UI thread to copy the data contents of the device file
// to the snapshot file.
void OnDidGetFileInfoToCreateSnapshotFile(
scoped_ptr<SnapshotRequestInfo> snapshot_request_info,
const base::File::Info& file_info);
// Called when ReadDirectory() succeeds.
//
// |dir_id| is the directory read.
// |success_callback| is invoked to notify the caller about the directory
// file entries.
// |file_list| contains the directory file entries with their file ids.
// |has_more| is true if there are more file entries to read.
void OnDidReadDirectory(uint32 dir_id,
const ReadDirectorySuccessCallback& success_callback,
const storage::AsyncFileUtil::EntryList& file_list,
bool has_more);
// Called when WriteDataIntoSnapshotFile() succeeds.
//
// |snapshot_file_info| specifies the snapshot file metadata details.
//
// |current_snapshot_request_info_.success_callback| is invoked to notify the
// caller about |snapshot_file_info|.
void OnDidWriteDataIntoSnapshotFile(
const base::File::Info& snapshot_file_info,
const base::FilePath& snapshot_file_path);
// Called when WriteDataIntoSnapshotFile() fails.
//
// |error| specifies the file error code.
//
// |current_snapshot_request_info_.error_callback| is invoked to notify the
// caller about |error|.
void OnWriteDataIntoSnapshotFileError(base::File::Error error);
// Called when ReadBytes() succeeds.
//
// |success_callback| is invoked to notify the caller about the read bytes.
// |bytes_read| is the number of bytes read.
void OnDidReadBytes(const ReadBytesSuccessCallback& success_callback,
const base::File::Info& file_info, int bytes_read);
// Called when FillFileCache() succeeds.
void OnDidFillFileCache(const base::FilePath& path,
const storage::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_;
// Used to make sure only one task is in progress at any time.
// Otherwise the browser will try to send too many requests at once and
// overload the device.
bool task_in_progress_;
// Registered file system device path. This path does not
// correspond to a real device path (e.g. "/usb:2,2:81282").
const base::FilePath device_path_;
// MTP device storage name (e.g. "usb:2,2:81282").
std::string storage_name_;
// 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::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
// to the snapshot file in chunks. In order to retain the order of the
// snapshot file requests, make sure there is only one active snapshot file
// 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_;
// A list of child nodes encountered while a ReadDirectory operation, which
// can return results over multiple callbacks, is in progress.
std::set<std::string> child_nodes_seen_;
// A cache to store file metadata for file entries read during a ReadDirectory
// operation. Used to service incoming GetFileInfo calls for the duration of
// the ReadDirectory operation.
FileInfoCache file_info_cache_;
// For callbacks that may run after destruction.
base::WeakPtrFactory<MTPDeviceDelegateImplLinux> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(MTPDeviceDelegateImplLinux);
};
#endif // CHROME_BROWSER_MEDIA_GALLERIES_LINUX_MTP_DEVICE_DELEGATE_IMPL_LINUX_H_
|