summaryrefslogtreecommitdiff
path: root/chromium/third_party/mtpd/source/mtpd_server_impl.cc
blob: 068f1953a9e2916d93fd8b0a714fa2276e0859b9 (plain)
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
// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "mtpd_server_impl.h"

#include <base/logging.h>
#include <base/rand_util.h>
#include <base/stl_util.h>

#include "build_config.h"
#include "service_constants.h"

// TODO(thestig) Merge these once libchrome catches up to Chromium's base,
// or when mtpd moves into its own repo. http://crbug.com/221123
#if defined(CROS_BUILD)
#include <base/string_number_conversions.h>
#else
#include <base/strings/string_number_conversions.h>
#endif

namespace mtpd {

namespace {

// Maximum number of bytes to read from the device at one time. This is set low
// enough such that a reasonable device can read this much data before D-Bus
// times out.
const uint32_t kMaxReadCount = 1024 * 1024;

const char kInvalidHandleErrorMessage[] = "Invalid handle ";

void SetInvalidHandleError(const std::string& handle, DBus::Error* error) {
  std::string error_msg = kInvalidHandleErrorMessage + handle;
  error->set(kMtpdServiceError, error_msg.c_str());
}

template<typename ReturnType>
ReturnType InvalidHandle(const std::string& handle, DBus::Error* error) {
  SetInvalidHandleError(handle, error);
  return ReturnType();
}


}  // namespace

MtpdServer::MtpdServer(DBus::Connection& connection)
    : DBus::ObjectAdaptor(connection, kMtpdServicePath),
      device_manager_(this) {
}

MtpdServer::~MtpdServer() {
}

std::vector<std::string> MtpdServer::EnumerateStorages(DBus::Error& error) {
  return device_manager_.EnumerateStorages();
}

std::vector<uint8_t> MtpdServer::GetStorageInfo(const std::string& storageName,
                                                DBus::Error& error) {
  const StorageInfo* info = device_manager_.GetStorageInfo(storageName);
  return info ? info->ToDBusFormat() : StorageInfo().ToDBusFormat();
}

std::string MtpdServer::OpenStorage(const std::string& storageName,
                                    const std::string& mode,
                                    DBus::Error& error) {
  // TODO(thestig) Handle read-write and possibly append-only modes.
  if (mode != kReadOnlyMode) {
    std::string error_msg = "Cannot open " + storageName + " in mode: " + mode;
    error.set(kMtpdServiceError, error_msg.c_str());
    return std::string();
  }

  if (!device_manager_.HasStorage(storageName)) {
    std::string error_msg = "Cannot open unknown storage " + storageName;
    error.set(kMtpdServiceError, error_msg.c_str());
    return std::string();
  }

  std::string id;
  uint32_t random_data[4];
  do {
    base::RandBytes(random_data, sizeof(random_data));
    id = base::HexEncode(random_data, sizeof(random_data));
  } while (ContainsKey(handle_map_, id));

  handle_map_.insert(std::make_pair(id, storageName));
  return id;
}

void MtpdServer::CloseStorage(const std::string& handle, DBus::Error& error) {
  if (handle_map_.erase(handle) == 0)
    SetInvalidHandleError(handle, &error);
}

std::vector<uint8_t> MtpdServer::ReadDirectoryByPath(
    const std::string& handle,
    const std::string& filePath,
    DBus::Error& error) {
  std::string storage_name = LookupHandle(handle);
  if (storage_name.empty()) {
    SetInvalidHandleError(handle, &error);
    return FileEntry::EmptyFileEntriesToDBusFormat();
  }

  std::vector<FileEntry> directory_listing;
  if (!device_manager_.ReadDirectoryByPath(storage_name,
                                           filePath,
                                           &directory_listing)) {
    error.set(kMtpdServiceError, "ReadDirectoryByPath failed");
    return FileEntry::EmptyFileEntriesToDBusFormat();
  }
  return FileEntry::FileEntriesToDBusFormat(directory_listing);
}

std::vector<uint8_t> MtpdServer::ReadDirectoryById(const std::string& handle,
                                                   const uint32_t& fileId,
                                                   DBus::Error& error) {
  std::string storage_name = LookupHandle(handle);
  if (storage_name.empty()) {
    SetInvalidHandleError(handle, &error);
    return FileEntry::EmptyFileEntriesToDBusFormat();
  }

  std::vector<FileEntry> directory_listing;
  if (!device_manager_.ReadDirectoryById(storage_name,
                                         fileId,
                                         &directory_listing)) {
    error.set(kMtpdServiceError, "ReadDirectoryById failed");
    return FileEntry::EmptyFileEntriesToDBusFormat();
  }
  return FileEntry::FileEntriesToDBusFormat(directory_listing);
}

std::vector<uint8_t> MtpdServer::ReadFileChunkByPath(
    const std::string& handle,
    const std::string& filePath,
    const uint32_t& offset,
    const uint32_t& count,
    DBus::Error& error) {
  if (count > kMaxReadCount || count == 0) {
    error.set(kMtpdServiceError, "Invalid count for ReadFileChunkByPath");
    return std::vector<uint8_t>();
  }
  std::string storage_name = LookupHandle(handle);
  if (storage_name.empty())
    return InvalidHandle<std::vector<uint8_t> >(handle, &error);

  std::vector<uint8_t> file_contents;
  if (!device_manager_.ReadFileChunkByPath(storage_name, filePath, offset,
                                           count, &file_contents)) {
    error.set(kMtpdServiceError, "ReadFileChunkByPath failed");
    return std::vector<uint8_t>();
  }
  return file_contents;
}

std::vector<uint8_t> MtpdServer::ReadFileChunkById(const std::string& handle,
                                                   const uint32_t& fileId,
                                                   const uint32_t& offset,
                                                   const uint32_t& count,
                                                   DBus::Error& error) {
  if (count > kMaxReadCount || count == 0) {
    error.set(kMtpdServiceError, "Invalid count for ReadFileChunkById");
    return std::vector<uint8_t>();
  }
  std::string storage_name = LookupHandle(handle);
  if (storage_name.empty())
    return InvalidHandle<std::vector<uint8_t> >(handle, &error);

  std::vector<uint8_t> file_contents;
  if (!device_manager_.ReadFileChunkById(storage_name, fileId, offset, count,
                                         &file_contents)) {
    error.set(kMtpdServiceError, "ReadFileChunkById failed");
    return std::vector<uint8_t>();
  }
  return file_contents;
}

std::vector<uint8_t> MtpdServer::GetFileInfoByPath(const std::string& handle,
                                                   const std::string& filePath,
                                                   DBus::Error& error) {
  std::string storage_name = LookupHandle(handle);
  if (storage_name.empty()) {
    SetInvalidHandleError(handle, &error);
    return FileEntry().ToDBusFormat();
  }

  FileEntry entry;
  if (!device_manager_.GetFileInfoByPath(storage_name, filePath, &entry)) {
    error.set(kMtpdServiceError, "GetFileInfoByPath failed");
    return FileEntry().ToDBusFormat();
  }
  return entry.ToDBusFormat();
}

std::vector<uint8_t> MtpdServer::GetFileInfoById(const std::string& handle,
                                                 const uint32_t& fileId,
                                                 DBus::Error& error) {
  std::string storage_name = LookupHandle(handle);
  if (storage_name.empty()) {
    SetInvalidHandleError(handle, &error);
    return FileEntry().ToDBusFormat();
  }

  FileEntry entry;
  if (!device_manager_.GetFileInfoById(storage_name, fileId, &entry)) {
    error.set(kMtpdServiceError, "GetFileInfoById failed");
    return FileEntry().ToDBusFormat();
  }
  return entry.ToDBusFormat();
}

bool MtpdServer::IsAlive(DBus::Error& error) {
  return true;
}

void MtpdServer::StorageAttached(const std::string& storage_name) {
  // Fire DBus signal.
  MTPStorageAttached(storage_name);
}

void MtpdServer::StorageDetached(const std::string& storage_name) {
  // Fire DBus signal.
  MTPStorageDetached(storage_name);
}

int MtpdServer::GetDeviceEventDescriptor() const {
  return device_manager_.GetDeviceEventDescriptor();
}

void MtpdServer::ProcessDeviceEvents() {
  device_manager_.ProcessDeviceEvents();
}

std::string MtpdServer::LookupHandle(const std::string& handle) {
  HandleMap::const_iterator it = handle_map_.find(handle);
  return (it == handle_map_.end()) ? std::string() : it->second;
}

}  // namespace mtpd