Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(80)

Side by Side Diff: chrome/browser/media_gallery/media_storage_util.cc

Issue 10871049: Connect MediaFileSystemRegistry with MediaGalleriesPreferences (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 // chrome::MediaStorageUtil implementation. 5 // chrome::MediaStorageUtil implementation.
6 6
7 #include "chrome/browser/media_gallery/media_storage_util.h" 7 #include "chrome/browser/media_gallery/media_storage_util.h"
8 8
9 #include <vector> 9 #include <vector>
10 10
(...skipping 15 matching lines...) Expand all
26 // Prefix constants for different device id spaces. 26 // Prefix constants for different device id spaces.
27 const char kRemovableMassStorageWithDCIMPrefix[] = "dcim:"; 27 const char kRemovableMassStorageWithDCIMPrefix[] = "dcim:";
28 const char kRemovableMassStorageNoDCIMPrefix[] = "nodcim:"; 28 const char kRemovableMassStorageNoDCIMPrefix[] = "nodcim:";
29 const char kFixedMassStoragePrefix[] = "path:"; 29 const char kFixedMassStoragePrefix[] = "path:";
30 const char kMtpPtpPrefix[] = "mtp:"; 30 const char kMtpPtpPrefix[] = "mtp:";
31 31
32 static void (*g_test_get_device_info_from_path_function)( 32 static void (*g_test_get_device_info_from_path_function)(
33 const FilePath& path, std::string* device_id, string16* device_name, 33 const FilePath& path, std::string* device_id, string16* device_name,
34 FilePath* relative_path) = NULL; 34 FilePath* relative_path) = NULL;
35 35
36 void EmptyPathIsFalseCallback(const MediaStorageUtil::BoolCallback& callback,
37 FilePath path) {
38 callback.Run(!path.empty());
39 }
40
41 void ValidatePathOnFileThread( 36 void ValidatePathOnFileThread(
42 const FilePath& path, const MediaStorageUtil::FilePathCallback& callback) { 37 const FilePath& path, const MediaStorageUtil::BoolCallback& callback) {
43 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 38 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
44 FilePath result;
45 if (file_util::PathExists(path))
46 result = path;
47 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 39 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
48 base::Bind(callback, path)); 40 base::Bind(callback, file_util::PathExists(path)));
49 } 41 }
50 42
51 FilePath::StringType FindRemovableStorageLocationById( 43 FilePath::StringType FindRemovableStorageLocationById(
52 const std::string& device_id) { 44 const std::string& device_id) {
53 RemovableStorageInfo media_devices = 45 RemovableStorageInfo media_devices =
54 SystemMonitor::Get()->GetAttachedRemovableStorage(); 46 SystemMonitor::Get()->GetAttachedRemovableStorage();
55 for (RemovableStorageInfo::const_iterator it = media_devices.begin(); 47 for (RemovableStorageInfo::const_iterator it = media_devices.begin();
56 it != media_devices.end(); 48 it != media_devices.end();
57 ++it) { 49 ++it) {
58 if (it->device_id == device_id) 50 if (it->device_id == device_id)
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
116 (type == REMOVABLE_MASS_STORAGE_WITH_DCIM || type == MTP_OR_PTP); 108 (type == REMOVABLE_MASS_STORAGE_WITH_DCIM || type == MTP_OR_PTP);
117 } 109 }
118 110
119 // static 111 // static
120 bool MediaStorageUtil::IsRemovableDevice(const std::string& device_id) { 112 bool MediaStorageUtil::IsRemovableDevice(const std::string& device_id) {
121 Type type; 113 Type type;
122 return CrackDeviceId(device_id, &type, NULL) && type != FIXED_MASS_STORAGE; 114 return CrackDeviceId(device_id, &type, NULL) && type != FIXED_MASS_STORAGE;
123 } 115 }
124 116
125 // static 117 // static
118 bool MediaStorageUtil::IsMassStorageDevice(const std::string& device_id) {
119 Type type;
120 return CrackDeviceId(device_id, &type, NULL) && type != MTP_OR_PTP;
121 }
122
123 // static
126 void MediaStorageUtil::IsDeviceAttached(const std::string& device_id, 124 void MediaStorageUtil::IsDeviceAttached(const std::string& device_id,
127 const BoolCallback& callback) { 125 const BoolCallback& callback) {
128 Type type; 126 Type type;
129 std::string unique_id; 127 std::string unique_id;
130 if (!CrackDeviceId(device_id, &type, &unique_id)) { 128 if (!CrackDeviceId(device_id, &type, &unique_id)) {
131 callback.Run(false); 129 callback.Run(false);
132 return; 130 return;
133 } 131 }
134 132
135 switch (type) { 133 switch (type) {
136 case MTP_OR_PTP: // Fall through. 134 case MTP_OR_PTP: // Fall through.
137 case REMOVABLE_MASS_STORAGE_WITH_DCIM: // Fall through. 135 case REMOVABLE_MASS_STORAGE_WITH_DCIM: // Fall through.
138 case REMOVABLE_MASS_STORAGE_NO_DCIM: 136 case REMOVABLE_MASS_STORAGE_NO_DCIM:
139 // We should be able to find removable storage in SystemMonitor. 137 // We should be able to find removable storage in SystemMonitor.
140 callback.Run(!FindRemovableStorageLocationById(device_id).empty()); 138 callback.Run(!FindRemovableStorageLocationById(device_id).empty());
141 break; 139 break;
142 case FIXED_MASS_STORAGE: 140 case FIXED_MASS_STORAGE:
143 // For this type, the unique_id is the path. 141 // For this type, the unique_id is the path.
144 BrowserThread::PostTask( 142 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
145 BrowserThread::FILE, FROM_HERE, 143 base::Bind(&ValidatePathOnFileThread,
146 base::Bind(&ValidatePathOnFileThread, 144 FilePath::FromUTF8Unsafe(unique_id),
147 FilePath::FromUTF8Unsafe(unique_id), 145 callback));
148 base::Bind(&EmptyPathIsFalseCallback, callback)));
149 break; 146 break;
150 } 147 }
151 NOTREACHED(); 148 NOTREACHED();
152 callback.Run(false); 149 callback.Run(false);
153 } 150 }
154 151
155 // static 152 // static
156 void MediaStorageUtil::GetDeviceInfoFromPath(const FilePath& path, 153 void MediaStorageUtil::GetDeviceInfoFromPath(const FilePath& path,
157 std::string* device_id, 154 std::string* device_id,
158 string16* device_name, 155 string16* device_name,
159 FilePath* relative_path) { 156 FilePath* relative_path) {
160 if (g_test_get_device_info_from_path_function) { 157 if (g_test_get_device_info_from_path_function) {
161 g_test_get_device_info_from_path_function(path, device_id, device_name, 158 g_test_get_device_info_from_path_function(path, device_id, device_name,
162 relative_path); 159 relative_path);
163 } else { 160 } else {
164 GetDeviceInfoFromPathImpl(path, device_id, device_name, relative_path); 161 GetDeviceInfoFromPathImpl(path, device_id, device_name, relative_path);
165 } 162 }
166 } 163 }
167 164
168 // static 165 // static
169 void MediaStorageUtil::FindDevicePathById(const std::string& device_id, 166 FilePath MediaStorageUtil::FindDevicePathById(const std::string& device_id) {
170 const FilePathCallback& callback) {
171 Type type; 167 Type type;
172 std::string unique_id; 168 std::string unique_id;
173 if (!CrackDeviceId(device_id, &type, &unique_id)) 169 if (!CrackDeviceId(device_id, &type, &unique_id))
174 callback.Run(FilePath()); 170 return FilePath();
175 171
176 switch (type) { 172 switch (type) {
177 case MTP_OR_PTP: 173 case MTP_OR_PTP:
178 // TODO(kmadhusu) We may want to return the MTP device location here. 174 // TODO(kmadhusu) We may want to return the MTP device location here.
179 callback.Run(FilePath()); 175 return FilePath();
180 break;
181 case REMOVABLE_MASS_STORAGE_WITH_DCIM: // Fall through. 176 case REMOVABLE_MASS_STORAGE_WITH_DCIM: // Fall through.
182 case REMOVABLE_MASS_STORAGE_NO_DCIM: 177 case REMOVABLE_MASS_STORAGE_NO_DCIM:
183 callback.Run(FilePath(FindRemovableStorageLocationById(device_id))); 178 return FilePath(FindRemovableStorageLocationById(device_id));
184 break;
185 case FIXED_MASS_STORAGE: 179 case FIXED_MASS_STORAGE:
186 // For this type, the unique_id is the path. 180 // For this type, the unique_id is the path.
187 BrowserThread::PostTask( 181 return FilePath::FromUTF8Unsafe(unique_id);
188 BrowserThread::FILE, FROM_HERE,
189 base::Bind(&ValidatePathOnFileThread,
190 FilePath::FromUTF8Unsafe(unique_id), callback));
191 break;
192 } 182 }
183
193 NOTREACHED(); 184 NOTREACHED();
194 callback.Run(FilePath()); 185 return FilePath();
195 } 186 }
196 187
197 // static 188 // static
198 void MediaStorageUtil::SetGetDeviceInfoFromPathFunctionForTesting( 189 void MediaStorageUtil::SetGetDeviceInfoFromPathFunctionForTesting(
199 GetDeviceInfoFromPathFunction function) { 190 GetDeviceInfoFromPathFunction function) {
200 g_test_get_device_info_from_path_function = function; 191 g_test_get_device_info_from_path_function = function;
201 } 192 }
202 193
203 MediaStorageUtil::MediaStorageUtil() {} 194 MediaStorageUtil::MediaStorageUtil() {}
204 195
(...skipping 12 matching lines...) Expand all
217 if (device_id) 208 if (device_id)
218 *device_id = MakeDeviceId(FIXED_MASS_STORAGE, path.AsUTF8Unsafe()); 209 *device_id = MakeDeviceId(FIXED_MASS_STORAGE, path.AsUTF8Unsafe());
219 if (device_name) 210 if (device_name)
220 *device_name = path.BaseName().LossyDisplayName(); 211 *device_name = path.BaseName().LossyDisplayName();
221 if (relative_path) 212 if (relative_path)
222 *relative_path = FilePath(); 213 *relative_path = FilePath();
223 } 214 }
224 #endif 215 #endif
225 216
226 } // namespace chrome 217 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698