Home | History | Annotate | Download | only in storage_monitor
      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.
      6 
      7 #include "components/storage_monitor/test_portable_device_watcher_win.h"
      8 
      9 #include <vector>
     10 
     11 #include "base/strings/utf_string_conversions.h"
     12 
     13 namespace storage_monitor {
     14 
     15 namespace {
     16 
     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";
     25 
     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();
     33 
     34   if (storage_object_id == kStorageObjectIdA)
     35     return kStorageLabelA;
     36   return (storage_object_id == kStorageObjectIdB) ?
     37       kStorageLabelB : base::string16();
     38 }
     39 
     40 }  // namespace
     41 
     42 // TestPortableDeviceWatcherWin ------------------------------------------------
     43 
     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";
     54 
     55 TestPortableDeviceWatcherWin::TestPortableDeviceWatcherWin()
     56     : use_dummy_mtp_storage_info_(false) {
     57 }
     58 
     59 TestPortableDeviceWatcherWin::~TestPortableDeviceWatcherWin() {
     60 }
     61 
     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();
     70 }
     71 
     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;
     81 }
     82 
     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);
     94 
     95   if (unique_id)
     96     *unique_id = storage_unique_id;
     97 
     98   if (name)
     99     *name = GetMTPStorageName(pnp_device_id, storage_object_id);
    100 }
    101 
    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)));
    114   }
    115   return storage_objects;
    116 }
    117 
    118 void TestPortableDeviceWatcherWin::EnumerateAttachedDevices() {
    119 }
    120 
    121 void TestPortableDeviceWatcherWin::HandleDeviceAttachEvent(
    122     const base::string16& pnp_device_id) {
    123   DeviceDetails device_details = {
    124       (pnp_device_id != kMTPDeviceWithInvalidInfo) ?
    125            kMTPDeviceFriendlyName : base::string16(),
    126       pnp_device_id,
    127       GetDeviceStorageObjects(pnp_device_id)
    128   };
    129   OnDidHandleDeviceAttachEvent(&device_details, true);
    130 }
    131 
    132 bool TestPortableDeviceWatcherWin::GetMTPStorageInfoFromDeviceId(
    133     const std::string& storage_device_id,
    134     base::string16* device_location,
    135     base::string16* storage_object_id) const {
    136   DCHECK(!storage_device_id.empty());
    137   if (use_dummy_mtp_storage_info_) {
    138     if (storage_device_id == TestPortableDeviceWatcherWin::kStorageUniqueIdA) {
    139       *device_location = kMTPDeviceWithValidInfo;
    140       *storage_object_id = kStorageObjectIdA;
    141       return true;
    142     }
    143     return false;
    144   }
    145   return PortableDeviceWatcherWin::GetMTPStorageInfoFromDeviceId(
    146       storage_device_id, device_location, storage_object_id);
    147 }
    148 
    149 }  // namespace storage_monitor
    150