1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CHROME_BROWSER_MEDIA_GALLERIES_LINUX_MTP_READ_FILE_WORKER_H_
6#define CHROME_BROWSER_MEDIA_GALLERIES_LINUX_MTP_READ_FILE_WORKER_H_
7
8#include <string>
9
10#include "base/callback.h"
11#include "base/files/file.h"
12#include "base/memory/scoped_ptr.h"
13#include "base/memory/weak_ptr.h"
14
15namespace base {
16class FilePath;
17}
18
19class SnapshotFileDetails;
20struct SnapshotRequestInfo;
21
22// Worker class to copy the contents of the media transfer protocol(MTP) device
23// file to the given snapshot file.
24class MTPReadFileWorker {
25 public:
26  explicit MTPReadFileWorker(const std::string& device_handle);
27  ~MTPReadFileWorker();
28
29  // Dispatches the request to MediaTransferProtocolManager to get the media
30  // file contents.
31  //
32  // |request_info| specifies the snapshot file request params.
33  // |snapshot_file_info| specifies the metadata of the snapshot file.
34  void WriteDataIntoSnapshotFile(
35      const SnapshotRequestInfo& request_info,
36      const base::File::Info& snapshot_file_info);
37
38 private:
39  // Called when WriteDataIntoSnapshotFile() completes.
40  //
41  // |snapshot_file_details| contains the current state of the snapshot file
42  // (such as how many bytes written to the snapshot file, media device file
43  // path, snapshot file path, bytes remaining, etc).
44  //
45  // If there is an error, |snapshot_file_details.error_callback| is invoked on
46  // the IO thread to notify the caller about the failure.
47  //
48  // If there is no error, |snapshot_file_details.success_callback| is invoked
49  // on the IO thread to notify the caller about the success.
50  void OnDidWriteIntoSnapshotFile(
51      scoped_ptr<SnapshotFileDetails> snapshot_file_details);
52
53  // Dispatches the request to MediaTransferProtocolManager to get the device
54  // media file data chunk based on the parameters in |snapshot_file_details|.
55  void ReadDataChunkFromDeviceFile(
56      scoped_ptr<SnapshotFileDetails> snapshot_file_details);
57
58  // Called when ReadDataChunkFromDeviceFile() completes.
59  //
60  // If there is no error, |data| will contain the data chunk and |error| is
61  // set to false.
62  //
63  // If there is an error, |data| is empty and |error| is set to true.
64  void OnDidReadDataChunkFromDeviceFile(
65      scoped_ptr<SnapshotFileDetails> snapshot_file_details,
66      const std::string& data,
67      bool error);
68
69  // Called when the data chunk is written to the
70  // |snapshot_file_details_.snapshot_file_path|.
71  //
72  // If the write operation succeeds, |bytes_written| is set to a non-zero
73  // value.
74  //
75  // If the write operation fails, |bytes_written| is set to zero.
76  void OnDidWriteDataChunkIntoSnapshotFile(
77      scoped_ptr<SnapshotFileDetails> snapshot_file_details,
78      uint32 bytes_written);
79
80  // The device unique identifier to query the device.
81  const std::string device_handle_;
82
83  // For callbacks that may run after destruction.
84  base::WeakPtrFactory<MTPReadFileWorker> weak_ptr_factory_;
85
86  DISALLOW_COPY_AND_ASSIGN(MTPReadFileWorker);
87};
88
89#endif  // CHROME_BROWSER_MEDIA_GALLERIES_LINUX_MTP_READ_FILE_WORKER_H_
90