12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifndef CHROME_BROWSER_MEDIA_GALLERIES_LINUX_MTP_READ_FILE_WORKER_H_
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define CHROME_BROWSER_MEDIA_GALLERIES_LINUX_MTP_READ_FILE_WORKER_H_
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <string>
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/callback.h"
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/files/file.h"
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/memory/scoped_ptr.h"
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/memory/weak_ptr.h"
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace base {
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class FilePath;
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class SnapshotFileDetails;
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)struct SnapshotRequestInfo;
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Worker class to copy the contents of the media transfer protocol(MTP) device
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// file to the given snapshot file.
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class MTPReadFileWorker {
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public:
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  explicit MTPReadFileWorker(const std::string& device_handle);
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ~MTPReadFileWorker();
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Dispatches the request to MediaTransferProtocolManager to get the media
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // file contents.
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // |request_info| specifies the snapshot file request params.
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // |snapshot_file_info| specifies the metadata of the snapshot file.
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void WriteDataIntoSnapshotFile(
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const SnapshotRequestInfo& request_info,
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const base::File::Info& snapshot_file_info);
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) private:
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Called when WriteDataIntoSnapshotFile() completes.
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // |snapshot_file_details| contains the current state of the snapshot file
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // (such as how many bytes written to the snapshot file, media device file
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // path, snapshot file path, bytes remaining, etc).
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If there is an error, |snapshot_file_details.error_callback| is invoked on
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // the IO thread to notify the caller about the failure.
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If there is no error, |snapshot_file_details.success_callback| is invoked
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // on the IO thread to notify the caller about the success.
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void OnDidWriteIntoSnapshotFile(
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      scoped_ptr<SnapshotFileDetails> snapshot_file_details);
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Dispatches the request to MediaTransferProtocolManager to get the device
542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // media file data chunk based on the parameters in |snapshot_file_details|.
552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void ReadDataChunkFromDeviceFile(
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      scoped_ptr<SnapshotFileDetails> snapshot_file_details);
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Called when ReadDataChunkFromDeviceFile() completes.
592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If there is no error, |data| will contain the data chunk and |error| is
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // set to false.
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If there is an error, |data| is empty and |error| is set to true.
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void OnDidReadDataChunkFromDeviceFile(
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      scoped_ptr<SnapshotFileDetails> snapshot_file_details,
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const std::string& data,
672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      bool error);
682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Called when the data chunk is written to the
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // |snapshot_file_details_.snapshot_file_path|.
712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If the write operation succeeds, |bytes_written| is set to a non-zero
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // value.
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If the write operation fails, |bytes_written| is set to zero.
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void OnDidWriteDataChunkIntoSnapshotFile(
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      scoped_ptr<SnapshotFileDetails> snapshot_file_details,
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      uint32 bytes_written);
792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The device unique identifier to query the device.
812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const std::string device_handle_;
822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // For callbacks that may run after destruction.
842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::WeakPtrFactory<MTPReadFileWorker> weak_ptr_factory_;
852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(MTPReadFileWorker);
872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif  // CHROME_BROWSER_MEDIA_GALLERIES_LINUX_MTP_READ_FILE_WORKER_H_
90