Home | History | Annotate | Download | only in storage_monitor
      1 // Copyright (c) 2013 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 "chrome/browser/storage_monitor/test_portable_device_watcher_win.h"
      8 
      9 #include <vector>
     10 
     11 #include "base/strings/utf_string_conversions.h"
     12 
     13 namespace chrome {
     14 namespace test {
     15 namespace {
     16 
     17 // Sample MTP device storage information.
     18 const char16 kMTPDeviceFriendlyName[] = L"Camera V1.1";
     19 const char16 kStorageLabelA[] = L"Camera V1.1 (s10001)";
     20 const char16 kStorageLabelB[] = L"Camera V1.1 (s20001)";
     21 const char16 kStorageObjectIdA[] = L"s10001";
     22 const 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 string16 GetMTPStorageName(const string16& pnp_device_id,
     30                            const string16& storage_object_id) {
     31   if (pnp_device_id == TestPortableDeviceWatcherWin::kMTPDeviceWithInvalidInfo)
     32     return string16();
     33 
     34   if (storage_object_id == kStorageObjectIdA)
     35     return kStorageLabelA;
     36   return (storage_object_id == kStorageObjectIdB) ?
     37       kStorageLabelB : string16();
     38 }
     39 
     40 }  // namespace
     41 
     42 // TestPortableDeviceWatcherWin ------------------------------------------------
     43 
     44 // static
     45 const char16 TestPortableDeviceWatcherWin::kMTPDeviceWithMultipleStorages[] =
     46     L"\\?\\usb#vid_ff&pid_18#32&2&1#{ab33-1de4-f22e-1882-9724})";
     47 const char16 TestPortableDeviceWatcherWin::kMTPDeviceWithInvalidInfo[] =
     48     L"\\?\\usb#vid_00&pid_00#0&2&1#{0000-0000-0000-0000-0000})";
     49 const char16 TestPortableDeviceWatcherWin::kMTPDeviceWithValidInfo[] =
     50     L"\\?\\usb#vid_ff&pid_000f#32&2&1#{abcd-1234-ffde-1112-9172})";
     51 const char TestPortableDeviceWatcherWin::kStorageUniqueIdA[] =
     52     "mtp:StorageSerial:SID-{s10001, D, 12378}:123123";
     53 
     54 TestPortableDeviceWatcherWin::TestPortableDeviceWatcherWin()
     55     : use_dummy_mtp_storage_info_(false) {
     56 }
     57 
     58 TestPortableDeviceWatcherWin::~TestPortableDeviceWatcherWin() {
     59 }
     60 
     61 // static
     62 std::string TestPortableDeviceWatcherWin::GetMTPStorageUniqueId(
     63     const string16& pnp_device_id,
     64     const string16& storage_object_id) {
     65   if (storage_object_id == kStorageObjectIdA)
     66     return TestPortableDeviceWatcherWin::kStorageUniqueIdA;
     67   return (storage_object_id == kStorageObjectIdB) ?
     68       kStorageUniqueIdB : std::string();
     69 }
     70 
     71 // static
     72 PortableDeviceWatcherWin::StorageObjectIDs
     73 TestPortableDeviceWatcherWin::GetMTPStorageObjectIds(
     74     const string16& pnp_device_id) {
     75   PortableDeviceWatcherWin::StorageObjectIDs storage_object_ids;
     76   storage_object_ids.push_back(kStorageObjectIdA);
     77   if (pnp_device_id == kMTPDeviceWithMultipleStorages)
     78     storage_object_ids.push_back(kStorageObjectIdB);
     79   return storage_object_ids;
     80 }
     81 
     82 // static
     83 void TestPortableDeviceWatcherWin::GetMTPStorageDetails(
     84     const string16& pnp_device_id,
     85     const string16& storage_object_id,
     86     string16* device_location,
     87     std::string* unique_id,
     88     string16* name) {
     89   std::string storage_unique_id = GetMTPStorageUniqueId(pnp_device_id,
     90                                                         storage_object_id);
     91   if (device_location)
     92     *device_location = UTF8ToUTF16("\\\\" + storage_unique_id);
     93 
     94   if (unique_id)
     95     *unique_id = storage_unique_id;
     96 
     97   if (name)
     98     *name = GetMTPStorageName(pnp_device_id, storage_object_id);
     99 }
    100 
    101 // static
    102 PortableDeviceWatcherWin::StorageObjects
    103 TestPortableDeviceWatcherWin::GetDeviceStorageObjects(
    104     const string16& pnp_device_id) {
    105   PortableDeviceWatcherWin::StorageObjects storage_objects;
    106   PortableDeviceWatcherWin::StorageObjectIDs storage_object_ids =
    107       GetMTPStorageObjectIds(pnp_device_id);
    108   for (PortableDeviceWatcherWin::StorageObjectIDs::const_iterator it =
    109            storage_object_ids.begin();
    110        it != storage_object_ids.end(); ++it) {
    111     storage_objects.push_back(DeviceStorageObject(
    112         *it, GetMTPStorageUniqueId(pnp_device_id, *it)));
    113   }
    114   return storage_objects;
    115 }
    116 
    117 void TestPortableDeviceWatcherWin::EnumerateAttachedDevices() {
    118 }
    119 
    120 void TestPortableDeviceWatcherWin::HandleDeviceAttachEvent(
    121     const string16& pnp_device_id) {
    122   DeviceDetails device_details = {
    123       (pnp_device_id != kMTPDeviceWithInvalidInfo) ?
    124            kMTPDeviceFriendlyName : string16(),
    125       pnp_device_id,
    126       GetDeviceStorageObjects(pnp_device_id)
    127   };
    128   OnDidHandleDeviceAttachEvent(&device_details, true);
    129 }
    130 
    131 bool TestPortableDeviceWatcherWin::GetMTPStorageInfoFromDeviceId(
    132     const std::string& storage_device_id,
    133     string16* device_location,
    134     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;
    141     }
    142     return false;
    143   }
    144   return PortableDeviceWatcherWin::GetMTPStorageInfoFromDeviceId(
    145       storage_device_id, device_location, storage_object_id);
    146 }
    147 
    148 }  // namespace test
    149 }  // namespace chrome
    150