| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "webkit/fileapi/media/mtp_device_map_service.h" | 5 #include "webkit/fileapi/media/mtp_device_map_service.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| 11 #include "webkit/fileapi/isolated_context.h" | 11 #include "webkit/fileapi/isolated_context.h" |
| 12 #include "webkit/fileapi/media/mtp_device_delegate.h" | 12 #include "webkit/fileapi/media/mtp_device_delegate.h" |
| 13 | 13 |
| 14 namespace fileapi { | 14 namespace fileapi { |
| 15 | 15 |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 base::LazyInstance<MTPDeviceMapService> g_mtp_device_map_service = | 18 base::LazyInstance<MTPDeviceMapService> g_mtp_device_map_service = |
| 19 LAZY_INSTANCE_INITIALIZER; | 19 LAZY_INSTANCE_INITIALIZER; |
| 20 | 20 |
| 21 } // namespace | 21 } // namespace |
| 22 | 22 |
| 23 // static | 23 // static |
| 24 MTPDeviceMapService* MTPDeviceMapService::GetInstance() { | 24 MTPDeviceMapService* MTPDeviceMapService::GetInstance() { |
| 25 return g_mtp_device_map_service.Pointer(); | 25 return g_mtp_device_map_service.Pointer(); |
| 26 } | 26 } |
| 27 | 27 |
| 28 void MTPDeviceMapService::AddDelegate( | 28 void MTPDeviceMapService::AddDelegate( |
| 29 const FilePath::StringType& device_location, | 29 const base::FilePath::StringType& device_location, |
| 30 MTPDeviceDelegate* delegate) { | 30 MTPDeviceDelegate* delegate) { |
| 31 DCHECK(delegate); | 31 DCHECK(delegate); |
| 32 DCHECK(!device_location.empty()); | 32 DCHECK(!device_location.empty()); |
| 33 base::AutoLock lock(lock_); | 33 base::AutoLock lock(lock_); |
| 34 if (ContainsKey(delegate_map_, device_location)) | 34 if (ContainsKey(delegate_map_, device_location)) |
| 35 return; | 35 return; |
| 36 | 36 |
| 37 delegate_map_[device_location] = delegate; | 37 delegate_map_[device_location] = delegate; |
| 38 } | 38 } |
| 39 | 39 |
| 40 void MTPDeviceMapService::RemoveDelegate( | 40 void MTPDeviceMapService::RemoveDelegate( |
| 41 const FilePath::StringType& device_location) { | 41 const base::FilePath::StringType& device_location) { |
| 42 base::AutoLock lock(lock_); | 42 base::AutoLock lock(lock_); |
| 43 DelegateMap::iterator it = delegate_map_.find(device_location); | 43 DelegateMap::iterator it = delegate_map_.find(device_location); |
| 44 DCHECK(it != delegate_map_.end()); | 44 DCHECK(it != delegate_map_.end()); |
| 45 it->second->CancelPendingTasksAndDeleteDelegate(); | 45 it->second->CancelPendingTasksAndDeleteDelegate(); |
| 46 delegate_map_.erase(it); | 46 delegate_map_.erase(it); |
| 47 } | 47 } |
| 48 | 48 |
| 49 MTPDeviceDelegate* MTPDeviceMapService::GetMTPDeviceDelegate( | 49 MTPDeviceDelegate* MTPDeviceMapService::GetMTPDeviceDelegate( |
| 50 const std::string& filesystem_id) { | 50 const std::string& filesystem_id) { |
| 51 FilePath device_path; | 51 base::FilePath device_path; |
| 52 if (!IsolatedContext::GetInstance()->GetRegisteredPath(filesystem_id, | 52 if (!IsolatedContext::GetInstance()->GetRegisteredPath(filesystem_id, |
| 53 &device_path)) { | 53 &device_path)) { |
| 54 return NULL; | 54 return NULL; |
| 55 } | 55 } |
| 56 | 56 |
| 57 const FilePath::StringType& device_location = device_path.value(); | 57 const base::FilePath::StringType& device_location = device_path.value(); |
| 58 DCHECK(!device_location.empty()); | 58 DCHECK(!device_location.empty()); |
| 59 | 59 |
| 60 base::AutoLock lock(lock_); | 60 base::AutoLock lock(lock_); |
| 61 DelegateMap::const_iterator it = delegate_map_.find(device_location); | 61 DelegateMap::const_iterator it = delegate_map_.find(device_location); |
| 62 return (it != delegate_map_.end()) ? it->second : NULL; | 62 return (it != delegate_map_.end()) ? it->second : NULL; |
| 63 } | 63 } |
| 64 | 64 |
| 65 MTPDeviceMapService::MTPDeviceMapService() { | 65 MTPDeviceMapService::MTPDeviceMapService() { |
| 66 } | 66 } |
| 67 | 67 |
| 68 MTPDeviceMapService::~MTPDeviceMapService() {} | 68 MTPDeviceMapService::~MTPDeviceMapService() {} |
| 69 | 69 |
| 70 } // namespace fileapi | 70 } // namespace fileapi |
| OLD | NEW |