blob: 8dda349976f08736dbba4605c3684a92f637bbe1 [file] [log] [blame]
[email protected]2321d282012-01-31 23:06:591// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]4ae73292011-11-15 05:20:182// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]64e199252012-04-06 01:54:365#include "chromeos/dbus/cros_disks_client.h"
[email protected]4ae73292011-11-15 05:20:186
avi6e1a22d2015-12-21 03:43:207#include <stddef.h>
8#include <stdint.h>
9
[email protected]85b95a2012012-08-07 18:57:2710#include <map>
11
[email protected]4ae73292011-11-15 05:20:1812#include "base/bind.h"
[email protected]a5a8b412013-03-04 15:03:1113#include "base/files/file_path.h"
thestigb44bd352014-09-10 01:47:0614#include "base/files/file_util.h"
[email protected]593cf3b2013-03-05 04:16:5215#include "base/location.h"
avi6e1a22d2015-12-21 03:43:2016#include "base/macros.h"
[email protected]b307bceb2011-11-17 07:49:5517#include "base/stl_util.h"
[email protected]afa339d72013-06-11 06:32:5118#include "base/strings/stringprintf.h"
[email protected]49c4cf852013-09-27 19:28:2419#include "base/sys_info.h"
[email protected]593cf3b2013-03-05 04:16:5220#include "base/task_runner_util.h"
21#include "base/threading/worker_pool.h"
[email protected]81836aed2013-07-09 23:41:1222#include "base/values.h"
satorux8fd293802014-10-30 08:23:1223#include "chromeos/dbus/fake_cros_disks_client.h"
[email protected]4ae73292011-11-15 05:20:1824#include "dbus/bus.h"
25#include "dbus/message.h"
[email protected]216ed0b2012-02-14 21:29:0626#include "dbus/object_path.h"
[email protected]4ae73292011-11-15 05:20:1827#include "dbus/object_proxy.h"
[email protected]81836aed2013-07-09 23:41:1228#include "dbus/values_util.h"
[email protected]4ae73292011-11-15 05:20:1829#include "third_party/cros_system_api/dbus/service_constants.h"
30
31namespace chromeos {
32
33namespace {
34
35const char* kDefaultMountOptions[] = {
36 "rw",
37 "nodev",
38 "noexec",
39 "nosuid",
[email protected]4ae73292011-11-15 05:20:1840};
41
42const char* kDefaultUnmountOptions[] = {
43 "force",
44};
45
[email protected]10795ae2012-10-10 07:33:4946const char kLazyUnmountOption[] = "lazy";
47
[email protected]dcad8fc2012-04-30 23:31:3348const char kMountLabelOption[] = "mountlabel";
49
[email protected]2321d282012-01-31 23:06:5950// Checks if retrieved media type is in boundaries of DeviceMediaType.
avi6e1a22d2015-12-21 03:43:2051bool IsValidMediaType(uint32_t type) {
52 return type < static_cast<uint32_t>(cros_disks::DEVICE_MEDIA_NUM_VALUES);
[email protected]2321d282012-01-31 23:06:5953}
54
[email protected]2321d282012-01-31 23:06:5955// Translates enum used in cros-disks to enum used in Chrome.
56// Note that we could just do static_cast, but this is less sensitive to
57// changes in cros-disks.
avi6e1a22d2015-12-21 03:43:2058DeviceType DeviceMediaTypeToDeviceType(uint32_t media_type_uint32) {
[email protected]2321d282012-01-31 23:06:5959 if (!IsValidMediaType(media_type_uint32))
60 return DEVICE_TYPE_UNKNOWN;
61
62 cros_disks::DeviceMediaType media_type =
63 cros_disks::DeviceMediaType(media_type_uint32);
64
65 switch (media_type) {
66 case(cros_disks::DEVICE_MEDIA_UNKNOWN):
67 return DEVICE_TYPE_UNKNOWN;
68 case(cros_disks::DEVICE_MEDIA_USB):
69 return DEVICE_TYPE_USB;
70 case(cros_disks::DEVICE_MEDIA_SD):
71 return DEVICE_TYPE_SD;
72 case(cros_disks::DEVICE_MEDIA_OPTICAL_DISC):
73 return DEVICE_TYPE_OPTICAL_DISC;
74 case(cros_disks::DEVICE_MEDIA_MOBILE):
75 return DEVICE_TYPE_MOBILE;
[email protected]f4ae40ac2012-05-04 21:57:0076 case(cros_disks::DEVICE_MEDIA_DVD):
77 return DEVICE_TYPE_DVD;
[email protected]2321d282012-01-31 23:06:5978 default:
79 return DEVICE_TYPE_UNKNOWN;
80 }
[email protected]4ae73292011-11-15 05:20:1881}
82
[email protected]d7760592014-05-16 07:57:5283bool ReadMountEntryFromDbus(dbus::MessageReader* reader, MountEntry* entry) {
avi6e1a22d2015-12-21 03:43:2084 uint32_t error_code = 0;
[email protected]d7760592014-05-16 07:57:5285 std::string source_path;
avi6e1a22d2015-12-21 03:43:2086 uint32_t mount_type = 0;
[email protected]d7760592014-05-16 07:57:5287 std::string mount_path;
88 if (!reader->PopUint32(&error_code) ||
89 !reader->PopString(&source_path) ||
90 !reader->PopUint32(&mount_type) ||
91 !reader->PopString(&mount_path)) {
92 return false;
93 }
94 *entry = MountEntry(static_cast<MountError>(error_code), source_path,
95 static_cast<MountType>(mount_type), mount_path);
96 return true;
97}
98
[email protected]4ae73292011-11-15 05:20:1899// The CrosDisksClient implementation.
100class CrosDisksClientImpl : public CrosDisksClient {
101 public:
[email protected]c5fd5362013-08-27 12:23:04102 CrosDisksClientImpl() : proxy_(NULL), weak_ptr_factory_(this) {}
[email protected]4ae73292011-11-15 05:20:18103
104 // CrosDisksClient override.
dcheng0280cb62015-01-16 07:37:50105 void Mount(const std::string& source_path,
106 const std::string& source_format,
107 const std::string& mount_label,
108 const base::Closure& callback,
109 const base::Closure& error_callback) override {
[email protected]4ae73292011-11-15 05:20:18110 dbus::MethodCall method_call(cros_disks::kCrosDisksInterface,
111 cros_disks::kMount);
112 dbus::MessageWriter writer(&method_call);
113 writer.AppendString(source_path);
[email protected]b9f22d12012-04-25 21:46:48114 writer.AppendString(source_format);
[email protected]4ae73292011-11-15 05:20:18115 std::vector<std::string> mount_options(kDefaultMountOptions,
116 kDefaultMountOptions +
117 arraysize(kDefaultMountOptions));
[email protected]dcad8fc2012-04-30 23:31:33118 if (!mount_label.empty()) {
119 std::string mount_label_option = base::StringPrintf("%s=%s",
120 kMountLabelOption,
121 mount_label.c_str());
122 mount_options.push_back(mount_label_option);
123 }
[email protected]4ae73292011-11-15 05:20:18124 writer.AppendArrayOfStrings(mount_options);
125 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
126 base::Bind(&CrosDisksClientImpl::OnMount,
127 weak_ptr_factory_.GetWeakPtr(),
128 callback,
129 error_callback));
130 }
131
132 // CrosDisksClient override.
dcheng0280cb62015-01-16 07:37:50133 void Unmount(const std::string& device_path,
134 UnmountOptions options,
135 const base::Closure& callback,
136 const base::Closure& error_callback) override {
[email protected]4ae73292011-11-15 05:20:18137 dbus::MethodCall method_call(cros_disks::kCrosDisksInterface,
138 cros_disks::kUnmount);
139 dbus::MessageWriter writer(&method_call);
140 writer.AppendString(device_path);
[email protected]10795ae2012-10-10 07:33:49141
142 std::vector<std::string> unmount_options(
143 kDefaultUnmountOptions,
144 kDefaultUnmountOptions + arraysize(kDefaultUnmountOptions));
145 if (options == UNMOUNT_OPTIONS_LAZY)
146 unmount_options.push_back(kLazyUnmountOption);
147
[email protected]4ae73292011-11-15 05:20:18148 writer.AppendArrayOfStrings(unmount_options);
149 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
150 base::Bind(&CrosDisksClientImpl::OnUnmount,
151 weak_ptr_factory_.GetWeakPtr(),
[email protected]4ae73292011-11-15 05:20:18152 callback,
153 error_callback));
154 }
155
156 // CrosDisksClient override.
dcheng0280cb62015-01-16 07:37:50157 void EnumerateAutoMountableDevices(
[email protected]4a404e52012-04-11 02:25:35158 const EnumerateAutoMountableDevicesCallback& callback,
mostynb4f4cf142014-10-06 13:57:52159 const base::Closure& error_callback) override {
[email protected]4ae73292011-11-15 05:20:18160 dbus::MethodCall method_call(cros_disks::kCrosDisksInterface,
161 cros_disks::kEnumerateAutoMountableDevices);
162 proxy_->CallMethod(
163 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
164 base::Bind(&CrosDisksClientImpl::OnEnumerateAutoMountableDevices,
165 weak_ptr_factory_.GetWeakPtr(),
166 callback,
167 error_callback));
168 }
169
170 // CrosDisksClient override.
dcheng0280cb62015-01-16 07:37:50171 void EnumerateMountEntries(const EnumerateMountEntriesCallback& callback,
172 const base::Closure& error_callback) override {
[email protected]d7760592014-05-16 07:57:52173 dbus::MethodCall method_call(cros_disks::kCrosDisksInterface,
174 cros_disks::kEnumerateMountEntries);
175 proxy_->CallMethod(
176 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
177 base::Bind(&CrosDisksClientImpl::OnEnumerateMountEntries,
178 weak_ptr_factory_.GetWeakPtr(),
179 callback,
180 error_callback));
181 }
182
183 // CrosDisksClient override.
dcheng0280cb62015-01-16 07:37:50184 void Format(const std::string& device_path,
185 const std::string& filesystem,
186 const base::Closure& callback,
187 const base::Closure& error_callback) override {
[email protected]4ae73292011-11-15 05:20:18188 dbus::MethodCall method_call(cros_disks::kCrosDisksInterface,
[email protected]f026c0f2014-05-06 21:52:35189 cros_disks::kFormat);
[email protected]4ae73292011-11-15 05:20:18190 dbus::MessageWriter writer(&method_call);
191 writer.AppendString(device_path);
192 writer.AppendString(filesystem);
[email protected]f026c0f2014-05-06 21:52:35193 // No format option is currently specified, but we can later use this
194 // argument to specify options for the format operation.
195 std::vector<std::string> format_options;
196 writer.AppendArrayOfStrings(format_options);
[email protected]4ae73292011-11-15 05:20:18197 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
[email protected]f026c0f2014-05-06 21:52:35198 base::Bind(&CrosDisksClientImpl::OnFormat,
[email protected]4ae73292011-11-15 05:20:18199 weak_ptr_factory_.GetWeakPtr(),
[email protected]4ae73292011-11-15 05:20:18200 callback,
201 error_callback));
202 }
203
204 // CrosDisksClient override.
dcheng0280cb62015-01-16 07:37:50205 void GetDeviceProperties(const std::string& device_path,
206 const GetDevicePropertiesCallback& callback,
207 const base::Closure& error_callback) override {
[email protected]4ae73292011-11-15 05:20:18208 dbus::MethodCall method_call(cros_disks::kCrosDisksInterface,
209 cros_disks::kGetDeviceProperties);
210 dbus::MessageWriter writer(&method_call);
211 writer.AppendString(device_path);
212 proxy_->CallMethod(&method_call,
213 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
214 base::Bind(&CrosDisksClientImpl::OnGetDeviceProperties,
215 weak_ptr_factory_.GetWeakPtr(),
216 device_path,
217 callback,
218 error_callback));
219 }
220
221 // CrosDisksClient override.
dcheng0280cb62015-01-16 07:37:50222 void SetMountEventHandler(
mostynb4f4cf142014-10-06 13:57:52223 const MountEventHandler& mount_event_handler) override {
[email protected]4ae73292011-11-15 05:20:18224 static const SignalEventTuple kSignalEventTuples[] = {
[email protected]e3c1fc92012-11-15 00:56:46225 { cros_disks::kDeviceAdded, CROS_DISKS_DEVICE_ADDED },
226 { cros_disks::kDeviceScanned, CROS_DISKS_DEVICE_SCANNED },
227 { cros_disks::kDeviceRemoved, CROS_DISKS_DEVICE_REMOVED },
228 { cros_disks::kDiskAdded, CROS_DISKS_DISK_ADDED },
229 { cros_disks::kDiskChanged, CROS_DISKS_DISK_CHANGED },
230 { cros_disks::kDiskRemoved, CROS_DISKS_DISK_REMOVED },
[email protected]4ae73292011-11-15 05:20:18231 };
232 const size_t kNumSignalEventTuples = arraysize(kSignalEventTuples);
233
234 for (size_t i = 0; i < kNumSignalEventTuples; ++i) {
235 proxy_->ConnectToSignal(
236 cros_disks::kCrosDisksInterface,
237 kSignalEventTuples[i].signal_name,
238 base::Bind(&CrosDisksClientImpl::OnMountEvent,
239 weak_ptr_factory_.GetWeakPtr(),
240 kSignalEventTuples[i].event_type,
241 mount_event_handler),
242 base::Bind(&CrosDisksClientImpl::OnSignalConnected,
243 weak_ptr_factory_.GetWeakPtr()));
244 }
[email protected]a0278d52014-05-06 03:36:15245 }
246
247 // CrosDisksClient override.
dcheng0280cb62015-01-16 07:37:50248 void SetMountCompletedHandler(
mostynb4f4cf142014-10-06 13:57:52249 const MountCompletedHandler& mount_completed_handler) override {
[email protected]4ae73292011-11-15 05:20:18250 proxy_->ConnectToSignal(
251 cros_disks::kCrosDisksInterface,
[email protected]b3e3f492011-11-18 18:46:00252 cros_disks::kMountCompleted,
[email protected]4ae73292011-11-15 05:20:18253 base::Bind(&CrosDisksClientImpl::OnMountCompleted,
254 weak_ptr_factory_.GetWeakPtr(),
[email protected]85b95a2012012-08-07 18:57:27255 mount_completed_handler),
[email protected]4ae73292011-11-15 05:20:18256 base::Bind(&CrosDisksClientImpl::OnSignalConnected,
257 weak_ptr_factory_.GetWeakPtr()));
258 }
259
[email protected]a0278d52014-05-06 03:36:15260 // CrosDisksClient override.
dcheng0280cb62015-01-16 07:37:50261 void SetFormatCompletedHandler(
mostynb4f4cf142014-10-06 13:57:52262 const FormatCompletedHandler& format_completed_handler) override {
[email protected]a0278d52014-05-06 03:36:15263 proxy_->ConnectToSignal(
264 cros_disks::kCrosDisksInterface,
265 cros_disks::kFormatCompleted,
266 base::Bind(&CrosDisksClientImpl::OnFormatCompleted,
267 weak_ptr_factory_.GetWeakPtr(),
268 format_completed_handler),
269 base::Bind(&CrosDisksClientImpl::OnSignalConnected,
270 weak_ptr_factory_.GetWeakPtr()));
271 }
272
[email protected]c5fd5362013-08-27 12:23:04273 protected:
dcheng0280cb62015-01-16 07:37:50274 void Init(dbus::Bus* bus) override {
[email protected]c5fd5362013-08-27 12:23:04275 proxy_ = bus->GetObjectProxy(
276 cros_disks::kCrosDisksServiceName,
277 dbus::ObjectPath(cros_disks::kCrosDisksServicePath));
278 }
279
[email protected]4ae73292011-11-15 05:20:18280 private:
281 // A struct to contain a pair of signal name and mount event type.
[email protected]a0278d52014-05-06 03:36:15282 // Used by SetMountEventHandler.
[email protected]4ae73292011-11-15 05:20:18283 struct SignalEventTuple {
284 const char *signal_name;
285 MountEventType event_type;
286 };
287
288 // Handles the result of Mount and calls |callback| or |error_callback|.
[email protected]5624a252013-07-04 03:17:53289 void OnMount(const base::Closure& callback,
290 const base::Closure& error_callback,
[email protected]4ae73292011-11-15 05:20:18291 dbus::Response* response) {
292 if (!response) {
293 error_callback.Run();
294 return;
295 }
296 callback.Run();
297 }
298
[email protected]d7760592014-05-16 07:57:52299 // Handles the result of Unmount and calls |callback| or |error_callback|.
[email protected]ffdcc7a9c2013-07-02 06:59:39300 void OnUnmount(const base::Closure& callback,
301 const base::Closure& error_callback,
[email protected]4ae73292011-11-15 05:20:18302 dbus::Response* response) {
303 if (!response) {
[email protected]ffdcc7a9c2013-07-02 06:59:39304 error_callback.Run();
[email protected]4ae73292011-11-15 05:20:18305 return;
306 }
[email protected]ddcb18e2013-09-19 07:17:28307
308 // Temporarly allow Unmount method to report failure both by setting dbus
309 // error (in which case response is not set) and by returning mount error
310 // different from MOUNT_ERROR_NONE. This is done so we can change Unmount
311 // method to return mount error (http://crbug.com/288974) without breaking
312 // Chrome.
313 // TODO(tbarzic): When Unmount implementation is changed on cros disks side,
314 // make this fail if reader is not able to read the error code value from
315 // the response.
316 dbus::MessageReader reader(response);
avi6e1a22d2015-12-21 03:43:20317 uint32_t error_code = 0;
[email protected]ddcb18e2013-09-19 07:17:28318 if (reader.PopUint32(&error_code) &&
319 static_cast<MountError>(error_code) != MOUNT_ERROR_NONE) {
320 error_callback.Run();
321 return;
322 }
323
[email protected]ffdcc7a9c2013-07-02 06:59:39324 callback.Run();
[email protected]4ae73292011-11-15 05:20:18325 }
326
327 // Handles the result of EnumerateAutoMountableDevices and calls |callback| or
328 // |error_callback|.
329 void OnEnumerateAutoMountableDevices(
[email protected]4a404e52012-04-11 02:25:35330 const EnumerateAutoMountableDevicesCallback& callback,
[email protected]5624a252013-07-04 03:17:53331 const base::Closure& error_callback,
[email protected]4ae73292011-11-15 05:20:18332 dbus::Response* response) {
333 if (!response) {
334 error_callback.Run();
335 return;
336 }
337 dbus::MessageReader reader(response);
338 std::vector<std::string> device_paths;
339 if (!reader.PopArrayOfStrings(&device_paths)) {
340 LOG(ERROR) << "Invalid response: " << response->ToString();
341 error_callback.Run();
342 return;
343 }
344 callback.Run(device_paths);
345 }
346
[email protected]d7760592014-05-16 07:57:52347 // Handles the result of EnumerateMountEntries and calls |callback| or
348 // |error_callback|.
349 void OnEnumerateMountEntries(
350 const EnumerateMountEntriesCallback& callback,
351 const base::Closure& error_callback,
352 dbus::Response* response) {
353 if (!response) {
354 error_callback.Run();
355 return;
356 }
357
358 dbus::MessageReader reader(response);
359 dbus::MessageReader array_reader(NULL);
360 if (!reader.PopArray(&array_reader)) {
361 LOG(ERROR) << "Invalid response: " << response->ToString();
362 error_callback.Run();
363 return;
364 }
365
366 std::vector<MountEntry> entries;
367 while (array_reader.HasMoreData()) {
368 MountEntry entry;
369 dbus::MessageReader sub_reader(NULL);
370 if (!array_reader.PopStruct(&sub_reader) ||
371 !ReadMountEntryFromDbus(&sub_reader, &entry)) {
372 LOG(ERROR) << "Invalid response: " << response->ToString();
373 error_callback.Run();
374 return;
375 }
376 entries.push_back(entry);
377 }
378 callback.Run(entries);
379 }
380
[email protected]f026c0f2014-05-06 21:52:35381 // Handles the result of Format and calls |callback| or |error_callback|.
382 void OnFormat(const base::Closure& callback,
383 const base::Closure& error_callback,
384 dbus::Response* response) {
[email protected]4ae73292011-11-15 05:20:18385 if (!response) {
386 error_callback.Run();
387 return;
388 }
[email protected]f026c0f2014-05-06 21:52:35389 callback.Run();
[email protected]4ae73292011-11-15 05:20:18390 }
391
392 // Handles the result of GetDeviceProperties and calls |callback| or
393 // |error_callback|.
394 void OnGetDeviceProperties(const std::string& device_path,
[email protected]4a404e52012-04-11 02:25:35395 const GetDevicePropertiesCallback& callback,
[email protected]5624a252013-07-04 03:17:53396 const base::Closure& error_callback,
[email protected]4ae73292011-11-15 05:20:18397 dbus::Response* response) {
398 if (!response) {
399 error_callback.Run();
400 return;
401 }
402 DiskInfo disk(device_path, response);
403 callback.Run(disk);
404 }
405
406 // Handles mount event signals and calls |handler|.
407 void OnMountEvent(MountEventType event_type,
408 MountEventHandler handler,
409 dbus::Signal* signal) {
410 dbus::MessageReader reader(signal);
411 std::string device;
412 if (!reader.PopString(&device)) {
413 LOG(ERROR) << "Invalid signal: " << signal->ToString();
414 return;
415 }
416 handler.Run(event_type, device);
417 }
418
419 // Handles MountCompleted signal and calls |handler|.
420 void OnMountCompleted(MountCompletedHandler handler, dbus::Signal* signal) {
421 dbus::MessageReader reader(signal);
[email protected]d7760592014-05-16 07:57:52422 MountEntry entry;
423 if (!ReadMountEntryFromDbus(&reader, &entry)) {
[email protected]4ae73292011-11-15 05:20:18424 LOG(ERROR) << "Invalid signal: " << signal->ToString();
425 return;
426 }
[email protected]d7760592014-05-16 07:57:52427 handler.Run(entry);
[email protected]4ae73292011-11-15 05:20:18428 }
429
[email protected]a0278d52014-05-06 03:36:15430 // Handles FormatCompleted signal and calls |handler|.
431 void OnFormatCompleted(FormatCompletedHandler handler, dbus::Signal* signal) {
432 dbus::MessageReader reader(signal);
avi6e1a22d2015-12-21 03:43:20433 uint32_t error_code = 0;
[email protected]a0278d52014-05-06 03:36:15434 std::string device_path;
435 if (!reader.PopUint32(&error_code) || !reader.PopString(&device_path)) {
436 LOG(ERROR) << "Invalid signal: " << signal->ToString();
437 return;
438 }
439 handler.Run(static_cast<FormatError>(error_code), device_path);
440 }
441
[email protected]4ae73292011-11-15 05:20:18442 // Handles the result of signal connection setup.
443 void OnSignalConnected(const std::string& interface,
444 const std::string& signal,
[email protected]d6311dcb2012-10-22 03:40:43445 bool succeeded) {
446 LOG_IF(ERROR, !succeeded) << "Connect to " << interface << " " <<
[email protected]4ae73292011-11-15 05:20:18447 signal << " failed.";
448 }
449
450 dbus::ObjectProxy* proxy_;
[email protected]926957b2012-09-07 05:34:16451
452 // Note: This should remain the last member so it'll be destroyed and
453 // invalidate its weak pointers before any other members are destroyed.
[email protected]4ae73292011-11-15 05:20:18454 base::WeakPtrFactory<CrosDisksClientImpl> weak_ptr_factory_;
455
456 DISALLOW_COPY_AND_ASSIGN(CrosDisksClientImpl);
457};
458
[email protected]85b95a2012012-08-07 18:57:27459} // namespace
[email protected]4ae73292011-11-15 05:20:18460
461////////////////////////////////////////////////////////////////////////////////
462// DiskInfo
463
464DiskInfo::DiskInfo(const std::string& device_path, dbus::Response* response)
465 : device_path_(device_path),
466 is_drive_(false),
467 has_media_(false),
468 on_boot_device_(false),
[email protected]79ed457b2014-07-22 04:07:26469 on_removable_device_(false),
[email protected]2321d282012-01-31 23:06:59470 device_type_(DEVICE_TYPE_UNKNOWN),
[email protected]4ae73292011-11-15 05:20:18471 total_size_in_bytes_(0),
472 is_read_only_(false),
473 is_hidden_(true) {
474 InitializeFromResponse(response);
475}
476
477DiskInfo::~DiskInfo() {
478}
479
[email protected]85b95a2012012-08-07 18:57:27480// Initializes |this| from |response| given by the cros-disks service.
[email protected]4ae73292011-11-15 05:20:18481// Below is an example of |response|'s raw message (long string is ellipsized).
482//
483//
484// message_type: MESSAGE_METHOD_RETURN
485// destination: :1.8
486// sender: :1.16
487// signature: a{sv}
488// serial: 96
489// reply_serial: 267
490//
491// array [
492// dict entry {
493// string "DeviceFile"
494// variant string "/dev/sdb"
495// }
496// dict entry {
497// string "DeviceIsDrive"
498// variant bool true
499// }
500// dict entry {
501// string "DeviceIsMediaAvailable"
502// variant bool true
503// }
504// dict entry {
505// string "DeviceIsMounted"
506// variant bool false
507// }
508// dict entry {
509// string "DeviceIsOnBootDevice"
510// variant bool false
511// }
512// dict entry {
[email protected]79ed457b2014-07-22 04:07:26513// string "DeviceIsOnRemovableDevice"
514// variant bool true
515// }
516// dict entry {
[email protected]4ae73292011-11-15 05:20:18517// string "DeviceIsReadOnly"
518// variant bool false
519// }
520// dict entry {
521// string "DeviceIsVirtual"
522// variant bool false
523// }
524// dict entry {
525// string "DeviceMediaType"
avi6e1a22d2015-12-21 03:43:20526// variant uint32_t 1
[email protected]4ae73292011-11-15 05:20:18527// }
528// dict entry {
529// string "DeviceMountPaths"
530// variant array [
531// ]
532// }
533// dict entry {
534// string "DevicePresentationHide"
535// variant bool true
536// }
537// dict entry {
538// string "DeviceSize"
avi6e1a22d2015-12-21 03:43:20539// variant uint64_t 7998537728
[email protected]4ae73292011-11-15 05:20:18540// }
541// dict entry {
542// string "DriveIsRotational"
543// variant bool false
544// }
545// dict entry {
[email protected]202e9fee2012-09-13 20:21:29546// string "VendorId"
547// variant string "18d1"
548// }
549// dict entry {
550// string "VendorName"
551// variant string "Google Inc."
552// }
553// dict entry {
554// string "ProductId"
555// variant string "4e11"
556// }
557// dict entry {
558// string "ProductName"
559// variant string "Nexus One"
560// }
561// dict entry {
[email protected]4ae73292011-11-15 05:20:18562// string "DriveModel"
563// variant string "TransMemory"
564// }
565// dict entry {
566// string "IdLabel"
567// variant string ""
568// }
569// dict entry {
570// string "IdUuid"
571// variant string ""
572// }
573// dict entry {
574// string "NativePath"
575// variant string "/sys/devices/pci0000:00/0000:00:1d.7/usb1/1-4/...
576// }
577// ]
578void DiskInfo::InitializeFromResponse(dbus::Response* response) {
[email protected]81836aed2013-07-09 23:41:12579 dbus::MessageReader reader(response);
dcheng0a6e80c2016-04-08 18:37:38580 std::unique_ptr<base::Value> value(dbus::PopDataAsValue(&reader));
[email protected]81836aed2013-07-09 23:41:12581 base::DictionaryValue* properties = NULL;
582 if (!value || !value->GetAsDictionary(&properties))
[email protected]4ae73292011-11-15 05:20:18583 return;
[email protected]4ae73292011-11-15 05:20:18584
[email protected]81836aed2013-07-09 23:41:12585 properties->GetBooleanWithoutPathExpansion(
586 cros_disks::kDeviceIsDrive, &is_drive_);
587 properties->GetBooleanWithoutPathExpansion(
588 cros_disks::kDeviceIsReadOnly, &is_read_only_);
589 properties->GetBooleanWithoutPathExpansion(
590 cros_disks::kDevicePresentationHide, &is_hidden_);
591 properties->GetBooleanWithoutPathExpansion(
592 cros_disks::kDeviceIsMediaAvailable, &has_media_);
593 properties->GetBooleanWithoutPathExpansion(
594 cros_disks::kDeviceIsOnBootDevice, &on_boot_device_);
[email protected]79ed457b2014-07-22 04:07:26595 properties->GetBooleanWithoutPathExpansion(
596 cros_disks::kDeviceIsOnRemovableDevice, &on_removable_device_);
[email protected]81836aed2013-07-09 23:41:12597 properties->GetStringWithoutPathExpansion(
598 cros_disks::kNativePath, &system_path_);
599 properties->GetStringWithoutPathExpansion(
600 cros_disks::kDeviceFile, &file_path_);
601 properties->GetStringWithoutPathExpansion(cros_disks::kVendorId, &vendor_id_);
602 properties->GetStringWithoutPathExpansion(
603 cros_disks::kVendorName, &vendor_name_);
604 properties->GetStringWithoutPathExpansion(
605 cros_disks::kProductId, &product_id_);
606 properties->GetStringWithoutPathExpansion(
607 cros_disks::kProductName, &product_name_);
608 properties->GetStringWithoutPathExpansion(
609 cros_disks::kDriveModel, &drive_model_);
610 properties->GetStringWithoutPathExpansion(cros_disks::kIdLabel, &label_);
611 properties->GetStringWithoutPathExpansion(cros_disks::kIdUuid, &uuid_);
[email protected]2321d282012-01-31 23:06:59612
avi6e1a22d2015-12-21 03:43:20613 // dbus::PopDataAsValue() pops uint64_t as double.
614 // The top 11 bits of uint64_t are dropped by the use of double. But, this
615 // works
[email protected]81836aed2013-07-09 23:41:12616 // unless the size exceeds 8 PB.
617 double device_size_double = 0;
618 if (properties->GetDoubleWithoutPathExpansion(cros_disks::kDeviceSize,
619 &device_size_double))
620 total_size_in_bytes_ = device_size_double;
621
avi6e1a22d2015-12-21 03:43:20622 // dbus::PopDataAsValue() pops uint32_t as double.
[email protected]81836aed2013-07-09 23:41:12623 double media_type_double = 0;
624 if (properties->GetDoubleWithoutPathExpansion(cros_disks::kDeviceMediaType,
625 &media_type_double))
626 device_type_ = DeviceMediaTypeToDeviceType(media_type_double);
627
628 base::ListValue* mount_paths = NULL;
629 if (properties->GetListWithoutPathExpansion(cros_disks::kDeviceMountPaths,
630 &mount_paths))
631 mount_paths->GetString(0, &mount_path_);
[email protected]4ae73292011-11-15 05:20:18632}
633
634////////////////////////////////////////////////////////////////////////////////
635// CrosDisksClient
636
637CrosDisksClient::CrosDisksClient() {}
638
639CrosDisksClient::~CrosDisksClient() {}
640
641// static
[email protected]c5fd5362013-08-27 12:23:04642CrosDisksClient* CrosDisksClient::Create(DBusClientImplementationType type) {
[email protected]e8db03d62012-03-31 04:08:38643 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION)
[email protected]c5fd5362013-08-27 12:23:04644 return new CrosDisksClientImpl();
[email protected]e8db03d62012-03-31 04:08:38645 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type);
satorux8fd293802014-10-30 08:23:12646 return new FakeCrosDisksClient();
[email protected]4ae73292011-11-15 05:20:18647}
648
[email protected]a5a8b412013-03-04 15:03:11649// static
650base::FilePath CrosDisksClient::GetArchiveMountPoint() {
[email protected]49c4cf852013-09-27 19:28:24651 return base::FilePath(base::SysInfo::IsRunningOnChromeOS() ?
[email protected]a5a8b412013-03-04 15:03:11652 FILE_PATH_LITERAL("/media/archive") :
653 FILE_PATH_LITERAL("/tmp/chromeos/media/archive"));
654}
655
656// static
657base::FilePath CrosDisksClient::GetRemovableDiskMountPoint() {
[email protected]49c4cf852013-09-27 19:28:24658 return base::FilePath(base::SysInfo::IsRunningOnChromeOS() ?
[email protected]a5a8b412013-03-04 15:03:11659 FILE_PATH_LITERAL("/media/removable") :
660 FILE_PATH_LITERAL("/tmp/chromeos/media/removable"));
661}
662
[email protected]4ae73292011-11-15 05:20:18663} // namespace chromeos