Home | History | Annotate | Download | only in fileapi
      1 // Copyright (c) 2012 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/fileapi/mtp_device_map_service.h"
      6 
      7 #include <string>
      8 #include <utility>
      9 
     10 #include "base/stl_util.h"
     11 #include "chrome/browser/media_galleries/fileapi/mtp_device_async_delegate.h"
     12 #include "webkit/browser/fileapi/isolated_context.h"
     13 
     14 namespace chrome {
     15 
     16 namespace {
     17 
     18 base::LazyInstance<MTPDeviceMapService> g_mtp_device_map_service =
     19     LAZY_INSTANCE_INITIALIZER;
     20 
     21 }  // namespace
     22 
     23 // static
     24 MTPDeviceMapService* MTPDeviceMapService::GetInstance() {
     25   return g_mtp_device_map_service.Pointer();
     26 }
     27 
     28 /////////////////////////////////////////////////////////////////////////////
     29 //   Following methods are used to manage MTPDeviceAsyncDelegate objects.  //
     30 /////////////////////////////////////////////////////////////////////////////
     31 void MTPDeviceMapService::AddAsyncDelegate(
     32     const base::FilePath::StringType& device_location,
     33     MTPDeviceAsyncDelegate* delegate) {
     34   DCHECK(thread_checker_.CalledOnValidThread());
     35   DCHECK(delegate);
     36   DCHECK(!device_location.empty());
     37   if (ContainsKey(async_delegate_map_, device_location))
     38     return;
     39   async_delegate_map_[device_location] = delegate;
     40 }
     41 
     42 void MTPDeviceMapService::RemoveAsyncDelegate(
     43     const base::FilePath::StringType& device_location) {
     44   DCHECK(thread_checker_.CalledOnValidThread());
     45   AsyncDelegateMap::iterator it = async_delegate_map_.find(device_location);
     46   DCHECK(it != async_delegate_map_.end());
     47   it->second->CancelPendingTasksAndDeleteDelegate();
     48   async_delegate_map_.erase(it);
     49 }
     50 
     51 MTPDeviceAsyncDelegate* MTPDeviceMapService::GetMTPDeviceAsyncDelegate(
     52     const std::string& filesystem_id) {
     53   DCHECK(thread_checker_.CalledOnValidThread());
     54   base::FilePath device_path;
     55   if (!fileapi::IsolatedContext::GetInstance()->GetRegisteredPath(
     56           filesystem_id, &device_path)) {
     57     return NULL;
     58   }
     59 
     60   const base::FilePath::StringType& device_location = device_path.value();
     61   DCHECK(!device_location.empty());
     62   AsyncDelegateMap::const_iterator it =
     63       async_delegate_map_.find(device_location);
     64   return (it != async_delegate_map_.end()) ? it->second : NULL;
     65 }
     66 
     67 
     68 MTPDeviceMapService::MTPDeviceMapService() {
     69 }
     70 
     71 MTPDeviceMapService::~MTPDeviceMapService() {}
     72 
     73 }  // namespace chrome
     74