Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / storage_monitor / test_portable_device_watcher_win.cc
blobfe4d4508333a3c5fa2b5360101d4762269b9c7a6
1 // Copyright 2014 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 // TestPortableDeviceWatcherWin implementation.
7 #include "components/storage_monitor/test_portable_device_watcher_win.h"
9 #include <vector>
11 #include "base/strings/utf_string_conversions.h"
13 namespace storage_monitor {
15 namespace {
17 // Sample MTP device storage information.
18 const base::char16 kMTPDeviceFriendlyName[] = L"Camera V1.1";
19 const base::char16 kStorageLabelA[] = L"Camera V1.1 (s10001)";
20 const base::char16 kStorageLabelB[] = L"Camera V1.1 (s20001)";
21 const base::char16 kStorageObjectIdA[] = L"s10001";
22 const base::char16 kStorageObjectIdB[] = L"s20001";
23 const char kStorageUniqueIdB[] =
24 "mtp:StorageSerial:SID-{s20001, S, 2238}:123123";
26 // Returns the storage name of the device specified by |pnp_device_id|.
27 // |storage_object_id| specifies the string ID that uniquely identifies the
28 // object on the device.
29 base::string16 GetMTPStorageName(const base::string16& pnp_device_id,
30 const base::string16& storage_object_id) {
31 if (pnp_device_id == TestPortableDeviceWatcherWin::kMTPDeviceWithInvalidInfo)
32 return base::string16();
34 if (storage_object_id == kStorageObjectIdA)
35 return kStorageLabelA;
36 return (storage_object_id == kStorageObjectIdB) ?
37 kStorageLabelB : base::string16();
40 } // namespace
42 // TestPortableDeviceWatcherWin ------------------------------------------------
44 // static
45 const base::char16
46 TestPortableDeviceWatcherWin::kMTPDeviceWithMultipleStorages[] =
47 L"\\?\\usb#vid_ff&pid_18#32&2&1#{ab33-1de4-f22e-1882-9724})";
48 const base::char16 TestPortableDeviceWatcherWin::kMTPDeviceWithInvalidInfo[] =
49 L"\\?\\usb#vid_00&pid_00#0&2&1#{0000-0000-0000-0000-0000})";
50 const base::char16 TestPortableDeviceWatcherWin::kMTPDeviceWithValidInfo[] =
51 L"\\?\\usb#vid_ff&pid_000f#32&2&1#{abcd-1234-ffde-1112-9172})";
52 const char TestPortableDeviceWatcherWin::kStorageUniqueIdA[] =
53 "mtp:StorageSerial:SID-{s10001, D, 12378}:123123";
55 TestPortableDeviceWatcherWin::TestPortableDeviceWatcherWin()
56 : use_dummy_mtp_storage_info_(false) {
59 TestPortableDeviceWatcherWin::~TestPortableDeviceWatcherWin() {
62 // static
63 std::string TestPortableDeviceWatcherWin::GetMTPStorageUniqueId(
64 const base::string16& pnp_device_id,
65 const base::string16& storage_object_id) {
66 if (storage_object_id == kStorageObjectIdA)
67 return TestPortableDeviceWatcherWin::kStorageUniqueIdA;
68 return (storage_object_id == kStorageObjectIdB) ?
69 kStorageUniqueIdB : std::string();
72 // static
73 PortableDeviceWatcherWin::StorageObjectIDs
74 TestPortableDeviceWatcherWin::GetMTPStorageObjectIds(
75 const base::string16& pnp_device_id) {
76 PortableDeviceWatcherWin::StorageObjectIDs storage_object_ids;
77 storage_object_ids.push_back(kStorageObjectIdA);
78 if (pnp_device_id == kMTPDeviceWithMultipleStorages)
79 storage_object_ids.push_back(kStorageObjectIdB);
80 return storage_object_ids;
83 // static
84 void TestPortableDeviceWatcherWin::GetMTPStorageDetails(
85 const base::string16& pnp_device_id,
86 const base::string16& storage_object_id,
87 base::string16* device_location,
88 std::string* unique_id,
89 base::string16* name) {
90 std::string storage_unique_id = GetMTPStorageUniqueId(pnp_device_id,
91 storage_object_id);
92 if (device_location)
93 *device_location = base::UTF8ToUTF16("\\\\" + storage_unique_id);
95 if (unique_id)
96 *unique_id = storage_unique_id;
98 if (name)
99 *name = GetMTPStorageName(pnp_device_id, storage_object_id);
102 // static
103 PortableDeviceWatcherWin::StorageObjects
104 TestPortableDeviceWatcherWin::GetDeviceStorageObjects(
105 const base::string16& pnp_device_id) {
106 PortableDeviceWatcherWin::StorageObjects storage_objects;
107 PortableDeviceWatcherWin::StorageObjectIDs storage_object_ids =
108 GetMTPStorageObjectIds(pnp_device_id);
109 for (PortableDeviceWatcherWin::StorageObjectIDs::const_iterator it =
110 storage_object_ids.begin();
111 it != storage_object_ids.end(); ++it) {
112 storage_objects.push_back(DeviceStorageObject(
113 *it, GetMTPStorageUniqueId(pnp_device_id, *it)));
115 return storage_objects;
118 void TestPortableDeviceWatcherWin::EnumerateAttachedDevices() {
121 void TestPortableDeviceWatcherWin::HandleDeviceAttachEvent(
122 const base::string16& pnp_device_id) {
123 DeviceDetails device_details;
124 if (pnp_device_id != kMTPDeviceWithInvalidInfo)
125 device_details.name = kMTPDeviceFriendlyName;
126 device_details.location = pnp_device_id;
127 device_details.storage_objects = GetDeviceStorageObjects(pnp_device_id);
128 OnDidHandleDeviceAttachEvent(&device_details, true);
131 bool TestPortableDeviceWatcherWin::GetMTPStorageInfoFromDeviceId(
132 const std::string& storage_device_id,
133 base::string16* device_location,
134 base::string16* storage_object_id) const {
135 DCHECK(!storage_device_id.empty());
136 if (use_dummy_mtp_storage_info_) {
137 if (storage_device_id == TestPortableDeviceWatcherWin::kStorageUniqueIdA) {
138 *device_location = kMTPDeviceWithValidInfo;
139 *storage_object_id = kStorageObjectIdA;
140 return true;
142 return false;
144 return PortableDeviceWatcherWin::GetMTPStorageInfoFromDeviceId(
145 storage_device_id, device_location, storage_object_id);
148 } // namespace storage_monitor