12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/media_galleries/win/portable_device_map_service.h"
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/logging.h"
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/stl_util.h"
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/threading/thread_restrictions.h"
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/browser_thread.h"
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace {
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)base::LazyInstance<PortableDeviceMapService> g_portable_device_map_service =
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    LAZY_INSTANCE_INITIALIZER;
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// static
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)PortableDeviceMapService* PortableDeviceMapService::GetInstance() {
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return g_portable_device_map_service.Pointer();
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void PortableDeviceMapService::AddPortableDevice(
25a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    const base::string16& device_location,
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    IPortableDevice* device) {
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::ThreadRestrictions::AssertIOAllowed();
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(!device_location.empty());
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(device);
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::AutoLock lock(lock_);
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  device_map_[device_location] = PortableDeviceInfo(device);
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void PortableDeviceMapService::MarkPortableDeviceForDeletion(
35a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    const base::string16& device_location) {
36e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(!device_location.empty());
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::AutoLock lock(lock_);
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  PortableDeviceMap::iterator it = device_map_.find(device_location);
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (it != device_map_.end())
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    it->second.scheduled_to_delete = true;
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void PortableDeviceMapService::RemovePortableDevice(
45a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    const base::string16& device_location) {
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::ThreadRestrictions::AssertIOAllowed();
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(!device_location.empty());
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::AutoLock lock(lock_);
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  PortableDeviceMap::const_iterator it = device_map_.find(device_location);
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if ((it != device_map_.end()) && it->second.scheduled_to_delete)
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    device_map_.erase(it);
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)IPortableDevice* PortableDeviceMapService::GetPortableDevice(
55a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    const base::string16& device_location) {
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::ThreadRestrictions::AssertIOAllowed();
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(!device_location.empty());
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::AutoLock lock(lock_);
592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  PortableDeviceMap::const_iterator it = device_map_.find(device_location);
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return (it == device_map_.end() || it->second.scheduled_to_delete) ?
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      NULL : it->second.portable_device.get();
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)PortableDeviceMapService::PortableDeviceInfo::PortableDeviceInfo()
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    : scheduled_to_delete(false) {
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)PortableDeviceMapService::PortableDeviceInfo::PortableDeviceInfo(
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    IPortableDevice* device)
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    : portable_device(device),
712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      scheduled_to_delete(false) {
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)PortableDeviceMapService::PortableDeviceMapService() {
752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)PortableDeviceMapService::~PortableDeviceMapService() {
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
79