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.
5 #include "chrome/browser/media_galleries/win/portable_device_map_service.h"
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"
14 base::LazyInstance
<PortableDeviceMapService
> g_portable_device_map_service
=
15 LAZY_INSTANCE_INITIALIZER
;
20 PortableDeviceMapService
* PortableDeviceMapService::GetInstance() {
21 return g_portable_device_map_service
.Pointer();
24 void PortableDeviceMapService::AddPortableDevice(
25 const base::string16
& device_location
,
26 IPortableDevice
* device
) {
27 base::ThreadRestrictions::AssertIOAllowed();
28 DCHECK(!device_location
.empty());
30 base::AutoLock
lock(lock_
);
31 device_map_
[device_location
] = PortableDeviceInfo(device
);
34 void 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;
44 void 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
);
54 IPortableDevice
* 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();
64 PortableDeviceMapService::PortableDeviceInfo::PortableDeviceInfo()
65 : scheduled_to_delete(false) {
68 PortableDeviceMapService::PortableDeviceInfo::PortableDeviceInfo(
69 IPortableDevice
* device
)
70 : portable_device(device
),
71 scheduled_to_delete(false) {
74 PortableDeviceMapService::PortableDeviceMapService() {
77 PortableDeviceMapService::~PortableDeviceMapService() {