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 "chrome/browser/media_galleries/fileapi/mtp_device_map_service.h" | 5 #include "chrome/browser/media_galleries/fileapi/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 "chrome/browser/media_galleries/fileapi/mtp_device_async_delegate.h" | 11 #include "chrome/browser/media_galleries/fileapi/mtp_device_async_delegate.h" |
12 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
13 #include "storage/browser/fileapi/external_mount_points.h" | 13 #include "storage/browser/fileapi/external_mount_points.h" |
14 | 14 |
15 namespace { | 15 namespace { |
16 | 16 |
17 base::LazyInstance<MTPDeviceMapService> g_mtp_device_map_service = | 17 base::LazyInstance<MTPDeviceMapService> g_mtp_device_map_service = |
18 LAZY_INSTANCE_INITIALIZER; | 18 LAZY_INSTANCE_INITIALIZER; |
19 | 19 |
20 } // namespace | 20 } // namespace |
21 | 21 |
22 // static | 22 // static |
23 MTPDeviceMapService* MTPDeviceMapService::GetInstance() { | 23 MTPDeviceMapService* MTPDeviceMapService::GetInstance() { |
24 return g_mtp_device_map_service.Pointer(); | 24 return g_mtp_device_map_service.Pointer(); |
25 } | 25 } |
26 | 26 |
27 void MTPDeviceMapService::RegisterMTPFileSystem( | 27 void MTPDeviceMapService::RegisterMTPFileSystem( |
28 const base::FilePath::StringType& device_location, | 28 const base::FilePath::StringType& device_location, |
29 const std::string& fsid) { | 29 const std::string& filesystem_id, |
30 const bool read_only) { | |
30 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 31 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
32 DCHECK(!device_location.empty()); | |
33 DCHECK(!filesystem_id.empty()); | |
31 | 34 |
32 if (!ContainsKey(mtp_device_usage_map_, device_location)) { | 35 const AsyncDelegateKey key = GetAsyncDelegateKey(device_location, read_only); |
36 if (!ContainsKey(mtp_device_usage_map_, key)) { | |
33 // Note that this initializes the delegate asynchronously, but since | 37 // Note that this initializes the delegate asynchronously, but since |
34 // the delegate will only be used from the IO thread, it is guaranteed | 38 // the delegate will only be used from the IO thread, it is guaranteed |
35 // to be created before use of it expects it to be there. | 39 // to be created before use of it expects it to be there. |
36 CreateMTPDeviceAsyncDelegate(device_location, | 40 CreateMTPDeviceAsyncDelegate( |
41 device_location, read_only, | |
37 base::Bind(&MTPDeviceMapService::AddAsyncDelegate, | 42 base::Bind(&MTPDeviceMapService::AddAsyncDelegate, |
38 base::Unretained(this), device_location)); | 43 base::Unretained(this), device_location, read_only)); |
39 mtp_device_usage_map_[device_location] = 0; | 44 mtp_device_usage_map_[key] = 0; |
40 } | 45 } |
41 | 46 |
42 mtp_device_usage_map_[device_location]++; | 47 mtp_device_usage_map_[key]++; |
43 mtp_device_map_[fsid] = device_location; | 48 mtp_device_map_[filesystem_id] = make_pair(device_location, read_only); |
44 } | 49 } |
45 | 50 |
46 void MTPDeviceMapService::RevokeMTPFileSystem(const std::string& fsid) { | 51 void MTPDeviceMapService::RevokeMTPFileSystem( |
52 const std::string& filesystem_id) { | |
47 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 53 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
54 DCHECK(!filesystem_id.empty()); | |
48 | 55 |
49 MTPDeviceFileSystemMap::iterator it = mtp_device_map_.find(fsid); | 56 MTPDeviceFileSystemMap::iterator it = mtp_device_map_.find(filesystem_id); |
50 if (it != mtp_device_map_.end()) { | 57 if (it != mtp_device_map_.end()) { |
51 base::FilePath::StringType device_location = it->second; | 58 const base::FilePath::StringType device_location = it->second.first; |
59 const bool read_only = it->second.second; | |
60 | |
52 mtp_device_map_.erase(it); | 61 mtp_device_map_.erase(it); |
53 MTPDeviceUsageMap::iterator delegate_it = | 62 |
54 mtp_device_usage_map_.find(device_location); | 63 const AsyncDelegateKey key = |
64 GetAsyncDelegateKey(device_location, read_only); | |
65 MTPDeviceUsageMap::iterator delegate_it = mtp_device_usage_map_.find(key); | |
55 DCHECK(delegate_it != mtp_device_usage_map_.end()); | 66 DCHECK(delegate_it != mtp_device_usage_map_.end()); |
56 mtp_device_usage_map_[device_location]--; | 67 |
57 if (mtp_device_usage_map_[device_location] == 0) { | 68 mtp_device_usage_map_[key]--; |
69 if (mtp_device_usage_map_[key] == 0) { | |
58 mtp_device_usage_map_.erase(delegate_it); | 70 mtp_device_usage_map_.erase(delegate_it); |
59 RemoveAsyncDelegate(device_location); | 71 RemoveAsyncDelegate(device_location, read_only); |
60 } | 72 } |
61 } | 73 } |
62 } | 74 } |
63 | 75 |
64 void MTPDeviceMapService::AddAsyncDelegate( | 76 void MTPDeviceMapService::AddAsyncDelegate( |
65 const base::FilePath::StringType& device_location, | 77 const base::FilePath::StringType& device_location, |
78 const bool read_only, | |
66 MTPDeviceAsyncDelegate* delegate) { | 79 MTPDeviceAsyncDelegate* delegate) { |
67 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 80 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
68 DCHECK(delegate); | 81 DCHECK(delegate); |
69 DCHECK(!device_location.empty()); | 82 DCHECK(!device_location.empty()); |
70 if (ContainsKey(async_delegate_map_, device_location)) | 83 |
84 const AsyncDelegateKey key = GetAsyncDelegateKey(device_location, read_only); | |
85 if (ContainsKey(async_delegate_map_, key)) | |
71 return; | 86 return; |
72 async_delegate_map_[device_location] = delegate; | 87 async_delegate_map_[key] = delegate; |
73 } | 88 } |
74 | 89 |
75 void MTPDeviceMapService::RemoveAsyncDelegate( | 90 void MTPDeviceMapService::RemoveAsyncDelegate( |
76 const base::FilePath::StringType& device_location) { | 91 const base::FilePath::StringType& device_location, |
92 const bool read_only) { | |
77 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 93 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
78 AsyncDelegateMap::iterator it = async_delegate_map_.find(device_location); | 94 DCHECK(!device_location.empty()); |
95 | |
96 const AsyncDelegateKey key = GetAsyncDelegateKey(device_location, read_only); | |
97 AsyncDelegateMap::iterator it = async_delegate_map_.find(key); | |
79 DCHECK(it != async_delegate_map_.end()); | 98 DCHECK(it != async_delegate_map_.end()); |
80 it->second->CancelPendingTasksAndDeleteDelegate(); | 99 it->second->CancelPendingTasksAndDeleteDelegate(); |
81 async_delegate_map_.erase(it); | 100 async_delegate_map_.erase(it); |
82 } | 101 } |
83 | 102 |
103 MTPDeviceMapService::AsyncDelegateKey MTPDeviceMapService::GetAsyncDelegateKey( | |
104 const base::FilePath::StringType& device_location, | |
105 const bool read_only) { | |
106 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
107 | |
108 base::FilePath::StringType key; | |
109 key.append(read_only ? FILE_PATH_LITERAL("ReadOnly") | |
110 : FILE_PATH_LITERAL("ReadWrite")); | |
111 key.append(FILE_PATH_LITERAL("|")); | |
112 key.append(device_location); | |
113 return key; | |
114 } | |
115 | |
84 MTPDeviceAsyncDelegate* MTPDeviceMapService::GetMTPDeviceAsyncDelegate( | 116 MTPDeviceAsyncDelegate* MTPDeviceMapService::GetMTPDeviceAsyncDelegate( |
85 const std::string& filesystem_id) { | 117 const std::string& filesystem_id) { |
86 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 118 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
87 base::FilePath device_path; | 119 DCHECK(!filesystem_id.empty()); |
88 if (!storage::ExternalMountPoints::GetSystemInstance()->GetRegisteredPath( | 120 |
Lei Zhang
2015/03/02 23:41:38
I can't remember why we do this. I'll need to dig
Lei Zhang
2015/03/03 00:16:34
I suspect the reason for this is:
MediaFileSystem
yawano
2015/03/03 09:16:54
Done. Yes, we needed the check. I added it. Thank
| |
89 filesystem_id, &device_path)) { | 121 MTPDeviceFileSystemMap::const_iterator mtp_device_map_it = |
122 mtp_device_map_.find(filesystem_id); | |
123 if (mtp_device_map_it == mtp_device_map_.end()) | |
90 return NULL; | 124 return NULL; |
91 } | |
92 | 125 |
93 const base::FilePath::StringType& device_location = device_path.value(); | 126 const base::FilePath::StringType& device_location = |
94 DCHECK(!device_location.empty()); | 127 mtp_device_map_it->second.first; |
95 AsyncDelegateMap::const_iterator it = | 128 const bool read_only = mtp_device_map_it->second.second; |
96 async_delegate_map_.find(device_location); | 129 const AsyncDelegateKey key = GetAsyncDelegateKey(device_location, read_only); |
97 return (it != async_delegate_map_.end()) ? it->second : NULL; | 130 |
131 AsyncDelegateMap::const_iterator async_delegate_map_it = | |
132 async_delegate_map_.find(key); | |
133 return (async_delegate_map_it != async_delegate_map_.end()) | |
134 ? async_delegate_map_it->second | |
135 : NULL; | |
98 } | 136 } |
99 | 137 |
100 MTPDeviceMapService::MTPDeviceMapService() { | 138 MTPDeviceMapService::MTPDeviceMapService() { |
101 } | 139 } |
102 | 140 |
103 MTPDeviceMapService::~MTPDeviceMapService() { | 141 MTPDeviceMapService::~MTPDeviceMapService() { |
104 DCHECK(mtp_device_usage_map_.empty()); | 142 DCHECK(mtp_device_usage_map_.empty()); |
105 } | 143 } |
OLD | NEW |