| 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/media_galleries_preferences.h" | 5 #include "chrome/browser/media_galleries/media_galleries_preferences.h" |
| 6 | 6 |
| 7 #include "base/base_paths_posix.h" | 7 #include "base/base_paths_posix.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/i18n/time_formatting.h" | 9 #include "base/i18n/time_formatting.h" |
| 10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| (...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 if (dict->GetString(kMediaGalleryIdKey, &string_id) && | 238 if (dict->GetString(kMediaGalleryIdKey, &string_id) && |
| 239 base::StringToUint64(string_id, &out_permission->pref_id) && | 239 base::StringToUint64(string_id, &out_permission->pref_id) && |
| 240 dict->GetBoolean(kMediaGalleryHasPermissionKey, | 240 dict->GetBoolean(kMediaGalleryHasPermissionKey, |
| 241 &out_permission->has_permission)) { | 241 &out_permission->has_permission)) { |
| 242 return true; | 242 return true; |
| 243 } | 243 } |
| 244 NOTREACHED(); | 244 NOTREACHED(); |
| 245 return false; | 245 return false; |
| 246 } | 246 } |
| 247 | 247 |
| 248 string16 GetDisplayNameForDevice(uint64 storage_size_in_bytes, | |
| 249 const string16& name) { | |
| 250 DCHECK(!name.empty()); | |
| 251 return (storage_size_in_bytes == 0) ? | |
| 252 name : ui::FormatBytes(storage_size_in_bytes) + ASCIIToUTF16(" ") + name; | |
| 253 } | |
| 254 | |
| 255 // For a device with |device_name| and a relative path |sub_folder|, construct | |
| 256 // a display name. If |sub_folder| is empty, then just return |device_name|. | |
| 257 string16 GetDisplayNameForSubFolder(const string16& device_name, | |
| 258 const base::FilePath& sub_folder) { | |
| 259 if (sub_folder.empty()) | |
| 260 return device_name; | |
| 261 return (sub_folder.BaseName().LossyDisplayName() + | |
| 262 ASCIIToUTF16(" - ") + | |
| 263 device_name); | |
| 264 } | |
| 265 | |
| 266 string16 GetFullProductName(const string16& vendor_name, | |
| 267 const string16& model_name) { | |
| 268 if (vendor_name.empty() && model_name.empty()) | |
| 269 return string16(); | |
| 270 | |
| 271 string16 product_name; | |
| 272 if (vendor_name.empty()) | |
| 273 product_name = model_name; | |
| 274 else if (model_name.empty()) | |
| 275 product_name = vendor_name; | |
| 276 else if (!vendor_name.empty() && !model_name.empty()) | |
| 277 product_name = vendor_name + UTF8ToUTF16(", ") + model_name; | |
| 278 | |
| 279 return product_name; | |
| 280 } | |
| 281 | |
| 282 } // namespace | 248 } // namespace |
| 283 | 249 |
| 284 MediaGalleryPrefInfo::MediaGalleryPrefInfo() | 250 MediaGalleryPrefInfo::MediaGalleryPrefInfo() |
| 285 : pref_id(kInvalidMediaGalleryPrefId), | 251 : pref_id(kInvalidMediaGalleryPrefId), |
| 286 type(kInvalidType), | 252 type(kInvalidType), |
| 287 total_size_in_bytes(0), | 253 total_size_in_bytes(0), |
| 288 volume_metadata_valid(false), | 254 volume_metadata_valid(false), |
| 289 prefs_version(0) { | 255 prefs_version(0) { |
| 290 } | 256 } |
| 291 | 257 |
| 292 MediaGalleryPrefInfo::~MediaGalleryPrefInfo() {} | 258 MediaGalleryPrefInfo::~MediaGalleryPrefInfo() {} |
| 293 | 259 |
| 294 base::FilePath MediaGalleryPrefInfo::AbsolutePath() const { | 260 base::FilePath MediaGalleryPrefInfo::AbsolutePath() const { |
| 295 base::FilePath base_path = MediaStorageUtil::FindDevicePathById(device_id); | 261 base::FilePath base_path = MediaStorageUtil::FindDevicePathById(device_id); |
| 296 DCHECK(!path.IsAbsolute()); | 262 DCHECK(!path.IsAbsolute()); |
| 297 return base_path.empty() ? base_path : base_path.Append(path); | 263 return base_path.empty() ? base_path : base_path.Append(path); |
| 298 } | 264 } |
| 299 | 265 |
| 300 string16 MediaGalleryPrefInfo::GetGalleryDisplayName() const { | |
| 301 if (!StorageInfo::IsRemovableDevice(device_id)) { | |
| 302 // For fixed storage, the default name is the fully qualified directory | |
| 303 // name, or in the case of a root directory, the root directory name. | |
| 304 // Exception: ChromeOS -- the full pathname isn't visible there, so only | |
| 305 // the directory name is used. | |
| 306 base::FilePath path = AbsolutePath(); | |
| 307 if (!display_name.empty()) | |
| 308 return display_name; | |
| 309 | |
| 310 #if defined(OS_CHROMEOS) | |
| 311 // See chrome/browser/chromeos/fileapi/file_system_backend.cc | |
| 312 base::FilePath home_path; | |
| 313 if (PathService::Get(base::DIR_HOME, &home_path)) { | |
| 314 home_path = home_path.AppendASCII("Downloads"); | |
| 315 base::FilePath relative; | |
| 316 if (home_path.AppendRelativePath(path, &relative)) | |
| 317 return relative.LossyDisplayName(); | |
| 318 } | |
| 319 return path.BaseName().LossyDisplayName(); | |
| 320 #else | |
| 321 return path.LossyDisplayName(); | |
| 322 #endif | |
| 323 } | |
| 324 | |
| 325 string16 name = display_name; | |
| 326 if (name.empty()) | |
| 327 name = volume_label; | |
| 328 if (name.empty()) | |
| 329 name = GetFullProductName(vendor_name, model_name); | |
| 330 if (name.empty()) | |
| 331 name = l10n_util::GetStringUTF16(IDS_MEDIA_GALLERIES_UNLABELED_DEVICE); | |
| 332 | |
| 333 name = GetDisplayNameForDevice(total_size_in_bytes, name); | |
| 334 | |
| 335 if (!path.empty()) | |
| 336 name = GetDisplayNameForSubFolder(name, path); | |
| 337 | |
| 338 return name; | |
| 339 } | |
| 340 | |
| 341 string16 MediaGalleryPrefInfo::GetGalleryTooltip() const { | 266 string16 MediaGalleryPrefInfo::GetGalleryTooltip() const { |
| 342 return AbsolutePath().LossyDisplayName(); | 267 return AbsolutePath().LossyDisplayName(); |
| 343 } | 268 } |
| 344 | 269 |
| 345 string16 MediaGalleryPrefInfo::GetGalleryAdditionalDetails() const { | 270 string16 MediaGalleryPrefInfo::GetGalleryAdditionalDetails() const { |
| 346 string16 attached; | 271 string16 attached; |
| 347 if (StorageInfo::IsRemovableDevice(device_id)) { | 272 if (StorageInfo::IsRemovableDevice(device_id)) { |
| 348 if (MediaStorageUtil::IsRemovableStorageAttached(device_id)) { | 273 if (MediaStorageUtil::IsRemovableStorageAttached(device_id)) { |
| 349 attached = l10n_util::GetStringUTF16( | 274 attached = l10n_util::GetStringUTF16( |
| 350 IDS_MEDIA_GALLERIES_DIALOG_DEVICE_ATTACHED); | 275 IDS_MEDIA_GALLERIES_DIALOG_DEVICE_ATTACHED); |
| (...skipping 750 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1101 if (extension_prefs_for_testing_) | 1026 if (extension_prefs_for_testing_) |
| 1102 return extension_prefs_for_testing_; | 1027 return extension_prefs_for_testing_; |
| 1103 return extensions::ExtensionPrefs::Get(profile_); | 1028 return extensions::ExtensionPrefs::Get(profile_); |
| 1104 } | 1029 } |
| 1105 | 1030 |
| 1106 void MediaGalleriesPreferences::SetExtensionPrefsForTesting( | 1031 void MediaGalleriesPreferences::SetExtensionPrefsForTesting( |
| 1107 extensions::ExtensionPrefs* extension_prefs) { | 1032 extensions::ExtensionPrefs* extension_prefs) { |
| 1108 DCHECK(IsInitialized()); | 1033 DCHECK(IsInitialized()); |
| 1109 extension_prefs_for_testing_ = extension_prefs; | 1034 extension_prefs_for_testing_ = extension_prefs; |
| 1110 } | 1035 } |
| OLD | NEW |