Home | History | Annotate | Download | only in win
      1 // Copyright (c) 2013 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 // This file has several utility functions to open a media transfer protocol
      6 // (MTP) device for communication, to enumerate the device contents, to read the
      7 // device file object, etc. All these tasks may take an arbitary long time
      8 // to complete. This file segregates those functionalities and runs them
      9 // in the blocking pool thread rather than in the UI thread.
     10 
     11 #ifndef CHROME_BROWSER_MEDIA_GALLERIES_WIN_MTP_DEVICE_OPERATIONS_UTIL_H_
     12 #define CHROME_BROWSER_MEDIA_GALLERIES_WIN_MTP_DEVICE_OPERATIONS_UTIL_H_
     13 
     14 #include <portabledeviceapi.h>
     15 
     16 #include <string>
     17 
     18 #include "base/files/file.h"
     19 #include "base/strings/string16.h"
     20 #include "base/win/scoped_comptr.h"
     21 #include "chrome/browser/media_galleries/win/mtp_device_object_entry.h"
     22 
     23 namespace media_transfer_protocol {
     24 
     25 // Opens the device for communication. |pnp_device_id| specifies the plug and
     26 // play device ID string. On success, returns the IPortableDevice interface.
     27 // On failure, returns NULL.
     28 base::win::ScopedComPtr<IPortableDevice> OpenDevice(
     29     const base::string16& pnp_device_id);
     30 
     31 // Gets the details of the object specified by |object_id| from the given MTP
     32 // |device|. On success, returns no error (base::File::FILE_OK) and fills in
     33 // |file_entry_info|. On failure, returns the corresponding platform file error
     34 // and |file_entry_info| is not set.
     35 base::File::Error GetFileEntryInfo(
     36     IPortableDevice* device,
     37     const base::string16& object_id,
     38     base::File::Info* file_entry_info);
     39 
     40 // Gets the entries of the directory specified by |directory_object_id| from
     41 // the given MTP |device|. On success, returns true and fills in
     42 // |object_entries|. On failure, returns false and |object_entries| is not
     43 // set.
     44 bool GetDirectoryEntries(IPortableDevice* device,
     45                          const base::string16& directory_object_id,
     46                          MTPDeviceObjectEntries* object_entries);
     47 
     48 // Gets an IStream interface to read the object content data from the |device|.
     49 // |file_object_id| specifies the device file object identifier.
     50 // On success, returns S_OK and sets |file_stream| and |optimal_transfer_size|.
     51 // On failure, returns an error code and |file_stream| and
     52 // |optimal_transfer_size| are not set.
     53 HRESULT GetFileStreamForObject(IPortableDevice* device,
     54                                const base::string16& file_object_id,
     55                                IStream** file_stream,
     56                                DWORD* optimal_transfer_size);
     57 
     58 // Copies a data chunk from |stream| to the file specified by the |local_path|.
     59 // |optimal_transfer_size| specifies the optimal data transfer size.
     60 //
     61 // On success, appends the data chunk in |local_path| and returns a non-zero
     62 // value indicating the total number of bytes written to the file specified
     63 // by the |local_path|. If the end of the |stream| is not reached,
     64 // the return value will be equal to |optimal_transfer_size|. If the end of the
     65 // |stream| is reached, the return value will be less than or equal to
     66 // |optimal_transfer_size|.
     67 //
     68 // On failure, returns 0.
     69 DWORD CopyDataChunkToLocalFile(IStream* stream,
     70                                const base::FilePath& local_path,
     71                                size_t optimal_transfer_size);
     72 
     73 // Returns the identifier of the object specified by the |object_name|.
     74 // |parent_id| specifies the object's parent identifier.
     75 // |object_name| specifies the friendly name of the object.
     76 base::string16 GetObjectIdFromName(IPortableDevice* device,
     77                                    const base::string16& parent_id,
     78                                    const base::string16& object_name);
     79 
     80 }  // namespace media_transfer_protocol
     81 
     82 #endif  // CHROME_BROWSER_MEDIA_GALLERIES_WIN_MTP_DEVICE_OPERATIONS_UTIL_H_
     83