1// Copyright 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#include "chrome/browser/media_galleries/win/portable_device_map_service.h" 6 7#include "base/logging.h" 8#include "base/stl_util.h" 9#include "base/threading/thread_restrictions.h" 10#include "content/public/browser/browser_thread.h" 11 12namespace { 13 14base::LazyInstance<PortableDeviceMapService> g_portable_device_map_service = 15 LAZY_INSTANCE_INITIALIZER; 16 17} // namespace 18 19// static 20PortableDeviceMapService* PortableDeviceMapService::GetInstance() { 21 return g_portable_device_map_service.Pointer(); 22} 23 24void PortableDeviceMapService::AddPortableDevice( 25 const base::string16& device_location, 26 IPortableDevice* device) { 27 base::ThreadRestrictions::AssertIOAllowed(); 28 DCHECK(!device_location.empty()); 29 DCHECK(device); 30 base::AutoLock lock(lock_); 31 device_map_[device_location] = PortableDeviceInfo(device); 32} 33 34void PortableDeviceMapService::MarkPortableDeviceForDeletion( 35 const base::string16& device_location) { 36 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 37 DCHECK(!device_location.empty()); 38 base::AutoLock lock(lock_); 39 PortableDeviceMap::iterator it = device_map_.find(device_location); 40 if (it != device_map_.end()) 41 it->second.scheduled_to_delete = true; 42} 43 44void PortableDeviceMapService::RemovePortableDevice( 45 const base::string16& device_location) { 46 base::ThreadRestrictions::AssertIOAllowed(); 47 DCHECK(!device_location.empty()); 48 base::AutoLock lock(lock_); 49 PortableDeviceMap::const_iterator it = device_map_.find(device_location); 50 if ((it != device_map_.end()) && it->second.scheduled_to_delete) 51 device_map_.erase(it); 52} 53 54IPortableDevice* PortableDeviceMapService::GetPortableDevice( 55 const base::string16& device_location) { 56 base::ThreadRestrictions::AssertIOAllowed(); 57 DCHECK(!device_location.empty()); 58 base::AutoLock lock(lock_); 59 PortableDeviceMap::const_iterator it = device_map_.find(device_location); 60 return (it == device_map_.end() || it->second.scheduled_to_delete) ? 61 NULL : it->second.portable_device.get(); 62} 63 64PortableDeviceMapService::PortableDeviceInfo::PortableDeviceInfo() 65 : scheduled_to_delete(false) { 66} 67 68PortableDeviceMapService::PortableDeviceInfo::PortableDeviceInfo( 69 IPortableDevice* device) 70 : portable_device(device), 71 scheduled_to_delete(false) { 72} 73 74PortableDeviceMapService::PortableDeviceMapService() { 75} 76 77PortableDeviceMapService::~PortableDeviceMapService() { 78} 79