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.
5 #ifndef DEVICE_MEDIA_TRANSFER_PROTOCOL_MEDIA_TRANSFER_PROTOCOL_MANAGER_H_
6 #define DEVICE_MEDIA_TRANSFER_PROTOCOL_MEDIA_TRANSFER_PROTOCOL_MANAGER_H_
11 #include "base/callback.h"
12 #include "base/memory/ref_counted.h"
13 #include "build/build_config.h"
15 #if !defined(OS_LINUX)
16 #error "Only used on Linux and ChromeOS"
23 class SequencedTaskRunner
;
28 // This class handles the interaction with mtpd.
29 // Other classes can add themselves as observers.
30 class MediaTransferProtocolManager
{
32 // A callback to handle the result of OpenStorage.
33 // The first argument is the returned handle.
34 // The second argument is true if there was an error.
35 typedef base::Callback
<void(const std::string
& handle
,
36 bool error
)> OpenStorageCallback
;
38 // A callback to handle the result of CloseStorage.
39 // The argument is true if there was an error.
40 typedef base::Callback
<void(bool error
)> CloseStorageCallback
;
42 // A callback to handle the result of ReadDirectoryByPath/Id.
43 // The first argument is a vector of file entries.
44 // The second argument is true if there was an error.
45 typedef base::Callback
<void(const std::vector
<MtpFileEntry
>& file_entries
,
46 bool error
)> ReadDirectoryCallback
;
48 // A callback to handle the result of ReadFileChunkByPath/Id.
49 // The first argument is a string containing the file data.
50 // The second argument is true if there was an error.
51 typedef base::Callback
<void(const std::string
& data
,
52 bool error
)> ReadFileCallback
;
54 // A callback to handle the result of GetFileInfoByPath/Id.
55 // The first argument is a file entry.
56 // The second argument is true if there was an error.
57 typedef base::Callback
<void(const MtpFileEntry
& file_entry
,
58 bool error
)> GetFileInfoCallback
;
60 // Implement this interface to be notified about MTP storage
61 // attachment / detachment events.
64 virtual ~Observer() {}
66 // A function called after a MTP storage has been attached / detached.
67 virtual void StorageChanged(bool is_attached
,
68 const std::string
& storage_name
) = 0;
71 virtual ~MediaTransferProtocolManager() {}
74 virtual void AddObserver(Observer
* observer
) = 0;
76 // Removes an observer.
77 virtual void RemoveObserver(Observer
* observer
) = 0;
79 // Returns a vector of available MTP storages.
80 virtual const std::vector
<std::string
> GetStorages() const = 0;
82 // On success, returns the the metadata for |storage_name|.
83 // Otherwise returns NULL.
84 virtual const MtpStorageInfo
* GetStorageInfo(
85 const std::string
& storage_name
) const = 0;
87 // Opens |storage_name| in |mode| and runs |callback|.
88 virtual void OpenStorage(const std::string
& storage_name
,
89 const std::string
& mode
,
90 const OpenStorageCallback
& callback
) = 0;
92 // Close |storage_handle| and runs |callback|.
93 virtual void CloseStorage(const std::string
& storage_handle
,
94 const CloseStorageCallback
& callback
) = 0;
96 // Reads directory entries from |path| on |storage_handle| and runs
98 virtual void ReadDirectoryByPath(const std::string
& storage_handle
,
99 const std::string
& path
,
100 const ReadDirectoryCallback
& callback
) = 0;
102 // Reads directory entries from |file_id| on |storage_handle| and runs
104 virtual void ReadDirectoryById(const std::string
& storage_handle
,
106 const ReadDirectoryCallback
& callback
) = 0;
108 // Reads file data from |path| on |storage_handle| and runs |callback|.
109 // Reads |count| bytes of data starting at |offset|.
110 virtual void ReadFileChunkByPath(const std::string
& storage_handle
,
111 const std::string
& path
,
114 const ReadFileCallback
& callback
) = 0;
116 // Reads file data from |file_id| on |storage_handle| and runs |callback|.
117 // Reads |count| bytes of data starting at |offset|.
118 virtual void ReadFileChunkById(const std::string
& storage_handle
,
122 const ReadFileCallback
& callback
) = 0;
124 // Gets the file metadata for |path| on |storage_handle| and runs |callback|.
125 virtual void GetFileInfoByPath(const std::string
& storage_handle
,
126 const std::string
& path
,
127 const GetFileInfoCallback
& callback
) = 0;
129 // Gets the file metadata for |file_id| on |storage_handle| and runs
131 virtual void GetFileInfoById(const std::string
& storage_handle
,
133 const GetFileInfoCallback
& callback
) = 0;
135 // Creates and returns the global MediaTransferProtocolManager instance.
136 // On Linux, |task_runner| specifies the task runner to process asynchronous
138 // On ChromeOS, |task_runner| should just be set to NULL because ChromeOS
139 // already has a dedicated message loop proxy.
140 static MediaTransferProtocolManager
* Initialize(
141 scoped_refptr
<base::SequencedTaskRunner
> task_runner
);
144 } // namespace device
146 #endif // DEVICE_MEDIA_TRANSFER_PROTOCOL_MEDIA_TRANSFER_PROTOCOL_MANAGER_H_